This example will demonstrate using Emscripten to compile a C library to webassembly and loading it from Python using PytyhonMonkey.
PythonMonkey is a Python library for executing JavaScript and WebAssembly from Python. Check it out on GitHub: https://github.com/Distributive-Network/PythonMonkey
In this example the following C code will be executed from Python:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
print(my_c_lib.add(1,2)) # this outputs 3
Install Emscripten https://emscripten.org/docs/getting_started/downloads.html
Install Python with a minimum version of 3.8.
Install PythonMonkey using pip: $ pip install pythonmonkey
Compile the code using emcc
$ emcc adder.c -s WASM=1 -s SIDE_MODULE=1 -o adder.wasm
Run the python program:
$ python3 main.py