2024-01-16 21:26:16 +08:00
|
|
|
(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){
|
|
|
|
var indexOf = function (xs, item) {
|
|
|
|
if (xs.indexOf) return xs.indexOf(item);
|
|
|
|
else for (var i = 0; i < xs.length; i++) {
|
|
|
|
if (xs[i] === item) return i;
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
return -1;
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
|
|
|
var Object_keys = function (obj) {
|
|
|
|
if (Object.keys) return Object.keys(obj)
|
|
|
|
else {
|
|
|
|
var res = [];
|
|
|
|
for (var key in obj) res.push(key)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var forEach = function (xs, fn) {
|
|
|
|
if (xs.forEach) return xs.forEach(fn)
|
|
|
|
else for (var i = 0; i < xs.length; i++) {
|
|
|
|
fn(xs[i], i, xs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
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 = {};
|
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
var Script = exports.Script = function NodeScript (code) {
|
|
|
|
if (!(this instanceof Script)) return new Script(code);
|
|
|
|
this.code = code;
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
Script.prototype.runInContext = function (context) {
|
|
|
|
if (!(context instanceof Context)) {
|
|
|
|
throw new TypeError("needs a 'context' argument.");
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
if (!iframe.style) iframe.style = {};
|
|
|
|
iframe.style.display = 'none';
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
document.body.appendChild(iframe);
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var win = iframe.contentWindow;
|
|
|
|
var wEval = win.eval, wExecScript = win.execScript;
|
|
|
|
|
|
|
|
if (!wEval && wExecScript) {
|
|
|
|
// win.eval() magically appears when this is called in IE:
|
|
|
|
wExecScript.call(win, 'null');
|
|
|
|
wEval = win.eval;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
forEach(Object_keys(context), function (key) {
|
|
|
|
win[key] = context[key];
|
|
|
|
});
|
2024-01-16 21:26:16 +08:00
|
|
|
forEach(globals, function (key) {
|
|
|
|
if (context[key]) {
|
|
|
|
win[key] = context[key];
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var winKeys = Object_keys(win);
|
|
|
|
|
|
|
|
var res = wEval.call(win, this.code);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
forEach(Object_keys(win), function (key) {
|
2024-01-16 21:26:16 +08:00
|
|
|
// Avoid copying circular objects like `top` and `window` by only
|
|
|
|
// updating existing context properties or new properties in the `win`
|
|
|
|
// that was only introduced after the eval.
|
|
|
|
if (key in context || indexOf(winKeys, key) === -1) {
|
|
|
|
context[key] = win[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
forEach(globals, function (key) {
|
|
|
|
if (!(key in context)) {
|
|
|
|
defineProp(context, key, win[key]);
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
document.body.removeChild(iframe);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
Script.prototype.runInThisContext = function () {
|
|
|
|
return eval(this.code); // maybe...
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
Script.prototype.runInNewContext = function (context) {
|
|
|
|
var ctx = Script.createContext(context);
|
|
|
|
var res = this.runInContext(ctx);
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
forEach(Object_keys(ctx), function (key) {
|
|
|
|
context[key] = ctx[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
forEach(Object_keys(Script.prototype), function (name) {
|
|
|
|
exports[name] = Script[name] = function (code) {
|
|
|
|
var s = Script(code);
|
|
|
|
return s[name].apply(s, [].slice.call(arguments, 1));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
exports.isContext = function (context) {
|
|
|
|
return context instanceof Context;
|
|
|
|
};
|
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
exports.createScript = function (code) {
|
|
|
|
return exports.Script(code);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createContext = Script.createContext = function (context) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var copy = new Context();
|
|
|
|
if(typeof context === 'object') {
|
|
|
|
forEach(Object_keys(context), function (key) {
|
|
|
|
copy[key] = context[key];
|
|
|
|
});
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
return copy;
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
},{}],2:[function(require,module,exports){
|
|
|
|
var vm = require('vm');
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
window.addEventListener('load', function () {
|
2023-12-18 13:12:25 +08:00
|
|
|
var res = vm.runInNewContext('a + 5', { a : 100 });
|
2024-01-16 21:26:16 +08:00
|
|
|
document.querySelector('#res').textContent = res;
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
},{"vm":1}]},{},[2]);
|