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_cirequire('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst vm = require('vm');
261cb0ef41Sopenharmony_ciconst Script = vm.Script;
271cb0ef41Sopenharmony_cilet script = new Script('"passed";');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Run in a new empty context
301cb0ef41Sopenharmony_cilet context = vm.createContext();
311cb0ef41Sopenharmony_cilet result = script.runInContext(context);
321cb0ef41Sopenharmony_ciassert.strictEqual(result, 'passed');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// Create a new pre-populated context
351cb0ef41Sopenharmony_cicontext = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
361cb0ef41Sopenharmony_ciassert.strictEqual(context.foo, 'bar');
371cb0ef41Sopenharmony_ciassert.strictEqual(context.thing, 'lala');
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// Test updating context
401cb0ef41Sopenharmony_ciscript = new Script('foo = 3;');
411cb0ef41Sopenharmony_ciresult = script.runInContext(context);
421cb0ef41Sopenharmony_ciassert.strictEqual(context.foo, 3);
431cb0ef41Sopenharmony_ciassert.strictEqual(context.thing, 'lala');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// Issue GH-227:
461cb0ef41Sopenharmony_ciassert.throws(() => {
471cb0ef41Sopenharmony_ci  vm.runInNewContext('', null, 'some.js');
481cb0ef41Sopenharmony_ci}, {
491cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
501cb0ef41Sopenharmony_ci  name: 'TypeError'
511cb0ef41Sopenharmony_ci});
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Issue GH-1140:
541cb0ef41Sopenharmony_ci// Test runInContext signature
551cb0ef41Sopenharmony_cilet gh1140Exception;
561cb0ef41Sopenharmony_citry {
571cb0ef41Sopenharmony_ci  vm.runInContext('throw new Error()', context, 'expected-filename.js');
581cb0ef41Sopenharmony_ci} catch (e) {
591cb0ef41Sopenharmony_ci  gh1140Exception = e;
601cb0ef41Sopenharmony_ci  assert.match(e.stack, /expected-filename/);
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci// This is outside of catch block to confirm catch block ran.
631cb0ef41Sopenharmony_ciassert.strictEqual(gh1140Exception.toString(), 'Error');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciconst nonContextualObjectError = {
661cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
671cb0ef41Sopenharmony_ci  name: 'TypeError',
681cb0ef41Sopenharmony_ci  message: /must be of type object/
691cb0ef41Sopenharmony_ci};
701cb0ef41Sopenharmony_ciconst contextifiedObjectError = {
711cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
721cb0ef41Sopenharmony_ci  name: 'TypeError',
731cb0ef41Sopenharmony_ci  message: /The "contextifiedObject" argument must be an vm\.Context/
741cb0ef41Sopenharmony_ci};
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci[
771cb0ef41Sopenharmony_ci  [undefined, nonContextualObjectError],
781cb0ef41Sopenharmony_ci  [null, nonContextualObjectError],
791cb0ef41Sopenharmony_ci  [0, nonContextualObjectError],
801cb0ef41Sopenharmony_ci  [0.0, nonContextualObjectError],
811cb0ef41Sopenharmony_ci  ['', nonContextualObjectError],
821cb0ef41Sopenharmony_ci  [{}, contextifiedObjectError],
831cb0ef41Sopenharmony_ci  [[], contextifiedObjectError],
841cb0ef41Sopenharmony_ci].forEach((e) => {
851cb0ef41Sopenharmony_ci  assert.throws(() => { script.runInContext(e[0]); }, e[1]);
861cb0ef41Sopenharmony_ci  assert.throws(() => { vm.runInContext('', e[0]); }, e[1]);
871cb0ef41Sopenharmony_ci});
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci// Issue GH-693:
901cb0ef41Sopenharmony_ci// Test RegExp as argument to assert.throws
911cb0ef41Sopenharmony_ciscript = vm.createScript('const assert = require(\'assert\'); assert.throws(' +
921cb0ef41Sopenharmony_ci                         'function() { throw "hello world"; }, /hello/);',
931cb0ef41Sopenharmony_ci                         'some.js');
941cb0ef41Sopenharmony_ciscript.runInNewContext({ require });
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci// Issue GH-7529
971cb0ef41Sopenharmony_ciscript = vm.createScript('delete b');
981cb0ef41Sopenharmony_cilet ctx = {};
991cb0ef41Sopenharmony_ciObject.defineProperty(ctx, 'b', { configurable: false });
1001cb0ef41Sopenharmony_cictx = vm.createContext(ctx);
1011cb0ef41Sopenharmony_ciassert.strictEqual(script.runInContext(ctx), false);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// Error on the first line of a module should have the correct line and column
1041cb0ef41Sopenharmony_ci// number.
1051cb0ef41Sopenharmony_ci{
1061cb0ef41Sopenharmony_ci  let stack = null;
1071cb0ef41Sopenharmony_ci  assert.throws(() => {
1081cb0ef41Sopenharmony_ci    vm.runInContext(' throw new Error()', context, {
1091cb0ef41Sopenharmony_ci      filename: 'expected-filename.js',
1101cb0ef41Sopenharmony_ci      lineOffset: 32,
1111cb0ef41Sopenharmony_ci      columnOffset: 123
1121cb0ef41Sopenharmony_ci    });
1131cb0ef41Sopenharmony_ci  }, (err) => {
1141cb0ef41Sopenharmony_ci    stack = err.stack;
1151cb0ef41Sopenharmony_ci    return /^ \^/m.test(stack) &&
1161cb0ef41Sopenharmony_ci           /expected-filename\.js:33:131/.test(stack);
1171cb0ef41Sopenharmony_ci  }, `stack not formatted as expected: ${stack}`);
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/6158
1211cb0ef41Sopenharmony_cictx = new Proxy({}, {});
1221cb0ef41Sopenharmony_ciassert.strictEqual(typeof vm.runInNewContext('String', ctx), 'function');
123