-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm,src: add property query interceptors
Distinguish named property and indexed property when enumerating keys and handle query interceptions. Co-Authored-By: Michaël Zasso <[email protected]>
- Loading branch information
1 parent
f7d82a1
commit 09b0f0f
Showing
4 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
require('../common'); | ||
const vm = require('vm'); | ||
const assert = require('assert'); | ||
|
||
// Regression of https://github.com/nodejs/node/issues/53346 | ||
|
||
const cases = [ | ||
{ | ||
get key() { | ||
return "value"; | ||
}, | ||
}, | ||
{ | ||
set key(value) {}, | ||
}, | ||
{}, | ||
{ | ||
key: "value", | ||
}, | ||
(new class GetterObject { | ||
get key() { | ||
return "value"; | ||
} | ||
}), | ||
(new class SetterObject { | ||
set key(value) { | ||
// noop | ||
} | ||
}), | ||
[], | ||
[["key", "value"]], | ||
Check failure on line 32 in test/parallel/test-vm-global-property-enumerator.js GitHub Actions / lint-js-and-md
|
||
{ | ||
__proto__: { | ||
key: "value", | ||
}, | ||
}, | ||
]; | ||
|
||
for (const [idx, obj] of cases.entries()) { | ||
const ctx = vm.createContext(obj); | ||
const globalObj = vm.runInContext("this", ctx); | ||
const keys = Object.keys(globalObj); | ||
assert.deepStrictEqual(keys, Object.keys(obj), `Case ${idx} failed`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
const sandbox = { | ||
onSelf: 'onSelf', | ||
}; | ||
|
||
function onSelfGetter() { | ||
return 'onSelfGetter'; | ||
} | ||
|
||
Object.defineProperty(sandbox, 'onSelfGetter', { | ||
get: onSelfGetter, | ||
}); | ||
|
||
Object.defineProperty(sandbox, 1, { | ||
value: 'onSelfIndexed', | ||
writable: false, | ||
enumerable: false, | ||
configurable: true, | ||
}); | ||
|
||
const ctx = vm.createContext(sandbox); | ||
|
||
const result = vm.runInContext(` | ||
Object.prototype.onProto = 'onProto'; | ||
Object.defineProperty(Object.prototype, 'onProtoGetter', { | ||
get() { | ||
return 'onProtoGetter'; | ||
}, | ||
}); | ||
Object.defineProperty(Object.prototype, 2, { | ||
value: 'onProtoIndexed', | ||
writable: false, | ||
enumerable: false, | ||
configurable: true, | ||
}); | ||
const resultHasOwn = { | ||
onSelf: Object.hasOwn(this, 'onSelf'), | ||
onSelfGetter: Object.hasOwn(this, 'onSelfGetter'), | ||
onSelfIndexed: Object.hasOwn(this, 1), | ||
onProto: Object.hasOwn(this, 'onProto'), | ||
onProtoGetter: Object.hasOwn(this, 'onProtoGetter'), | ||
onProtoIndexed: Object.hasOwn(this, 2), | ||
}; | ||
const getDesc = (prop) => Object.getOwnPropertyDescriptor(this, prop); | ||
const resultDesc = { | ||
onSelf: getDesc('onSelf'), | ||
onSelfGetter: getDesc('onSelfGetter'), | ||
onSelfIndexed: getDesc(1), | ||
onProto: getDesc('onProto'), | ||
onProtoGetter: getDesc('onProtoGetter'), | ||
onProtoIndexed: getDesc(2), | ||
}; | ||
({ | ||
resultHasOwn, | ||
resultDesc, | ||
}); | ||
`, ctx); | ||
|
||
// eslint-disable-next-line no-restricted-properties | ||
assert.deepEqual(result, { | ||
resultHasOwn: { | ||
onSelf: true, | ||
onSelfGetter: true, | ||
onSelfIndexed: true, | ||
onProto: false, | ||
onProtoGetter: false, | ||
onProtoIndexed: false, | ||
}, | ||
resultDesc: { | ||
onSelf: { value: 'onSelf', writable: true, enumerable: true, configurable: true }, | ||
onSelfGetter: { get: onSelfGetter, set: undefined, enumerable: false, configurable: false }, | ||
onSelfIndexed: { value: 'onSelfIndexed', writable: false, enumerable: false, configurable: true }, | ||
onProto: undefined, | ||
onProtoGetter: undefined, | ||
onProtoIndexed: undefined, | ||
}, | ||
}); |