Skip to content

Commit

Permalink
Make this inside a concatenated module the global object.
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Jun 5, 2014
1 parent 2d5ba89 commit a20bf8d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
12 changes: 8 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,24 @@ exports.sourcePosition = sourcePosition;

function IFFE() {
if (!IFFE.AST) {
IFFE.AST = JSON.stringify(recast.parse('(function(){})()', { esprima: esprima }));
IFFE.AST = JSON.stringify(
recast.parse('(function(){}).call(this)', { esprima: esprima })
);
}

var result = JSON.parse(IFFE.AST);
var body = result.program.body[0].expression.callee.body.body;
var expression = result.program.body[0].expression;
var body = expression.callee.object.body.body;

Array.prototype.slice.call(arguments).forEach(function(arg) {
var args = Array.prototype.slice.call(arguments);
args.forEach(function(arg) {
if (Object.prototype.toString.call(arg) === '[object Array]') {
body.push.apply(body, arg);
} else {
body.push(arg);
}
});

return result.program.body[0].expression;
return expression;
}
exports.IFFE = IFFE;
2 changes: 1 addition & 1 deletion test/examples/module-from/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ assert.deepEqual(Object.keys(things).sort(), ['count', 'incr']);
// can't add keys
assert.throws(function() {
things.iAmNotReal = 1;
}, TypeError);
}, /Can't add property iAmNotReal, object is not extensible/);

// can't replace keys
assert.throws(function() {
Expand Down
8 changes: 8 additions & 0 deletions test/examples/this-is-global/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* jshint esnext:true */

assert.strictEqual(
this,
global,
'`this` (keys=' + Object.keys(this) + ') does not equal ' +
'`global` (keys=' + Object.keys(global) + ')'
);
21 changes: 11 additions & 10 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,17 @@ function requireTestFile(path, relativeTo, assert) {

if (!testFileGlobal) { testFileGlobal = {}; }

vm.runInNewContext(code, {
assert: assert,
global: testFileGlobal,

module: mod,
exports: mod.exports,
require: function(requiredPath) {
return requireTestFile(requiredPath, Path.dirname(path), assert);
}
}, path);
testFileGlobal.assert = assert;
testFileGlobal.global = testFileGlobal;
testFileGlobal.module = mod;
testFileGlobal.exports = mod.exports;
testFileGlobal.require = function(requiredPath) {
return requireTestFile(requiredPath, Path.dirname(path), assert);
};

// Hack to work around an issue where vm does not set `this` to the context.
code = '(function(){' + code + '}).call(global);';
vm.runInNewContext(code, testFileGlobal, path);

testFileCache[path] = mod.exports;
return mod.exports;
Expand Down

0 comments on commit a20bf8d

Please sign in to comment.