Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,vm: enable strict mode for vm tests #6209

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions test/parallel/test-vm-new-script-new-context.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable strict */
var common = require('../common');
var assert = require('assert');
var Script = require('vm').Script;
'use strict';
const common = require('../common');
const assert = require('assert');
const Script = require('vm').Script;

common.globalCheck = false;

console.error('run a string');
var script = new Script('\'passed\';');
console.error('script created');
var result1 = script.runInNewContext();
var result2 = script.runInNewContext();
const result1 = script.runInNewContext();
const result2 = script.runInNewContext();
assert.equal('passed', result1);
assert.equal('passed', result2);

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


hello = 5;
global.hello = 5;
script = new Script('hello = 2');
script.runInNewContext();
assert.equal(5, hello);
assert.equal(5, global.hello);


console.error('pass values in and out');
code = 'foo = 1;' +
'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');';
foo = 2;
obj = { foo: 0, baz: 3 };
script = new Script(code);
global.code = 'foo = 1;' +
'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');';
global.foo = 2;
global.obj = { foo: 0, baz: 3 };
script = new Script(global.code);
/* eslint-disable no-unused-vars */
var baz = script.runInNewContext(obj);
var baz = script.runInNewContext(global.obj);
/* eslint-enable no-unused-vars */
assert.equal(1, obj.foo);
assert.equal(2, obj.bar);
assert.equal(2, foo);
assert.equal(1, global.obj.foo);
assert.equal(2, global.obj.bar);
assert.equal(2, global.foo);

console.error('call a function by reference');
script = new Script('f()');
function changeFoo() { foo = 100; }
function changeFoo() { global.foo = 100; }
script.runInNewContext({ f: changeFoo });
assert.equal(foo, 100);
assert.equal(global.foo, 100);

console.error('modify an object by reference');
script = new Script('f.a = 2');
Expand Down
36 changes: 18 additions & 18 deletions test/parallel/test-vm-new-script-this-context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable strict */
var common = require('../common');
var assert = require('assert');
var Script = require('vm').Script;
'use strict';
const common = require('../common');
const assert = require('assert');
const Script = require('vm').Script;

common.globalCheck = false;

console.error('run a string');
var script = new Script('\'passed\';');
var result = script.runInThisContext(script);
const result = script.runInThisContext(script);
assert.equal('passed', result);

console.error('thrown error');
Expand All @@ -16,26 +16,26 @@ assert.throws(function() {
script.runInThisContext(script);
});

hello = 5;
global.hello = 5;
script = new Script('hello = 2');
script.runInThisContext(script);
assert.equal(2, hello);
assert.equal(2, global.hello);


console.error('pass values');
code = 'foo = 1;' +
'bar = 2;' +
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
foo = 2;
obj = { foo: 0, baz: 3 };
script = new Script(code);
global.code = 'foo = 1;' +
'bar = 2;' +
'if (typeof baz !== "undefined") throw new Error("test fail");';
global.foo = 2;
global.obj = { foo: 0, baz: 3 };
script = new Script(global.code);
script.runInThisContext(script);
assert.equal(0, obj.foo);
assert.equal(2, bar);
assert.equal(1, foo);
assert.equal(0, global.obj.foo);
assert.equal(2, global.bar);
assert.equal(1, global.foo);

console.error('call a function');
f = function() { foo = 100; };
global.f = function() { global.foo = 100; };
script = new Script('f()');
script.runInThisContext(script);
assert.equal(100, foo);
assert.equal(100, global.foo);
36 changes: 18 additions & 18 deletions test/parallel/test-vm-run-in-new-context.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
/* eslint-disable strict */
'use strict';
// Flags: --expose-gc

var common = require('../common');
var assert = require('assert');
var vm = require('vm');
const common = require('../common');
const assert = require('assert');
const vm = require('vm');

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

common.globalCheck = false;

console.error('run a string');
var result = vm.runInNewContext('\'passed\';');
const result = vm.runInNewContext('\'passed\';');
assert.equal('passed', result);

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

hello = 5;
global.hello = 5;
vm.runInNewContext('hello = 2');
assert.equal(5, hello);
assert.equal(5, global.hello);


console.error('pass values in and out');
code = 'foo = 1;' +
'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');';
foo = 2;
obj = { foo: 0, baz: 3 };
global.code = 'foo = 1;' +
'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');';
global.foo = 2;
global.obj = { foo: 0, baz: 3 };
/* eslint-disable no-unused-vars */
var baz = vm.runInNewContext(code, obj);
var baz = vm.runInNewContext(global.code, global.obj);
/* eslint-enable no-unused-vars */
assert.equal(1, obj.foo);
assert.equal(2, obj.bar);
assert.equal(2, foo);
assert.equal(1, global.obj.foo);
assert.equal(2, global.obj.bar);
assert.equal(2, global.foo);

console.error('call a function by reference');
function changeFoo() { foo = 100; }
function changeFoo() { global.foo = 100; }
vm.runInNewContext('f()', { f: changeFoo });
assert.equal(foo, 100);
assert.equal(global.foo, 100);

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