wasm-bindgen-test: Have the test runner JS call the original console.log

This makes control flow a little easier to follow and avoids wasm->js->wasm
re-entrancy.
This commit is contained in:
Nick Fitzgerald
2019-01-14 14:41:32 -08:00
parent 51ef19b85f
commit a94f3f4403
4 changed files with 37 additions and 30 deletions

View File

@ -13,24 +13,30 @@
logs.innerHTML += `${msg}\n`; logs.innerHTML += `${msg}\n`;
} }
}; };
const orig_console_error = function(...args) { const orig_console_error = function(...args) {
const logs = document.getElementById('console_error'); const logs = document.getElementById('console_error');
for (let msg of args) { for (let msg of args) {
logs.innerHTML += `${msg}\n`; logs.innerHTML += `${msg}\n`;
} }
}; };
console.log = function(...args) { console.log = function(...args) {
if (window.console_log_redirect) if (window.console_log_redirect) {
window.console_log_redirect(orig_console_log, args); window.console_log_redirect(args);
else }
orig_console_log.apply(this, args);
orig_console_log.apply(this, args);
}; };
console.error = function(...args) { console.error = function(...args) {
if (window.console_error_redirect) if (window.console_error_redirect) {
window.console_error_redirect(orig_console_error, args); window.console_error_redirect(args);
else }
orig_console_error.apply(this, args);
orig_console_error.apply(this, args);
}; };
window.__wbg_test_invoke = f => f(); window.__wbg_test_invoke = f => f();
</script> </script>
<script src='run.js' type=module></script> <script src='run.js' type=module></script>

View File

@ -7,17 +7,21 @@
<script> <script>
const orig_console_log = console.log; const orig_console_log = console.log;
const orig_console_error = console.error; const orig_console_error = console.error;
console.log = function(...args) { console.log = function(...args) {
if (window.console_log_redirect) if (window.console_log_redirect) {
window.console_log_redirect(orig_console_log, args); window.console_log_redirect(args);
else }
orig_console_log.apply(this, args);
orig_console_log.apply(this, args);
}; };
console.error = function(...args) { console.error = function(...args) {
if (window.console_error_redirect) if (window.console_error_redirect) {
window.console_error_redirect(orig_console_error, args); window.console_error_redirect(args);
else }
orig_console_error.apply(this, args);
orig_console_error.apply(this, args);
}; };
window.__wbg_test_invoke = f => f(); window.__wbg_test_invoke = f => f();

View File

@ -24,19 +24,17 @@ pub fn execute(
// all these calls and capture the output of tests // all these calls and capture the output of tests
const prev_log = console.log; const prev_log = console.log;
console.log = function(...args) {{ console.log = function(...args) {{
if (console_log_redirect === null) {{ if (console_log_redirect) {{
prev_log.apply(null, args); console_log_redirect(args);
}} else {{
console_log_redirect(prev_log, args);
}} }}
prev_log.apply(null, args);
}}; }};
const prev_error = console.error; const prev_error = console.error;
console.error = function(...args) {{ console.error = function(...args) {{
if (console_error_redirect === null) {{ if (console_error_redirect) {{
prev_error.apply(null, args); console_error_redirect(args);
}} else {{
console_error_redirect(prev_error, args);
}} }}
prev_error.apply(null, args);
}}; }};
global.__wbg_test_invoke = f => f(); global.__wbg_test_invoke = f => f();

View File

@ -305,21 +305,20 @@ scoped_thread_local!(static CURRENT_OUTPUT: RefCell<Output>);
// attach it to. The main `test` crate in the rust repo also has issues about // attach it to. The main `test` crate in the rust repo also has issues about
// how not all output is captured, causing some inconsistencies sometimes. // how not all output is captured, causing some inconsistencies sometimes.
#[wasm_bindgen] #[wasm_bindgen]
pub fn __wbgtest_console_log(original: &Function, args: &Array) { pub fn __wbgtest_console_log(args: &Array) {
record(original, args, |output| &mut output.log) record(args, |output| &mut output.log)
} }
/// Handler for `console.error` invocations. /// Handler for `console.error` invocations.
/// ///
/// Works the same as `console_log` above. /// Works the same as `console_log` above.
#[wasm_bindgen] #[wasm_bindgen]
pub fn __wbgtest_console_error(original: &Function, args: &Array) { pub fn __wbgtest_console_error(args: &Array) {
record(original, args, |output| &mut output.error) record(args, |output| &mut output.error)
} }
fn record(orig: &Function, args: &Array, dst: impl FnOnce(&mut Output) -> &mut String) { fn record(args: &Array, dst: impl FnOnce(&mut Output) -> &mut String) {
if !CURRENT_OUTPUT.is_set() { if !CURRENT_OUTPUT.is_set() {
drop(orig.apply(&JsValue::null(), args));
return; return;
} }