forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-vm-proxy-sandbox-property-query.js
More file actions
74 lines (68 loc) · 2.25 KB
/
Copy pathtest-vm-proxy-sandbox-property-query.js
File metadata and controls
74 lines (68 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
require('../common');
const assert = require('node:assert');
const vm = require('node:vm');
// This test ensures that Proxy-backed vm sandboxes report own properties
// consistently across descriptor and membership queries.
const sandbox = new Proxy({}, {});
const ctx = vm.createContext(sandbox);
vm.runInContext(`
Object.defineProperty(this, 'foo', {
value: 42,
writable: true,
enumerable: true,
configurable: true,
});
Object.defineProperty(this, 'hidden', {
value: 1,
enumerable: false,
configurable: true,
});
Object.defineProperty(this, 'readonly', {
value: 2,
writable: false,
enumerable: true,
configurable: true,
});
`, ctx);
const result = vm.runInContext(`({
value: foo,
descriptor: Object.getOwnPropertyDescriptor(globalThis, 'foo'),
ownNamesIncludes: Object.getOwnPropertyNames(globalThis).includes('foo'),
hasOwnProperty:
Object.prototype.hasOwnProperty.call(globalThis, 'foo'),
objectHasOwn: Object.hasOwn(globalThis, 'foo'),
inGlobalThis: 'foo' in globalThis,
reflectHas: Reflect.has(globalThis, 'foo'),
keysIncludesFoo: Object.keys(globalThis).includes('foo'),
keysIncludesHidden: Object.keys(globalThis).includes('hidden'),
hiddenIsEnumerable:
Object.prototype.propertyIsEnumerable.call(globalThis, 'hidden'),
readonlyDescriptor:
Object.getOwnPropertyDescriptor(globalThis, 'readonly'),
hasOwnToString: Object.hasOwn(globalThis, 'toString'),
toStringInGlobalThis: 'toString' in globalThis,
})`, ctx);
assert.strictEqual(result.value, 42);
assert.deepStrictEqual({ ...result.descriptor }, {
value: 42,
writable: true,
enumerable: true,
configurable: true,
});
assert.strictEqual(result.ownNamesIncludes, true);
assert.strictEqual(result.hasOwnProperty, true);
assert.strictEqual(result.objectHasOwn, true);
assert.strictEqual(result.inGlobalThis, true);
assert.strictEqual(result.reflectHas, true);
assert.strictEqual(result.keysIncludesFoo, true);
assert.strictEqual(result.keysIncludesHidden, false);
assert.strictEqual(result.hiddenIsEnumerable, false);
assert.deepStrictEqual({ ...result.readonlyDescriptor }, {
value: 2,
writable: false,
enumerable: true,
configurable: true,
});
assert.strictEqual(result.hasOwnToString, false);
assert.strictEqual(result.toStringInGlobalThis, true);