|  | 
| 1 | 1 | 'use strict'; | 
| 2 |  | -const common = require('../common'); | 
|  | 2 | +require('../common'); | 
|  | 3 | + | 
| 3 | 4 | const assert = require('assert'); | 
|  | 5 | + | 
| 4 | 6 | const Script = require('vm').Script; | 
| 5 | 7 | 
 | 
| 6 |  | -common.globalCheck = false; | 
|  | 8 | +{ | 
|  | 9 | +  const script = new Script('\'passed\';'); | 
|  | 10 | +  const result1 = script.runInNewContext(); | 
|  | 11 | +  const result2 = script.runInNewContext(); | 
|  | 12 | +  assert.strictEqual('passed', result1); | 
|  | 13 | +  assert.strictEqual('passed', result2); | 
|  | 14 | +} | 
| 7 | 15 | 
 | 
| 8 |  | -console.error('run a string'); | 
| 9 |  | -let script = new Script('\'passed\';'); | 
| 10 |  | -console.error('script created'); | 
| 11 |  | -const result1 = script.runInNewContext(); | 
| 12 |  | -const result2 = script.runInNewContext(); | 
| 13 |  | -assert.strictEqual('passed', result1); | 
| 14 |  | -assert.strictEqual('passed', result2); | 
|  | 16 | +{ | 
|  | 17 | +  const script = new Script('throw new Error(\'test\');'); | 
|  | 18 | +  assert.throws(function() { | 
|  | 19 | +    script.runInNewContext(); | 
|  | 20 | +  }, /^Error: test$/); | 
|  | 21 | +} | 
| 15 | 22 | 
 | 
| 16 |  | -console.error('thrown error'); | 
| 17 |  | -script = new Script('throw new Error(\'test\');'); | 
| 18 |  | -assert.throws(function() { | 
| 19 |  | -  script.runInNewContext(); | 
| 20 |  | -}, /^Error: test$/); | 
|  | 23 | +{ | 
|  | 24 | +  const script = new Script('foo.bar = 5;'); | 
|  | 25 | +  assert.throws(function() { | 
|  | 26 | +    script.runInNewContext(); | 
|  | 27 | +  }, /^ReferenceError: foo is not defined$/); | 
|  | 28 | +} | 
| 21 | 29 | 
 | 
| 22 |  | - | 
| 23 |  | -console.error('undefined reference'); | 
| 24 |  | -script = new Script('foo.bar = 5;'); | 
| 25 |  | -assert.throws(function() { | 
|  | 30 | +{ | 
|  | 31 | +  global.hello = 5; | 
|  | 32 | +  const script = new Script('hello = 2'); | 
| 26 | 33 |   script.runInNewContext(); | 
| 27 |  | -}, /^ReferenceError: foo is not defined$/); | 
|  | 34 | +  assert.strictEqual(5, global.hello); | 
| 28 | 35 | 
 | 
|  | 36 | +  // cleanup | 
|  | 37 | +  delete global.hello; | 
|  | 38 | +} | 
| 29 | 39 | 
 | 
| 30 |  | -global.hello = 5; | 
| 31 |  | -script = new Script('hello = 2'); | 
| 32 |  | -script.runInNewContext(); | 
| 33 |  | -assert.strictEqual(5, global.hello); | 
|  | 40 | +{ | 
|  | 41 | +  global.code = 'foo = 1;' + | 
|  | 42 | +                'bar = 2;' + | 
|  | 43 | +                'if (baz !== 3) throw new Error(\'test fail\');'; | 
|  | 44 | +  global.foo = 2; | 
|  | 45 | +  global.obj = { foo: 0, baz: 3 }; | 
|  | 46 | +  const script = new Script(global.code); | 
|  | 47 | +  /* eslint-disable no-unused-vars */ | 
|  | 48 | +  const baz = script.runInNewContext(global.obj); | 
|  | 49 | +  /* eslint-enable no-unused-vars */ | 
|  | 50 | +  assert.strictEqual(1, global.obj.foo); | 
|  | 51 | +  assert.strictEqual(2, global.obj.bar); | 
|  | 52 | +  assert.strictEqual(2, global.foo); | 
| 34 | 53 | 
 | 
|  | 54 | +  //cleanup | 
|  | 55 | +  delete global.code; | 
|  | 56 | +  delete global.foo; | 
|  | 57 | +  delete global.obj; | 
|  | 58 | +} | 
| 35 | 59 | 
 | 
