workaround console.log() for new function()

This commit is contained in:
alexlamsl 2017-04-23 14:46:35 +08:00
parent 7194c09e4c
commit f868e9e552

View File

@ -2,14 +2,18 @@ var vm = require("vm");
var FUNC_TOSTRING = [ var FUNC_TOSTRING = [
"Function.prototype.toString = Function.prototype.valueOf = function() {", "Function.prototype.toString = Function.prototype.valueOf = function() {",
" var ids = [];", " var id = 0;",
" return function() {", " return function() {",
" var i = ids.indexOf(this);", " var i = this.name;",
" if (i < 0) {", ' if (typeof i != "number") {',
" i = ids.length;", " i = ++id;",
" ids.push(this);", ' Object.defineProperty(this, "name", {',
" get: function() {",
" return i;",
" }", " }",
' return "[Function: __func_" + i + "__]";', " });",
" }",
' return "[Function: " + i + "]";',
" }", " }",
"}();", "}();",
].join("\n"); ].join("\n");
@ -29,7 +33,14 @@ exports.run_code = function(code) {
console: { console: {
log: function() { log: function() {
return console.log.apply(console, [].map.call(arguments, function(arg) { return console.log.apply(console, [].map.call(arguments, function(arg) {
return typeof arg == "function" || arg && /Error$/.test(arg.name) ? arg.toString() : arg; if (arg) switch (typeof arg) {
case "function":
return arg.toString();
case "object":
if (/Error$/.test(arg.name)) return arg.toString();
if (typeof arg.constructor == "function") arg.constructor.toString();
}
return arg;
})); }));
} }
} }