From 18eb56d31418812c8963ae89c3914216df9e38ad Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Sun, 28 Jul 2024 12:54:29 -0400 Subject: [PATCH] stuff --- Cargo.lock | 65 ++++++++++- Cargo.toml | 2 +- buffer64.pit | 5 + pit-basic/Cargo.toml | 6 + pit-basic/build.sh | 3 +- pit-basic/src/buffer.rs | 112 ++++++++++++++++++- pit-basic/src/buffer/ffi.rs | 194 +++++++++++++++++++++++++++++++++ pit-basic/src/buffer64.rs | 82 ++++++++++++++ pit-basic/src/buffer64/ffi.rs | 194 +++++++++++++++++++++++++++++++++ pit-basic/src/buffer64/ic.rs | 39 +++++++ pit-basic/src/lib.rs | 21 +--- pit-core/src/lib.rs | 117 ++++++++++++++++++-- pit-patch/src/lower.rs | 35 ++++-- pit-patch/src/util.rs | 66 ++++++++++- pit-rust-externref/Cargo.toml | 7 ++ pit-rust-externref/src/lib.rs | 5 + pit-rust-generator/src/main.rs | 2 +- pit-rust-guest/src/lib.rs | 45 ++++---- pit-rust-host-lib/src/lib.rs | 2 +- 19 files changed, 932 insertions(+), 70 deletions(-) create mode 100644 buffer64.pit create mode 100644 pit-basic/src/buffer/ffi.rs create mode 100644 pit-basic/src/buffer64.rs create mode 100644 pit-basic/src/buffer64/ffi.rs create mode 100644 pit-basic/src/buffer64/ic.rs create mode 100644 pit-rust-externref/Cargo.toml create mode 100644 pit-rust-externref/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 089f324..e17d47b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,7 +299,9 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b078507c9badc1c2304de402f373950b30ef80b793096cd2a8c0312c8c676cad" dependencies = [ + "anyhow", "externref-macro", + "walrus", ] [[package]] @@ -394,6 +396,27 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "ic-stable-structures" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03f3044466a69802de74e710dc0300b706a05696a0531c942ca856751a13b0db" +dependencies = [ + "ic_principal", +] + +[[package]] +name = "ic_principal" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1762deb6f7c8d8c2bdee4b6c5a47b60195b74e9b5280faa5ba29692f8e17429c" + +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + [[package]] name = "indexmap" version = "2.2.6" @@ -508,6 +531,7 @@ name = "pit-basic" version = "0.1.0" dependencies = [ "externref", + "ic-stable-structures", ] [[package]] @@ -533,6 +557,13 @@ dependencies = [ "waffle-ast", ] +[[package]] +name = "pit-rust-externref" +version = "0.1.0" +dependencies = [ + "externref", +] + [[package]] name = "pit-rust-generator" version = "0.1.0" @@ -599,7 +630,7 @@ dependencies = [ "stacker", "structopt", "wasm-encoder", - "wasmparser", + "wasmparser 0.202.0", ] [[package]] @@ -949,6 +980,32 @@ dependencies = [ "portal-pc-waffle", ] +[[package]] +name = "walrus" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb08e48cde54c05f363d984bb54ce374f49e242def9468d2e1b6c2372d291f8" +dependencies = [ + "anyhow", + "id-arena", + "leb128", + "log", + "walrus-macro", + "wasmparser 0.77.1", +] + +[[package]] +name = "walrus-macro" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e5bd22c71e77d60140b0bd5be56155a37e5bd14e24f5f87298040d0cc40d7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "wasm-encoder" version = "0.202.0" @@ -970,6 +1027,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "wasmparser" +version = "0.77.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fe3d5405e9ea6c1317a656d6e0820912d8b7b3607823a7596117c8f666daf6f" + [[package]] name = "wasmparser" version = "0.202.0" diff --git a/Cargo.toml b/Cargo.toml index 0011e5f..ab20354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members=[ "pit-basic","pit-core", "pit-patch", "pit-rust-generator", "pit-rust-guest", "pit-rust-host", "pit-rust-host-lib"] +members=[ "pit-basic","pit-core", "pit-patch", "pit-rust-externref", "pit-rust-generator", "pit-rust-guest", "pit-rust-host", "pit-rust-host-lib"] resolver="2" [workspace.dependencies] diff --git a/buffer64.pit b/buffer64.pit new file mode 100644 index 0000000..f3b1c76 --- /dev/null +++ b/buffer64.pit @@ -0,0 +1,5 @@ +{ + read8(I64) -> (I32); + write8(I64,I32) -> (); + size() -> (I64) +} \ No newline at end of file diff --git a/pit-basic/Cargo.toml b/pit-basic/Cargo.toml index 4db5d3e..6a841aa 100644 --- a/pit-basic/Cargo.toml +++ b/pit-basic/Cargo.toml @@ -5,3 +5,9 @@ edition = "2021" [dependencies] externref = "0.2.0" +ic-stable-structures = { version = "0.6.5", optional = true } + +[features] +host=[] +host-ic=["host","ic-stable-structures"] +ic-stable-structures = ["dep:ic-stable-structures"] diff --git a/pit-basic/build.sh b/pit-basic/build.sh index de9a0b2..c17951a 100644 --- a/pit-basic/build.sh +++ b/pit-basic/build.sh @@ -1,3 +1,4 @@ set -e cd $(dirname $0) -cargo run -p pit-rust-generator ../buffer.pit src/buffer.rs \ No newline at end of file +cargo run -p pit-rust-generator ../buffer.pit src/buffer/ffi.rs +cargo run -p pit-rust-generator ../buffer64.pit src/buffer64/ffi.rs \ No newline at end of file diff --git a/pit-basic/src/buffer.rs b/pit-basic/src/buffer.rs index 5a5a8a0..a5226bf 100644 --- a/pit-basic/src/buffer.rs +++ b/pit-basic/src/buffer.rs @@ -1 +1,111 @@ -pub trait R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 { fn read8 (& self , p0 : u32) -> (u32) ; fn size (& self) -> (u32) ; fn write8 (& self , p0 : u32 , p1 : u32) -> () ; } const _ : () = { # [link_section = ".pit-types"] static SECTION_CONTENT : [u8 ; 60usize] = [123u8 , 114u8 , 101u8 , 97u8 , 100u8 , 56u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 59u8 , 115u8 , 105u8 , 122u8 , 101u8 , 40u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 59u8 , 119u8 , 114u8 , 105u8 , 116u8 , 101u8 , 56u8 , 40u8 , 73u8 , 51u8 , 50u8 , 44u8 , 73u8 , 51u8 , 50u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 41u8 , 125u8 , 0u8] ; impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> { fn read8 (& self , p0 : u32) -> (u32) { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "read8")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> , p0 : u32) -> (u32) ; } return unsafe { go (self ,p0) } ; } fn size (& self) -> (u32) { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "size")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >>) -> (u32) ; } return unsafe { go (self ,) } ; } fn write8 (& self , p0 : u32 , p1 : u32) -> () { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "write8")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> , p0 : u32 , p1 : u32) -> () ; } return unsafe { go (self ,p0 , p1) } ; } } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e.drop"] extern "C" fn _drop (a : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) { unsafe { Box :: from_raw (a) } ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/read8"] extern "C" fn read8 (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 > , p0 : u32) -> (u32) { return unsafe { & mut * id } .read8 (p0) ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/size"] extern "C" fn size (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> (u32) { return unsafe { & mut * id } .size () ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/write8"] extern "C" fn write8 (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 > , p0 : u32 , p1 : u32) -> () { return unsafe { & mut * id } .write8 (p0 , p1) ; } } ; mod R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5_utils { use super :: R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 ; # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e")] fn _push (a : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >>; } pub fn push (a : Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> { return unsafe { _push (Box :: into_raw (Box :: new (a))) } } } \ No newline at end of file +pub mod ffi; +pub use ffi::*; + +use crate::buffer64::R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d; +macro_rules! buffer_slice_impl { + ($t:ty) => { + impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for $t { + fn read8(&mut self, p0: u32) -> (u32) { + return self[p0 as usize].into(); + } + + fn write8(&mut self, p0: u32, p1: u32) -> () { + self[p0 as usize] = (p1 & 0xff) as u8; + } + + fn size(&mut self) -> (u32) { + return self.len().try_into().unwrap(); + } + } + }; +} +buffer_slice_impl!(Vec); +buffer_slice_impl!(Box<[u8]>); +buffer_slice_impl!(&'static mut [u8]); +pub fn slice<'a>( + x: &'a mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, +) -> impl Iterator + 'a { + return (0..x.size()).map(|a| (x.read8(a) & 0xff) as u8); +} +pub fn copy<'a, 'b>( + a: &'a mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + ai: u32, + b: &'b mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + bi: u32, +) { + let l = (a.size() - ai).min(b.size() - bi); + for i in 0..l { + a.write8(ai + i, b.read8(bi + i)); + } +} +pub fn copy_slice_in<'a,'b>( + a: &'a mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + ai: u32, + b: &'b [u8] +){ + for (i,b) in b.iter().enumerate(){ + if (i as u32) + ai >= a.size(){ + return; + } + a.write8(ai + (i as u32), *b as u32) + } +} +pub fn copy_slice_out<'a,'b>( + c: &'a mut [u8], + b: &'b mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + bi: u32, +){ + for (i,c) in c.iter_mut().enumerate(){ + if (i as u32) + bi >= b.size(){ + return; + } + *c = (b.read8(bi + (i as u32)) & 0xff) as u8; + } +} +pub struct Snip{ + pub wrapped: T, +} +impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Snip{ + fn read8(&mut self, p0: u32) -> (u32) { + self.wrapped.read8(p0 as u64) + } + + fn size(&mut self) -> (u32) { + self.wrapped.size() as u32 + } + + fn write8(&mut self, p0: u32, p1: u32) -> () { + self.wrapped.write8(p0 as u64, p1) + } +} +impl R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d for Snip{ + fn read8(&mut self, p0: u64) -> (u32) { + self.wrapped.read8(p0 as u32) + } + + fn size(&mut self) -> (u64) { + self.wrapped.size().into() + } + + fn write8(&mut self, p0: u64, p1: u32) -> () { + self.wrapped.write8(p0 as u32, p1) + } +} +pub struct Slice{ + pub wrapped: T, + pub begin: u32, + pub size: u32 +} +impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Slice{ + fn read8(&mut self, p0: u32) -> (u32) { + self.wrapped.read8(p0 + self.begin) + } + + fn size(&mut self) -> (u32) { + self.size + } + + fn write8(&mut self, p0: u32, p1: u32) -> () { + self.wrapped.write8(p0 + self.begin, p1) + } +} \ No newline at end of file diff --git a/pit-basic/src/buffer/ffi.rs b/pit-basic/src/buffer/ffi.rs new file mode 100644 index 0000000..ec941dd --- /dev/null +++ b/pit-basic/src/buffer/ffi.rs @@ -0,0 +1,194 @@ +pub trait R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 { + fn read8(&mut self, p0: u32) -> (u32); + fn size(&mut self) -> (u32); + fn write8(&mut self, p0: u32, p1: u32) -> (); +} +const _: () = { + #[link_section = ".pit-types"] + static SECTION_CONTENT: [u8; 60usize] = [ + 123u8, + 114u8, + 101u8, + 97u8, + 100u8, + 56u8, + 40u8, + 73u8, + 51u8, + 50u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 73u8, + 51u8, + 50u8, + 41u8, + 59u8, + 115u8, + 105u8, + 122u8, + 101u8, + 40u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 73u8, + 51u8, + 50u8, + 41u8, + 59u8, + 119u8, + 114u8, + 105u8, + 116u8, + 101u8, + 56u8, + 40u8, + 73u8, + 51u8, + 50u8, + 44u8, + 73u8, + 51u8, + 50u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 41u8, + 125u8, + 0u8, + ]; + impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 + for ::externref::Resource< + Box, + > { + fn read8(&mut self, p0: u32) -> (u32) { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5" + )] + extern "C" { + #[link_name = "read8"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + >, + p0: u32, + ) -> (u32); + } + return unsafe { go(self, p0) }; + } + fn size(&mut self) -> (u32) { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5" + )] + extern "C" { + #[link_name = "size"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + >, + ) -> (u32); + } + return unsafe { go(self) }; + } + fn write8(&mut self, p0: u32, p1: u32) -> () { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5" + )] + extern "C" { + #[link_name = "write8"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + >, + p0: u32, + p1: u32, + ) -> (); + } + return unsafe { go(self, p0, p1) }; + } + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e.drop"] + extern "C" fn _drop( + a: *mut Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + ) { + unsafe { Box::from_raw(a) }; + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/read8"] + extern "C" fn read8( + id: *mut Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + p0: u32, + ) -> (u32) { + return unsafe { &mut *id }.read8(p0); + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/size"] + extern "C" fn size( + id: *mut Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + ) -> (u32) { + return unsafe { &mut *id }.size(); + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/write8"] + extern "C" fn write8( + id: *mut Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + p0: u32, + p1: u32, + ) -> () { + return unsafe { &mut *id }.write8(p0, p1); + } + impl From> + for ::externref::Resource< + Box, + > { + fn from( + a: Box, + ) -> Self { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5" + )] + extern "C" { + #[link_name = "~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e"] + fn _push( + a: *mut Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + ) -> ::externref::Resource< + Box< + dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, + >, + >; + } + return unsafe { _push(Box::into_raw(Box::new(a))) }; + } + } +}; diff --git a/pit-basic/src/buffer64.rs b/pit-basic/src/buffer64.rs new file mode 100644 index 0000000..ae0a3bb --- /dev/null +++ b/pit-basic/src/buffer64.rs @@ -0,0 +1,82 @@ +pub mod ffi; +pub use ffi::*; +#[cfg(feature = "ic-stable-structures")] +pub mod ic; +macro_rules! buffer_slice_impl { + ($t:ty) => { + impl R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d for $t { + fn read8(&mut self, p0: u64) -> (u32) { + return self[p0 as usize].into(); + } + + fn write8(&mut self, p0: u64, p1: u32) -> () { + self[p0 as usize] = (p1 & 0xff) as u8; + } + + fn size(&mut self) -> (u64) { + return self.len().try_into().unwrap(); + } + } + }; +} +buffer_slice_impl!(Vec); +buffer_slice_impl!(Box<[u8]>); +buffer_slice_impl!(&'static mut [u8]); +pub fn slice<'a>( + x: &'a mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, +) -> impl Iterator + 'a { + return (0..x.size()).map(|a| (x.read8(a) & 0xff) as u8); +} +pub fn copy<'a, 'b>( + a: &'a mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + ai: u64, + b: &'b mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + bi: u64, +) { + let l = (a.size() - ai).min(b.size() - bi); + for i in 0..l { + a.write8(ai + i, b.read8(bi + i)); + } +} +pub fn copy_slice_in<'a, 'b>( + a: &'a mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + ai: u64, + b: &'b [u8], +) { + for (i, b) in b.iter().enumerate() { + if (i as u64) + ai >= a.size() { + return; + } + a.write8(ai + (i as u64), *b as u32) + } +} +pub fn copy_slice_out<'a, 'b>( + c: &'a mut [u8], + b: &'b mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + bi: u64, +) { + for (i, c) in c.iter_mut().enumerate() { + if (i as u64) + bi >= b.size() { + return; + } + *c = (b.read8(bi + (i as u64)) & 0xff) as u8; + } +} +pub struct Slice{ + pub wrapped: T, + pub begin: u64, + pub size: u64 +} +impl R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d for Slice{ + fn read8(&mut self, p0: u64) -> (u32) { + self.wrapped.read8(p0 + self.begin) + } + + fn size(&mut self) -> (u64) { + self.size + } + + fn write8(&mut self, p0: u64, p1: u32) -> () { + self.wrapped.write8(p0 + self.begin, p1) + } +} \ No newline at end of file diff --git a/pit-basic/src/buffer64/ffi.rs b/pit-basic/src/buffer64/ffi.rs new file mode 100644 index 0000000..f7c790d --- /dev/null +++ b/pit-basic/src/buffer64/ffi.rs @@ -0,0 +1,194 @@ +pub trait R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d { + fn read8(&mut self, p0: u64) -> (u32); + fn size(&mut self) -> (u64); + fn write8(&mut self, p0: u64, p1: u32) -> (); +} +const _: () = { + #[link_section = ".pit-types"] + static SECTION_CONTENT: [u8; 60usize] = [ + 123u8, + 114u8, + 101u8, + 97u8, + 100u8, + 56u8, + 40u8, + 73u8, + 54u8, + 52u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 73u8, + 51u8, + 50u8, + 41u8, + 59u8, + 115u8, + 105u8, + 122u8, + 101u8, + 40u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 73u8, + 54u8, + 52u8, + 41u8, + 59u8, + 119u8, + 114u8, + 105u8, + 116u8, + 101u8, + 56u8, + 40u8, + 73u8, + 54u8, + 52u8, + 44u8, + 73u8, + 51u8, + 50u8, + 41u8, + 32u8, + 45u8, + 62u8, + 32u8, + 40u8, + 41u8, + 125u8, + 0u8, + ]; + impl R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d + for ::externref::Resource< + Box, + > { + fn read8(&mut self, p0: u64) -> (u32) { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d" + )] + extern "C" { + #[link_name = "read8"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + >, + p0: u64, + ) -> (u32); + } + return unsafe { go(self, p0) }; + } + fn size(&mut self) -> (u64) { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d" + )] + extern "C" { + #[link_name = "size"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + >, + ) -> (u64); + } + return unsafe { go(self) }; + } + fn write8(&mut self, p0: u64, p1: u32) -> () { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d" + )] + extern "C" { + #[link_name = "write8"] + fn go( + this: &mut ::externref::Resource< + Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + >, + p0: u64, + p1: u32, + ) -> (); + } + return unsafe { go(self, p0, p1) }; + } + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d/~b3435bc747738a8874da05bf54d1e6b7c57bbab9ef27b0d40a5db3c3c8b6e5b9.drop"] + extern "C" fn _drop( + a: *mut Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + ) { + unsafe { Box::from_raw(a) }; + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d/~b3435bc747738a8874da05bf54d1e6b7c57bbab9ef27b0d40a5db3c3c8b6e5b9/read8"] + extern "C" fn read8( + id: *mut Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + p0: u64, + ) -> (u32) { + return unsafe { &mut *id }.read8(p0); + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d/~b3435bc747738a8874da05bf54d1e6b7c57bbab9ef27b0d40a5db3c3c8b6e5b9/size"] + extern "C" fn size( + id: *mut Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + ) -> (u64) { + return unsafe { &mut *id }.size(); + } + #[::externref::externref(crate = ":: externref")] + #[export_name = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d/~b3435bc747738a8874da05bf54d1e6b7c57bbab9ef27b0d40a5db3c3c8b6e5b9/write8"] + extern "C" fn write8( + id: *mut Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + p0: u64, + p1: u32, + ) -> () { + return unsafe { &mut *id }.write8(p0, p1); + } + impl From> + for ::externref::Resource< + Box, + > { + fn from( + a: Box, + ) -> Self { + #[::externref::externref(crate = ":: externref")] + #[link( + wasm_import_module = "pit/68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d" + )] + extern "C" { + #[link_name = "~b3435bc747738a8874da05bf54d1e6b7c57bbab9ef27b0d40a5db3c3c8b6e5b9"] + fn _push( + a: *mut Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + ) -> ::externref::Resource< + Box< + dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d, + >, + >; + } + return unsafe { _push(Box::into_raw(Box::new(a))) }; + } + } +}; diff --git a/pit-basic/src/buffer64/ic.rs b/pit-basic/src/buffer64/ic.rs new file mode 100644 index 0000000..a7c4155 --- /dev/null +++ b/pit-basic/src/buffer64/ic.rs @@ -0,0 +1,39 @@ +use ic_stable_structures::Memory; + +pub struct MemBuf { + pub wrapped: T, +} +impl super::R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d + for MemBuf +{ + fn read8(&mut self, p0: u64) -> (u32) { + let mut a = [0u8; 1]; + self.wrapped.read(p0, &mut a); + return a[0] as u32; + } + + fn size(&mut self) -> (u64) { + return self.wrapped.size() * 65536; + } + + fn write8(&mut self, p0: u64, p1: u32) -> () { + self.wrapped.write(p0, &[(p1 & 0xff) as u8]) + } +} +impl crate::buffer::R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 + for MemBuf +{ + fn read8(&mut self, p0: u32) -> (u32) { + let mut a = [0u8; 1]; + self.wrapped.read(p0.into(), &mut a); + return a[0] as u32; + } + + fn size(&mut self) -> (u32) { + return ((self.wrapped.size() * 65536) & 0xffffffff) as u32; + } + + fn write8(&mut self, p0: u32, p1: u32) -> () { + self.wrapped.write(p0.into(), &[(p1 & 0xff) as u8]) + } +} diff --git a/pit-basic/src/lib.rs b/pit-basic/src/lib.rs index 2841d2f..29ff341 100644 --- a/pit-basic/src/lib.rs +++ b/pit-basic/src/lib.rs @@ -3,24 +3,5 @@ use std::{ sync::Mutex, }; -include!("buffer.rs"); -impl + DerefMut> - R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Mutex -{ - fn read8(&self, p0: u32) -> (u32) { - return self.lock().unwrap()[p0 as usize].into(); - } - fn write8(&self, p0: u32, p1: u32) -> () { - self.lock().unwrap()[p0 as usize] = (p1 & 0xff) as u8; - } - - fn size(&self) -> (u32) { - return self.lock().unwrap().len().try_into().unwrap(); - } -} -pub fn slice( - x: &dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5, -) -> impl Iterator + '_ { - return (0..x.size()).map(|a| (x.read8(a) & 0xff) as u8); -} +pub mod buffer;pub mod buffer64; \ No newline at end of file diff --git a/pit-core/src/lib.rs b/pit-core/src/lib.rs index 50bddd2..c9ed10c 100644 --- a/pit-core/src/lib.rs +++ b/pit-core/src/lib.rs @@ -1,9 +1,73 @@ use derive_more::Display; use nom::{ - bytes::complete::{is_not, tag, take, take_while_m_n}, character::complete::{alpha1, alphanumeric1, char, multispace0}, combinator::opt, error::Error, multi::{many0, separated_list0}, sequence::{delimited, tuple}, IResult + bytes::complete::{is_not, tag, take, take_while_m_n}, + character::complete::{alpha1, alphanumeric1, char, multispace0}, + combinator::opt, + error::Error, + multi::{many0, separated_list0}, + sequence::{delimited, tuple}, + IResult, }; use sha3::{Digest, Sha3_256}; use std::{collections::BTreeMap, fmt::Display}; +#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] +pub struct Attr { + pub name: String, + pub value: String, +} + +pub fn parse_balanced(mut a: &str) -> IResult<&str, String> { + let mut v = vec![]; + let mut i = 0; + loop { + let (b, x) = nom::character::complete::anychar(a)?; + match x { + '[' => i += 1, + ']' => { + if i == 0 { + return Ok((a, v.into_iter().collect())); + } + i -= 1; + } + _ => {} + } + a = b; + v.push(x) + } +} + +pub fn parse_attr(a: &str) -> IResult<&str, Attr> { + let (a, _) = multispace0(a)?; + let (a, _) = char('[')(a)?; + let (a, _) = multispace0(a)?; + let (a, name) = alphanumeric1(a)?; + let (a, _) = multispace0(a)?; + let (a, _) = char('=')(a)?; + let (a, _) = multispace0(a)?; + let (a, value) = parse_balanced(a)?; + let (a, _) = char(']')(a)?; + let (a, _) = multispace0(a)?; + return Ok(( + a, + Attr { + name: name.to_owned(), + value, + }, + )); +} + +pub fn parse_attrs(a: &str) -> IResult<&str, Vec> { + let (a, mut b) = many0(parse_attr)(a)?; + b.sort_by_key(|a| a.name.clone()); + Ok((a, b)) +} + +impl Display for Attr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{}={}]", self.name, self.value) + } +} + #[derive(Display, Clone, Eq, PartialEq, PartialOrd, Ord)] pub enum ResTy { #[display(fmt = "")] @@ -37,17 +101,25 @@ pub enum Arg { I64, F32, F64, - #[display(fmt = "R{}{}{}", "ty", "if *nullable{\"n\"}else{\"\"}","if *take{\"\"}else{\"&\"}")] + #[display( + fmt = "{}R{}{}{}", + "ann.iter().map(|a|a.to_string()).collect::>().join(\"\")", + "ty", + "if *nullable{\"n\"}else{\"\"}", + "if *take{\"\"}else{\"&\"}" + )] Resource { ty: ResTy, nullable: bool, take: bool, + ann: Vec, }, // #[display(fmt = "{}", _0)] // Func(Sig), } pub fn parse_arg(a: &str) -> IResult<&str, Arg> { - let (a,_) = multispace0(a)?; + let (a, ann) = parse_attrs(a)?; + let (a, _) = multispace0(a)?; // let (c,b) = take(1usize)(a)?; match a.strip_prefix("R") { Some(b) => { @@ -70,6 +142,7 @@ pub fn parse_arg(a: &str) -> IResult<&str, Arg> { ty: d, nullable: k.is_some(), take: take.is_none(), + ann, }, )); } @@ -78,13 +151,13 @@ pub fn parse_arg(a: &str) -> IResult<&str, Arg> { // return Ok((a, Arg::Func(x))); // } None => { - let (a,c) = take(3usize)(a)?; + let (a, c) = take(3usize)(a)?; match c { "I32" => return Ok((a, Arg::I32)), "I64" => return Ok((a, Arg::I64)), "F32" => return Ok((a, Arg::F32)), "F64" => return Ok((a, Arg::F64)), - _ => return Err(nom::Err::Error(Error::new(a, nom::error::ErrorKind::Tag))) + _ => return Err(nom::Err::Error(Error::new(a, nom::error::ErrorKind::Tag))), } } } @@ -92,15 +165,18 @@ pub fn parse_arg(a: &str) -> IResult<&str, Arg> { } #[derive(Display, Clone, Eq, PartialEq, PartialOrd, Ord)] #[display( - fmt = "({}) -> ({})", + fmt = "{}({}) -> ({})", + "ann.iter().map(|a|a.to_string()).collect::>().join(\"\")", "params.iter().map(|a|a.to_string()).collect::>().join(\",\")", "rets.iter().map(|a|a.to_string()).collect::>().join(\",\")" )] pub struct Sig { + pub ann: Vec, pub params: Vec, pub rets: Vec, } pub fn parse_sig(a: &str) -> IResult<&str, Sig> { + let (a, b) = parse_attrs(a)?; let (a, _) = multispace0(a)?; let mut d = delimited(char('('), separated_list0(char(','), parse_arg), char(')')); let (a, params) = d(a)?; @@ -108,37 +184,54 @@ pub fn parse_sig(a: &str) -> IResult<&str, Sig> { let (a, _) = tag("->")(a)?; let (a, _) = multispace0(a)?; let (a, rets) = d(a)?; - return Ok((a, Sig { params, rets })); + return Ok(( + a, + Sig { + params, + rets, + ann: b, + }, + )); } #[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] pub struct Interface { pub methods: BTreeMap, + pub ann: Vec, } impl Display for Interface { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}","{")?; - for (i, (a, b)) in self.methods.iter().enumerate() { + for a in self.ann.iter() { + write!(f, "{a}")?; + } + write!(f, "{}", "{")?; + let mut x = self.methods.iter().collect::>(); + x.sort_by_key(|a| a.0); + for (i, (a, b)) in x.into_iter().enumerate() { if i != 0 { write!(f, ";")?; } write!(f, "{}{}", a, b)?; } - return write!(f, "{}","}"); + return write!(f, "{}", "}"); } } pub fn parse_interface(a: &str) -> IResult<&str, Interface> { pub fn go(a: &str) -> IResult<&str, Interface> { let (a, s) = separated_list0(char(';'), tuple((multispace0, alphanumeric1, parse_sig)))(a)?; - let (a,_) = multispace0(a)?; + let (a, _) = multispace0(a)?; return Ok(( a, Interface { methods: s.into_iter().map(|(_, a, b)| (a.to_owned(), b)).collect(), + ann: vec![], }, )); } let (a, _) = multispace0(a)?; - return delimited(char('{'), go, char('}'))(a); + let (a, b) = parse_attrs(a)?; + let (a, mut c) = delimited(char('{'), go, char('}'))(a)?; + c.ann = b; + return Ok((a, c)); } impl Interface { pub fn rid(&self) -> [u8; 32] { diff --git a/pit-patch/src/lower.rs b/pit-patch/src/lower.rs index 31abe47..63a20bf 100644 --- a/pit-patch/src/lower.rs +++ b/pit-patch/src/lower.rs @@ -36,7 +36,11 @@ pub fn import_fn(m: &mut Module, mo: String, n: String, s: SignatureData) -> Fun }); return f; } -pub fn instantiate(m: &mut Module, root: &str) -> anyhow::Result<()> { +pub struct Cfg { + pub unexportable_i32_tables: bool, +} +pub fn instantiate(m: &mut Module, cfg: &Cfg) -> anyhow::Result<()> { + let root = "pit_patch_internal_instantiate"; let i = crate::get_interfaces(m)?; let interfaces = i .into_iter() @@ -110,19 +114,20 @@ pub fn instantiate(m: &mut Module, root: &str) -> anyhow::Result<()> { }, ); f.append_to_block(b, w); - let id = format!("pit-patch-rt/{}", o.to_string().split_once("<").unwrap().0); + let id = + format!("pit-patch-rt/{}", o.to_string().split_once("<").unwrap().0); let mut a = module.exports.iter(); - let a = loop{ - let Some(b) = a.next() else{ + let a = loop { + let Some(b) = a.next() else { anyhow::bail!("pit patch rt not found") }; - if b.name != id{ + if b.name != id { continue; } - let ExportKind::Func(a) = &b.kind else{ + let ExportKind::Func(a) = &b.kind else { continue; }; - break *a; + break *a; }; DontObf {}.obf( Operator::Call { function_index: a }, @@ -140,10 +145,18 @@ pub fn instantiate(m: &mut Module, root: &str) -> anyhow::Result<()> { } } } - obf_mod(m, &mut X {})?; - for t in m.tables.values_mut() { - if t.ty == Type::ExternRef { - t.ty = Type::FuncRef; + if cfg.unexportable_i32_tables { + for t in m.tables.values_mut() { + if t.ty == Type::ExternRef { + t.ty = Type::I32; + } + } + } else { + obf_mod(m, &mut X {})?; + for t in m.tables.values_mut() { + if t.ty == Type::ExternRef { + t.ty = Type::FuncRef; + } } } for i in take(&mut m.imports) { diff --git a/pit-patch/src/util.rs b/pit-patch/src/util.rs index 5bc0bf8..2460dc7 100644 --- a/pit-patch/src/util.rs +++ b/pit-patch/src/util.rs @@ -1,6 +1,10 @@ -use waffle::{util::new_sig, BlockTarget, Func, FunctionBody, Module, Operator, SignatureData, Table, Type}; -use waffle_ast::{add_op, Builder, Expr}; +use std::collections::BTreeMap; +use waffle::{ + util::new_sig, BlockTarget, Func, FunctionBody, Import, ImportKind, Module, Operator, + SignatureData, Table, Type, +}; +use waffle_ast::{add_op, Builder, Expr}; pub fn talloc(m: &mut Module, t: Table) -> anyhow::Result { let e = m.tables[t].ty.clone(); @@ -127,3 +131,61 @@ pub fn tfree(m: &mut Module, t: Table) -> anyhow::Result { .funcs .push(waffle::FuncDecl::Body(sig, format!("tfree"), f))); } + +// use waffle::{util::new_sig, Module}; + +pub fn to_waffle_type(t: &pit_core::Arg) -> waffle::Type { + match t { + pit_core::Arg::I32 => waffle::Type::I32, + pit_core::Arg::I64 => waffle::Type::I64, + pit_core::Arg::F32 => waffle::Type::F32, + pit_core::Arg::F64 => waffle::Type::F64, + pit_core::Arg::Resource { + ty, + nullable, + take, + ann, + } => waffle::Type::ExternRef, + } +} +pub fn to_waffle_sig(m: &mut Module, t: &pit_core::Sig) -> waffle::Signature { + return new_sig( + m, + waffle::SignatureData { + params: t.params.iter().map(to_waffle_type).collect(), + returns: t.rets.iter().map(to_waffle_type).collect(), + }, + ); +} +pub fn waffle_funcs(m: &mut Module, i: &pit_core::Interface) -> BTreeMap { + return i + .methods + .iter() + .map(|(a, b)| { + let module = format!("pit/{}", i.rid_str()); + let name = a.clone(); + if let Some(f) = m.imports.iter().find_map(|i| { + if i.module == module && i.name == name { + match &i.kind { + ImportKind::Func(f) => Some(*f), + _ => None, + } + } else { + None + } + }) { + return (a.clone(), f); + }; + let s = to_waffle_sig(m, b); + let f = m + .funcs + .push(waffle::FuncDecl::Import(s, format!("{module}.{name}"))); + m.imports.push(Import { + module, + name, + kind: waffle::ImportKind::Func(f), + }); + (a.clone(), f) + }) + .collect(); +} diff --git a/pit-rust-externref/Cargo.toml b/pit-rust-externref/Cargo.toml new file mode 100644 index 0000000..6b02b61 --- /dev/null +++ b/pit-rust-externref/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "pit-rust-externref" +version = "0.1.0" +edition = "2021" + +[dependencies] +externref = { version = "0.2.0", features = ["processor"] } diff --git a/pit-rust-externref/src/lib.rs b/pit-rust-externref/src/lib.rs new file mode 100644 index 0000000..9d24bcd --- /dev/null +++ b/pit-rust-externref/src/lib.rs @@ -0,0 +1,5 @@ +pub fn setup<'a, 'b>( + x: &'a mut externref::processor::Processor<'b>, +) -> &'a mut externref::processor::Processor<'b> { + x.set_drop_fn("pit", "drop") +} diff --git a/pit-rust-generator/src/main.rs b/pit-rust-generator/src/main.rs index 87eef37..37aa19b 100644 --- a/pit-rust-generator/src/main.rs +++ b/pit-rust-generator/src/main.rs @@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()>{ }; let v = pit_rust_guest::render(&x, &src); - // let v = prettyplease::unparse(&syn::parse_file(&v.to_string())?); + let v = prettyplease::unparse(&syn::parse_file(&v.to_string())?); let Some(dst) = a.next() else{ println!("{v}"); return Ok(()); diff --git a/pit-rust-guest/src/lib.rs b/pit-rust-guest/src/lib.rs index 97535b6..f04ab9d 100644 --- a/pit-rust-guest/src/lib.rs +++ b/pit-rust-guest/src/lib.rs @@ -20,15 +20,15 @@ pub fn render(opts: &Opts, i: &Interface) -> TokenStream{ let id2 = format_ident!("R{}",i.rid_str()); let internal = format_ident!("R{id}_utils"); let methods = i.methods.iter().map(|(a,b)|quasiquote! { - fn #{format_ident!("{a}")}#{render_sig(root,i,b,"e! {&self})}; + fn #{format_ident!("{a}")}#{render_sig(root,i,b,"e! {&mut self})}; }); let impl_dyns = i.methods.iter().map(|(a,b)|quasiquote! { - fn #{format_ident!("{a}")}#{render_sig(root,i,b,"e! {&self})}{ + fn #{format_ident!("{a}")}#{render_sig(root,i,b,"e! {&mut self})}{ #[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})] #[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})] extern "C"{ - #[link(wasm_import_name = #a)] - fn go #{render_sig(root, i,b, "e! {this: &#root::externref::Resource>})}; + #[link_name = #a] + fn go #{render_sig(root, i,b, "e! {this: &mut #root::externref::Resource>})}; } return unsafe{go(self,#{ let params = b.params.iter().enumerate().map(|(a,_)|format_ident!("p{a}")); @@ -74,21 +74,28 @@ pub fn render(opts: &Opts, i: &Interface) -> TokenStream{ }; } #(#chains2)* - }; - mod #internal{ - use super::#id2; - #[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})] - #[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})] - extern "C"{ - #[link(wasm_import_name = #{format!("~{ha}")})] - fn _push(a: *mut Box) -> #root::externref::Resource>; - } - pub fn push(a: Box) -> #root::externref::Resource>{ - return unsafe{ - _push(Box::into_raw(Box::new(a))) + impl From> for #root::externref::Resource>{ + fn from(a: Box) -> Self{ + #[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})] + #[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})] + extern "C"{ + #[link_name = #{format!("~{ha}")}] + fn _push(a: *mut Box) -> #root::externref::Resource>; + } + return unsafe{ + _push(Box::into_raw(Box::new(a))) + } } } - } + }; + // mod #internal{ + // use super::#id2; + // pub fn push(a: Box) -> #root::externref::Resource>{ + // return unsafe{ + // _push(Box::into_raw(Box::new(a))) + // } + // } + // } } } pub fn render_sig(root: &TokenStream,base: &Interface, s: &Sig, self_: &TokenStream) -> TokenStream{ @@ -113,7 +120,7 @@ pub fn render_ty(root: &TokenStream,base:&Interface, p: &Arg) -> TokenStream{ Arg::F64 => quote! { f64 }, - Arg::Resource{ty,nullable,take} => { + Arg::Resource{ty,nullable,take, ann } => { let ty = match ty{ ResTy::Of(a) => quasiquote!{ #root::externref::Resource> @@ -133,7 +140,7 @@ pub fn render_ty(root: &TokenStream,base:&Interface, p: &Arg) -> TokenStream{ let ty = if *take{ ty }else{ - quote! {&#ty} + quote! {&mut #ty} }; ty }, diff --git a/pit-rust-host-lib/src/lib.rs b/pit-rust-host-lib/src/lib.rs index e76785f..bc90269 100644 --- a/pit-rust-host-lib/src/lib.rs +++ b/pit-rust-host-lib/src/lib.rs @@ -40,7 +40,7 @@ pub fn emit_ty(a: &Arg) -> ValueType { Arg::I64 => ValueType::I64, Arg::F32 => ValueType::F32, Arg::F64 => ValueType::F64, - Arg::Resource { ty, nullable, take } => ValueType::ExternRef, + Arg::Resource { ty, nullable, take, ann } => ValueType::ExternRef, // Arg::Func(f) => ValueType::FuncRef, } }