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

Execute wasm file in NodeJS #161

Closed
Gregoirevda opened this issue May 22, 2019 · 13 comments
Closed

Execute wasm file in NodeJS #161

Gregoirevda opened this issue May 22, 2019 · 13 comments

Comments

@Gregoirevda
Copy link
Contributor

I'd like to expand the tutorial on how to run the wasm file in NodeJS after running it with wasmtime.

Normally you can simply run WebAssembly.instantiate(buf), but here the demo.c contains arguments input.txt and output.txt.
The WebAssembly requires a wasi_unstable object and within that a fd_prestat_get function, but I don't know how it should look like...

index.js

const fs = require('fs');
const buf = fs.readFileSync('./demo.wasm');

(async () => {
  const res = await WebAssembly.instantiate(buf, {wasi_unstable: {fd_prestat_get: () => {}}});

  console.log(res.instance);
})();
@bjorn3
Copy link
Contributor

bjorn3 commented May 22, 2019

https://github.com/devsnek/node-wasi may be what you want.

@Gregoirevda
Copy link
Contributor Author

Thanks @bjorn3

I managed to run the wasm package, but get into the first error defined in the demo.c file
usage: (null) <from> <to>

You know how to pass params to wasm main function?

const WASI = require('wasi');
const fs = require('fs');

const buf = fs.readFileSync('./demo.wasm');

const wasi = new WASI({
  // preopenDirectories: { '.': '.' },
});

(async () => {
  let res = await WebAssembly.instantiate(buf, {
    wasi_unstable: wasi.exports
  });
  wasi.setMemory(res.instance.exports.memory);

  res.instance.exports._start("./input.txt", "output.txt");
})()

@bjorn3
Copy link
Contributor

bjorn3 commented May 22, 2019

https://github.com/devsnek/node-wasi/blob/68902020ce28210ab1fc12dfaec090ef82c44263/index.js#L507

It seems like you can use new WASI({ args: ["arg1", "arg2"] }) to pass args.

@Gregoirevda
Copy link
Contributor Author

It's overriding the 1st arg, so I tried passing the wasm executable path and the 2 other args
args: ["fullpath/demo.wasm", "input.txt", "output.txt"]

but getting

TypeError: wasm function signature contains illegal type
    at openat (wasm-function[24]:318)
    at open (wasm-function[26]:114)
    at main (wasm-function[15]:209)
    at _start (wasm-function[14]:405)
    at /wasi/index.js:19:24

Line 19 res.instance.exports._start()

@devsnek
Copy link

devsnek commented May 22, 2019

@Gregoirevda you'll need to run it with node --experimental-wasm-bigint

@Gregoirevda
Copy link
Contributor Author

I managed to make it work with path.resolve

js
const WASI = require('wasi');
const fs = require('fs');
const path = require('path');

const buf = fs.readFileSync('./demo.wasm');

const wasi = new WASI({
  preopenDirectories: { '.': '.' },
  args: [path.resolve(__dirname, "test.wasm"), path.resolve(__dirname, 'test.txt'), path.resolve(__dirname, 'output.txt')]
});

(async () => {
  let res = await WebAssembly.instantiate(buf, {
    wasi_unstable: wasi.exports
  });

  wasi.setMemory(res.instance.exports.memory);
  res.instance.exports._start();
})()

I'm getting the next error error opening input /Users/gvanderauw001/dev/workshop/wasi/test.txt: Capabilities insufficient

Is there a way to give capability with WASI like
wasmtime --dir=. --dir=/tmp demo.wasm test.txt /tmp/somewhere.txt?

@bjorn3
Copy link
Contributor

bjorn3 commented May 22, 2019

Is there a way to give capability with WASI like
wasmtime --dir=. --dir=/tmp demo.wasm test.txt /tmp/somewhere.txt?

That's what preopenDirectories does: preopenDirectories: { '.': '.', '/tmp': '/tmp' },

@Gregoirevda
Copy link
Contributor Author

@bjorn3 both input.txt and output.txt are in the same folder. I tried passing ., ./, {__dirname} without success...

@Gregoirevda
Copy link
Contributor Author

What are key:value used for?

@devsnek
Copy link

devsnek commented May 22, 2019

pathForWasi: realPath

e.g. if you wanted to expose ./project/sandbox as / to wasi you would do '/': './project/sandbox'

@Gregoirevda
Copy link
Contributor Author

Thanks @devsnek
I went back to

const wasi = new WASI({
  preopenDirectories: { '.': '.' },
  args: [__dirname, 'test.txt', 'output.txt']
});

instead of path.resolve(__dirname, 'file.txt'), which fixed the permission error.

I also went from node 11.x to node 12.3.0 and there only I could have access to the --experimental-wasm-bigint flag, which deleted the typeError.

Arguments are now red correctly, files are opened, but the C code fails at writing.
I'm getting: write error: Bad file descriptor

From this file. But it ofcourse works with wasmtime.

I tried changing the out file to int out = open(argv[2], O_CREAT | O_RDWR, 0660); or other flags with no luck.

@Gregoirevda
Copy link
Contributor Author

Same error with stdio

kubkon added a commit that referenced this issue Nov 7, 2019
* Treat all warnings as errors in the CI

* Build only in release mode
@bjorn3
Copy link
Contributor

bjorn3 commented Feb 3, 2021

I think this can be closed. It isn't related to Cranelift or Wasmtime.

@cfallin cfallin closed this as completed Feb 3, 2021
grishasobol pushed a commit to grishasobol/wasmtime that referenced this issue Nov 29, 2021
pchickey added a commit to pchickey/wasmtime that referenced this issue May 12, 2023
We have only ever used Unknown for the stdio streams, and I don't expect
us to use it for anything else in the future, so rename it.

Set the returned filetype to character device: Closes bytecodealliance#146.

Also, fix some warnings.
mooori pushed a commit to mooori/wasmtime that referenced this issue Dec 20, 2023
Add support for WASM Global Variables
dhil added a commit to dhil/wasmtime that referenced this issue Apr 29, 2024
avanhatt pushed a commit to wellesley-prog-sys/wasmtime that referenced this issue Oct 18, 2024
Verify integer min/max.

These rules are a great demonstration of the new priority support, since
the correctness of the `cmp_and_choose` helper rule for `fits_in_64`
relies on the higher priority 16-bit case.

Updates bytecodealliance#128
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

4 participants