Redo as js function + fix tests

This commit is contained in:
Richard Dodd
2019-01-10 23:37:12 +00:00
parent 5b51d279b4
commit 126efd5a95
3 changed files with 79 additions and 48 deletions

View File

@ -350,16 +350,67 @@ impl<'a> Context<'a> {
))
})?;
self.bind("__wbindgen_to_string", &|me| {
self.bind("__wbindgen_debug_string", &|me| {
me.expose_pass_string_to_wasm()?;
me.expose_get_object();
me.expose_uint32_memory();
Ok(String::from(
"
function(i, len_ptr) {
let toString = getObject(i).toString();
if (typeof(toString) !== 'string') return 0;
const ptr = passStringToWasm(toString);
const debug_str = val => {
// primitive types
const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) {
return val + '';
}
if (type == 'string') {
return '\"' + val + '\"';
}
if (type == 'symbol') {
const description = val.description;
if (description == null) {
return 'Symbol()';
} else {
return 'Symbol(' + description + ')';
}
}
if (type == 'function') {
return 'Function';
}
// objects
if (Array.isArray(val)) {
const length = val.length;
let debug = '[';
if (length > 0) {
debug += debug_str(val[0]);
}
for(let i = 1; i < length; i++) {
debug += debug_str(val[i]) + ', ';
}
debug += ']';
return debug;
}
// Test for built-in
const builtInMatches = /\\[object ([^])+\\]/.exec(val.toString());
let className;
if (builtInMatches.len > 0) {
className = builtInMatches[0];
} else {
// Failed to match the standard '[object ClassName]'
return val.toString();
}
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) + ')';
} else {
return className;
}
};
const val = getObject(i);
const debug = debug_str(val);
const ptr = passStringToWasm(debug);
getUint32Memory()[len_ptr / 4] = WASM_VECTOR_LEN;
return ptr;
}