Repository navigation
fix(schema): recognize cross-realm arrays with Array.isArray - #1646
Conversation
ProtoNode and objectKindOf used instanceof Array, which rejects arrays from other windows/vm contexts. Use Array.isArray (and the compiled equivalent) so number[] accepts foreign-realm arrays (fixes arktypeio#1597).
There was a problem hiding this comment.
Important
The fix itself is correct, but the newly-added test fails as written, so the suite is red. attest(T(foreign)).equals([1, 2, 3]) at array.test.ts:294 throws Objects did not have the same constructor because attest's equals rejects mismatched constructors, and a cross-realm array's constructor differs from a local [1, 2, 3] literal.
Reviewed changes
proto.ts—ProtoNodenow routes theArrayproto throughArray.isArrayin bothtraverseAllowsandcompiledCondition, so arrays from other realms validate.objectKinds.ts—objectKindOfearly-returns"Array"for anyArray.isArrayvalue, keeping kind/error descriptions accurate across realms.array.test.ts— adds avm-realm cross-realm array test.
I verified the runtime behavior directly: with the fix, type("number[]").allows returns true for a vm-realm [1, 2, 3] (and reverting proto.ts to instanceof Array breaks it), so the fix is sound. The only problem is the test's final assertion, which fails regardless of the fix and must be corrected before this can merge.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
attest().equals treats foreign-realm arrays as unequal to literals because their constructors differ. Assert identity + element values.
Unblocks arktype CI prChecks (prettier --check on proto.ts).
|
@pullfrog addressed: the test no longer uses `attest(...).equals` against a foreign-realm array (constructor identity fails). It asserts identity + element values instead. CI `core` / `prChecks` / compatibility are green on the latest head. |
There was a problem hiding this comment.
✅ No new issues found.
Confirmed the fix: array.test.ts no longer calls attest(...).equals against the foreign-realm array (whose constructor differs, which attest treats as unequal). It now asserts realm-safe facts — Array.isArray(foreign) is true, T.allows(foreign) is true, T(foreign) returns the same object (result === foreign) — and compares element values via a local spread [...(result as number[])].
pnpm mocha ark/type/__tests__/arrays/array.test.ts passes accepts arrays from another realm. (The benchTemplate/snapTemplate module-not-found output in that run comes from the ark/attest dev build not being present locally and is unrelated to this PR.) The earlier review blocker is resolved.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
|
@pullfrog based on the intent of this fix, are there other prototypes with similar built-in methods for validating instances cross-realm that should also be included as part of this for consistency? |
|
Good question. I dug through the builtin constructors ( Here's the breakdown: Other prototypes with a built-in realm-safe check:
Prototypes with no dedicated static predicate ( One consistency gap worth flagging: the So my recommendation, if we want consistency rather than a one-off:
I'd lean toward the general
|
|
@pullfrog implement an alternate version of this that universally uses the Object.prototype.toString.call... I assume this can't be tricked by just naming a particular prototype directly and it will only return true for the built in? If so, build this version on another branch and simplify as much as possible so instanceof works consistently across all builtins cross-realm. |
|
Run failed. View the logs →
|
|
Re: the const spoofed = { [Symbol.toStringTag]: "Array", length: 3 };
Object.prototype.toString.call(spoofed); // "[object Array]" — false positive
Array.isArray(spoofed); // false — correct
const foreign = vm.runInContext("[1,2,3]", vm.createContext());
Object.prototype.toString.call(foreign); // "[object Array]" — correct
Array.isArray(foreign); // true — also correct
Separately: the |
|
@edenbuilds I figured that may be the case and probably should have googled it, just trying to inflate pullfrog usage numbers as per usual 😆 I'm not sure about this. I guess it makes sense to give arrays some special status as they are uniquely represented as |
|
Fair question. The reason Array gets special-cased rather than a general `instanceof`-is-unsafe-across-realms fix: it's not just Array that's spoofable via `Symbol.toStringTag` — that trick works on any object, including a real `Date` (`realDate[Symbol.toStringTag] = "X"` makes `Object.prototype.toString.call(realDate)` return `"[object X]"` too, spec-wise the tag lookup happens before the internal-slot fallback). So there's no `toString.call`-based fix for the general case either — it has the same weakness for every built-in, not just Array. What actually differs for Array is that the language shipped a dedicated, unspoofable primitive for exactly this problem (`Array.isArray`, added specifically because `instanceof Array` breaks across iframes/vm/workers), and nothing equivalent exists for If cross-realm `Date`/`Map`/etc. is something you want covered too, happy to look at what's feasible (probably an opt-in Node-only path, or documenting it as a known limitation) — but wanted to flag that it's a materially different problem, not a copy-paste of this fix, so I'd treat it as a separate issue/PR rather than scope creep on this one. |
|
@edenbuilds this makes sense, I buy the existence of a language feature being sufficient justification for having this special-cased. @pullfrog add a note to the documentation for instanceof that Array is the only constructor that will work cross-realm. |
|
Added the requested note to the
|
|
@edenbuilds thanks again for your help :-) |

Summary
ProtoNodevalidated arrays withinstanceof Array(and the same in the JITcompiledCondition), which fails for arrays from another window/vmrealm.Array.isArrayfor the Array proto; early-return"Array"fromobjectKindOfthe same way so error text stays accurate.Fixes #1597
Test plan
vmforeign[1,2,3]→allowsfalse on main, true with fix;T(foreign)returns[1,2,3]accepts arrays from another realminark/type/__tests__/arrays/array.test.tsproto.ts/objectKinds.tsare stashedMade with Cursor