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

Unable to get tokens or ast from wast code #799

Open
AchalaSB opened this issue Sep 1, 2020 · 3 comments
Open

Unable to get tokens or ast from wast code #799

AchalaSB opened this issue Sep 1, 2020 · 3 comments

Comments

@AchalaSB
Copy link

AchalaSB commented Sep 1, 2020

Describe the bug
When I try to get at form wat code, I'm getting errors

To Reproduce
Steps to reproduce the behavior:

  1. My rust code looks like
#[no_mangle]
pub extern fn sum(x: i64, y: i64) -> i64 {
   x + y
}
fn main() {}
  1. Compile to wasm using cargo build --release --target=wasm32-unknown-unknown
  2. Convert it into wat using wasm2wat main.wasm -o main.wat
  3. 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?

@AchalaSB AchalaSB changed the title Unable to get ast from wast code Unable to get tokens or ast from wast code Sep 1, 2020
@AchalaSB
Copy link
Author

AchalaSB commented Sep 2, 2020

@xtuc is there any specific grammar the library looking for? how can convert my wasm/wast code ?

@chehx
Copy link

chehx commented Jan 23, 2021

Hello, I meet the same question!

Did you figure this problem out?

@xtuc
Copy link
Owner

xtuc commented Jan 24, 2021

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);

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

3 participants