|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const vm = require('vm'); |
| 5 | + |
| 6 | +// This, admittedly contrived, example tests an edge cases of the vm module. |
| 7 | +// |
| 8 | +// The GetterCallback explicitly checks the global_proxy() if a property is |
| 9 | +// not found on the sandbox. In the following test, the explicit check |
| 10 | +// inside the callback yields different results than deferring the |
| 11 | +// check until after the callback. The check is deferred if the |
| 12 | +// callback does not intercept, i.e., if args.GetReturnValue().Set() is |
| 13 | +// not called. |
| 14 | + |
| 15 | +// Check that the GetterCallback explicitly calls GetRealNamedProperty() |
| 16 | +// on the global proxy if the property is not found on the sandbox. |
| 17 | +// |
| 18 | +// foo is not defined on the sandbox until we call CopyProperties(). |
| 19 | +// In the GetterCallback, we do not find the property on the sandbox and |
| 20 | +// get the property from the global proxy. Since the return value is |
| 21 | +// the sandbox, we replace it by |
| 22 | +// the global_proxy to keep the correct identities. |
| 23 | +// |
| 24 | +// This test case is partially inspired by |
| 25 | +// https://github.com/nodejs/node/issues/855 |
| 26 | +const sandbox = {console}; |
| 27 | +sandbox.document = {defaultView: sandbox}; |
| 28 | +vm.createContext(sandbox); |
| 29 | +const code = `Object.defineProperty( |
| 30 | + this, |
| 31 | + 'foo', |
| 32 | + { get: function() {return document.defaultView} } |
| 33 | + ); |
| 34 | + var result = foo === this;`; |
| 35 | + |
| 36 | +vm.runInContext(code, sandbox); |
| 37 | +assert.strictEqual(sandbox.result, true); |
0 commit comments