This commit is contained in:
Graham Kelly 2024-07-28 12:54:29 -04:00
parent c36c85fa4f
commit 18eb56d314
19 changed files with 932 additions and 70 deletions

65
Cargo.lock generated
View file

@ -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"

View file

@ -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]

5
buffer64.pit Normal file
View file

@ -0,0 +1,5 @@
{
read8(I64) -> (I32);
write8(I64,I32) -> ();
size() -> (I64)
}

View file

@ -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"]

View file

@ -1,3 +1,4 @@
set -e
cd $(dirname $0)
cargo run -p pit-rust-generator ../buffer.pit src/buffer.rs
cargo run -p pit-rust-generator ../buffer.pit src/buffer/ffi.rs
cargo run -p pit-rust-generator ../buffer64.pit src/buffer64/ffi.rs

View file

@ -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))) } } }
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<u8>);
buffer_slice_impl!(Box<[u8]>);
buffer_slice_impl!(&'static mut [u8]);
pub fn slice<'a>(
x: &'a mut dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5,
) -> impl Iterator<Item = u8> + '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<T>{
pub wrapped: T,
}
impl<T: crate::buffer64::R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d> R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Snip<T>{
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<T: R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5> R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d for Snip<T>{
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<T>{
pub wrapped: T,
pub begin: u32,
pub size: u32
}
impl<T: R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5> R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Slice<T>{
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)
}
}

194
pit-basic/src/buffer/ffi.rs Normal file
View file

@ -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<dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5>,
> {
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<Box<dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5>>
for ::externref::Resource<
Box<dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5>,
> {
fn from(
a: Box<dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5>,
) -> 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))) };
}
}
};

82
pit-basic/src/buffer64.rs Normal file
View file

@ -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<u8>);
buffer_slice_impl!(Box<[u8]>);
buffer_slice_impl!(&'static mut [u8]);
pub fn slice<'a>(
x: &'a mut dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d,
) -> impl Iterator<Item = u8> + '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<T>{
pub wrapped: T,
pub begin: u64,
pub size: u64
}
impl<T: R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d> R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d for Slice<T>{
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)
}
}

View file

@ -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<dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d>,
> {
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<Box<dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d>>
for ::externref::Resource<
Box<dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d>,
> {
fn from(
a: Box<dyn R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d>,
) -> 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))) };
}
}
};

View file

@ -0,0 +1,39 @@
use ic_stable_structures::Memory;
pub struct MemBuf<T> {
pub wrapped: T,
}
impl<T: Memory> super::R68da167712ddf1601aed7908c99972e62a41bdea1e28b241306a6b58d29e532d
for MemBuf<T>
{
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<T: Memory> crate::buffer::R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5
for MemBuf<T>
{
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])
}
}

View file

@ -3,24 +3,5 @@ use std::{
sync::Mutex,
};
include!("buffer.rs");
impl<T: Deref<Target = [u8]> + DerefMut>
R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Mutex<T>
{
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<Item = u8> + '_ {
return (0..x.size()).map(|a| (x.read8(a) & 0xff) as u8);
}
pub mod buffer;pub mod buffer64;

View file

@ -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<Attr>> {
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::<Vec<_>>().join(\"\")",
"ty",
"if *nullable{\"n\"}else{\"\"}",
"if *take{\"\"}else{\"&\"}"
)]
Resource {
ty: ResTy,
nullable: bool,
take: bool,
ann: Vec<Attr>,
},
// #[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::<Vec<_>>().join(\"\")",
"params.iter().map(|a|a.to_string()).collect::<Vec<_>>().join(\",\")",
"rets.iter().map(|a|a.to_string()).collect::<Vec<_>>().join(\",\")"
)]
pub struct Sig {
pub ann: Vec<Attr>,
pub params: Vec<Arg>,
pub rets: Vec<Arg>,
}
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<String, Sig>,
pub ann: Vec<Attr>,
}
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::<Vec<_>>();
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] {

View file

@ -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,16 +114,17 @@ 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;
@ -140,12 +145,20 @@ pub fn instantiate(m: &mut Module, root: &str) -> anyhow::Result<()> {
}
}
}
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) {
if let Some(rid) = i.module.strip_prefix("pit/") {
let ridx = interfaces

View file

@ -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<Func> {
let e = m.tables[t].ty.clone();
@ -127,3 +131,61 @@ pub fn tfree(m: &mut Module, t: Table) -> anyhow::Result<Func> {
.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<String, Func> {
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();
}

View file

@ -0,0 +1,7 @@
[package]
name = "pit-rust-externref"
version = "0.1.0"
edition = "2021"
[dependencies]
externref = { version = "0.2.0", features = ["processor"] }

View file

@ -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")
}

View file

@ -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(());

View file

@ -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,&quote! {&self})};
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&mut self})};
});
let impl_dyns = i.methods.iter().map(|(a,b)|quasiquote! {
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&self})}{
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&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, &quote! {this: &#root::externref::Resource<Box<dyn #id2>>})};
#[link_name = #a]
fn go #{render_sig(root, i,b, &quote! {this: &mut #root::externref::Resource<Box<dyn #id2>>})};
}
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;
impl From<Box<dyn #id2>> for #root::externref::Resource<Box<dyn #id2>>{
fn from(a: Box<dyn #id2>) -> 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 = #{format!("~{ha}")})]
#[link_name = #{format!("~{ha}")}]
fn _push(a: *mut Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>;
}
pub fn push(a: Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>{
return unsafe{
_push(Box::into_raw(Box::new(a)))
}
}
}
};
// mod #internal{
// use super::#id2;
// pub fn push(a: Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>{
// 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<Box<dyn #{format_ident!("R{}",hex::encode(a))}>>
@ -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
},

View file

@ -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,
}
}