Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uvwasi #5

Open
preveen-stack opened this issue Sep 5, 2023 · 1 comment
Open

uvwasi #5

preveen-stack opened this issue Sep 5, 2023 · 1 comment

Comments

@preveen-stack
Copy link
Owner

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);
});
@preveen-stack
Copy link
Owner Author

// file.wat

(module
  (import "uvwasi_snapshot_preview1" "fd_read" (func $fd_read (param i32 i32 i32 i32) (result i32)))
  (memory 1)
  (data (i32.const 0) "Hello, world!")
  (export "read_file" (func $read_file))
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant