Skip to content

API Special Values

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

内置上下文值

$state and $args

脚本 API 目录

Script API Directory

概述

Overview

$state$args 不是构造器,而是编译器提供的上下文值。它们不能被用作 CompileBlock 的参数名。

$state and $args are compiler-provided context values, not constructors. They cannot be used as CompileBlock parameter names.

$state

用途

读取宿主在创建 ScriptDomain 时提供的 ScriptObject 用户状态。

Reads the ScriptObject user state supplied by the host when it creates a ScriptDomain.

Returns

没有状态时为 null,否则为宿主提供的对象。

null when no state exists, otherwise the host-supplied object.

@module(MAIN);

export func userName() {
    return $state.Name;
}

$args

用途

读取当前函数调用收到的全部参数。

Reads all arguments received by the current function call.

Returns

参数集合;可通过 length 和数值索引读取。

An argument collection readable through length and numeric indexes.

@module(MAIN);

export func last() {
    return $args[$args.length - 1];
}

边界行为

优先使用显式函数参数;仅当调用方允许可变参数时使用 $args

Prefer explicit function parameters; use $args only when variable arity is part of the contract.

Clone this wiki locally