Functions expose Java methods to scripts.
A function implements:
org.densy.scriptify.api.script.function.ScriptFunctionMinimal function:
public final class AddFunction implements ScriptFunction {
@Override
public String getName() {
return "add";
}
@ExecuteAt
public Number execute(
@Argument(name = "left") Number left,
@Argument(name = "right") Number right
) {
return left.doubleValue() + right.doubleValue();
}
}script.getFunctionManager().register(new AddFunction());
script.evalOneShot("add(2, 3)");Global functions are added to the runtime global module during compilation.
SimpleScriptInternalModule math = new SimpleScriptInternalModule("math");
math.export(new ScriptFunctionExport(new AddFunction()));
script.getModuleManager().addModule(math);import * as math from "math";
math.add(2, 3);Mark callable methods with @ExecuteAt.
@ExecuteAt
public String execute(@Argument(name = "value") String value) {
return value.toUpperCase();
}A single function class may have multiple @ExecuteAt methods. Runtime dispatch selects a compatible executor by argument count and vararg compatibility; the core executor then validates Java types.
Avoid ambiguous overloads with the same script argument count unless conversion is predictable.
Use @Argument for script-provided values.
@Argument(name = "filePath") String filePathOptional arguments:
@Argument(name = "recursive", required = false) Boolean recursiveVarargs:
@ExecuteAt
public void execute(@Argument(name = "args") Object... args) {
}Use @Executor to receive the current Script<?>.
@ExecuteAt
public String execute(
@Executor Script<?> script,
@Argument(name = "filePath") String filePath
) {
Path path = script.getSecurityManager().getFileSystem().getPath(filePath);
return Files.readString(path);
}This is how standard file functions access security-aware paths.
ScriptFunctionManager provides:
ScriptFunctionDefinitionFactory getFunctionDefinitionFactory();
void setFunctionDefinitionFactory(ScriptFunctionDefinitionFactory factory);
Map<String, ScriptFunctionDefinition> getFunctions();
ScriptFunctionDefinition getFunction(String name);
void register(ScriptFunction function);
void remove(String name);Default implementation:
org.densy.scriptify.core.script.function.StandardFunctionManagerBehavior:
- duplicate function names are rejected;
- removing a missing function throws;
- returned maps are unmodifiable views;
ScriptFunctionDefinitionFactorycan be replaced for custom function metadata.
CommonFunctionManager exists for compatibility but is deprecated. Use StandardScriptModule or your own internal modules instead.