react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / vm-browserify / index.js
80713 viewsvar indexOf = require('indexof');12var Object_keys = function (obj) {3if (Object.keys) return Object.keys(obj)4else {5var res = [];6for (var key in obj) res.push(key)7return res;8}9};1011var forEach = function (xs, fn) {12if (xs.forEach) return xs.forEach(fn)13else for (var i = 0; i < xs.length; i++) {14fn(xs[i], i, xs);15}16};1718var defineProp = (function() {19try {20Object.defineProperty({}, '_', {});21return function(obj, name, value) {22Object.defineProperty(obj, name, {23writable: true,24enumerable: false,25configurable: true,26value: value27})28};29} catch(e) {30return function(obj, name, value) {31obj[name] = value;32};33}34}());3536var globals = ['Array', 'Boolean', 'Date', 'Error', 'EvalError', 'Function',37'Infinity', 'JSON', 'Math', 'NaN', 'Number', 'Object', 'RangeError',38'ReferenceError', 'RegExp', 'String', 'SyntaxError', 'TypeError', 'URIError',39'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape',40'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'undefined', 'unescape'];4142function Context() {}43Context.prototype = {};4445var Script = exports.Script = function NodeScript (code) {46if (!(this instanceof Script)) return new Script(code);47this.code = code;48};4950Script.prototype.runInContext = function (context) {51if (!(context instanceof Context)) {52throw new TypeError("needs a 'context' argument.");53}5455var iframe = document.createElement('iframe');56if (!iframe.style) iframe.style = {};57iframe.style.display = 'none';5859document.body.appendChild(iframe);6061var win = iframe.contentWindow;62var wEval = win.eval, wExecScript = win.execScript;6364if (!wEval && wExecScript) {65// win.eval() magically appears when this is called in IE:66wExecScript.call(win, 'null');67wEval = win.eval;68}6970forEach(Object_keys(context), function (key) {71win[key] = context[key];72});73forEach(globals, function (key) {74if (context[key]) {75win[key] = context[key];76}77});7879var winKeys = Object_keys(win);8081var res = wEval.call(win, this.code);8283forEach(Object_keys(win), function (key) {84// Avoid copying circular objects like `top` and `window` by only85// updating existing context properties or new properties in the `win`86// that was only introduced after the eval.87if (key in context || indexOf(winKeys, key) === -1) {88context[key] = win[key];89}90});9192forEach(globals, function (key) {93if (!(key in context)) {94defineProp(context, key, win[key]);95}96});9798document.body.removeChild(iframe);99100return res;101};102103Script.prototype.runInThisContext = function () {104return eval(this.code); // maybe...105};106107Script.prototype.runInNewContext = function (context) {108var ctx = Script.createContext(context);109var res = this.runInContext(ctx);110111forEach(Object_keys(ctx), function (key) {112context[key] = ctx[key];113});114115return res;116};117118forEach(Object_keys(Script.prototype), function (name) {119exports[name] = Script[name] = function (code) {120var s = Script(code);121return s[name].apply(s, [].slice.call(arguments, 1));122};123});124125exports.createScript = function (code) {126return exports.Script(code);127};128129exports.createContext = Script.createContext = function (context) {130var copy = new Context();131if(typeof context === 'object') {132forEach(Object_keys(context), function (key) {133copy[key] = context[key];134});135}136return copy;137};138139140