How to use the bindings/wasi.fd_read function in bindings

To help you get started, we’ve selected a few bindings examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jedisct1 / as-wasi / assembly / wasa.ts View on Github external
static read(
    fd: Descriptor,
    data: Array = [],
    chunk_size: usize = 4096
  ): Array | null {
    let data_partial_len = chunk_size;
    let data_partial = changetype(new ArrayBuffer(data_partial_len));
    let iov = changetype(new ArrayBuffer(2 * sizeof()));
    store(iov, data_partial);
    store(iov + sizeof(), data_partial_len);
    let read_ptr = changetype(new ArrayBuffer(sizeof()));
    fd_read(fd, iov, 1, read_ptr);
    let read = load(read_ptr);
    if (read > 0) {
      for (let i: usize = 0; i < read; i++) {
        data.push(load(data_partial + i));
      }
    }
    if (read <= 0) {
      return null;
    }
    return data;
  }
github jedisct1 / as-wasi / assembly / wasa.ts View on Github external
static readAll(
    fd: Descriptor,
    data: Array = [],
    chunk_size: usize = 4096
  ): Array | null {
    let data_partial_len = chunk_size;
    let data_partial = changetype(new ArrayBuffer(data_partial_len));
    let iov = changetype(new ArrayBuffer(2 * sizeof()));
    store(iov, data_partial);
    store(iov + sizeof(), data_partial_len);
    let read_ptr = changetype(new ArrayBuffer(sizeof()));
    let read: usize = 0;
    for (; ;) {
      if (fd_read(fd, iov, 1, read_ptr) != errno.SUCCESS) {
        break;
      }
      read = load(read_ptr);
      if (read <= 0) {
        break;
      }
      for (let i: usize = 0; i < read; i++) {
        data.push(load(data_partial + i));
      }
    }
    if (read < 0) {
      return null;
    }
    return data;
  }