├── go.mod ├── go.sum ├── bindgen └── rust │ ├── wasm │ ├── Cargo.lock │ ├── README.md │ ├── Cargo.toml │ └── src │ │ └── lib.rs │ └── macro │ ├── Cargo.toml │ ├── Cargo.lock │ ├── README.md │ └── src │ └── lib.rs ├── test └── BindgenFuncs │ ├── host │ ├── go │ │ ├── go.sum │ │ ├── go.mod │ │ └── bindgen_funcs.go │ ├── rust │ │ ├── Cargo.toml │ │ ├── src │ │ │ └── main.rs │ │ └── Cargo.lock │ └── rust-sdk-test │ │ ├── Cargo.toml │ │ ├── src │ │ └── main.rs │ │ └── Cargo.lock │ └── wasm │ └── rust_bindgen_funcs │ ├── Cargo.toml │ ├── src │ └── lib.rs │ └── Cargo.lock ├── .gitignore ├── host ├── rust │ ├── Cargo.toml │ └── src │ │ └── lib.rs ├── rust-sdk │ ├── Cargo.toml │ ├── Cargo.lock │ └── src │ │ └── lib.rs └── go │ ├── README.md │ └── bindgen.go ├── README.md └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/second-state/wasmedge-bindgen 2 | 3 | go 1.17 4 | 5 | require github.com/second-state/WasmEdge-go v0.11.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/second-state/WasmEdge-go v0.11.0 h1:AJ9N2vzXTKQeut5LPxxUDaAKz/VpNk+OXkGSbLvJRkE= 2 | github.com/second-state/WasmEdge-go v0.11.0/go.mod h1:HyBf9hVj1sRAjklsjc1Yvs9b5RcmthPG9z99dY78TKg= 3 | -------------------------------------------------------------------------------- /bindgen/rust/wasm/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "wasmedge-bindgen" 7 | version = "0.4.0" 8 | -------------------------------------------------------------------------------- /test/BindgenFuncs/host/go/go.sum: -------------------------------------------------------------------------------- 1 | github.com/second-state/WasmEdge-go v0.11.0 h1:AJ9N2vzXTKQeut5LPxxUDaAKz/VpNk+OXkGSbLvJRkE= 2 | github.com/second-state/WasmEdge-go v0.11.0/go.mod h1:HyBf9hVj1sRAjklsjc1Yvs9b5RcmthPG9z99dY78TKg= 3 | -------------------------------------------------------------------------------- /bindgen/rust/wasm/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This crate contains some host functions for wasmedge-bindgen to use. 4 | You should just import it and don't need to care about any other things. 5 | 6 | ```rust 7 | use wasmedge_bindgen::*; 8 | ``` -------------------------------------------------------------------------------- /test/BindgenFuncs/host/go/go.mod: -------------------------------------------------------------------------------- 1 | module bindgen_funcs 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/second-state/WasmEdge-go v0.11.0 7 | github.com/second-state/wasmedge-bindgen v0.3.0 8 | ) 9 | 10 | replace github.com/second-state/wasmedge-bindgen => ../../../../ 11 | -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bindgen-funcs" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | wasmedge-sys = "0.7.0" 10 | wasmedge-bindgen-host = { path = "../../../../host/rust" } -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust-sdk-test/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bindgen-funcs" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | wasmedge-sdk = "0.4.0" 10 | wasmedge-sdk-bindgen = { path = "../../../../host/rust-sdk" } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | vendor/ 16 | 17 | target/ 18 | 19 | .DS_Store 20 | 21 | node_modules 22 | .vscode 23 | Cargo.lock 24 | -------------------------------------------------------------------------------- /bindgen/rust/wasm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasmedge-bindgen" 3 | version = "0.4.1" 4 | edition = "2021" 5 | description = "Pass string, vec and primitive types from host to webassembly" 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/second-state/wasmedge-bindgen" 8 | 9 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 10 | 11 | [lib] 12 | crate-type = ["cdylib", "rlib"] 13 | 14 | [dependencies] 15 | -------------------------------------------------------------------------------- /host/rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasmedge-bindgen-host" 3 | version = "0.4.1" 4 | edition = "2021" 5 | description = "Pass string, vec and primitive types from host to webassembly" 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/second-state/wasmedge-bindgen" 8 | 9 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 10 | 11 | [dependencies] 12 | num-traits = "0.2" 13 | num-derive = "0.3" 14 | wasmedge-sys = "0.9.0" 15 | wasmedge-types = "0.2.1" 16 | 17 | -------------------------------------------------------------------------------- /test/BindgenFuncs/wasm/rust_bindgen_funcs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust_bindgen_funcs" 3 | version = "0.1.0" 4 | authors = ["ubuntu"] 5 | edition = "2018" 6 | 7 | [lib] 8 | name = "rust_bindgen_funcs_lib" 9 | path = "src/lib.rs" 10 | crate-type =["cdylib"] 11 | 12 | [dependencies] 13 | num-integer = "0.1" 14 | sha3 = "0.8.2" 15 | serde = { version = "1.0", features = ["derive"] } 16 | serde_json = "1.0" 17 | wasmedge-bindgen = { path = "../../../../bindgen/rust/wasm" } 18 | wasmedge-bindgen-macro = { path = "../../../../bindgen/rust/macro" } -------------------------------------------------------------------------------- /host/rust-sdk/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasmedge-sdk-bindgen" 3 | version = "0.4.0" 4 | edition = "2021" 5 | description = "Pass string, vec and primitive types from host to webassembly, with WasmEdge Rust SDK" 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/second-state/wasmedge-bindgen-sdk" 8 | 9 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 10 | 11 | [dependencies] 12 | num-traits = "0.2" 13 | num-derive = "0.3" 14 | wasmedge-sdk = "0.4.0" 15 | wasmedge-types = "0.2.0" 16 | -------------------------------------------------------------------------------- /bindgen/rust/wasm/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::mem; 2 | 3 | /// We hand over the the pointer to the allocated memory. 4 | /// Caller has to ensure that the memory gets freed again. 5 | #[no_mangle] 6 | pub unsafe extern fn allocate(size: i32) -> *const u8 { 7 | let buffer = Vec::with_capacity(size as usize); 8 | 9 | let buffer = mem::ManuallyDrop::new(buffer); 10 | buffer.as_ptr() as *const u8 11 | } 12 | 13 | #[no_mangle] 14 | pub unsafe extern fn deallocate(pointer: *mut u8, size: i32) { 15 | drop(Vec::from_raw_parts(pointer, size as usize, size as usize)); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /bindgen/rust/macro/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasmedge-bindgen-macro" 3 | version = "0.4.1" 4 | edition = "2021" 5 | description = "Pass string, vec and primitive types from host to webassembly" 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/second-state/wasmedge-bindgen" 8 | 9 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 10 | 11 | [lib] 12 | proc-macro = true 13 | 14 | [dependencies] 15 | quote = "1.0" 16 | syn = { version = "1.0", features = ["full", "extra-traits"] } 17 | proc-macro2 = "1.0" 18 | -------------------------------------------------------------------------------- /bindgen/rust/macro/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "proc-macro2" 7 | version = "1.0.43" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" 10 | dependencies = [ 11 | "unicode-ident", 12 | ] 13 | 14 | [[package]] 15 | name = "quote" 16 | version = "1.0.21" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" 19 | dependencies = [ 20 | "proc-macro2", 21 | ] 22 | 23 | [[package]] 24 | name = "syn" 25 | version = "1.0.99" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" 28 | dependencies = [ 29 | "proc-macro2", 30 | "quote", 31 | "unicode-ident", 32 | ] 33 | 34 | [[package]] 35 | name = "unicode-ident" 36 | version = "1.0.3" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" 39 | 40 | [[package]] 41 | name = "wasmedge-bindgen-macro" 42 | version = "0.4.0" 43 | dependencies = [ 44 | "proc-macro2", 45 | "quote", 46 | "syn", 47 | ] 48 | -------------------------------------------------------------------------------- /bindgen/rust/macro/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This crate only export a macro named #[wasmedge_bindgen] that used for 4 | retouching exporting functions to make it support more data types. 5 | 6 | ## Data Types 7 | 8 | ### Parameters 9 | 10 | You can set the parameters to any one of the following types: 11 | 12 | * Scalar Types: i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, bool, char 13 | * String 14 | * Vec: Vec<i8>, Vec<u8>, Vec<i16>, Vec<u16>, Vec<i32>, Vec<u32>, Vec<i64>, Vec<u64> 15 | 16 | ### Return Values 17 | 18 | You can set the return values to any one of the following types: 19 | 20 | * Scalar Types: i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, bool, char 21 | * String 22 | * Vec: Vec<i8>, Vec<u8>, Vec<i16>, Vec<u16>, Vec<i32>, Vec<u32>, Vec<i64>, Vec<u64> 23 | * Tuple Type: compounded by any number of the above three types 24 | * Result: Ok<any one of the above four types>, Err<String> 25 | 26 | The only way to tell the host that the error has occurred is to return Err<String> of Result. 27 | 28 | ## Examples 29 | 30 | ```rust 31 | #[wasmedge_bindgen] 32 | pub fn create_line(p1: String, p2: String, desc: String) -> String 33 | 34 | #[wasmedge_bindgen] 35 | pub fn lowest_common_multiple(a: i32, b: i32) -> i32 36 | 37 | #[wasmedge_bindgen] 38 | pub fn sha3_digest(v: Vec) -> Vec 39 | 40 | #[wasmedge_bindgen] 41 | pub fn info(v: Vec) -> Result<(u8, String), String> 42 | ``` -------------------------------------------------------------------------------- /host/go/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This is a tiny library lets you call the WebAssembly exported functions that are tweaked by wasmedge-bindgen. 4 | 5 | ## Guide 6 | 7 | Originally, you can call the wasm function from [WasmEdge-go](https://github.com/second-state/WasmEdge-go) like this: 8 | 9 | ```go 10 | import ( 11 | "github.com/second-state/WasmEdge-go/wasmedge" 12 | ) 13 | 14 | func main() { 15 | vm := wasmedge.NewVM() 16 | vm.LoadWasmFile("say.wasm") 17 | vm.Validate() 18 | vm.Instantiate() 19 | vm.Execute("say") 20 | } 21 | ``` 22 | 23 | When your wasm function is retouched by #[wasmedge_bindgen]: 24 | ```rust 25 | #[wasmedge_bindgen] 26 | pub fn say(s: String) -> String { 27 | let r = String::from("hello "); 28 | return r + s.as_str(); 29 | } 30 | ``` 31 | Then you should call it using this library: 32 | ```go 33 | import ( 34 | "github.com/second-state/WasmEdge-go/wasmedge" 35 | bindgen "github.com/second-state/wasmedge-bindgen/host/go" 36 | ) 37 | 38 | func main() { 39 | vm := wasmedge.NewVM() 40 | vm.LoadWasmFile("say.wasm") 41 | vm.Validate() 42 | 43 | // Instantiate the bindgen and vm 44 | bg := bindgen.Instantiate(vm) 45 | 46 | /// say: string -> string 47 | result, err := bg.Execute("say", "wasmedge-bindgen") 48 | if err == nil { 49 | fmt.Println("Run bindgen -- say:", result[0].(string)) 50 | } 51 | } 52 | ``` 53 | 54 | ## Explain 55 | 56 | The result is an interface array ([]interface{}). 57 | Normally, the length of the array is 1. 58 | When the return value of wasm(rust) is (Tuple,) or Result<(Tuple,), String>, 59 | the array length will be the same with the members' count of tuple. 60 | 61 | If the function returns Err(String), you will get it by err. -------------------------------------------------------------------------------- /test/BindgenFuncs/wasm/rust_bindgen_funcs/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[allow(unused_imports)] 2 | use wasmedge_bindgen::*; 3 | use wasmedge_bindgen_macro::*; 4 | use num_integer::lcm; 5 | use sha3::{Digest, Sha3_256, Keccak256}; 6 | use serde::{Serialize, Deserialize}; 7 | 8 | #[derive(Serialize, Deserialize, Debug)] 9 | struct Point { 10 | x: f32, 11 | y: f32 12 | } 13 | 14 | #[derive(Serialize, Deserialize, Debug)] 15 | struct Line { 16 | points: Vec, 17 | valid: bool, 18 | length: f32, 19 | desc: String 20 | } 21 | 22 | #[wasmedge_bindgen] 23 | pub fn create_line(p1: String, p2: String, desc: String) -> String { 24 | let point1: Point = serde_json::from_str(p1.as_str()).unwrap(); 25 | let point2: Point = serde_json::from_str(p2.as_str()).unwrap(); 26 | let length = ((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y)).sqrt(); 27 | 28 | let valid = if length == 0.0 { false } else { true }; 29 | 30 | let line = Line { points: vec![point1, point2], valid: valid, length: length, desc: desc }; 31 | 32 | return serde_json::to_string(&line).unwrap(); 33 | } 34 | 35 | #[wasmedge_bindgen] 36 | pub fn say(s: String) -> Result<(u16, String), String> { 37 | let r = String::from("hello "); 38 | return Ok((5 as u16, r + s.as_str())); 39 | // return Err(String::from("abc")); 40 | } 41 | 42 | #[wasmedge_bindgen] 43 | pub fn obfusticate(s: String) -> String { 44 | (&s).chars().map(|c| { 45 | match c { 46 | 'A' ..= 'M' | 'a' ..= 'm' => ((c as u8) + 13) as char, 47 | 'N' ..= 'Z' | 'n' ..= 'z' => ((c as u8) - 13) as char, 48 | _ => c 49 | } 50 | }).collect() 51 | } 52 | 53 | #[wasmedge_bindgen] 54 | pub fn lowest_common_multiple(a: i32, b: i32) -> i32 { 55 | return lcm(a, b); 56 | } 57 | 58 | #[wasmedge_bindgen] 59 | pub fn sha3_digest(v: Vec) -> Vec { 60 | return Sha3_256::digest(&v).as_slice().to_vec(); 61 | } 62 | 63 | #[wasmedge_bindgen] 64 | pub fn keccak_digest(s: Vec) -> Vec { 65 | return Keccak256::digest(&s).as_slice().to_vec(); 66 | } -------------------------------------------------------------------------------- /test/BindgenFuncs/host/go/bindgen_funcs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/second-state/WasmEdge-go/wasmedge" 8 | bindgen "github.com/second-state/wasmedge-bindgen/host/go" 9 | ) 10 | 11 | func main() { 12 | /// Expected Args[0]: program name (./bindgen_funcs) 13 | /// Expected Args[1]: wasm file (rust_bindgen_funcs_lib.wasm)) 14 | 15 | /// Set not to print debug info 16 | wasmedge.SetLogErrorLevel() 17 | 18 | /// Create configure 19 | var conf = wasmedge.NewConfigure(wasmedge.WASI) 20 | 21 | /// Create VM with configure 22 | var vm = wasmedge.NewVMWithConfig(conf) 23 | 24 | /// Init WASI 25 | var wasi = vm.GetImportModule(wasmedge.WASI) 26 | wasi.InitWasi( 27 | os.Args[1:], /// The args 28 | os.Environ(), /// The envs 29 | []string{".:."}, /// The mapping preopens 30 | ) 31 | 32 | /// Load and validate the wasm 33 | vm.LoadWasmFile(os.Args[1]) 34 | vm.Validate() 35 | 36 | // Instantiate the bindgen and vm 37 | bg := bindgen.New(vm) 38 | bg.Instantiate() 39 | 40 | /// create_line: string, string, string -> string (inputs are JSON stringified) 41 | res, _, err := bg.Execute("create_line", "{\"x\":2.5,\"y\":7.8}", "{\"x\":2.5,\"y\":5.8}", "A thin red line") 42 | if err == nil { 43 | fmt.Println("Run bindgen -- create_line:", res[0].(string)) 44 | } else { 45 | fmt.Println("Run bindgen -- create_line FAILED", err) 46 | } 47 | 48 | /// say: string -> string 49 | res, e, err := bg.Execute("say", "bindgen funcs test") 50 | if err == nil { 51 | if e != nil { 52 | fmt.Println("Error -- say:", e) 53 | } else { 54 | fmt.Println("Run bindgen -- say:", res[1].(string), res[0].(uint16)) 55 | } 56 | } else { 57 | fmt.Println("Run bindgen -- say FAILED") 58 | } 59 | 60 | /// obfusticate: string -> string 61 | res, _, err = bg.Execute("obfusticate", "A quick brown fox jumps over the lazy dog") 62 | if err == nil { 63 | fmt.Println("Run bindgen -- obfusticate:", res[0].(string)) 64 | } else { 65 | fmt.Println("Run bindgen -- obfusticate FAILED") 66 | } 67 | 68 | /// lowest_common_multiple: i32, i32 -> i32 69 | res, _, err = bg.Execute("lowest_common_multiple", int32(123), int32(2)) 70 | if err == nil { 71 | fmt.Println("Run bindgen -- lowest_common_multiple:", res[0].(int32)) 72 | } else { 73 | fmt.Println("Run bindgen -- lowest_common_multiple FAILED") 74 | } 75 | 76 | /// sha3_digest: array -> array 77 | res, _, err = bg.Execute("sha3_digest", []byte("This is an important message")) 78 | if err == nil { 79 | fmt.Println("Run bindgen -- sha3_digest:", res[0].([]byte)) 80 | } else { 81 | fmt.Println("Run bindgen -- sha3_digest FAILED") 82 | } 83 | 84 | /// keccak_digest: array -> array 85 | res, _, err = bg.Execute("keccak_digest", []byte("This is an important message")) 86 | if err == nil { 87 | fmt.Println("Run bindgen -- keccak_digest:", res[0].([]byte)) 88 | } else { 89 | fmt.Println("Run bindgen -- keccak_digest FAILED") 90 | } 91 | 92 | bg.Release() 93 | conf.Release() 94 | } 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | Let WebAssembly's exported function support more data types for its parameters and return values. 4 | 5 | ## Example 6 | 7 | 8 | ## Export Rust things to host program 9 | 10 | See demo program in [rust_bindgen_func](/test/BindgenFuncs/wasm/rust_bindgen_funcs/). 11 | 12 | ```rust 13 | use wasmedge_bindgen::*; 14 | use wasmedge_bindgen_macro::*; 15 | 16 | // Export a `say` function from Rust, that returns a hello message. 17 | #[wasmedge_bindgen] 18 | pub fn say(s: String) -> String { 19 | let r = String::from("hello "); 20 | return r + s.as_str(); 21 | } 22 | ``` 23 | 24 | Run with command will compile the above code to a WASM bytes file. 25 | 26 | ```bash 27 | cargo build --target wasm32-wasi --release 28 | ``` 29 | 30 | ### Rust SDK 31 | Use exported functions in [Rust SDK](https://github.com/WasmEdge/WasmEdge/tree/master/bindings/rust/wasmedge-sdk). 32 | 33 | Refer to the completed exmaple in [rust-sdk-test](/test/BindgenFuncs/host/rust-sdk-test/src/main.rs) 34 | 35 | ```rust 36 | 37 | fn main() { 38 | .... 39 | 40 | let vm = Vm::new(Some(config)).unwrap(); 41 | let args: Vec = env::args().collect(); 42 | let wasm_path = Path::new(&args[1]); 43 | let module = Module::from_file(None, wasm_path).unwrap(); 44 | let vm = vm.register_module(None, module).unwrap(); 45 | let mut bg = Bindgen::new(vm); 46 | 47 | // create_line: string, string, string -> string (inputs are JSON stringified) 48 | let params = vec![ 49 | Param::String("{\"x\":2.5,\"y\":7.8}"), 50 | Param::String("{\"x\":2.5,\"y\":5.8}"), 51 | Param::String("A thin red line"), 52 | ]; 53 | match bg.run_wasm("create_line", params) { 54 | Ok(rv) => { 55 | println!( 56 | "Run bindgen -- create_line: {:?}", 57 | rv.unwrap().pop().unwrap().downcast::().unwrap() 58 | ); 59 | } 60 | Err(e) => { 61 | println!("Run bindgen -- create_line FAILED {:?}", e); 62 | } 63 | } 64 | 65 | ... 66 | } 67 | 68 | ``` 69 | 70 | ### Go SDK 71 | Use exported Rust things from [WasmEdge-go](https://github.com/second-state/WasmEdge-go)! 72 | 73 | Refer to the completed example in [bindgen_funcs.go](./test/BindgenFuncs/host/go/bindgen_funcs.go) 74 | 75 | ```go 76 | import ( 77 | "github.com/second-state/WasmEdge-go/wasmedge" 78 | bindgen "github.com/second-state/wasmedge-bindgen/host/go" 79 | ) 80 | 81 | func main() { 82 | . 83 | . 84 | . 85 | 86 | // Instantiate the bindgen and vm 87 | bg := bindgen.Instantiate(vm) 88 | 89 | /// say: string -> string 90 | res, _ := bg.Execute("say", "wasmedge-bindgen") 91 | fmt.Println("Run bindgen -- say:", res[0].(string)) 92 | } 93 | ``` 94 | 95 | ## Guide 96 | 97 | [**Find all the data types you can use!**](bindgen/rust/macro) 98 | 99 | You can find how to call the exported function from go host [here](host/go). 100 | 101 | -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust/src/main.rs: -------------------------------------------------------------------------------- 1 | use wasmedge_sys::*; 2 | use std::env; 3 | use std::path::Path; 4 | use wasmedge_bindgen_host::*; 5 | 6 | fn main() { 7 | let mut config = Config::create().unwrap(); 8 | config.wasi(true); 9 | 10 | let mut vm = Vm::create(Some(config), None).unwrap(); 11 | 12 | // get default wasi module 13 | let mut wasi_module = vm.wasi_module_mut().unwrap(); 14 | // init the default wasi module 15 | wasi_module.init_wasi( 16 | Some(vec![]), 17 | Some(vec![]), 18 | Some(vec![]), 19 | ); 20 | 21 | let args: Vec = env::args().collect(); 22 | let wasm_path = Path::new(&args[1]); 23 | let _ = vm.load_wasm_from_file(wasm_path); 24 | let _ = vm.validate(); 25 | 26 | let mut bg = Bindgen::new(vm); 27 | bg.instantiate(); 28 | 29 | // create_line: string, string, string -> string (inputs are JSON stringified) 30 | let params = vec![Param::String("{\"x\":2.5,\"y\":7.8}"), Param::String("{\"x\":2.5,\"y\":5.8}"), Param::String("A thin red line")]; 31 | match bg.run_wasm("create_line", params) { 32 | Ok(rv) => { 33 | println!("Run bindgen -- create_line: {:?}", rv.unwrap().pop().unwrap().downcast::().unwrap()); 34 | } 35 | Err(e) => { 36 | println!("Run bindgen -- create_line FAILED {:?}", e); 37 | } 38 | } 39 | 40 | let params = vec![Param::String("bindgen funcs test")]; 41 | match bg.run_wasm("say", params) { 42 | Ok(rv) => { 43 | match rv { 44 | Ok(mut x) => println!("Run bindgen -- say: {:?} {}", x.pop().unwrap().downcast::().unwrap(), x.pop().unwrap().downcast::().unwrap()), 45 | Err(e) => println!("Err -- say: {}", e), 46 | } 47 | } 48 | Err(e) => { 49 | println!("Run bindgen -- say FAILED {:?}", e); 50 | } 51 | } 52 | 53 | let params = vec![Param::String("A quick brown fox jumps over the lazy dog")]; 54 | match bg.run_wasm("obfusticate", params) { 55 | Ok(rv) => { 56 | println!("Run bindgen -- obfusticate: {:?}", rv.unwrap().pop().unwrap().downcast::().unwrap()); 57 | } 58 | Err(e) => { 59 | println!("Run bindgen -- obfusticate FAILED {:?}", e); 60 | } 61 | } 62 | 63 | let params = vec![Param::I32(123), Param::I32(2)]; 64 | match bg.run_wasm("lowest_common_multiple", params) { 65 | Ok(rv) => { 66 | println!("Run bindgen -- lowest_common_multiple: {:?}", rv.unwrap().pop().unwrap().downcast::().unwrap()); 67 | } 68 | Err(e) => { 69 | println!("Run bindgen -- lowest_common_multiple FAILED {:?}", e); 70 | } 71 | } 72 | 73 | let params = "This is an important message".as_bytes().to_vec(); 74 | let params = vec![Param::VecU8(¶ms)]; 75 | match bg.run_wasm("sha3_digest", params) { 76 | Ok(rv) => { 77 | println!("Run bindgen -- sha3_digest: {:?}", rv.unwrap().pop().unwrap().downcast::>().unwrap()); 78 | } 79 | Err(e) => { 80 | println!("Run bindgen -- sha3_digest FAILED {:?}", e); 81 | } 82 | } 83 | 84 | let params = "This is an important message".as_bytes().to_vec(); 85 | let params = vec![Param::VecU8(¶ms)]; 86 | match bg.run_wasm("keccak_digest", params) { 87 | Ok(rv) => { 88 | println!("Run bindgen -- keccak_digest: {:?}", rv.unwrap().pop().unwrap().downcast::>().unwrap()); 89 | } 90 | Err(e) => { 91 | println!("Run bindgen -- keccak_digest FAILED {:?}", e); 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust-sdk-test/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::Path; 3 | use wasmedge_sdk::config::*; 4 | use wasmedge_sdk::*; 5 | use wasmedge_sdk_bindgen::*; 6 | 7 | fn main() { 8 | let common_options = CommonConfigOptions::default() 9 | .bulk_memory_operations(true) 10 | .multi_value(true) 11 | .mutable_globals(true) 12 | .non_trap_conversions(true) 13 | .reference_types(true) 14 | .sign_extension_operators(true) 15 | .simd(true); 16 | 17 | let host_options = HostRegistrationConfigOptions::default() 18 | .wasi(true) 19 | .wasmedge_process(false); 20 | 21 | let config = ConfigBuilder::new(common_options) 22 | .with_host_registration_config(host_options) 23 | .build() 24 | .unwrap(); 25 | 26 | let vm = Vm::new(Some(config)).unwrap(); 27 | let args: Vec = env::args().collect(); 28 | let wasm_path = Path::new(&args[1]); 29 | let module = Module::from_file(None, wasm_path).unwrap(); 30 | let vm = vm.register_module(None, module).unwrap(); 31 | let mut bg = Bindgen::new(vm); 32 | 33 | // create_line: string, string, string -> string (inputs are JSON stringified) 34 | let params = vec![ 35 | Param::String("{\"x\":2.5,\"y\":7.8}"), 36 | Param::String("{\"x\":2.5,\"y\":5.8}"), 37 | Param::String("A thin red line"), 38 | ]; 39 | match bg.run_wasm("create_line", params) { 40 | Ok(rv) => { 41 | println!( 42 | "Run bindgen -- create_line: {:?}", 43 | rv.unwrap().pop().unwrap().downcast::().unwrap() 44 | ); 45 | } 46 | Err(e) => { 47 | println!("Run bindgen -- create_line FAILED {:?}", e); 48 | } 49 | } 50 | 51 | let params = vec![Param::String("bindgen funcs test")]; 52 | match bg.run_wasm("say", params) { 53 | Ok(rv) => match rv { 54 | Ok(mut x) => println!( 55 | "Run bindgen -- say: {:?} {}", 56 | x.pop().unwrap().downcast::().unwrap(), 57 | x.pop().unwrap().downcast::().unwrap() 58 | ), 59 | Err(e) => println!("Err -- say: {}", e), 60 | }, 61 | Err(e) => { 62 | println!("Run bindgen -- say FAILED {:?}", e); 63 | } 64 | } 65 | 66 | let params = vec![Param::String("A quick brown fox jumps over the lazy dog")]; 67 | match bg.run_wasm("obfusticate", params) { 68 | Ok(rv) => { 69 | println!( 70 | "Run bindgen -- obfusticate: {:?}", 71 | rv.unwrap().pop().unwrap().downcast::().unwrap() 72 | ); 73 | } 74 | Err(e) => { 75 | println!("Run bindgen -- obfusticate FAILED {:?}", e); 76 | } 77 | } 78 | 79 | let params = vec![Param::I32(123), Param::I32(2)]; 80 | match bg.run_wasm("lowest_common_multiple", params) { 81 | Ok(rv) => { 82 | println!( 83 | "Run bindgen -- lowest_common_multiple: {:?}", 84 | rv.unwrap().pop().unwrap().downcast::().unwrap() 85 | ); 86 | } 87 | Err(e) => { 88 | println!("Run bindgen -- lowest_common_multiple FAILED {:?}", e); 89 | } 90 | } 91 | 92 | let params = "This is an important message".as_bytes().to_vec(); 93 | let params = vec![Param::VecU8(¶ms)]; 94 | match bg.run_wasm("sha3_digest", params) { 95 | Ok(rv) => { 96 | println!( 97 | "Run bindgen -- sha3_digest: {:?}", 98 | rv.unwrap().pop().unwrap().downcast::>().unwrap() 99 | ); 100 | } 101 | Err(e) => { 102 | println!("Run bindgen -- sha3_digest FAILED {:?}", e); 103 | } 104 | } 105 | 106 | let params = "This is an important message".as_bytes().to_vec(); 107 | let params = vec![Param::VecU8(¶ms)]; 108 | match bg.run_wasm("keccak_digest", params) { 109 | Ok(rv) => { 110 | println!( 111 | "Run bindgen -- keccak_digest: {:?}", 112 | rv.unwrap().pop().unwrap().downcast::>().unwrap() 113 | ); 114 | } 115 | Err(e) => { 116 | println!("Run bindgen -- keccak_digest FAILED {:?}", e); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /test/BindgenFuncs/wasm/rust_bindgen_funcs/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.0.1" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 10 | 11 | [[package]] 12 | name = "block-buffer" 13 | version = "0.7.3" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" 16 | dependencies = [ 17 | "block-padding", 18 | "byte-tools", 19 | "byteorder", 20 | "generic-array", 21 | ] 22 | 23 | [[package]] 24 | name = "block-padding" 25 | version = "0.1.5" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" 28 | dependencies = [ 29 | "byte-tools", 30 | ] 31 | 32 | [[package]] 33 | name = "byte-tools" 34 | version = "0.3.1" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" 37 | 38 | [[package]] 39 | name = "byteorder" 40 | version = "1.4.3" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" 43 | 44 | [[package]] 45 | name = "digest" 46 | version = "0.8.1" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" 49 | dependencies = [ 50 | "generic-array", 51 | ] 52 | 53 | [[package]] 54 | name = "generic-array" 55 | version = "0.12.4" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" 58 | dependencies = [ 59 | "typenum", 60 | ] 61 | 62 | [[package]] 63 | name = "itoa" 64 | version = "1.0.1" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" 67 | 68 | [[package]] 69 | name = "keccak" 70 | version = "0.1.0" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" 73 | 74 | [[package]] 75 | name = "num-integer" 76 | version = "0.1.44" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" 79 | dependencies = [ 80 | "autocfg", 81 | "num-traits", 82 | ] 83 | 84 | [[package]] 85 | name = "num-traits" 86 | version = "0.2.14" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 89 | dependencies = [ 90 | "autocfg", 91 | ] 92 | 93 | [[package]] 94 | name = "opaque-debug" 95 | version = "0.2.3" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" 98 | 99 | [[package]] 100 | name = "proc-macro2" 101 | version = "1.0.36" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" 104 | dependencies = [ 105 | "unicode-xid", 106 | ] 107 | 108 | [[package]] 109 | name = "quote" 110 | version = "1.0.14" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" 113 | dependencies = [ 114 | "proc-macro2", 115 | ] 116 | 117 | [[package]] 118 | name = "rust_bindgen_funcs" 119 | version = "0.1.0" 120 | dependencies = [ 121 | "num-integer", 122 | "serde", 123 | "serde_json", 124 | "sha3", 125 | "wasmedge-bindgen", 126 | "wasmedge-bindgen-macro", 127 | ] 128 | 129 | [[package]] 130 | name = "ryu" 131 | version = "1.0.9" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" 134 | 135 | [[package]] 136 | name = "serde" 137 | version = "1.0.133" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a" 140 | dependencies = [ 141 | "serde_derive", 142 | ] 143 | 144 | [[package]] 145 | name = "serde_derive" 146 | version = "1.0.133" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537" 149 | dependencies = [ 150 | "proc-macro2", 151 | "quote", 152 | "syn", 153 | ] 154 | 155 | [[package]] 156 | name = "serde_json" 157 | version = "1.0.74" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | checksum = "ee2bb9cd061c5865d345bb02ca49fcef1391741b672b54a0bf7b679badec3142" 160 | dependencies = [ 161 | "itoa", 162 | "ryu", 163 | "serde", 164 | ] 165 | 166 | [[package]] 167 | name = "sha3" 168 | version = "0.8.2" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "dd26bc0e7a2e3a7c959bc494caf58b72ee0c71d67704e9520f736ca7e4853ecf" 171 | dependencies = [ 172 | "block-buffer", 173 | "byte-tools", 174 | "digest", 175 | "keccak", 176 | "opaque-debug", 177 | ] 178 | 179 | [[package]] 180 | name = "syn" 181 | version = "1.0.85" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7" 184 | dependencies = [ 185 | "proc-macro2", 186 | "quote", 187 | "unicode-xid", 188 | ] 189 | 190 | [[package]] 191 | name = "typenum" 192 | version = "1.15.0" 193 | source = "registry+https://github.com/rust-lang/crates.io-index" 194 | checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" 195 | 196 | [[package]] 197 | name = "unicode-xid" 198 | version = "0.2.2" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 201 | 202 | [[package]] 203 | name = "wasmedge-bindgen" 204 | version = "0.4.0" 205 | 206 | [[package]] 207 | name = "wasmedge-bindgen-macro" 208 | version = "0.4.0" 209 | dependencies = [ 210 | "proc-macro2", 211 | "quote", 212 | "syn", 213 | ] 214 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.1.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 10 | 11 | [[package]] 12 | name = "bindgen" 13 | version = "0.59.2" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8" 16 | dependencies = [ 17 | "bitflags", 18 | "cexpr", 19 | "clang-sys", 20 | "lazy_static", 21 | "lazycell", 22 | "peeking_take_while", 23 | "proc-macro2", 24 | "quote", 25 | "regex", 26 | "rustc-hash", 27 | "shlex", 28 | ] 29 | 30 | [[package]] 31 | name = "bindgen-funcs" 32 | version = "0.1.0" 33 | dependencies = [ 34 | "wasmedge-bindgen-host", 35 | "wasmedge-sys", 36 | ] 37 | 38 | [[package]] 39 | name = "bitflags" 40 | version = "1.3.2" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 43 | 44 | [[package]] 45 | name = "cc" 46 | version = "1.0.73" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" 49 | 50 | [[package]] 51 | name = "cexpr" 52 | version = "0.6.0" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" 55 | dependencies = [ 56 | "nom", 57 | ] 58 | 59 | [[package]] 60 | name = "cfg-if" 61 | version = "1.0.0" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 64 | 65 | [[package]] 66 | name = "clang-sys" 67 | version = "1.3.1" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "4cc00842eed744b858222c4c9faf7243aafc6d33f92f96935263ef4d8a41ce21" 70 | dependencies = [ 71 | "glob", 72 | "libc", 73 | "libloading", 74 | ] 75 | 76 | [[package]] 77 | name = "cmake" 78 | version = "0.1.48" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" 81 | dependencies = [ 82 | "cc", 83 | ] 84 | 85 | [[package]] 86 | name = "getrandom" 87 | version = "0.2.4" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" 90 | dependencies = [ 91 | "cfg-if", 92 | "libc", 93 | "wasi", 94 | ] 95 | 96 | [[package]] 97 | name = "glob" 98 | version = "0.3.0" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" 101 | 102 | [[package]] 103 | name = "lazy_static" 104 | version = "1.4.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 107 | 108 | [[package]] 109 | name = "lazycell" 110 | version = "1.3.0" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" 113 | 114 | [[package]] 115 | name = "leb128" 116 | version = "0.2.5" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" 119 | 120 | [[package]] 121 | name = "libc" 122 | version = "0.2.117" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" 125 | 126 | [[package]] 127 | name = "libloading" 128 | version = "0.7.3" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" 131 | dependencies = [ 132 | "cfg-if", 133 | "winapi", 134 | ] 135 | 136 | [[package]] 137 | name = "memchr" 138 | version = "2.4.1" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 141 | 142 | [[package]] 143 | name = "minimal-lexical" 144 | version = "0.2.1" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" 147 | 148 | [[package]] 149 | name = "nom" 150 | version = "7.1.0" 151 | source = "registry+https://github.com/rust-lang/crates.io-index" 152 | checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" 153 | dependencies = [ 154 | "memchr", 155 | "minimal-lexical", 156 | "version_check", 157 | ] 158 | 159 | [[package]] 160 | name = "num-derive" 161 | version = "0.3.3" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" 164 | dependencies = [ 165 | "proc-macro2", 166 | "quote", 167 | "syn", 168 | ] 169 | 170 | [[package]] 171 | name = "num-traits" 172 | version = "0.2.14" 173 | source = "registry+https://github.com/rust-lang/crates.io-index" 174 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 175 | dependencies = [ 176 | "autocfg", 177 | ] 178 | 179 | [[package]] 180 | name = "paste" 181 | version = "1.0.6" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5" 184 | 185 | [[package]] 186 | name = "peeking_take_while" 187 | version = "0.1.2" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" 190 | 191 | [[package]] 192 | name = "ppv-lite86" 193 | version = "0.2.16" 194 | source = "registry+https://github.com/rust-lang/crates.io-index" 195 | checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" 196 | 197 | [[package]] 198 | name = "proc-macro2" 199 | version = "1.0.36" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" 202 | dependencies = [ 203 | "unicode-xid", 204 | ] 205 | 206 | [[package]] 207 | name = "quote" 208 | version = "1.0.15" 209 | source = "registry+https://github.com/rust-lang/crates.io-index" 210 | checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" 211 | dependencies = [ 212 | "proc-macro2", 213 | ] 214 | 215 | [[package]] 216 | name = "rand" 217 | version = "0.8.5" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 220 | dependencies = [ 221 | "libc", 222 | "rand_chacha", 223 | "rand_core", 224 | ] 225 | 226 | [[package]] 227 | name = "rand_chacha" 228 | version = "0.3.1" 229 | source = "registry+https://github.com/rust-lang/crates.io-index" 230 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 231 | dependencies = [ 232 | "ppv-lite86", 233 | "rand_core", 234 | ] 235 | 236 | [[package]] 237 | name = "rand_core" 238 | version = "0.6.3" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" 241 | dependencies = [ 242 | "getrandom", 243 | ] 244 | 245 | [[package]] 246 | name = "regex" 247 | version = "1.5.4" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 250 | dependencies = [ 251 | "regex-syntax", 252 | ] 253 | 254 | [[package]] 255 | name = "regex-syntax" 256 | version = "0.6.25" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 259 | 260 | [[package]] 261 | name = "rustc-hash" 262 | version = "1.1.0" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 265 | 266 | [[package]] 267 | name = "shlex" 268 | version = "1.1.0" 269 | source = "registry+https://github.com/rust-lang/crates.io-index" 270 | checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" 271 | 272 | [[package]] 273 | name = "syn" 274 | version = "1.0.86" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" 277 | dependencies = [ 278 | "proc-macro2", 279 | "quote", 280 | "unicode-xid", 281 | ] 282 | 283 | [[package]] 284 | name = "thiserror" 285 | version = "1.0.30" 286 | source = "registry+https://github.com/rust-lang/crates.io-index" 287 | checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" 288 | dependencies = [ 289 | "thiserror-impl", 290 | ] 291 | 292 | [[package]] 293 | name = "thiserror-impl" 294 | version = "1.0.30" 295 | source = "registry+https://github.com/rust-lang/crates.io-index" 296 | checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" 297 | dependencies = [ 298 | "proc-macro2", 299 | "quote", 300 | "syn", 301 | ] 302 | 303 | [[package]] 304 | name = "unicode-width" 305 | version = "0.1.9" 306 | source = "registry+https://github.com/rust-lang/crates.io-index" 307 | checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" 308 | 309 | [[package]] 310 | name = "unicode-xid" 311 | version = "0.2.2" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 314 | 315 | [[package]] 316 | name = "version_check" 317 | version = "0.9.4" 318 | source = "registry+https://github.com/rust-lang/crates.io-index" 319 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 320 | 321 | [[package]] 322 | name = "wasi" 323 | version = "0.10.2+wasi-snapshot-preview1" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 326 | 327 | [[package]] 328 | name = "wasmedge-bindgen-host" 329 | version = "0.4.0" 330 | dependencies = [ 331 | "num-derive", 332 | "num-traits", 333 | "wasmedge-sys", 334 | "wasmedge-types", 335 | ] 336 | 337 | [[package]] 338 | name = "wasmedge-sys" 339 | version = "0.7.0" 340 | source = "registry+https://github.com/rust-lang/crates.io-index" 341 | checksum = "fb17b890cbf53fdc9b899c9c9a37a0cb1b4701deb818f34a1e1a443511c69fd4" 342 | dependencies = [ 343 | "bindgen", 344 | "cmake", 345 | "lazy_static", 346 | "libc", 347 | "paste", 348 | "rand", 349 | "thiserror", 350 | "wasmedge-types", 351 | ] 352 | 353 | [[package]] 354 | name = "wasmedge-types" 355 | version = "0.1.3" 356 | source = "registry+https://github.com/rust-lang/crates.io-index" 357 | checksum = "7f4d917f64db782e34599fc2fcade721341a532d363889a33020fcd0105673e5" 358 | dependencies = [ 359 | "thiserror", 360 | "wat", 361 | ] 362 | 363 | [[package]] 364 | name = "wast" 365 | version = "41.0.0" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "f882898b8b817cc4edc16aa3692fdc087b356edc8cc0c2164f5b5181e31c3870" 368 | dependencies = [ 369 | "leb128", 370 | "memchr", 371 | "unicode-width", 372 | ] 373 | 374 | [[package]] 375 | name = "wat" 376 | version = "1.0.43" 377 | source = "registry+https://github.com/rust-lang/crates.io-index" 378 | checksum = "48b3b9b3e39e66c7fd3f8be785e74444d216260f491e93369e317ed6482ff80f" 379 | dependencies = [ 380 | "wast", 381 | ] 382 | 383 | [[package]] 384 | name = "winapi" 385 | version = "0.3.9" 386 | source = "registry+https://github.com/rust-lang/crates.io-index" 387 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 388 | dependencies = [ 389 | "winapi-i686-pc-windows-gnu", 390 | "winapi-x86_64-pc-windows-gnu", 391 | ] 392 | 393 | [[package]] 394 | name = "winapi-i686-pc-windows-gnu" 395 | version = "0.4.0" 396 | source = "registry+https://github.com/rust-lang/crates.io-index" 397 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 398 | 399 | [[package]] 400 | name = "winapi-x86_64-pc-windows-gnu" 401 | version = "0.4.0" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 404 | -------------------------------------------------------------------------------- /host/rust-sdk/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "anyhow" 7 | version = "1.0.58" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" 10 | 11 | [[package]] 12 | name = "autocfg" 13 | version = "1.1.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 16 | 17 | [[package]] 18 | name = "bindgen" 19 | version = "0.59.2" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8" 22 | dependencies = [ 23 | "bitflags", 24 | "cexpr", 25 | "clang-sys", 26 | "lazy_static", 27 | "lazycell", 28 | "peeking_take_while", 29 | "proc-macro2", 30 | "quote", 31 | "regex", 32 | "rustc-hash", 33 | "shlex", 34 | ] 35 | 36 | [[package]] 37 | name = "bitflags" 38 | version = "1.3.2" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 41 | 42 | [[package]] 43 | name = "cc" 44 | version = "1.0.73" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" 47 | 48 | [[package]] 49 | name = "cexpr" 50 | version = "0.6.0" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" 53 | dependencies = [ 54 | "nom", 55 | ] 56 | 57 | [[package]] 58 | name = "cfg-if" 59 | version = "1.0.0" 60 | source = "registry+https://github.com/rust-lang/crates.io-index" 61 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 62 | 63 | [[package]] 64 | name = "clang-sys" 65 | version = "1.3.3" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | checksum = "5a050e2153c5be08febd6734e29298e844fdb0fa21aeddd63b4eb7baa106c69b" 68 | dependencies = [ 69 | "glob", 70 | "libc", 71 | "libloading", 72 | ] 73 | 74 | [[package]] 75 | name = "cmake" 76 | version = "0.1.48" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" 79 | dependencies = [ 80 | "cc", 81 | ] 82 | 83 | [[package]] 84 | name = "getrandom" 85 | version = "0.2.7" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" 88 | dependencies = [ 89 | "cfg-if", 90 | "libc", 91 | "wasi", 92 | ] 93 | 94 | [[package]] 95 | name = "glob" 96 | version = "0.3.0" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" 99 | 100 | [[package]] 101 | name = "lazy_static" 102 | version = "1.4.0" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 105 | 106 | [[package]] 107 | name = "lazycell" 108 | version = "1.3.0" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" 111 | 112 | [[package]] 113 | name = "leb128" 114 | version = "0.2.5" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" 117 | 118 | [[package]] 119 | name = "libc" 120 | version = "0.2.126" 121 | source = "registry+https://github.com/rust-lang/crates.io-index" 122 | checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" 123 | 124 | [[package]] 125 | name = "libloading" 126 | version = "0.7.3" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" 129 | dependencies = [ 130 | "cfg-if", 131 | "winapi", 132 | ] 133 | 134 | [[package]] 135 | name = "lock_api" 136 | version = "0.4.8" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" 139 | dependencies = [ 140 | "autocfg", 141 | "scopeguard", 142 | ] 143 | 144 | [[package]] 145 | name = "memchr" 146 | version = "2.5.0" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" 149 | 150 | [[package]] 151 | name = "minimal-lexical" 152 | version = "0.2.1" 153 | source = "registry+https://github.com/rust-lang/crates.io-index" 154 | checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" 155 | 156 | [[package]] 157 | name = "nom" 158 | version = "7.1.1" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" 161 | dependencies = [ 162 | "memchr", 163 | "minimal-lexical", 164 | ] 165 | 166 | [[package]] 167 | name = "num-derive" 168 | version = "0.3.3" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" 171 | dependencies = [ 172 | "proc-macro2", 173 | "quote", 174 | "syn", 175 | ] 176 | 177 | [[package]] 178 | name = "num-traits" 179 | version = "0.2.15" 180 | source = "registry+https://github.com/rust-lang/crates.io-index" 181 | checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" 182 | dependencies = [ 183 | "autocfg", 184 | ] 185 | 186 | [[package]] 187 | name = "parking_lot" 188 | version = "0.12.1" 189 | source = "registry+https://github.com/rust-lang/crates.io-index" 190 | checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 191 | dependencies = [ 192 | "lock_api", 193 | "parking_lot_core", 194 | ] 195 | 196 | [[package]] 197 | name = "parking_lot_core" 198 | version = "0.9.3" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" 201 | dependencies = [ 202 | "cfg-if", 203 | "libc", 204 | "redox_syscall", 205 | "smallvec", 206 | "windows-sys", 207 | ] 208 | 209 | [[package]] 210 | name = "paste" 211 | version = "1.0.7" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" 214 | 215 | [[package]] 216 | name = "peeking_take_while" 217 | version = "0.1.2" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" 220 | 221 | [[package]] 222 | name = "ppv-lite86" 223 | version = "0.2.16" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" 226 | 227 | [[package]] 228 | name = "proc-macro2" 229 | version = "1.0.40" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" 232 | dependencies = [ 233 | "unicode-ident", 234 | ] 235 | 236 | [[package]] 237 | name = "quote" 238 | version = "1.0.20" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" 241 | dependencies = [ 242 | "proc-macro2", 243 | ] 244 | 245 | [[package]] 246 | name = "rand" 247 | version = "0.8.5" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 250 | dependencies = [ 251 | "libc", 252 | "rand_chacha", 253 | "rand_core", 254 | ] 255 | 256 | [[package]] 257 | name = "rand_chacha" 258 | version = "0.3.1" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 261 | dependencies = [ 262 | "ppv-lite86", 263 | "rand_core", 264 | ] 265 | 266 | [[package]] 267 | name = "rand_core" 268 | version = "0.6.3" 269 | source = "registry+https://github.com/rust-lang/crates.io-index" 270 | checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" 271 | dependencies = [ 272 | "getrandom", 273 | ] 274 | 275 | [[package]] 276 | name = "redox_syscall" 277 | version = "0.2.16" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" 280 | dependencies = [ 281 | "bitflags", 282 | ] 283 | 284 | [[package]] 285 | name = "regex" 286 | version = "1.6.0" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" 289 | dependencies = [ 290 | "regex-syntax", 291 | ] 292 | 293 | [[package]] 294 | name = "regex-syntax" 295 | version = "0.6.27" 296 | source = "registry+https://github.com/rust-lang/crates.io-index" 297 | checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" 298 | 299 | [[package]] 300 | name = "rustc-hash" 301 | version = "1.1.0" 302 | source = "registry+https://github.com/rust-lang/crates.io-index" 303 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 304 | 305 | [[package]] 306 | name = "scopeguard" 307 | version = "1.1.0" 308 | source = "registry+https://github.com/rust-lang/crates.io-index" 309 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 310 | 311 | [[package]] 312 | name = "shlex" 313 | version = "1.1.0" 314 | source = "registry+https://github.com/rust-lang/crates.io-index" 315 | checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" 316 | 317 | [[package]] 318 | name = "smallvec" 319 | version = "1.9.0" 320 | source = "registry+https://github.com/rust-lang/crates.io-index" 321 | checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" 322 | 323 | [[package]] 324 | name = "syn" 325 | version = "1.0.98" 326 | source = "registry+https://github.com/rust-lang/crates.io-index" 327 | checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" 328 | dependencies = [ 329 | "proc-macro2", 330 | "quote", 331 | "unicode-ident", 332 | ] 333 | 334 | [[package]] 335 | name = "thiserror" 336 | version = "1.0.31" 337 | source = "registry+https://github.com/rust-lang/crates.io-index" 338 | checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a" 339 | dependencies = [ 340 | "thiserror-impl", 341 | ] 342 | 343 | [[package]] 344 | name = "thiserror-impl" 345 | version = "1.0.31" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" 348 | dependencies = [ 349 | "proc-macro2", 350 | "quote", 351 | "syn", 352 | ] 353 | 354 | [[package]] 355 | name = "unicode-ident" 356 | version = "1.0.2" 357 | source = "registry+https://github.com/rust-lang/crates.io-index" 358 | checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7" 359 | 360 | [[package]] 361 | name = "unicode-width" 362 | version = "0.1.9" 363 | source = "registry+https://github.com/rust-lang/crates.io-index" 364 | checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" 365 | 366 | [[package]] 367 | name = "wasi" 368 | version = "0.11.0+wasi-snapshot-preview1" 369 | source = "registry+https://github.com/rust-lang/crates.io-index" 370 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 371 | 372 | [[package]] 373 | name = "wasm-encoder" 374 | version = "0.14.0" 375 | source = "registry+https://github.com/rust-lang/crates.io-index" 376 | checksum = "f76068e87fe9b837a6bc2ccded66784173eadb828c4168643e9fddf6f9ed2e61" 377 | dependencies = [ 378 | "leb128", 379 | ] 380 | 381 | [[package]] 382 | name = "wasmedge-sdk" 383 | version = "0.4.0" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "b8e99d09e3eec707d3de9a21e572cfd16e8552ff0421bf9f21832f9564dc4377" 386 | dependencies = [ 387 | "anyhow", 388 | "thiserror", 389 | "wasmedge-sys", 390 | "wasmedge-types", 391 | "wat", 392 | ] 393 | 394 | [[package]] 395 | name = "wasmedge-sdk-bindgen" 396 | version = "0.1.0" 397 | dependencies = [ 398 | "num-derive", 399 | "num-traits", 400 | "wasmedge-sdk", 401 | "wasmedge-types", 402 | ] 403 | 404 | [[package]] 405 | name = "wasmedge-sys" 406 | version = "0.9.0" 407 | source = "registry+https://github.com/rust-lang/crates.io-index" 408 | checksum = "ee931f695fe145f7838b8ada293802e64ca39b8e8a8869597748fd599c404c68" 409 | dependencies = [ 410 | "bindgen", 411 | "cmake", 412 | "lazy_static", 413 | "libc", 414 | "parking_lot", 415 | "paste", 416 | "rand", 417 | "thiserror", 418 | "wasmedge-types", 419 | ] 420 | 421 | [[package]] 422 | name = "wasmedge-types" 423 | version = "0.2.1" 424 | source = "registry+https://github.com/rust-lang/crates.io-index" 425 | checksum = "f28bfe2dc292414ecc439b4d92bbb16b0a73bc704e9d4a46a0df41ad5f9e29a8" 426 | dependencies = [ 427 | "thiserror", 428 | "wat", 429 | ] 430 | 431 | [[package]] 432 | name = "wast" 433 | version = "44.0.0" 434 | source = "registry+https://github.com/rust-lang/crates.io-index" 435 | checksum = "5f474d1b1cb7d92e5360b293f28e8bc9b2d115197a5bbf76bdbfba9161cf9cdc" 436 | dependencies = [ 437 | "leb128", 438 | "memchr", 439 | "unicode-width", 440 | "wasm-encoder", 441 | ] 442 | 443 | [[package]] 444 | name = "wat" 445 | version = "1.0.46" 446 | source = "registry+https://github.com/rust-lang/crates.io-index" 447 | checksum = "82d002ce2eca0730c6df2c21719e9c4d8d0cafe74fb0cb8ff137c0774b8e4ed1" 448 | dependencies = [ 449 | "wast", 450 | ] 451 | 452 | [[package]] 453 | name = "winapi" 454 | version = "0.3.9" 455 | source = "registry+https://github.com/rust-lang/crates.io-index" 456 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 457 | dependencies = [ 458 | "winapi-i686-pc-windows-gnu", 459 | "winapi-x86_64-pc-windows-gnu", 460 | ] 461 | 462 | [[package]] 463 | name = "winapi-i686-pc-windows-gnu" 464 | version = "0.4.0" 465 | source = "registry+https://github.com/rust-lang/crates.io-index" 466 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 467 | 468 | [[package]] 469 | name = "winapi-x86_64-pc-windows-gnu" 470 | version = "0.4.0" 471 | source = "registry+https://github.com/rust-lang/crates.io-index" 472 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 473 | 474 | [[package]] 475 | name = "windows-sys" 476 | version = "0.36.1" 477 | source = "registry+https://github.com/rust-lang/crates.io-index" 478 | checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" 479 | dependencies = [ 480 | "windows_aarch64_msvc", 481 | "windows_i686_gnu", 482 | "windows_i686_msvc", 483 | "windows_x86_64_gnu", 484 | "windows_x86_64_msvc", 485 | ] 486 | 487 | [[package]] 488 | name = "windows_aarch64_msvc" 489 | version = "0.36.1" 490 | source = "registry+https://github.com/rust-lang/crates.io-index" 491 | checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" 492 | 493 | [[package]] 494 | name = "windows_i686_gnu" 495 | version = "0.36.1" 496 | source = "registry+https://github.com/rust-lang/crates.io-index" 497 | checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" 498 | 499 | [[package]] 500 | name = "windows_i686_msvc" 501 | version = "0.36.1" 502 | source = "registry+https://github.com/rust-lang/crates.io-index" 503 | checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" 504 | 505 | [[package]] 506 | name = "windows_x86_64_gnu" 507 | version = "0.36.1" 508 | source = "registry+https://github.com/rust-lang/crates.io-index" 509 | checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" 510 | 511 | [[package]] 512 | name = "windows_x86_64_msvc" 513 | version = "0.36.1" 514 | source = "registry+https://github.com/rust-lang/crates.io-index" 515 | checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" 516 | -------------------------------------------------------------------------------- /test/BindgenFuncs/host/rust-sdk-test/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "anyhow" 7 | version = "1.0.58" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" 10 | 11 | [[package]] 12 | name = "autocfg" 13 | version = "1.1.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 16 | 17 | [[package]] 18 | name = "bindgen" 19 | version = "0.59.2" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8" 22 | dependencies = [ 23 | "bitflags", 24 | "cexpr", 25 | "clang-sys", 26 | "lazy_static", 27 | "lazycell", 28 | "peeking_take_while", 29 | "proc-macro2", 30 | "quote", 31 | "regex", 32 | "rustc-hash", 33 | "shlex", 34 | ] 35 | 36 | [[package]] 37 | name = "bindgen-funcs" 38 | version = "0.1.0" 39 | dependencies = [ 40 | "wasmedge-sdk", 41 | "wasmedge-sdk-bindgen", 42 | ] 43 | 44 | [[package]] 45 | name = "bitflags" 46 | version = "1.3.2" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 49 | 50 | [[package]] 51 | name = "cc" 52 | version = "1.0.73" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" 55 | 56 | [[package]] 57 | name = "cexpr" 58 | version = "0.6.0" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" 61 | dependencies = [ 62 | "nom", 63 | ] 64 | 65 | [[package]] 66 | name = "cfg-if" 67 | version = "1.0.0" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 70 | 71 | [[package]] 72 | name = "clang-sys" 73 | version = "1.3.1" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | checksum = "4cc00842eed744b858222c4c9faf7243aafc6d33f92f96935263ef4d8a41ce21" 76 | dependencies = [ 77 | "glob", 78 | "libc", 79 | "libloading", 80 | ] 81 | 82 | [[package]] 83 | name = "cmake" 84 | version = "0.1.48" 85 | source = "registry+https://github.com/rust-lang/crates.io-index" 86 | checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" 87 | dependencies = [ 88 | "cc", 89 | ] 90 | 91 | [[package]] 92 | name = "getrandom" 93 | version = "0.2.4" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" 96 | dependencies = [ 97 | "cfg-if", 98 | "libc", 99 | "wasi", 100 | ] 101 | 102 | [[package]] 103 | name = "glob" 104 | version = "0.3.0" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" 107 | 108 | [[package]] 109 | name = "lazy_static" 110 | version = "1.4.0" 111 | source = "registry+https://github.com/rust-lang/crates.io-index" 112 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 113 | 114 | [[package]] 115 | name = "lazycell" 116 | version = "1.3.0" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" 119 | 120 | [[package]] 121 | name = "leb128" 122 | version = "0.2.5" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" 125 | 126 | [[package]] 127 | name = "libc" 128 | version = "0.2.117" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" 131 | 132 | [[package]] 133 | name = "libloading" 134 | version = "0.7.3" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" 137 | dependencies = [ 138 | "cfg-if", 139 | "winapi", 140 | ] 141 | 142 | [[package]] 143 | name = "lock_api" 144 | version = "0.4.8" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" 147 | dependencies = [ 148 | "autocfg", 149 | "scopeguard", 150 | ] 151 | 152 | [[package]] 153 | name = "memchr" 154 | version = "2.4.1" 155 | source = "registry+https://github.com/rust-lang/crates.io-index" 156 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 157 | 158 | [[package]] 159 | name = "minimal-lexical" 160 | version = "0.2.1" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" 163 | 164 | [[package]] 165 | name = "nom" 166 | version = "7.1.0" 167 | source = "registry+https://github.com/rust-lang/crates.io-index" 168 | checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" 169 | dependencies = [ 170 | "memchr", 171 | "minimal-lexical", 172 | "version_check", 173 | ] 174 | 175 | [[package]] 176 | name = "num-derive" 177 | version = "0.3.3" 178 | source = "registry+https://github.com/rust-lang/crates.io-index" 179 | checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" 180 | dependencies = [ 181 | "proc-macro2", 182 | "quote", 183 | "syn", 184 | ] 185 | 186 | [[package]] 187 | name = "num-traits" 188 | version = "0.2.14" 189 | source = "registry+https://github.com/rust-lang/crates.io-index" 190 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 191 | dependencies = [ 192 | "autocfg", 193 | ] 194 | 195 | [[package]] 196 | name = "parking_lot" 197 | version = "0.12.1" 198 | source = "registry+https://github.com/rust-lang/crates.io-index" 199 | checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 200 | dependencies = [ 201 | "lock_api", 202 | "parking_lot_core", 203 | ] 204 | 205 | [[package]] 206 | name = "parking_lot_core" 207 | version = "0.9.3" 208 | source = "registry+https://github.com/rust-lang/crates.io-index" 209 | checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" 210 | dependencies = [ 211 | "cfg-if", 212 | "libc", 213 | "redox_syscall", 214 | "smallvec", 215 | "windows-sys", 216 | ] 217 | 218 | [[package]] 219 | name = "paste" 220 | version = "1.0.6" 221 | source = "registry+https://github.com/rust-lang/crates.io-index" 222 | checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5" 223 | 224 | [[package]] 225 | name = "peeking_take_while" 226 | version = "0.1.2" 227 | source = "registry+https://github.com/rust-lang/crates.io-index" 228 | checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" 229 | 230 | [[package]] 231 | name = "ppv-lite86" 232 | version = "0.2.16" 233 | source = "registry+https://github.com/rust-lang/crates.io-index" 234 | checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" 235 | 236 | [[package]] 237 | name = "proc-macro2" 238 | version = "1.0.36" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" 241 | dependencies = [ 242 | "unicode-xid", 243 | ] 244 | 245 | [[package]] 246 | name = "quote" 247 | version = "1.0.15" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" 250 | dependencies = [ 251 | "proc-macro2", 252 | ] 253 | 254 | [[package]] 255 | name = "rand" 256 | version = "0.8.5" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 259 | dependencies = [ 260 | "libc", 261 | "rand_chacha", 262 | "rand_core", 263 | ] 264 | 265 | [[package]] 266 | name = "rand_chacha" 267 | version = "0.3.1" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 270 | dependencies = [ 271 | "ppv-lite86", 272 | "rand_core", 273 | ] 274 | 275 | [[package]] 276 | name = "rand_core" 277 | version = "0.6.3" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" 280 | dependencies = [ 281 | "getrandom", 282 | ] 283 | 284 | [[package]] 285 | name = "redox_syscall" 286 | version = "0.2.16" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" 289 | dependencies = [ 290 | "bitflags", 291 | ] 292 | 293 | [[package]] 294 | name = "regex" 295 | version = "1.5.4" 296 | source = "registry+https://github.com/rust-lang/crates.io-index" 297 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 298 | dependencies = [ 299 | "regex-syntax", 300 | ] 301 | 302 | [[package]] 303 | name = "regex-syntax" 304 | version = "0.6.25" 305 | source = "registry+https://github.com/rust-lang/crates.io-index" 306 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 307 | 308 | [[package]] 309 | name = "rustc-hash" 310 | version = "1.1.0" 311 | source = "registry+https://github.com/rust-lang/crates.io-index" 312 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 313 | 314 | [[package]] 315 | name = "scopeguard" 316 | version = "1.1.0" 317 | source = "registry+https://github.com/rust-lang/crates.io-index" 318 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 319 | 320 | [[package]] 321 | name = "shlex" 322 | version = "1.1.0" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" 325 | 326 | [[package]] 327 | name = "smallvec" 328 | version = "1.9.0" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" 331 | 332 | [[package]] 333 | name = "syn" 334 | version = "1.0.86" 335 | source = "registry+https://github.com/rust-lang/crates.io-index" 336 | checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" 337 | dependencies = [ 338 | "proc-macro2", 339 | "quote", 340 | "unicode-xid", 341 | ] 342 | 343 | [[package]] 344 | name = "thiserror" 345 | version = "1.0.30" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" 348 | dependencies = [ 349 | "thiserror-impl", 350 | ] 351 | 352 | [[package]] 353 | name = "thiserror-impl" 354 | version = "1.0.30" 355 | source = "registry+https://github.com/rust-lang/crates.io-index" 356 | checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" 357 | dependencies = [ 358 | "proc-macro2", 359 | "quote", 360 | "syn", 361 | ] 362 | 363 | [[package]] 364 | name = "unicode-width" 365 | version = "0.1.9" 366 | source = "registry+https://github.com/rust-lang/crates.io-index" 367 | checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" 368 | 369 | [[package]] 370 | name = "unicode-xid" 371 | version = "0.2.2" 372 | source = "registry+https://github.com/rust-lang/crates.io-index" 373 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 374 | 375 | [[package]] 376 | name = "version_check" 377 | version = "0.9.4" 378 | source = "registry+https://github.com/rust-lang/crates.io-index" 379 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 380 | 381 | [[package]] 382 | name = "wasi" 383 | version = "0.10.2+wasi-snapshot-preview1" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 386 | 387 | [[package]] 388 | name = "wasmedge-sdk" 389 | version = "0.4.0" 390 | source = "registry+https://github.com/rust-lang/crates.io-index" 391 | checksum = "b8e99d09e3eec707d3de9a21e572cfd16e8552ff0421bf9f21832f9564dc4377" 392 | dependencies = [ 393 | "anyhow", 394 | "thiserror", 395 | "wasmedge-sys", 396 | "wasmedge-types", 397 | "wat", 398 | ] 399 | 400 | [[package]] 401 | name = "wasmedge-sdk-bindgen" 402 | version = "0.1.0" 403 | dependencies = [ 404 | "num-derive", 405 | "num-traits", 406 | "wasmedge-sdk", 407 | "wasmedge-types", 408 | ] 409 | 410 | [[package]] 411 | name = "wasmedge-sys" 412 | version = "0.9.0" 413 | source = "registry+https://github.com/rust-lang/crates.io-index" 414 | checksum = "ee931f695fe145f7838b8ada293802e64ca39b8e8a8869597748fd599c404c68" 415 | dependencies = [ 416 | "bindgen", 417 | "cmake", 418 | "lazy_static", 419 | "libc", 420 | "parking_lot", 421 | "paste", 422 | "rand", 423 | "thiserror", 424 | "wasmedge-types", 425 | ] 426 | 427 | [[package]] 428 | name = "wasmedge-types" 429 | version = "0.2.1" 430 | source = "registry+https://github.com/rust-lang/crates.io-index" 431 | checksum = "f28bfe2dc292414ecc439b4d92bbb16b0a73bc704e9d4a46a0df41ad5f9e29a8" 432 | dependencies = [ 433 | "thiserror", 434 | "wat", 435 | ] 436 | 437 | [[package]] 438 | name = "wast" 439 | version = "41.0.0" 440 | source = "registry+https://github.com/rust-lang/crates.io-index" 441 | checksum = "f882898b8b817cc4edc16aa3692fdc087b356edc8cc0c2164f5b5181e31c3870" 442 | dependencies = [ 443 | "leb128", 444 | "memchr", 445 | "unicode-width", 446 | ] 447 | 448 | [[package]] 449 | name = "wat" 450 | version = "1.0.43" 451 | source = "registry+https://github.com/rust-lang/crates.io-index" 452 | checksum = "48b3b9b3e39e66c7fd3f8be785e74444d216260f491e93369e317ed6482ff80f" 453 | dependencies = [ 454 | "wast", 455 | ] 456 | 457 | [[package]] 458 | name = "winapi" 459 | version = "0.3.9" 460 | source = "registry+https://github.com/rust-lang/crates.io-index" 461 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 462 | dependencies = [ 463 | "winapi-i686-pc-windows-gnu", 464 | "winapi-x86_64-pc-windows-gnu", 465 | ] 466 | 467 | [[package]] 468 | name = "winapi-i686-pc-windows-gnu" 469 | version = "0.4.0" 470 | source = "registry+https://github.com/rust-lang/crates.io-index" 471 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 472 | 473 | [[package]] 474 | name = "winapi-x86_64-pc-windows-gnu" 475 | version = "0.4.0" 476 | source = "registry+https://github.com/rust-lang/crates.io-index" 477 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 478 | 479 | [[package]] 480 | name = "windows-sys" 481 | version = "0.36.1" 482 | source = "registry+https://github.com/rust-lang/crates.io-index" 483 | checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" 484 | dependencies = [ 485 | "windows_aarch64_msvc", 486 | "windows_i686_gnu", 487 | "windows_i686_msvc", 488 | "windows_x86_64_gnu", 489 | "windows_x86_64_msvc", 490 | ] 491 | 492 | [[package]] 493 | name = "windows_aarch64_msvc" 494 | version = "0.36.1" 495 | source = "registry+https://github.com/rust-lang/crates.io-index" 496 | checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" 497 | 498 | [[package]] 499 | name = "windows_i686_gnu" 500 | version = "0.36.1" 501 | source = "registry+https://github.com/rust-lang/crates.io-index" 502 | checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" 503 | 504 | [[package]] 505 | name = "windows_i686_msvc" 506 | version = "0.36.1" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" 509 | 510 | [[package]] 511 | name = "windows_x86_64_gnu" 512 | version = "0.36.1" 513 | source = "registry+https://github.com/rust-lang/crates.io-index" 514 | checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" 515 | 516 | [[package]] 517 | name = "windows_x86_64_msvc" 518 | version = "0.36.1" 519 | source = "registry+https://github.com/rust-lang/crates.io-index" 520 | checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" 521 | -------------------------------------------------------------------------------- /host/rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::any::Any; 2 | use std::ptr::NonNull; 3 | use core::ops::{Deref, DerefMut}; 4 | 5 | use num_derive::FromPrimitive; 6 | use num_traits::FromPrimitive; 7 | use wasmedge_sys::*; 8 | use wasmedge_types::*; 9 | 10 | pub enum Param<'a> { 11 | I8(i8), 12 | U8(u8), 13 | I16(i16), 14 | U16(u16), 15 | I32(i32), 16 | U32(u32), 17 | I64(i64), 18 | U64(u64), 19 | F32(f32), 20 | F64(f64), 21 | Bool(bool), 22 | VecI8(&'a Vec), 23 | VecU8(&'a Vec), 24 | VecI16(&'a Vec), 25 | VecU16(&'a Vec), 26 | VecI32(&'a Vec), 27 | VecU32(&'a Vec), 28 | VecI64(&'a Vec), 29 | VecU64(&'a Vec), 30 | String(&'a str), 31 | } 32 | 33 | impl<'a> Param<'a> { 34 | fn settle(&self, vm: &Vm, mem: &mut Memory) -> WasmEdgeResult<(i32, i32)> { 35 | match self { 36 | Param::I8(v) => { 37 | let length = 1; 38 | let pointer = allocate(vm, length)?; 39 | mem.set_data(vec![*v as u8], pointer as u32)?; 40 | Ok((pointer, length)) 41 | } 42 | Param::U8(v) => { 43 | let length = 1; 44 | let pointer = allocate(vm, length)?; 45 | mem.set_data(vec![*v], pointer as u32)?; 46 | Ok((pointer, length)) 47 | } 48 | Param::I16(v) => { 49 | let length = 1; 50 | let pointer = allocate(vm, length * 2)?; 51 | let bytes = v.to_le_bytes(); 52 | mem.set_data(bytes, pointer as u32)?; 53 | Ok((pointer, length)) 54 | } 55 | Param::U16(v) => { 56 | let length = 2; 57 | let pointer = allocate(vm, length * 2)?; 58 | let bytes = v.to_le_bytes(); 59 | mem.set_data(bytes, pointer as u32)?; 60 | Ok((pointer, length)) 61 | } 62 | Param::I32(v) => { 63 | let length = 1; 64 | let pointer = allocate(vm, length * 4)?; 65 | let bytes = v.to_le_bytes(); 66 | mem.set_data(bytes, pointer as u32)?; 67 | Ok((pointer, length)) 68 | } 69 | Param::U32(v) => { 70 | let length = 1; 71 | let pointer = allocate(vm, length * 4)?; 72 | let bytes = v.to_le_bytes(); 73 | mem.set_data(bytes, pointer as u32)?; 74 | Ok((pointer, length)) 75 | } 76 | Param::I64(v) => { 77 | let length = 1; 78 | let pointer = allocate(vm, length * 8)?; 79 | let bytes = v.to_le_bytes(); 80 | mem.set_data(bytes, pointer as u32)?; 81 | Ok((pointer, length)) 82 | } 83 | Param::U64(v) => { 84 | let length = 1; 85 | let pointer = allocate(vm, length * 8)?; 86 | let bytes = v.to_le_bytes(); 87 | mem.set_data(bytes, pointer as u32)?; 88 | Ok((pointer, length)) 89 | } 90 | Param::F32(v) => { 91 | let length = 1; 92 | let pointer = allocate(vm, length * 4)?; 93 | let bytes = v.to_le_bytes(); 94 | mem.set_data(bytes, pointer as u32)?; 95 | Ok((pointer, length)) 96 | } 97 | Param::F64(v) => { 98 | let length = 1; 99 | let pointer = allocate(vm, length * 8)?; 100 | let bytes = v.to_le_bytes(); 101 | mem.set_data(bytes, pointer as u32)?; 102 | Ok((pointer, length)) 103 | } 104 | Param::Bool(v) => { 105 | let length = 1; 106 | let pointer = allocate(vm, length)?; 107 | let byte: u8 = match v { 108 | true => 1, 109 | false => 0 110 | }; 111 | mem.set_data(vec![byte], pointer as u32)?; 112 | Ok((pointer, length)) 113 | } 114 | Param::VecI8(v) => { 115 | let length = v.len() as i32; 116 | let pointer = allocate(vm, length)?; 117 | let mut bytes = vec![0; length as usize]; 118 | for (pos, iv) in v.iter().enumerate() { 119 | bytes[pos] = *iv as u8; 120 | } 121 | mem.set_data(bytes, pointer as u32)?; 122 | Ok((pointer, length)) 123 | } 124 | Param::VecU8(v) => { 125 | let length = v.len() as i32; 126 | let pointer = allocate(vm, length)?; 127 | let mut bytes = vec![0; length as usize]; 128 | for (pos, iv) in v.iter().enumerate() { 129 | bytes[pos] = *iv; 130 | } 131 | mem.set_data(bytes, pointer as u32)?; 132 | Ok((pointer, length)) 133 | } 134 | Param::VecI16(v) => { 135 | let length = v.len() as i32; 136 | let pointer = allocate(vm, length * 2)?; 137 | let mut bytes = vec![0; length as usize * 2]; 138 | for (pos, iv) in v.iter().enumerate() { 139 | let b = iv.to_le_bytes(); 140 | for i in 0..2 { 141 | bytes[pos * 2 + i] = b[i]; 142 | } 143 | } 144 | mem.set_data(bytes, pointer as u32)?; 145 | Ok((pointer, length)) 146 | } 147 | Param::VecU16(v) => { 148 | let length = v.len() as i32; 149 | let pointer = allocate(vm, length * 2)?; 150 | let mut bytes = vec![0; length as usize * 2]; 151 | for (pos, iv) in v.iter().enumerate() { 152 | let b = iv.to_le_bytes(); 153 | for i in 0..2 { 154 | bytes[pos * 2 + i] = b[i]; 155 | } 156 | } 157 | mem.set_data(bytes, pointer as u32)?; 158 | Ok((pointer, length)) 159 | } 160 | Param::VecI32(v) => { 161 | let length = v.len() as i32; 162 | let pointer = allocate(vm, length * 4)?; 163 | let mut bytes = vec![0; length as usize * 4]; 164 | for (pos, iv) in v.iter().enumerate() { 165 | let b = iv.to_le_bytes(); 166 | for i in 0..4 { 167 | bytes[pos * 4 + i] = b[i]; 168 | } 169 | } 170 | mem.set_data(bytes, pointer as u32)?; 171 | Ok((pointer, length)) 172 | } 173 | Param::VecU32(v) => { 174 | let length = v.len() as i32; 175 | let pointer = allocate(vm, length * 4)?; 176 | let mut bytes = vec![0; length as usize * 4]; 177 | for (pos, iv) in v.iter().enumerate() { 178 | let b = iv.to_le_bytes(); 179 | for i in 0..4 { 180 | bytes[pos * 4 + i] = b[i]; 181 | } 182 | } 183 | mem.set_data(bytes, pointer as u32)?; 184 | Ok((pointer, length)) 185 | } 186 | Param::VecI64(v) => { 187 | let length = v.len() as i32; 188 | let pointer = allocate(vm, length * 8)?; 189 | let mut bytes = vec![0; length as usize * 8]; 190 | for (pos, iv) in v.iter().enumerate() { 191 | let b = iv.to_le_bytes(); 192 | for i in 0..8 { 193 | bytes[pos * 8 + i] = b[i]; 194 | } 195 | } 196 | mem.set_data(bytes, pointer as u32)?; 197 | Ok((pointer, length)) 198 | } 199 | Param::VecU64(v) => { 200 | let length = v.len() as i32; 201 | let pointer = allocate(vm, length * 8)?; 202 | let mut bytes = vec![0; length as usize * 8]; 203 | for (pos, iv) in v.iter().enumerate() { 204 | let b = iv.to_le_bytes(); 205 | for i in 0..8 { 206 | bytes[pos * 8 + i] = b[i]; 207 | } 208 | } 209 | mem.set_data(bytes, pointer as u32)?; 210 | Ok((pointer, length)) 211 | } 212 | Param::String(v) => { 213 | let bytes = v.as_bytes().to_vec(); 214 | let length = bytes.len() as i32; 215 | let pointer = allocate(vm, length)?; 216 | mem.set_data(bytes, pointer as u32)?; 217 | Ok((pointer, length)) 218 | } 219 | } 220 | } 221 | } 222 | 223 | fn allocate(vm: &Vm, size: i32) -> WasmEdgeResult { 224 | match vm.run_function("allocate", vec![WasmValue::from_i32(size)]) { 225 | Ok(rv) => { 226 | Ok(rv[0].to_i32()) 227 | } 228 | Err(e) => { 229 | Err(e) 230 | } 231 | } 232 | } 233 | 234 | #[derive(FromPrimitive)] 235 | enum RetTypes { 236 | U8 = 1, 237 | I8 = 2, 238 | U16 = 3, 239 | I16 = 4, 240 | U32 = 5, 241 | I32 = 6, 242 | U64 = 7, 243 | I64 = 8, 244 | F32 = 9, 245 | F64 = 10, 246 | Bool = 11, 247 | Char = 12, 248 | U8Array = 21, 249 | I8Array = 22, 250 | U16Array = 23, 251 | I16Array = 24, 252 | U32Array = 25, 253 | I32Array = 26, 254 | U64Array = 27, 255 | I64Array = 28, 256 | String = 31, 257 | } 258 | 259 | // Like Arc but don't check clone count when get mut 260 | #[derive(Copy)] 261 | struct VmArc { 262 | inner: NonNull 263 | } 264 | 265 | unsafe impl Send for VmArc {} 266 | unsafe impl Sync for VmArc {} 267 | 268 | impl Deref for VmArc { 269 | type Target = Vm; 270 | 271 | #[inline] 272 | fn deref(&self) -> &Vm { 273 | unsafe { 274 | &self.inner.as_ref() 275 | } 276 | } 277 | } 278 | 279 | impl DerefMut for VmArc { 280 | #[inline] 281 | fn deref_mut(&mut self) -> &mut Vm { 282 | unsafe { 283 | &mut (*self.inner.as_ptr()) 284 | } 285 | } 286 | } 287 | 288 | impl Clone for VmArc { 289 | #[inline] 290 | fn clone(&self) -> VmArc { 291 | Self {inner: self.inner} 292 | } 293 | } 294 | 295 | pub struct Bindgen { 296 | vm: VmArc, // Can't use Arc because vm can be get_mut after cloned for hostfunc 297 | } 298 | 299 | impl Clone for Bindgen { 300 | fn clone(&self) -> Self { 301 | Bindgen { 302 | vm: self.vm, 303 | } 304 | } 305 | } 306 | 307 | unsafe impl Send for Bindgen {} 308 | unsafe impl Sync for Bindgen {} 309 | 310 | impl Bindgen { 311 | pub fn new(vm: Vm) -> Self { 312 | 313 | let vm_inner = Box::new(vm); 314 | Bindgen { 315 | vm: VmArc {inner: Box::leak(vm_inner).into()}, 316 | } 317 | } 318 | 319 | pub fn instantiate(&mut self) { 320 | _ = self.vm.instantiate(); 321 | } 322 | 323 | pub fn vm(&mut self) -> &mut Vm { 324 | unsafe { 325 | self.vm.inner.as_mut() 326 | } 327 | } 328 | 329 | pub fn run_wasm(&mut self, func_name: impl AsRef, inputs: Vec) -> WasmEdgeResult>, String>> { 330 | let inputs_count = inputs.len() as i32; 331 | 332 | // allocate new frame for passing pointers 333 | let pointer_of_pointers = match self.vm.run_function("allocate", vec![WasmValue::from_i32(inputs_count * 4 * 2)]) { 334 | Ok(rv) => { 335 | rv[0].to_i32() 336 | } 337 | Err(e) => { 338 | return Err(e); 339 | } 340 | }; 341 | 342 | let mut memory = self.vm.active_module().unwrap().get_memory("memory").unwrap(); 343 | 344 | for (pos, inp) in inputs.iter().enumerate() { 345 | let sr = inp.settle(&self.vm, &mut memory); 346 | 347 | let (pointer, length_of_input) = match sr { 348 | Ok(r) => { 349 | (r.0, r.1) 350 | } 351 | Err(e) => { 352 | return Err(e); 353 | } 354 | }; 355 | 356 | memory.set_data(pointer.to_le_bytes(), pointer_of_pointers as u32 + pos as u32 * 4 * 2)?; 357 | memory.set_data(length_of_input.to_le_bytes(), pointer_of_pointers as u32 + pos as u32 * 4 * 2 + 4)?; 358 | } 359 | 360 | let rets = self.vm.run_function(func_name, vec![WasmValue::from_i32(pointer_of_pointers), WasmValue::from_i32(inputs_count)])?; 361 | // Don't need to deallocate because the memory will be loaded and free in the wasm 362 | // self.vm.run_function("deallocate", vec![WasmValue::from_i32(pointer_of_pointers), WasmValue::from_i32(inputs_count * 4 * 2)])?; 363 | 364 | if rets.len() != 1 { 365 | return Ok(Err(String::from("Invalid return value"))); 366 | } 367 | let rvec = memory.get_data(rets[0].to_i32() as u32, 9)?; 368 | let _ = self.vm.run_function("deallocate", vec![WasmValue::from_i32(rets[0].to_i32()), WasmValue::from_i32(9)]); 369 | 370 | let flag = rvec[0]; 371 | let ret_pointer = i32::from_le_bytes(rvec[1..5].try_into().unwrap()); 372 | let ret_len = i32::from_le_bytes(rvec[5..9].try_into().unwrap()); 373 | match flag { 374 | 0 => Ok(Ok(self.parse_result(ret_pointer, ret_len))), 375 | _ => Ok(Err(self.parse_error(ret_pointer, ret_len))), 376 | } 377 | } 378 | 379 | fn parse_error(&self, ret_pointer: i32, ret_len: i32) -> String { 380 | let memory = self.vm.active_module().unwrap().get_memory("memory").unwrap(); 381 | let err_bytes = memory.get_data(ret_pointer as u32, ret_len as u32).unwrap(); 382 | let _ = self.vm.run_function("deallocate", vec![WasmValue::from_i32(ret_pointer), WasmValue::from_i32(ret_len)]); 383 | String::from_utf8(err_bytes).unwrap_or_default() 384 | } 385 | 386 | fn parse_result(&self, ret_pointer: i32, ret_len: i32) -> Vec> { 387 | let size = ret_len as usize; 388 | let memory = self.vm.active_module().unwrap().get_memory("memory").unwrap(); 389 | let p_data = memory.get_data(ret_pointer as u32, size as u32 * 3 * 4).unwrap(); 390 | let _ = self.vm.run_function("deallocate", vec![WasmValue::from_i32(ret_pointer), WasmValue::from_i32(size as i32 * 3 * 4)]); 391 | 392 | let mut p_values = vec![0; size * 3]; 393 | 394 | for i in 0..size * 3 { 395 | p_values[i] = i32::from_le_bytes(p_data[i*4..(i+1)*4].try_into().unwrap()); 396 | } 397 | 398 | let mut results: Vec> = Vec::with_capacity(size); 399 | 400 | for i in 0..size { 401 | let bytes = memory.get_data(p_values[i*3] as u32, p_values[i*3+2] as u32).unwrap(); 402 | let _ = self.vm.run_function("deallocate", vec![WasmValue::from_i32(p_values[i*3]), WasmValue::from_i32(p_values[i*3+2])]); 403 | match FromPrimitive::from_i32(p_values[i*3+1]) { 404 | Some(RetTypes::U8) => { 405 | results.push(Box::new(bytes[0])); 406 | } 407 | Some(RetTypes::I8) => { 408 | results.push(Box::new(bytes[0] as i8)); 409 | } 410 | Some(RetTypes::U16) => { 411 | let v = u16::from_le_bytes(bytes.try_into().unwrap()); 412 | results.push(Box::new(v)); 413 | } 414 | Some(RetTypes::I16) => { 415 | let v = i16::from_le_bytes(bytes.try_into().unwrap()); 416 | results.push(Box::new(v)); 417 | } 418 | Some(RetTypes::U32) => { 419 | let v = u32::from_le_bytes(bytes.try_into().unwrap()); 420 | results.push(Box::new(v)); 421 | } 422 | Some(RetTypes::I32) => { 423 | let v = i32::from_le_bytes(bytes.try_into().unwrap()); 424 | results.push(Box::new(v)); 425 | } 426 | Some(RetTypes::U64) => { 427 | let v = u64::from_le_bytes(bytes.try_into().unwrap()); 428 | results.push(Box::new(v)); 429 | } 430 | Some(RetTypes::I64) => { 431 | let v = i64::from_le_bytes(bytes.try_into().unwrap()); 432 | results.push(Box::new(v)); 433 | } 434 | Some(RetTypes::F32) => { 435 | let v = f32::from_le_bytes(bytes.try_into().unwrap()); 436 | results.push(Box::new(v)); 437 | } 438 | Some(RetTypes::F64) => { 439 | let v = f64::from_le_bytes(bytes.try_into().unwrap()); 440 | results.push(Box::new(v)); 441 | } 442 | Some(RetTypes::Bool) => { 443 | results.push(Box::new(bytes[0] == 1 as u8)); 444 | } 445 | Some(RetTypes::Char) => { 446 | let v = u32::from_le_bytes(bytes.try_into().unwrap()); 447 | results.push(Box::new(char::from_u32(v))); 448 | } 449 | Some(RetTypes::U8Array) => { 450 | let len = bytes.len(); 451 | let mut v = vec![0; len]; 452 | for i in 0..len { 453 | v[i] = bytes[i] as u8; 454 | } 455 | results.push(Box::new(v)); 456 | } 457 | Some(RetTypes::I8Array) => { 458 | let len = bytes.len(); 459 | let mut v = vec![0; len]; 460 | for i in 0..len { 461 | v[i] = bytes[i] as i8; 462 | } 463 | results.push(Box::new(v)); 464 | } 465 | Some(RetTypes::U16Array) => { 466 | let len = bytes.len() / 2; 467 | let mut v = vec![0; len]; 468 | for i in 0..len { 469 | v[i] = u16::from_le_bytes(bytes[i*2..(i+1)*2].try_into().unwrap()); 470 | } 471 | results.push(Box::new(v)); 472 | } 473 | Some(RetTypes::I16Array) => { 474 | let len = bytes.len() / 2; 475 | let mut v = vec![0; len]; 476 | for i in 0..len { 477 | v[i] = i16::from_le_bytes(bytes[i*2..(i+1)*2].try_into().unwrap()); 478 | } 479 | results.push(Box::new(v)); 480 | } 481 | Some(RetTypes::U32Array) => { 482 | let len = bytes.len() / 4; 483 | let mut v = vec![0; len]; 484 | for i in 0..len { 485 | v[i] = u32::from_le_bytes(bytes[i*4..(i+1)*4].try_into().unwrap()); 486 | } 487 | results.push(Box::new(v)); 488 | } 489 | Some(RetTypes::I32Array) => { 490 | let len = bytes.len() / 4; 491 | let mut v = vec![0; len]; 492 | for i in 0..len { 493 | v[i] = i32::from_le_bytes(bytes[i*4..(i+1)*4].try_into().unwrap()); 494 | } 495 | results.push(Box::new(v)); 496 | } 497 | Some(RetTypes::U64Array) => { 498 | let len = bytes.len() / 8; 499 | let mut v = vec![0; len]; 500 | for i in 0..len { 501 | v[i] = u64::from_le_bytes(bytes[i*8..(i+1)*8].try_into().unwrap()); 502 | } 503 | results.push(Box::new(v)); 504 | } 505 | Some(RetTypes::I64Array) => { 506 | let len = bytes.len() / 8; 507 | let mut v = vec![0; len]; 508 | for i in 0..len { 509 | v[i] = i64::from_le_bytes(bytes[i*8..(i+1)*8].try_into().unwrap()); 510 | } 511 | results.push(Box::new(v)); 512 | } 513 | Some(RetTypes::String) => { 514 | results.push(Box::new(String::from_utf8(bytes).unwrap())); 515 | } 516 | None => {} 517 | } 518 | } 519 | 520 | results 521 | } 522 | } -------------------------------------------------------------------------------- /host/rust-sdk/src/lib.rs: -------------------------------------------------------------------------------- 1 | use num_derive::FromPrimitive; 2 | use num_traits::FromPrimitive; 3 | use std::any::Any; 4 | use wasmedge_sdk::*; 5 | 6 | #[derive(Debug)] 7 | pub enum Param<'a> { 8 | I8(i8), 9 | U8(u8), 10 | I16(i16), 11 | U16(u16), 12 | I32(i32), 13 | U32(u32), 14 | I64(i64), 15 | U64(u64), 16 | F32(f32), 17 | F64(f64), 18 | Bool(bool), 19 | VecI8(&'a Vec), 20 | VecU8(&'a Vec), 21 | VecI16(&'a Vec), 22 | VecU16(&'a Vec), 23 | VecI32(&'a Vec), 24 | VecU32(&'a Vec), 25 | VecI64(&'a Vec), 26 | VecU64(&'a Vec), 27 | String(&'a str), 28 | } 29 | 30 | impl<'a> Param<'a> { 31 | fn settle(&self, vm: &Vm, mem: &mut Memory) -> WasmEdgeResult<(i32, i32)> { 32 | match self { 33 | Param::I8(v) => { 34 | let length = 1; 35 | let pointer = allocate(vm, length)?; 36 | mem.write(vec![*v as u8], pointer as u32)?; 37 | Ok((pointer, length)) 38 | } 39 | Param::U8(v) => { 40 | let length = 1; 41 | let pointer = allocate(vm, length)?; 42 | mem.write(vec![*v], pointer as u32)?; 43 | Ok((pointer, length)) 44 | } 45 | Param::I16(v) => { 46 | let length = 1; 47 | let pointer = allocate(vm, length * 2)?; 48 | let bytes = v.to_le_bytes(); 49 | mem.write(bytes, pointer as u32)?; 50 | Ok((pointer, length)) 51 | } 52 | Param::U16(v) => { 53 | let length = 2; 54 | let pointer = allocate(vm, length * 2)?; 55 | let bytes = v.to_le_bytes(); 56 | mem.write(bytes, pointer as u32)?; 57 | Ok((pointer, length)) 58 | } 59 | Param::I32(v) => { 60 | let length = 1; 61 | let pointer = allocate(vm, length * 4)?; 62 | let bytes = v.to_le_bytes(); 63 | mem.write(bytes, pointer as u32)?; 64 | Ok((pointer, length)) 65 | } 66 | Param::U32(v) => { 67 | let length = 1; 68 | let pointer = allocate(vm, length * 4)?; 69 | let bytes = v.to_le_bytes(); 70 | mem.write(bytes, pointer as u32)?; 71 | Ok((pointer, length)) 72 | } 73 | Param::I64(v) => { 74 | let length = 1; 75 | let pointer = allocate(vm, length * 8)?; 76 | let bytes = v.to_le_bytes(); 77 | mem.write(bytes, pointer as u32)?; 78 | Ok((pointer, length)) 79 | } 80 | Param::U64(v) => { 81 | let length = 1; 82 | let pointer = allocate(vm, length * 8)?; 83 | let bytes = v.to_le_bytes(); 84 | mem.write(bytes, pointer as u32)?; 85 | Ok((pointer, length)) 86 | } 87 | Param::F32(v) => { 88 | let length = 1; 89 | let pointer = allocate(vm, length * 4)?; 90 | let bytes = v.to_le_bytes(); 91 | mem.write(bytes, pointer as u32)?; 92 | Ok((pointer, length)) 93 | } 94 | Param::F64(v) => { 95 | let length = 1; 96 | let pointer = allocate(vm, length * 8)?; 97 | let bytes = v.to_le_bytes(); 98 | mem.write(bytes, pointer as u32)?; 99 | Ok((pointer, length)) 100 | } 101 | Param::Bool(v) => { 102 | let length = 1; 103 | let pointer = allocate(vm, length)?; 104 | let byte: u8 = match v { 105 | true => 1, 106 | false => 0, 107 | }; 108 | mem.write(vec![byte], pointer as u32)?; 109 | Ok((pointer, length)) 110 | } 111 | Param::VecI8(v) => { 112 | let length = v.len() as i32; 113 | let pointer = allocate(vm, length)?; 114 | let mut bytes = vec![0; length as usize]; 115 | for (pos, iv) in v.iter().enumerate() { 116 | bytes[pos] = *iv as u8; 117 | } 118 | mem.write(bytes, pointer as u32)?; 119 | Ok((pointer, length)) 120 | } 121 | Param::VecU8(v) => { 122 | let length = v.len() as i32; 123 | let pointer = allocate(vm, length)?; 124 | let mut bytes = vec![0; length as usize]; 125 | for (pos, iv) in v.iter().enumerate() { 126 | bytes[pos] = *iv; 127 | } 128 | mem.write(bytes, pointer as u32)?; 129 | Ok((pointer, length)) 130 | } 131 | Param::VecI16(v) => { 132 | let length = v.len() as i32; 133 | let pointer = allocate(vm, length * 2)?; 134 | let mut bytes = vec![0; length as usize * 2]; 135 | for (pos, iv) in v.iter().enumerate() { 136 | let b = iv.to_le_bytes(); 137 | for i in 0..2 { 138 | bytes[pos * 2 + i] = b[i]; 139 | } 140 | } 141 | mem.write(bytes, pointer as u32)?; 142 | Ok((pointer, length)) 143 | } 144 | Param::VecU16(v) => { 145 | let length = v.len() as i32; 146 | let pointer = allocate(vm, length * 2)?; 147 | let mut bytes = vec![0; length as usize * 2]; 148 | for (pos, iv) in v.iter().enumerate() { 149 | let b = iv.to_le_bytes(); 150 | for i in 0..2 { 151 | bytes[pos * 2 + i] = b[i]; 152 | } 153 | } 154 | mem.write(bytes, pointer as u32)?; 155 | Ok((pointer, length)) 156 | } 157 | Param::VecI32(v) => { 158 | let length = v.len() as i32; 159 | let pointer = allocate(vm, length * 4)?; 160 | let mut bytes = vec![0; length as usize * 4]; 161 | for (pos, iv) in v.iter().enumerate() { 162 | let b = iv.to_le_bytes(); 163 | for i in 0..4 { 164 | bytes[pos * 4 + i] = b[i]; 165 | } 166 | } 167 | mem.write(bytes, pointer as u32)?; 168 | Ok((pointer, length)) 169 | } 170 | Param::VecU32(v) => { 171 | let length = v.len() as i32; 172 | let pointer = allocate(vm, length * 4)?; 173 | let mut bytes = vec![0; length as usize * 4]; 174 | for (pos, iv) in v.iter().enumerate() { 175 | let b = iv.to_le_bytes(); 176 | for i in 0..4 { 177 | bytes[pos * 4 + i] = b[i]; 178 | } 179 | } 180 | mem.write(bytes, pointer as u32)?; 181 | Ok((pointer, length)) 182 | } 183 | Param::VecI64(v) => { 184 | let length = v.len() as i32; 185 | let pointer = allocate(vm, length * 8)?; 186 | let mut bytes = vec![0; length as usize * 8]; 187 | for (pos, iv) in v.iter().enumerate() { 188 | let b = iv.to_le_bytes(); 189 | for i in 0..8 { 190 | bytes[pos * 8 + i] = b[i]; 191 | } 192 | } 193 | mem.write(bytes, pointer as u32)?; 194 | Ok((pointer, length)) 195 | } 196 | Param::VecU64(v) => { 197 | let length = v.len() as i32; 198 | let pointer = allocate(vm, length * 8)?; 199 | let mut bytes = vec![0; length as usize * 8]; 200 | for (pos, iv) in v.iter().enumerate() { 201 | let b = iv.to_le_bytes(); 202 | for i in 0..8 { 203 | bytes[pos * 8 + i] = b[i]; 204 | } 205 | } 206 | mem.write(bytes, pointer as u32)?; 207 | Ok((pointer, length)) 208 | } 209 | Param::String(v) => { 210 | let bytes = v.as_bytes().to_vec(); 211 | let length = bytes.len() as i32; 212 | let pointer = allocate(vm, length)?; 213 | mem.write(bytes, pointer as u32)?; 214 | Ok((pointer, length)) 215 | } 216 | } 217 | } 218 | } 219 | 220 | fn allocate(vm: &Vm, size: i32) -> WasmEdgeResult { 221 | match vm.run_func(None, "allocate", vec![WasmValue::from_i32(size)]) { 222 | Ok(rv) => Ok(rv[0].to_i32()), 223 | Err(e) => Err(e), 224 | } 225 | } 226 | 227 | #[derive(FromPrimitive)] 228 | enum RetTypes { 229 | U8 = 1, 230 | I8 = 2, 231 | U16 = 3, 232 | I16 = 4, 233 | U32 = 5, 234 | I32 = 6, 235 | U64 = 7, 236 | I64 = 8, 237 | F32 = 9, 238 | F64 = 10, 239 | Bool = 11, 240 | Char = 12, 241 | U8Array = 21, 242 | I8Array = 22, 243 | U16Array = 23, 244 | I16Array = 24, 245 | U32Array = 25, 246 | I32Array = 26, 247 | U64Array = 27, 248 | I64Array = 28, 249 | String = 31, 250 | } 251 | 252 | pub struct Bindgen { 253 | vm: Box, // Can't use Arc because vm can be get_mut after cloned for hostfunc 254 | } 255 | 256 | unsafe impl Send for Bindgen {} 257 | unsafe impl Sync for Bindgen {} 258 | 259 | impl Bindgen { 260 | pub fn new(vm: Vm) -> Self { 261 | Bindgen { vm: Box::new(vm) } 262 | } 263 | 264 | pub fn run_wasm( 265 | &mut self, 266 | func_name: impl AsRef, 267 | inputs: Vec, 268 | ) -> WasmEdgeResult>, String>> { 269 | let inputs_count = inputs.len() as i32; 270 | 271 | // allocate new frame for passing pointers 272 | let pointer_of_pointers = match self.vm.run_func( 273 | None, 274 | "allocate", 275 | vec![WasmValue::from_i32(inputs_count * 4 * 2)], 276 | ) { 277 | Ok(rv) => rv[0].to_i32(), 278 | Err(e) => { 279 | println!("allocate error: {:?}", e); 280 | return Err(e); 281 | } 282 | }; 283 | 284 | let mut memory = self.vm.active_module().unwrap().memory("memory").unwrap(); 285 | 286 | for (pos, inp) in inputs.iter().enumerate() { 287 | let sr = inp.settle(&self.vm, &mut memory); 288 | let (pointer, length_of_input) = match sr { 289 | Ok(r) => (r.0, r.1), 290 | Err(e) => { 291 | println!("run_wasm error: {}", func_name.as_ref()); 292 | return Err(e); 293 | } 294 | }; 295 | 296 | memory.write( 297 | pointer.to_le_bytes(), 298 | pointer_of_pointers as u32 + pos as u32 * 4 * 2, 299 | )?; 300 | memory.write( 301 | length_of_input.to_le_bytes(), 302 | pointer_of_pointers as u32 + pos as u32 * 4 * 2 + 4, 303 | )?; 304 | } 305 | 306 | let rets = self.vm.run_func( 307 | None, 308 | func_name, 309 | vec![ 310 | WasmValue::from_i32(pointer_of_pointers), 311 | WasmValue::from_i32(inputs_count), 312 | ], 313 | )?; 314 | // Don't need to deallocate because the memory will be loaded and free in the wasm 315 | // self.vm.run_function("deallocate", vec![WasmValue::from_i32(pointer_of_pointers), WasmValue::from_i32(inputs_count * 4 * 2)])?; 316 | 317 | if rets.len() != 1 { 318 | return Ok(Err(String::from("Invalid return value"))); 319 | } 320 | let rvec = memory.read(rets[0].to_i32() as u32, 9)?; 321 | let _ = self.vm.run_func( 322 | None, 323 | "deallocate", 324 | vec![ 325 | WasmValue::from_i32(rets[0].to_i32()), 326 | WasmValue::from_i32(9), 327 | ], 328 | ); 329 | 330 | let flag = rvec[0]; 331 | let ret_pointer = i32::from_le_bytes(rvec[1..5].try_into().unwrap()); 332 | let ret_len = i32::from_le_bytes(rvec[5..9].try_into().unwrap()); 333 | match flag { 334 | 0 => Ok(Ok(self.parse_result(ret_pointer, ret_len))), 335 | _ => Ok(Err(self.parse_error(ret_pointer, ret_len))), 336 | } 337 | } 338 | 339 | fn parse_error(&self, ret_pointer: i32, ret_len: i32) -> String { 340 | let memory = self.vm.active_module().unwrap().memory("memory").unwrap(); 341 | let err_bytes = memory.read(ret_pointer as u32, ret_len as u32).unwrap(); 342 | let _ = self.vm.run_func( 343 | None, 344 | "deallocate", 345 | vec![ 346 | WasmValue::from_i32(ret_pointer), 347 | WasmValue::from_i32(ret_len), 348 | ], 349 | ); 350 | String::from_utf8(err_bytes).unwrap_or_default() 351 | } 352 | 353 | fn parse_result(&self, ret_pointer: i32, ret_len: i32) -> Vec> { 354 | let size = ret_len as usize; 355 | let memory = self.vm.active_module().unwrap().memory("memory").unwrap(); 356 | let p_data = memory 357 | .read(ret_pointer as u32, size as u32 * 3 * 4) 358 | .unwrap(); 359 | let _ = self.vm.run_func( 360 | None, 361 | "deallocate", 362 | vec![ 363 | WasmValue::from_i32(ret_pointer), 364 | WasmValue::from_i32(size as i32 * 3 * 4), 365 | ], 366 | ); 367 | 368 | let mut p_values = vec![0; size * 3]; 369 | 370 | for i in 0..size * 3 { 371 | p_values[i] = i32::from_le_bytes(p_data[i * 4..(i + 1) * 4].try_into().unwrap()); 372 | } 373 | 374 | let mut results: Vec> = Vec::with_capacity(size); 375 | 376 | for i in 0..size { 377 | let bytes = memory 378 | .read(p_values[i * 3] as u32, p_values[i * 3 + 2] as u32) 379 | .unwrap(); 380 | let _ = self.vm.run_func( 381 | None, 382 | "deallocate", 383 | vec![ 384 | WasmValue::from_i32(p_values[i * 3]), 385 | WasmValue::from_i32(p_values[i * 3 + 2]), 386 | ], 387 | ); 388 | match FromPrimitive::from_i32(p_values[i * 3 + 1]) { 389 | Some(RetTypes::U8) => { 390 | results.push(Box::new(bytes[0])); 391 | } 392 | Some(RetTypes::I8) => { 393 | results.push(Box::new(bytes[0] as i8)); 394 | } 395 | Some(RetTypes::U16) => { 396 | let v = u16::from_le_bytes(bytes.try_into().unwrap()); 397 | results.push(Box::new(v)); 398 | } 399 | Some(RetTypes::I16) => { 400 | let v = i16::from_le_bytes(bytes.try_into().unwrap()); 401 | results.push(Box::new(v)); 402 | } 403 | Some(RetTypes::U32) => { 404 | let v = u32::from_le_bytes(bytes.try_into().unwrap()); 405 | results.push(Box::new(v)); 406 | } 407 | Some(RetTypes::I32) => { 408 | let v = i32::from_le_bytes(bytes.try_into().unwrap()); 409 | results.push(Box::new(v)); 410 | } 411 | Some(RetTypes::U64) => { 412 | let v = u64::from_le_bytes(bytes.try_into().unwrap()); 413 | results.push(Box::new(v)); 414 | } 415 | Some(RetTypes::I64) => { 416 | let v = i64::from_le_bytes(bytes.try_into().unwrap()); 417 | results.push(Box::new(v)); 418 | } 419 | Some(RetTypes::F32) => { 420 | let v = f32::from_le_bytes(bytes.try_into().unwrap()); 421 | results.push(Box::new(v)); 422 | } 423 | Some(RetTypes::F64) => { 424 | let v = f64::from_le_bytes(bytes.try_into().unwrap()); 425 | results.push(Box::new(v)); 426 | } 427 | Some(RetTypes::Bool) => { 428 | results.push(Box::new(bytes[0] == 1 as u8)); 429 | } 430 | Some(RetTypes::Char) => { 431 | let v = u32::from_le_bytes(bytes.try_into().unwrap()); 432 | results.push(Box::new(char::from_u32(v))); 433 | } 434 | Some(RetTypes::U8Array) => { 435 | let len = bytes.len(); 436 | let mut v = vec![0; len]; 437 | for i in 0..len { 438 | v[i] = bytes[i] as u8; 439 | } 440 | results.push(Box::new(v)); 441 | } 442 | Some(RetTypes::I8Array) => { 443 | let len = bytes.len(); 444 | let mut v = vec![0; len]; 445 | for i in 0..len { 446 | v[i] = bytes[i] as i8; 447 | } 448 | results.push(Box::new(v)); 449 | } 450 | Some(RetTypes::U16Array) => { 451 | let len = bytes.len() / 2; 452 | let mut v = vec![0; len]; 453 | for i in 0..len { 454 | v[i] = u16::from_le_bytes(bytes[i * 2..(i + 1) * 2].try_into().unwrap()); 455 | } 456 | results.push(Box::new(v)); 457 | } 458 | Some(RetTypes::I16Array) => { 459 | let len = bytes.len() / 2; 460 | let mut v = vec![0; len]; 461 | for i in 0..len { 462 | v[i] = i16::from_le_bytes(bytes[i * 2..(i + 1) * 2].try_into().unwrap()); 463 | } 464 | results.push(Box::new(v)); 465 | } 466 | Some(RetTypes::U32Array) => { 467 | let len = bytes.len() / 4; 468 | let mut v = vec![0; len]; 469 | for i in 0..len { 470 | v[i] = u32::from_le_bytes(bytes[i * 4..(i + 1) * 4].try_into().unwrap()); 471 | } 472 | results.push(Box::new(v)); 473 | } 474 | Some(RetTypes::I32Array) => { 475 | let len = bytes.len() / 4; 476 | let mut v = vec![0; len]; 477 | for i in 0..len { 478 | v[i] = i32::from_le_bytes(bytes[i * 4..(i + 1) * 4].try_into().unwrap()); 479 | } 480 | results.push(Box::new(v)); 481 | } 482 | Some(RetTypes::U64Array) => { 483 | let len = bytes.len() / 8; 484 | let mut v = vec![0; len]; 485 | for i in 0..len { 486 | v[i] = u64::from_le_bytes(bytes[i * 8..(i + 1) * 8].try_into().unwrap()); 487 | } 488 | results.push(Box::new(v)); 489 | } 490 | Some(RetTypes::I64Array) => { 491 | let len = bytes.len() / 8; 492 | let mut v = vec![0; len]; 493 | for i in 0..len { 494 | v[i] = i64::from_le_bytes(bytes[i * 8..(i + 1) * 8].try_into().unwrap()); 495 | } 496 | results.push(Box::new(v)); 497 | } 498 | Some(RetTypes::String) => { 499 | results.push(Box::new(String::from_utf8(bytes).unwrap())); 500 | } 501 | None => {} 502 | } 503 | } 504 | 505 | results 506 | } 507 | } 508 | -------------------------------------------------------------------------------- /bindgen/rust/macro/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate proc_macro; 2 | 3 | use proc_macro::TokenStream; 4 | use quote::{quote, ToTokens}; 5 | use syn; 6 | 7 | static mut FUNC_NUMBER: i32 = 0; 8 | 9 | enum RetTypes { 10 | U8 = 1, 11 | I8 = 2, 12 | U16 = 3, 13 | I16 = 4, 14 | U32 = 5, 15 | I32 = 6, 16 | U64 = 7, 17 | I64 = 8, 18 | F32 = 9, 19 | F64 = 10, 20 | Bool = 11, 21 | Char = 12, 22 | U8Array = 21, 23 | I8Array = 22, 24 | U16Array = 23, 25 | I16Array = 24, 26 | U32Array = 25, 27 | I32Array = 26, 28 | U64Array = 27, 29 | I64Array = 28, 30 | String = 31, 31 | } 32 | 33 | #[proc_macro_attribute] 34 | pub fn wasmedge_bindgen(_: TokenStream, item: TokenStream) -> TokenStream { 35 | let mut ast: syn::ItemFn = syn::parse(item).unwrap(); 36 | 37 | let func_ident = ast.sig.ident; 38 | 39 | let ori_run: String; 40 | unsafe { 41 | ori_run = format!("run{}", FUNC_NUMBER); 42 | FUNC_NUMBER += 1; 43 | } 44 | let ori_run_ident = proc_macro2::Ident::new(ori_run.as_str(), proc_macro2::Span::call_site()); 45 | ast.sig.ident = ori_run_ident.clone(); 46 | 47 | let (arg_names, arg_values) = parse_params(&ast); 48 | let (ret_names, ret_pointers, ret_types, ret_sizes, is_rust_result) = parse_returns(&ast); 49 | let ret_len = ret_names.len(); 50 | let ret_i = (0..ret_len).map(syn::Index::from); 51 | 52 | let params_len = arg_names.len(); 53 | let i = (0..params_len).map(syn::Index::from); 54 | 55 | let ret_result = match is_rust_result { 56 | true => quote! { 57 | match #ori_run_ident(#(#arg_names),*) { 58 | Ok((#(#ret_names),*)) => { 59 | let mut result_vec = vec![0; #ret_len * 3]; 60 | #( 61 | result_vec[#ret_i * 3 + 2] = #ret_sizes; 62 | result_vec[#ret_i * 3] = #ret_pointers; 63 | result_vec[#ret_i * 3 + 1] = #ret_types; 64 | )* 65 | let result_vec = std::mem::ManuallyDrop::new(result_vec); 66 | // return_result 67 | let mut rvec = vec![0 as u8; 9]; 68 | rvec.splice(1..5, (result_vec.as_ptr() as i32).to_le_bytes()); 69 | rvec.splice(5..9, (#ret_len as i32).to_le_bytes()); 70 | let rvec = std::mem::ManuallyDrop::new(rvec); 71 | return rvec.as_ptr() as i32; 72 | } 73 | Err(message) => { 74 | let message = std::mem::ManuallyDrop::new(message); 75 | // return_error 76 | let mut rvec = vec![1 as u8; 9]; 77 | rvec.splice(1..5, (message.as_ptr() as i32).to_le_bytes()); 78 | rvec.splice(5..9, (message.len() as i32).to_le_bytes()); 79 | let rvec = std::mem::ManuallyDrop::new(rvec); 80 | return rvec.as_ptr() as i32; 81 | } 82 | } 83 | }, 84 | false => quote! { 85 | let (#(#ret_names),*) = #ori_run_ident(#(#arg_names),*); 86 | let mut result_vec = vec![0; #ret_len * 3]; 87 | #( 88 | result_vec[#ret_i * 3 + 2] = #ret_sizes; 89 | result_vec[#ret_i * 3] = #ret_pointers; 90 | result_vec[#ret_i * 3 + 1] = #ret_types; 91 | )* 92 | let result_vec = std::mem::ManuallyDrop::new(result_vec); 93 | // return_result 94 | let mut rvec = vec![0 as u8; 9]; 95 | rvec.splice(1..5, (result_vec.as_ptr() as i32).to_le_bytes()); 96 | rvec.splice(5..9, (#ret_len as i32).to_le_bytes()); 97 | let rvec = std::mem::ManuallyDrop::new(rvec); 98 | return rvec.as_ptr() as i32; 99 | } 100 | }; 101 | 102 | let gen = quote! { 103 | 104 | #[no_mangle] 105 | pub unsafe extern "C" fn #func_ident(params_pointer: *mut u32, params_count: i32) -> i32 { 106 | if #params_len != params_count as usize { 107 | let err_msg = format!("Invalid params count, expect {}, got {}", #params_len, params_count); 108 | let err_msg = std::mem::ManuallyDrop::new(err_msg); 109 | // return_error 110 | let mut rvec = vec![1 as u8; 9]; 111 | rvec.splice(1..5, (err_msg.as_ptr() as i32).to_le_bytes()); 112 | rvec.splice(5..9, (err_msg.len() as i32).to_le_bytes()); 113 | let rvec = std::mem::ManuallyDrop::new(rvec); 114 | return rvec.as_ptr() as i32; 115 | } 116 | 117 | #( 118 | let pointer = *params_pointer.offset(#i * 2) as *mut u8; 119 | let size= *params_pointer.offset(#i * 2 + 1); 120 | let #arg_names = #arg_values; 121 | )* 122 | 123 | #ret_result; 124 | } 125 | }; 126 | 127 | let ori_run_str = ast.to_token_stream().to_string(); 128 | let x = gen.to_string() + &ori_run_str; 129 | x.parse().unwrap() 130 | } 131 | 132 | 133 | fn parse_returns(ast: &syn::ItemFn) -> (Vec::, Vec::, Vec::, Vec::, bool) { 134 | let mut ret_names = Vec::::new(); 135 | let mut ret_pointers = Vec::::new(); 136 | let mut ret_types = Vec::::new(); 137 | let mut ret_sizes = Vec::::new(); 138 | let mut is_rust_result = false; 139 | 140 | let mut prep_types = |seg: &syn::PathSegment, pos: usize| { 141 | let ret_name = quote::format_ident!("ret{}", pos.to_string()); 142 | match seg.ident.to_string().as_str() { 143 | "u8" => { 144 | ret_pointers.push(quote! {{ 145 | let x = #ret_name.to_le_bytes()[..].to_vec(); 146 | std::mem::ManuallyDrop::new(x).as_ptr() as *const u8 as i32 147 | }}); 148 | ret_names.push(ret_name); 149 | ret_types.push(RetTypes::U8 as i32); 150 | ret_sizes.push(quote! { 151 | 1 152 | }); 153 | } 154 | "i8" => { 155 | ret_pointers.push(quote! {{ 156 | let x = #ret_name.to_le_bytes()[..].to_vec(); 157 | std::mem::ManuallyDrop::new(x).as_ptr() as *const i8 as i32 158 | }}); 159 | ret_names.push(ret_name); 160 | ret_types.push(RetTypes::I8 as i32); 161 | ret_sizes.push(quote! { 162 | 1 163 | }); 164 | } 165 | "u16" => { 166 | ret_pointers.push(quote! {{ 167 | let x = #ret_name.to_le_bytes()[..].to_vec(); 168 | std::mem::ManuallyDrop::new(x).as_ptr() as *const u16 as i32 169 | }}); 170 | ret_names.push(ret_name); 171 | ret_types.push(RetTypes::U16 as i32); 172 | ret_sizes.push(quote! { 173 | 2 174 | }); 175 | } 176 | "i16" => { 177 | ret_pointers.push(quote! {{ 178 | let x = #ret_name.to_le_bytes()[..].to_vec(); 179 | std::mem::ManuallyDrop::new(x).as_ptr() as *const i16 as i32 180 | }}); 181 | ret_names.push(ret_name); 182 | ret_types.push(RetTypes::I16 as i32); 183 | ret_sizes.push(quote! { 184 | 2 185 | }); 186 | } 187 | "u32" => { 188 | ret_pointers.push(quote! {{ 189 | let x = #ret_name.to_le_bytes()[..].to_vec(); 190 | std::mem::ManuallyDrop::new(x).as_ptr() as *const u32 as i32 191 | }}); 192 | ret_names.push(ret_name); 193 | ret_types.push(RetTypes::U32 as i32); 194 | ret_sizes.push(quote! { 195 | 4 196 | }); 197 | } 198 | "i32" => { 199 | ret_pointers.push(quote! {{ 200 | let x = #ret_name.to_le_bytes()[..].to_vec(); 201 | std::mem::ManuallyDrop::new(x).as_ptr() as *const i32 as i32 202 | }}); 203 | ret_names.push(ret_name); 204 | ret_types.push(RetTypes::I32 as i32); 205 | ret_sizes.push(quote! { 206 | 4 207 | }); 208 | } 209 | "u64" => { 210 | ret_pointers.push(quote! {{ 211 | let x = #ret_name.to_le_bytes()[..].to_vec(); 212 | std::mem::ManuallyDrop::new(x).as_ptr() as *const u64 as i32 213 | }}); 214 | ret_names.push(ret_name); 215 | ret_types.push(RetTypes::U64 as i32); 216 | ret_sizes.push(quote! { 217 | 8 218 | }); 219 | } 220 | "i64" => { 221 | ret_pointers.push(quote! {{ 222 | let x = #ret_name.to_le_bytes()[..].to_vec(); 223 | std::mem::ManuallyDrop::new(x).as_ptr() as *const i64 as i32 224 | }}); 225 | ret_names.push(ret_name); 226 | ret_types.push(RetTypes::I64 as i32); 227 | ret_sizes.push(quote! { 228 | 8 229 | }); 230 | } 231 | "f32" => { 232 | ret_pointers.push(quote! {{ 233 | let x = #ret_name.to_le_bytes()[..].to_vec(); 234 | std::mem::ManuallyDrop::new(x).as_ptr() as *const f32 as i32 235 | }}); 236 | ret_names.push(ret_name); 237 | ret_types.push(RetTypes::F32 as i32); 238 | ret_sizes.push(quote! { 239 | 4 240 | }); 241 | } 242 | "f64" => { 243 | ret_pointers.push(quote! {{ 244 | let x = #ret_name.to_le_bytes()[..].to_vec(); 245 | std::mem::ManuallyDrop::new(x).as_ptr() as *const f64 as i32 246 | }}); 247 | ret_names.push(ret_name); 248 | ret_types.push(RetTypes::F64 as i32); 249 | ret_sizes.push(quote! { 250 | 8 251 | }); 252 | } 253 | "bool" => { 254 | ret_pointers.push(quote! {{ 255 | let x = #ret_name.to_le_bytes()[..].to_vec(); 256 | std::mem::ManuallyDrop::new(x).as_ptr() as *const bool as i32 257 | }}); 258 | ret_names.push(ret_name); 259 | ret_types.push(RetTypes::Bool as i32); 260 | ret_sizes.push(quote! { 261 | 1 262 | }); 263 | } 264 | "char" => { 265 | ret_pointers.push(quote! {{ 266 | let x = #ret_name.to_le_bytes()[..].to_vec(); 267 | std::mem::ManuallyDrop::new(x).as_ptr() as *const char as i32 268 | }}); 269 | ret_names.push(ret_name); 270 | ret_types.push(RetTypes::Char as i32); 271 | ret_sizes.push(quote! { 272 | 4 273 | }); 274 | } 275 | "String" => { 276 | ret_pointers.push(quote! { 277 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 278 | }); 279 | ret_types.push(RetTypes::String as i32); 280 | ret_sizes.push(quote! { 281 | #ret_name.len() as i32 282 | }); 283 | ret_names.push(ret_name); 284 | } 285 | "Vec" => { 286 | match &seg.arguments { 287 | syn::PathArguments::AngleBracketed(args) => { 288 | match args.args.first().unwrap() { 289 | syn::GenericArgument::Type(arg_type) => { 290 | match arg_type { 291 | syn::Type::Path(arg_type_path) => { 292 | let arg_seg = arg_type_path.path.segments.first().unwrap(); 293 | match arg_seg.ident.to_string().as_str() { 294 | "u8" => { 295 | ret_pointers.push(quote! { 296 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 297 | }); 298 | ret_sizes.push(quote! { 299 | #ret_name.len() as i32 300 | }); 301 | ret_types.push(RetTypes::U8Array as i32); 302 | ret_names.push(ret_name); 303 | } 304 | "i8" => { 305 | ret_pointers.push(quote! { 306 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 307 | }); 308 | ret_sizes.push(quote! { 309 | #ret_name.len() as i32 310 | }); 311 | ret_types.push(RetTypes::I8Array as i32); 312 | ret_names.push(ret_name); 313 | } 314 | "u16" => { 315 | ret_pointers.push(quote! { 316 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 317 | }); 318 | ret_sizes.push(quote! { 319 | #ret_name.len() as i32 * 2 320 | }); 321 | ret_types.push(RetTypes::U16Array as i32); 322 | ret_names.push(ret_name); 323 | } 324 | "i16" => { 325 | ret_pointers.push(quote! { 326 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 327 | }); 328 | ret_sizes.push(quote! { 329 | #ret_name.len() as i32 * 2 330 | }); 331 | ret_types.push(RetTypes::I16Array as i32); 332 | ret_names.push(ret_name); 333 | } 334 | "u32" => { 335 | ret_pointers.push(quote! { 336 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 337 | }); 338 | ret_sizes.push(quote! { 339 | #ret_name.len() as i32 * 4 340 | }); 341 | ret_types.push(RetTypes::U32Array as i32); 342 | ret_names.push(ret_name); 343 | } 344 | "i32" => { 345 | ret_pointers.push(quote! { 346 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 347 | }); 348 | ret_sizes.push(quote! { 349 | #ret_name.len() as i32 * 4 350 | }); 351 | ret_types.push(RetTypes::I32Array as i32); 352 | ret_names.push(ret_name); 353 | } 354 | "u64" => { 355 | ret_pointers.push(quote! { 356 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 357 | }); 358 | ret_sizes.push(quote! { 359 | #ret_name.len() as i32 * 8 360 | }); 361 | ret_types.push(RetTypes::U64Array as i32); 362 | ret_names.push(ret_name); 363 | } 364 | "i64" => { 365 | ret_pointers.push(quote! { 366 | std::mem::ManuallyDrop::new(#ret_name).as_ptr() as i32 367 | }); 368 | ret_sizes.push(quote! { 369 | #ret_name.len() as i32 * 8 370 | }); 371 | ret_types.push(RetTypes::I64Array as i32); 372 | ret_names.push(ret_name); 373 | } 374 | _ => {} 375 | } 376 | } 377 | _ => {} 378 | } 379 | } 380 | _ => {} 381 | } 382 | } 383 | _ => {} 384 | } 385 | } 386 | _ => {} 387 | } 388 | }; 389 | 390 | match ast.sig.output { 391 | syn::ReturnType::Type(_, ref rt) => { 392 | match &**rt { 393 | syn::Type::Path(type_path) => { 394 | let seg = &type_path.path.segments.first().unwrap(); 395 | let seg_type = seg.ident.to_string(); 396 | if seg_type == "Result" { 397 | is_rust_result = true; 398 | match &seg.arguments { 399 | syn::PathArguments::AngleBracketed(args) => { 400 | match args.args.first().unwrap() { 401 | syn::GenericArgument::Type(arg_type) => { 402 | match arg_type { 403 | syn::Type::Path(arg_type_path) => { 404 | let arg_seg = arg_type_path.path.segments.first().unwrap(); 405 | prep_types(&arg_seg, 0) 406 | } 407 | syn::Type::Tuple(arg_type_tuple) => { 408 | for (pos, elem) in arg_type_tuple.elems.iter().enumerate() { 409 | match elem { 410 | syn::Type::Path(type_path) => { 411 | let seg = &type_path.path.segments.first().unwrap(); 412 | prep_types(&seg, pos); 413 | } 414 | _ => {} 415 | } 416 | } 417 | } 418 | _ => {} 419 | } 420 | } 421 | _ => {} 422 | } 423 | } 424 | _ => {} 425 | } 426 | } else { 427 | prep_types(&seg, 0); 428 | } 429 | } 430 | syn::Type::Tuple(type_tuple) => { 431 | for (pos, elem) in type_tuple.elems.iter().enumerate() { 432 | match elem { 433 | syn::Type::Path(type_path) => { 434 | let seg = &type_path.path.segments.first().unwrap(); 435 | prep_types(&seg, pos); 436 | } 437 | _ => {} 438 | } 439 | } 440 | } 441 | _ => {} 442 | } 443 | } 444 | _ => {} 445 | } 446 | 447 | (ret_names, ret_pointers, ret_types, ret_sizes, is_rust_result) 448 | } 449 | 450 | fn parse_params(ast: &syn::ItemFn) -> (Vec::, Vec::) { 451 | let mut arg_names = Vec::::new(); 452 | let mut arg_values = Vec::::new(); 453 | 454 | let params_iter = ast.sig.inputs.iter(); 455 | for (pos, param) in params_iter.enumerate() { 456 | match param { 457 | syn::FnArg::Typed(param_type) => { 458 | match &*param_type.ty { 459 | syn::Type::Path(type_path) => { 460 | let seg = &type_path.path.segments.first().unwrap(); 461 | match seg.ident.to_string().as_str() { 462 | "Vec" => { 463 | match &seg.arguments { 464 | syn::PathArguments::AngleBracketed(args) => { 465 | match args.args.first().unwrap() { 466 | syn::GenericArgument::Type(arg_type) => { 467 | match arg_type { 468 | syn::Type::Path(arg_type_path) => { 469 | let arg_seg = arg_type_path.path.segments.first().unwrap(); 470 | match arg_seg.ident.to_string().as_str() { 471 | "u8" => { 472 | arg_names.push(quote::format_ident!("arg{}", pos)); 473 | arg_values.push(quote! { 474 | Vec::from_raw_parts(pointer, size as usize, size as usize) 475 | }) 476 | } 477 | "i8" => { 478 | arg_names.push(quote::format_ident!("arg{}", pos)); 479 | arg_values.push(quote! { 480 | Vec::from_raw_parts(pointer as *mut i8, size as usize, size as usize) 481 | }) 482 | } 483 | "u16" => { 484 | arg_names.push(quote::format_ident!("arg{}", pos)); 485 | arg_values.push(quote! { 486 | Vec::from_raw_parts(pointer as *mut u16, size as usize, size as usize) 487 | }) 488 | } 489 | "i16" => { 490 | arg_names.push(quote::format_ident!("arg{}", pos)); 491 | arg_values.push(quote! { 492 | Vec::from_raw_parts(pointer as *mut i16, size as usize, size as usize) 493 | }) 494 | } 495 | "u32" => { 496 | arg_names.push(quote::format_ident!("arg{}", pos)); 497 | arg_values.push(quote! { 498 | Vec::from_raw_parts(pointer as *mut u32, size as usize, size as usize) 499 | }) 500 | } 501 | "i32" => { 502 | arg_names.push(quote::format_ident!("arg{}", pos)); 503 | arg_values.push(quote! { 504 | Vec::from_raw_parts(pointer as *mut i32, size as usize, size as usize) 505 | }) 506 | } 507 | "u64" => { 508 | arg_names.push(quote::format_ident!("arg{}", pos)); 509 | arg_values.push(quote! { 510 | Vec::from_raw_parts(pointer as *mut u64, size as usize, size as usize) 511 | }) 512 | } 513 | "i64" => { 514 | arg_names.push(quote::format_ident!("arg{}", pos)); 515 | arg_values.push(quote! { 516 | Vec::from_raw_parts(pointer as *mut i64, size as usize, size as usize) 517 | }) 518 | } 519 | _ => {} 520 | } 521 | } 522 | _ => {} 523 | } 524 | } 525 | _ => {} 526 | } 527 | } 528 | _ => {} 529 | } 530 | } 531 | "bool" => { 532 | arg_names.push(quote::format_ident!("arg{}", pos)); 533 | arg_values.push(quote! { 534 | Vec::from_raw_parts(pointer as *mut bool, size as usize, size as usize)[0] 535 | }) 536 | } 537 | "char" => { 538 | arg_names.push(quote::format_ident!("arg{}", pos)); 539 | arg_values.push(quote! { 540 | Vec::from_raw_parts(pointer as *mut char, size as usize, size as usize)[0] 541 | }) 542 | } 543 | "i8" => { 544 | arg_names.push(quote::format_ident!("arg{}", pos)); 545 | arg_values.push(quote! { 546 | Vec::from_raw_parts(pointer as *mut i8, size as usize, size as usize)[0] 547 | }) 548 | } 549 | "u8" => { 550 | arg_names.push(quote::format_ident!("arg{}", pos)); 551 | arg_values.push(quote! { 552 | Vec::from_raw_parts(pointer as *mut u8, size as usize, size as usize)[0] 553 | }) 554 | } 555 | "i16" => { 556 | arg_names.push(quote::format_ident!("arg{}", pos)); 557 | arg_values.push(quote! { 558 | Vec::from_raw_parts(pointer as *mut i16, size as usize, size as usize)[0] 559 | }) 560 | } 561 | "u16" => { 562 | arg_names.push(quote::format_ident!("arg{}", pos)); 563 | arg_values.push(quote! { 564 | Vec::from_raw_parts(pointer as *mut u16, size as usize, size as usize)[0] 565 | }) 566 | } 567 | "i32" => { 568 | arg_names.push(quote::format_ident!("arg{}", pos)); 569 | arg_values.push(quote! { 570 | Vec::from_raw_parts(pointer as *mut i32, size as usize, size as usize)[0] 571 | }) 572 | } 573 | "u32" => { 574 | arg_names.push(quote::format_ident!("arg{}", pos)); 575 | arg_values.push(quote! { 576 | Vec::from_raw_parts(pointer as *mut u32, size as usize, size as usize)[0] 577 | }) 578 | } 579 | "i64" => { 580 | arg_names.push(quote::format_ident!("arg{}", pos)); 581 | arg_values.push(quote! { 582 | Vec::from_raw_parts(pointer as *mut i64, size as usize, size as usize)[0] 583 | }) 584 | } 585 | "u64" => { 586 | arg_names.push(quote::format_ident!("arg{}", pos)); 587 | arg_values.push(quote! { 588 | Vec::from_raw_parts(pointer as *mut u64, size as usize, size as usize)[0] 589 | }) 590 | } 591 | "f32" => { 592 | arg_names.push(quote::format_ident!("arg{}", pos)); 593 | arg_values.push(quote! { 594 | Vec::from_raw_parts(pointer as *mut f32, size as usize, size as usize)[0] 595 | }) 596 | } 597 | "f64" => { 598 | arg_names.push(quote::format_ident!("arg{}", pos)); 599 | arg_values.push(quote! { 600 | Vec::from_raw_parts(pointer as *mut f64, size as usize, size as usize)[0] 601 | }) 602 | } 603 | "String" => { 604 | arg_names.push(quote::format_ident!("arg{}", pos)); 605 | arg_values.push(quote! { 606 | std::str::from_utf8(&Vec::from_raw_parts(pointer, size as usize, size as usize)).unwrap().to_string() 607 | }) 608 | } 609 | _ => {} 610 | } 611 | } 612 | syn::Type::Reference(_) => { 613 | 614 | } 615 | syn::Type::Slice(_) => { 616 | 617 | } 618 | _ => {} 619 | } 620 | } 621 | _ => {} 622 | } 623 | } 624 | 625 | (arg_names, arg_values) 626 | } -------------------------------------------------------------------------------- /host/go/bindgen.go: -------------------------------------------------------------------------------- 1 | package bindgen 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "errors" 7 | "fmt" 8 | "math" 9 | 10 | "github.com/second-state/WasmEdge-go/wasmedge" 11 | ) 12 | 13 | const ( 14 | U8 int32 = 1 15 | I8 = 2 16 | U16 = 3 17 | I16 = 4 18 | U32 = 5 19 | I32 = 6 20 | U64 = 7 21 | I64 = 8 22 | F32 = 9 23 | F64 = 10 24 | Bool = 11 25 | Rune = 12 26 | ByteArray = 21 27 | I8Array = 22 28 | U16Array = 23 29 | I16Array = 24 30 | U32Array = 25 31 | I32Array = 26 32 | U64Array = 27 33 | I64Array = 28 34 | String = 31 35 | ) 36 | 37 | type Bindgen struct { 38 | vm *wasmedge.VM 39 | 40 | funcImports *wasmedge.Module 41 | } 42 | 43 | func New(vm *wasmedge.VM) *Bindgen { 44 | b := &Bindgen { 45 | vm: vm, 46 | } 47 | 48 | return b 49 | } 50 | 51 | func (b *Bindgen) Instantiate() { 52 | b.vm.Instantiate() 53 | } 54 | 55 | func (b *Bindgen) GetVm() *wasmedge.VM { 56 | return b.vm 57 | } 58 | 59 | func (b *Bindgen) Execute(funcName string, inputs... interface{}) ([]interface{}, interface{}, error) { 60 | inputsCount := len(inputs) 61 | 62 | // allocate new frame for passing pointers 63 | allocateResult, err := b.vm.Execute("allocate", int32(inputsCount * 4 * 2)) 64 | if err != nil { 65 | return nil, nil, err 66 | } 67 | pointerOfPointers := allocateResult[0].(int32) 68 | // Don't need to deallocate because the memory will be loaded and free in the wasm 69 | // defer b.vm.Execute("deallocate", pointerOfPointers, int32(inputsCount * 4 * 2)) 70 | 71 | memory := b.vm.GetActiveModule().FindMemory("memory") 72 | if memory == nil { 73 | return nil, nil, errors.New("Memory not found") 74 | } 75 | 76 | for idx, inp := range inputs { 77 | var pointer, lengthOfInput int32 78 | var err error 79 | switch input := inp.(type) { 80 | case []byte: 81 | pointer, lengthOfInput, err = b.settleByteSlice(memory, input) 82 | case []int8: 83 | pointer, lengthOfInput, err = b.settleI8Slice(memory, input) 84 | case []uint16: 85 | pointer, lengthOfInput, err = b.settleU16Slice(memory, input) 86 | case []int16: 87 | pointer, lengthOfInput, err = b.settleI16Slice(memory, input) 88 | case []uint32: 89 | pointer, lengthOfInput, err = b.settleU32Slice(memory, input) 90 | case []int32: 91 | pointer, lengthOfInput, err = b.settleI32Slice(memory, input) 92 | case []uint64: 93 | pointer, lengthOfInput, err = b.settleU64Slice(memory, input) 94 | case []int64: 95 | pointer, lengthOfInput, err = b.settleI64Slice(memory, input) 96 | case bool: 97 | pointer, lengthOfInput, err = b.settleBool(memory, input) 98 | case int8: 99 | pointer, lengthOfInput, err = b.settleI8(memory, input) 100 | case uint8: 101 | pointer, lengthOfInput, err = b.settleU8(memory, input) 102 | case int16: 103 | pointer, lengthOfInput, err = b.settleI16(memory, input) 104 | case uint16: 105 | pointer, lengthOfInput, err = b.settleU16(memory, input) 106 | case int32: 107 | pointer, lengthOfInput, err = b.settleI32(memory, input) 108 | case uint32: 109 | pointer, lengthOfInput, err = b.settleU32(memory, input) 110 | case int64: 111 | pointer, lengthOfInput, err = b.settleI64(memory, input) 112 | case uint64: 113 | pointer, lengthOfInput, err = b.settleU64(memory, input) 114 | case float32: 115 | pointer, lengthOfInput, err = b.settleF32(memory, input) 116 | case float64: 117 | pointer, lengthOfInput, err = b.settleF64(memory, input) 118 | case string: 119 | pointer, lengthOfInput, err = b.settleString(memory, input) 120 | default: 121 | return nil, nil, errors.New(fmt.Sprintf("Unsupported arg type %T", input)) 122 | } 123 | if err != nil { 124 | return nil, nil, err 125 | } 126 | b.putPointerOfPointer(pointerOfPointers, memory, idx, pointer, lengthOfInput) 127 | } 128 | 129 | var rets = make([]interface{}, 0); 130 | if rets, err = b.vm.Execute(funcName, pointerOfPointers, int32(inputsCount)); err != nil { 131 | return nil, nil, err 132 | } 133 | 134 | if len(rets) != 1 { 135 | return nil, nil, errors.New("Invalid return value") 136 | } 137 | 138 | revc, err := memory.GetData(uint(rets[0].(int32)), 9) 139 | if err != nil { 140 | return nil, nil, err 141 | } 142 | flag := revc[0] 143 | retPointer := b.getI32(revc[1:5]) 144 | retLen := b.getI32(revc[5:9]) 145 | 146 | if flag == 0 { 147 | return b.parse_result(retPointer, retLen); 148 | } else { 149 | return b.parse_error(retPointer, retLen); 150 | } 151 | } 152 | 153 | func (b *Bindgen) Release() { 154 | b.vm.Release() 155 | } 156 | 157 | 158 | func (b *Bindgen) parse_result(pointer int32, size int32) ([]interface{}, interface{}, error) { 159 | memory := b.vm.GetActiveModule().FindMemory("memory") 160 | if memory == nil { 161 | return nil, nil, errors.New("Can't get memory object") 162 | } 163 | 164 | data, err := memory.GetData(uint(pointer), uint(size) * 3 * 4) 165 | if err != nil { 166 | return nil, nil, err 167 | } 168 | 169 | rets := make([]int32, size * 3) 170 | 171 | for i := 0; i < int(size * 3); i++ { 172 | buf := bytes.NewBuffer(data[i*4:(i+1)*4]) 173 | var p int32 174 | binary.Read(buf, binary.LittleEndian, &p) 175 | rets[i] = p 176 | } 177 | 178 | result := make([]interface{}, size) 179 | for i := 0; i < int(size); i++ { 180 | bytes, err := memory.GetData(uint(rets[i * 3]), uint(rets[i * 3 + 2])) 181 | if err != nil { 182 | return nil, nil, err 183 | } 184 | switch rets[i * 3 + 1] { 185 | case U8: 186 | result[i] = interface{}(b.getU8(bytes)) 187 | case I8: 188 | result[i] = interface{}(b.getI8(bytes)) 189 | case U16: 190 | result[i] = interface{}(b.getU16(bytes)) 191 | case I16: 192 | result[i] = interface{}(b.getI16(bytes)) 193 | case U32: 194 | result[i] = interface{}(b.getU32(bytes)) 195 | case I32: 196 | result[i] = interface{}(b.getI32(bytes)) 197 | case U64: 198 | result[i] = interface{}(b.getU64(bytes)) 199 | case I64: 200 | result[i] = interface{}(b.getI64(bytes)) 201 | case F32: 202 | result[i] = interface{}(b.getF32(bytes)) 203 | case F64: 204 | result[i] = interface{}(b.getF64(bytes)) 205 | case Bool: 206 | result[i] = interface{}(b.getBool(bytes)) 207 | case Rune: 208 | result[i] = interface{}(b.getRune(bytes)) 209 | case String: 210 | result[i] = interface{}(b.getString(bytes)) 211 | case ByteArray: 212 | result[i] = interface{}(b.getByteSlice(bytes)) 213 | case I8Array: 214 | result[i] = interface{}(b.getI8Slice(bytes)) 215 | case U16Array: 216 | result[i] = interface{}(b.getU16Slice(bytes)) 217 | case I16Array: 218 | result[i] = interface{}(b.getI16Slice(bytes)) 219 | case U32Array: 220 | result[i] = interface{}(b.getU32Slice(bytes)) 221 | case I32Array: 222 | result[i] = interface{}(b.getI32Slice(bytes)) 223 | case U64Array: 224 | result[i] = interface{}(b.getU64Slice(bytes)) 225 | case I64Array: 226 | result[i] = interface{}(b.getI64Slice(bytes)) 227 | } 228 | } 229 | 230 | return result, nil, nil 231 | } 232 | 233 | func (b *Bindgen) parse_error(pointer int32, size int32) ([]interface{}, interface{}, error) { 234 | memory := b.vm.GetActiveModule().FindMemory("memory") 235 | if memory == nil { 236 | return nil, nil, errors.New("Can't get memory object") 237 | } 238 | 239 | data, err := memory.GetData(uint(pointer), uint(size)) 240 | if err != nil { 241 | return nil, nil, err 242 | } 243 | 244 | return nil, string(data), nil 245 | } 246 | 247 | func (b *Bindgen) putPointerOfPointer(pointerOfPointers int32, memory *wasmedge.Memory, inputIdx int, pointer int32, lengthOfInput int32) { 248 | // set data for pointer of pointer 249 | pointerBytes := make([]byte, 4) 250 | binary.LittleEndian.PutUint32(pointerBytes, uint32(pointer)) 251 | memory.SetData(pointerBytes, uint(pointerOfPointers) + uint(inputIdx * 4 * 2), uint(4)) 252 | lengthBytes := make([]byte, 4) 253 | binary.LittleEndian.PutUint32(lengthBytes, uint32(lengthOfInput)) 254 | memory.SetData(lengthBytes, uint(pointerOfPointers) + uint(inputIdx * 4 * 2 + 4), uint(4)) 255 | } 256 | 257 | func (b *Bindgen) settleByteSlice(memory *wasmedge.Memory, input []byte) (int32, int32, error) { 258 | lengthOfInput := int32(len(input)) 259 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 260 | if err != nil { 261 | return 0, 0, err 262 | } 263 | pointer := allocateResult[0].(int32) 264 | 265 | memory.SetData(input, uint(pointer), uint(lengthOfInput)) 266 | 267 | return pointer, lengthOfInput, nil 268 | } 269 | 270 | func (b *Bindgen) settleI8Slice(memory *wasmedge.Memory, input []int8) (int32, int32, error) { 271 | lengthOfInput := int32(len(input)) 272 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 273 | if err != nil { 274 | return 0, 0, err 275 | } 276 | pointer := allocateResult[0].(int32) 277 | 278 | data := make([]byte, lengthOfInput) 279 | for i := 0; i < int(lengthOfInput); i++ { 280 | data[i] = byte(input[i]) 281 | } 282 | 283 | memory.SetData(data, uint(pointer), uint(lengthOfInput)) 284 | 285 | return pointer, lengthOfInput, nil 286 | } 287 | 288 | func (b *Bindgen) settleU16Slice(memory *wasmedge.Memory, input []uint16) (int32, int32, error) { 289 | lengthOfInput := int32(len(input)) 290 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 2) 291 | if err != nil { 292 | return 0, 0, err 293 | } 294 | pointer := allocateResult[0].(int32) 295 | 296 | data := make([]byte, lengthOfInput * 2) 297 | for i := 0; i < int(lengthOfInput); i++ { 298 | binary.LittleEndian.PutUint16(data[i*2:(i+1)*2], input[i]) 299 | } 300 | 301 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 2)) 302 | 303 | return pointer, lengthOfInput, nil 304 | } 305 | 306 | func (b *Bindgen) settleI16Slice(memory *wasmedge.Memory, input []int16) (int32, int32, error) { 307 | lengthOfInput := int32(len(input)) 308 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 2) 309 | if err != nil { 310 | return 0, 0, err 311 | } 312 | pointer := allocateResult[0].(int32) 313 | 314 | data := make([]byte, lengthOfInput * 2) 315 | for i := 0; i < int(lengthOfInput); i++ { 316 | binary.LittleEndian.PutUint16(data[i*2:(i+1)*2], uint16(input[i])) 317 | } 318 | 319 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 2)) 320 | 321 | return pointer, lengthOfInput, nil 322 | } 323 | 324 | func (b *Bindgen) settleU32Slice(memory *wasmedge.Memory, input []uint32) (int32, int32, error) { 325 | lengthOfInput := int32(len(input)) 326 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 4) 327 | if err != nil { 328 | return 0, 0, err 329 | } 330 | pointer := allocateResult[0].(int32) 331 | 332 | data := make([]byte, lengthOfInput * 4) 333 | for i := 0; i < int(lengthOfInput); i++ { 334 | binary.LittleEndian.PutUint32(data[i*4:(i+1)*4], input[i]) 335 | } 336 | 337 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 4)) 338 | 339 | return pointer, lengthOfInput, nil 340 | } 341 | 342 | func (b *Bindgen) settleI32Slice(memory *wasmedge.Memory, input []int32) (int32, int32, error) { 343 | lengthOfInput := int32(len(input)) 344 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 4) 345 | if err != nil { 346 | return 0, 0, err 347 | } 348 | pointer := allocateResult[0].(int32) 349 | 350 | data := make([]byte, lengthOfInput * 4) 351 | for i := 0; i < int(lengthOfInput); i++ { 352 | binary.LittleEndian.PutUint32(data[i*4:(i+1)*4], uint32(input[i])) 353 | } 354 | 355 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 4)) 356 | 357 | return pointer, lengthOfInput, nil 358 | } 359 | 360 | func (b *Bindgen) settleU64Slice(memory *wasmedge.Memory, input []uint64) (int32, int32, error) { 361 | lengthOfInput := int32(len(input)) 362 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 8) 363 | if err != nil { 364 | return 0, 0, err 365 | } 366 | pointer := allocateResult[0].(int32) 367 | 368 | data := make([]byte, lengthOfInput * 8) 369 | for i := 0; i < int(lengthOfInput); i++ { 370 | binary.LittleEndian.PutUint64(data[i*8:(i+1)*8], input[i]) 371 | } 372 | 373 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 8)) 374 | 375 | return pointer, lengthOfInput, nil 376 | } 377 | 378 | func (b *Bindgen) settleI64Slice(memory *wasmedge.Memory, input []int64) (int32, int32, error) { 379 | lengthOfInput := int32(len(input)) 380 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 8) 381 | if err != nil { 382 | return 0, 0, err 383 | } 384 | pointer := allocateResult[0].(int32) 385 | 386 | data := make([]byte, lengthOfInput * 8) 387 | for i := 0; i < int(lengthOfInput); i++ { 388 | binary.LittleEndian.PutUint64(data[i*8:(i+1)*8], uint64(input[i])) 389 | } 390 | 391 | memory.SetData(data, uint(pointer), uint(lengthOfInput * 8)) 392 | 393 | return pointer, lengthOfInput, nil 394 | } 395 | 396 | func (b *Bindgen) settleBool(memory *wasmedge.Memory, input bool) (int32, int32, error) { 397 | lengthOfInput := int32(1) 398 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 399 | if err != nil { 400 | return 0, 0, err 401 | } 402 | pointer := allocateResult[0].(int32) 403 | 404 | var bt byte = 0 405 | if input { 406 | bt = 1 407 | } 408 | memory.SetData([]byte{bt}, uint(pointer), uint(lengthOfInput)) 409 | 410 | return pointer, lengthOfInput, nil 411 | } 412 | 413 | func (b *Bindgen) settleRune(memory *wasmedge.Memory, input rune) (int32, int32, error) { 414 | lengthOfInput := int32(4) 415 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 416 | if err != nil { 417 | return 0, 0, err 418 | } 419 | pointer := allocateResult[0].(int32) 420 | 421 | bytes := make([]byte, 4) 422 | binary.LittleEndian.PutUint32(bytes, uint32(input)) 423 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput)) 424 | 425 | return pointer, lengthOfInput, nil 426 | } 427 | 428 | func (b *Bindgen) settleI8(memory *wasmedge.Memory, input int8) (int32, int32, error) { 429 | lengthOfInput := int32(1) 430 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 431 | if err != nil { 432 | return 0, 0, err 433 | } 434 | pointer := allocateResult[0].(int32) 435 | 436 | memory.SetData([]byte{byte(input)}, uint(pointer), uint(lengthOfInput)) 437 | 438 | return pointer, lengthOfInput, nil 439 | } 440 | 441 | func (b *Bindgen) settleU8(memory *wasmedge.Memory, input uint8) (int32, int32, error) { 442 | lengthOfInput := int32(1) 443 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 444 | if err != nil { 445 | return 0, 0, err 446 | } 447 | pointer := allocateResult[0].(int32) 448 | 449 | memory.SetData([]byte{byte(input)}, uint(pointer), uint(lengthOfInput)) 450 | 451 | return pointer, lengthOfInput, nil 452 | } 453 | 454 | func (b *Bindgen) settleI16(memory *wasmedge.Memory, input int16) (int32, int32, error) { 455 | lengthOfInput := int32(1) 456 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 2) 457 | if err != nil { 458 | return 0, 0, err 459 | } 460 | pointer := allocateResult[0].(int32) 461 | 462 | bytes := make([]byte, 2) 463 | binary.LittleEndian.PutUint16(bytes, uint16(input)) 464 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 2)) 465 | 466 | return pointer, lengthOfInput, nil 467 | } 468 | 469 | func (b *Bindgen) settleU16(memory *wasmedge.Memory, input uint16) (int32, int32, error) { 470 | lengthOfInput := int32(1) 471 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 2) 472 | if err != nil { 473 | return 0, 0, err 474 | } 475 | pointer := allocateResult[0].(int32) 476 | 477 | bytes := make([]byte, 2) 478 | binary.LittleEndian.PutUint16(bytes, input) 479 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 2)) 480 | 481 | return pointer, lengthOfInput, nil 482 | } 483 | 484 | func (b *Bindgen) settleI32(memory *wasmedge.Memory, input int32) (int32, int32, error) { 485 | lengthOfInput := int32(1) 486 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 4) 487 | if err != nil { 488 | return 0, 0, err 489 | } 490 | pointer := allocateResult[0].(int32) 491 | 492 | bytes := make([]byte, 4) 493 | binary.LittleEndian.PutUint32(bytes, uint32(input)) 494 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 4)) 495 | 496 | return pointer, lengthOfInput, nil 497 | } 498 | 499 | func (b *Bindgen) settleU32(memory *wasmedge.Memory, input uint32) (int32, int32, error) { 500 | lengthOfInput := int32(1) 501 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 4) 502 | if err != nil { 503 | return 0, 0, err 504 | } 505 | pointer := allocateResult[0].(int32) 506 | 507 | bytes := make([]byte, 4) 508 | binary.LittleEndian.PutUint32(bytes, input) 509 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 4)) 510 | 511 | return pointer, lengthOfInput, nil 512 | } 513 | 514 | func (b *Bindgen) settleI64(memory *wasmedge.Memory, input int64) (int32, int32, error) { 515 | lengthOfInput := int32(1) 516 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 8) 517 | if err != nil { 518 | return 0, 0, err 519 | } 520 | pointer := allocateResult[0].(int32) 521 | 522 | bytes := make([]byte, 8) 523 | binary.LittleEndian.PutUint64(bytes, uint64(input)) 524 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 8)) 525 | 526 | return pointer, lengthOfInput, nil 527 | } 528 | 529 | func (b *Bindgen) settleU64(memory *wasmedge.Memory, input uint64) (int32, int32, error) { 530 | lengthOfInput := int32(1) 531 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 8) 532 | if err != nil { 533 | return 0, 0, err 534 | } 535 | pointer := allocateResult[0].(int32) 536 | 537 | bytes := make([]byte, 8) 538 | binary.LittleEndian.PutUint64(bytes, input) 539 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 8)) 540 | 541 | return pointer, lengthOfInput, nil 542 | } 543 | 544 | func (b *Bindgen) settleF32(memory *wasmedge.Memory, input float32) (int32, int32, error) { 545 | lengthOfInput := int32(1) 546 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 4) 547 | if err != nil { 548 | return 0, 0, err 549 | } 550 | pointer := allocateResult[0].(int32) 551 | 552 | bytes := make([]byte, 4) 553 | binary.LittleEndian.PutUint32(bytes, math.Float32bits(input)) 554 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 4)) 555 | 556 | return pointer, lengthOfInput, nil 557 | } 558 | 559 | func (b *Bindgen) settleF64(memory *wasmedge.Memory, input float64) (int32, int32, error) { 560 | lengthOfInput := int32(1) 561 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput * 8) 562 | if err != nil { 563 | return 0, 0, err 564 | } 565 | pointer := allocateResult[0].(int32) 566 | 567 | bytes := make([]byte, 8) 568 | binary.LittleEndian.PutUint64(bytes, math.Float64bits(input)) 569 | memory.SetData(bytes, uint(pointer), uint(lengthOfInput * 8)) 570 | 571 | return pointer, lengthOfInput, nil 572 | } 573 | 574 | func (b *Bindgen) settleString(memory *wasmedge.Memory, input string) (int32, int32, error) { 575 | lengthOfInput := int32(len([]byte(input))) 576 | allocateResult, err := b.vm.Execute("allocate", lengthOfInput) 577 | if err != nil { 578 | return 0, 0, err 579 | } 580 | pointer := allocateResult[0].(int32) 581 | 582 | memory.SetData([]byte(input), uint(pointer), uint(lengthOfInput)) 583 | 584 | return pointer, lengthOfInput, nil 585 | } 586 | 587 | 588 | 589 | func (b *Bindgen) getU8(d []byte) uint8 { 590 | return uint8(d[0]) 591 | } 592 | 593 | func (b *Bindgen) getI8(d []byte) int8 { 594 | return int8(d[0]) 595 | } 596 | 597 | func (b *Bindgen) getU16(d []byte) (r uint16) { 598 | buf := bytes.NewBuffer(d) 599 | binary.Read(buf, binary.LittleEndian, &r) 600 | return 601 | } 602 | 603 | func (b *Bindgen) getI16(d []byte) (r int16) { 604 | buf := bytes.NewBuffer(d) 605 | binary.Read(buf, binary.LittleEndian, &r) 606 | return 607 | } 608 | 609 | func (b *Bindgen) getU32(d []byte) (r uint32) { 610 | buf := bytes.NewBuffer(d) 611 | binary.Read(buf, binary.LittleEndian, &r) 612 | return 613 | } 614 | 615 | func (b *Bindgen) getI32(d []byte) (r int32) { 616 | buf := bytes.NewBuffer(d) 617 | binary.Read(buf, binary.LittleEndian, &r) 618 | return 619 | } 620 | 621 | func (b *Bindgen) getU64(d []byte) (r uint64) { 622 | buf := bytes.NewBuffer(d) 623 | binary.Read(buf, binary.LittleEndian, &r) 624 | return 625 | } 626 | 627 | func (b *Bindgen) getI64(d []byte) (r int64) { 628 | buf := bytes.NewBuffer(d) 629 | binary.Read(buf, binary.LittleEndian, &r) 630 | return 631 | } 632 | 633 | func (b *Bindgen) getF32(d []byte) float32 { 634 | buf := bytes.NewBuffer(d) 635 | var p uint32 636 | binary.Read(buf, binary.LittleEndian, &p) 637 | return math.Float32frombits(p) 638 | } 639 | 640 | func (b *Bindgen) getF64(d []byte) float64 { 641 | buf := bytes.NewBuffer(d) 642 | var p uint64 643 | binary.Read(buf, binary.LittleEndian, &p) 644 | return math.Float64frombits(p) 645 | } 646 | 647 | func (b *Bindgen) getBool(d []byte) bool { 648 | return d[0] == byte(1) 649 | } 650 | 651 | func (b *Bindgen) getRune(d []byte) rune { 652 | buf := bytes.NewBuffer(d) 653 | var p uint32 654 | binary.Read(buf, binary.LittleEndian, &p) 655 | return rune(p) 656 | } 657 | 658 | func (b *Bindgen) getString(d []byte) string { 659 | return string(d) 660 | } 661 | 662 | func (b *Bindgen) getByteSlice(d []byte) []byte { 663 | x := make([]byte, len(d)) 664 | copy(x, d) 665 | return x 666 | } 667 | 668 | func (b *Bindgen) getI8Slice(d []byte) []int8 { 669 | r := make([]int8, len(d)) 670 | for i, v := range d { 671 | r[i] = int8(v) 672 | } 673 | return r 674 | } 675 | 676 | func (b *Bindgen) getU16Slice(d []byte) []uint16 { 677 | r := make([]uint16, len(d) / 2) 678 | for i := 0; i < len(r); i++ { 679 | buf := bytes.NewBuffer(d[i*2 : (i+1)*2]) 680 | binary.Read(buf, binary.LittleEndian, &r[i]) 681 | } 682 | return r 683 | } 684 | 685 | func (b *Bindgen) getI16Slice(d []byte) []int16 { 686 | r := make([]int16, len(d) / 2) 687 | for i := 0; i < len(r); i++ { 688 | buf := bytes.NewBuffer(d[i*2 : (i+1)*2]) 689 | binary.Read(buf, binary.LittleEndian, &r[i]) 690 | } 691 | return r 692 | 693 | } 694 | 695 | func (b *Bindgen) getU32Slice(d []byte) []uint32 { 696 | r := make([]uint32, len(d) / 4) 697 | for i := 0; i < len(r); i++ { 698 | buf := bytes.NewBuffer(d[i*4 : (i+1)*4]) 699 | binary.Read(buf, binary.LittleEndian, &r[i]) 700 | } 701 | return r 702 | 703 | } 704 | 705 | func (b *Bindgen) getI32Slice(d []byte) []int32 { 706 | r := make([]int32, len(d) / 4) 707 | for i := 0; i < len(r); i++ { 708 | buf := bytes.NewBuffer(d[i*4 : (i+1)*4]) 709 | binary.Read(buf, binary.LittleEndian, &r[i]) 710 | } 711 | return r 712 | 713 | } 714 | 715 | func (b *Bindgen) getU64Slice(d []byte) []uint64 { 716 | r := make([]uint64, len(d) / 8) 717 | for i := 0; i < len(r); i++ { 718 | buf := bytes.NewBuffer(d[i*8 : (i+1)*8]) 719 | binary.Read(buf, binary.LittleEndian, &r[i]) 720 | } 721 | return r 722 | 723 | } 724 | 725 | func (b *Bindgen) getI64Slice(d []byte) []int64 { 726 | r := make([]int64, len(d) / 8) 727 | for i := 0; i < len(r); i++ { 728 | buf := bytes.NewBuffer(d[i*8 : (i+1)*8]) 729 | binary.Read(buf, binary.LittleEndian, &r[i]) 730 | } 731 | return r 732 | 733 | } --------------------------------------------------------------------------------