// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. // // SPDX-License-Identifier: MPL-2.0 // Copyright (c) 2026 Jairus Tanaka import { allocateObject, linkObject } from "./runtime"; /** Ordinary zero-length Array construction, specialized for its native layout. */ @inline export function newNativeArray(minimumCapacity: usize = 8): T { // Subclasses must run their own constructors. Other runtimes retain the // standard constructor, including their backing-buffer initialization. if (ASC_RUNTIME == 2 && sizeof() == 4 && isArray>()) { if (idof>() == idof>>>()) { const out = changetype>( allocateObject(offsetof>(), idof>()), ); // Ordinary callers keep Array's normal default. Numeric readers can start // with two slots; generated reference-array fields can reserve more to // avoid copying managed storage on early growth. The ordinary base setter // grows smaller buffers as needed. This is separate from empty arrays. // Both allocations remain managed roots and retain distinct identity. const bytes = max(minimumCapacity, 2) << alignof>>(); const buffer = changetype( allocateObject(bytes, idof()), ); const ptr = changetype(out); const data = changetype(buffer); store(ptr, data, offsetof("buffer")); store(ptr, data, offsetof("dataStart")); store(ptr, bytes, offsetof("byteLength")); store(ptr, 0, offsetof>("length_")); linkObject(ptr, data, false); return changetype(out); } } return instantiate(); } /** Read the native slot even when a subclass overrides the public getter. */ @inline export function nativeArrayLength(ptr: usize): i32 { return load(ptr, offsetof>("length_")); } /** Append through native storage, keeping the common no-growth path local. */ @inline export function appendNativeArray( out: T, item: valueof, ): void { const ptr = changetype(out); // Use base-layout fields even when the instance is a subclass. Its public // accessors and operators do not define the parser's native storage policy. const index = load(ptr, offsetof>>("length_")); const shift = alignof>(); const capacity = load(ptr, offsetof("byteLength")) >> shift; if (index < capacity) { const data = load(ptr, offsetof("dataStart")); store>(data + ((index) << shift), item); if (isManaged>()) linkObject(ptr, changetype(item), true); store(ptr, index + 1, offsetof>>("length_")); } else { // The ordinary base setter retains growth, overflow checks, zeroing and // buffer/root barriers. Keep both managed arguments alive through it. changetype>>(out)[index] = item; } } /** * Resize the native layout without virtual length access. Growth still goes * through the base indexed setter, retaining overflow checks, buffer renewal, * and GC barriers. Clear newly exposed reference slots before later parsing * can allocate: a shortened array may contain untraced stale pointers there. */ @inline export function setNativeArrayLength(out: T, length: i32): void { if (length < 0) throw new RangeError("Invalid array length"); const ptr = changetype(out); const previous = nativeArrayLength>(ptr); const shift = alignof>(); if (length > previous) { const capacity = load(ptr, offsetof("byteLength")) >> shift; if (length > capacity) { const storage = changetype>>(out); if (isReference>()) { storage[length - 1] = changetype>(0); } else { storage[length - 1] = >0; } } if (isReference>()) { const data = load(ptr, offsetof("dataStart")); memory.fill( data + ((previous) << shift), 0, ((length - previous)) << shift, ); } } store(ptr, length, offsetof>>("length_")); }