WIP: experiment with passing data between Rust wasm modules

This commit is contained in:
Rob Watson 2020-09-17 15:34:38 +02:00
parent a30ca384b0
commit 5723985f6a
6 changed files with 38 additions and 10 deletions

View File

@ -28,5 +28,6 @@ features = [
"HtmlCanvasElement", "HtmlCanvasElement",
"CanvasRenderingContext2d", "CanvasRenderingContext2d",
"Window", "Window",
"File",
] ]

View File

@ -1,10 +1,18 @@
use super::canvas::Canvas; use super::canvas::Canvas;
use crate::agents::audio_agent::{self, AudioAgent}; use crate::agents::audio_agent::{self, AudioAgent};
use crate::components::Player; use crate::components::Player;
use web_sys::HtmlInputElement; use web_sys::{File, HtmlInputElement};
use yew::agent::Dispatcher; use yew::agent::Dispatcher;
use yew::prelude::*; use yew::prelude::*;
use yew::services::ConsoleService; use yew::services::ConsoleService;
use wasm_bindgen::prelude::*;
use weblog::*;
#[wasm_bindgen(module = "/static/player.js")]
extern "C" {
fn get_val() -> i32;
fn load_file(file: &str) -> i32;
}
pub struct Container { pub struct Container {
link: ComponentLink<Self>, link: ComponentLink<Self>,
@ -88,8 +96,9 @@ impl Container {
let file_list = file_input.files().ok_or("could not get file list")?; let file_list = file_input.files().ok_or("could not get file list")?;
let file = file_list.get(0).ok_or("could not get file")?; let file = file_list.get(0).ok_or("could not get file")?;
self.audio_agent load_file("oh!");
.send(audio_agent::Request::LoadSamplesFromFile(file));
console_log!("Done.");
Ok(()) Ok(())
} }

View File

@ -12,17 +12,10 @@ mod agents;
mod components; mod components;
mod utils; mod utils;
#[wasm_bindgen(module = "/static/player.js")]
extern "C" {
fn get_val() -> i32;
}
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
pub fn run_app() { pub fn run_app() {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic::set_hook(Box::new(console_error_panic_hook::hook)); panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log!("Get value from player module:", get_val());
App::<components::App>::new().mount_to_body(); App::<components::App>::new().mount_to_body();
} }

View File

@ -27,4 +27,8 @@ export function get_val() {
return wasmModule.get_val(); return wasmModule.get_val();
} }
export function load_file(f) {
return wasmModule.load_file(f);
}
init(); init();

View File

@ -10,3 +10,9 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
wasm-bindgen = { version = "0.2" } wasm-bindgen = { version = "0.2" }
lazy_static = "1.4.0"
weblog = "0.2"
[dependencies.web-sys]
version = "0.3.44"
features = ["File"]

View File

@ -1,6 +1,21 @@
#[macro_use]
extern crate lazy_static;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::File;
use weblog::*;
lazy_static! {
static ref BUFFER: Vec<f32> = vec![];
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn get_val() -> i32 { pub fn get_val() -> i32 {
123 123
} }
#[wasm_bindgen]
pub fn load_file(file_element_id: &str) -> i32 {
console_log!("Got file:", file_element_id);
0
}