Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a safer (and slower, and worse) runInNewContext (see #1469)

  • Loading branch information...
commit 089f3a1a05d82696887d0478b978cdeca49de50c 1 parent cb6b9eb
@thejh authored
Showing with 45 additions and 0 deletions.
  1. +45 −0 lib/vm.js
View
45 lib/vm.js
@@ -30,3 +30,48 @@ exports.createContext = binding.NodeScript.createContext;
exports.runInContext = binding.NodeScript.runInContext;
exports.runInThisContext = binding.NodeScript.runInThisContext;
exports.runInNewContext = binding.NodeScript.runInNewContext;
+
+exports.runInNewSafeContext = function(code, context) {
+ var middleContext = {};
+ middleContext.middleContext = middleContext;
+ middleContext.subGlobal = context;
+ middleContext.g_code = code;
+ middleContext.runInNewContext = binding.NodeScript.runInNewContext;
+ var copyingClosure = (function() {
+ function deepClone(obj) {
+ var type = typeof obj;
+ if (type==='undefined' || obj===null || type==='boolean' || type==='number' || type==='string') {
+ return obj;
+ }
+ if (type==='function') {
+ return function() {
+ obj.apply(this, arguments);
+ };
+ }
+ if (type==='object') {
+ var toReturn = Array.isArray(obj) ? [] : {};
+ var objKeys = Object.getOwnPropertyNames(obj);
+ for (var i=0; i<objKeys.length; i++) {
+ toReturn[objKeys[i]] = deepClone(obj[objKeys[i]]);
+ }
+ return toReturn;
+ }
+ throw new Error('unknown typeof result: "'+type+'"');
+ }
+ subGlobal = deepClone(subGlobal);
+ }).toString();
+ var newCode = "("+copyingClosure+")();"
+ var protectiveRecursion = (function() {
+ if (!this || !this.goOn) {
+ return arguments.callee.call({goOn: true});
+ }
+ var code = g_code;
+ delete middleContext.g_code;
+ var run = runInNewContext;
+ delete middleContext.runInNewContext;
+ delete middleContext.middleContext;
+ return run(code, subGlobal);
+ }).toString();
+ newCode += "("+protectiveRecursion+")()";
+ return binding.NodeScript.runInNewContext(newCode, middleContext);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.