vm-browserify
Advanced tools
Comparing version 0.0.2 to 0.0.3
71
index.js
@@ -19,2 +19,29 @@ var indexOf = require('indexof'); | ||
var defineProp = (function() { | ||
try { | ||
Object.defineProperty({}, '_', {}); | ||
return function(obj, name, value) { | ||
Object.defineProperty(obj, name, { | ||
writable: true, | ||
enumerable: false, | ||
configurable: true, | ||
value: value | ||
}) | ||
}; | ||
} catch(e) { | ||
return function(obj, name, value) { | ||
obj[name] = value; | ||
}; | ||
} | ||
}()); | ||
var globals = ['Array', 'Boolean', 'Date', 'Error', 'EvalError', 'Function', | ||
'Infinity', '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() {} | ||
Context.prototype = {}; | ||
var Script = exports.Script = function NodeScript (code) { | ||
@@ -25,4 +52,6 @@ if (!(this instanceof Script)) return new Script(code); | ||
Script.prototype.runInNewContext = function (context) { | ||
if (!context) context = {}; | ||
Script.prototype.runInContext = function (context) { | ||
if (!(context instanceof Context)) { | ||
throw new TypeError("needs a 'context' argument."); | ||
} | ||
@@ -36,2 +65,3 @@ var iframe = document.createElement('iframe'); | ||
var win = iframe.contentWindow; | ||
var wEval = win.eval, wExecScript = win.execScript; | ||
@@ -41,6 +71,11 @@ forEach(Object_keys(context), function (key) { | ||
}); | ||
if (!win.eval && win.execScript) { | ||
forEach(globals, function (key) { | ||
if (context[key]) { | ||
win[key] = context[key]; | ||
} | ||
}); | ||
if (!wEval && wExecScript) { | ||
// win.eval() magically appears when this is called in IE: | ||
win.execScript('null'); | ||
wExecScript.call(win, 'null'); | ||
} | ||
@@ -50,3 +85,3 @@ | ||
var res = win.eval(this.code); | ||
var res = wEval.call(win, this.code); | ||
@@ -61,2 +96,8 @@ forEach(Object_keys(win), function (key) { | ||
}); | ||
forEach(globals, function (key) { | ||
if (!(key in context)) { | ||
defineProp(context, key, win[key]); | ||
} | ||
}); | ||
@@ -72,7 +113,11 @@ document.body.removeChild(iframe); | ||
Script.prototype.runInContext = function (context) { | ||
// seems to be just runInNewContext on magical context objects which are | ||
// otherwise indistinguishable from objects except plain old objects | ||
// for the parameter segfaults node | ||
return this.runInNewContext(context); | ||
Script.prototype.runInNewContext = function (context) { | ||
var ctx = Script.createContext(context); | ||
var res = this.runInContext(ctx); | ||
forEach(Object_keys(ctx), function (key) { | ||
context[key] = ctx[key]; | ||
}); | ||
return res; | ||
}; | ||
@@ -92,5 +137,3 @@ | ||
exports.createContext = Script.createContext = function (context) { | ||
// not really sure what this one does | ||
// seems to just make a shallow copy | ||
var copy = {}; | ||
var copy = new Context(); | ||
if(typeof context === 'object') { | ||
@@ -97,0 +140,0 @@ forEach(Object_keys(context), function (key) { |
{ | ||
"name": "vm-browserify", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "vm module for the browser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,3 @@ var test = require('tape'); | ||
test('vmRunInNewContext', function (t) { | ||
t.plan(5); | ||
t.plan(6); | ||
@@ -20,2 +20,3 @@ t.equal(vm.runInNewContext('a + 5', { a : 100 }), 105); | ||
t.equal(vars.x, 11); | ||
t.equal(vars.y, 3); | ||
})(); | ||
@@ -22,0 +23,0 @@ |
20417
543
8