Repository navigation
fix(runtime,codegen): #5894 — x instanceof <non-constructor namespace> throws TypeError - #5971
Conversation
…> throws TypeError `1 instanceof Math` (and JSON/Reflect/Atomics) must throw a TypeError: these namespace objects have no [[Call]]/[[Construct]]. The codegen static-instanceof path mapped an unknown RHS name to class_id 0, and `js_instanceof(_, 0)` folds to `false` — so `x instanceof Math` wrongly returned false instead of throwing. Add js_instanceof_noncallable_rhs() (throws the 'not callable' TypeError, honoring the SUPPRESS_INSTANCEOF_RHS_THROW scope) and emit it from codegen when the bare-identifier RHS is a known non-constructor built-in namespace not shadowed by a user class. The LHS is still lowered first so its side effects run. Fixes S11.8.6_A6_T2. Normal `x instanceof Class/Array/Error/Object` and the dynamic-RHS path are unchanged. Refs #5894
📝 WalkthroughWalkthroughIntroduces a runtime helper ChangesNon-callable instanceof RHS handling
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Codegen as Perry-codegen (InstanceOf lowering)
participant Runtime as js_instanceof_noncallable_rhs
participant JS as JS Exception Handling
Codegen->>Codegen: Evaluate RHS identifier of instanceof
alt RHS is Math/JSON/Reflect/Atomics and not user-defined
Codegen->>Runtime: Call js_instanceof_noncallable_rhs()
alt SUPPRESS_INSTANCEOF_RHS_THROW set
Runtime-->>Codegen: Return NaN-boxed false
else not suppressed
Runtime->>JS: Throw TypeError
end
else RHS is regular class
Codegen->>Codegen: Continue existing cid-based instanceof path
end
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/perry-codegen/src/expr/instance_misc1.rs`:
- Around line 341-347: The new instanceof guard in instance_misc1 should also
catch globalThis-qualified builtins, not just bare identifiers. Update the logic
around the existing Math/JSON/Reflect/Atomics check so expressions like
globalThis.Math, globalThis.JSON, globalThis.Reflect, and globalThis.Atomics are
recognized and routed to the same js_instanceof_noncallable_rhs throw path
instead of falling through to the class-id lookup in the instanceof handling
code.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 139a81c5-212b-4a90-b736-4649d5bab2eb
📒 Files selected for processing (3)
crates/perry-codegen/src/expr/instance_misc1.rscrates/perry-codegen/src/runtime_decls/strings.rscrates/perry-runtime/src/object/instanceof.rs
| if matches!(ty.as_str(), "Math" | "JSON" | "Reflect" | "Atomics") | ||
| && !ctx.class_ids.contains_key(ty) | ||
| { | ||
| return Ok(ctx | ||
| .block() | ||
| .call(DOUBLE, "js_instanceof_noncallable_rhs", &[])); | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
file='crates/perry-codegen/src/expr/instance_misc1.rs'
wc -l "$file"
sed -n '320,370p' "$file" | cat -nRepository: PerryTS/perry
Length of output: 3205
🏁 Script executed:
#!/bin/bash
set -euo pipefail
file='crates/perry-codegen/src/expr/instance_misc1.rs'
rg -n 'globalThis\.(Math|JSON|Reflect|Atomics|EvalError|URIError|Event|CustomEvent|DOMException)|Math"|JSON"|Reflect"|Atomics"' "$file"
sed -n '370,520p' "$file" | cat -nRepository: PerryTS/perry
Length of output: 11107
globalThis.Math/globalThis.JSON/etc. still bypass the throw path.
The new guard only matches bare identifiers, so globalThis.Math, globalThis.JSON, globalThis.Reflect, and globalThis.Atomics still fall through to the class-id lookup and can hit the js_instanceof(_, 0) -> false path instead of throwing.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@crates/perry-codegen/src/expr/instance_misc1.rs` around lines 341 - 347, The
new instanceof guard in instance_misc1 should also catch globalThis-qualified
builtins, not just bare identifiers. Update the logic around the existing
Math/JSON/Reflect/Atomics check so expressions like globalThis.Math,
globalThis.JSON, globalThis.Reflect, and globalThis.Atomics are recognized and
routed to the same js_instanceof_noncallable_rhs throw path instead of falling
through to the class-id lookup in the instanceof handling code.
Summary
1 instanceof Math(andJSON/Reflect/Atomics) must throw aTypeError— these namespace objects have no[[Call]]/[[Construct]], soInstanceofOperatorrejects them regardless of the LHS.Perry's static-
instanceofcodegen mapped an unrecognized RHS name toclass_id = 0, andjs_instanceof(_, 0)folds tofalse(the #585class_id == 0guard). So1 instanceof Mathwrongly returnedfalseinstead of throwing. (The dynamic RHS path —1 instanceof (Math as any)— already threw correctly; only the statically-folded bare-identifier form was wrong.)Fix
js_instanceof_noncallable_rhs()— throwsTypeError: Right-hand side of 'instanceof' is not callable, honoring the existingSUPPRESS_INSTANCEOF_RHS_THROWscope used by theSymbol.hasInstancehelpers.Math/JSON/Reflect/Atomics) and isn't shadowed by a user class, emit that helper instead of thejs_instanceof(_, 0)fold. The LHS is still lowered first, so its side effects run.Before / after (
language/expressions/instanceof)pass 29 → 30, same other failures (zero regressions). Fixed:
S11.8.6_A6_T2(1 instanceof Math).Verified normal usage is untouched:
b instanceof B/A/Object,[] instanceof Array,new Error() instanceof Errorall stilltrue.The remaining instanceof failures (
F.prototypenot an object;Function.prototypeas RHS; getter side effects) are a separate, more involved root injs_instanceof_dynamicand are out of scope here.Validation
cargo fmt --all -- --checkclean;check_file_size.shOK.Refs #5894. Code-only per contributor guidance.
Summary by CodeRabbit
instanceofnow handles built-in namespace objects likeMath,JSON,Reflect, andAtomicscorrectly.TypeErrorinstead of silently returningfalsein cases where the value is not callable.instanceofbehavior for other values remains unchanged.