wasm/web: o.isPrototypeOf(x) and o.hasOwnProperty(k) throw "Cannot convert undefined or null to object"

未关闭 适合新手
#15,076 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
backend, tooling

调研方向

从 crates/perry-codegen-wasm/src/wasm_runtime.js 中 mem_call、mem_call_i32 及其分发表附近开始。检查 bridge 查找是否只接受表自身的属性,然后为名为 isPrototypeOf、hasOwnProperty 和 propertyIsEnumerable 的调用添加一个 perry-codegen-wasm 单元测试。这些调用能够到达接收者的方法且测试通过,即为完成;类原型链的行为另行跟踪。

由索引模型根据 Issue 内容生成。

描述

On --target web, calling a method whose name is also an Object.prototype member — isPrototypeOf, hasOwnProperty, propertyIsEnumerable — on an object throws Cannot convert undefined or null to object. The same method called through .call works.

class A {}
class B extends A {}
const base: any = { k: 1 };
const child: any = { own: 2 };
Object.setPrototypeOf(child, base);
const typed: { k: number } = { k: 1 };
const show = (label: string, f: () => any) => { try { console.log(label + ": " + f()); } catch (e: any) { console.log(label + ": threw " + e.message); } };
show("base.isPrototypeOf(child)", () => base.isPrototypeOf(child));
show("Object.prototype.isPrototypeOf({})", () => Object.prototype.isPrototypeOf({}));
show("child.hasOwnProperty('own')", () => child.hasOwnProperty("own"));
show("typed.hasOwnProperty('k')", () => typed.hasOwnProperty("k"));
show("child.propertyIsEnumerable('own')", () => child.propertyIsEnumerable("own"));
show("A.isPrototypeOf(B)", () => A.isPrototypeOf(B));
show("A.prototype.isPrototypeOf(new B())", () => A.prototype.isPrototypeOf(new B()));
show("Object.prototype.hasOwnProperty.call(child, 'own') (control)", () => Object.prototype.hasOwnProperty.call(child, "own"));
case Node 26.12.1 --target web on main
base.isPrototypeOf(child) true threw Cannot convert undefined or null to object
Object.prototype.isPrototypeOf({}) true threw Cannot convert undefined or null to object
child.hasOwnProperty('own') true threw Cannot convert undefined or null to object
typed.hasOwnProperty('k') true threw Cannot convert undefined or null to object
child.propertyIsEnumerable('own') true threw Cannot convert undefined or null to object
A.isPrototypeOf(B) true threw Cannot convert undefined or null to object
A.prototype.isPrototypeOf(new B()) true threw Cannot convert undefined or null to object
Object.prototype.hasOwnProperty.call(child, 'own') (control) true true

Run with perry compile fc.ts --target web -o fc.html, then node scripts/parity_web_host.cjs fc.html. Tested on macOS arm64 against main at 88b1e37142, with Node 26.12.1.

Cause. A method call the emitter does not special-case reaches mem_call under the method's own name, with the receiver as the first argument. mem_call looks that name up with a plain index, __memDispatch[name] (crates/perry-codegen-wasm/src/wasm_runtime.js:1419, and __perryUiDispatch[name] at :1424; mem_call_i32 at :1447). Both tables are object literals, so __memDispatch["isPrototypeOf"] is Object.prototype.isPrototypeOf itself. mem_call calls it as a bridge, coreFn(...args), with no receiver, and it throws. A name that is not an Object.prototype member misses both tables and falls through to __classDispatch(receiver, name, args), which calls the receiver's method. The control is Object.prototype.hasOwnProperty.call(child, "own"), which does not go through the method-name lookup.

Proposed fix. mem_call and mem_call_i32 take only a table's own bridges (Object.prototype.hasOwnProperty.call(table, name)), so these names fall through to __classDispatch like any other method. I'll send a PR with a perry-codegen-wasm unit test.

With that fix, the two class rows stop throwing but print false: A.isPrototypeOf(B) also needs #15053 (B's class value inherits from A's), and A.prototype.isPrototypeOf(new B()) needs instances and class prototypes to share one prototype chain (filed separately).

主要语言
Rust
星标
4.9k
派生
166
平均合并
6 小时 5 分钟
30 天内合并 PR
949

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PerryTS/perry 的其他 Issue

查看 PerryTS/perry 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。