11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst vm = require('vm'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst dSymbol = Symbol('d'); 71cb0ef41Sopenharmony_ciconst sandbox = { 81cb0ef41Sopenharmony_ci a: 'a', 91cb0ef41Sopenharmony_ci dSymbol 101cb0ef41Sopenharmony_ci}; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciObject.defineProperties(sandbox, { 131cb0ef41Sopenharmony_ci b: { 141cb0ef41Sopenharmony_ci value: 'b' 151cb0ef41Sopenharmony_ci }, 161cb0ef41Sopenharmony_ci c: { 171cb0ef41Sopenharmony_ci value: 'c', 181cb0ef41Sopenharmony_ci writable: true, 191cb0ef41Sopenharmony_ci enumerable: true 201cb0ef41Sopenharmony_ci }, 211cb0ef41Sopenharmony_ci [dSymbol]: { 221cb0ef41Sopenharmony_ci value: 'd' 231cb0ef41Sopenharmony_ci }, 241cb0ef41Sopenharmony_ci e: { 251cb0ef41Sopenharmony_ci value: 'e', 261cb0ef41Sopenharmony_ci configurable: true 271cb0ef41Sopenharmony_ci }, 281cb0ef41Sopenharmony_ci f: {} 291cb0ef41Sopenharmony_ci}); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst ctx = vm.createContext(sandbox); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciconst result = vm.runInContext(` 341cb0ef41Sopenharmony_ciconst getDesc = (prop) => Object.getOwnPropertyDescriptor(this, prop); 351cb0ef41Sopenharmony_ciconst result = { 361cb0ef41Sopenharmony_ci a: getDesc('a'), 371cb0ef41Sopenharmony_ci b: getDesc('b'), 381cb0ef41Sopenharmony_ci c: getDesc('c'), 391cb0ef41Sopenharmony_ci d: getDesc(dSymbol), 401cb0ef41Sopenharmony_ci e: getDesc('e'), 411cb0ef41Sopenharmony_ci f: getDesc('f'), 421cb0ef41Sopenharmony_ci g: getDesc('g') 431cb0ef41Sopenharmony_ci}; 441cb0ef41Sopenharmony_ciresult; 451cb0ef41Sopenharmony_ci`, ctx); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci// eslint-disable-next-line no-restricted-properties 481cb0ef41Sopenharmony_ciassert.deepEqual(result, { 491cb0ef41Sopenharmony_ci a: { value: 'a', writable: true, enumerable: true, configurable: true }, 501cb0ef41Sopenharmony_ci b: { value: 'b', writable: false, enumerable: false, configurable: false }, 511cb0ef41Sopenharmony_ci c: { value: 'c', writable: true, enumerable: true, configurable: false }, 521cb0ef41Sopenharmony_ci d: { value: 'd', writable: false, enumerable: false, configurable: false }, 531cb0ef41Sopenharmony_ci e: { value: 'e', writable: false, enumerable: false, configurable: true }, 541cb0ef41Sopenharmony_ci f: { 551cb0ef41Sopenharmony_ci value: undefined, 561cb0ef41Sopenharmony_ci writable: false, 571cb0ef41Sopenharmony_ci enumerable: false, 581cb0ef41Sopenharmony_ci configurable: false 591cb0ef41Sopenharmony_ci }, 601cb0ef41Sopenharmony_ci g: undefined 611cb0ef41Sopenharmony_ci}); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Define new properties 641cb0ef41Sopenharmony_civm.runInContext(` 651cb0ef41Sopenharmony_ciObject.defineProperty(this, 'h', {value: 'h'}); 661cb0ef41Sopenharmony_ciObject.defineProperty(this, 'i', {}); 671cb0ef41Sopenharmony_ciObject.defineProperty(this, 'j', { 681cb0ef41Sopenharmony_ci get() { return 'j'; } 691cb0ef41Sopenharmony_ci}); 701cb0ef41Sopenharmony_cilet kValue = 0; 711cb0ef41Sopenharmony_ciObject.defineProperty(this, 'k', { 721cb0ef41Sopenharmony_ci get() { return kValue; }, 731cb0ef41Sopenharmony_ci set(value) { kValue = value } 741cb0ef41Sopenharmony_ci}); 751cb0ef41Sopenharmony_ci`, ctx); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciassert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'h'), { 781cb0ef41Sopenharmony_ci value: 'h', 791cb0ef41Sopenharmony_ci writable: false, 801cb0ef41Sopenharmony_ci enumerable: false, 811cb0ef41Sopenharmony_ci configurable: false 821cb0ef41Sopenharmony_ci}); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciassert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'i'), { 851cb0ef41Sopenharmony_ci value: undefined, 861cb0ef41Sopenharmony_ci writable: false, 871cb0ef41Sopenharmony_ci enumerable: false, 881cb0ef41Sopenharmony_ci configurable: false 891cb0ef41Sopenharmony_ci}); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ciconst jDesc = Object.getOwnPropertyDescriptor(ctx, 'j'); 921cb0ef41Sopenharmony_ciassert.strictEqual(typeof jDesc.get, 'function'); 931cb0ef41Sopenharmony_ciassert.strictEqual(typeof jDesc.set, 'undefined'); 941cb0ef41Sopenharmony_ciassert.strictEqual(jDesc.enumerable, false); 951cb0ef41Sopenharmony_ciassert.strictEqual(jDesc.configurable, false); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciconst kDesc = Object.getOwnPropertyDescriptor(ctx, 'k'); 981cb0ef41Sopenharmony_ciassert.strictEqual(typeof kDesc.get, 'function'); 991cb0ef41Sopenharmony_ciassert.strictEqual(typeof kDesc.set, 'function'); 1001cb0ef41Sopenharmony_ciassert.strictEqual(kDesc.enumerable, false); 1011cb0ef41Sopenharmony_ciassert.strictEqual(kDesc.configurable, false); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciassert.strictEqual(ctx.k, 0); 1041cb0ef41Sopenharmony_cictx.k = 1; 1051cb0ef41Sopenharmony_ciassert.strictEqual(ctx.k, 1); 1061cb0ef41Sopenharmony_ciassert.strictEqual(vm.runInContext('k;', ctx), 1); 1071cb0ef41Sopenharmony_civm.runInContext('k = 2;', ctx); 1081cb0ef41Sopenharmony_ciassert.strictEqual(ctx.k, 2); 1091cb0ef41Sopenharmony_ciassert.strictEqual(vm.runInContext('k;', ctx), 2); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci// Redefine properties on the global object 1121cb0ef41Sopenharmony_ciassert.strictEqual(typeof vm.runInContext('encodeURI;', ctx), 'function'); 1131cb0ef41Sopenharmony_ciassert.strictEqual(ctx.encodeURI, undefined); 1141cb0ef41Sopenharmony_civm.runInContext(` 1151cb0ef41Sopenharmony_ciObject.defineProperty(this, 'encodeURI', { value: 42 }); 1161cb0ef41Sopenharmony_ci`, ctx); 1171cb0ef41Sopenharmony_ciassert.strictEqual(vm.runInContext('encodeURI;', ctx), 42); 1181cb0ef41Sopenharmony_ciassert.strictEqual(ctx.encodeURI, 42); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci// Redefine properties on the sandbox 1211cb0ef41Sopenharmony_civm.runInContext(` 1221cb0ef41Sopenharmony_ciObject.defineProperty(this, 'e', { value: 'newE' }); 1231cb0ef41Sopenharmony_ci`, ctx); 1241cb0ef41Sopenharmony_ciassert.strictEqual(ctx.e, 'newE'); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ciassert.throws(() => vm.runInContext(` 1271cb0ef41Sopenharmony_ci'use strict'; 1281cb0ef41Sopenharmony_ciObject.defineProperty(this, 'f', { value: 'newF' }); 1291cb0ef41Sopenharmony_ci`, ctx), /TypeError: Cannot redefine property: f/); 130