Skip to main content

Creating Native C Modules

QuickJS supports native ES6 modules written in C that can be dynamically or statically linked. This allows you to expose C functionality to JavaScript while maintaining the ES6 module interface.

Module Structure

Every native module must export a js_init_module function that QuickJS calls when loading the module:
The JS_MODULE_EXTERN macro ensures proper symbol visibility on Windows and other platforms.

Simple Function Module

Here’s a complete example from examples/fib.c that exports a Fibonacci function:
Signatures:

Module with Classes

The examples/point.c example demonstrates creating a module with a custom class:
Signatures for class API:

Building Modules

Dynamic Module (Shared Library)

Using CMake (from CMakeLists.txt):
Using gcc/clang directly:
On Windows, you must define QUICKJS_NG_MODULE_BUILD and link against the QuickJS library.

Static Linking

Compile the module source with your application:
Register it before use:

Using Modules in JavaScript

Dynamic Module

Class Module

Function List Entry Macros

QuickJS provides convenient macros for defining exports:

Advanced: GC Mark Function

If your opaque data contains JSValue references, provide a GC mark function:
Signature:

Next Steps