From c647adff54877c3a916fb92a1b07bf214dfdaab2 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 4 Jan 2025 01:10:33 +0100 Subject: [PATCH] feat: add get variable Signed-off-by: kjuulh --- alloy/Cargo.lock | 30 ++ alloy/Cargo.toml | 1 + alloy/files/config.alloy | 4 +- alloy/src/bindings.rs | 53 +++- alloy/src/lib.rs | 17 +- churn-tasks/src/bindings.rs | 596 ++++++++++++++++++++++++++++++------ churn-tasks/wit/world.wit | 1 + 7 files changed, 591 insertions(+), 111 deletions(-) diff --git a/alloy/Cargo.lock b/alloy/Cargo.lock index aee6ddb..bb0ebf7 100644 --- a/alloy/Cargo.lock +++ b/alloy/Cargo.lock @@ -7,6 +7,7 @@ name = "alloy" version = "0.1.0" dependencies = [ "dirs", + "minijinja", "wit-bindgen-rt", ] @@ -70,6 +71,15 @@ dependencies = [ "libc", ] +[[package]] +name = "minijinja" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c37e1b517d1dcd0e51dc36c4567b9d5a29262b3ec8da6cb5d35e27a8fb529b5" +dependencies = [ + "serde", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -105,6 +115,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "serde" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "2.0.90" diff --git a/alloy/Cargo.toml b/alloy/Cargo.toml index b3ec6bf..864f9ef 100644 --- a/alloy/Cargo.toml +++ b/alloy/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] dirs = "5.0.1" +minijinja = "2.5.0" wit-bindgen-rt = { version = "0.36.0", features = ["bitflags"] } [lib] diff --git a/alloy/files/config.alloy b/alloy/files/config.alloy index bb3bdaa..a41dc95 100644 --- a/alloy/files/config.alloy +++ b/alloy/files/config.alloy @@ -30,7 +30,7 @@ prometheus.scrape "default" { prometheus.remote_write "default" { external_labels = { - "node" = "new-node", + "node" = "{{ node_name }}", } endpoint { @@ -75,7 +75,7 @@ loki.process "filter_logs" { loki.write "grafana_loki" { external_labels = { - "node" = "new-node", + "node" = "{{ node_name }}", } endpoint { diff --git a/alloy/src/bindings.rs b/alloy/src/bindings.rs index 7cd7f6f..20521b7 100644 --- a/alloy/src/bindings.rs +++ b/alloy/src/bindings.rs @@ -127,6 +127,40 @@ pub mod component { } } } + impl Process { + #[allow(unused_unsafe, clippy::all)] + pub fn get_variable(&self, key: &str) -> _rt::String { + unsafe { + #[repr(align(4))] + struct RetArea([::core::mem::MaybeUninit; 8]); + let mut ret_area = RetArea( + [::core::mem::MaybeUninit::uninit(); 8], + ); + let vec0 = key; + let ptr0 = vec0.as_ptr().cast::(); + let len0 = vec0.len(); + let ptr1 = ret_area.0.as_mut_ptr().cast::(); + #[cfg(target_arch = "wasm32")] + #[link( + wasm_import_module = "component:churn-tasks/process@0.1.0" + )] + extern "C" { + #[link_name = "[method]process.get-variable"] + fn wit_import(_: i32, _: *mut u8, _: usize, _: *mut u8); + } + #[cfg(not(target_arch = "wasm32"))] + fn wit_import(_: i32, _: *mut u8, _: usize, _: *mut u8) { + unreachable!() + } + wit_import((self).handle() as i32, ptr0.cast_mut(), len0, ptr1); + let l2 = *ptr1.add(0).cast::<*mut u8>(); + let l3 = *ptr1.add(4).cast::(); + let len4 = l3; + let bytes4 = _rt::Vec::from_raw_parts(l2.cast(), len4, len4); + _rt::string_lift(bytes4) + } + } + } } } } @@ -344,16 +378,17 @@ pub(crate) use __export_alloy_impl as export; #[cfg(target_arch = "wasm32")] #[link_section = "component-type:wit-bindgen:0.35.0:component:alloy:alloy:encoded world"] #[doc(hidden)] -pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 390] = *b"\ -\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x8a\x02\x01A\x02\x01\ -A\x04\x01B\x08\x04\0\x07process\x03\x01\x01i\0\x01@\0\0\x01\x04\0\x14[constructo\ +pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 439] = *b"\ +\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xbb\x02\x01A\x02\x01\ +A\x04\x01B\x0a\x04\0\x07process\x03\x01\x01i\0\x01@\0\0\x01\x04\0\x14[constructo\ r]process\x01\x02\x01h\0\x01ps\x01@\x02\x04self\x03\x06inputs\x04\0s\x04\0\x1b[m\ -ethod]process.run-process\x01\x05\x03\0#component:churn-tasks/process@0.1.0\x05\0\ -\x01B\x06\x01@\0\0s\x04\0\x02id\x01\0\x01@\0\0\x7f\x04\0\x0ashould-run\x01\x01\x01\ -@\0\x01\0\x04\0\x07execute\x01\x02\x04\0\x20component:churn-tasks/task@0.1.0\x05\ -\x01\x04\0\x15component:alloy/alloy\x04\0\x0b\x0b\x01\0\x05alloy\x03\0\0\0G\x09p\ -roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\ -\x060.35.0"; +ethod]process.run-process\x01\x05\x01@\x02\x04self\x03\x03keys\0s\x04\0\x1c[meth\ +od]process.get-variable\x01\x06\x03\0#component:churn-tasks/process@0.1.0\x05\0\x01\ +B\x06\x01@\0\0s\x04\0\x02id\x01\0\x01@\0\0\x7f\x04\0\x0ashould-run\x01\x01\x01@\0\ +\x01\0\x04\0\x07execute\x01\x02\x04\0\x20component:churn-tasks/task@0.1.0\x05\x01\ +\x04\0\x15component:alloy/alloy\x04\0\x0b\x0b\x01\0\x05alloy\x03\0\0\0G\x09produ\ +cers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\x06\ +0.35.0"; #[inline(never)] #[doc(hidden)] pub fn __link_custom_section_describing_imports() { diff --git a/alloy/src/lib.rs b/alloy/src/lib.rs index 3c88360..c7058e1 100644 --- a/alloy/src/lib.rs +++ b/alloy/src/lib.rs @@ -3,6 +3,7 @@ use std::{io::Write, path::PathBuf}; use bindings::{ component::churn_tasks::process::Process, exports::component::churn_tasks::task::Guest, }; +use minijinja::{context, Environment}; #[allow(warnings)] mod bindings; @@ -55,6 +56,9 @@ impl Guest for Component { fn execute() { println!("running alloy installation"); + let process = Process::new(); + let node_name = process.get_variable("node_name"); + let output = Process::new().run_process( &["systemctl", "is-enabled", "alloy.service"] .into_iter() @@ -67,6 +71,11 @@ impl Guest for Component { let restart = match std::fs::read_to_string(ALLOY_CONFIG_PATH) { Ok(content) => { + let mut env = Environment::new(); + env.add_template("alloy.config", &content).unwrap(); + let tmpl = env.get_template("alloy.config").unwrap(); + let content = tmpl.render(context! {node_name => node_name}).unwrap(); + if content != ALLOY_CONFIG_FILE { let mut file = std::fs::File::create(ALLOY_CONFIG_PATH) .expect("to be able to create file"); @@ -122,7 +131,13 @@ fn install_alloy() -> Result<(), String> { run_command(["bash", "-c", "wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null"])?; run_command(["bash", "-c", "echo \"deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main\" | sudo tee /etc/apt/sources.list.d/grafana.list"])?; run_command(["apt-get", "update"])?; - run_command(["apt-get", "install", "alloy"])?; + run_command([ + "apt-get", + "install", + "-o Dpkg::Options::=\"--force-confdef\"", + "-o Dpkg::Options::=\"--force-confold\"", + "alloy", + ])?; println!("=== finished installing alloy ==="); Ok(()) diff --git a/churn-tasks/src/bindings.rs b/churn-tasks/src/bindings.rs index bd5f055..d566d8d 100644 --- a/churn-tasks/src/bindings.rs +++ b/churn-tasks/src/bindings.rs @@ -1,93 +1,397 @@ -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn _export_id_cabi() -> *mut u8 { - #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); - let result0 = T::id(); - let ptr1 = _RET_AREA.0.as_mut_ptr().cast::(); - let vec2 = (result0.into_bytes()).into_boxed_slice(); - let ptr2 = vec2.as_ptr().cast::(); - let len2 = vec2.len(); - ::core::mem::forget(vec2); - *ptr1.add(4).cast::() = len2; - *ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut(); - ptr1 +#[allow(dead_code)] +pub mod exports { + #[allow(dead_code)] + pub mod component { + #[allow(dead_code)] + pub mod churn_tasks { + #[allow(dead_code, clippy::all)] + pub mod task { + #[used] + #[doc(hidden)] + static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports; + use super::super::super::super::_rt; + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_id_cabi() -> *mut u8 { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + let result0 = T::id(); + let ptr1 = _RET_AREA.0.as_mut_ptr().cast::(); + let vec2 = (result0.into_bytes()).into_boxed_slice(); + let ptr2 = vec2.as_ptr().cast::(); + let len2 = vec2.len(); + ::core::mem::forget(vec2); + *ptr1.add(4).cast::() = len2; + *ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut(); + ptr1 + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn __post_return_id(arg0: *mut u8) { + let l0 = *arg0.add(0).cast::<*mut u8>(); + let l1 = *arg0.add(4).cast::(); + _rt::cabi_dealloc(l0, l1, 1); + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_should_run_cabi() -> i32 { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + let result0 = T::should_run(); + match result0 { + true => 1, + false => 0, + } + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_execute_cabi() { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + T::execute(); + } + pub trait Guest { + fn id() -> _rt::String; + fn should_run() -> bool; + fn execute(); + } + #[doc(hidden)] + macro_rules! __export_component_churn_tasks_task_0_1_0_cabi { + ($ty:ident with_types_in $($path_to_types:tt)*) => { + const _ : () = { #[export_name = + "component:churn-tasks/task@0.1.0#id"] unsafe extern "C" fn + export_id() -> * mut u8 { $($path_to_types)*:: + _export_id_cabi::<$ty > () } #[export_name = + "cabi_post_component:churn-tasks/task@0.1.0#id"] unsafe extern + "C" fn _post_return_id(arg0 : * mut u8,) { $($path_to_types)*:: + __post_return_id::<$ty > (arg0) } #[export_name = + "component:churn-tasks/task@0.1.0#should-run"] unsafe extern "C" + fn export_should_run() -> i32 { $($path_to_types)*:: + _export_should_run_cabi::<$ty > () } #[export_name = + "component:churn-tasks/task@0.1.0#execute"] unsafe extern "C" fn + export_execute() { $($path_to_types)*:: + _export_execute_cabi::<$ty > () } }; + }; + } + #[doc(hidden)] + pub(crate) use __export_component_churn_tasks_task_0_1_0_cabi; + #[repr(align(4))] + struct _RetArea([::core::mem::MaybeUninit; 8]); + static mut _RET_AREA: _RetArea = _RetArea( + [::core::mem::MaybeUninit::uninit(); 8], + ); + } + #[allow(dead_code, clippy::all)] + pub mod process { + #[used] + #[doc(hidden)] + static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports; + use super::super::super::super::_rt; + #[derive(Debug)] + #[repr(transparent)] + pub struct Process { + handle: _rt::Resource, + } + type _ProcessRep = Option; + impl Process { + /// Creates a new resource from the specified representation. + /// + /// This function will create a new resource handle by moving `val` onto + /// the heap and then passing that heap pointer to the component model to + /// create a handle. The owned handle is then returned as `Process`. + pub fn new(val: T) -> Self { + Self::type_guard::(); + let val: _ProcessRep = Some(val); + let ptr: *mut _ProcessRep = _rt::Box::into_raw( + _rt::Box::new(val), + ); + unsafe { Self::from_handle(T::_resource_new(ptr.cast())) } + } + /// Gets access to the underlying `T` which represents this resource. + pub fn get(&self) -> &T { + let ptr = unsafe { &*self.as_ptr::() }; + ptr.as_ref().unwrap() + } + /// Gets mutable access to the underlying `T` which represents this + /// resource. + pub fn get_mut(&mut self) -> &mut T { + let ptr = unsafe { &mut *self.as_ptr::() }; + ptr.as_mut().unwrap() + } + /// Consumes this resource and returns the underlying `T`. + pub fn into_inner(self) -> T { + let ptr = unsafe { &mut *self.as_ptr::() }; + ptr.take().unwrap() + } + #[doc(hidden)] + pub unsafe fn from_handle(handle: u32) -> Self { + Self { + handle: _rt::Resource::from_handle(handle), + } + } + #[doc(hidden)] + pub fn take_handle(&self) -> u32 { + _rt::Resource::take_handle(&self.handle) + } + #[doc(hidden)] + pub fn handle(&self) -> u32 { + _rt::Resource::handle(&self.handle) + } + #[doc(hidden)] + fn type_guard() { + use core::any::TypeId; + static mut LAST_TYPE: Option = None; + unsafe { + assert!(! cfg!(target_feature = "atomics")); + let id = TypeId::of::(); + match LAST_TYPE { + Some(ty) => { + assert!( + ty == id, "cannot use two types with this resource type" + ) + } + None => LAST_TYPE = Some(id), + } + } + } + #[doc(hidden)] + pub unsafe fn dtor(handle: *mut u8) { + Self::type_guard::(); + let _ = _rt::Box::from_raw(handle as *mut _ProcessRep); + } + fn as_ptr(&self) -> *mut _ProcessRep { + Process::type_guard::(); + T::_resource_rep(self.handle()).cast() + } + } + /// A borrowed version of [`Process`] which represents a borrowed value + /// with the lifetime `'a`. + #[derive(Debug)] + #[repr(transparent)] + pub struct ProcessBorrow<'a> { + rep: *mut u8, + _marker: core::marker::PhantomData<&'a Process>, + } + impl<'a> ProcessBorrow<'a> { + #[doc(hidden)] + pub unsafe fn lift(rep: usize) -> Self { + Self { + rep: rep as *mut u8, + _marker: core::marker::PhantomData, + } + } + /// Gets access to the underlying `T` in this resource. + pub fn get(&self) -> &T { + let ptr = unsafe { &mut *self.as_ptr::() }; + ptr.as_ref().unwrap() + } + fn as_ptr(&self) -> *mut _ProcessRep { + Process::type_guard::(); + self.rep.cast() + } + } + unsafe impl _rt::WasmResource for Process { + #[inline] + unsafe fn drop(_handle: u32) { + #[cfg(not(target_arch = "wasm32"))] + unreachable!(); + #[cfg(target_arch = "wasm32")] + { + #[link( + wasm_import_module = "[export]component:churn-tasks/process@0.1.0" + )] + extern "C" { + #[link_name = "[resource-drop]process"] + fn drop(_: u32); + } + drop(_handle); + } + } + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_constructor_process_cabi() -> i32 { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + let result0 = Process::new(T::new()); + (result0).take_handle() as i32 + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_method_process_run_process_cabi( + arg0: *mut u8, + arg1: *mut u8, + arg2: usize, + ) -> *mut u8 { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + let base3 = arg1; + let len3 = arg2; + let mut result3 = _rt::Vec::with_capacity(len3); + for i in 0..len3 { + let base = base3.add(i * 8); + let e3 = { + let l0 = *base.add(0).cast::<*mut u8>(); + let l1 = *base.add(4).cast::(); + let len2 = l1; + let bytes2 = _rt::Vec::from_raw_parts(l0.cast(), len2, len2); + _rt::string_lift(bytes2) + }; + result3.push(e3); + } + _rt::cabi_dealloc(base3, len3 * 8, 4); + let result4 = T::run_process( + ProcessBorrow::lift(arg0 as u32 as usize).get(), + result3, + ); + let ptr5 = _RET_AREA.0.as_mut_ptr().cast::(); + let vec6 = (result4.into_bytes()).into_boxed_slice(); + let ptr6 = vec6.as_ptr().cast::(); + let len6 = vec6.len(); + ::core::mem::forget(vec6); + *ptr5.add(4).cast::() = len6; + *ptr5.add(0).cast::<*mut u8>() = ptr6.cast_mut(); + ptr5 + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn __post_return_method_process_run_process( + arg0: *mut u8, + ) { + let l0 = *arg0.add(0).cast::<*mut u8>(); + let l1 = *arg0.add(4).cast::(); + _rt::cabi_dealloc(l0, l1, 1); + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn _export_method_process_get_variable_cabi( + arg0: *mut u8, + arg1: *mut u8, + arg2: usize, + ) -> *mut u8 { + #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); + let len0 = arg2; + let bytes0 = _rt::Vec::from_raw_parts(arg1.cast(), len0, len0); + let result1 = T::get_variable( + ProcessBorrow::lift(arg0 as u32 as usize).get(), + _rt::string_lift(bytes0), + ); + let ptr2 = _RET_AREA.0.as_mut_ptr().cast::(); + let vec3 = (result1.into_bytes()).into_boxed_slice(); + let ptr3 = vec3.as_ptr().cast::(); + let len3 = vec3.len(); + ::core::mem::forget(vec3); + *ptr2.add(4).cast::() = len3; + *ptr2.add(0).cast::<*mut u8>() = ptr3.cast_mut(); + ptr2 + } + #[doc(hidden)] + #[allow(non_snake_case)] + pub unsafe fn __post_return_method_process_get_variable( + arg0: *mut u8, + ) { + let l0 = *arg0.add(0).cast::<*mut u8>(); + let l1 = *arg0.add(4).cast::(); + _rt::cabi_dealloc(l0, l1, 1); + } + pub trait Guest { + type Process: GuestProcess; + } + pub trait GuestProcess: 'static { + #[doc(hidden)] + unsafe fn _resource_new(val: *mut u8) -> u32 + where + Self: Sized, + { + #[cfg(not(target_arch = "wasm32"))] + { + let _ = val; + unreachable!(); + } + #[cfg(target_arch = "wasm32")] + { + #[link( + wasm_import_module = "[export]component:churn-tasks/process@0.1.0" + )] + extern "C" { + #[link_name = "[resource-new]process"] + fn new(_: *mut u8) -> u32; + } + new(val) + } + } + #[doc(hidden)] + fn _resource_rep(handle: u32) -> *mut u8 + where + Self: Sized, + { + #[cfg(not(target_arch = "wasm32"))] + { + let _ = handle; + unreachable!(); + } + #[cfg(target_arch = "wasm32")] + { + #[link( + wasm_import_module = "[export]component:churn-tasks/process@0.1.0" + )] + extern "C" { + #[link_name = "[resource-rep]process"] + fn rep(_: u32) -> *mut u8; + } + unsafe { rep(handle) } + } + } + fn new() -> Self; + fn run_process(&self, inputs: _rt::Vec<_rt::String>) -> _rt::String; + fn get_variable(&self, key: _rt::String) -> _rt::String; + } + #[doc(hidden)] + macro_rules! __export_component_churn_tasks_process_0_1_0_cabi { + ($ty:ident with_types_in $($path_to_types:tt)*) => { + const _ : () = { #[export_name = + "component:churn-tasks/process@0.1.0#[constructor]process"] + unsafe extern "C" fn export_constructor_process() -> i32 { + $($path_to_types)*:: _export_constructor_process_cabi::<<$ty as + $($path_to_types)*:: Guest >::Process > () } #[export_name = + "component:churn-tasks/process@0.1.0#[method]process.run-process"] + unsafe extern "C" fn export_method_process_run_process(arg0 : * + mut u8, arg1 : * mut u8, arg2 : usize,) -> * mut u8 { + $($path_to_types)*:: + _export_method_process_run_process_cabi::<<$ty as + $($path_to_types)*:: Guest >::Process > (arg0, arg1, arg2) } + #[export_name = + "cabi_post_component:churn-tasks/process@0.1.0#[method]process.run-process"] + unsafe extern "C" fn _post_return_method_process_run_process(arg0 + : * mut u8,) { $($path_to_types)*:: + __post_return_method_process_run_process::<<$ty as + $($path_to_types)*:: Guest >::Process > (arg0) } #[export_name = + "component:churn-tasks/process@0.1.0#[method]process.get-variable"] + unsafe extern "C" fn export_method_process_get_variable(arg0 : * + mut u8, arg1 : * mut u8, arg2 : usize,) -> * mut u8 { + $($path_to_types)*:: + _export_method_process_get_variable_cabi::<<$ty as + $($path_to_types)*:: Guest >::Process > (arg0, arg1, arg2) } + #[export_name = + "cabi_post_component:churn-tasks/process@0.1.0#[method]process.get-variable"] + unsafe extern "C" fn + _post_return_method_process_get_variable(arg0 : * mut u8,) { + $($path_to_types)*:: + __post_return_method_process_get_variable::<<$ty as + $($path_to_types)*:: Guest >::Process > (arg0) } const _ : () = { + #[doc(hidden)] #[export_name = + "component:churn-tasks/process@0.1.0#[dtor]process"] + #[allow(non_snake_case)] unsafe extern "C" fn dtor(rep : * mut + u8) { $($path_to_types)*:: Process::dtor::< <$ty as + $($path_to_types)*:: Guest >::Process > (rep) } }; }; + }; + } + #[doc(hidden)] + pub(crate) use __export_component_churn_tasks_process_0_1_0_cabi; + #[repr(align(4))] + struct _RetArea([::core::mem::MaybeUninit; 8]); + static mut _RET_AREA: _RetArea = _RetArea( + [::core::mem::MaybeUninit::uninit(); 8], + ); + } + } + } } -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn __post_return_id(arg0: *mut u8) { - let l0 = *arg0.add(0).cast::<*mut u8>(); - let l1 = *arg0.add(4).cast::(); - _rt::cabi_dealloc(l0, l1, 1); -} -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn _export_should_run_cabi() -> *mut u8 { - #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); - let result0 = T::should_run(); - let ptr1 = _RET_AREA.0.as_mut_ptr().cast::(); - let vec2 = (result0.into_bytes()).into_boxed_slice(); - let ptr2 = vec2.as_ptr().cast::(); - let len2 = vec2.len(); - ::core::mem::forget(vec2); - *ptr1.add(4).cast::() = len2; - *ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut(); - ptr1 -} -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn __post_return_should_run(arg0: *mut u8) { - let l0 = *arg0.add(0).cast::<*mut u8>(); - let l1 = *arg0.add(4).cast::(); - _rt::cabi_dealloc(l0, l1, 1); -} -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn _export_execute_cabi() -> *mut u8 { - #[cfg(target_arch = "wasm32")] _rt::run_ctors_once(); - let result0 = T::execute(); - let ptr1 = _RET_AREA.0.as_mut_ptr().cast::(); - let vec2 = (result0.into_bytes()).into_boxed_slice(); - let ptr2 = vec2.as_ptr().cast::(); - let len2 = vec2.len(); - ::core::mem::forget(vec2); - *ptr1.add(4).cast::() = len2; - *ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut(); - ptr1 -} -#[doc(hidden)] -#[allow(non_snake_case)] -pub unsafe fn __post_return_execute(arg0: *mut u8) { - let l0 = *arg0.add(0).cast::<*mut u8>(); - let l1 = *arg0.add(4).cast::(); - _rt::cabi_dealloc(l0, l1, 1); -} -pub trait Guest { - fn id() -> _rt::String; - fn should_run() -> _rt::String; - fn execute() -> _rt::String; -} -#[doc(hidden)] -macro_rules! __export_world_task_cabi { - ($ty:ident with_types_in $($path_to_types:tt)*) => { - const _ : () = { #[export_name = "id"] unsafe extern "C" fn export_id() -> * mut - u8 { $($path_to_types)*:: _export_id_cabi::<$ty > () } #[export_name = - "cabi_post_id"] unsafe extern "C" fn _post_return_id(arg0 : * mut u8,) { - $($path_to_types)*:: __post_return_id::<$ty > (arg0) } #[export_name = - "should-run"] unsafe extern "C" fn export_should_run() -> * mut u8 { - $($path_to_types)*:: _export_should_run_cabi::<$ty > () } #[export_name = - "cabi_post_should-run"] unsafe extern "C" fn _post_return_should_run(arg0 : * mut - u8,) { $($path_to_types)*:: __post_return_should_run::<$ty > (arg0) } - #[export_name = "execute"] unsafe extern "C" fn export_execute() -> * mut u8 { - $($path_to_types)*:: _export_execute_cabi::<$ty > () } #[export_name = - "cabi_post_execute"] unsafe extern "C" fn _post_return_execute(arg0 : * mut u8,) - { $($path_to_types)*:: __post_return_execute::<$ty > (arg0) } }; - }; -} -#[doc(hidden)] -pub(crate) use __export_world_task_cabi; -#[repr(align(4))] -struct _RetArea([::core::mem::MaybeUninit; 8]); -static mut _RET_AREA: _RetArea = _RetArea([::core::mem::MaybeUninit::uninit(); 8]); mod _rt { #[cfg(target_arch = "wasm32")] pub fn run_ctors_once() { @@ -101,6 +405,89 @@ mod _rt { alloc::dealloc(ptr, layout); } pub use alloc_crate::string::String; + use core::fmt; + use core::marker; + use core::sync::atomic::{AtomicU32, Ordering::Relaxed}; + /// A type which represents a component model resource, either imported or + /// exported into this component. + /// + /// This is a low-level wrapper which handles the lifetime of the resource + /// (namely this has a destructor). The `T` provided defines the component model + /// intrinsics that this wrapper uses. + /// + /// One of the chief purposes of this type is to provide `Deref` implementations + /// to access the underlying data when it is owned. + /// + /// This type is primarily used in generated code for exported and imported + /// resources. + #[repr(transparent)] + pub struct Resource { + handle: AtomicU32, + _marker: marker::PhantomData, + } + /// A trait which all wasm resources implement, namely providing the ability to + /// drop a resource. + /// + /// This generally is implemented by generated code, not user-facing code. + #[allow(clippy::missing_safety_doc)] + pub unsafe trait WasmResource { + /// Invokes the `[resource-drop]...` intrinsic. + unsafe fn drop(handle: u32); + } + impl Resource { + #[doc(hidden)] + pub unsafe fn from_handle(handle: u32) -> Self { + debug_assert!(handle != u32::MAX); + Self { + handle: AtomicU32::new(handle), + _marker: marker::PhantomData, + } + } + /// Takes ownership of the handle owned by `resource`. + /// + /// Note that this ideally would be `into_handle` taking `Resource` by + /// ownership. The code generator does not enable that in all situations, + /// unfortunately, so this is provided instead. + /// + /// Also note that `take_handle` is in theory only ever called on values + /// owned by a generated function. For example a generated function might + /// take `Resource` as an argument but then call `take_handle` on a + /// reference to that argument. In that sense the dynamic nature of + /// `take_handle` should only be exposed internally to generated code, not + /// to user code. + #[doc(hidden)] + pub fn take_handle(resource: &Resource) -> u32 { + resource.handle.swap(u32::MAX, Relaxed) + } + #[doc(hidden)] + pub fn handle(resource: &Resource) -> u32 { + resource.handle.load(Relaxed) + } + } + impl fmt::Debug for Resource { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Resource").field("handle", &self.handle).finish() + } + } + impl Drop for Resource { + fn drop(&mut self) { + unsafe { + match self.handle.load(Relaxed) { + u32::MAX => {} + other => T::drop(other), + } + } + } + } + pub use alloc_crate::boxed::Box; + pub use alloc_crate::vec::Vec; + pub unsafe fn string_lift(bytes: Vec) -> String { + if cfg!(debug_assertions) { + String::from_utf8(bytes).unwrap() + } else { + String::from_utf8_unchecked(bytes) + } + } pub use alloc_crate::alloc; extern crate alloc as alloc_crate; } @@ -122,25 +509,36 @@ mod _rt { /// ``` #[allow(unused_macros)] #[doc(hidden)] -macro_rules! __export_task_impl { +macro_rules! __export_churn_impl { ($ty:ident) => { self::export!($ty with_types_in self); }; ($ty:ident with_types_in $($path_to_types_root:tt)*) => { - $($path_to_types_root)*:: __export_world_task_cabi!($ty with_types_in - $($path_to_types_root)*); + $($path_to_types_root)*:: + exports::component::churn_tasks::task::__export_component_churn_tasks_task_0_1_0_cabi!($ty + with_types_in $($path_to_types_root)*:: exports::component::churn_tasks::task); + $($path_to_types_root)*:: + exports::component::churn_tasks::process::__export_component_churn_tasks_process_0_1_0_cabi!($ty + with_types_in $($path_to_types_root)*:: + exports::component::churn_tasks::process); }; } #[doc(inline)] -pub(crate) use __export_task_impl as export; +pub(crate) use __export_churn_impl as export; #[cfg(target_arch = "wasm32")] -#[link_section = "component-type:wit-bindgen:0.35.0:component:churn-tasks:task:encoded world"] +#[link_section = "component-type:wit-bindgen:0.35.0:component:churn-tasks@0.1.0:churn:encoded world"] #[doc(hidden)] -pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 198] = *b"\ -\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07L\x01A\x02\x01A\x04\x01\ -@\0\0s\x04\0\x02id\x01\0\x04\0\x0ashould-run\x01\0\x04\0\x07execute\x01\0\x04\0\x1a\ -component:churn-tasks/task\x04\0\x0b\x0a\x01\0\x04task\x03\0\0\0G\x09producers\x01\ -\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\x060.35.0"; +pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 451] = *b"\ +\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xc7\x02\x01A\x02\x01\ +A\x04\x01B\x06\x01@\0\0s\x04\0\x02id\x01\0\x01@\0\0\x7f\x04\0\x0ashould-run\x01\x01\ +\x01@\0\x01\0\x04\0\x07execute\x01\x02\x04\0\x20component:churn-tasks/task@0.1.0\ +\x05\0\x01B\x0a\x04\0\x07process\x03\x01\x01i\0\x01@\0\0\x01\x04\0\x14[construct\ +or]process\x01\x02\x01h\0\x01ps\x01@\x02\x04self\x03\x06inputs\x04\0s\x04\0\x1b[\ +method]process.run-process\x01\x05\x01@\x02\x04self\x03\x03keys\0s\x04\0\x1c[met\ +hod]process.get-variable\x01\x06\x04\0#component:churn-tasks/process@0.1.0\x05\x01\ +\x04\0!component:churn-tasks/churn@0.1.0\x04\0\x0b\x0b\x01\0\x05churn\x03\0\0\0G\ +\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen\ +-rust\x060.35.0"; #[inline(never)] #[doc(hidden)] pub fn __link_custom_section_describing_imports() { diff --git a/churn-tasks/wit/world.wit b/churn-tasks/wit/world.wit index b2f1748..1ca05af 100644 --- a/churn-tasks/wit/world.wit +++ b/churn-tasks/wit/world.wit @@ -4,6 +4,7 @@ interface process { resource process { constructor(); run-process: func(inputs: list) -> string; + get-variable: func(key: string) -> string; } }