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'); 24 25const assert = require('assert'); 26 27const Script = require('vm').Script; 28 29{ 30 const script = new Script('\'passed\';'); 31 const result1 = script.runInNewContext(); 32 const result2 = script.runInNewContext(); 33 assert.strictEqual(result1, 'passed'); 34 assert.strictEqual(result2, 'passed'); 35} 36 37{ 38 const script = new Script('throw new Error(\'test\');'); 39 assert.throws(() => { 40 script.runInNewContext(); 41 }, /^Error: test$/); 42} 43 44{ 45 const script = new Script('foo.bar = 5;'); 46 assert.throws(() => { 47 script.runInNewContext(); 48 }, /^ReferenceError: foo is not defined$/); 49} 50 51{ 52 global.hello = 5; 53 const script = new Script('hello = 2'); 54 script.runInNewContext(); 55 assert.strictEqual(global.hello, 5); 56 57 // Cleanup 58 delete global.hello; 59} 60 61{ 62 global.code = 'foo = 1;' + 63 'bar = 2;' + 64 'if (baz !== 3) throw new Error(\'test fail\');'; 65 global.foo = 2; 66 global.obj = { foo: 0, baz: 3 }; 67 const script = new Script(global.code); 68 /* eslint-disable no-unused-vars */ 69 const baz = script.runInNewContext(global.obj); 70 /* eslint-enable no-unused-vars */ 71 assert.strictEqual(global.obj.foo, 1); 72 assert.strictEqual(global.obj.bar, 2); 73 assert.strictEqual(global.foo, 2); 74 75 // cleanup 76 delete global.code; 77 delete global.foo; 78 delete global.obj; 79} 80 81{ 82 const script = new Script('f()'); 83 function changeFoo() { global.foo = 100; } 84 script.runInNewContext({ f: changeFoo }); 85 assert.strictEqual(global.foo, 100); 86 87 // cleanup 88 delete global.foo; 89} 90 91{ 92 const script = new Script('f.a = 2'); 93 const f = { a: 1 }; 94 script.runInNewContext({ f }); 95 assert.strictEqual(f.a, 2); 96 97 assert.throws(() => { 98 script.runInNewContext(); 99 }, /^ReferenceError: f is not defined$/); 100} 101 102{ 103 const script = new Script(''); 104 assert.throws(() => { 105 script.runInNewContext.call('\'hello\';'); 106 }, /^TypeError: this\.runInContext is not a function$/); 107} 108