Skip to content

Commit 01d4522

Browse files
TrottMylesBorins
authored andcommitted
test,vm: enable strict mode for vm tests
Some vm tests are not in strict mode because they need to create and use global variables. By using `global.foo` instead of just `foo`, we can still enable strict mode. PR-URL: #6209 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 1796e49 commit 01d4522

3 files changed

+56
-56
lines changed

test/parallel/test-vm-new-script-new-context.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/* eslint-disable strict */
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var Script = require('vm').Script;
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const Script = require('vm').Script;
55

66
common.globalCheck = false;
77

88
console.error('run a string');
99
var script = new Script('\'passed\';');
1010
console.error('script created');
11-
var result1 = script.runInNewContext();
12-
var result2 = script.runInNewContext();
11+
const result1 = script.runInNewContext();
12+
const result2 = script.runInNewContext();
1313
assert.equal('passed', result1);
1414
assert.equal('passed', result2);
1515

@@ -27,31 +27,31 @@ assert.throws(function() {
2727
}, /not defined/);
2828

2929

30-
hello = 5;
30+
global.hello = 5;
3131
script = new Script('hello = 2');
3232
script.runInNewContext();
33-
assert.equal(5, hello);
33+
assert.equal(5, global.hello);
3434

3535

3636
console.error('pass values in and out');
37-
code = 'foo = 1;' +
38-
'bar = 2;' +
39-
'if (baz !== 3) throw new Error(\'test fail\');';
40-
foo = 2;
41-
obj = { foo: 0, baz: 3 };
42-
script = new Script(code);
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);
4343
/* eslint-disable no-unused-vars */
44-
var baz = script.runInNewContext(obj);
44+
var baz = script.runInNewContext(global.obj);
4545
/* eslint-enable no-unused-vars */
46-
assert.equal(1, obj.foo);
47-
assert.equal(2, obj.bar);
48-
assert.equal(2, foo);
46+
assert.equal(1, global.obj.foo);
47+
assert.equal(2, global.obj.bar);
48+
assert.equal(2, global.foo);
4949

5050
console.error('call a function by reference');
5151
script = new Script('f()');
52-
function changeFoo() { foo = 100; }
52+
function changeFoo() { global.foo = 100; }
5353
script.runInNewContext({ f: changeFoo });
54-
assert.equal(foo, 100);
54+
assert.equal(global.foo, 100);
5555

5656
console.error('modify an object by reference');
5757
script = new Script('f.a = 2');
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
/* eslint-disable strict */
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var Script = require('vm').Script;
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const Script = require('vm').Script;
55

66
common.globalCheck = false;
77

88
console.error('run a string');
99
var script = new Script('\'passed\';');
10-
var result = script.runInThisContext(script);
10+
const result = script.runInThisContext(script);
1111
assert.equal('passed', result);
1212

1313
console.error('thrown error');
@@ -16,26 +16,26 @@ assert.throws(function() {
1616
script.runInThisContext(script);
1717
});
1818

19-
hello = 5;
19+
global.hello = 5;
2020
script = new Script('hello = 2');
2121
script.runInThisContext(script);
22-
assert.equal(2, hello);
22+
assert.equal(2, global.hello);
2323

2424

2525
console.error('pass values');
26-
code = 'foo = 1;' +
27-
'bar = 2;' +
28-
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
29-
foo = 2;
30-
obj = { foo: 0, baz: 3 };
31-
script = new Script(code);
26+
global.code = 'foo = 1;' +
27+
'bar = 2;' +
28+
'if (typeof baz !== "undefined") throw new Error("test fail");';
29+
global.foo = 2;
30+
global.obj = { foo: 0, baz: 3 };
31+
script = new Script(global.code);
3232
script.runInThisContext(script);
33-
assert.equal(0, obj.foo);
34-
assert.equal(2, bar);
35-
assert.equal(1, foo);
33+
assert.equal(0, global.obj.foo);
34+
assert.equal(2, global.bar);
35+
assert.equal(1, global.foo);
3636

3737
console.error('call a function');
38-
f = function() { foo = 100; };
38+
global.f = function() { global.foo = 100; };
3939
script = new Script('f()');
4040
script.runInThisContext(script);
41-
assert.equal(100, foo);
41+
assert.equal(100, global.foo);

test/parallel/test-vm-run-in-new-context.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
/* eslint-disable strict */
1+
'use strict';
22
// Flags: --expose-gc
33

4-
var common = require('../common');
5-
var assert = require('assert');
6-
var vm = require('vm');
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const vm = require('vm');
77

88
assert.equal(typeof gc, 'function', 'Run this test with --expose-gc');
99

1010
common.globalCheck = false;
1111

1212
console.error('run a string');
13-
var result = vm.runInNewContext('\'passed\';');
13+
const result = vm.runInNewContext('\'passed\';');
1414
assert.equal('passed', result);
1515

1616
console.error('thrown error');
1717
assert.throws(function() {
1818
vm.runInNewContext('throw new Error(\'test\');');
1919
});
2020

21-
hello = 5;
21+
global.hello = 5;
2222
vm.runInNewContext('hello = 2');
23-
assert.equal(5, hello);
23+
assert.equal(5, global.hello);
2424

2525

2626
console.error('pass values in and out');
27-
code = 'foo = 1;' +
28-
'bar = 2;' +
29-
'if (baz !== 3) throw new Error(\'test fail\');';
30-
foo = 2;
31-
obj = { foo: 0, baz: 3 };
27+
global.code = 'foo = 1;' +
28+
'bar = 2;' +
29+
'if (baz !== 3) throw new Error(\'test fail\');';
30+
global.foo = 2;
31+
global.obj = { foo: 0, baz: 3 };
3232
/* eslint-disable no-unused-vars */
33-
var baz = vm.runInNewContext(code, obj);
33+
var baz = vm.runInNewContext(global.code, global.obj);
3434
/* eslint-enable no-unused-vars */
35-
assert.equal(1, obj.foo);
36-
assert.equal(2, obj.bar);
37-
assert.equal(2, foo);
35+
assert.equal(1, global.obj.foo);
36+
assert.equal(2, global.obj.bar);
37+
assert.equal(2, global.foo);
3838

3939
console.error('call a function by reference');
40-
function changeFoo() { foo = 100; }
40+
function changeFoo() { global.foo = 100; }
4141
vm.runInNewContext('f()', { f: changeFoo });
42-
assert.equal(foo, 100);
42+
assert.equal(global.foo, 100);
4343

4444
console.error('modify an object by reference');
4545
var f = { a: 1 };

0 commit comments

Comments
 (0)