11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ci// Flags: --expose-gc 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst common = require('../common'); 261cb0ef41Sopenharmony_ciconst assert = require('assert'); 271cb0ef41Sopenharmony_ciconst vm = require('vm'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciif (typeof global.gc !== 'function') 301cb0ef41Sopenharmony_ci assert.fail('Run this test with --expose-gc'); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Run a string 331cb0ef41Sopenharmony_ciconst result = vm.runInNewContext('\'passed\';'); 341cb0ef41Sopenharmony_ciassert.strictEqual(result, 'passed'); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci// Thrown error 371cb0ef41Sopenharmony_ciassert.throws(() => { 381cb0ef41Sopenharmony_ci vm.runInNewContext('throw new Error(\'test\');'); 391cb0ef41Sopenharmony_ci}, /^Error: test$/); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciglobal.hello = 5; 421cb0ef41Sopenharmony_civm.runInNewContext('hello = 2'); 431cb0ef41Sopenharmony_ciassert.strictEqual(global.hello, 5); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci// Pass values in and out 471cb0ef41Sopenharmony_ciglobal.code = 'foo = 1;' + 481cb0ef41Sopenharmony_ci 'bar = 2;' + 491cb0ef41Sopenharmony_ci 'if (baz !== 3) throw new Error(\'test fail\');'; 501cb0ef41Sopenharmony_ciglobal.foo = 2; 511cb0ef41Sopenharmony_ciglobal.obj = { foo: 0, baz: 3 }; 521cb0ef41Sopenharmony_ci/* eslint-disable no-unused-vars */ 531cb0ef41Sopenharmony_ciconst baz = vm.runInNewContext(global.code, global.obj); 541cb0ef41Sopenharmony_ci/* eslint-enable no-unused-vars */ 551cb0ef41Sopenharmony_ciassert.strictEqual(global.obj.foo, 1); 561cb0ef41Sopenharmony_ciassert.strictEqual(global.obj.bar, 2); 571cb0ef41Sopenharmony_ciassert.strictEqual(global.foo, 2); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// Call a function by reference 601cb0ef41Sopenharmony_cifunction changeFoo() { global.foo = 100; } 611cb0ef41Sopenharmony_civm.runInNewContext('f()', { f: changeFoo }); 621cb0ef41Sopenharmony_ciassert.strictEqual(global.foo, 100); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci// Modify an object by reference 651cb0ef41Sopenharmony_ciconst f = { a: 1 }; 661cb0ef41Sopenharmony_civm.runInNewContext('f.a = 2', { f }); 671cb0ef41Sopenharmony_ciassert.strictEqual(f.a, 2); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci// Use function in context without referencing context 701cb0ef41Sopenharmony_ciconst fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} }); 711cb0ef41Sopenharmony_ciglobal.gc(); 721cb0ef41Sopenharmony_cifn(); 731cb0ef41Sopenharmony_ci// Should not crash 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciconst filename = 'test_file.vm'; 761cb0ef41Sopenharmony_cifor (const arg of [filename, { filename }]) { 771cb0ef41Sopenharmony_ci // Verify that providing a custom filename works. 781cb0ef41Sopenharmony_ci const code = 'throw new Error("foo");'; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci assert.throws(() => { 811cb0ef41Sopenharmony_ci vm.runInNewContext(code, {}, arg); 821cb0ef41Sopenharmony_ci }, (err) => { 831cb0ef41Sopenharmony_ci const lines = err.stack.split('\n'); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci assert.strictEqual(lines[0].trim(), `${filename}:1`); 861cb0ef41Sopenharmony_ci assert.strictEqual(lines[1].trim(), code); 871cb0ef41Sopenharmony_ci // Skip lines[2] and lines[3]. They're just a ^ and blank line. 881cb0ef41Sopenharmony_ci assert.strictEqual(lines[4].trim(), 'Error: foo'); 891cb0ef41Sopenharmony_ci assert.strictEqual(lines[5].trim(), `at ${filename}:1:7`); 901cb0ef41Sopenharmony_ci // The rest of the stack is uninteresting. 911cb0ef41Sopenharmony_ci return true; 921cb0ef41Sopenharmony_ci }); 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cicommon.allowGlobals( 961cb0ef41Sopenharmony_ci global.hello, 971cb0ef41Sopenharmony_ci global.code, 981cb0ef41Sopenharmony_ci global.foo, 991cb0ef41Sopenharmony_ci global.obj 1001cb0ef41Sopenharmony_ci); 101