|
| 1 | +# Custom Runtime |
| 2 | + |
| 3 | +Scriptify can support additional scripting runtimes by implementing `Script<T>`. |
| 4 | + |
| 5 | +## Required Contract |
| 6 | + |
| 7 | +```java |
| 8 | +public final class MyScript implements Script<Object> { |
| 9 | + private final ScriptSecurityManager securityManager = new StandardSecurityManager(); |
| 10 | + private final ScriptFunctionManager functionManager = new StandardFunctionManager(); |
| 11 | + private final ScriptConstantManager constantManager = new StandardConstantManager(); |
| 12 | + private final ScriptModuleManager moduleManager = createModuleManager(); |
| 13 | + |
| 14 | + @Override |
| 15 | + public ScriptSecurityManager getSecurityManager() { |
| 16 | + return securityManager; |
| 17 | + } |
| 18 | + |
| 19 | + @Override |
| 20 | + public ScriptModuleManager getModuleManager() { |
| 21 | + return moduleManager; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public ScriptFunctionManager getFunctionManager() { |
| 26 | + return functionManager; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ScriptConstantManager getConstantManager() { |
| 31 | + return constantManager; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public CompiledScript<Object> compile(String source) throws ScriptException { |
| 36 | + throw new UnsupportedOperationException(); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## Runtime Responsibilities |
| 42 | + |
| 43 | +A runtime implementation should: |
| 44 | + |
| 45 | +- create the engine context; |
| 46 | +- configure security before script execution; |
| 47 | +- expose global functions; |
| 48 | +- expose global constants; |
| 49 | +- expose internal modules; |
| 50 | +- load external modules; |
| 51 | +- apply `ScriptAccess.ALL` and `ScriptAccess.EXPLICIT`; |
| 52 | +- convert script values into Java values before invoking `ScriptFunction`; |
| 53 | +- convert Java return values back into runtime values; |
| 54 | +- wrap runtime failures in `ScriptException`; |
| 55 | +- close engine resources in `CompiledScript.close`. |
| 56 | + |
| 57 | +## Function Bridge |
| 58 | + |
| 59 | +Your runtime needs an engine-specific callable wrapper for `ScriptFunctionDefinition`. |
| 60 | + |
| 61 | +That wrapper should: |
| 62 | + |
| 63 | +- receive script arguments; |
| 64 | +- convert them to Java values; |
| 65 | +- find a matching `ScriptFunctionExecutor`; |
| 66 | +- call `executor.execute(script, args...)`; |
| 67 | +- convert the result back to the script engine. |
| 68 | + |
| 69 | +## Module Export Resolver |
| 70 | + |
| 71 | +Implement: |
| 72 | + |
| 73 | +```java |
| 74 | +ScriptModuleExportResolverFactory |
| 75 | +ScriptModuleExportResolver |
| 76 | +``` |
| 77 | + |
| 78 | +The resolver maps Scriptify exports to runtime values: |
| 79 | + |
| 80 | +- `ScriptFunctionExport`; |
| 81 | +- `ScriptFunctionDefinitionExport`; |
| 82 | +- `ScriptConstantExport`; |
| 83 | +- `ScriptValueExport`; |
| 84 | +- custom `ScriptExport` implementations if you add them. |
| 85 | + |
| 86 | +Throw `ScriptModuleWrongContextException` if the factory receives a context object from another runtime. |
| 87 | + |
| 88 | +## Security Integration |
| 89 | + |
| 90 | +If the engine supports host class lookup restrictions, wire it to `ScriptSecurityManager.getExcludes()`. |
| 91 | + |
| 92 | +If the engine supports file system hooks, route file paths through: |
| 93 | + |
| 94 | +```java |
| 95 | +script.getSecurityManager().getPathAccessor() |
| 96 | +``` |
| 97 | + |
| 98 | +If the engine exposes Java objects/classes, implement `ScriptAccess.EXPLICIT`. |
0 commit comments