Skip to content

API Host ScriptDomain

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

ScriptDomain

隔离的脚本运行环境

Isolated script execution environment

.NET Host API 目录

概述

ScriptDomain 绑定一个已编译的 AuroraEngine,但拥有独立的 Global、模块注册表和可选 UserState。它实现 IDisposable;不再使用时必须释放。

ScriptDomain is bound to a compiled AuroraEngine, but owns an independent Global, module registry, and optional UserState. It implements IDisposable and must be disposed when no longer needed.

Global / Engine / UserState

Returns

当前 Domain 的全局对象、所属 Engine 和可选状态。

Current domain global, owning engine, and optional state.

using var domain = engine.CreateDomain();
Console.WriteLine(ReferenceEquals(domain.Engine, engine));
domain.Global.Define("REQUEST_ID", "r-42");

Execute(moduleName, methodName, ...arguments)

Parameters:

  • moduleName
    @module 名。

    @module name.

  • methodName
    导出函数名。

    Exported function name.

  • arguments
    ScriptDatumScriptObject 实参。

    ScriptDatum or ScriptObject arguments;部分重载也接受调用时的 userState / some overloads also accept call-time userState.

Returns

ScriptDatum 结果。

ScriptDatum result.

using var domain = engine.CreateDomain();
var result = domain.Execute(
    "MAIN",
    "main",
    ScriptDatum.FromNumber(20));

GetMethod(moduleName, methodName)

Parameters:

  • moduleName
    模块名。

    Module name.

  • methodName
    导出函数名。

    Exported function name.

Returns

用于高级调用的 ClosureFunction

ClosureFunction for advanced invocation.

var method = domain.GetMethod("MAIN", "main");
var result = method.InvokeClrDetached(ScriptDatum.FromNumber(20));

通常优先使用 Execute;只有需要控制 ScriptContext 或重复调用闭包时再使用 GetMethod

prefer Execute normally; use GetMethod only when controlling ScriptContext or repeatedly invoking a closure is needed.

GetModule(moduleName)

Parameters:

  • moduleName
    模块名。

    Module name.

Returns

模块对象。

Module object.

var module = domain.GetModule("MAIN");

DynamicPatch(source, patchType) / DynamicPatch(modulePath, script, patchType)

Parameters:

  • sourcemodulePath

    Patch source or target module path.

  • script
    补丁源码。

    Patch source text.

  • patchType
    HotPatchType 标志。

    HotPatchType flags.

Returns

无。

None.

domain.DynamicPatch(
    "scripts/main.as",
    "@module(MAIN); export func run() { return 42; }",
    HotPatchType.Replace);

DynamicPatchAsync(...)

Parameters:

  • 与同步版本相同,另加可选取消令牌。

    Same inputs as synchronous overloads plus optional cancellation token.

Returns

代表补丁完成的 Task

Task for patch completion.

await domain.DynamicPatchAsync(
    "scripts/main.as",
    "export func added() { return 1; }",
    HotPatchType.Incremental);

ReplacePatch(modulePath, script, [ignoreDepends]) / IncrementalPatch(modulePath, script, [ignoreDepends])

Parameters:

  • modulePath
    目标路径。

    Target path.

  • script
    替换或增量源码。

    Replacement or incremental source.

  • ignoreDepends
    是否跳过依赖更新。

    Whether to skip dependency updates.

Returns

无。

None.

domain.ReplacePatch("scripts/main.as", "@module(MAIN); export func run() { return 42; }");
domain.IncrementalPatch("scripts/main.as", "export func added() { return 1; }");

ReplacePatchAsync(...) / IncrementalPatchAsync(...)

Parameters:

  • 与同步版本相同,另加可选取消令牌。

    Same inputs as synchronous versions plus optional cancellation token.

Returns

Task

Task.

await domain.ReplacePatchAsync("scripts/main.as", "@module(MAIN); export func run() { return 42; }");
await domain.IncrementalPatchAsync("scripts/main.as", "export func added() { return 1; }");

Dispose()

Parameters:

  • 无。

    None.

Returns

无。

None.

用途

释放 Domain 关联资源,之后不得再调用它。

Releases domain-associated resources; do not use the domain afterward.

using var domain = engine.CreateDomain();
var result = domain.Execute("MAIN", "run");

Clone this wiki locally