Introduction
Ant can be embedded as a C library (libant) in your applications, allowing you to run JavaScript code within your C/C++ programs. This guide will walk you through the setup process and basic usage.
Prerequisites
- C compiler (clang or gcc)
- Build tools (make, bash)
- Platform-specific libraries:
- macOS: Security and CoreFoundation frameworks
- Linux: pthread, dl, and m libraries
- Windows: ws2_32, rpcrt4, secur32, ntdll, crypt32, and userenv libraries
Building libant
Build the static library
Run the build script from the This script will:
libant directory:- Set up the build environment
- Compile dependencies
- Compile the Ant runtime
- Bundle everything into
libant.a
Creating Your First Embedded Application
Basic Example
Here’s a minimal example that evaluates a JavaScript expression:Compiling Your Application
Key Concepts
Runtime Creation
There are two ways to create a runtime:Stack Management
Always set the stack base for proper stack overflow detection:Value Types
JavaScript values are represented asjsval_t with various types:
T_NUM- NumberT_STR- StringT_BOOL- BooleanT_OBJ- ObjectT_ERR- ErrorT_UNDEF- UndefinedT_NULL- Null
vtype(value) and convert using functions like:
js_getnum(value)for numbersjs_getstr(js, value, &len)for stringsjs_str(js, value)for string representation
Global Object
Access the global object to set or get global variables:Next Steps
- Explore the API Reference for detailed function documentation
- Check out Examples for practical use cases
- Learn about exposing C functions to JavaScript
- Understand object and array manipulation