Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WASM build fails with error #53

Closed
wenkokke opened this issue Dec 31, 2021 · 6 comments
Closed

WASM build fails with error #53

wenkokke opened this issue Dec 31, 2021 · 6 comments

Comments

@wenkokke
Copy link
Contributor

wenkokke commented Dec 31, 2021

Using the wasm generated with npx tree-sitter build-wasm fails with the following error:

Aborted(Assertion failed: undefined symbol `_ZNSt3__24cerrE`. 
    perhaps a side module was not linked in? 
    if this global was expected to arrive from a system library, 
    try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment)
@wenkokke
Copy link
Contributor Author

wenkokke commented Dec 31, 2021

Compiling web-tree-sitter using ASSERTIONS=1, via script/build-wasm --debug, refines the error message to:

Aborted(Assertion failed: undefined symbol `_ZTVN10__cxxabiv117__class_type_infoE`. 
    perhaps a side module was not linked in?
    if this global was expected to arrive from a system library, 
    try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment)

Compiling with EMCC_FORCE_STDLIBS=1 does not make a difference.

@wenkokke
Copy link
Contributor Author

wenkokke commented Dec 31, 2021

The issue is that web-tree-sitter performs dead code elimination, and eliminates all but a select few functions listed in exports.json. The following patch to tree-sitter ensures that the functions used by tree-sitter-haskell are kept:

diff --git a/lib/binding_web/exports.json b/lib/binding_web/exports.json
index e0b3f718..36342e52 100644
--- a/lib/binding_web/exports.json
+++ b/lib/binding_web/exports.json
@@ -3,6 +3,15 @@
   "_free",
   "_malloc",
 
+  "__ZNSt3__24cerrE",
+  "__ZNSt3__25ctypeIcE2idE",
+  "__ZTVN10__cxxabiv117__class_type_infoE",
+  "__ZTVN10__cxxabiv120__si_class_type_infoE",
+  "__ZTVN10__cxxabiv120__function_type_infoE",
+  "__ZTVN10__cxxabiv119__pointer_type_infoE",
+  "__ZTVN10__cxxabiv121__vmi_class_type_infoE",
+  "___cxa_pure_virtual",
+
   "__ZNKSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4copyEPcmm",
   "__ZNKSt3__220__vector_base_commonILb1EE20__throw_length_errorEv",
   "__ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm",

However, I suppose such a patch is unlikely to be accepted. Is the use of these functions really worth the hassle?

@wenkokke
Copy link
Contributor Author

wenkokke commented Dec 31, 2021

With the above patch applied to tree-sitter running a simple parser test results in the following error:

TypeError: Cannot read properties of undefined (reading 'apply')
    at e.<computed> (tree-sitter-haskell/node_modules/web-tree-sitter/tree-sitter.js:1:10543)
    at wasm://wasm/00ea81ae:wasm-function[2590]:0xc09ac
    at e.<computed> (tree-sitter-haskell/node_modules/web-tree-sitter/tree-sitter.js:1:10543)
    at wasm://wasm/00ea81ae:wasm-function[2962]:0xc55b5
    at e.<computed> (tree-sitter-haskell/node_modules/web-tree-sitter/tree-sitter.js:1:10543)
    at wasm://wasm/00ea81ae:wasm-function[2961]:0xc55a8
    at e.<computed> (tree-sitter-haskell/node_modules/web-tree-sitter/tree-sitter.js:1:10543)
    at wasm://wasm/00ea81ae:wasm-function[2939]:0xc5468
    at e.<computed> (tree-sitter-haskell/node_modules/web-tree-sitter/tree-sitter.js:1:10543)
    at wasm://wasm/00ea81ae:wasm-function[2583]:0xc089a

@wenkokke
Copy link
Contributor Author

wenkokke commented Dec 31, 2021

To help reproducing these errors, here is a simple script which uses the generated wasm, to be called as script/parse-example.js:

#!/usr/bin/env node

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

if (process.argv.length < 3) {
  console.log('Usage: script/parse-example.js <haskell-file>')
  process.exit(1)
}

const fileName = process.argv[2]
const sourceCode = fs.readFileSync(fileName, 'utf8')

Parser.init().then(() => {
  Parser.Language.load('tree-sitter-haskell.wasm').then((Haskell) => {
    const parser = new Parser;
    parser.setLanguage(Haskell);
    const tree = parser.parse(sourceCode);
    console.log(tree.rootNode.toString());
  });
});

@wenkokke
Copy link
Contributor Author

The following patch to web-tree-sitter makes tree-sitter-haskell work:

diff --git a/lib/binding_web/exports.json b/lib/binding_web/exports.json
index e0b3f718..3a43ae33 100644
--- a/lib/binding_web/exports.json
+++ b/lib/binding_web/exports.json
@@ -3,6 +3,17 @@
   "_free",
   "_malloc",
 
+  "__ZNSt3__24cerrE",
+  "__ZNSt3__25ctypeIcE2idE",
+  "__ZTVN10__cxxabiv117__class_type_infoE",
+  "__ZTVN10__cxxabiv120__si_class_type_infoE",
+  "__ZTVN10__cxxabiv120__function_type_infoE",
+  "__ZTVN10__cxxabiv119__pointer_type_infoE",
+  "__ZTVN10__cxxabiv121__vmi_class_type_infoE",
+  "__ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE25__init_copy_ctor_externalEPKcm",
+  "__ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE25__init_copy_ctor_externalEPKcm",
+  "___cxa_pure_virtual",
+
   "__ZNKSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4copyEPcmm",
   "__ZNKSt3__220__vector_base_commonILb1EE20__throw_length_errorEv",
   "__ZNSt3__212basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm",

@wenkokke
Copy link
Contributor Author

wenkokke commented Jan 1, 2022

Created two issues which may help debug such problems in the future (tree-sitter/tree-sitter#1565 and tree-sitter/tree-sitter#1566).

wenkokke added a commit to wenkokke/fork4pr-tree-sitter-haskell that referenced this issue Jan 1, 2022
@tek tek closed this as completed in d7978db Jan 1, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant