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 ajs_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 fromexamples/fib.c that exports a Fibonacci function:
Signatures:
Module with Classes
Theexamples/point.c example demonstrates creating a module with a custom class:
Signatures for class API:
Building Modules
Dynamic Module (Shared Library)
Using CMake (fromCMakeLists.txt):
On Windows, you must define
QUICKJS_NG_MODULE_BUILD and link against the QuickJS library.Static Linking
Compile the module source with your application: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:Next Steps
- Memory Management - Understand memory allocation in modules
- Performance - Optimize native module performance
- Embedding - Integrate modules with embedded runtime