-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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 <targos@protonmail.com> PR-URL: #53517 Fixes: #52720 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
1 parent
213fea0
commit c0962dc
Showing
4 changed files
with
255 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,49 @@ | ||
'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'; | ||
}, | ||
}, | ||
{ | ||
// Intentionally single setter. | ||
// eslint-disable-next-line accessor-pairs | ||
set key(value) {}, | ||
}, | ||
{}, | ||
{ | ||
key: 'value', | ||
}, | ||
(new class GetterObject { | ||
get key() { | ||
return 'value'; | ||
} | ||
}()), | ||
(new class SetterObject { | ||
// Intentionally single setter. | ||
// eslint-disable-next-line accessor-pairs | ||
set key(value) { | ||
// noop | ||
} | ||
}()), | ||
[], | ||
[['key', 'value']], | ||
{ | ||
__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, | ||
}, | ||
}); |