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

How to use memory in version 3.0 #3113

Closed
poria-cat opened this issue Aug 16, 2022 · 5 comments
Closed

How to use memory in version 3.0 #3113

poria-cat opened this issue Aug 16, 2022 · 5 comments
Labels
❓ question I've a question!

Comments

@poria-cat
Copy link

poria-cat commented Aug 16, 2022

Summary

I am trying to use wasmer to run as AssemblyScript in rust, import functions is the main way to do this, but I'm stuck with memory and ptr at the moment 😭

We can use memory in version 1 and version 2 by set with LazyInit, but in new version, LazyInit was deleted. So I'm confused about how to use it in import functions.

How should I use it in the new version?

@poria-cat poria-cat added the ❓ question I've a question! label Aug 16, 2022
@poria-cat poria-cat changed the title How to use memory and ptr in version 3.0 How to use memory in version 3.0 Aug 16, 2022
@syrusakbary
Copy link
Member

Hi! Thanks for opening the issue.

Seeing how other projects have upgraded Wasmer might help you a bit: swc-project/swc#5456

You can also post here your code and we can help you on migrating to 3.0.

Meanwhile, here are the migration docs: https://github.com/wasmerio/wasmer/blob/master/docs/migration_to_3.0.0.md

@syrusakbary
Copy link
Member

Basically,

You will need to move from

#[derive(wasmer::WasmerEnv, Clone)]
pub struct MyEnv {
    #[wasmer(export)]
    pub memory: wasmer::LazyInit<Memory>,
    #[wasmer(export(name = "__alloc"))]
    pub alloc_guest_memory: LazyInit<NativeFunc<u32, i32>>,
}

let my_env = MyEnv {
  memory: Default::default(),
  alloc_guest_memory: Default::default(),
};

let instance = Instance::new(&module, &imports);

To:

pub struct MyEnv {
    pub memory: Option<Memory>,
    pub alloc_guest_memory: Option<TypedFunction<i32, i32>>,
}

let mut my_env = MyEnv {
  memory: None,
  alloc_guest_memory: None,
};

let instance = Instance::new(&module, &imports);
my_env.memory = instance.exports.get_memory("memory");
my_env.alloc_guest_memory = instance.exports.get_typed_function("__alloc");

@poria-cat
Copy link
Author

This is so great, just what I was looking for. Many thanks @syrusakbary !

@poria-cat
Copy link
Author

Basically,

You will need to move from

#[derive(wasmer::WasmerEnv, Clone)]
pub struct MyEnv {
    #[wasmer(export)]
    pub memory: wasmer::LazyInit<Memory>,
    #[wasmer(export(name = "__alloc"))]
    pub alloc_guest_memory: LazyInit<NativeFunc<u32, i32>>,
}

let my_env = MyEnv {
  memory: Default::default(),
  alloc_guest_memory: Default::default(),
};

let instance = Instance::new(&module, &imports);

To:

pub struct MyEnv {
    pub memory: Option<Memory>,
    pub alloc_guest_memory: Option<TypedFunction<i32, i32>>,
}

let mut my_env = MyEnv {
  memory: None,
  alloc_guest_memory: None,
};

let instance = Instance::new(&module, &imports);
my_env.memory = instance.exports.get_memory("memory");
my_env.alloc_guest_memory = instance.exports.get_typed_function("__alloc");

WoW get it , this is even clearer. Thanks again 🙏🏻

I think this issue can be closed in a few days, so if anyone has a similar problem it will be easy to find a way @syrusakbary .

@syrusakbary
Copy link
Member

I'll add my suggested code on the migration guide so it's easier to find!

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