second-state / wasmedge-quickjs

A high-performance, secure, extensible, and OCI-complaint JavaScript runtime for WasmEdge.
Apache License 2.0
477 stars 59 forks source link

Allocate error: Instance(NotFoundFunc("allocate")) when using a wasmedge_quickjs module with rust sdk host #136

Open jsoneaday opened 5 months ago

jsoneaday commented 5 months ago

I get this error when calling a function called init from a rust host. I've tried including the js file both in the wasm file and the in the host project but continue to get this error. My entire project can be found at https://github.com/jsoneaday/ao in the servers/cu_rs folder

This is the wasm module

use wasmedge_quickjs::*;
use wasmedge_bindgen_macro::*;

#[wasmedge_bindgen]
pub fn init() {
    let mut ctx = Context::new();

    let code = r#"
        import("main.js")
            .then(res => {
                return res.init();
            })
    "#;

    let p = ctx.eval_global_str(code);
    ctx.promise_loop_poll();
    if let JsValue::Promise(ref p) = p {
        let v = p.get_result();
        println!("v: {:?}", v);
    }
}

// this is the host function calling the init function in the module

fn do_js_work() -> Result<(), Box<dyn std::error::Error>> {
    let common_options = CommonConfigOptions::default()
        .bulk_memory_operations(true)
        .multi_value(true)
        .mutable_globals(true)
        .non_trap_conversions(true)
        .reference_types(true)
        .sign_extension_operators(true)
        .simd(true);
    let host_options = HostRegistrationConfigOptions::default()
        .wasi(true);
    let config = ConfigBuilder::new(common_options)
        .with_host_registration_config(host_options)
        .build()
        .unwrap();

    let vm = VmBuilder::new().with_config(config).build()?;
    let module = Module::from_file(None, "wasm/js_lib.wasm").unwrap();
    let vm = vm.register_module(None, module).unwrap();
    let mut bg = Bindgen::new(vm);

    match bg.run_wasm("init", params!()) {
        Ok(res) => println!("init result {:?}", res),
        Err(e) => println!("error {:?}", e)
    };

    Ok(())
}
jsoneaday commented 5 months ago

I just tried switching to this module code, but same error.

#[wasmedge_bindgen]
pub fn init() {
    let mut ctx = Context::new();

    let code = r#"
        let module = import("main.js")
        module
    "#;

    let p = ctx.eval_global_str(code);
    ctx.promise_loop_poll();
    if let JsValue::Promise(ref p) = p {
        let module = p.get_result();
        println!("v: {:?}", module);

        if let JsValue::Object(obj_mod) = module {
            let func = obj_mod.get("init");
            println!("init func: {:?}", func);

            if let JsValue::Function(func) = func {
                let result = func.call(&mut []);
                println!("result {:?}", result);
            }
        }
    }
}

The js module is a large bundled file, but the main function that I'm trying to call looks like this

import Arweave from "arweave";
export { default as Arweave } from "arweave";
export { default as WarpArBundles } from "warp-arbundles";

export function init() {
  return Arweave.init({});
}
L-jasmine commented 5 months ago

Your use case seems to be running WebAssembly in a browser and then using JavaScript within the WebAssembly. This scenario is not supported by us; our goal is to run JavaScript within WASI.