forked from rustwasm/wasm-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the necessary emscripten test mode.
Because the generated .js file when targeting emscripten in wasm-bindgen is meant to be consumed by emscripten rather than a standalone executable, we need some custom testing logic for emscripten.
- Loading branch information
1 parent
c3a0e58
commit 8eb889c
Showing
9 changed files
with
233 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
crates/cli/src/bin/wasm-bindgen-test-runner/emscripten_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(function() {{ | ||
var elem = document.querySelector('#output'); | ||
window.extraLibraryFuncs = []; | ||
window.addToLibrary = function(LibraryWbg) { | ||
window.wasmExports = {__wbindgen_start:() => {}}; | ||
window.cachedTextEncoder = {encodeInto:() => {}}; | ||
window.Module = {}; | ||
|
||
try { | ||
LibraryWbg.$initBindgen(); | ||
} catch (e) { | ||
elem.innerText = "test setup failed: " + e; | ||
} | ||
|
||
function testExtraLibraryFuncs () { | ||
['$initBindgen', '$addOnInit', '$CLOSURE_DTORS', '$getStringFromWasm0'].forEach((value) => { | ||
if (!extraLibraryFuncs.includes(value)) { | ||
return { status: false, e: `test result: ${value} not found`}; | ||
} | ||
}); | ||
return {status: true, e: `test result: ok`}; | ||
} | ||
|
||
function testLibraryWbg () { | ||
if (typeof Module.hello !== "function") { | ||
return {status: false, e:'test result: hello() is not found'}; | ||
} | ||
if (typeof Module.Interval !== "function") { | ||
return {status: false, e:'test result: Interval is not found'}; | ||
} | ||
|
||
const keys = Object.keys(LibraryWbg); | ||
const testNames = ['clearInterval', 'setInterval', 'log']; | ||
|
||
for (const name of testNames) { | ||
const regex = new RegExp(`^__wbg_${name}`); | ||
const res = keys.find(key => regex.test(key)); | ||
if (!res) { | ||
return {status: false, e:`test result: ${name} not found`}; | ||
} | ||
} | ||
return {status: true, e:'test result: ok'}; | ||
} | ||
|
||
const tests = [testExtraLibraryFuncs(), testLibraryWbg()]; | ||
for (const res of tests) { | ||
if (!res.status) { | ||
elem.innerText = res.e; | ||
return; | ||
} | ||
} | ||
elem.innerText = 'test result: ok'; | ||
|
||
}; | ||
}}()); |
14 changes: 14 additions & 0 deletions
14
crates/cli/src/bin/wasm-bindgen-test-runner/index-emscripten.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> | ||
</head> | ||
<body> | ||
<pre id="output">Loading scripts...</pre> | ||
<pre id="console_log"></pre> | ||
<pre id="console_info"></pre> | ||
<pre id="console_warn"></pre> | ||
<pre id="console_error"></pre> | ||
<!-- {IMPORT_SCRIPTS} --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![cfg(all(target_arch = "wasm32", target_os = "emscripten"))] | ||
|
||
extern crate wasm_bindgen; | ||
extern crate wasm_bindgen_test; | ||
|
||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_test::*; | ||
|
||
wasm_bindgen_test_configure!(run_in_emscripten); | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
fn setInterval(closure: &Closure<dyn FnMut()>, millis: u32) -> f64; | ||
fn clearInterval(token: f64); | ||
|
||
#[wasm_bindgen(js_namespace = console)] | ||
fn log(s: &str); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct Interval { | ||
closure: Closure<dyn FnMut()>, | ||
token: f64, | ||
} | ||
|
||
impl Interval { | ||
pub fn new<F: 'static>(millis: u32, f: F) -> Interval | ||
where | ||
F: FnMut() | ||
{ | ||
// Construct a new closure. | ||
let closure = Closure::new(f); | ||
|
||
// Pass the closure to JS, to run every n milliseconds. | ||
let token = setInterval(&closure, millis); | ||
|
||
Interval { closure, token } | ||
} | ||
} | ||
|
||
// When the Interval is destroyed, clear its `setInterval` timer. | ||
impl Drop for Interval { | ||
fn drop(&mut self) { | ||
clearInterval(self.token); | ||
} | ||
} | ||
|
||
// Keep logging "hello" every second until the resulting `Interval` is dropped. | ||
#[wasm_bindgen] | ||
pub fn hello() -> Interval { | ||
Interval::new(1_000, || log("hello")) | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn hello_test() { | ||
hello(); | ||
} |