Implementation:APIJSON APIJSON Demo GraalJavaScriptExecutor
| Knowledge Sources | |
|---|---|
| Domains | Scripting, Polyglot, Extensibility |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for executing server-side JavaScript functions via the GraalVM polyglot engine, provided by the APIJSON Demo's Script module.
Description
The GraalJavaScriptExecutor class implements the ScriptExecutor interface and uses GraalVM's polyglot API to execute JavaScript functions. Scripts are compiled as Source objects and cached in a thread-safe ConcurrentHashMap. At execution time, a new GraalVM Context is created for each call, with bindings for _meta (version, tag, args) and optional extParam.
Key features:
- Thread-safe caching:
ConcurrentHashMap<String, Source>for lock-free script lookups. - Per-call context: Each execution creates a fresh GraalVM
ContextwithallowAllAccess(true), preventing cross-request contamination. - Context binding: The
_metamap provides request metadata (version, tag, args);extParamprovides extension data. - Return type handling: Automatically converts GraalVM
Valueto Java types (Boolean, Integer, String).
Usage
- Registering a script: Call
load(name, script)to compile and cache a JavaScript function. - Executing a script: The framework calls
execute(parser, currentObject, methodName, args)when akey()function references a script. - Cleaning up: Call
cleanCache()to clear all cached scripts.
Code Reference
Source Location
- Repository: APIJSON-Demo
- File: APIJSON-Java-Server/APIJSONDemo-Script/src/main/java/apijson/demo/script/GraalJavaScriptExecutor.java
Signature
public class GraalJavaScriptExecutor implements ScriptExecutor {
private final Map<String, Source> scriptMap = new ConcurrentHashMap<>();
private Engine engine;
@Override
public ScriptExecutor init(); // Creates GraalVM Engine
@Override
public void load(String name, String script); // Compiles and caches Source
@Override
public Object execute(AbstractFunctionParser parser, JSONObject currentObject,
String methodName, Object[] args) throws Exception;
@Override
public void cleanCache();
private Object extendParameter(AbstractFunctionParser parser,
JSONObject currentObject, String methodName, Object[] args);
}
Import
import apijson.demo.script.GraalJavaScriptExecutor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | String | Yes | Script function name (used as cache key) |
| script | String | Yes (load) | JavaScript source code |
| parser | AbstractFunctionParser | Yes (execute) | Function parser context (provides version, tag) |
| currentObject | JSONObject | Yes (execute) | Current request JSON object |
| methodName | String | Yes (execute) | Function name to execute |
| args | Object[] | Yes (execute) | Function arguments |
Outputs
| Name | Type | Description |
|---|---|---|
| execute return | Object | Boolean, Integer, String, or polyglot Value |
Usage Examples
Registering and Executing a Script
GraalJavaScriptExecutor executor = new GraalJavaScriptExecutor();
executor.init();
// Register a JavaScript function
executor.load("double", "function double(x) { return x * 2; }");
// Execute (with parser context)
Object result = executor.execute(parser, curObj, "double", new Object[]{ 21 });
// result = 42 (as Integer)
Accessing _meta in JavaScript
// Script has access to _meta.version, _meta.tag, _meta.args
function validateVersion() {
if (_meta.version < 1) {
return false;
}
return true;
}
Related Pages
Implements Principle
- Principle:APIJSON_APIJSON_Demo_Script_Execution_Engine
- Principle:APIJSON_APIJSON_Demo_Custom_Remote_Functions