vm: TypeError thrown instead of ReferenceError when a Proxy is the vm context
未关闭
还没有人认领这个 Issue。
never-stale
v8 engine
vm
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.3k
- 平均合并
- 4 天 2 小时
- 30 天内合并 PR
- 283
描述
Version
v22.7.0
Platform
Microsoft Windows NT 10.0.22631.0 x64
Subsystem
vm
What steps will reproduce the bug?
"use strict";
const vm = require("vm");
const context = vm.createContext({
__proto__: new Proxy({}, {
get(target, property, receiver) {
return Reflect.get(target, property, receiver);
}
})
});
vm.runInContext("thisFunctionDoesNotExist()", context);
Output:
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior? Why is that the expected behavior?
If I instead do
const context = vm.createContext({
__proto__: new Proxy({}, {})
});
vm.runInContext("thisFunctionDoesNotExist()", context);
or simply
const context = vm.createContext({});
vm.runInContext("thisFunctionDoesNotExist()", context);
then I get the expected output:
$ node test.js
evalmachine.<anonymous>:1
thisFunctionDoesNotExist()
^
ReferenceError: thisFunctionDoesNotExist is not defined
at evalmachine.<anonymous>:1:1
at Script.runInContext (node:vm:148:12)
at Object.runInContext (node:vm:300:6)
What do you see instead?
evalmachine.<anonymous>:1
thisFunctionDoesNotExist()
^
TypeError: thisFunctionDoesNotExist is not a function
at evalmachine.<anonymous>:1:1
at Script.runInContext (node:vm:148:12)
at Object.runInContext (node:vm:300:6)
at Object.<anonymous> (C:\Users\d\OneDrive - domenic.me\Code\GitHub\jsdom\jsdom\test.js:31:4)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
at Module.load (node:internal/modules/cjs/loader:1317:32)
at Module._load (node:internal/modules/cjs/loader:1127:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
Node.js v22.7.0
Additional information
I am not 100% sure this is a vm bug. It may be a fundamental limitation of Proxy and the complicated ECMAScript spec rules governing ReferenceError vs. TypeError. But I am pretty sure this is a vm limitation instead:
- Browsers manage to give a ReferenceError here
- Browsers have something very similar to proxies (the WindowProperties object) as part of their global object
- I can't see anything in the spec for WindowProperties that cannot be emulated by a Proxy.
This is blocking jsdom from passing the web platform test window-runtime-error.html once I implement the WindowProperties object (https://github.com/jsdom/jsdom/pull/3765).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行 issue 中的最小 vm.createContext 和 vm.runInContext 复现程序,比较各个 Proxy 变体与普通 context。使用链接的 window-runtime-error.html 测试作为兼容性目标;当基于 Proxy 的 context 对缺失函数报告 ReferenceError,并与浏览器和非 Proxy 情况一致时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, nodejs
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100