Skip to content

TypedArray constructor does not handle iterable and array-like arguments #551

@frostney

Description

@frostney

Summary

new TypedArray(object) where object is an iterable or an array-like (not a TypedArray, ArrayBuffer, or GocciaScript Array) falls through to the numeric-length construction path instead of following ES2026 §23.2.5.1 steps 5-6. The object is coerced to a number via ToNumberLiteral, producing NaN → length 0.

Why

The spec requires two object paths:

  • Step 5a: If the object has @@iterator, iterate it and construct from the values.
  • Step 5b: Otherwise, treat it as an array-like: read length, then Get(obj, k) for each index.

Both paths are missing. This affects any code that constructs typed arrays from plain iterables or array-like objects, and causes 4 test262 conformance failures where the bundled testTypedArray.js harness factories produce length-0 arrays instead of the intended length-1.

Current behavior

./build.pas loaderbare
printf 'print(new Int32Array({0: 7, length: 1}).length);\n' | ./build/GocciaScriptLoaderBare --asi --compat-all
# 0
printf 'print(new Int32Array([7][Symbol.iterator]()).length);\n' | ./build/GocciaScriptLoaderBare --asi --compat-all
# 0

Expected behavior

1
1

Scope notes

The constructor in TGocciaTypedArrayClassValue.CreateNativeInstance (source/units/Goccia.Values.TypedArrayValue.pas:1967) handles TypedArray, ArrayBuffer, SharedArrayBuffer, and GocciaScript Array, then falls through to the numeric-length path. The fix adds two branches before the fallback: check @@iterator (construct from iterable), then array-like (read length + indexed Get).

Blocked test262 tests (currently failing via the testTypedArray.js harness makeArrayLike and makeIterable factories):

  • TypedArray/prototype/includes/return-abrupt-tointeger-fromindex-symbol.js (+ BigInt variant)
  • TypedArray/prototype/indexOf/return-abrupt-tointeger-fromindex-symbol.js
  • TypedArray/prototype/lastIndexOf/return-abrupt-tointeger-fromindex-symbol.js

Surfaced by #526 which added a spec-correct FLength = 0 early return to TypedArray search methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineTGocciaEngine: language semantics, ECMAScript built-ins, parser, interpreter, bytecode VMspec complianceMismatch against official JavaScript/TypeScript specification

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions