-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin implementation of WASM - host API
- Loading branch information
Showing
18 changed files
with
1,467 additions
and
440 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "crows-bindings" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
borsh.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use borsh::{from_slice, to_vec, BorshDeserialize, BorshSchema, BorshSerialize}; | ||
use std::{cell::RefCell, collections::HashMap, mem::MaybeUninit}; | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] | ||
pub enum HTTPMethod { | ||
HEAD, | ||
GET, | ||
POST, | ||
PUT, | ||
DELETE, | ||
OPTIONS, | ||
} | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] | ||
pub struct HTTPRequest { | ||
url: String, | ||
method: HTTPMethod, | ||
headers: HashMap<String, String>, | ||
body: Option<String>, | ||
} | ||
|
||
#[derive(Debug, BorshDeserialize, BorshSerialize)] | ||
pub struct HTTPError {} | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] | ||
pub struct HTTPResponse { | ||
// TODO: these should not be public I think, I'd prefer to do a public interface for them | ||
pub headers: HashMap<String, String>, | ||
pub body: String, | ||
pub status: u16, | ||
} | ||
|
||
fn extract_from_return_value(value: u64) -> (u8, u32, u32) { | ||
let status = ((value >> 56) & 0xFF) as u8; | ||
let length = ((value >> 32) & 0x00FFFFFF) as u32; | ||
let ptr = (value & 0xFFFFFFFF) as u32; | ||
(status, length, ptr) | ||
} | ||
|
||
mod bindings { | ||
#[link(wasm_import_module = "crows")] | ||
extern "C" { | ||
pub fn log(content: *mut u8, content_len: usize); | ||
pub fn http(content: *mut u8, content_len: usize) -> u64; | ||
pub fn consume_buffer(index: u32, content: *mut u8, content_len: usize); | ||
|
||
} | ||
} | ||
|
||
fn with_buffer<R>(f: impl FnOnce(&mut Vec<u8>) -> R) -> R { | ||
thread_local! { | ||
static BUFFER: RefCell<Vec<u8>> = RefCell::new(Vec::new()); | ||
} | ||
|
||
BUFFER.with(|r| { | ||
let mut buf = r.borrow_mut(); | ||
buf.clear(); | ||
f(&mut buf) | ||
}) | ||
} | ||
|
||
pub fn http_request( | ||
url: String, | ||
method: HTTPMethod, | ||
headers: HashMap<String, String>, | ||
body: String, | ||
) -> Result<HTTPResponse, HTTPError> { | ||
let body = Some(body); | ||
let request = HTTPRequest { | ||
method, | ||
url, | ||
headers, | ||
body, | ||
}; | ||
|
||
call_host_function(&request, |buf| unsafe { | ||
bindings::http(buf.as_mut_ptr(), buf.len()) | ||
}) | ||
} | ||
|
||
fn call_host_function<T, R, E>(arguments: &T, f: impl FnOnce(&mut Vec<u8>) -> u64) -> Result<R, E> | ||
where | ||
T: BorshSerialize, | ||
R: BorshDeserialize, | ||
E: BorshDeserialize, | ||
{ | ||
let mut encoded = to_vec(arguments).unwrap(); | ||
|
||
println!("encoded length: {}", encoded.len()); | ||
let (status, length, index) = with_buffer(|mut buf| { | ||
buf.append(&mut encoded); | ||
let response = f(&mut buf); | ||
|
||
extract_from_return_value(response) | ||
}); | ||
|
||
println!("response: {status}, {length}, {index}"); | ||
with_buffer(|buf| { | ||
let capacity = buf.capacity(); | ||
if capacity < length as usize { | ||
let additional = length as usize - buf.capacity(); | ||
buf.reserve_exact(additional); | ||
} | ||
|
||
unsafe { | ||
bindings::consume_buffer(index, buf.as_mut_ptr(), length as usize); | ||
buf.set_len(length as usize); | ||
} | ||
|
||
if status == 0 { | ||
Ok(from_slice(buf).expect("Couldn't decode message from the host")) | ||
} else { | ||
Err(from_slice(buf).expect("Couldn't decode message from the host")) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.