11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst vm = require('vm'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// These assertions check that we can set new keys to the global context, 71cb0ef41Sopenharmony_ci// get them back and also list them via getOwnProperty* or in. 81cb0ef41Sopenharmony_ci// 91cb0ef41Sopenharmony_ci// Related to: 101cb0ef41Sopenharmony_ci// - https://github.com/nodejs/node/issues/45983 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst global = vm.runInContext('this', vm.createContext()); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cifunction runAssertions(data, property, viaDefine, value1, value2, value3) { 151cb0ef41Sopenharmony_ci // Define the property for the first time 161cb0ef41Sopenharmony_ci setPropertyAndAssert(data, property, viaDefine, value1); 171cb0ef41Sopenharmony_ci // Update the property 181cb0ef41Sopenharmony_ci setPropertyAndAssert(data, property, viaDefine, value2); 191cb0ef41Sopenharmony_ci // Delete the property 201cb0ef41Sopenharmony_ci deletePropertyAndAssert(data, property); 211cb0ef41Sopenharmony_ci // Re-define the property 221cb0ef41Sopenharmony_ci setPropertyAndAssert(data, property, viaDefine, value3); 231cb0ef41Sopenharmony_ci // Delete the property again 241cb0ef41Sopenharmony_ci deletePropertyAndAssert(data, property); 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst fun1 = () => 1; 281cb0ef41Sopenharmony_ciconst fun2 = () => 2; 291cb0ef41Sopenharmony_ciconst fun3 = () => 3; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cifunction runAssertionsOnSandbox(builder) { 321cb0ef41Sopenharmony_ci const sandboxContext = vm.createContext({ runAssertions, fun1, fun2, fun3 }); 331cb0ef41Sopenharmony_ci vm.runInContext(builder('this'), sandboxContext); 341cb0ef41Sopenharmony_ci vm.runInContext(builder('{}'), sandboxContext); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci// Assertions on: define property 381cb0ef41Sopenharmony_cirunAssertions(global, 'toto', true, 1, 2, 3); 391cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('toto'), true, 1, 2, 3); 401cb0ef41Sopenharmony_cirunAssertions(global, 'tutu', true, fun1, fun2, fun3); 411cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('tutu'), true, fun1, fun2, fun3); 421cb0ef41Sopenharmony_cirunAssertions(global, 'tyty', true, fun1, 2, 3); 431cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('tyty'), true, fun1, 2, 3); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci// Assertions on: direct assignment 461cb0ef41Sopenharmony_cirunAssertions(global, 'titi', false, 1, 2, 3); 471cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('titi'), false, 1, 2, 3); 481cb0ef41Sopenharmony_cirunAssertions(global, 'tata', false, fun1, fun2, fun3); 491cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('tata'), false, fun1, fun2, fun3); 501cb0ef41Sopenharmony_cirunAssertions(global, 'tztz', false, fun1, 2, 3); 511cb0ef41Sopenharmony_cirunAssertions(global, Symbol.for('tztz'), false, fun1, 2, 3); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci// Assertions on: define property from sandbox 541cb0ef41Sopenharmony_cirunAssertionsOnSandbox( 551cb0ef41Sopenharmony_ci (variable) => ` 561cb0ef41Sopenharmony_ci runAssertions(${variable}, 'toto', true, 1, 2, 3); 571cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('toto'), true, 1, 2, 3); 581cb0ef41Sopenharmony_ci runAssertions(${variable}, 'tutu', true, fun1, fun2, fun3); 591cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('tutu'), true, fun1, fun2, fun3); 601cb0ef41Sopenharmony_ci runAssertions(${variable}, 'tyty', true, fun1, 2, 3); 611cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('tyty'), true, fun1, 2, 3);` 621cb0ef41Sopenharmony_ci); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci// Assertions on: direct assignment from sandbox 651cb0ef41Sopenharmony_cirunAssertionsOnSandbox( 661cb0ef41Sopenharmony_ci (variable) => ` 671cb0ef41Sopenharmony_ci runAssertions(${variable}, 'titi', false, 1, 2, 3); 681cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('titi'), false, 1, 2, 3); 691cb0ef41Sopenharmony_ci runAssertions(${variable}, 'tata', false, fun1, fun2, fun3); 701cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('tata'), false, fun1, fun2, fun3); 711cb0ef41Sopenharmony_ci runAssertions(${variable}, 'tztz', false, fun1, 2, 3); 721cb0ef41Sopenharmony_ci runAssertions(${variable}, Symbol.for('tztz'), false, fun1, 2, 3);` 731cb0ef41Sopenharmony_ci); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci// Helpers 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci// Set the property on data and assert it worked 781cb0ef41Sopenharmony_cifunction setPropertyAndAssert(data, property, viaDefine, value) { 791cb0ef41Sopenharmony_ci if (viaDefine) { 801cb0ef41Sopenharmony_ci Object.defineProperty(data, property, { 811cb0ef41Sopenharmony_ci enumerable: true, 821cb0ef41Sopenharmony_ci writable: true, 831cb0ef41Sopenharmony_ci value: value, 841cb0ef41Sopenharmony_ci configurable: true, 851cb0ef41Sopenharmony_ci }); 861cb0ef41Sopenharmony_ci } else { 871cb0ef41Sopenharmony_ci data[property] = value; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci assert.strictEqual(data[property], value); 901cb0ef41Sopenharmony_ci assert.ok(property in data); 911cb0ef41Sopenharmony_ci if (typeof property === 'string') { 921cb0ef41Sopenharmony_ci assert.ok(Object.getOwnPropertyNames(data).includes(property)); 931cb0ef41Sopenharmony_ci } else { 941cb0ef41Sopenharmony_ci assert.ok(Object.getOwnPropertySymbols(data).includes(property)); 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci// Delete the property from data and assert it worked 991cb0ef41Sopenharmony_cifunction deletePropertyAndAssert(data, property) { 1001cb0ef41Sopenharmony_ci delete data[property]; 1011cb0ef41Sopenharmony_ci assert.strictEqual(data[property], undefined); 1021cb0ef41Sopenharmony_ci assert.ok(!(property in data)); 1031cb0ef41Sopenharmony_ci assert.ok(!Object.getOwnPropertyNames(data).includes(property)); 1041cb0ef41Sopenharmony_ci assert.ok(!Object.getOwnPropertySymbols(data).includes(property)); 1051cb0ef41Sopenharmony_ci} 106