Skip to content

API Host BuiltInModuleDefinition

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

BuiltInModuleDefinition

可选原生模块的不可变宿主定义。

命名空间:AuroraScript.Runtime.Package。类型:class。

返回 .NET 宿主 API 参考

概述

BuiltInModuleDefinition 将裸脚本导入路径连接到配置每个运行时 ScriptModule 的工厂回调。定义不可变,可在多个选项实例间复用。引擎为其全局环境和域创建新的模块对象,因此可变模块状态不会在引擎或域之间共享。

大多数宿主应从 BuiltInModules 选择定义。仅在提供自定义原生模块时直接构造此类型。

构造函数

BuiltInModuleDefinition(string name, Action<ScriptModule> configure)
BuiltInModuleDefinition(string name, string modulePath, Action<ScriptModule> configure)
名称 类型 必需 说明
name string 宿主模块 API 和脚本 global.getModule 使用的显式查找名称。必须是合法的 AuroraScript 标识符(非关键字)。
modulePath string 裸导入路径。反斜杠规范化为 /;拒绝根路径、空段、尾部 / 以及 ... 段。
configure Action<ScriptModule> 定义每个新创建模块实例的回调。

两参数构造函数将 name 用作 modulePath

var metrics = new BuiltInModuleDefinition("metrics", module =>
{
    module.Define("now", (Func<double>)(() => Stopwatch.GetTimestamp()));
});

var options = EngineOptions.Default.WithBuiltIns(builtIns =>
    builtIns.Add(metrics));

脚本导入已配置的裸路径:

import metrics from "metrics";

属性

Name

string Name

宿主模块 API、脚本 global.getModule 及名称冲突检测使用的显式查找名称。运行时存储标识仍为内置源 FullPath

ModulePath

string ModulePath

import Alias from "path"; 使用的规范化裸导入路径。

行为与边界

  • 定义不会自行启用。在构造 AuroraEngine 之前将其添加到 EngineOptions.BuiltIns
  • 回调对每个运行时模块实例运行。不要在回调中捕获非预期的共享可变状态。
  • 已启用的内置模块名称和路径必须唯一。
  • 解析器可见的项目模块若其 @module 名称与已启用原生模块冲突,会导致链接错误。
  • 裸路径可包含 /,但模块名称是单个 AuroraScript 标识符。

Clone this wiki locally