Principle:APIJSON APIJSON Demo Script Execution Engine
| Knowledge Sources | |
|---|---|
| Domains | Scripting, Extensibility, Polyglot |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Architecture for executing server-side scripts in multiple languages (JavaScript via GraalVM/Nashorn, Lua via LuaJ) within a Java application, with per-script caching, thread safety, and context binding for API request processing.
Description
The Script Execution Engine pattern allows APIJSON to run user-provided scripts as remote functions. Each language has a dedicated ScriptExecutor implementation that handles initialization, script loading, execution, and cache management. Scripts receive a binding context with request metadata (_meta), the current JSON object, and optional extension parameters (extParam).
This solves:
- Multi-language support: Business logic can be written in JavaScript or Lua, not just Java.
- Hot deployment: Scripts can be loaded and updated at runtime via the database without restarting the server.
- Thread safety: Different executors handle concurrency differently — Lua uses per-script synchronized locking, GraalVM uses ConcurrentHashMap for thread-safe Source storage, Nashorn uses no locking.
The ScriptExecutor interface defines four operations:
- init(): Initialize the script engine (create GraalVM Engine, Nashorn factory, or Lua engine)
- load(name, script): Compile and cache a script by name
- execute(parser, currentObject, methodName, args): Run the named script with context
- cleanCache(): Clear all cached scripts
Usage
Use this principle when:
- You need to execute server-side scripts in multiple languages from a unified interface.
- Scripts should be cached after compilation for performance.
- Thread safety requirements vary by language (Lua needs locking; JavaScript engines may not).
Theoretical Basis
ScriptExecutor Interface:
init() → ScriptExecutor
load(name: String, script: String) → void
execute(parser, currentObject, methodName, args) → Object
cleanCache() → void
Execution Context Binding:
_meta = {
version: parser.getVersion(),
tag: parser.getTag(),
args: Object[]
}
extParam = extendParameter(parser, currentObject, methodName, args)
Thread Safety Strategies:
GraalVM: ConcurrentHashMap<String, Source> (lock-free reads)
Nashorn: No locking (isLockScript = false)
Lua: synchronized(methodName) (per-script lock)
Return value handling (GraalVM):
Value value = context.eval(scriptMap.get(methodName));
if (value.isBoolean()) return value.asBoolean();
if (value.isNumber()) return value.asInt();
if (value.isString()) return value.asString();
return value; // Polyglot Value wrapper
Related Pages
Implemented By
- Implementation:APIJSON_APIJSON_Demo_GraalJavaScriptExecutor
- Implementation:APIJSON_APIJSON_Demo_NashornScriptExecutor
- Implementation:APIJSON_APIJSON_Demo_LuaScriptExecutor