From e0f132692758221e35d715ebde201e2f592a8746 Mon Sep 17 00:00:00 2001 From: Larry Ewing Date: Thu, 11 Feb 2021 00:03:03 -0600 Subject: [PATCH] Clean up the console redirect a little --- src/mono/wasm/runtime-test.js | 93 +++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/src/mono/wasm/runtime-test.js b/src/mono/wasm/runtime-test.js index 952598414d21cd..9f928f3521c092 100644 --- a/src/mono/wasm/runtime-test.js +++ b/src/mono/wasm/runtime-test.js @@ -5,63 +5,70 @@ //glue code to deal with the differences between chrome, ch, d8, jsc and sm. var is_browser = typeof window != "undefined"; -var consoleWebSocket; -var print; -if (is_browser) { - // We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters - window.real_print = console.log; - print = function(_msg) { window.real_print(_msg); }; - console.log = print; - console.debug = print; - console.error = print; - console.trace = print; - console.warn = print; - console.info = print; +// if the engine doesn't provide a console +if (typeof (console) === "undefined") { + var console = { + log: globalThis.print, + clear: function () { } + }; +} +globalThis.testConsole = console; + +function proxyMethod (prefix, func, asJson) { + return function() { + var args = [...arguments]; + if (asJson) { + func (JSON.stringify({ + method: prefix, + payload: args[0], + arguments: args + })); + } else { + func([prefix + args[0], ...args.slice(1)]); + } + }; +}; + +var methods = ["debug", "trace", "warn", "info", "error"]; +for (var m of methods) { + if (typeof(console[m]) != "function") { + console[m] = proxyMethod(`console.${m}: `, console.log, false); + } +} + +function proxyJson (func) { + for (var m of ["log", ...methods]) + console[m] = proxyMethod(`console.${m}`,func, true); +} + +if (is_browser) { const consoleUrl = `${window.location.origin}/console`.replace('http://', 'ws://'); - consoleWebSocket = new WebSocket(consoleUrl); + let consoleWebSocket = new WebSocket(consoleUrl); consoleWebSocket.onopen = function(event) { - consoleWebSocket.send("browser: Console websocket connected."); - - window.real_print = function(msg) { - consoleWebSocket.send(msg); - }; + proxyJson(function (msg) { consoleWebSocket.send (msg); }); + globalThis.testConsole.log("browser: Console websocket connected."); }; consoleWebSocket.onerror = function(event) { console.log(`websocket error: ${event}`); }; + // We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters var url = new URL (decodeURI (window.location)); arguments = []; for (var v of url.searchParams) { if (v [0] == "arg") { - console.log ("URL ARG: " + v [0] + "=" + v [1]); arguments.push (v [1]); } } } +//proxyJson(console.log); -// JavaScript core does not have a console defined -if (typeof console === "undefined") { - var Console = function () { - this.log = function(msg){ print(msg) }; - this.clear = function() { }; - }; - console = new Console(); -} -if (typeof console !== "undefined") { - if (!console.debug) - console.debug = console.log; - if (!console.trace) - console.trace = console.log; - if (!console.warn) - console.warn = console.log; - if (!console.error) - console.error = console.log; -} +let print = globalThis.testConsole.log; +let printErr = globalThis.testConsole.error; if (typeof crypto === 'undefined') { // **NOTE** this is a simple insecure polyfill for testing purposes only @@ -112,7 +119,7 @@ function test_exit (exit_code) { if (is_browser) { // Notify the selenium script Module.exit_code = exit_code; - print ("WASM EXIT " + exit_code); + Module.print ("WASM EXIT " + exit_code); var tests_done_elem = document.createElement ("label"); tests_done_elem.id = "tests_done"; tests_done_elem.innerHTML = exit_code.toString (); @@ -123,7 +130,7 @@ function test_exit (exit_code) { } function fail_exec (reason) { - print (reason); + Module.print (reason); test_exit (1); } @@ -138,7 +145,7 @@ function inspect_object (o) { // Preprocess arguments var args = testArguments; -print("Arguments: " + testArguments); +console.info("Arguments: " + testArguments); profilers = []; setenv = {}; runtime_args = []; @@ -202,8 +209,8 @@ loadScript ("mono-config.js"); var Module = { mainScriptUrlOrBlob: "dotnet.js", - print: print, - printErr: function(x) { print ("WASM-ERR: " + x) }, + print, + printErr, onAbort: function(x) { print ("ABORT: " + x); @@ -297,7 +304,7 @@ var App = { Module.wasm_exit = Module.cwrap ('mono_wasm_exit', 'void', ['number']); - Module.print("Initializing....."); + console.info("Initializing....."); for (var i = 0; i < profilers.length; ++i) { var init = Module.cwrap ('mono_wasm_load_profiler_' + profilers [i], 'void', ['string'])