bytecodealliance / StarlingMonkey

The StarlingMonkey JS runtime
Apache License 2.0
96 stars 14 forks source link

I want to run a js file that I wrote myself #153

Open Guo-yyds opened 3 weeks ago

Guo-yyds commented 3 weeks ago

I have two questions: Question one: What is the relationship between this and spidermonkey? What is the structure of this project that I see mainly in the test section consisting of wasmtime + wasmtools? What does this have to do with spidermonkey? Question two: I want to run a js file that I wrote myself. How do I do it? tests is too confusing

drogus commented 3 weeks ago

What is the relationship between this and spidermonkey?

This project uses SpiderMonke under the hood, but adds necessary bindings and glue code to allow run SpiderMonkey as a WASI component.

I want to run a js file that I wrote myself. How do I do it? tests is too confusing

The easiest way is to use componentize.sh to merge the js file into WASM, it's described at the end of the usage section

Guo-yyds commented 3 weeks ago

image What does this code in componentize.sh mean?

I try this with the following code:

./componentize.sh my.js
wasmtime my.wasm

my.js

console.log("hellow world")

but it is error image

In the last question, I want to ask SpiderMoinkey to print intermediate garbage collection information. Is it possible to solve this problem by adding parameters?

drogus commented 3 weeks ago

What does this code in componentize.sh mean?

Wizer is a tool to make the JS runtime instantiation quicker. It starts the runtime and dumps the memory after start, so when it's loaded to WASM it doesn't have to be initialized. From what I read it lowers the time to start SpiderMonkey in WASM from 10-20 miliseconds to a few microseconds.

but it is error

What version of wasmtime binary do you have? It looks like it doesn't provide implementation for some of the WASI APIs 🤔

In the last question, I want to ask SpiderMoinkey to print intermediate garbage collection information. Is it possible to solve this problem by adding parameters?

For that I'm not sure. You could probably add your own C++ functions and bind them to JavaScript APIs, but I'm not sure if there's something like that already available or if you have to write your own implementation

tschneidereit commented 3 weeks ago

@Guo-yyds the resulting component imports a few different WASI interfaces, not all of which are enabled by default by wasmtime. If you change wasmtime ghn.wasm to wasmtime -S http ghn.wasm, things should work. (Though nothing would actually happen, because your JS file already has fully executed during the componentization step.)

Guo-yyds commented 3 weeks ago

I also want to ask why the componentization phase executes js code?

image

Is the project structured like this?

tschneidereit commented 3 weeks ago

Yes, that's roughly correct.

As @drogus says, executing the top-level script during componentization makes it so that when processing an event later on, the script doesn't have to be run over and over again.

If you don't want this to happen, you can call componentize.sh without passing in a JS file. You can then run wasmtime -S http ghn.wasm my.js, and it should print hello world

Guo-yyds commented 3 weeks ago

image If you do not input, it does not seem to be possible

cfallin commented 3 weeks ago

@Guo-yyds, I don't have an answer to your question, but would you mind posting plain text rather than screenshots? I am sure you mean well, but screenshots are a fairly inaccessible way to present text for several reasons, which I described in this comment previously. Thanks!

Guo-yyds commented 3 weeks ago

componentize.txt I want to call componentize.sh without passing in a JS file, but it is error

./componentize my.js
tschneidereit commented 3 weeks ago

@Guo-yyds thank you for trying to heed Chris' advice! ❤️ Unfortunately the .txt file is even harder to use on GitHub than an image, because it always automatically downloads, and then one has to open it manually. Could you instead past the contents in a block here? E.g., if you want to share a shell script, you could do so with this kind of block:

```sh
echo "hello world"


In this particular case though, it'd be even better if you could just [link to the file in this repository](https://github.com/bytecodealliance/StarlingMonkey/blob/main/componentize.sh), or ideally even to the [exact part you mean](https://github.com/bytecodealliance/StarlingMonkey/blob/main/componentize.sh#L11-L16) (which you can do by clicking on the line number, and then Ctrl/Cmd-clicking a second line number to highlight a range of lines.)
tschneidereit commented 3 weeks ago

As for your actual message: the shell input you mentioned does pass a JS file, so it wouldn't work.

This should I think work:

./componentize.sh -o my-component.wasm