summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTimothy Gu <timothygu99@gmail.com>2018-08-18 14:04:05 -0400
committerTimothy Gu <timothygu99@gmail.com>2018-08-23 23:09:19 -0400
commit85c356c10eec14f96eaf92ffc9a8481b591e3652 (patch)
treec04257cb8f6770e0d9d5dbfe6e8a1f6e4c617dfe /test
parent349612b233a1af261991143b22e59461f075a987 (diff)
downloadandroid-node-v8-85c356c10eec14f96eaf92ffc9a8481b591e3652.tar.gz
android-node-v8-85c356c10eec14f96eaf92ffc9a8481b591e3652.tar.bz2
android-node-v8-85c356c10eec14f96eaf92ffc9a8481b591e3652.zip
src: implement query callbacks for vm
This allows using a Proxy object as the sandbox for a VM context. PR-URL: https://github.com/nodejs/node/pull/22390 Fixes: https://github.com/nodejs/node/issues/17480 Fixes: https://github.com/nodejs/node/issues/17481 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-vm-proxy.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/test/parallel/test-vm-proxy.js b/test/parallel/test-vm-proxy.js
new file mode 100644
index 0000000000..e83ba0dee9
--- /dev/null
+++ b/test/parallel/test-vm-proxy.js
@@ -0,0 +1,83 @@
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const sandbox = {};
+const proxyHandlers = {};
+const contextifiedProxy = vm.createContext(new Proxy(sandbox, proxyHandlers));
+
+// One must get the globals and manually assign it to our own global object, to
+// mitigate against https://github.com/nodejs/node/issues/17465.
+const contextThis = vm.runInContext('this', contextifiedProxy);
+for (const prop of Reflect.ownKeys(contextThis)) {
+ const descriptor = Object.getOwnPropertyDescriptor(contextThis, prop);
+ Object.defineProperty(sandbox, prop, descriptor);
+}
+
+// Finally, activate the proxy.
+const numCalled = {};
+for (const hook of Reflect.ownKeys(Reflect)) {
+ numCalled[hook] = 0;
+ proxyHandlers[hook] = (...args) => {
+ numCalled[hook]++;
+ return Reflect[hook](...args);
+ };
+}
+
+{
+ // Make sure the `in` operator only calls `getOwnPropertyDescriptor` and not
+ // `get`.
+ // Refs: https://github.com/nodejs/node/issues/17480
+ assert.strictEqual(vm.runInContext('"a" in this', contextifiedProxy), false);
+ assert.deepStrictEqual(numCalled, {
+ defineProperty: 0,
+ deleteProperty: 0,
+ apply: 0,
+ construct: 0,
+ get: 0,
+ getOwnPropertyDescriptor: 1,
+ getPrototypeOf: 0,
+ has: 0,
+ isExtensible: 0,
+ ownKeys: 0,
+ preventExtensions: 0,
+ set: 0,
+ setPrototypeOf: 0
+ });
+}
+
+{
+ // Make sure `Object.getOwnPropertyDescriptor` only calls
+ // `getOwnPropertyDescriptor` and not `get`.
+ // Refs: https://github.com/nodejs/node/issues/17481
+
+ // Get and store the function in a lexically scoped variable to avoid
+ // interfering with the actual test.
+ vm.runInContext(
+ 'const { getOwnPropertyDescriptor } = Object;',
+ contextifiedProxy);
+
+ for (const p of Reflect.ownKeys(numCalled)) {
+ numCalled[p] = 0;
+ }
+
+ assert.strictEqual(
+ vm.runInContext('getOwnPropertyDescriptor(this, "a")', contextifiedProxy),
+ undefined);
+ assert.deepStrictEqual(numCalled, {
+ defineProperty: 0,
+ deleteProperty: 0,
+ apply: 0,
+ construct: 0,
+ get: 0,
+ getOwnPropertyDescriptor: 1,
+ getPrototypeOf: 0,
+ has: 0,
+ isExtensible: 0,
+ ownKeys: 0,
+ preventExtensions: 0,
+ set: 0,
+ setPrototypeOf: 0
+ });
+}