| 36 |  | -console.error('pass values in and out'); | 
| 37 |  | -global.code = 'foo = 1;' + | 
| 38 |  | -              'bar = 2;' + | 
| 39 |  | -              'if (baz !== 3) throw new Error(\'test fail\');'; | 
| 40 |  | -global.foo = 2; | 
| 41 |  | -global.obj = { foo: 0, baz: 3 }; | 
| 42 |  | -script = new Script(global.code); | 
| 43 |  | -/* eslint-disable no-unused-vars */ | 
| 44 |  | -const baz = script.runInNewContext(global.obj); | 
| 45 |  | -/* eslint-enable no-unused-vars */ | 
| 46 |  | -assert.strictEqual(1, global.obj.foo); | 
| 47 |  | -assert.strictEqual(2, global.obj.bar); | 
| 48 |  | -assert.strictEqual(2, global.foo); | 
|  | 60 | +{ | 
|  | 61 | +  const script = new Script('f()'); | 
|  | 62 | +  function changeFoo() { global.foo = 100; } | 
|  | 63 | +  script.runInNewContext({ f: changeFoo }); | 
|  | 64 | +  assert.strictEqual(global.foo, 100); | 
| 49 | 65 | 
 | 
| 50 |  | -console.error('call a function by reference'); | 
| 51 |  | -script = new Script('f()'); | 
| 52 |  | -function changeFoo() { global.foo = 100; } | 
| 53 |  | -script.runInNewContext({ f: changeFoo }); | 
| 54 |  | -assert.strictEqual(global.foo, 100); | 
|  | 66 | +  // cleanup | 
|  | 67 | +  delete global.foo; | 
|  | 68 | +} | 
| 55 | 69 | 
 | 
| 56 |  | -console.error('modify an object by reference'); | 
| 57 |  | -script = new Script('f.a = 2'); | 
| 58 |  | -const f = { a: 1 }; | 
| 59 |  | -script.runInNewContext({ f: f }); | 
| 60 |  | -assert.strictEqual(f.a, 2); | 
|  | 70 | +{ | 
|  | 71 | +  const script = new Script('f.a = 2'); | 
|  | 72 | +  const f = { a: 1 }; | 
|  | 73 | +  script.runInNewContext({ f: f }); | 
|  | 74 | +  assert.strictEqual(f.a, 2); | 
| 61 | 75 | 
 | 
| 62 |  | -assert.throws(function() { | 
| 63 |  | -  script.runInNewContext(); | 
| 64 |  | -}, /^ReferenceError: f is not defined$/); | 
|  | 76 | +  assert.throws(function() { | 
|  | 77 | +    script.runInNewContext(); | 
|  | 78 | +  }, /^ReferenceError: f is not defined$/); | 
|  | 79 | +} | 
| 65 | 80 | 
 | 
| 66 |  | -console.error('invalid this'); | 
| 67 |  | -assert.throws(function() { | 
| 68 |  | -  script.runInNewContext.call('\'hello\';'); | 
| 69 |  | -}, /^TypeError: this\.runInContext is not a function$/); | 
|  | 81 | +{ | 
|  | 82 | +  const script = new Script(''); | 
|  | 83 | +  assert.throws(function() { | 
|  | 84 | +    script.runInNewContext.call('\'hello\';'); | 
|  | 85 | +  }, /^TypeError: this\.runInContext is not a function$/); | 
|  | 86 | +} | 
0 commit comments