Browse Source

Added a proof-of-concept showing how a WASM actor could be built and
run.

Matthew Carr 1 năm trước cách đây
mục cha
commit
9716b024f3

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 549 - 32
Cargo.lock


+ 10 - 0
crates/btrun-wasm/Cargo.toml

@@ -0,0 +1,10 @@
+[package]
+name = "btrun-wasm"
+version = "0.1.0"
+edition = "2021"
+description = "An actor runtime for Blocktree."
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+wasmtime = { git = "https://github.com/bytecodealliance/wasmtime.git", tag = "dev", features = ["component-model"] }

+ 7 - 0
crates/btrun-wasm/run.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+# Builds the example_actor crate as a WASM component and then runs it using this crate.
+set -e
+cd ../example_actor
+./build.sh
+cd -
+cargo run

+ 33 - 0
crates/btrun-wasm/src/main.rs

@@ -0,0 +1,33 @@
+use bt::actor_model::host::Host;
+use wasmtime::component::*;
+use wasmtime::{Config, Engine, Store};
+
+bindgen!("actor" in "../../wit");
+
+struct RustHost;
+
+impl Host for RustHost {
+    fn print(&mut self, msg: String) -> wasmtime::Result<()> {
+        println!("{}", msg);
+        Ok(())
+    }
+}
+
+fn main() -> wasmtime::Result<()> {
+    let mut config = Config::new();
+    config.wasm_component_model(true);
+    let engine = Engine::new(&config)?;
+    let component = Component::from_file(
+        &engine,
+        "../../target/wasm32-unknown-unknown/debug/example_actor.component.wasm",
+    )?;
+
+    let mut linker = Linker::new(&engine);
+    Actor::add_to_linker(&mut linker, |state: &mut RustHost| state)?;
+
+    let mut store = Store::new(&engine, RustHost);
+    let (bindings, _) = Actor::instantiate(&mut store, &component, &linker)?;
+
+    bindings.call_activate(&mut store)?;
+    Ok(())
+}

+ 13 - 0
crates/example_actor/Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "example_actor"
+version = "0.1.0"
+edition = "2021"
+description = "An example implementation of an actor."
+
+[lib]
+crate-type = ["cdylib"]
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+wit-bindgen = { git = "https://github.com/bytecodealliance/wit-bindgen.git", tag="wit-bindgen-0.7.0" }

+ 9 - 0
crates/example_actor/build.sh

@@ -0,0 +1,9 @@
+#!/bin/sh
+# Builds this crate as a WASM component.
+set -eu
+
+PROJ="$(basename "$PWD")"
+TARGET=wasm32-unknown-unknown
+TARGET_DIR=../../target/$TARGET/debug
+cargo build --target $TARGET
+wasm-tools component new $TARGET_DIR/"$PROJ".wasm  -o $TARGET_DIR/"$PROJ".component.wasm

+ 11 - 0
crates/example_actor/src/lib.rs

@@ -0,0 +1,11 @@
+wit_bindgen::generate!("actor");
+
+struct MyActor;
+
+impl Actor for MyActor {
+    fn activate() {
+        bt::actor_model::host::print("Hello, world!");
+    }
+}
+
+export_actor!(MyActor);

+ 1 - 0
crates/example_actor/wit

@@ -0,0 +1 @@
+../../wit

+ 11 - 0
wit/actor-model.wit

@@ -0,0 +1,11 @@
+package bt:actor-model@0.0.1
+
+interface host {
+  print: func(msg: string)
+}
+
+world actor {
+  import host
+
+  export activate: func()
+}

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác