mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-13 23:56:06 +00:00
Implement suggestings from @fitzgen.
This commit is contained in:
parent
72765757ef
commit
b60d82a531
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user