beef up console.log() workaround
avoid `this` as catch variable
This commit is contained in:
parent
38f046a1eb
commit
2c1c8e8485
|
|
@ -1,9 +1,25 @@
|
||||||
var vm = require("vm");
|
var vm = require("vm");
|
||||||
|
|
||||||
|
function safe_log(arg) {
|
||||||
|
if (arg) switch (typeof arg) {
|
||||||
|
case "function":
|
||||||
|
return arg.toString();
|
||||||
|
case "object":
|
||||||
|
if (/Error$/.test(arg.name)) return arg.toString();
|
||||||
|
arg.constructor.toString();
|
||||||
|
for (var key in arg) {
|
||||||
|
arg[key] = safe_log(arg[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
|
||||||
var FUNC_TOSTRING = [
|
var FUNC_TOSTRING = [
|
||||||
"Function.prototype.toString = Function.prototype.valueOf = function() {",
|
"Function.prototype.toString = Function.prototype.valueOf = function() {",
|
||||||
" var id = 0;",
|
" var id = 0;",
|
||||||
" return function() {",
|
" return function() {",
|
||||||
|
' if (this === Array) return "[Function: Array]";',
|
||||||
|
' if (this === Object) return "[Function: Object]";',
|
||||||
" var i = this.name;",
|
" var i = this.name;",
|
||||||
' if (typeof i != "number") {',
|
' if (typeof i != "number") {',
|
||||||
" i = ++id;",
|
" i = ++id;",
|
||||||
|
|
@ -32,16 +48,7 @@ exports.run_code = function(code) {
|
||||||
].join("\n"), {
|
].join("\n"), {
|
||||||
console: {
|
console: {
|
||||||
log: function() {
|
log: function() {
|
||||||
return console.log.apply(console, [].map.call(arguments, function(arg) {
|
return console.log.apply(console, [].map.call(arguments, safe_log));
|
||||||
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;
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, { timeout: 5000 });
|
}, { timeout: 5000 });
|
||||||
|
|
|
||||||
|
|
@ -513,6 +513,7 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
|
||||||
// we have to do go through some trouble here to prevent leaking it
|
// we have to do go through some trouble here to prevent leaking it
|
||||||
var nameLenBefore = VAR_NAMES.length;
|
var nameLenBefore = VAR_NAMES.length;
|
||||||
var catchName = createVarName(MANDATORY);
|
var catchName = createVarName(MANDATORY);
|
||||||
|
if (catchName == 'this') catchName = 'a';
|
||||||
var freshCatchName = VAR_NAMES.length !== nameLenBefore;
|
var freshCatchName = VAR_NAMES.length !== nameLenBefore;
|
||||||
s += ' catch (' + catchName + ') { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
s += ' catch (' + catchName + ') { ' + createStatements(3, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + ' }';
|
||||||
if (freshCatchName) VAR_NAMES.splice(nameLenBefore, 1); // remove catch name
|
if (freshCatchName) VAR_NAMES.splice(nameLenBefore, 1); // remove catch name
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user