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 LuaScriptExecutor

From Leeroopedia
Knowledge Sources
Domains Scripting, Extensibility, Concurrency
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for executing server-side Lua functions via the LuaJ engine with per-script synchronized locking, provided by the APIJSON Demo's Script module.

Description

The LuaScriptExecutor class extends JSR223ScriptExecutor and uses LuaJ (via JSR 223) to execute Lua scripts. Its distinguishing feature is per-script synchronized locking: isLockScript() returns true and the execute() method wraps the superclass call with synchronized(methodName), ensuring only one thread executes a given Lua script at a time.

This is necessary because LuaJ's script engine is not inherently thread-safe for concurrent execution of the same script. The extendParameter() method returns a UserEntity object, demonstrating how domain objects can be passed to Lua scripts as context.

Usage

  • Registering: Via SCRIPT_EXECUTOR_MAP.put("luaj", new LuaScriptExecutor().init()).
  • Executing: The framework calls execute() when a key() function references a Lua-registered script. The synchronized(methodName) block ensures thread safety.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONDemo-Script/src/main/java/apijson/demo/script/LuaScriptExecutor.java

Signature

public class LuaScriptExecutor extends JSR223ScriptExecutor {
    @Override
    protected String scriptEngineName();  // Returns "luaj"

    @Override
    protected Object extendParameter(AbstractFunctionParser parser,
        JSONObject currentObject, String methodName, Object[] args);
    // Returns UserEntity with username="ddd"

    @Override
    protected boolean isLockScript(String methodName);  // Returns true

    @Override
    public Object execute(AbstractFunctionParser parser, JSONObject currentObject,
        String methodName, Object[] args) throws Exception {
        // Per-script synchronized locking
        synchronized (methodName) {
            return super.execute(parser, currentObject, methodName, args);
        }
    }
}

Import

import apijson.demo.script.LuaScriptExecutor;

I/O Contract

Inputs

Name Type Required Description
name String Yes Script function name (cache key and lock key)
script String Yes (load) Lua source code
parser AbstractFunctionParser Yes Function parser context
methodName String Yes Function name (used as synchronized lock key)
args Object[] Yes Function arguments

Outputs

Name Type Description
execute return Object Result of Lua script execution

Usage Examples

Registration and Execution

LuaScriptExecutor executor = new LuaScriptExecutor();
executor.init();
APIJSONFunctionParser.SCRIPT_EXECUTOR_MAP.put("luaj", executor);

// Register a Lua script
executor.load("compute", "function compute(a, b) return a + b end");

// Thread-safe execution (synchronized on "compute")
Object result = executor.execute(parser, curObj, "compute", new Object[]{1, 2});
// result = 3

Thread Safety Behavior

// Two threads calling the same Lua script are serialized:
// Thread 1: synchronized("compute") { execute("compute") }  → runs first
// Thread 2: synchronized("compute") { execute("compute") }  → waits for Thread 1
// Thread 3: synchronized("other") { execute("other") }      → runs concurrently with Thread 1

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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