tree-sitter / tree-sitter-haskell

Haskell grammar for tree-sitter.
MIT License
152 stars 36 forks source link

How do I generate a valid WASM file #70

Closed FoamScience closed 2 years ago

FoamScience commented 2 years ago

We need c++14 here but there is no way to pass --std=c++14 to emcc used by tree-sitter build-wasm The Makefile mentions a patched version of web-tree-sitter I've tried it but does not solve the issue;

When I use the generated WASM file (with npx tree-sitter build-wasm) I get:

bad export type for `_ZNSt3__25ctypeIcE2idE`: undefined

I think that is related to build-wasm not building with c++14 so how was it done here? (Makefile does not specify c++14)

414owen commented 2 years ago

tree-sitter-haskell no longer requires c++14, only c99. See this pr. I've also added all required symbols to the WASM export.

Can you upgrade tree-sitter-haskell?
If not, is it because the npm pakage needs updating? There's an issue for that. https://github.com/tree-sitter/tree-sitter-haskell/issues/50

wenkokke commented 2 years ago

It won't work until the latest tree-sitter is released. Until then, you can use the version in #68.

FoamScience commented 2 years ago

Yes, I've seen the pure-c jump; I asked because I'm having similar problems with WASM files for my own language (was using a c++14 scanner library myself) I think the best option for me is to revert back to C too 😄 Thanks!

414owen commented 2 years ago

C is the most portable language :)

FoamScience commented 2 years ago

OK, I think I'm in a much better position with the C-scanner; I'm generating WASM files for my language with this CI workflow (please note that it's building the pure_c_scanner branch)

Then, to test the generated WASM file:

const Parser = require('web-tree-sitter');

(async () => {
  await Parser.init();
  const parser = new Parser();
  const Lang = await Parser.Language.load(`${__dirname}/languages/foam.wasm`);
  parser.setLanguage(Lang);
  const tree = parser.parse('wm 1;');
  console.log(tree.rootNode.toString());
})();

BTW, no errors with any other WASM file (for cpp, haskell ... etc) just mine :(

Node errors out with:

(node:191242) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'apply' of undefined
    at e.<computed> (/home/elwardi/repos/foamParser/node_modules/web-tree-sitter/tree-sitter.js:1:10542)
    at tree_sitter_foam_external_scanner_scan (<anonymous>:wasm-function[11]:0xa1b)
    at ts_parser_parse_wasm (<anonymous>:wasm-function[214]:0x22e29)
    at Object.Module._ts_parser_parse_wasm (/home/elwardi/repos/foamParser/node_modules/web-tree-sitter/tree-sitter.js:1:20097)
    at Parser.parse (/home/elwardi/repos/foamParser/node_modules/web-tree-sitter/tree-sitter.js:1:35112)
    at /home/elwardi/repos/foamParser/test-wasm.js:8:23

Which suggests something is wrong with tree_sitter_foam_external_scanner_scan which is just a copy of yours.

I know very little about WebAssembly, but my WASM files and Haskell's are not much different so I can't really tell what's the problem. Any idea on what might be the cause of this thing? Any kind of help is much appreciated

414owen commented 2 years ago

@FoamScience On my machine, when I run tree-sitter build-wasm and then run your node file (with paths updated), I get Error: bad export type for _ZNSt3__25ctypeIcE2idE: undefined. Not sure if that's a step forward, or backward...

FoamScience commented 2 years ago

@FoamScience On my machine, when I run tree-sitter build-wasm and then run your node file (with paths updated), I get Error: bad export type for _ZNSt3__25ctypeIcE2idE: undefined. Not sure if that's a step forward, or backward...

Yep, you're using the master branch which still needs C++14, please try pure_c_scanner

414owen commented 2 years ago

@FoamScience I ran your setup in a debugger, and this is where the error occured.

Screenshot from 2022-01-06 15-45-20

I've gone into the surrounding code and I'm 80% this will be fixed by adding memset in the same way as this pr.

There was a point at which it was looking up memset in a dict of the symbols we've marked as exported.

Why exactly it manifests in this way, I have no idea...

@wenkokke did you have to do anything special to get the nice undefined symbol errors?

edit I guess it's slightly different, it can't find memset, rather than _memset

wenkokke commented 2 years ago

Yes, compile web-tree-sitter with the —debug flag.