FluxonRuntime.java 的登记手册,帮助你在宿主启动时向脚本注入函数、变量或扩展。
适用场景
- 暴露宿主能力(文件/网络/游戏对象等)给脚本。
- 设置函数的线程模型(异步或主线程)。
- 在不改核心源码的情况下增量添加命名空间或扩展函数。
单例访问
newEnvironment():创建新的解释器环境(自动 bake 缓存)。getSystemFunctions()/getExtensionFunctions()/getSystemVariables():调试或导出当前注册表。setPrimaryThreadExecutor(Executor executor):替换主线程执行器,供主线程函数使用。
函数注册
| 方法 | 用途 |
|---|---|
registerFunction(name, paramCount, callable) | 注册同步函数;可用 List<Integer> 支持多种参数个数。 |
registerFunction(namespace, name, paramCount, callable) | 带命名空间的函数,例如 fs:crypto。 |
registerAsyncFunction(...) | 异步函数,Function#isAsync() 为 true。 |
registerPrimarySyncFunction(...) | 主线程函数,isPrimarySync = true。 |
registerVariable(name, value) | 注入全局变量,可在脚本中直接访问。 |
newEnvironment() 时自动重建缓存数组。
扩展函数(target::method())
Builder 链式注册
直接注册
| 方法 | 说明 |
|---|---|
registerExtensionFunction(type, name, paramCount, callable) | 同步扩展。paramCount 支持 int 或 List<Integer>;命名空间可为 null。 |
registerAsyncExtensionFunction(...) | 异步扩展。 |
registerSyncExtensionFunction(...) | 主线程扩展。 |
fluxon-functions.json 导出,供 IDE/文档消费。
变量与全局对象
- 运行时默认注册了
g(),返回GlobalObject.INSTANCE,便于上下文调用(如g() :: someExtension(...))。 - 其他全局对象可通过
Environment#getRootVariables()验证。
调试与导出
- 直接打印
runtime.getSystemFunctions()/getExtensionFunctions()查看注册结果。 - 使用
./gradlew :core:dumpFluxonCatalog导出最新函数目录,保持 VS Code 补全与文档同步。