I have two structs exported to Javascript. I can create instances and use them in JavaScript without any error but when I push the instances into a vector in Rust side, I have an error "Uncaught Error: null pointer passed to rust"
Since ownership is changed, it is totally normal that JS objects turns null but I also need to keep my JavaScript objects in order to change things in JavaScript side.
Is there any correct way to keep "vect" object not null and open to changes?
I added a working example. You can see error in your browser's console.
Rust code
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
struct Vector3 {
x: f32,
y: f32,
z: f32,
}
#[wasm_bindgen]
impl Vector3 {
#[wasm_bindgen(constructor)]
pub fn new() -> Vector3 {
return Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
};
}
pub fn get_x(&self) -> f32 {
self.x
}
}
#[wasm_bindgen(extends = Object)]
struct Group {
list: Vec<Vector3>,
}
#[wasm_bindgen]
impl Group {
#[wasm_bindgen(constructor)]
pub fn new() -> Group {
return Group { list: vec![] };
}
pub fn add(&mut self, vec: Vector3) {
self.list.push(vec);
}
}
JavaScript code
let group = new Group();
let list = [];
for (let i = 0; i < 10; i++) {
let vect = new Vector3();
list.push(vect);
group.add(vect);
}
setInterval(() => {
for (let i = 0; i < list.length; i++) {
const vect = list[i];
console.log(vect.get_x());
}
}, 1000);