Unable to get tokens or ast from wast code
AchalaSB opened this issue · comments
Describe the bug
When I try to get at form wat code, I'm getting errors
To Reproduce
Steps to reproduce the behavior:
- My rust code looks like
#[no_mangle]
pub extern fn sum(x: i64, y: i64) -> i64 {
x + y
}
fn main() {}
- Compile to wasm using cargo build --release --target=wasm32-unknown-unknown
- Convert it into wat using wasm2wat main.wasm -o main.wat
- use the wat file and get ast.
Expected behavior
Should generate the tokens or ast in json form
Actual result
Getting error because of grammar( the generate wast file format is different than what this library expected)
/home/achala/kip-project/varna-cli/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:940
throw function () {
^
Error:
(func $f0 (type $t10) (result i32)
(local $l0 i32) (local $l1 i32)
global.get $g0
i32.const 16
^
i32.sub
local.tee $l0
global.set $g0
local.get $l0
i32.const 0
Unexpected token in instruction argument, given dot
at /home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:941:20
at parseFuncInstrArguments (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:942:12)
at parseFuncInstr (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1057:37)
at parseFunc (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1171:23)
at walk (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1612:22)
at parseModule (/home/achalawasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:853:27)
at walk (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1624:22)
at Object.parse (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1765:15)
at Object.parse (/home/achala/wasm-parser/node_modules/@webassemblyjs/wast-parser/lib/index.js:33:20)
at /home/achala/wasm-parser/index.js:29:28
Have tried wasm-parser
as well, even that didn't work
/home/achala/wasm-parser/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:281
throw new Error("unexpected end");
^
Error: unexpected end
at parseModuleHeader (/home/achala/wasm-parser/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:281:13)
at Object.decode (/home/achala/wasm-parser/node_modules/@webassemblyjs/wasm-parser/lib/decoder.js:1681:3)
at Object.decode (/home/achala/wasm-parser/node_modules/@webassemblyjs/wasm-parser/lib/index.js:248:21)
at /home/achala/wasm-parser/index.js:21:28
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)
How can get wasm/wat file that is fit to this library?
@xtuc is there any specific grammar the library looking for? how can convert my wasm/wast code ?
Hello, I meet the same question!
Did you figure this problem out?
I just to reproduce and got a different error:
/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:80
throw new Error("\n" + (0, _helperCodeFrame.codeFrameFromSource)(source, token.loc) + "Assertion error: expected token of type " + type + ", given " + tokenToString(token));
^
Error:
(module
(type (;0;) (func))
^^^^^^
(type (;1;) (func (param i32)))
(type (;2;) (func (param i32 i32)))
(type (;3;) (func (param i32) (result i32)))
(type (;4;) (func (param i32 i32 i32) (result i32)))
(type (;5;) (func (param i32 i32) (result i32)))
Assertion error: expected token of type closeParen, given comment
at eatTokenOfType (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:80:15)
at walk (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1715:9)
at parseModule (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:858:27)
at walk (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1635:22)
at Object.parse (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/grammar.js:1776:15)
at parse (/tmp/31161/node_modules/@webassemblyjs/wast-parser/lib/index.js:33:20)
at Object.<anonymous> (/tmp/31161/index.js:5:13)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
It looks like we fail to parse the comment in text format.
However, the wasm-parser decoded the file, my test is:
const { decode } = require("@webassemblyjs/wasm-parser");
const { readFileSync } = require("fs")
const binary = readFileSync("target/wasm32-unknown-unknown/release/testff.wasm")
const decoderOpts = {};
const ast = decode(binary, decoderOpts);
console.log(ast);