|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | "use strict"; |
5 | | -var Module = { |
| 5 | + |
| 6 | +let runBenchmark; |
| 7 | +let setTasks; |
| 8 | + |
| 9 | +class MainApp { |
| 10 | + init({ BINDING }) { |
| 11 | + runBenchmark = BINDING.bind_static_method("[Wasm.Browser.Bench.Sample] Sample.Test:RunBenchmark"); |
| 12 | + setTasks = BINDING.bind_static_method("[Wasm.Browser.Bench.Sample] Sample.Test:SetTasks"); |
| 13 | + |
| 14 | + var url = new URL(decodeURI(window.location)); |
| 15 | + let tasks = url.searchParams.getAll('task'); |
| 16 | + if (tasks != '') { |
| 17 | + setTasks(tasks.join(',')); |
| 18 | + } |
| 19 | + |
| 20 | + this.yieldBench(); |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + yieldBench() { |
| 25 | + let promise = runBenchmark(); |
| 26 | + promise.then(ret => { |
| 27 | + document.getElementById("out").innerHTML += ret; |
| 28 | + if (ret.length > 0) { |
| 29 | + setTimeout(() => { this.yieldBench(); }, 0); |
| 30 | + } else { |
| 31 | + document.getElementById("out").innerHTML += "Finished"; |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + async PageShow() { |
| 37 | + try { |
| 38 | + await this.waitFor('pageshow'); |
| 39 | + } finally { |
| 40 | + this.removeFrame(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + async ReachedManaged() { |
| 45 | + try { |
| 46 | + await this.waitFor('reached'); |
| 47 | + } finally { |
| 48 | + this.removeFrame(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async waitFor(eventName) { |
| 53 | + try { |
| 54 | + let promise; |
| 55 | + let promiseResolve; |
| 56 | + this._frame = document.createElement('iframe'); |
| 57 | + this._frame.src = 'appstart-frame.html'; |
| 58 | + |
| 59 | + promise = new Promise(resolve => { promiseResolve = resolve; }) |
| 60 | + window.resolveAppStartEvent = function (event) { |
| 61 | + if (!eventName || event == eventName) |
| 62 | + promiseResolve(); |
| 63 | + } |
| 64 | + |
| 65 | + document.body.appendChild(this._frame); |
| 66 | + await promise; |
| 67 | + } catch (err) { |
| 68 | + console.log(err); |
| 69 | + throw err; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + removeFrame() { |
| 74 | + this._frame.contentWindow.muteErrors(); |
| 75 | + document.body.removeChild(this._frame); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +globalThis.mainApp = new MainApp(); |
| 80 | + |
| 81 | +createDotnetRuntime(({ BINDING }) => ({ |
| 82 | + disableDotnet6Compatibility: true, |
6 | 83 | configSrc: "./mono-config.json", |
7 | 84 | onDotnetReady: () => { |
8 | 85 | try { |
9 | | - App.init(); |
| 86 | + mainApp.init({ BINDING }); |
10 | 87 | } catch (error) { |
11 | | - console.log("exception: " + error); |
12 | 88 | set_exit_code(1, error); |
13 | 89 | throw (error); |
14 | 90 | } |
15 | 91 | }, |
16 | 92 | onAbort: (error) => { |
17 | 93 | set_exit_code(1, error); |
18 | 94 | }, |
| 95 | +})); |
| 96 | + |
| 97 | +function set_exit_code(exit_code, reason) { |
| 98 | + /* Set result in a tests_done element, to be read by xharness */ |
| 99 | + const tests_done_elem = document.createElement("label"); |
| 100 | + tests_done_elem.id = "tests_done"; |
| 101 | + tests_done_elem.innerHTML = exit_code.toString(); |
| 102 | + document.body.appendChild(tests_done_elem); |
| 103 | + |
| 104 | + console.log(`WASM EXIT ${exit_code}`); |
19 | 105 | }; |
0 commit comments