Skip to content

Commit

Permalink
Merge pull request #2221 from megawac/global-vm
Browse files Browse the repository at this point in the history
Ensure underscore can be run in a Node VM
  • Loading branch information
megawac committed Jul 6, 2015
2 parents 6567011 + 7fc15e5 commit dfe2307
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
31 changes: 31 additions & 0 deletions test/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@

});

if (typeof this == 'object') {
test('noConflict', function() {
var underscore = _.noConflict();
equal(underscore.identity(1), 1);
if (typeof require != 'function') {
equal(this._, void 0, 'global underscore is removed');
this._ = underscore;
}
});
}

if (typeof require == 'function') {
asyncTest('noConflict (node vm)', 2, function() {
var fs = require('fs');
var vm = require('vm');
var filename = __dirname + '/../underscore.js';
fs.readFile(filename, function(err, content){
var sandbox = vm.createScript(
content + 'this.underscore = this._.noConflict();',
filename
);
var context = {_: 'oldvalue'};
sandbox.runInNewContext(context);
equal(context._, 'oldvalue');
equal(context.underscore.VERSION, _.VERSION);

start();
});
});
}

test('#750 - Return _ instance.', 2, function() {
var instance = _([]);
ok(_(instance) === instance);
Expand Down
8 changes: 5 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
// Baseline setup
// --------------

// Establish the root object, `window` (`self`) in the browser, or `global` on the server.
// We use `self` instead of `window` for `WebWorker` support.
// Establish the root object, `window` (`self`) in the browser, `global`
// on the server, or `this` in some virtual machines. We use `self`
// instead of `window` for `WebWorker` support.
var root = typeof self === 'object' && self.self === self && self ||
typeof global === 'object' && global.global === global && global;
typeof global === 'object' && global.global === global && global ||
this;

// Save the previous value of the `_` variable.
var previousUnderscore = root._;
Expand Down

0 comments on commit dfe2307

Please sign in to comment.