Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating web worker from Rust with Emscripten target

I'm trying to create a web worker from Rust, calling a function in the worker file and passing some data to the main thread.

main.rs

mod externs;
extern crate libc;

fn main() {
    println!("starting worker");
    let worker = externs::create_worker("./worker.js");
    externs::call_worker(worker, "worker_fn", "", 0);
    println!("worker called");
}

worker.rs

#![feature(link_args)]
#[link_args = "-s EXPORTED_FUNCTIONS=['_worker_fn'] -s BUILD_AS_WORKER=1"]

extern crate libc;

mod externs;

extern {}

fn main() {
    println!("worker main");
}

#[no_mangle]
pub extern fn worker_fn() {
    println!("hello from the other side!");
}

When I compile the worker and main files, I'm able to see the message from main.rs and even the "worker main" message in the worker file. I can also see that the browser sends a request to worker.js, but it seems like the main thread does not call the worker_fn inside the worker file.

This is the externs file:

use std::ffi::CString;
use libc::*;
use std::str::FromStr;

/// Creating web worker
pub fn create_worker(url: &str) -> ffi::worker_handle {
    let url = CString::new(url).unwrap();
    let ptr = url.as_ptr();
    unsafe { ffi::emscripten_create_worker(ptr) }
}

extern "C" fn do_something_handler(arg1: *mut c_char, arg2: c_int, arg3: *mut c_void) {
    println!("worker done!");
}

/// Creating web worker
pub fn call_worker(worker: ffi::worker_handle, func_name: &str, data: &str, size: i32) {
    let func_name = CString::new(func_name).unwrap();

    let mut string = String::from_str(data).unwrap();
    let bytes = string.into_bytes();
    let mut cchar : Vec<c_char> = bytes.iter().map(|&w| w as c_char).collect();
    let data_slice = cchar.as_mut_slice();

    let mut state = 42;
    let state_ptr: *mut c_void = &mut state as *mut _ as *mut c_void;

    unsafe {
        ffi::emscripten_call_worker(
            worker,
            func_name.as_ptr(),
            data_slice.as_mut_ptr(),
            size as c_int,
            Some(do_something_handler),
            state_ptr
        )
    };
}

// This is mostly standard Rust-C FFI stuff.
mod ffi {
    use libc::*;
    pub type worker_handle = c_int;
    pub type em_worker_callback_func = Option<unsafe extern "C" fn(arg1: *mut c_char,
                                                                   arg2: c_int,
                                                                   arg3: *mut c_void)>;

    extern "C" {
        pub fn emscripten_run_script_int(x: *const c_char) -> c_int;
        pub fn emscripten_create_worker(url: *const c_char) -> worker_handle;
        pub fn emscripten_call_worker(
            worker: worker_handle,
            funcname: *const c_char,
            data: *mut c_char,
            size: c_int,
            callback: em_worker_callback_func,
            arg: *mut c_void
        );
        pub fn emscripten_worker_respond(data: *mut c_char, size: c_int);
        pub fn emscripten_worker_respond_provisionally(data: *mut c_char, size: c_int);
    }
}

I don't understand what the problem is. Should I somehow change the worker file or maybe even the link_args?

like image 419
Afshin Mehrabani Avatar asked Jul 21 '17 23:07

Afshin Mehrabani


People also ask

Does Rust use emscripten?

It is possible to use Emscripten to compile Rust programs using C APIs to small WebAssembly binaries. They can be just as small as corresponding C programs, in fact, and those can be quite compact.

Can WebAssembly be multithreaded?

The WebAssembly Threads feature allows multiple WebAssembly instances in separate Web Workers to share a single WebAssembly. Memory object. As with SharedArrayBuffers in JavaScript, this allows very fast communication between the Workers.

What is Wasm_bindgen?

The wasm-bindgen tool is sort of half polyfill for features like the host bindings proposal and half features for empowering high-level interactions between JS and wasm-compiled code (currently mostly from Rust).

Where should you place the JavaScript code to run in the context of a web worker?

You can run whatever code you like inside the worker thread, with some exceptions. For example, you can't directly manipulate the DOM from inside a worker, or use some default methods and properties of the window object.


1 Answers

I fixed the problem by using stdweb crate like this (Thanks ivanceras):

worker.rs

#![feature(link_args)]
#[link_args = "-s BUILD_AS_WORKER=1"]

#[macro_use]
extern crate stdweb;

fn main(){
    stdweb::initialize();

    js! {
        this.addEventListener("message", (e) => {
            console.log("The main thread said something", e.data);
        })
    }
    stdweb::event_loop();
}

loader.js

var wasm_file = "worker.wasm"; // wasm file
var wjs_file = "worker.js"; // w.js file that links the wasm file

Module = {}
console.log("Loading webassembly version");
/// fetch wasm file and inject the js file
fetch(wasm_file)
  .then(response => response.arrayBuffer())
  .then(bytes => {
    Module.wasmBinary = bytes;
    console.log("wasm has loaded..");
    console.log("attaching as script");
    self.importScripts(wjs_file);
  });

and finally, the HTML file:

<script>
    var worker = new Worker("loader.js");
    setTimeout(function () {
        worker.postMessage({"cmd":"doSomething"});
    }, 1000);
</script>

Don't forget to add the --target=wasm32-unknown-emscripten flag when you build the Rust file.

like image 158
Afshin Mehrabani Avatar answered Sep 28 '22 05:09

Afshin Mehrabani