Implement suggestings from @fitzgen.

This commit is contained in:
Richard Dodd 2019-01-11 19:23:33 +00:00
parent 72765757ef
commit b60d82a531

View File

@ -357,25 +357,31 @@ impl<'a> Context<'a> {
Ok(String::from( Ok(String::from(
" "
function(i, len_ptr) { function(i, len_ptr) {
const toString = Object.prototype.toString;
const debug_str = val => { const debug_str = val => {
// primitive types // primitive types
const type = typeof val; const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) { if (type == 'number' || type == 'boolean' || val == null) {
return val + ''; return `${val}`;
} }
if (type == 'string') { if (type == 'string') {
return '\"' + val + '\"'; return `\"${val}\"`;
} }
if (type == 'symbol') { if (type == 'symbol') {
const description = val.description; const description = val.description;
if (description == null) { if (description == null) {
return 'Symbol'; return 'Symbol';
} else { } else {
return 'Symbol(' + description + ')'; return `Symbol(${description})`;
} }
} }
if (type == 'function') { if (type == 'function') {
return 'Function'; const name = val.name;
if (typeof name == 'string') {
return `Function(${name})`;
} else {
return 'Function';
}
} }
// objects // objects
if (Array.isArray(val)) { if (Array.isArray(val)) {
@ -391,19 +397,23 @@ impl<'a> Context<'a> {
return debug; return debug;
} }
// Test for built-in // Test for built-in
const builtInMatches = /\\[object ([^])+\\]/.exec(val.toString()); const builtInMatches = /\\[object ([^])+\\]/.exec(toString.call(val));
let className; let className;
if (builtInMatches.len > 0) { if (builtInMatches.len > 0) {
className = builtInMatches[0]; className = builtInMatches[0];
} else { } else {
// Failed to match the standard '[object ClassName]' // Failed to match the standard '[object ClassName]'
return val.toString(); return toString.call(val);
} }
if (className == 'Object') { if (className == 'Object') {
// we're a user defined class or Object // we're a user defined class or Object
// JSON.stringify avoids problems with cycles, and is generally much // JSON.stringify avoids problems with cycles, and is generally much
// easier than looping through ownProperties of `val`. // easier than looping through ownProperties of `val`.
return 'Object(' + JSON.stringify(val) + ')'; try {
return 'Object(' + JSON.stringify(val) + ')';
} catch (_) {
return 'Object';
}
} else { } else {
return className; return className;
} }