11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst vm = require('vm');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// This, admittedly contrived, example tests an edge cases of the vm module.
71cb0ef41Sopenharmony_ci//
81cb0ef41Sopenharmony_ci// The GetterCallback explicitly checks the global_proxy() if a property is
91cb0ef41Sopenharmony_ci// not found on the sandbox. In the following test, the explicit check
101cb0ef41Sopenharmony_ci// inside the callback yields different results than deferring the
111cb0ef41Sopenharmony_ci// check until after the callback. The check is deferred if the
121cb0ef41Sopenharmony_ci// callback does not intercept, i.e., if args.GetReturnValue().Set() is
131cb0ef41Sopenharmony_ci// not called.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Check that the GetterCallback explicitly calls GetRealNamedProperty()
161cb0ef41Sopenharmony_ci// on the global proxy if the property is not found on the sandbox.
171cb0ef41Sopenharmony_ci//
181cb0ef41Sopenharmony_ci// foo is not defined on the sandbox until we call CopyProperties().
191cb0ef41Sopenharmony_ci// In the GetterCallback, we do not find the property on the sandbox and
201cb0ef41Sopenharmony_ci// get the property from the global proxy. Since the return value is
211cb0ef41Sopenharmony_ci// the sandbox, we replace it by
221cb0ef41Sopenharmony_ci// the global_proxy to keep the correct identities.
231cb0ef41Sopenharmony_ci//
241cb0ef41Sopenharmony_ci// This test case is partially inspired by
251cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/855
261cb0ef41Sopenharmony_ciconst sandbox = { console };
271cb0ef41Sopenharmony_cisandbox.document = { defaultView: sandbox };
281cb0ef41Sopenharmony_civm.createContext(sandbox);
291cb0ef41Sopenharmony_ciconst code = `Object.defineProperty(
301cb0ef41Sopenharmony_ci               this,
311cb0ef41Sopenharmony_ci               'foo',
321cb0ef41Sopenharmony_ci               { get: function() {return document.defaultView} }
331cb0ef41Sopenharmony_ci             );
341cb0ef41Sopenharmony_ci             var result = foo === this;`;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_civm.runInContext(code, sandbox);
371cb0ef41Sopenharmony_ciassert.strictEqual(sandbox.result, true);
38