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

How to use this project in node.js? #861

Open
chehx opened this issue Jan 13, 2021 · 5 comments
Open

How to use this project in node.js? #861

chehx opened this issue Jan 13, 2021 · 5 comments

Comments

@chehx
Copy link

chehx commented Jan 13, 2021

As the title claims, I meet some trouble with running this project in node.js.

`(node:12960) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
c:\Users\parserTest.js:3
import { decode } from "./node_modules/@webassemblyjs/wasm-parser";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`

I also try to use this project in an HTML file, but it fails too.

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

I try many times and read the issues and documents to solve this problem, but I failed.

Really hope you can help me to figure it out!

Thanks!

@chehx
Copy link
Author

chehx commented Jan 13, 2021

After I add "type": "module", in the package.json file, the new thing happens:

node:26528) ExperimentalWarning: The ESM module loader is experimental. import { decode } from "@webassemblyjs/wasm-parser"; ^^^^^^ SyntaxError: The requested module '@webassemblyjs/wasm-parser' does not provide an export named 'decode' at ModuleJob._instantiate (internal/modules/esm/module_job.js:96:21) at async ModuleJob.run (internal/modules/esm/module_job.js:134:20) at async Loader.import (internal/modules/esm/loader.js:179:24)

And here is the code I used:

`import { decode } from "@webassemblyjs/wasm-parser";

const decoderOpts = {};

const ast = decode("./test.wasm", decoderOpts);`

@xtuc
Copy link
Owner

xtuc commented Jan 24, 2021

The decode is exported by @webassemblyjs/wasm-parser, but it looks like nodejs requires file to be .mjs. Could you please send a change to rename the files?

@chehx
Copy link
Author

chehx commented Jan 25, 2021

The decode is exported by @webassemblyjs/wasm-parser, but it looks like nodejs requires file to be .mjs. Could you please send a change to rename the files?

Many thanks for the help. I have already figured out this problem before. But a problem raises as #799

The WAT text form can't be parsed successfully. I guess maybe the WASM updates some new features.

@xtuc
Copy link
Owner

xtuc commented Jan 25, 2021

@chehx what is the parse failure you are seeing?

@chehx
Copy link
Author

chehx commented Jan 25, 2021

@chehx what is the parse failure you are seeing?

            return new Error("\n" + (0, _helperCodeFrame.codeFrameFromSource)(source, token.loc) + "\n" + "Unexpected token in instruction argument" + ", given " + tokenToString(token));
                   ^

Error:
(module  (type $t0 (func (param i32 i32) (result i32)))  (func $add (type $t0) (param $p0 i32) (param $p1 i32) (result i32)    local.get $p0    local.get $p1    i32.add)  (export "add" (func $add)))
                                                                                                                                                ^^^^^

Unexpected token in instruction argument, given name
    at C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:946:20
    at parseFuncInstrArguments (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:947:12)
    at parseFuncInstr (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1068:37)
    at parseFunc (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1182:23)
    at walk (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1623:22)
    at parseModule (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:858:27)
    at walk (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1635:22)
    at Object.parse (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\grammar.js:1776:15)
    at parse (C:\Users\219019055\Desktop\WASMPARSER\js-parser\node_modules\@webassemblyjs\wast-parser\lib\index.js:33:20)
    at file:///C:/Users/219019055/Desktop/WASMPARSER/js-parser/test.mjs:14:13

Under my test mjs file is:


import { parse } from "@webassemblyjs/wast-parser";

var sourceCode = "(module\
  (type $t0 (func (param i32 i32) (result i32)))\
  (func $add (type $t0) (param $p0 i32) (param $p1 i32) (result i32)\
    local.get $p0\
    local.get $p1\
    i32.add)\
  (export \"add\" (func $add)))"

const ast = parse(sourceCode);

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

2 participants