How do I write a custom Emscripten shim for a C library? Emscripten bundles shims for certain C libraries such as SDL and OpenAL, but for other libraries you're going to have to roll your own.
By shim I mean, a JavaScript replacement for a C library that the code to be ported depends on.
Emscripten has some documentation on the subject, although it's somewhat incomplete at the time of writing.
First of all, you need to write an Emscripten "library" in JavaScript, let's assume the corresponding C library is called Example:
example.js:
// "use strict";
var LibraryExample = {
// Internal functions
$EXAMPLE: {
internal_func: function () {
}
},
example_initialize: function (arg) {
EXAMPLE.internal_func()
}
}
autoAddDeps(LibraryExample, '$EXAMPLE')
mergeInto(LibraryManager.library, LibraryExample)
Second, you must integrate your example.js file into the build, through emcc's --js-library option:
emcc --js-library shims/example.js -o project.js project.bc
Now the function example_initialize
should be available to the JS code generated by Emscripten, thus replacing the C library dependency.