Skip to content

Commit c89182b

Browse files
committed
Merge remote-tracking branch 'origin/master' into bump
2 parents 98008b9 + 502311f commit c89182b

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ serde = { version = "1.0", optional = true }
3333
serde_json = { version = "1.0", optional = true }
3434

3535
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
36+
js-sys = { path = 'crates/js-sys', version = '0.2.3' }
3637
wasm-bindgen-test = { path = 'crates/test', version = '=0.2.18' }
3738
serde_derive = "1.0"
3839
wasm-bindgen-test-crate-a = { path = 'tests/crates/a', version = '0.1' }

crates/cli-support/src/js/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ impl<'a> Context<'a> {
380380
))
381381
})?;
382382

383+
self.bind("__wbindgen_memory", &|me| {
384+
me.expose_add_heap_object();
385+
let mem = me.memory();
386+
Ok(format!(
387+
"
388+
function() {{
389+
return addHeapObject({});
390+
}}
391+
", mem
392+
))
393+
})?;
394+
383395
self.create_memory_export();
384396
self.unexport_unused_internal_exports();
385397
self.gc()?;

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ externs! {
437437
fn __wbindgen_json_parse(ptr: *const u8, len: usize) -> u32;
438438
fn __wbindgen_json_serialize(idx: u32, ptr: *mut *mut u8) -> usize;
439439
fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
440+
441+
fn __wbindgen_memory() -> u32;
440442
}
441443

442444
impl Clone for JsValue {
@@ -562,6 +564,13 @@ pub fn throw(s: &str) -> ! {
562564
}
563565
}
564566

567+
/// Returns a handle to this wasm instance's `WebAssembly.Memory`
568+
pub fn memory() -> JsValue {
569+
unsafe {
570+
JsValue { idx: __wbindgen_memory() }
571+
}
572+
}
573+
565574
#[doc(hidden)]
566575
pub mod __rt {
567576
use core::cell::{Cell, UnsafeCell};

tests/wasm/api.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use wasm_bindgen::{self, JsCast};
12
use wasm_bindgen_test::*;
23
use wasm_bindgen::prelude::*;
4+
use js_sys::{WebAssembly, Uint8Array};
35

46
#[wasm_bindgen(module = "tests/wasm/api.js")]
57
extern {
@@ -135,3 +137,17 @@ fn null_keeps_working() {
135137
assert_null(JsValue::null());
136138
assert_null(JsValue::null());
137139
}
140+
141+
#[wasm_bindgen_test]
142+
fn memory_accessor_appears_to_work() {
143+
let data = 3u32;
144+
let ptr = &data as *const u32 as u32;
145+
146+
let my_mem = wasm_bindgen::memory();
147+
let mem = my_mem.dyn_into::<WebAssembly::Memory>().unwrap();
148+
let buf = mem.buffer();
149+
let slice = Uint8Array::new(&buf);
150+
let mut v = Vec::new();
151+
slice.subarray(ptr, ptr + 4).for_each(&mut |val, _, _| v.push(val));
152+
assert_eq!(v, [3, 0, 0, 0]);
153+
}

tests/wasm/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg(target_arch = "wasm32")]
22

3+
extern crate js_sys;
34
extern crate wasm_bindgen_test;
45
extern crate wasm_bindgen;
56
extern crate wasm_bindgen_test_crate_a;

0 commit comments

Comments
 (0)