It's possible to pass an array of integers like this:
const js = import("./webassembly_rust");
let array_nums = [1,2,3,4,5,6,7,8,9];
js.then(js => {
js.test( array_nums );
});
to WebAssembly and save it in a vector like this:
extern crate serde_json;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[macro_use]
extern crate serde_derive;
#[wasm_bindgen]
pub fn test(array: JsValue) {
let elements: Vec<u32> = array.into_serde().unwrap();
}
It's also possible to pass a single object like this:
const js = import("./webassembly_rust");
let jsObject = {name: "hello world", id: "99", parent_id: "11"};
js.then(js => {
js.test( jsObject );
});
to WebAssembly and save it as a Element
struct like this:
extern crate serde_json;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[macro_use]
extern crate serde_derive;
#[derive(Serialize, Deserialize)]
pub struct Element {
name: String,
id: String,
parent_id: String,
}
#[wasm_bindgen]
pub fn test(js_object: &JsValue) {
let element: Element = js_object.into_serde().unwrap();
}
The next thing I tried is to pass an array of objects like this:
const js = import("./webassembly_rust");
let arrayOfObjects = [
{name: "hello world", id: "99", parent_id: "88"},
{name: "hello world2", id: "88", parent_id: "12"},
{name: "hello world3", id: "77", parent_id: "88"}
]
js.then(js => {
js.test( arrayOfObjects );
});
to WebAssembly and save it as a vector of Element
structs like this:
extern crate serde_json;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[macro_use]
extern crate serde_derive;
#[derive(Serialize, Deserialize)]
pub struct Element {
name: String,
id: String,
parent_id: String,
}
#[wasm_bindgen]
pub fn test(js_objects: &JsValue) {
let elements: Vec<Element> = js_objects.into_serde().unwrap();
}
This compiles, but when I run this code I get the error:
func $__rust_start_panic (param i32) (result i32)
unreachable
unreachable
end
Passing an array of objects filled with numbers like this:
const js = import("./webassembly_rust");
let arrayOfNumObjects = [
{name: 1, id: 2, parent_id: 3 },
{name: 1, id: 2, parent_id: 3 },
{name: 1, id: 2, parent_id: 3 }
]
js.then(js => {
js.test( arrayOfNumObjects );
});
to WebAssembly is possible when the Element
struct contains only u32
values.
extern crate serde_json;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[macro_use]
extern crate serde_derive;
#[derive(Serialize, Deserialize)]
pub struct Element {
name: u32,
id: u32,
parent_id: u32,
}
#[wasm_bindgen]
pub fn test(js_objects: &JsValue) {
let elements: Vec<Element> = js_objects.into_serde().unwrap();
}
It seems like the problem is caused by the String
type in the Element
struct.
What did I do wrong?
I found the following articles, but I can't find a solution for my problem in them:
Serializing and Deserializing Arbitrary Data Into and From
JsValue
with SerdeThis explains how to convert a JavaScript object to a struct, but not how to convert an array of objects to a vector of structs.
-
This crate allows to use JavaScript types like arrays or objects in Rust, but this is not what I want. I want to convert JavaScript values into their Rust counterparts. This crate only allows to use JavaScript inline in Rust, as far as I understand... and this isn't as fast as using just Rust.