Packaging prolog as a C++ program to compile for webassembly


I understand that C/C++ now readily compiles for web assembly.

Does this mean that wrapping a prolog app into a C++ program could directly support full swi prolog functionality as a web assembly module.

My aim is to possibly run a demo program that includes a swi prolog component within node.js… My assumption is that via webassemly interconnnection between prolog and node.js would be faster than say via a websocket interface.

any comments are much appreciated,


Reference would be nice. :wink:

Are you aware of GitHub SWI-Prolog ported to WebAssembly. AFIAK it never achieved a status of fully operational but might have some useful knowledge.

1 Like


Yes, the swi port to WebAssembly triggered my interest – but, i understand that its experimental, a demo only and only covers a small subset.

So, my next thought was, what if prolog is embedded in a C++ wrapper and from web searches I am assuming that C++ is readily supported.



1 Like

As I understand it (I could be wrong), that is not going to work. If you have C/C++ and want to use it with node.js, you have two options: compile (using emscripten) to WebAssembly or use the node.js C/C++ native API to connect the C/C++ natively.

The first has some drawbacks. WebAssembly is 32-bits and single threaded, which isn’t a very good environment for SWI-Prolog and I understood roughly 2-3 times slower than native code. As is (last time I loooked at it), some of the emscripten file API is broken in such a way that SWI-Prolog can’t access local files. Its in the emscripten bug reports, and still not fixed AFAIK.

The latter (using native embedding), I guess, is possible. Its surely faster then websockets, but typically also way harder to manage. If you use node.js as web server you can probably find some course grain interaction that makes using a socket fast enough. Note that node.js is basically single threaded (or green/cooperatively threaded) and calling out to expensive Prolog calls probably blocks node. You can probably work around that by using threads in Prolog, where one thread chats with node, but that mostly makes things complicates. Why no just let Prolog do the web services?

The demo requires real time behavior, and close interaction with Prolog. Think of a real time game with 30-40 frames per second loops, and where would Prolog plays a semantic decision-making role as well. It all runs in a node.js box or within a browser.

I am afraid that web services will be too slow. I will check websockets – but, if there is a way to do without communication and within the node.js box only, that would be preferred.

In theory i could go with something like Unreal which works in C++ and and i, guess, swi prolog could be embedded there – although, it would be quite a learning curve for me.


In that scenario embedding SWI-Prolog using the node native API might be a solution. It is probably complicated though.

If you do not need to exchange too much data websockets are probably fine. I’d expect the delay to be about 0.1ms on a localhost connection, depending on hardware, OS and enough available cores. If you need to share/exchange a lot of data you could consider shared memory. All in all you’ve got 25-30ms, so you can afford a couple of ms on communication.

webassembly could be very usefull for web applications,

instead of porting swi prolog to web assembly, wouldnt it be a better solution to write a swi-prolog program which can parse prolog code, and which can then output code in webassembly format for this parsed code?.

or to let this program generate webassembly script which then can be used for webassembly,

they say that C code can already be converted to webassembly, is this true for all cases because of the procedural nature of the language C?

would it then be good to learn C as a language because you can then use it for webassembly as well,
or would it be benificial to learn webassembly-script for reason that this is made for the purpose of generating webassembly code, in other words does webassemblyscript have a better connection to webassembly-code than C-code?