Skip to content

API Host Extended

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

Extended

Host callback helpers for reading ScriptDatum spans and checking runtime value flags.

Namespace: AuroraScript. Kind: class.

Back to .NET Host API Reference

Overview

Extended is a static class of extension methods. The span readers pull a typed value out of a positional argument span without copying or boxing it, and Include tests a ValueKind flag.

Note

These helpers are mainly useful inside ClrDatumDelegate or other raw ScriptDatum host callbacks.

Quick Reference

A minimal use of Extended.

Span<ScriptDatum> args = [ScriptDatum.FromNumber(42)];

if (args.TryGetInt32(0, out var value))
{
    Console.WriteLine(value); // 42
}

Methods

Include

static bool Include(this ValueKind valueKind, ValueKind flag)

Tests whether a value kind contains a flag.

Parameters

Name Type Required Description
valueKind ValueKind Yes Value kind to inspect.
flag ValueKind Yes Flag to test for.

Returns

true when the flag is set; otherwise false.

if (datum.Kind.Include(ValueKind.String))
{
    Console.WriteLine(ScriptDatum.ToString(datum));
}

Span readers

static bool TryGetNumber(this Span<ScriptDatum> source, int index, out double value)
static bool TryGetInteger(this Span<ScriptDatum> source, int index, out long value)
static bool TryGetInt32(this Span<ScriptDatum> source, int index, out int value)
static bool TryGetStrictNumber(this Span<ScriptDatum> source, int index, out double value)
static bool TryGetString(this Span<ScriptDatum> source, int index, out string value)
static bool TryGetObject(this Span<ScriptDatum> source, int index, out ScriptObject value)
static bool TryGetEnumerator(this Span<ScriptDatum> source, int index, out ScriptEnumerator value)
static bool TryGetFunction(this Span<ScriptDatum> source, int index, out ClosureFunction value)
static bool TryGetBoolean(this Span<ScriptDatum> source, int index, out bool value)
static bool TryGetRef(this Span<ScriptDatum> source, int index, ref ScriptDatum value)

Reads one positional argument as a specific type.

Parameters

Name Type Required Description
source Span<ScriptDatum> Yes Argument span supplied to the host callback.
index int Yes Zero-based argument index to read.
value out or ref parameter Yes Receives the read value. Its type depends on the reader.

Returns

true when the argument exists and matches the requested type; otherwise false.

Behavior

  • TryGetNumber applies script number coercion; TryGetStrictNumber accepts number values only.
  • TryGetRef takes the target by ref so the callback can read and write the same slot.
  • Reading past the end of source returns false instead of throwing, so a callback can treat missing arguments as optional.
ClrDatumDelegate add = (
    ScriptContext ctx,
    ScriptObject module,
    Span<ScriptDatum> args,
    ref ScriptDatum result) =>
{
    if (args.TryGetNumber(0, out var left) && args.TryGetNumber(1, out var right))
        result = ScriptDatum.FromNumber(left + right);
};

Clone this wiki locally