emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Exception if the entry script has non-Ascii characters

Open jameslan opened this issue 9 months ago • 0 comments

My library, libxml2-wasm compiles libxml2 into wasm with emscripten, and wraps with some js classes.

I've got an error report, whose entry script has non-Ascii characters. Thus we got the following error,

$ node 入口.mjs                                                                                                                                                                                                       5:22:37 PM
Aborted(Assertion failed)
/Users/james/sat/xw/lib/libxml2raw.cjs:675
  var e = new WebAssembly.RuntimeError(what);
          ^

RuntimeError: Aborted(Assertion failed)
    at abort (/Users/james/sat/xw/lib/libxml2raw.cjs:675:11)
    at assert (/Users/james/sat/xw/lib/libxml2raw.cjs:293:5)
    at stringToAscii (/Users/james/sat/xw/lib/libxml2raw.cjs:3864:9)
    at /Users/james/sat/xw/lib/libxml2raw.cjs:3875:9
    at Array.forEach (<anonymous>)
    at _environ_get (/Users/james/sat/xw/lib/libxml2raw.cjs:3872:23)
    at wasm://wasm/0039360e:wasm-function[540]:0x23b0d
    at wasm://wasm/0039360e:wasm-function[15]:0x12bd
    at initRuntime (/Users/james/sat/xw/lib/libxml2raw.cjs:553:35)
    at doRun (/Users/james/sat/xw/lib/libxml2raw.cjs:4709:5)

Node.js v18.20.4

I think the reason is that when storing the environment variables, it doesn't convert javascript's UTF16 string to UTF8:

  var _environ_get = (__environ, environ_buf) => {
      var bufSize = 0;
      getEnvStrings().forEach((string, i) => {
        var ptr = environ_buf + bufSize;
        HEAPU32[(((__environ)+(i*4))>>2)] = ptr;
        stringToAscii(string, ptr);
        bufSize += string.length + 1;
      });
      return 0;
    };

Version of emscripten/emsdk: 4.0.6

$ emcc -v                                                                                                                                                                                                             5:28:11 PM
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.6-git
clang version 21.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/emscripten/4.0.6/libexec/llvm/bin

jameslan avatar Apr 01 '25 01:04 jameslan