Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo GraalJavaScriptExecutor

From Leeroopedia
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 Context with allowAllAccess(true), preventing cross-request contamination.
  • Context binding: The _meta map provides request metadata (version, tag, args); extParam provides extension data.
  • Return type handling: Automatically converts GraalVM Value to 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 a key() 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

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment