Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,26 @@ var defineProp = (function() {
}());

var globals = ['Array', 'Boolean', 'Date', 'Error', 'EvalError', 'Function',
'Infinity', 'JSON', 'Math', 'NaN', 'Number', 'Object', 'RangeError',
'JSON', 'Math', 'NaN', 'Number', 'Object', 'RangeError',
'ReferenceError', 'RegExp', 'String', 'SyntaxError', 'TypeError', 'URIError',
'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape',
'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'undefined', 'unescape'];

function Context() {}
function Context() {

var iframe = document.createElement('iframe');
if (!iframe.style) iframe.style = {};
iframe.style.display = 'none';

document.body.appendChild(iframe);

Object.defineProperty(this, "_iframe", {
enumerable: false,
writable: true
});

this._iframe = iframe;
}
Context.prototype = {};

var Script = exports.Script = function NodeScript (code) {
Expand All @@ -53,21 +67,15 @@ Script.prototype.runInContext = function (context) {
throw new TypeError("needs a 'context' argument.");
}

var iframe = document.createElement('iframe');
if (!iframe.style) iframe.style = {};
iframe.style.display = 'none';

document.body.appendChild(iframe);

var win = iframe.contentWindow;
var win = context._iframe.contentWindow;
var wEval = win.eval, wExecScript = win.execScript;

if (!wEval && wExecScript) {
// win.eval() magically appears when this is called in IE:
wExecScript.call(win, 'null');
wEval = win.eval;
}

forEach(Object_keys(context), function (key) {
win[key] = context[key];
});
Expand Down Expand Up @@ -95,9 +103,6 @@ Script.prototype.runInContext = function (context) {
defineProp(context, key, win[key]);
}
});

document.body.removeChild(iframe);

return res;
};

Expand Down Expand Up @@ -132,7 +137,12 @@ exports.createContext = Script.createContext = function (context) {
if(typeof context === 'object') {
forEach(Object_keys(context), function (key) {
copy[key] = context[key];
copy._iframe.contentWindow[key] = context[key];
});
}
return copy;
};

exports.isContext = function(sandbox){
return sandbox instanceof Context;
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vm-browserify",
"version": "0.0.4",
"name": "vm-browserify2",
"version": "0.0.6",
"description": "vm module for the browser",
"main": "index.js",
"repository": {
Expand Down
24 changes: 24 additions & 0 deletions test/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ test('vmRunInContext', function (t) {
vm.runInContext('var y = 1', context);
t.deepEqual(context, { foo: 1, x: 1, y: 1 });
});


test('vmRunInContext with closure', function (t) {
t.plan(4);

var context = vm.createContext();

vm.runInContext('var x = 1', context);
t.deepEqual(context, { x: 1 });

vm.runInContext('function y() { return ++x; }', context);
var x = vm.runInContext('y()', context);
t.equal(x, 2);
t.equal(context.x, 2);
t.equal(typeof context.y, 'function');
});

test('isContext', function (t) {
t.plan(2);

var context = vm.createContext();
t.true(vm.isContext(context));
t.false(vm.isContext({}));
});