From f50e715a934f83fc8045d2c97bec3e20eb7b592c Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Fri, 11 Jan 2019 19:23:33 +0000 Subject: [PATCH] Implement suggestings from @fitzgen. --- crates/cli-support/src/js/mod.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index f706df20c6a..2e1466a9d15 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -355,25 +355,31 @@ impl<'a> Context<'a> { Ok(String::from( " function(i, len_ptr) { + const toString = Object.prototype.toString; const debug_str = val => { // primitive types const type = typeof val; if (type == 'number' || type == 'boolean' || val == null) { - return val + ''; + return `${val}`; } if (type == 'string') { - return '\"' + val + '\"'; + return `\"${val}\"`; } if (type == 'symbol') { const description = val.description; if (description == null) { return 'Symbol'; } else { - return 'Symbol(' + description + ')'; + return `Symbol(${description})`; } } if (type == 'function') { - return 'Function'; + const name = val.name; + if (typeof name == 'string') { + return `Function(${name})`; + } else { + return 'Function'; + } } // objects if (Array.isArray(val)) { @@ -389,19 +395,23 @@ impl<'a> Context<'a> { return debug; } // Test for built-in - const builtInMatches = /\\[object ([^])+\\]/.exec(val.toString()); + const builtInMatches = /\\[object ([^])+\\]/.exec(toString.call(val)); let className; if (builtInMatches.len > 0) { className = builtInMatches[0]; } else { // Failed to match the standard '[object ClassName]' - return val.toString(); + return toString.call(val); } if (className == 'Object') { // we're a user defined class or Object // JSON.stringify avoids problems with cycles, and is generally much // easier than looping through ownProperties of `val`. - return 'Object(' + JSON.stringify(val) + ')'; + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } } else { return className; }