mmomtchev / swig

This is SWIG JavaScript Evolution, a fork of the SWIG project with modern JavaScript/TypeScript support including WASM and async
http://www.swig.org
Other
7 stars 0 forks source link
javascript node-addon-api node-js swig typescript wasm

SWIG JavaScript Evolution

This is SWIG JavaScript Evolution, an up-to-date fork of the SWIG project with a focus on JavaScript - native/Node.js, WASM/Node.js and WASM/browser.

Major New Features

Usage

SWIG JavaScript Evolution is currently available only as a build from source from this repository and as a Github Action.

You can find an example skeleton for a new project that uses a dual-build Node.js/native and browser/WASM system at swig-napi-example-project.

If you want to see a real-world complex project that uses conan to manage a large pool of dependencies - including WASM builds - you should take a look at magickwand.js - the ImageMagick-7 bindings for JavaScript.

I am currently working on a second generation C++/JavaScript project with a new build system which will be based on the meson/conan/xpm trio instead of node-gyp - it will feature clean integration of 3rd party C++ libraries on all platforms (conan), use of the native build system (meson) and fully self-contained builds that do not need a working C++ environment on the final user host (xpm). This new project is the PROJ bindings for JavaScript. The project is partially functional and still not published.

Install

Building from source

# from git
git clone https://github.com/mmomtchev/swig.git

# from a release
tar -zxvf v5.0.0.tar.gz

cd swig

# add a suffix if you want to be able to use the main trunk SWIG
# on the same host
./configure --program-suffix=-jse

# this will install everything in /usr/local/share/swig-jse
# w/o affecting your existing SWIG
make -j 4 && sudo make install

For WASM, you will also need to install emsdk and setup its environment variables as described at https://github.com/emscripten-core/emsdk.

Conan recipes

SWIG JSE is now available on conan. You can simply add it to your conanfile.txt:

[tool_requires]
swig-jse/[>=5.0.3]
# optionally, get also emscripten
emsdk/[>=3.1.50]

It is available on my custom conan repository:

conan remote add swig-jse https://swig.momtchev.com/artifactory/api/conan/swig-jse

You can either use the prebuilt packages - which should now work on all three major OS - or - should you be unsettled by the recent events around xz-utils - you can also build it yourself.

The conan recipes are available at https://github.com/mmomtchev/swig-conan.

JavaScript manual

The only changes in the manual relative to the mainline SWIG are in the JavaScript section.

Maturity

Late technology preview / Early public release. The first open-source package - magickwand.js - is published on npm and it is used in production on some low-traffic websites. It uses all major new SWIG JSE features - async, WASM, locking and TypeScript. A second one, proj.js is currently in the works.

Planned major features

WASM without COOP/COEP

Currently, WASM projects using asynchronous wrappers require that COOP/COEP is enabled. In this example, it is enabled by the webpack built-in server and by the karma test runner. Users of your module will have to host it on web servers that support and send these headers - this is a requirement on the web server end - ie a configuration option that must be enabled in Apache or nginx. For example, currently Github Pages and many low-end hosting providers do not support it.

Alternatively, this example can be built without asynchronous wrappers in order to produce a WASM binary that does not require COOP/COEP. The only real difference is the emscripten build configuration which can be found in emscripten.gypi.

In this case, there are two possible strategies:

Mixing the two is possible, but C++ functions running in the main thread and C++ functions running the in comlink worker won't be able to share objects as they will be running in separate memory spaces.

Build systems

SWIG-generated projects for JavaScript can currently choose between two build systems:

Description node-gyp meson + conan + xpm
Overview The official Node.js and Node.js native addon build system from the Node.js core team A new, still under development, experimental build system from SWIG JSE
Status Very mature Still not completely finished
Platforms with native builds All platforms supported by Node.js Linux, Windows and macOS
WASM builds Hackish, see swig-napi-example-project and magickwand.js@1.1 for solutions Out-of-the-box
Node.js APIs All APIs, including the now obsolete raw V8 and NAN and the current Node-API Only Node-API
Integration with other builds systems for external dependencies Very hackish, see magickwand.js@1.1 for solutions, the only good solution is to recreate the build system of all dependencies around node-gyp Out-of-the-box support for meson, CMake and autotools
conan integration Very hackish, see magickwand.js@1.0 Out-of-the-box
Build configurations through npm install CLI options Yes Yes
Distributing prebuilt binaries Yes, multiple options, including @mapbox/node-pre-gyp, prebuild-install and prebuildify prebuild-install
Requirements for the target host when installing from source Node.js, Python and a working C++17 build environment Only Node.js when using xpack-dev-tools, a working C++17 build environment otherwise
Makefile language Obscure and obsolete (gyp) Modern and supported (meson)

When choosing a build system, if your project:

Project templates

You can find the classical SWIG Node-API project skeleton using node-gyp here. It has a dual-environment setup with Node.js/native and Browser/WASM builds.

You can find the new SWIG Node-API project skeleton using hadron here. It has a dual-environment setup with Node.js/native and Browser/WASM builds and external dependencies using conan.

conan / meson

Alas, the current state of my affair has made working with conan and meson extremely difficult.

In both projects, they tried to play a psychosis game with my work (my PRs) by doing simultaneously seemingly random acts and then trying to send me to see a psychiatrist. Given the context of the current affair, this has made any real work extremely difficult.

At the moment, both software packages need to be installed from my own repositories.