Repository: node
Issue: WASI preview 1: `fd_readdir` ignores `cookie` argument
Labels: wasi,uvwasi-agenda

### Version

v19.8.1

### Platform

Darwin uhyo 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

### Subsystem

_No response_

### What steps will reproduce the bug?

Create a WASM binary that calls the [fd_readdir](https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-fd_readdirfd-fd-buf-pointeru8-buf_len-size-cookie-dircookie---resultsize-errno) function with a non-zero `cookie` argument.

Reproduction repository: https://github.com/uhyo/rust-wasi-readdir

<details>
<summary>Example Rust code that demonstrates the problem</summary>

```rust
use std::{env, fs, io};

fn main() -> io::Result<()> {
    println!("Calling fs::read_dir");
    for entry in fs::read_dir(env::current_dir()?)? {
        let entry = entry?;
        println!("Entry {}", entry.path().display());
    }
    Ok(())
}
```

</details>

### How often does it reproduce? Is there a required condition?

always

### What is the expected behavior? Why is that the expected behavior?

A typical use case of the `fd_readdir` function is to repeatedly call it until the caller can retrieve all entries in a directory. The `cookie` parameter is used as a cursor so that the function can know where in the given directory to continue from. The `fd_readdir` should respect the `cookie` parameter. (Read more in the reproduction repository's README)

### What do you see instead?

Actually, the `fd_readdir` function ignores the `cookie` parameters and always returns the same set of entries. The caller falls into an infinite loop.

<details>
<summary>Log of `fd_readdir` calls and how Node.js responded to them</summary>

```
fd_readdir(4,1065824,128,0,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
Entry /run-node-wasi.mjs
Entry /Cargo.toml
Entry /run-wasmtime-wasi.sh
fd_readdir(4,1065824,128,2,1047548) = 0
offset = 0 {
  d_next: 0n,
  d_ino: 0n,
  d_namlen: 17,
  d_type: 4,
  name: 'run-node-wasi.mjs'
}
offset = 41 { d_next: 1n, d_ino: 0n, d_namlen: 10, d_type: 4, name: 'Cargo.toml' }
offset = 75 {
  d_next: 2n,
  d_ino: 0n,
  d_namlen: 20,
  d_type: 4,
  name: 'run-wasmtime-wasi.sh'
}
(offset = 119)
```

</details>

### Additional information

_No response_