1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25const vm = require('vm'); 26const Script = vm.Script; 27let script = new Script('"passed";'); 28 29// Run in a new empty context 30let context = vm.createContext(); 31let result = script.runInContext(context); 32assert.strictEqual(result, 'passed'); 33 34// Create a new pre-populated context 35context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' }); 36assert.strictEqual(context.foo, 'bar'); 37assert.strictEqual(context.thing, 'lala'); 38 39// Test updating context 40script = new Script('foo = 3;'); 41result = script.runInContext(context); 42assert.strictEqual(context.foo, 3); 43assert.strictEqual(context.thing, 'lala'); 44 45// Issue GH-227: 46assert.throws(() => { 47 vm.runInNewContext('', null, 'some.js'); 48}, { 49 code: 'ERR_INVALID_ARG_TYPE', 50 name: 'TypeError' 51}); 52 53// Issue GH-1140: 54// Test runInContext signature 55let gh1140Exception; 56try { 57 vm.runInContext('throw new Error()', context, 'expected-filename.js'); 58} catch (e) { 59 gh1140Exception = e; 60 assert.match(e.stack, /expected-filename/); 61} 62// This is outside of catch block to confirm catch block ran. 63assert.strictEqual(gh1140Exception.toString(), 'Error'); 64 65const nonContextualObjectError = { 66 code: 'ERR_INVALID_ARG_TYPE', 67 name: 'TypeError', 68 message: /must be of type object/ 69}; 70const contextifiedObjectError = { 71 code: 'ERR_INVALID_ARG_TYPE', 72 name: 'TypeError', 73 message: /The "contextifiedObject" argument must be an vm\.Context/ 74}; 75 76[ 77 [undefined, nonContextualObjectError], 78 [null, nonContextualObjectError], 79 [0, nonContextualObjectError], 80 [0.0, nonContextualObjectError], 81 ['', nonContextualObjectError], 82 [{}, contextifiedObjectError], 83 [[], contextifiedObjectError], 84].forEach((e) => { 85 assert.throws(() => { script.runInContext(e[0]); }, e[1]); 86 assert.throws(() => { vm.runInContext('', e[0]); }, e[1]); 87}); 88 89// Issue GH-693: 90// Test RegExp as argument to assert.throws 91script = vm.createScript('const assert = require(\'assert\'); assert.throws(' + 92 'function() { throw "hello world"; }, /hello/);', 93 'some.js'); 94script.runInNewContext({ require }); 95 96// Issue GH-7529 97script = vm.createScript('delete b'); 98let ctx = {}; 99Object.defineProperty(ctx, 'b', { configurable: false }); 100ctx = vm.createContext(ctx); 101assert.strictEqual(script.runInContext(ctx), false); 102 103// Error on the first line of a module should have the correct line and column 104// number. 105{ 106 let stack = null; 107 assert.throws(() => { 108 vm.runInContext(' throw new Error()', context, { 109 filename: 'expected-filename.js', 110 lineOffset: 32, 111 columnOffset: 123 112 }); 113 }, (err) => { 114 stack = err.stack; 115 return /^ \^/m.test(stack) && 116 /expected-filename\.js:33:131/.test(stack); 117 }, `stack not formatted as expected: ${stack}`); 118} 119 120// https://github.com/nodejs/node/issues/6158 121ctx = new Proxy({}, {}); 122assert.strictEqual(typeof vm.runInNewContext('String', ctx), 'function'); 123