Skip to content

Commit

Permalink
Merge 6cf2ce8 into d728b1b
Browse files Browse the repository at this point in the history
  • Loading branch information
aleafs committed Dec 31, 2013
2 parents d728b1b + 6cf2ce8 commit ddfcbdd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/module.js
Expand Up @@ -38,6 +38,8 @@ var _nativeNames = [
'url',
];

var _extendGlobals = {};

// See: https://github.com/joyent/node/issues/1707#issuecomment-2106309
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
Expand Down Expand Up @@ -78,6 +80,11 @@ exports.config = function (options) {
_nativeModules[k] = options.modules[k];
}
}
if (options.globals) {
for (var j in options.globals) {
_extendGlobals[j] = options.globals[j];
}
}
_enableAddons = options.enableAddons === true;
_limitRootDir = options.limitRoot || '/';
// make sure limit root dir end withs '/'
Expand Down Expand Up @@ -222,6 +229,10 @@ SandboxModule.prototype._compile = function (content, filename) {

var dirname = path.dirname(filename);

for (var j in _extendGlobals) {
global[j] = _extendGlobals[j];
}

SandboxModule._contextLoad = true;
if (SandboxModule._contextLoad) {
debug('self._compile() %s', self.id);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/extend_globals.js
@@ -0,0 +1 @@
foo();
12 changes: 12 additions & 0 deletions test/module.test.js
Expand Up @@ -66,6 +66,18 @@ describe('module.test.js', function () {
mod.load(path.join(fixtures, 'limit_root.js'));
}).should.throw("Cannot find module '../../lib/module'");
});

it('should extend globals works fine', function (done) {
sm.config({
'globals' : {
'foo' : function () {
should.ok(true);
done();
},
}
});
(new Module('.')).load(path.join(fixtures, 'extend_globals.js'));
});
});

describe('securty hook', function () {
Expand Down

0 comments on commit ddfcbdd

Please sign in to comment.