| 1 |
use wasm_bindgen::prelude::*; |
| 2 |
use wasm_bindgen::Clamped; |
| 3 |
use wasm_bindgen_test::*; |
| 4 |
|
| 5 |
#[wasm_bindgen(module = "tests/wasm/slice.js")] |
| 6 |
extern "C" { |
| 7 |
fn js_export(); |
| 8 |
|
| 9 |
fn js_import(); |
| 10 |
|
| 11 |
fn js_pass_array(); |
| 12 |
|
| 13 |
fn js_export_mut(); |
| 14 |
|
| 15 |
fn js_return_vec(); |
| 16 |
|
| 17 |
fn js_clamped(val: Clamped<&[u8]>, offset: u8); |
| 18 |
#[wasm_bindgen(js_name = js_clamped)] |
| 19 |
fn js_clamped2(val: Clamped<Vec<u8>>, offset: u8); |
| 20 |
#[wasm_bindgen(js_name = js_clamped)] |
| 21 |
fn js_clamped3(val: Clamped<&mut [u8]>, offset: u8); |
| 22 |
} |
| 23 |
|
| 24 |
macro_rules! export_macro { |
| 25 |
($(($i:ident, $n:ident, $optional_n:ident))*) => ($( |
| 26 |
#[wasm_bindgen] |
| 27 |
pub fn $n(a: &[$i]) -> Vec<$i> { |
| 28 |
assert_eq!(a.len(), 2); |
| 29 |
assert_eq!(a[0], 1 as $i); |
| 30 |
assert_eq!(a[1], 2 as $i); |
| 31 |
a.to_vec() |
| 32 |
} |
| 33 |
|
| 34 |
#[wasm_bindgen] |
| 35 |
pub fn $optional_n(a: Option<Vec<$i>>) -> Option<Vec<$i>> { |
| 36 |
a.map(|a| { |
| 37 |
assert_eq!(a.len(), 2); |
| 38 |
assert_eq!(a[0], 1 as $i); |
| 39 |
assert_eq!(a[1], 2 as $i); |
| 40 |
a.to_vec() |
| 41 |
}) |
| 42 |
} |
| 43 |
)*) |
| 44 |
} |
| 45 |
|
| 46 |
export_macro! { |
| 47 |
(i8, export_i8, export_optional_i8) |
| 48 |
(u8, export_u8, export_optional_u8) |
| 49 |
(i16, export_i16, export_optional_i16) |
| 50 |
(u16, export_u16, export_optional_u16) |
| 51 |
(i32, export_i32, export_optional_i32) |
| 52 |
(u32, export_u32, export_optional_u32) |
| 53 |
(isize, export_isize, export_optional_isize) |
| 54 |
(usize, export_usize, export_optional_usize) |
| 55 |
(f32, export_f32, export_optional_f32) |
| 56 |
(f64, export_f64, export_optional_f64) |
| 57 |
} |
| 58 |
|
| 59 |
#[wasm_bindgen_test] |
| 60 |
fn export() { |
| 61 |
js_export(); |
| 62 |
} |
| 63 |
|
| 64 |
macro_rules! import_macro { |
| 65 |
($(($rust:ident, $js:ident, $i:ident))*) => ($( |
| 66 |
#[wasm_bindgen(module = "tests/wasm/slice.js")] |
| 67 |
extern "C" { |
| 68 |
fn $js(a: &[$i], b: Option<&[$i]>, c: Option<&[$i]>) -> Vec<$i>; |
| 69 |
} |
| 70 |
|
| 71 |
#[wasm_bindgen] |
| 72 |
pub fn $rust(a: &[$i]) -> Vec<$i> { |
| 73 |
assert_eq!(a.len(), 2); |
| 74 |
assert_eq!(a[0], 1 as $i); |
| 75 |
assert_eq!(a[1], 2 as $i); |
| 76 |
$js(a, Some(a), None) |
| 77 |
} |
| 78 |
)*) |
| 79 |
} |
| 80 |
|
| 81 |
import_macro! { |
| 82 |
(import_rust_i8, import_js_i8, i8) |
| 83 |
(import_rust_u8, import_js_u8, u8) |
| 84 |
(import_rust_i16, import_js_i16, i16) |
| 85 |
(import_rust_u16, import_js_u16, u16) |
| 86 |
(import_rust_i32, import_js_i32, i32) |
| 87 |
(import_rust_u32, import_js_u32, u32) |
| 88 |
(import_rust_isize, import_js_isize, isize) |
| 89 |
(import_rust_usize, import_js_usize, usize) |
| 90 |
(import_rust_f32, import_js_f32, f32) |
| 91 |
(import_rust_f64, import_js_f64, f64) |
| 92 |
} |
| 93 |
|
| 94 |
#[wasm_bindgen_test] |
| 95 |
fn import() { |
| 96 |
js_import(); |
| 97 |
} |
| 98 |
|
| 99 |
macro_rules! pass_array_marco { |
| 100 |
($(($rust:ident, $i:ident))*) => ($( |
| 101 |
#[wasm_bindgen] |
| 102 |
pub fn $rust(a: &[$i]) { |
| 103 |
assert_eq!(a.len(), 2); |
| 104 |
assert_eq!(a[0], 1 as $i); |
| 105 |
assert_eq!(a[1], 2 as $i); |
| 106 |
} |
| 107 |
)*) |
| 108 |
} |
| 109 |
|
| 110 |
pass_array_marco! { |
| 111 |
(pass_array_rust_i8, i8) |
| 112 |
(pass_array_rust_u8, u8) |
| 113 |
(pass_array_rust_i16, i16) |
| 114 |
(pass_array_rust_u16, u16) |
| 115 |
(pass_array_rust_i32, i32) |
| 116 |
(pass_array_rust_u32, u32) |
| 117 |
(pass_array_rust_isize, isize) |
| 118 |
(pass_array_rust_usize, usize) |
| 119 |
(pass_array_rust_f32, f32) |
| 120 |
(pass_array_rust_f64, f64) |
| 121 |
} |
| 122 |
|
| 123 |
#[wasm_bindgen_test] |
| 124 |
fn pass_array() { |
| 125 |
js_pass_array(); |
| 126 |
} |
| 127 |
|
| 128 |
macro_rules! import_mut_macro { |
| 129 |
($(($rust:ident, $js:ident, $i:ident))*) => ( |
| 130 |
$( |
| 131 |
#[wasm_bindgen(module = "tests/wasm/slice.js")] |
| 132 |
extern "C" { |
| 133 |
fn $js(a: &mut [$i], b: Option<&mut [$i]>, c: Option<&mut [$i]>); |
| 134 |
} |
| 135 |
|
| 136 |
fn $rust() { |
| 137 |
let mut buf1 = [ |
| 138 |
1 as $i, |
| 139 |
2 as $i, |
| 140 |
3 as $i, |
| 141 |
]; |
| 142 |
let mut buf2 = [ |
| 143 |
4 as $i, |
| 144 |
5 as $i, |
| 145 |
6 as $i, |
| 146 |
]; |
| 147 |
$js(&mut buf1, Some(&mut buf2), None); |
| 148 |
assert_eq!(buf1[0], 4 as $i); |
| 149 |
assert_eq!(buf1[1], 5 as $i); |
| 150 |
assert_eq!(buf1[2], 3 as $i); |
| 151 |
assert_eq!(buf2[0], 8 as $i); |
| 152 |
assert_eq!(buf2[1], 7 as $i); |
| 153 |
assert_eq!(buf2[2], 6 as $i); |
| 154 |
} |
| 155 |
)* |
| 156 |
|
| 157 |
#[wasm_bindgen_test] |
| 158 |
fn import_mut() { |
| 159 |
$($rust();)* |
| 160 |
} |
| 161 |
) |
| 162 |
} |
| 163 |
|
| 164 |
import_mut_macro! { |
| 165 |
(import_mut_rust_i8, import_mut_js_i8, i8) |
| 166 |
(import_mut_rust_u8, import_mut_js_u8, u8) |
| 167 |
(import_mut_rust_i16, import_mut_js_i16, i16) |
| 168 |
(import_mut_rust_u16, import_mut_js_u16, u16) |
| 169 |
(import_mut_rust_i32, import_mut_js_i32, i32) |
| 170 |
(import_mut_rust_u32, import_mut_js_u32, u32) |
| 171 |
(import_mut_rust_f32, import_mut_js_f32, f32) |
| 172 |
(import_mut_rust_f64, import_mut_js_f64, f64) |
| 173 |
} |
| 174 |
|
| 175 |
macro_rules! export_mut_macro { |
| 176 |
($(($i:ident, $n:ident))*) => ($( |
| 177 |
#[wasm_bindgen] |
| 178 |
pub fn $n(a: &mut [$i]) { |
| 179 |
assert_eq!(a.len(), 3); |
| 180 |
assert_eq!(a[0], 1 as $i); |
| 181 |
assert_eq!(a[1], 2 as $i); |
| 182 |
assert_eq!(a[2], 3 as $i); |
| 183 |
a[0] = 4 as $i; |
| 184 |
a[1] = 5 as $i; |
| 185 |
} |
| 186 |
)*) |
| 187 |
} |
| 188 |
|
| 189 |
export_mut_macro! { |
| 190 |
(i8, export_mut_i8) |
| 191 |
(u8, export_mut_u8) |
| 192 |
(i16, export_mut_i16) |
| 193 |
(u16, export_mut_u16) |
| 194 |
(i32, export_mut_i32) |
| 195 |
(u32, export_mut_u32) |
| 196 |
(isize, export_mut_isize) |
| 197 |
(usize, export_mut_usize) |
| 198 |
(f32, export_mut_f32) |
| 199 |
(f64, export_mut_f64) |
| 200 |
} |
| 201 |
|
| 202 |
#[wasm_bindgen_test] |
| 203 |
fn export_mut() { |
| 204 |
js_export_mut(); |
| 205 |
} |
| 206 |
|
| 207 |
#[wasm_bindgen] |
| 208 |
pub fn return_vec_broken_vec() -> Vec<u32> { |
| 209 |
vec![1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 210 |
} |
| 211 |
|
| 212 |
#[wasm_bindgen] |
| 213 |
pub fn return_vec_web_main() -> ReturnVecApplication { |
| 214 |
ReturnVecApplication::new() |
| 215 |
} |
| 216 |
|
| 217 |
#[wasm_bindgen] |
| 218 |
pub struct ReturnVecApplication { |
| 219 |
thing: Vec<u32>, |
| 220 |
} |
| 221 |
|
| 222 |
#[wasm_bindgen] |
| 223 |
impl ReturnVecApplication { |
| 224 |
#[allow(clippy::vec_init_then_push)] |
| 225 |
pub fn new() -> ReturnVecApplication { |
| 226 |
let mut thing = vec![]; |
| 227 |
thing.push(0); |
| 228 |
thing.push(0); |
| 229 |
thing.push(0); |
| 230 |
thing.push(0); |
| 231 |
thing.push(0); |
| 232 |
|
| 233 |
ReturnVecApplication { thing } |
| 234 |
} |
| 235 |
|
| 236 |
pub fn tick(&mut self) { |
| 237 |
self.thing = self.thing.clone(); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[wasm_bindgen_test] |
| 242 |
fn return_vec() { |
| 243 |
js_return_vec(); |
| 244 |
} |
| 245 |
|
| 246 |
#[wasm_bindgen_test] |
| 247 |
fn take_clamped() { |
| 248 |
js_clamped(Clamped(&[1, 2, 3]), 1); |
| 249 |
js_clamped2(Clamped(vec![4, 5, 6]), 4); |
| 250 |
js_clamped3(Clamped(&mut [7, 8, 9]), 7); |
| 251 |
} |