mosn / layotto

A fast and efficient cloud native application runtime
http://mosn.io/layotto/
Apache License 2.0
822 stars 172 forks source link

Integrate with wit-bindgen; 集成wit-bindgen #843

Open zhenjunMa opened 1 year ago

zhenjunMa commented 1 year ago

1. Background

The wit-bindgen project officially provided by WebAssembly can enable better interaction between the wasm module and the runtime, avoiding the development of various data conversion logics, and now Layotto integrates wasmtime, so we want to integrate wit-bindgen to improve the user's ability to develop wasm modules experience.

2. What I have tried

  1. define a wit file:
    
    record person {
    name: string,
    age: u32,
    }

test: func(who: person) -> string

2. develop user module

use hello::*;

wit_bindgen_rust::import!("wit/hello.wit");

fn main() { let p = Person{name:"gj", age: 18}; let result = test(p); print!("result: {}", result); }

3. add implementation in wasmtime

use hello::*;

wit_bindgen_rust::export!("/Users/gujin/workspace/rust/hello-wasmtime/wit/hello.wit");

struct Hello {}

impl hello::Hello for Hello { fn test(p: Person) -> String { return p.name; } }

//register to wasmtime

In this way, the test function can be implemented, but how to register this implementation with wasmtime and provide services to users has not yet been found.

# 3. what need to do

1. Learn how to integrate wit-bindgen with wasmtime
2. Learn how to integrate wit-bindgen with wasmtime-go

References:
1. https://github.com/bytecodealliance/wit-bindgen
5. https://github.com/fermyon/spin
6. discussion:#611

### 中文

# 一、背景

WebAssembly 官方提供的 wit-bindgen 项目可以让 wasm 模块跟运行时之间更好的交互,避免开发各种数据转换逻辑,现在Layotto集成了wasmtime,因此想要集成wit-bindgen来提高用户开发wasm模块的体验。

# 二、做过的一些尝试
1. 自定义一个wit文件:

record person { name: string, age: u32, }

test: func(who: person) -> string

2. 用户开发时使用

use hello::*;

wit_bindgen_rust::import!("wit/hello.wit");

fn main() { let p = Person{name:"gj", age: 18}; let result = test(p); print!("result: {}", result); }

3. 运行时提供具体实现

use hello::*;

wit_bindgen_rust::export!("/Users/gujin/workspace/rust/hello-wasmtime/wit/hello.wit");

struct Hello {}

impl hello::Hello for Hello { fn test(p: Person) -> String { return p.name; } }

//把实现注册到wasmtime


这样可以实现test接口,但是如何把该实现注册到wasmtime,对用户提供服务还没有找到办法。

# 三、需要做什么

1. 调研用wasmtime集成wit-bindgen的方法
2. 调研用wasmtime-go集成wit-bindgen的方法

参考资料:
1. https://github.com/bytecodealliance/wit-bindgen
5. https://github.com/fermyon/spin
6. 之前的讨论:#611
zu1k commented 1 year ago

https://radu-matei.com/blog/wasm-components-host-implementations/