Skip to content

Commit bb7ca34

Browse files
committed
Add WebAssembly.Memory to js-sys
This adds definitions for the `WebAssembly.Memory` type to `js-sys`.
1 parent 8ce7465 commit bb7ca34

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,44 @@ pub mod WebAssembly {
30053005
#[wasm_bindgen(method, getter, js_namespace = WebAssembly)]
30063006
pub fn length(this: &Table) -> u32;
30073007
}
3008+
3009+
// WebAssembly.Memory
3010+
#[wasm_bindgen]
3011+
extern "C" {
3012+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory
3013+
#[wasm_bindgen(js_namespace = WebAssembly, extends = Object)]
3014+
#[derive(Clone, Debug)]
3015+
pub type Memory;
3016+
3017+
/// The `WebAssembly.Memory()` constructor creates a new `Memory` object
3018+
/// which is a resizable `ArrayBuffer` that holds the raw bytes of
3019+
/// memory accessed by a WebAssembly `Instance`.
3020+
///
3021+
/// A memory created by JavaScript or in WebAssembly code will be
3022+
/// accessible and mutable from both JavaScript and WebAssembly.
3023+
///
3024+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory
3025+
#[wasm_bindgen(constructor, js_namespace = WebAssembly, catch)]
3026+
pub fn new(descriptor: &Object) -> Result<Memory, JsValue>;
3027+
3028+
/// An accessor property that returns the buffer contained in the
3029+
/// memory.
3030+
///
3031+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/buffer
3032+
#[wasm_bindgen(method, getter, js_namespace = WebAssembly)]
3033+
pub fn buffer(this: &Memory) -> JsValue;
3034+
3035+
/// The `grow()` protoype method of the `Memory` object increases the
3036+
/// size of the memory instance by a specified number of WebAssembly
3037+
/// pages.
3038+
///
3039+
/// Takes the number of pages to grow (64KiB in size) and returns the
3040+
/// previous size of memory, in pages.
3041+
///
3042+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow
3043+
#[wasm_bindgen(method, js_namespace = WebAssembly)]
3044+
pub fn grow(this: &Memory, pages: u32) -> u32;
3045+
}
30083046
}
30093047

30103048
// JSON

crates/js-sys/tests/wasm/WebAssembly.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,20 @@ fn runtime_error_inheritance() {
153153

154154
let _: &Error = error.as_ref();
155155
}
156+
157+
#[wasm_bindgen_test]
158+
fn memory_works() {
159+
let obj = Object::new();
160+
Reflect::set(obj.as_ref(), &"initial".into(), &1.into());
161+
let mem = WebAssembly::Memory::new(&obj).unwrap();
162+
assert!(mem.is_instance_of::<WebAssembly::Memory>());
163+
assert!(mem.is_instance_of::<Object>());
164+
assert!(mem.buffer().is_instance_of::<ArrayBuffer>());
165+
assert_eq!(mem.grow(1), 1);
166+
assert_eq!(mem.grow(2), 2);
167+
assert_eq!(mem.grow(3), 4);
168+
assert_eq!(
169+
mem.buffer().dyn_into::<ArrayBuffer>().unwrap().byte_length(),
170+
7 * 64 * 1024,
171+
);
172+
}

0 commit comments

Comments
 (0)