Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding non-enumerable properties to context like node does.

  • Loading branch information...
commit fe18a4804584425e78e4a33fb488e9762204775c 1 parent 5beb277
@pluma pluma authored committed
Showing with 36 additions and 1 deletion.
  1. +36 −1 index.js
View
37 index.js
@@ -16,6 +16,30 @@ var forEach = function (xs, fn) {
}
};
+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 = {};
@@ -40,7 +64,12 @@ Script.prototype.runInContext = function (context) {
forEach(Object_keys(context), function (key) {
win[key] = context[key];
});
-
+ forEach(globals, function (key) {
+ if (context[key]) {
+ win[key] = context[key];
+ }
+ });
+
if (!win.eval && win.execScript) {
// win.eval() magically appears when this is called in IE:
win.execScript('null');
@@ -58,6 +87,12 @@ Script.prototype.runInContext = function (context) {
context[key] = win[key];
}
});
+
+ forEach(globals, function (key) {
+ if (!(key in context)) {
+ defineProp(context, key, win[key]);
+ }
+ });
document.body.removeChild(iframe);
Please sign in to comment.
Something went wrong with that request. Please try again.