bundle.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. var indexOf = function (xs, item) {
  3. if (xs.indexOf) return xs.indexOf(item);
  4. else for (var i = 0; i < xs.length; i++) {
  5. if (xs[i] === item) return i;
  6. }
  7. return -1;
  8. };
  9. var Object_keys = function (obj) {
  10. if (Object.keys) return Object.keys(obj)
  11. else {
  12. var res = [];
  13. for (var key in obj) res.push(key)
  14. return res;
  15. }
  16. };
  17. var forEach = function (xs, fn) {
  18. if (xs.forEach) return xs.forEach(fn)
  19. else for (var i = 0; i < xs.length; i++) {
  20. fn(xs[i], i, xs);
  21. }
  22. };
  23. var defineProp = (function() {
  24. try {
  25. Object.defineProperty({}, '_', {});
  26. return function(obj, name, value) {
  27. Object.defineProperty(obj, name, {
  28. writable: true,
  29. enumerable: false,
  30. configurable: true,
  31. value: value
  32. })
  33. };
  34. } catch(e) {
  35. return function(obj, name, value) {
  36. obj[name] = value;
  37. };
  38. }
  39. }());
  40. var globals = ['Array', 'Boolean', 'Date', 'Error', 'EvalError', 'Function',
  41. 'Infinity', 'JSON', 'Math', 'NaN', 'Number', 'Object', 'RangeError',
  42. 'ReferenceError', 'RegExp', 'String', 'SyntaxError', 'TypeError', 'URIError',
  43. 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape',
  44. 'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'undefined', 'unescape'];
  45. function Context() {}
  46. Context.prototype = {};
  47. var Script = exports.Script = function NodeScript (code) {
  48. if (!(this instanceof Script)) return new Script(code);
  49. this.code = code;
  50. };
  51. Script.prototype.runInContext = function (context) {
  52. if (!(context instanceof Context)) {
  53. throw new TypeError("needs a 'context' argument.");
  54. }
  55. var iframe = document.createElement('iframe');
  56. if (!iframe.style) iframe.style = {};
  57. iframe.style.display = 'none';
  58. document.body.appendChild(iframe);
  59. var win = iframe.contentWindow;
  60. var wEval = win.eval, wExecScript = win.execScript;
  61. if (!wEval && wExecScript) {
  62. // win.eval() magically appears when this is called in IE:
  63. wExecScript.call(win, 'null');
  64. wEval = win.eval;
  65. }
  66. forEach(Object_keys(context), function (key) {
  67. win[key] = context[key];
  68. });
  69. forEach(globals, function (key) {
  70. if (context[key]) {
  71. win[key] = context[key];
  72. }
  73. });
  74. var winKeys = Object_keys(win);
  75. var res = wEval.call(win, this.code);
  76. forEach(Object_keys(win), function (key) {
  77. // Avoid copying circular objects like `top` and `window` by only
  78. // updating existing context properties or new properties in the `win`
  79. // that was only introduced after the eval.
  80. if (key in context || indexOf(winKeys, key) === -1) {
  81. context[key] = win[key];
  82. }
  83. });
  84. forEach(globals, function (key) {
  85. if (!(key in context)) {
  86. defineProp(context, key, win[key]);
  87. }
  88. });
  89. document.body.removeChild(iframe);
  90. return res;
  91. };
  92. Script.prototype.runInThisContext = function () {
  93. return eval(this.code); // maybe...
  94. };
  95. Script.prototype.runInNewContext = function (context) {
  96. var ctx = Script.createContext(context);
  97. var res = this.runInContext(ctx);
  98. if (context) {
  99. forEach(Object_keys(ctx), function (key) {
  100. context[key] = ctx[key];
  101. });
  102. }
  103. return res;
  104. };
  105. forEach(Object_keys(Script.prototype), function (name) {
  106. exports[name] = Script[name] = function (code) {
  107. var s = Script(code);
  108. return s[name].apply(s, [].slice.call(arguments, 1));
  109. };
  110. });
  111. exports.isContext = function (context) {
  112. return context instanceof Context;
  113. };
  114. exports.createScript = function (code) {
  115. return exports.Script(code);
  116. };
  117. exports.createContext = Script.createContext = function (context) {
  118. var copy = new Context();
  119. if(typeof context === 'object') {
  120. forEach(Object_keys(context), function (key) {
  121. copy[key] = context[key];
  122. });
  123. }
  124. return copy;
  125. };
  126. },{}],2:[function(require,module,exports){
  127. var vm = require('vm');
  128. window.addEventListener('load', function () {
  129. var res = vm.runInNewContext('a + 5', { a : 100 });
  130. document.querySelector('#res').textContent = res;
  131. });
  132. },{"vm":1}]},{},[2]);