Let's say I have the following definitions in the Rust code:
#[wasm_bindgen]
pub struct RustType {
foo: usize
}
#[wasm_bindgen]
impl RustType {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { foo: 100 }
}
}
#[wasm_bindgen]
pub fn print_addr(obj: &RustType) {
console_log!("rust addr = {}", obj as *const _ as u32);
}
JS code creates an instance of RustType
and passes it to the print_addr
function:
var obj = new RustType();
print_addr(obj);
After modifying the generated print_addr
function in index_bg.js
like this:
export function print_addr(obj) {
_assertClass(obj, RustType);
console.log("js addr = ", obj.ptr); // <== added this line
if (obj.ptr === 0) {
throw new Error('Attempt to use a moved value');
}
wasm.print_addr(obj.ptr);
}
in the dev console I'm getting the following output:
js addr = 1114120
rust addr = 1114124
The question is why the values of Rust pointer and JS pointer differ? Also according to my observations, the diff between Rust pointer and JS pointer is always equal to 4. Why is it like this?