hook printing up

This commit is contained in:
Michael Zhang 2024-06-23 17:29:29 -05:00
parent 947118def1
commit 3e505d8690
14 changed files with 268 additions and 127 deletions

111
Cargo.lock generated
View file

@ -23,7 +23,7 @@ version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"getrandom 0.2.15",
"once_cell",
"version_check",
@ -239,7 +239,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964"
dependencies = [
"async-lock",
"cfg-if",
"cfg-if 1.0.0",
"concurrent-queue",
"futures-io",
"futures-lite",
@ -274,7 +274,7 @@ dependencies = [
"async-signal",
"async-task",
"blocking",
"cfg-if",
"cfg-if 1.0.0",
"event-listener 5.3.1",
"futures-lite",
"rustix",
@ -302,7 +302,7 @@ dependencies = [
"async-io",
"async-lock",
"atomic-waker",
"cfg-if",
"cfg-if 1.0.0",
"futures-core",
"futures-io",
"rustix",
@ -455,7 +455,7 @@ checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
@ -783,6 +783,12 @@ dependencies = [
"target-lexicon",
]
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -1131,7 +1137,7 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
]
[[package]]
@ -1373,7 +1379,7 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"dirs-sys-next",
]
@ -1538,7 +1544,7 @@ version = "0.8.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
]
[[package]]
@ -1590,7 +1596,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"home",
"windows-sys 0.48.0",
]
@ -1646,7 +1652,7 @@ version = "4.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e5768da2206272c81ef0b5e951a41862938a6070da63bcea197899942d3b947"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"rustix",
"windows-sys 0.52.0",
]
@ -2014,7 +2020,7 @@ version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
@ -2025,7 +2031,7 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
@ -2545,7 +2551,7 @@ version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
@ -2658,7 +2664,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"
dependencies = [
"cesu8",
"cfg-if",
"cfg-if 1.0.0",
"combine",
"jni-sys",
"log",
@ -2682,14 +2688,6 @@ dependencies = [
"libc",
]
[[package]]
name = "journal"
version = "0.1.0"
dependencies = [
"panorama-app-sdk",
"wasm-bindgen",
]
[[package]]
name = "js-sys"
version = "0.3.69"
@ -2791,7 +2789,7 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"winapi",
]
@ -2801,7 +2799,7 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"windows-targets 0.52.5",
]
@ -2866,7 +2864,7 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"generator",
"scoped-tls",
"serde",
@ -2961,7 +2959,7 @@ version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"digest",
]
@ -3008,6 +3006,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "mime"
version = "0.3.17"
@ -3108,7 +3112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"cfg-if 1.0.0",
"libc",
"memoffset",
]
@ -3474,7 +3478,7 @@ dependencies = [
name = "panorama-app-sdk"
version = "0.1.0"
dependencies = [
"wasm-bindgen",
"wee_alloc",
]
[[package]]
@ -3527,6 +3531,13 @@ dependencies = [
"uuid",
]
[[package]]
name = "panorama-journal"
version = "0.1.0"
dependencies = [
"panorama-app-sdk",
]
[[package]]
name = "panorama-macros"
version = "0.1.0"
@ -3553,7 +3564,7 @@ version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"libc",
"redox_syscall 0.5.2",
"smallvec",
@ -3824,7 +3835,7 @@ version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"concurrent-queue",
"hermit-abi 0.4.0",
"pin-project-lite",
@ -4276,7 +4287,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [
"cc",
"cfg-if",
"cfg-if 1.0.0",
"getrandom 0.2.15",
"libc",
"spin 0.9.8",
@ -4690,7 +4701,7 @@ version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"cpufeatures",
"digest",
]
@ -4701,7 +4712,7 @@ version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"cpufeatures",
"digest",
]
@ -5133,7 +5144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af91f480ee899ab2d9f8435bfdfc14d08a5754bd9d3fef1f1a1c23336aad6c8b"
dependencies = [
"async-channel 1.9.0",
"cfg-if",
"cfg-if 1.0.0",
"futures-core",
"pin-project-lite",
]
@ -5799,7 +5810,7 @@ version = "3.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"fastrand",
"rustix",
"windows-sys 0.52.0",
@ -5848,7 +5859,7 @@ version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"once_cell",
]
@ -6452,7 +6463,7 @@ version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
@ -6477,7 +6488,7 @@ version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
@ -6568,7 +6579,7 @@ dependencies = [
"async-trait",
"bumpalo",
"cc",
"cfg-if",
"cfg-if 1.0.0",
"encoding_rs",
"hashbrown 0.14.5",
"indexmap 2.2.6",
@ -6610,7 +6621,7 @@ version = "22.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d697d99c341d4a9ffb72f3af7a02124d233eeb59aee010f36d88e97cca553d5e"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
]
[[package]]
@ -6641,7 +6652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3975deafea000457ba84355c7c0fce0372937204f77026510b7b454f28a3a65"
dependencies = [
"anyhow",
"cfg-if",
"cfg-if 1.0.0",
"cranelift-codegen",
"cranelift-control",
"cranelift-entity",
@ -6689,7 +6700,7 @@ checksum = "4ded58eb2d1bf0dcd2182d0ccd7055c4b10b50d711514f1d73f61515d0fa829d"
dependencies = [
"anyhow",
"cc",
"cfg-if",
"cfg-if 1.0.0",
"rustix",
"wasmtime-asm-macros",
"wasmtime-versioned-export-macros",
@ -6703,7 +6714,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5afe2f0499542f9a4bcfa1b55bfdda803b6ade4e7c93c6b99e0f39dba44b0a91"
dependencies = [
"anyhow",
"cfg-if",
"cfg-if 1.0.0",
"libc",
"windows-sys 0.52.0",
]
@ -6923,6 +6934,18 @@ dependencies = [
"windows-core 0.56.0",
]
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "whoami"
version = "1.5.1"
@ -7380,7 +7403,7 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"windows-sys 0.48.0",
]

View file

@ -1,2 +1,13 @@
workspace.resolver = "2"
workspace.members = ["apps/journal", "crates/*", "ui/src-tauri"]
[profile.wasm-debug]
inherits = "dev"
panic = "abort"
[profile.wasm-release]
inherits = "release"
lto = true
opt-level = 's'
strip = true
panic = "abort"

View file

@ -1,3 +1,13 @@
deploy-docs:
mdbook build docs
rsync -azrP docs/book/ root@veil:/home/blogDeploy/public/panorama
JOURNAL_SOURCES := $(shell find . apps/journal -name "*.rs" -not -path "./target/*")
journal: $(JOURNAL_SOURCES)
cargo build \
--profile=wasm-debug \
-p panorama-journal \
--target=wasm32-unknown-unknown
test-install-apps: journal
cargo test -p panorama-core -- tests::test_install_apps

View file

@ -1,5 +1,5 @@
[package]
name = "journal"
name = "panorama-journal"
version = "0.1.0"
edition = "2021"
@ -7,5 +7,4 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]
[dependencies]
wasm-bindgen = "0.2.92"
panorama-app-sdk = { path = "../../crates/panorama-app-sdk" }

View file

@ -3,4 +3,5 @@ version: 0.1.0
panorama_version: 0.1.0
description: Note taking app
installer_path: pkg/journal_bg.wasm
# installer_path: ../../target/wasm32-unknown-unknown/wasm-release/panorama_journal.wasm
installer_path: ../../target/wasm32-unknown-unknown/wasm-debug/panorama_journal.wasm

View file

@ -1,7 +1,15 @@
use wasm_bindgen::prelude::wasm_bindgen;
#![no_std]
#[wasm_bindgen]
#[macro_use]
extern crate panorama_app_sdk;
use panorama_app_sdk::prelude::*;
panorama_app_sdk::init!();
#[no_mangle]
pub fn install() -> i32 {
panorama_app_sdk::register_endpoint("/get_todays_date");
println!("SHIET");
// panorama_app_sdk::register_endpoint("/get_todays_date");
123
}

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["rlib"]
[dependencies]
wasm-bindgen = "0.2.92"
wee_alloc = "0.4.5"

View file

@ -0,0 +1,27 @@
use core::{
fmt,
sync::atomic::{AtomicBool, Ordering},
};
use alloc::{
ffi::CString,
string::String,
vec::{self, Vec},
};
#[doc(hidden)]
pub fn _println(args: fmt::Arguments<'_>) {
_print(format_args!("{}\n", args))
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments<'_>) {
// print_to(args, stdout, "stdout");
// TODO: Lock prints?
let mut buf = String::new();
alloc::fmt::write(&mut buf, args);
let cs = CString::new(buf).unwrap();
let len = cs.as_bytes_with_nul().len() as u64;
let cptr = cs.into_raw();
unsafe { crate::sys::_print(len, cptr) };
}

View file

@ -1,57 +1,18 @@
#![no_std]
pub extern crate wee_alloc;
#[macro_use]
extern crate alloc;
// Reference for FFI closures
// https://adventures.michaelfbryan.com/posts/rust-closures-in-ffi/
use std::ffi::{c_char, c_void, CString};
#[macro_use]
pub mod macros;
pub mod internal;
pub mod sys;
pub mod sys {
use std::ffi::{c_char, c_void};
use crate::RegisterCallback;
extern "C" {
pub fn register_endpoint(
url_len: u64,
url: *const c_char,
callback: *mut RegisterCallback,
callback_data: *mut c_void,
);
}
}
pub struct RegisterContext {}
pub type RegisterCallback = fn(*mut RegisterContext) -> ();
unsafe extern "C" fn register_endpoint_callback_trampoline<F>(
context: *mut RegisterContext,
register_callback: *mut c_void,
) where
F: FnMut(*mut RegisterContext),
{
let register_callback = &mut *(register_callback as *mut F);
register_callback(context);
}
pub fn get_trampoline<F>(_closure: &F) -> RegisterCallback
where
F: FnMut(*mut RegisterCallback),
{
register_endpoint_callback_trampoline::<F>
}
pub fn register_endpoint(
url: impl AsRef<str>,
callback: fn(&RegisterContext) -> (),
) {
let url = url.as_ref();
let url_cstr = CString::new(url).unwrap();
let mut callback2 = |ctx: *mut RegisterContext| callback(&*ctx);
let result = unsafe {
sys::register_endpoint(
url.len() as u64,
url_cstr.into_raw(),
register_endpoint_callback_trampoline,
&mut callback2 as *mut _ as *mut c_void,
)
};
println!("Result: {:?}", result);
pub mod prelude {
// pub use crate::macros::println;
}

View file

@ -0,0 +1,25 @@
#[macro_export]
macro_rules! println {
() => { $crate::print!("\n") };
($($arg:tt)*) => {{ $crate::internal::_println(format_args!($($arg)*)); }};
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{ $crate::internal::_print(format_args!($($arg)*)); }};
}
#[macro_export]
macro_rules! init {
() => {
// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: panorama_app_sdk::wee_alloc::WeeAlloc =
panorama_app_sdk::wee_alloc::WeeAlloc::INIT;
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
};
}

View file

@ -0,0 +1,14 @@
// use crate::RegisterCallback;
use core::ffi::{c_char, c_void};
extern "C" {
pub fn _print(len: u64, ptr: *const c_char);
pub fn register_endpoint(
url_len: u64,
url: *const c_char,
// callback: *mut RegisterCallback,
callback_data: *mut c_void,
);
}

View file

@ -0,0 +1,45 @@
use std::io::{stdout, Write};
use anyhow::Result;
use wasmtime::{Caller, InstancePre, Linker, Memory};
pub struct WasmtimeModule {
pub(crate) module: InstancePre<WasmtimeInstanceEnv>,
}
impl WasmtimeModule {
pub fn link_imports(linker: &mut Linker<WasmtimeInstanceEnv>) -> Result<()> {
macro_rules! link_function {
($($module:literal :: $func:ident),* $(,)?) => {
linker $(
.func_wrap(
$module,
concat!("_", stringify!($func)),
WasmtimeInstanceEnv::$func,
)?
)*;
};
}
abi_funcs!(link_function);
Ok(())
}
}
/// This is loosely based on SpacetimeDB's implementation of their host.
/// See: https://github.com/clockworklabs/SpacetimeDB/blob/c19c0d45c454db2a4215deb23c7f9f82cb5d7561/crates/core/src/host/wasmtime/wasm_instance_env.rs
pub struct WasmtimeInstanceEnv {
/// This is only an Option because memory is initialized after this is created so we need to come back and put it in later
pub(crate) mem: Option<Memory>,
}
impl WasmtimeInstanceEnv {
pub fn print(mut caller: Caller<'_, Self>, len: u64, ptr: u32) {
let mem = caller.data().mem.unwrap();
let mut buffer = vec![0; len as usize];
mem.read(caller, ptr as usize, &mut buffer);
let s = String::from_utf8(buffer).unwrap();
println!("Called print: {}", s);
}
pub fn register_endpoint(mut caller: Caller<'_, Self>) {}
}

View file

@ -0,0 +1,9 @@
macro_rules! abi_funcs {
($macro_name:ident) => {
// TODO: Why is this "env"? How do i use another name
$macro_name! {
"env"::print,
"env"::register_endpoint,
}
};
}

View file

@ -1,3 +1,7 @@
#[macro_use]
pub mod macros;
pub mod internal;
use std::{
collections::HashMap,
fs::{self, File},
@ -5,9 +9,11 @@ use std::{
path::{Path, PathBuf},
};
use anyhow::{Context as _, Result};
use anyhow::{anyhow, Context as _, Result};
use internal::{WasmtimeInstanceEnv, WasmtimeModule};
use itertools::Itertools;
use wasmtime::{
AsContext, Caller, Config, Engine, Linker, Memory, Module, Store,
AsContext, Caller, Config, Engine, Instance, Linker, Memory, Module, Store,
};
use wasmtime_wasi::WasiCtxBuilder;
@ -103,28 +109,30 @@ impl AppState {
let engine = Engine::new(&config)?;
let module = Module::new(&engine, &installer_program)?;
let wasi = WasiCtxBuilder::new().inherit_stdio().inherit_args().build();
let mut linker = Linker::new(&engine);
linker.func_wrap(
"env",
"register_endpoint",
|mut caller: Caller<'_, _>, url_len: i64, url: i32| {
println!("WTF? {url_len} {url}");
let mem = caller.get_export("memory").and_then(|e| e.into_memory());
if let Some(mem) = mem {
let result =
read_utf_8string(&mut caller, &mem, url_len as usize, url as usize);
println!("{:?}", result);
}
// println!("my host state is: {}", caller.data());
},
)?;
WasmtimeModule::link_imports(&mut linker)?;
let module = linker.instantiate_pre(&module)?;
let module = WasmtimeModule { module };
let mut store: Store<_> = Store::new(&engine, wasi);
let instance = linker
.instantiate(&mut store, &module)
let mut state = WasmtimeInstanceEnv { mem: None };
let mut store = Store::new(&engine, state);
println!(
"Required imports: {:?}",
module
.module
.module()
.imports()
.map(|s| s.name())
.collect_vec()
);
let instance = module
.module
.instantiate(&mut store)
.context("Could not instantiate")?;
let mem = instance
.get_memory(&mut store, "memory")
.ok_or_else(|| anyhow!("Fuck!"))?;
store.data_mut().mem = Some(mem);
instance.exports(&mut store).for_each(|export| {
println!("Export: {}", export.name());