Swipl-bundle.js not working

I compiled SWI-Prolog version 9.2.9 for WASM (cloned from git, emcc 4.0.4) and it seems to work fine with Node.js 18.9.0.

I want to be able to compile .qlf files of my projects to turn them into web apps. For that, I wanted to have one swipl locally to generate them, and then use the same version to interpret .qlf’s in a browser.

However, my compiled swipl-bundle.js does not seem to work in the browser. Namely,

swipl = await SWIPL()

gives swipl without prolog or consult()! I tried the same with swipl-bundle.js from the npm package and from the swi-prolog website, and it worked.

Any suggestions what could have happened?

NB: I invoked CMake as

emcmake cmake -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_FIND_ROOT_PATH=~/src/emsdk/wasm-root/ \
      -DINSTALL_DOCUMENTATION=OFF \
      -DUSE_GMP=OFF \
      -G Ninja ..

Maybe I should try the “official” way as in the manual, with pointing the toolchain path. I will also try to build a Debug version.

UPDATE: So, I compiled with -DCMAKE_BUILD_TYPE=Debug and I have now the following errors in the JS console:

Aborted(Attempt to set Module.onRuntimeInitialized after it has already been processed. This can happen, for example, when code is injected via ‘–post-js’ rather than ‘–pre-js’)

Unhandled Promise Rejection: RuntimeError: Aborted(Attempt to set Module.onRuntimeInitialized after it has already been processed. This can happen, for example, when code is injected via ‘–post-js’ rather than ‘–pre-js’)

This time I run CMake almost as described in the manual, calling

cmake -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \
      -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_FIND_ROOT_PATH=$WASM_ROOT \
      -DINSTALL_DOCUMENTATION=OFF \
      -DUSE_GMP=OFF \
      -G Ninja ..

directly from shell (after calling emsdk_env.sh of course). The main difference with the manual is that it is suggested to put such a command into a script and run it with bash. Also, I disabled GMP.

I also noticed that for both times I compiled the WASM swipl, I got a warning:

emcc: warning: main is defined in the input files, but _main is not in EXPORTED_FUNCTIONS. Add it to this list if you want main to run. [-Wunused-main]

when swipl-bundle.js and swipl-bundle-nodata.js were built.

Try the development versions. 9.2.x notably does not contain the patches that are required for Emscripten 4. There should be no need to rebuild the WASM. The latest development version is compatible with the WASM release.

Thanks for a suggestion! Probably I should have mentioned this issue myself, as the compatibility with Emscripten 4 was already discussed…

However, I still can’t get swipl-bundle.js to work! Now I can see the following output in the JS console:

[Error] ERROR: /swipl/library/wasm.pl:59:[Error] ERROR: source_sink `library(uri)’ does not exist
[Error] Warning: /swipl/library/wasm.pl:59:
[Error] Warning: Goal (directive) failed: wasm:use_module(library(uri),[uri_is_global/1,uri_normalized/3,uri_normalized/2])
[Log] wasm:file_url/2: Unknown procedure: wasm:uri_is_global/1 (swipl-bundle-devel.js, line 8)

when creating the SWIPL object.

Althoug the object gets created somehow, and even contains prolog this time, running

swipl.prolog.query("memberchk(X, [sdsd,ghh])").once().X

yields

[Log] wasm:wasm_call_string/3: Unknown procedure: wasm:exclude/3 (swipl-bundle-devel.js, line 8)

Looks like (most of) the libraries are not included. Did you install from GIT? If so, did you clone the submodules?

2 Likes

Indeed, I did not clone the submodules.

After executing

git submodule update --init

in the swipl-devel repository, it compiled and working like a charm.

I was also able to compile my .qlf and consult it in browser. Awesome!

Thank you so much!