| 1 |
//! A collection of tests to exercise imports where we don't need to generate a |
| 2 |
//! JS shim to convert arguments/returns even when Web IDL bindings is not |
| 3 |
//! implemented. |
| 4 |
|
| 5 |
use wasm_bindgen::prelude::*; |
| 6 |
use wasm_bindgen_test::*; |
| 7 |
|
| 8 |
#[wasm_bindgen(inline_js = " |
| 9 |
function assert_eq(a, b) { |
| 10 |
if (a !== b) { |
| 11 |
throw new Error(`assert_eq failed: ${a} != ${b}`); |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
module.exports.trivial = function () {}; |
| 16 |
|
| 17 |
module.exports.incoming_bool = function () { return true; }; |
| 18 |
module.exports.incoming_u8 = function () { return 255; }; |
| 19 |
module.exports.incoming_i8 = function () { return -127; }; |
| 20 |
module.exports.incoming_u16 = function () { return 65535; }; |
| 21 |
module.exports.incoming_i16 = function () { return 32767; }; |
| 22 |
module.exports.incoming_u32 = function () { return 4294967295; }; |
| 23 |
module.exports.incoming_i32 = function () { return 0; }; |
| 24 |
module.exports.incoming_f32 = function () { return 1.5; }; |
| 25 |
module.exports.incoming_f64 = function () { return Math.PI; }; |
| 26 |
|
| 27 |
module.exports.outgoing_u8 = function (k) { assert_eq(k, 255); }; |
| 28 |
module.exports.outgoing_i8 = function (i) { assert_eq(i, -127); }; |
| 29 |
module.exports.outgoing_u16 = function (l) { assert_eq(l, 65535); }; |
| 30 |
module.exports.outgoing_i16 = function (j) { assert_eq(j, 32767); }; |
| 31 |
module.exports.outgoing_i32 = function (x) { assert_eq(x, 0); }; |
| 32 |
module.exports.outgoing_f32 = function (y) { assert_eq(y, 1.5); }; |
| 33 |
module.exports.outgoing_f64 = function (pi) { assert_eq(pi, Math.PI); }; |
| 34 |
|
| 35 |
module.exports.many = function (x, y, pi) { |
| 36 |
assert_eq(x, 0); |
| 37 |
assert_eq(y, 1.5); |
| 38 |
assert_eq(pi, Math.PI); |
| 39 |
return 42; |
| 40 |
}; |
| 41 |
|
| 42 |
module.exports.works_when_externref_support_is_enabled = function (v) { |
| 43 |
assert_eq(v, 'hello'); |
| 44 |
return v; |
| 45 |
}; |
| 46 |
|
| 47 |
module.exports.MyNamespace = {}; |
| 48 |
module.exports.MyNamespace.incoming_namespaced = function () { return 13.37; }; |
| 49 |
module.exports.MyNamespace.outgoing_namespaced = function (w) { assert_eq(13.37, w); }; |
| 50 |
")] |
| 51 |
extern "C" { |
| 52 |
#[wasm_bindgen(assert_no_shim)] |
| 53 |
fn trivial(); |
| 54 |
|
| 55 |
#[wasm_bindgen(assert_no_shim)] |
| 56 |
fn incoming_bool() -> bool; |
| 57 |
#[wasm_bindgen(assert_no_shim)] |
| 58 |
fn incoming_u8() -> u8; |
| 59 |
#[wasm_bindgen(assert_no_shim)] |
| 60 |
fn incoming_i8() -> i8; |
| 61 |
#[wasm_bindgen(assert_no_shim)] |
| 62 |
fn incoming_u16() -> u16; |
| 63 |
#[wasm_bindgen(assert_no_shim)] |
| 64 |
fn incoming_i16() -> i16; |
| 65 |
#[wasm_bindgen(assert_no_shim)] |
| 66 |
fn incoming_u32() -> u32; |
| 67 |
#[wasm_bindgen(assert_no_shim)] |
| 68 |
fn incoming_i32() -> i32; |
| 69 |
#[wasm_bindgen(assert_no_shim)] |
| 70 |
fn incoming_f32() -> f32; |
| 71 |
#[wasm_bindgen(assert_no_shim)] |
| 72 |
fn incoming_f64() -> f64; |
| 73 |
|
| 74 |
#[wasm_bindgen(assert_no_shim)] |
| 75 |
fn outgoing_u8(k: u8); |
| 76 |
#[wasm_bindgen(assert_no_shim)] |
| 77 |
fn outgoing_i8(i: i8); |
| 78 |
#[wasm_bindgen(assert_no_shim)] |
| 79 |
fn outgoing_u16(l: u16); |
| 80 |
#[wasm_bindgen(assert_no_shim)] |
| 81 |
fn outgoing_i16(j: i16); |
| 82 |
#[wasm_bindgen(assert_no_shim)] |
| 83 |
fn outgoing_i32(x: i32); |
| 84 |
#[wasm_bindgen(assert_no_shim)] |
| 85 |
fn outgoing_f32(y: f32); |
| 86 |
#[wasm_bindgen(assert_no_shim)] |
| 87 |
fn outgoing_f64(z: f64); |
| 88 |
|
| 89 |
#[wasm_bindgen(assert_no_shim)] |
| 90 |
fn many(x: i32, y: f32, z: f64) -> i32; |
| 91 |
|
| 92 |
#[wasm_bindgen(assert_no_shim, js_namespace = MyNamespace)] |
| 93 |
fn incoming_namespaced() -> f64; |
| 94 |
#[wasm_bindgen(assert_no_shim, js_namespace = MyNamespace)] |
| 95 |
fn outgoing_namespaced(x: f64); |
| 96 |
|
| 97 |
// Note that this should only skip the JS shim if we have externref support |
| 98 |
// enabled. |
| 99 |
// |
| 100 |
// #[wasm_bindgen(assert_no_shim)] |
| 101 |
fn works_when_externref_support_is_enabled(v: JsValue) -> JsValue; |
| 102 |
} |
| 103 |
|
| 104 |
#[wasm_bindgen_test] |
| 105 |
fn no_shims() { |
| 106 |
trivial(); |
| 107 |
|
| 108 |
let k = incoming_u8(); |
| 109 |
assert_eq!(k, 255); |
| 110 |
outgoing_u8(k); |
| 111 |
|
| 112 |
let l = incoming_u16(); |
| 113 |
assert_eq!(l, 65535); |
| 114 |
outgoing_u16(l); |
| 115 |
|
| 116 |
let m = incoming_u32(); |
| 117 |
assert_eq!(m, 4294967295); |
| 118 |
|
| 119 |
let i = incoming_i8(); |
| 120 |
assert_eq!(i, -127); |
| 121 |
outgoing_i8(i); |
| 122 |
|
| 123 |
let j = incoming_i16(); |
| 124 |
assert_eq!(j, 32767); |
| 125 |
outgoing_i16(j); |
| 126 |
|
| 127 |
let x = incoming_i32(); |
| 128 |
assert_eq!(x, 0); |
| 129 |
outgoing_i32(x); |
| 130 |
|
| 131 |
let y = incoming_f32(); |
| 132 |
assert_eq!(y, 1.5); |
| 133 |
outgoing_f32(y); |
| 134 |
|
| 135 |
let pi = incoming_f64(); |
| 136 |
assert_eq!(pi, std::f64::consts::PI); |
| 137 |
outgoing_f64(pi); |
| 138 |
|
| 139 |
let z = many(x, y, pi); |
| 140 |
assert_eq!(z, 42); |
| 141 |
|
| 142 |
let w = incoming_namespaced(); |
| 143 |
assert_eq!(w, 13.37); |
| 144 |
outgoing_namespaced(w); |
| 145 |
|
| 146 |
let b = incoming_bool(); |
| 147 |
assert!(b); |
| 148 |
|
| 149 |
let v = JsValue::from("hello"); |
| 150 |
let vv = works_when_externref_support_is_enabled(v.clone()); |
| 151 |
assert_eq!(v, vv); |
| 152 |
} |