-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch fixes the problem with variables that are declared only on the sandbox but not on the global proxy. PR-URL: #16487 Fixes: #12300 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
Showing
3 changed files
with
27 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,14 @@ | ||
'use strict'; | ||
// https://github.com/nodejs/node/issues/12300 | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
const ctx = vm.createContext(); | ||
|
||
// Test strict mode inside a vm script, i.e., using an undefined variable | ||
// throws a ReferenceError. Also check that variables | ||
// that are not successfully set in the vm, must not be set | ||
// on the sandboxed context. | ||
|
||
vm.runInContext('w = 1;', ctx); | ||
assert.strictEqual(1, ctx.w); | ||
|
||
assert.throws(function() { vm.runInContext('"use strict"; x = 1;', ctx); }, | ||
/ReferenceError: x is not defined/); | ||
assert.strictEqual(undefined, ctx.x); | ||
|
||
vm.runInContext('"use strict"; var y = 1;', ctx); | ||
assert.strictEqual(1, ctx.y); | ||
const ctx = vm.createContext({ x: 42 }); | ||
|
||
vm.runInContext('"use strict"; this.z = 1;', ctx); | ||
assert.strictEqual(1, ctx.z); | ||
// This might look as if x has not been declared, but x is defined on the | ||
// sandbox and the assignment should not throw. | ||
vm.runInContext('"use strict"; x = 1', ctx); | ||
|
||
// w has been defined | ||
vm.runInContext('"use strict"; w = 2;', ctx); | ||
assert.strictEqual(2, ctx.w); | ||
assert.strictEqual(ctx.x, 1); |