A complete CMake build system for Mozilla's SpiderMonkey JavaScript engine headers with working integration examples.
This project provides the actual SpiderMonkey source headers (mozjs-128) with a CMake integration that allows you to:
- Use SpiderMonkey headers in your C/C++ projects
- Build examples demonstrating CMake integration
- Integrate as a CMake subdirectory in other projects
- Link against system SpiderMonkey library when available
This IS the SpiderMonkey header distribution that works as a portable CMake subdirectory.
# Build and run examples
./build.sh
# Clean build with verbose output
./build.sh --clean --verbose
# Debug build
./build.sh --type Debug
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)
./bin/basic_js
./bin/advanced_js
spidermonkey/
├── CMakeLists.txt # Main CMake configuration
├── build.sh # Automated build script
├── README.md # This file
├── examples/ # CMake integration examples
│ ├── basic_js.c # Basic C integration example
│ └── advanced_js.cpp # Advanced C++ integration example
├── js/ # SpiderMonkey JS API headers
├── mozilla/ # Mozilla utility headers
├── jsapi.h # Main SpiderMonkey API header
└── [SpiderMonkey headers...] # Complete SpiderMonkey header set
This is the primary use case - integrate SpiderMonkey headers into your project:
# Add SpiderMonkey as subdirectory
add_subdirectory(spidermonkey)
# Link your target against SpiderMonkey
target_link_libraries(your_target PRIVATE SpiderMonkey::spidermonkey)
#include "jsapi.h"
#include "js/Initialization.h"
#include "js/CompilationAndEvaluation.h"
// ... other SpiderMonkey headers as needed
int main() {
// Initialize SpiderMonkey
if (!JS_Init()) {
return 1;
}
// Create context
JSContext *cx = JS_NewContext(JS::DefaultHeapMaxBytes);
// ... use SpiderMonkey API
// Cleanup
JS_DestroyContext(cx);
JS_ShutDown();
return 0;
}
The basic_js.c
example demonstrates:
- How to use SpiderMonkey as a CMake subdirectory
- CMake target integration
- Header availability
- Project structure
The advanced_js.cpp
example shows:
- Modern C++ integration patterns
- SpiderMonkey API usage examples
- Best practices for JavaScript engine integration
For complete JavaScript execution capabilities:
-
Install system SpiderMonkey library:
# macOS brew install spidermonkey # Ubuntu/Debian sudo apt install libmozjs-128-dev # Fedora/CentOS sudo dnf install mozjs128-devel
-
Use headers from this project (they're compatible with system library)
-
Your CMake will automatically link against system library when available
Option | Description | Default |
---|---|---|
SPIDERMONKEY_BUILD_EXAMPLES |
Build integration examples | ON |
SPIDERMONKEY_BUILD_TESTS |
Build test programs | OFF |
SPIDERMONKEY_INSTALL |
Enable installation | OFF |
Usage: ./build.sh [OPTIONS]
Options:
-h, --help Show help
-t, --type TYPE Build type (Debug, Release, etc.)
-c, --clean Clean build
-e, --examples Build examples (default)
-v, --verbose Verbose output
-r, --run Run examples after build (default)
-j, --jobs N Parallel jobs
- CMake: 3.20+
- C++ Compiler: C++17 support (GCC 7+, Clang 5+, MSVC 2017+)
- C Compiler: C11 support
This project includes the complete SpiderMonkey header set:
jsapi.h
- Main JavaScript APIjs/Initialization.h
- Engine initializationjs/CompilationAndEvaluation.h
- Script executionjs/SourceText.h
- Source code handlingjs/Context.h
- JavaScript contextsjs/Value.h
- JavaScript valuesmozilla/*.h
- Mozilla utility headers- And many more...
When you have the system library installed, you can use patterns like:
// Basic JavaScript execution
var result = 2 + 3 * 4;
// String manipulation
var greeting = 'Hello, SpiderMonkey!';
// Functions
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
// Arrays
var numbers = [1, 2, 3, 4, 5];
var sum = numbers.reduce((a, b) => a + b, 0);
// Objects
var person = { name: 'Alice', age: 25 };
add_subdirectory(spidermonkey)
target_link_libraries(my_app SpiderMonkey::spidermonkey)
# Add SpiderMonkey
add_subdirectory(external/spidermonkey)
# Create your JavaScript-enabled application
add_executable(js_app main.cpp)
target_link_libraries(js_app PRIVATE SpiderMonkey::spidermonkey)
# Optional: copy SpiderMonkey headers for IDE
target_include_directories(js_app PRIVATE external/spidermonkey)
# Clean build with verbose output
./build.sh --clean --verbose
# Check requirements
cmake --version # Should be 3.20+
If you encounter header conflicts:
- The project uses minimal include paths by default
- SpiderMonkey headers are available but not automatically included
- Include specific headers as needed:
#include "jsapi.h"
# Check if system SpiderMonkey is available
pkg-config --exists mozjs-128 && echo "Available" || echo "Not available"
# Install system library for full functionality
brew install spidermonkey # macOS
This project follows Mozilla SpiderMonkey's license (MPL 2.0).
- SpiderMonkey Documentation
- SpiderMonkey Embedding Guide
- Mozilla SpiderMonkey Source
- CMake Documentation
This is the complete SpiderMonkey header distribution with CMake integration. Use it as a subdirectory in your projects for JavaScript engine capabilities.