Skip to content

Commit 5925871

Browse files
committed
Adds support for the UInt8Array constructor and its fill method.
1 parent 947dfbe commit 5925871

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/js.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ extern {
6565
pub fn eval(js_source_text: &str) -> Result<JsValue, JsValue>;
6666
}
6767

68+
// UInt8Array
69+
#[wasm_bindgen]
70+
extern {
71+
pub type Uint8Array;
72+
73+
/// The `Uint8Array()` constructor creates an array of unsigned 8-bit integers.
74+
///
75+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
76+
#[wasm_bindgen(constructor)]
77+
pub fn new(constructor_arg: JsValue) -> Uint8Array;
78+
79+
/// The fill() method fills all the elements of an array from a start index
80+
/// to an end index with a static value. The end index is not included.
81+
///
82+
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
83+
#[wasm_bindgen(method)]
84+
pub fn fill(this: &Uint8Array, value: JsValue, start: u32, end: u32) -> Uint8Array;
85+
}
86+
6887
// Array
6988
#[wasm_bindgen]
7089
extern {

tests/all/js_globals/TypedArray.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#![allow(non_snake_case)]
2+
3+
use project;
4+
5+
#[test]
6+
fn new_undefined() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn new_array() -> js::Uint8Array {
17+
js::Uint8Array::new(JsValue::undefined())
18+
}
19+
"#)
20+
.file("test.ts", r#"
21+
import * as assert from "assert";
22+
import * as wasm from "./out";
23+
24+
export function test() {
25+
assert.equal(wasm.new_array().length, 0);
26+
}
27+
"#)
28+
.test()
29+
}
30+
31+
#[test]
32+
fn new_length() {
33+
project()
34+
.file("src/lib.rs", r#"
35+
#![feature(proc_macro, wasm_custom_section)]
36+
37+
extern crate wasm_bindgen;
38+
use wasm_bindgen::prelude::*;
39+
use wasm_bindgen::js;
40+
41+
#[wasm_bindgen]
42+
pub fn new_array() -> js::Uint8Array {
43+
js::Uint8Array::new(JsValue::from_f64(4.0))
44+
}
45+
"#)
46+
.file("test.ts", r#"
47+
import * as assert from "assert";
48+
import * as wasm from "./out";
49+
50+
export function test() {
51+
assert.equal(wasm.new_array().length, 4);
52+
}
53+
"#)
54+
.test()
55+
}
56+
57+
#[test]
58+
fn fill() {
59+
project()
60+
.file("src/lib.rs", r#"
61+
#![feature(proc_macro, wasm_custom_section)]
62+
63+
extern crate wasm_bindgen;
64+
use wasm_bindgen::prelude::*;
65+
use wasm_bindgen::js;
66+
67+
#[wasm_bindgen]
68+
pub fn fill_with(this: &js::Uint8Array, value: JsValue, start: u32, end: u32) -> js::Uint8Array {
69+
this.fill(value, start, end)
70+
}
71+
"#)
72+
.file("test.ts", r#"
73+
import * as assert from "assert";
74+
import * as wasm from "./out";
75+
76+
export function test() {
77+
let characters = new Uint8Array([0, 0, 0, 0, 0, 0]);
78+
let subset = wasm.fill_with(characters, 1, 0, 3);
79+
80+
assert.equal(subset[0], 1);
81+
assert.equal(subset[4], 0);
82+
}
83+
"#)
84+
.test()
85+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod JsFunction;
99
mod JsString;
1010
mod Number;
1111
mod Math;
12+
mod TypedArray;
1213

1314
#[test]
1415
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)