Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link_name attribute usage #1454

Closed
HaronK opened this issue May 23, 2020 · 3 comments
Closed

link_name attribute usage #1454

HaronK opened this issue May 23, 2020 · 3 comments
Labels
❓ question I've a question!

Comments

@HaronK
Copy link

HaronK commented May 23, 2020

Summary

I'm trying to instantiate a wasm file built from Rust source but getting error about namespaces.

Additional details

So here is a simple Wasm source:

// wasm.rs
extern "C" {
    #[link_name = "ns1"]
    fn _ns1_func1();
    #[link_name = "ns1"]
    fn _ns1_func2();
}
extern "C" {
    #[link_name = "ns2"]
    fn _ns2_func1();
    #[link_name = "ns2"]
    fn _ns2_func2();
}

...

and host:

// host.rs
fn ns1_func1() {}
fn ns1_func2() {}
fn ns2_func1() {}
fn ns2_func2() {}

let mut import_object = ImportObject::new();

let mut ns1 = Namespace::new();
ns1.insert("_ns1_func1", func!(ns1_func1));
ns1.insert("_ns1_func2", func!(ns1_func2));
import_object.register("ns1", ns1);

let mut ns2 = Namespace::new();
ns2.insert("_ns2_func1", func!(ns2_func1));
ns2.insert("_ns2_func2", func!(ns2_func2));
import_object.register("ns2", ns2);

let instance = instantiate(wasm_bytes, &import_object).expect("failed to instantiate wasm module");

...

As you can see I'm using dynamic host function registration. I found the description of how to do it here.
So now when I try to run it I'm getting following error:

thread 'main' panicked at 'failed to instantiate wasm module: LinkError([ImportNotFound { namespace: "env", name: "ns2" }, ImportNotFound { namespace: "env", name: "ns1" }])', src\main.rs:32:1

Am I misunderstand meaning of namespaces or I just use them in a wrong way?

@HaronK HaronK added the ❓ question I've a question! label May 23, 2020
@HaronK
Copy link
Author

HaronK commented May 23, 2020

I found the solution.
Originally I was following recommendation at the bottom of this page to use #[link_name = "namespace"] attribute to specify function's namespace. But according to this example I have to use #[link(wasm_import_module = "namespace")] attribute to do that.

@HaronK
Copy link
Author

HaronK commented May 23, 2020

Closing this question.

@HaronK HaronK closed this as completed May 23, 2020
@MarkMcCaskey
Copy link
Contributor

Thanks for the report and sorry about the issue! It looks like that's a mistake in our docs, I'll add it to my todo list to correct that today!

MarkMcCaskey added a commit to wasmerio/old-docs.wasmer.io that referenced this issue May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants