Skip to content

Commit

Permalink
🧪 Force decode with named types
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Dec 27, 2024
1 parent 2ace7a9 commit d60a58f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[build]
target = "wasm32-wasip2"
#[build]
#target = "wasm32-wasip2"
3 changes: 2 additions & 1 deletion .run/test.sh → .run/build-wasi.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cargo component build --release --target wasm32-wasip2
cp target/wasm32-wasip2/release/legion_wit.wasm projects/legion-wasm32-wasi/legion-wasm32-wasi.wasm
jco transpile projects/legion-wasm32-wasi/legion-wasm32-wasi.wasm -o projects/legion-wasm32-wasi/src --name index
jco transpile projects/legion-wasm32-wasi/legion-wasm32-wasi.wasm -o projects/legion-wasm32-wasi/src \
--name index --no-namespaced-exports --multi-memory --valid-lifting-optimization --optimize
5 changes: 4 additions & 1 deletion projects/legion-wasm32-wasi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valkyrie-language/legion-wasm32-wasi",
"version": "0.0.1",
"version": "0.0.2",
"license": "UNLICENSED",
"repository": {
"type": "git",
Expand All @@ -15,6 +15,9 @@
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"scripts": {
"build": "cargo component build --release --target wasm32-wasip2"
},
"dependencies": {
"@bytecodealliance/preview2-shim": "^0.17.1"
}
Expand Down
103 changes: 55 additions & 48 deletions projects/legion-wit/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ impl ::core::fmt::Debug for EncodeConfig {
.finish()
}
}
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct DecodeConfig {
pub skeleton_only: bool,
pub name_unnamed: bool,
pub indent_text: _rt::String,
pub fold_instructions: bool,
}
impl ::core::fmt::Debug for DecodeConfig {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct("DecodeConfig")
.field("skeleton-only", &self.skeleton_only)
.field("name-unnamed", &self.name_unnamed)
.field("indent-text", &self.indent_text)
.field("fold-instructions", &self.fold_instructions)
.finish()
}
Expand All @@ -45,12 +44,14 @@ impl ::core::fmt::Debug for DecodeConfig {
pub struct PolyfillConfig {
pub name: _rt::String,
pub shim: _rt::Vec<(_rt::String, _rt::String)>,
pub debug: bool,
}
impl ::core::fmt::Debug for PolyfillConfig {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct("PolyfillConfig")
.field("name", &self.name)
.field("shim", &self.shim)
.field("debug", &self.debug)
.finish()
}
}
Expand Down Expand Up @@ -119,42 +120,45 @@ pub unsafe fn _export_wasm_decode_cabi<T: Guest>(
arg0: *mut u8,
arg1: usize,
arg2: i32,
arg3: i32,
arg4: i32,
arg3: *mut u8,
arg4: usize,
arg5: i32,
) -> *mut u8 {
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
let len0 = arg1;
let result1 = T::wasm_decode(
let len1 = arg4;
let bytes1 = _rt::Vec::from_raw_parts(arg3.cast(), len1, len1);
let result2 = T::wasm_decode(
_rt::Vec::from_raw_parts(arg0.cast(), len0, len0),
DecodeConfig {
skeleton_only: _rt::bool_lift(arg2 as u8),
name_unnamed: _rt::bool_lift(arg3 as u8),
fold_instructions: _rt::bool_lift(arg4 as u8),
indent_text: _rt::string_lift(bytes1),
fold_instructions: _rt::bool_lift(arg5 as u8),
},
);
let ptr2 = _RET_AREA.0.as_mut_ptr().cast::<u8>();
match result1 {
let ptr3 = _RET_AREA.0.as_mut_ptr().cast::<u8>();
match result2 {
Ok(e) => {
*ptr2.add(0).cast::<u8>() = (0i32) as u8;
let vec3 = (e.into_bytes()).into_boxed_slice();
let ptr3 = vec3.as_ptr().cast::<u8>();
let len3 = vec3.len();
::core::mem::forget(vec3);
*ptr2.add(8).cast::<usize>() = len3;
*ptr2.add(4).cast::<*mut u8>() = ptr3.cast_mut();
*ptr3.add(0).cast::<u8>() = (0i32) as u8;
let vec4 = (e.into_bytes()).into_boxed_slice();
let ptr4 = vec4.as_ptr().cast::<u8>();
let len4 = vec4.len();
::core::mem::forget(vec4);
*ptr3.add(8).cast::<usize>() = len4;
*ptr3.add(4).cast::<*mut u8>() = ptr4.cast_mut();
}
Err(e) => {
*ptr2.add(0).cast::<u8>() = (1i32) as u8;
let ToolsError { message: message4 } = e;
let vec5 = (message4.into_bytes()).into_boxed_slice();
let ptr5 = vec5.as_ptr().cast::<u8>();
let len5 = vec5.len();
::core::mem::forget(vec5);
*ptr2.add(8).cast::<usize>() = len5;
*ptr2.add(4).cast::<*mut u8>() = ptr5.cast_mut();
*ptr3.add(0).cast::<u8>() = (1i32) as u8;
let ToolsError { message: message5 } = e;
let vec6 = (message5.into_bytes()).into_boxed_slice();
let ptr6 = vec6.as_ptr().cast::<u8>();
let len6 = vec6.len();
::core::mem::forget(vec6);
*ptr3.add(8).cast::<usize>() = len6;
*ptr3.add(4).cast::<*mut u8>() = ptr6.cast_mut();
}
};
ptr2
ptr3
}
#[doc(hidden)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -182,6 +186,7 @@ pub unsafe fn _export_wasi_polyfill_cabi<T: Guest>(
arg3: usize,
arg4: *mut u8,
arg5: usize,
arg6: i32,
) -> *mut u8 {
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
let len0 = arg1;
Expand Down Expand Up @@ -211,6 +216,7 @@ pub unsafe fn _export_wasi_polyfill_cabi<T: Guest>(
PolyfillConfig {
name: _rt::string_lift(bytes1),
shim: result8,
debug: _rt::bool_lift(arg6 as u8),
},
);
let ptr10 = _RET_AREA.0.as_mut_ptr().cast::<u8>();
Expand Down Expand Up @@ -322,16 +328,17 @@ macro_rules! __export_world_tools_cabi {
_post_return_wat_encode(arg0 : * mut u8,) { $($path_to_types)*::
__post_return_wat_encode::<$ty > (arg0) } #[export_name = "wasm-decode"] unsafe
extern "C" fn export_wasm_decode(arg0 : * mut u8, arg1 : usize, arg2 : i32, arg3
: i32, arg4 : i32,) -> * mut u8 { $($path_to_types)*::
_export_wasm_decode_cabi::<$ty > (arg0, arg1, arg2, arg3, arg4) } #[export_name =
"cabi_post_wasm-decode"] unsafe extern "C" fn _post_return_wasm_decode(arg0 : *
mut u8,) { $($path_to_types)*:: __post_return_wasm_decode::<$ty > (arg0) }
#[export_name = "wasi-polyfill"] unsafe extern "C" fn export_wasi_polyfill(arg0 :
* mut u8, arg1 : usize, arg2 : * mut u8, arg3 : usize, arg4 : * mut u8, arg5 :
usize,) -> * mut u8 { $($path_to_types)*:: _export_wasi_polyfill_cabi::<$ty >
(arg0, arg1, arg2, arg3, arg4, arg5) } #[export_name = "cabi_post_wasi-polyfill"]
unsafe extern "C" fn _post_return_wasi_polyfill(arg0 : * mut u8,) {
$($path_to_types)*:: __post_return_wasi_polyfill::<$ty > (arg0) } };
: * mut u8, arg4 : usize, arg5 : i32,) -> * mut u8 { $($path_to_types)*::
_export_wasm_decode_cabi::<$ty > (arg0, arg1, arg2, arg3, arg4, arg5) }
#[export_name = "cabi_post_wasm-decode"] unsafe extern "C" fn
_post_return_wasm_decode(arg0 : * mut u8,) { $($path_to_types)*::
__post_return_wasm_decode::<$ty > (arg0) } #[export_name = "wasi-polyfill"]
unsafe extern "C" fn export_wasi_polyfill(arg0 : * mut u8, arg1 : usize, arg2 : *
mut u8, arg3 : usize, arg4 : * mut u8, arg5 : usize, arg6 : i32,) -> * mut u8 {
$($path_to_types)*:: _export_wasi_polyfill_cabi::<$ty > (arg0, arg1, arg2, arg3,
arg4, arg5, arg6) } #[export_name = "cabi_post_wasi-polyfill"] unsafe extern "C"
fn _post_return_wasi_polyfill(arg0 : * mut u8,) { $($path_to_types)*::
__post_return_wasi_polyfill::<$ty > (arg0) } };
};
}
#[doc(hidden)]
Expand Down Expand Up @@ -406,18 +413,18 @@ pub(crate) use __export_tools_impl as export;
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.35.0:legion:tools:tools:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 472] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xdc\x02\x01A\x02\x01\
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 478] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xe2\x02\x01A\x02\x01\
A\x16\x01r\x01\x07messages\x03\0\x0btools-error\x03\0\0\x01r\x01\x0egenerate-dwa\
rf\x7f\x03\0\x0dencode-config\x03\0\x02\x01r\x03\x0dskeleton-only\x7f\x0cname-un\
named\x7f\x11fold-instructions\x7f\x03\0\x0ddecode-config\x03\0\x04\x01o\x02ss\x01\
p\x06\x01r\x02\x04names\x04shim\x07\x03\0\x0fpolyfill-config\x03\0\x08\x01p}\x01\
j\x01\x0a\x01\x01\x01@\x02\x05inputs\x06config\x03\0\x0b\x04\0\x0awat-encode\x01\
\x0c\x01j\x01s\x01\x01\x01@\x02\x05input\x0a\x06config\x05\0\x0d\x04\0\x0bwasm-d\
ecode\x01\x0e\x01o\x02s\x0a\x01p\x0f\x01j\x01\x10\x01\x01\x01@\x02\x05input\x0a\x06\
config\x09\0\x11\x04\0\x0dwasi-polyfill\x01\x12\x04\0\x12legion:tools/tools\x04\0\
\x0b\x0b\x01\0\x05tools\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-c\
omponent\x070.220.0\x10wit-bindgen-rust\x060.35.0";
rf\x7f\x03\0\x0dencode-config\x03\0\x02\x01r\x03\x0dskeleton-only\x7f\x0bindent-\
texts\x11fold-instructions\x7f\x03\0\x0ddecode-config\x03\0\x04\x01o\x02ss\x01p\x06\
\x01r\x03\x04names\x04shim\x07\x05debug\x7f\x03\0\x0fpolyfill-config\x03\0\x08\x01\
p}\x01j\x01\x0a\x01\x01\x01@\x02\x05inputs\x06config\x03\0\x0b\x04\0\x0awat-enco\
de\x01\x0c\x01j\x01s\x01\x01\x01@\x02\x05input\x0a\x06config\x05\0\x0d\x04\0\x0b\
wasm-decode\x01\x0e\x01o\x02s\x0a\x01p\x0f\x01j\x01\x10\x01\x01\x01@\x02\x05inpu\
t\x0a\x06config\x09\0\x11\x04\0\x0dwasi-polyfill\x01\x12\x04\0\x12legion:tools/t\
ools\x04\0\x0b\x0b\x01\0\x05tools\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() {
Expand Down
9 changes: 5 additions & 4 deletions projects/legion-wit/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ impl Guest for ToolsContext {

fn wasm_decode(input: Vec<u8>, config: DecodeConfig) -> Result<String, ToolsError> {
let mut parser = wasmprinter::Config::new();
parser.name_unnamed(true);
parser.print_offsets(false);
parser.print_skeleton(config.skeleton_only);
parser.name_unnamed(config.name_unnamed);
// parser.name_unnamed(config.indent_text);
parser.fold_instructions(config.fold_instructions);
let mut dst = String::new();
parser.print(&input, &mut PrintFmtWrite(&mut dst))?;
Expand All @@ -44,9 +45,9 @@ impl Guest for ToolsContext {
map: Some(map),
no_nodejs_compat: false,
base64_cutoff: 0,
tla_compat: true,
valid_lifting_optimization: false,
tracing: false,
tla_compat: false,
valid_lifting_optimization: !config.debug,
tracing: config.debug,
no_namespaced_exports: true,
multi_memory: true,
};
Expand Down
5 changes: 3 additions & 2 deletions projects/legion-wit/wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ world tools {
}
record decode-config {
skeleton-only: bool,
name-unnamed: bool,
indent-text: string,
fold-instructions: bool
}
record polyfill-config {
name: string,
shim: list<tuple<string, string>>
shim: list<tuple<string, string>>,
debug: bool
}
export wat-encode: func(input: string, config: encode-config) -> result<list<u8>, tools-error>;
export wasm-decode: func(input: list<u8>, config: decode-config) -> result<string, tools-error>;
Expand Down

0 comments on commit d60a58f

Please sign in to comment.