This repository contains instructions to compile a Prolog implementation SWI-Prolog (http://swi-prolog.org) to WebAssembly.
These compilation instructions assume Linux-based host machine. The resulting WebAssembly binary is platform-independent.
You need to download the Emscripten compiler. Follow the instruction on its homepage.
After the successful installation load the Emscripten environment into the current terminal session (asjust path):
SWI-Prolog depends on zlib. To compile it to WebAssembly:
wget https://zlib.net/zlib-1.2.11.tar.gz -O "$HOME/zlib-1.2.11.tar.gz" tar -xf "$HOME/zlib-1.2.11.tar.gz" -C "$HOME" cd "$HOME/zlib-1.2.11" emconfigure ./configure emmake make
This will download and build zlib into the
subdirectory in your home directory.
git clone https://github.com/rla/swipl-devel.git "$HOME/swipl-devel" cd "$HOME/swipl-devel" git fetch git checkout wasm ./prepare
This will ask:
Do you want me to run git submodule update --init? [Y/n]
As we are not yet building packages, say "n".
If it asks:
Could not find documentation. What do you want to do?
Then say "3". We do not need documentation to build it.
After this we can run configure and compile:
cd "$HOME/swipl-devel/src" LDFLAGS=-L"$HOME/zlib-1.2.11" \ LIBS=-lzlib \ CPPFLAGS=-I"$HOME/zlib-1.2.11" \ COFLAGS=-O3 emconfigure ./configure \ --disable-mt \ --disable-gmp \ --disable-custom-flags emmake make
This will build the necessary 3 files in
Binary distribution (in the
dist directory) contains
swipl-web.wasm- binary distribution of SWI-Prolog executable.
swipl-web.dat- necessary files to initialize the runtime and the library.
- Set up a stub Module object with
noInitialRun: trueand other options.
- Set up FLI bindings in
- Use the bindings to call
- Set up the location for standard library.
- Use the FS API to write code files into the virtual filesystem.
- Load the code files from SWI-Prolog side using
consult/1or a similar way.
- Interact with SWI-Prolog through its FLI.
example/index.html as a simple example. It can be found online at
http://demos.rlaanemets.com/swi-prolog-wasm/example/. The commented code
inside the demo provides the documentation.
To test it out locally, you need to serve the files through an HTTP server.
- Provide full set of bindings?
- Compile and add useful packages.
- Provide a mechanism to load packs?
- Easier way to turn Prolog terms into JS objects?
- See where WebAssembly goes and what interfaces could be added (direct DOM access?).
SWI-Prolog is covered with the Simplified BSD license. See http://www.swi-prolog.org/license.html
zlib is covered with the zlib license. See https://zlib.net/zlib_license.html