Skip to main content

Default marshaling rules

Explicit JsMarshalAs overrides legal defaults. CLR plans contain no engine SDK values; wrapper/view/borrow semantics are engine-independent.

1. null and undefined​

SituationResult
CLR null/Nullable no value → JSnull
CLR void → JSundefined
JS null → reference/Nullablenull/no value
Required ordinary reference/nonnullable value ← undefinedMissing/mismatch failure
Nullable ← undefinedNo value
Optional missing/undefinedCached CLR default
Omitted params tailEmpty array
JS→CLR outNo input slot

Undefined retains its argument position. Plain JS missing properties return undefined; CLR misses throw. Table checks own-property presence separately from present undefined.

2. Numbers​

bool is boolean; char and integer types are integral number; Single/Double are number. Int64/UInt64 default to number with possible precision loss. IntPtr/UIntPtr/nint/nuint use pointer-width numbers, distinct from pointer tokens. Enums use underlying numbers even when 64-bit.

Integer reads require number, finite/integral value, and target range, not strings/bigint. Int64 checks [-2^63, 2^63) and UInt64 [0, 2^64) so rounded double maxima cannot admit overflow. Values beyond safe JS precision can round; receiving a rounded number cannot recover the original integer.

Explicit BigInt applies only to concrete long/ulong slots and legal byref elements: exact bigint output, bigint-only input, full signed/unsigned range checks. Do not extend to enums, IntPtr, object, whole arrays, or global type defaults. Valid direct Opaque storage remains address-bound; converted values use the slot policy.

[return: JsMarshalAs(JsMarshalType.BigInt)]
public static long GetExactCounter() => long.MaxValue;
public static void SetExactCounter([JsMarshalAs(JsMarshalType.BigInt)] long value) { }
Counter.SetExactCounter(9223372036854775807n);

3. Type matrix​

CLR typeCLR→JSJS→CLR
stringstring/nullString, compatible wrapper, null
class/interface/objectDeclared-view ByObj/nullCompatible wrapper/null; object supports specified primitive boxing
enumnumberIn-range number or compatible boxed wrapper
structDefault ByVal; explicit Opaque where legalCompatible value/boxed/Opaque under struct rules
Nullable<T>null or T representationnull/undefined or convertible T
T[]ByObj/nullWrapper, dense JS Array, null
mdarrayByObj/nullCompatible wrapper/null only
delegateOriginal JS function or callable wrapper/nullCallable, compatible delegate, null
Unmanaged/function pointerPointer token/nullCompatible token/null
TypedReferenceOpaqueCompatible Opaque only
decimal/ref structNo ordinary default by-value promiseOnly explicitly supported paths

object does not guess long versus ulong from bigint. Define concrete annotated slots for exact 64-bit values.

4. Arrays and results​

Ordinary SZArray inputs accept dense zero-based JS Arrays, converted/copied elementwise; holes fail. TypedArrays are not universal CLR arrays. Bytes defines only byte[]/Uint8Array.

Without out retain ordinary return shape; with out use [ret, out1, ...] or [out1, ...] for void. Each return/out is one logical result, including nested UnpackedValues arrays, never recursively flattened.

5. Configuration​

Legal member attributes precede AOT XML, then legal nongeneric type policies and Default. No XML for hot-update assemblies or runtime registration/reload. See policies.