Skip to content

uvwasi  #5

Description

@preveen-stack
const fs = require('fs');
const uvwasi = require('uvwasi');
const { WASI } = require('wasi');

async function main() {
  const module = await WebAssembly.compile(fs.readFileSync('./file.wasm'));

  const wasi = new WASI({
    preopens: {
      '/sandbox': './sandbox'
    },
    args: [ './file.wasm' ]
  });

  const importObject = {
    uvwasi_snapshot_preview1: uvwasi.snapshot_preview1
  };

  const instance = await WebAssembly.instantiate(module, {
    ...importObject,
    wasi_snapshot_preview1: wasi.wasiImport
  });

  wasi.start(instance);
  
  const { read_file } = instance.exports;

  const buffer = new Uint8Array(256);
  const bytesRead = read_file(3, buffer.byteOffset, buffer.byteLength, 0);
  const text = new TextDecoder().decode(buffer.slice(0, bytesRead));

  console.log(text);
}

main().catch((err) => {
  console.error(err);
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions