-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
441 additions
and
41 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ LABEL maintainer="tom wilson <[email protected]>" | |
|
||
# The working directory used by the base image is /src, so we can mount volumes to there | ||
# to expose files on the host to the ao container | ||
# | ||
# | ||
# https://github.com/emscripten-core/emsdk/blob/9b0db91883452051aca8deddc932363aab29060b/docker/Dockerfile#L120 | ||
|
||
RUN apt-get update --fix-missing -qq -y | ||
|
@@ -53,12 +53,12 @@ RUN cp -r /lua-${LUA_VERSION} /lua-${LUA_VERSION}-32 | |
# And, re-compile lua with "generic WASM" | ||
RUN cd /lua-${LUA_VERSION} && \ | ||
make clean && \ | ||
make generic CC='emcc -s WASM=1 -s MEMORY64=1 -s SUPPORT_LONGJMP=1' | ||
make generic CC='emcc -s WASM=1 -s MEMORY64=1 -s SUPPORT_LONGJMP=1' | ||
|
||
# And, re-compile lua with "generic WASM 32-bit" | ||
RUN cd /lua-${LUA_VERSION}-32 && \ | ||
make clean && \ | ||
make generic CC='emcc -s WASM=1 -s SUPPORT_LONGJMP=1' | ||
make generic CC='emcc -s WASM=1 -s SUPPORT_LONGJMP=1' | ||
|
||
|
||
############################# | ||
|
@@ -115,7 +115,7 @@ ENV NM='/emsdk/upstream/bin/llvm-nm' | |
########################################### | ||
# We first create a directory for the node impls to be placed | ||
# and dependencies installed | ||
# | ||
# | ||
# By running npm link, we allow any commands exposed by | ||
# the node module to be ran globally within the container | ||
RUN mkdir -p /opt/node | ||
|
@@ -124,3 +124,21 @@ RUN cd /opt/node && \ | |
npm install --omit="dev" && \ | ||
npm link | ||
|
||
|
||
########################################### | ||
### Rust and WASM toolchains ### | ||
########################################### | ||
|
||
# Set environment variables | ||
ENV RUSTUP_HOME=/usr/local/rustup \ | ||
CARGO_HOME=/usr/local/cargo \ | ||
PATH=/usr/local/cargo/bin:/usr/local/rustup/bin:$PATH | ||
|
||
# Install Rust, components, and cbindgen in a single step | ||
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly && \ | ||
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu && \ | ||
cargo install cbindgen && \ | ||
rustc --version && cargo --version && cbindgen --version && rustup show | ||
|
||
# Make build directory | ||
RUN mkdir /opt/aorustlib |
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
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,11 @@ | ||
/target/ | ||
Cargo.lock | ||
**/*.log | ||
**/*.tmp | ||
*.swp | ||
*.bak | ||
*.bk | ||
*.tmp | ||
*.old | ||
aorust.h | ||
process.wasm |
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,12 @@ | ||
[package] | ||
name = "aorust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
serde = "1.0" | ||
serde_json = "1.0" | ||
|
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,15 @@ | ||
all:build bindgen | ||
|
||
build: | ||
RUSTFLAGS="--cfg=web_sys_unstable_apis --Z wasm_c_abi=spec" \ | ||
cargo +nightly build \ | ||
-Zbuild-std=std,panic_unwind,panic_abort \ | ||
--target=wasm64-unknown-unknown \ | ||
--release | ||
|
||
bindgen: | ||
cbindgen --config cbindgen.toml --crate aorust --output aorust.h | ||
|
||
clean: | ||
cargo clean | ||
rm aorust.h |
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,7 @@ | ||
language = "C" | ||
|
||
[defines] | ||
MY_DEFINE = "1" | ||
|
||
[export] | ||
include = ["process_handler"] |
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,72 @@ | ||
pub mod libao; | ||
use std::ffi::{CStr, CString}; | ||
use std::os::raw::c_char; | ||
|
||
/// Processes the input `msg` and `env` strings, performs some operations, | ||
/// and returns the result as a C-compatible string. | ||
/// | ||
/// # Safety | ||
/// - The caller must ensure that the `msg` and `env` pointers are valid, null-terminated C strings. | ||
/// - The caller is responsible for freeing the returned string using `free_c_string`. | ||
/// | ||
/// # Arguments | ||
/// - `msg`: A pointer to a null-terminated C string representing the message. | ||
/// - `env`: A pointer to a null-terminated C string representing the environment. | ||
/// | ||
/// # Returns | ||
/// - A pointer to a null-terminated C string containing the result. The caller must not modify | ||
/// or attempt to free this pointer. | ||
#[no_mangle] | ||
pub extern "C" fn process_handle(msg: *const c_char, env: *const c_char) -> *const c_char { | ||
// Convert the C strings to Rust strings | ||
let msg = unsafe { CStr::from_ptr(msg).to_str().unwrap_or("Invalid msg") }; | ||
let env = unsafe { CStr::from_ptr(env).to_str().unwrap_or("Invalid env") }; | ||
|
||
// Call the Rust handler function to process the inputs | ||
let ret = handler(msg, env); | ||
|
||
// Convert the Rust string result into a CString | ||
let c_response = CString::new(ret).unwrap(); | ||
|
||
// Return the raw pointer to the C-compatible string | ||
// The caller is now responsible for freeing this memory | ||
c_response.into_raw() | ||
} | ||
|
||
pub fn handler(msg: &str, env: &str) -> String { | ||
let mut ao = libao::AO::new(); | ||
ao.init(env); | ||
ao.log("Normalize"); | ||
let norm = ao.normalize(msg); | ||
ao.log(&msg); | ||
let send = &ao.send(&norm); | ||
println!("Send: {}", send); | ||
"{\"ok\": true,\"response\":{\"Output\":\"Success\"},\"Memory\":50000000}".to_string() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::ffi::{CStr, CString}; | ||
|
||
#[test] | ||
fn test_process_handle() { | ||
let msg_json = r#"{"key": "value"}"#; | ||
let env_json = r#"{"env": "test"}"#; | ||
|
||
let c_msg = CString::new(msg_json).expect("Failed to create CString for msg"); | ||
let c_env = CString::new(env_json).expect("Failed to create CString for env"); | ||
|
||
let c_result = process_handle(c_msg.as_ptr(), c_env.as_ptr()); | ||
|
||
assert!(!c_result.is_null(), "Returned pointer is null"); | ||
|
||
let result_str = unsafe { CStr::from_ptr(c_result).to_string_lossy().into_owned() }; | ||
|
||
let expected_result = r#"{"ok": true,"response":{"Output":"Success"},"Memory":50000000}"#; | ||
assert_eq!( | ||
result_str, expected_result, | ||
"Output does not match expected result" | ||
); | ||
} | ||
} |
Oops, something went wrong.