-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-vm-strict-define.js
More file actions
51 lines (45 loc) · 1.43 KB
/
test-vm-strict-define.js
File metadata and controls
51 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
require('../common');
const assert = require('assert');
const vm = require('vm');
// Declared with `var`.
{
const ctx = vm.createContext();
vm.runInContext(`"use strict"; var x; x = 42;`, ctx);
assert.strictEqual(ctx.x, 42);
}
// Define on `globalThis`.
{
const ctx = vm.createContext();
vm.runInContext(`
"use strict";
Object.defineProperty(globalThis, "x", {
configurable: true,
value: 42,
});
`, ctx);
const ret = vm.runInContext(`"use strict"; x`, ctx);
assert.strictEqual(ret, 42);
assert.strictEqual(ctx.x, 42);
}
// Set on globalThis.
{
const ctx = vm.createContext();
vm.runInContext(`"use strict"; globalThis.x = 42`, ctx);
const ret = vm.runInContext(`"use strict"; x`, ctx);
assert.strictEqual(ret, 42);
assert.strictEqual(ctx.x, 42);
}
// Set on context.
// Should throw a ReferenceError when a variable is not defined in strict-mode.
assert.throws(() => vm.runInNewContext(`"use strict"; x = 42`),
/ReferenceError: x is not defined/);
// Known issue since V8 14.6.
// When the context is a "contextified" object, ReferenceError can not be thrown.
// TODO(legendecas): https://github.com/nodejs/node/pull/61898#issuecomment-4142811603
// Refs: https://chromium-review.googlesource.com/c/v8/v8/+/7474608
{
const ctx = vm.createContext({});
assert.throws(() => vm.runInContext(`"use strict"; x = 42`, ctx),
/ReferenceError: x is not defined/);
}