emulate global context in Node.js & web (#4379)

This commit is contained in:
Alex Lam S.L
2020-12-13 18:05:07 +00:00
committed by GitHub
parent fcc40d0502
commit f579f1aa47
2 changed files with 10 additions and 5 deletions

View File

@@ -588,7 +588,6 @@ issue_3197_1: {
ie8: false, ie8: false,
} }
input: { input: {
var window = {};
!function() { !function() {
function Foo() { function Foo() {
console.log(this instanceof Foo); console.log(this instanceof Foo);
@@ -598,7 +597,6 @@ issue_3197_1: {
new window.Foo(); new window.Foo();
} }
expect: { expect: {
var window = {};
window.Foo = function o() { window.Foo = function o() {
console.log(this instanceof o); console.log(this instanceof o);
}; };
@@ -619,7 +617,6 @@ issue_3197_1_ie8: {
ie8: true, ie8: true,
} }
input: { input: {
var window = {};
!function() { !function() {
function Foo() { function Foo() {
console.log(this instanceof Foo); console.log(this instanceof Foo);
@@ -629,7 +626,6 @@ issue_3197_1_ie8: {
new window.Foo(); new window.Foo();
} }
expect: { expect: {
var window = {};
window.Foo = function Foo() { window.Foo = function Foo() {
console.log(this instanceof Foo); console.log(this instanceof Foo);
}; };

View File

@@ -26,10 +26,19 @@ var setupContext = new vm.Script([
]).join("\n")); ]).join("\n"));
function createContext() { function createContext() {
var ctx = vm.createContext(Object.defineProperty({}, "console", { value: { log: log } })); var ctx = vm.createContext(Object.defineProperties({}, {
console: { value: { log: log } },
global: { get: self },
self: { get: self },
window: { get: self },
}));
var global = setupContext.runInContext(ctx); var global = setupContext.runInContext(ctx);
return ctx; return ctx;
function self() {
return this;
}
function safe_log(arg, level) { function safe_log(arg, level) {
if (arg) switch (typeof arg) { if (arg) switch (typeof arg) {
case "function": case "function":