wasmerio / wasmer-rust-example

Example of WebAssembly embedding in Rust using Wasmer
https://wasmerio.github.io/wasmer/rust/wasmer_runtime/
161 stars 24 forks source link

ABI generation using wasmer #7

Open AchalaSB opened 5 years ago

AchalaSB commented 5 years ago

How can we generate ABI using wasmer for rust lang code? Is it possible now? If yes what are the steps to generate ABI?

This blog https://medium.com/wasmer/webassembly-cloudabi-b573047fd0a9 shows that wasmer is going to support CloudABI. Is it done?

Can wasmer support any rust code or is there any simantics to write code in wasmer environment?

And can we use wasmer ( the ABI generated from wasmer) in blockchain?

When I run command wasmer run wasm-sample-app/target/wasm32-unknown-unknown/release/wasm_sample_app.wasm
i'm getting "Can\'t instantiate module: LinkError([ImportNotFound { namespace: \"env\", name: \"print_str\" }])" what is is issue? how to fix it?

xmclark commented 5 years ago

@AchalaSB thanks for reporting an issue! Regarding the error you're seeing when running: wasmer run wasm-sample-app/target/wasm32-unknown-unknown/release/wasm_sample_app.wasm

This project is a little different because we embed the wasmer runtime in a rust project. This is similar to the wasmer command line tool, except we do a few extra things that wasmer command line tool does not do.

The wasm code that you run depends on a set of imports as explained in that error message:

"Can\'t instantiate module: LinkError([ImportNotFound { namespace: \"env\", name: \"print_str\" }])"

This means that the WebAssembly is looking for an imported function called "print_str".

These imports are like what we are calling an "ABI". The imports are simply a set of functions made available to the wasm code. This allows wasm to call those imported functions e.g. calling the print_str function.

This code can only work if wasmer runtime imports those functions. The wasmer command line program does not know about the print_str function, so we need to manually import it ourselves. That is why this project exists. It shows how one can create an imports object, and load it into the runtime, then execute the wasm code.

This git repository contains two crates: the binary crate that loads webassembly with imports, and the a crate that builds webassembly file. Both are required.

Can wasmer support any rust code or is there any simantics to write code in wasmer environment?

Wasmer can support any rust code that the ABIs it supports allows. The wasm32-unknown-unknown target produces wasm with No ABI. This means it imports no additional functions from the environment. This means one can't build projects that do things like talk to the file system or make network calls.

If one builds for the other ABIs like Emscripten or WASI, they will get more capabilities like use the file system or talk to the internet.

And can we use wasmer ( the ABI generated from wasmer) in blockchain?

Absolutely!

AchalaSB commented 5 years ago

@xmclark Thanks for reverting. So here you are saying cargo run is enough which will take care of wasmer since it is embedded.

These imports are like what we are calling an "ABI". The imports are simply a set of functions made available to the wasm code. This allows wasm to call those imported functions e.g. calling the print_str function.

My question is can we get a seperate ABI files (usually in .json format) so that we can use it for transaction. ( reffereing to ethereum where bytecode and ABI are required for transaction)

Since you said we can use wasmer in blockchain. Need to know how can we use ? How the transaction happens?

xmclark commented 5 years ago

So here you are saying cargo run is enough which will take care of wasmer since it is embedded.

This repository has a cargo project which instantiates the wasmer runtime with those specific imports. If you want to import more functions (a different ABI), then you will need to pass more imports to the runtime. You can see how we create the imports in this bit of rust code: https://github.com/wasmerio/wasmer-rust-example/blob/master/src/main.rs#L28

The imports! macro can handle as many imports as you want! There is practically no limit to the number of imports you can import to a wasm instance. As it is today: the imports must be statically known. This is an important idea that makes it slightly more difficult to do dynamic importing.

My question is can we get a seperate ABI files (usually in .json format) so that we can use it for transaction. ( reffereing to ethereum where bytecode and ABI are required for transaction)

That is a cool feature request. Wasmer runtime does not use JSON to share and create imports.

Are you interested in using custom ABIs to share native code (e.g. Rust or C++ code) with wasmer runtime? If you know that you will always import the same ABI, then you may not need JSON to help you do that. If you do have dynamic requirements, then you will need to build a dynamic import system on top of wasmer. This is the point I was alluding to earlier.

The wasmer project does not support this kind of dynamic importing behavior yet. A few people in the community have explored how to make a plugin system with Wasmer.

Since you said we can use wasmer in blockchain. Need to know how can we use ? How the transaction happens?

There is no legal issue using wasmer in a blockchain application. Wasmer has an open source license. Wasmer is being used in other blockchain applications today. You may fork this repository and use it as starting point for your block chain application, if your block chain app is written in Rust. The implementation will be very similar.

Wasmer also supports interoping with other languages like C/C++, PHP, Python, and recently Ruby.

I recommend you report an issue on the wasmer repository with more specific questions about blockchain related features. A feature you may be interested in asking about is deterministic metering.