Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: Rust support #3061

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/server/wasm/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bool RegisterApiFunction(std::string_view name, Fn f, wasmtime::Linker* linker)
auto args_signature = wasmtime::FuncType({wasmtime::ValKind::I32}, {});

auto res = linker->func_new(module_name, name, args_signature, f);
linker->func_new("env", name, args_signature, f);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I need to find out how to tell wasi to look in a different namespace... I just couldn't! There is wasm-bindgen, but I don't wanna use it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (bool)res;
}

Expand Down
2 changes: 1 addition & 1 deletion src/wasm_sdk/cpp/include/dragonfly.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline std::string_view call(std::initializer_list<std::string> arguments) {

/* Private and NOT part of the public API */
DF_EXPORT("provide_buffer")
inline char* provide_buffer(size_t bytes) {
inline char* provide_buffer(int32_t bytes) {
call_buffer.resize(bytes);
return call_buffer.data();
}
Expand Down
7 changes: 7 additions & 0 deletions src/wasm_sdk/rustw/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/wasm_sdk/rustw/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rustw"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]
3 changes: 3 additions & 0 deletions src/wasm_sdk/rustw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/bytecodealliance/cargo-wasi/tree/main

then just run `cargo wasi build`
39 changes: 39 additions & 0 deletions src/wasm_sdk/rustw/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::io::Write;

extern "C" {
pub fn call(cmd: *const u8);
}

static mut BUFFER: Option<String> = None;

pub fn run(command: &[&str]) -> String {
let mut bytes = Vec::<u8>::new();

unsafe {
bytes
.write(&std::mem::transmute::<i32, [u8; 4]>(command.len() as i32))
.unwrap();

for part in command {
bytes
.write(&std::mem::transmute::<i32, [u8; 4]>(part.len() as i32))
.unwrap();
bytes.write(part.as_bytes()).unwrap();
}

call(bytes.as_ptr());

BUFFER.take().unwrap()
}
}

#[no_mangle]
pub unsafe fn provide_buffer(bytes: i32) -> *mut u8 {
BUFFER.insert(str::repeat(" ", bytes as usize)).as_mut_ptr()
}

#[no_mangle]
pub fn my_fun() {
let res = run(&["GET", "A"]);
println!("Got {}", res);
}
Loading