Using libc with WebAssembly enabled DataFrames
This post describes how to use the C standard library with WebAssembly enabled
DataFrames. You can find the source code
here.
Introduction
In my previous
post
I avoided solving the problem of linking to standard
library to provide memory allocation for my WebAssembly project by writing my
own memory allocator. In order to use external C packages I will need a
standard library.
wsi-libc
The wasi-libc project does what it
says on the tin by providing a standard library for wasm.
I have changed my setup as my laptop required reinstallation. I’m now running
Ubuntu 20.04 LTS.
I installed the latest (version 10) LLVM tool chain as a Debian package which
I got from this page. I did the following.
1 | wget https://apt.llvm.org/llvm.sh |
That installed clang et al with the suffix “-10”, such that clang could be
found as /usr/bin/clang-10
.
I installed wabt in /opt/wabt
.
I cloned the wasi-libc library and built it as follows.
1 | git clone git@github.com:WebAssembly/wasi-libc.git |
The C Source Code
In my previous post I had two source files: memory-allocation.c
and array-methods.c
.
We can delete memory-allocation.c
as we’ll use malloc
and free
from the
standard library. I split he array methods into two files: array-methods-int.c
and array-methods-double.c
, included stdlib.h
and changed the memory allocation
to use the standard library.
Here is the start of array-methods-int.c
.
1 |
|
Then I changed the makefile as follows.
1 | SYSROOT=/opt/wasi-libc |
The compilation stage node sets --sysroot
. This appears to set up the include
paths correctly.
The link stage adds the path to the libraries -L$(SYSROOT)/lib/wasm32-wasi
and
and includes libc and libm -lc -lm
.
The JavaScript Code
All I needed to do was to change the memory allocation imports to use malloc
and free
.
Everything just worked!
As a test I added a unary function logFloat64Array
. This required sme minor
changes to the javascript to allow ‘int’ type series to fall through to the double
methods. Again it all just worked.
Things To Do
I need to understand what the crt1.o
file is all about. I tried to link, but
that gives me other errors.