From 2d4c7172930c38266fe89f72ab01e6abd9e4f198 Mon Sep 17 00:00:00 2001 From: Avi Kohn Date: Sun, 30 Aug 2015 14:14:44 -0400 Subject: [PATCH] Move `vm.runInThisContext` to require.exec In split contexts, like NW.js, runInThisContext evaluates modules in the wrong context. And, due to its location, this behavior is impossible to override. This commit moves the call to require.exec, which is overridable, and changes makeNodeWrapper to use `global.requirejsVars` instead of just requirejsVars. --- build/jslib/node.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/build/jslib/node.js b/build/jslib/node.js index c117113d..df302077 100644 --- a/build/jslib/node.js +++ b/build/jslib/node.js @@ -105,7 +105,7 @@ req.makeNodeWrapper = function (contents) { return '(function (require, requirejs, define) { ' + contents + - '\n}(requirejsVars.require, requirejsVars.requirejs, requirejsVars.define));'; + '\n}(global.requirejsVars.require, global.requirejsVars.requirejs, global.requirejsVars.define));'; }; req.load = function (context, moduleName, url) { @@ -120,9 +120,8 @@ if (exists(url)) { contents = fs.readFileSync(url, 'utf8'); - contents = req.makeNodeWrapper(contents); try { - vm.runInThisContext(contents, fs.realpathSync(url)); + req.exec(contents, fs.realpathSync(url)); } catch (e) { err = new Error('Evaluating ' + url + ' as module "' + moduleName + '" failed with error: ' + e); @@ -170,9 +169,8 @@ }; //Override to provide the function wrapper for define/require. - req.exec = function (text) { - /*jslint evil: true */ + req.exec = function (text, realPath) { text = req.makeNodeWrapper(text); - return eval(text); + return vm.runInThisContext(text, realPath); }; -}()); \ No newline at end of file +}());