Skip to content

Commit

Permalink
Merge pull request #356 from 0xPolygonMiden/greenhat/delete-cargo-com…
Browse files Browse the repository at this point in the history
…ponent-based-tests

[2/x] chore: remove pure `cargo-component`-based tests and artifacts, hand-written Wasm CM tests
  • Loading branch information
bitwalker authored Dec 16, 2024
2 parents 4a0b2c6 + 73262e3 commit 74aabe8
Show file tree
Hide file tree
Showing 29 changed files with 0 additions and 27,455 deletions.
6 changes: 0 additions & 6 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ args = [
"midenc",
]

[tasks.install-cargo-component]
category = "Test"
description = "Install cargo-component extension"
command = "cargo"
args = ["install", "[email protected]"]

[tasks.test-rust]
category = "Test"
Expand All @@ -227,7 +222,6 @@ args = [
"@@split(CARGO_MAKE_CARGO_BUILD_TEST_FLAGS, )",
"${@}",
]
dependencies = ["install-cargo-component"]

[tasks.test-lit]
category = "Test"
Expand Down
187 changes: 0 additions & 187 deletions frontend-wasm/src/component/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,190 +61,3 @@ fn inline(
.map_err(Report::msg)?;
Ok(component_dfg.finish())
}

#[cfg(test)]
mod tests {
use midenc_hir::{FunctionType, Ident, InterfaceFunctionIdent, InterfaceIdent, Symbol};
use midenc_hir_type::Type;

use super::*;
use crate::{component::StaticModuleIndex, test_utils::test_context};

#[test]
fn translate_simple() {
let wat = r#"
(component
(core module (;0;)
(type (;0;) (func))
(type (;1;) (func (param i32 i32) (result i32)))
(func $add (;0;) (type 1) (param i32 i32) (result i32)
local.get 1
local.get 0
i32.add
)
(memory (;0;) 17)
(global $__stack_pointer (;0;) (mut i32) i32.const 1048576)
(export "memory" (memory 0))
(export "add" (func $add))
)
(core instance (;0;) (instantiate 0))
(alias core export 0 "memory" (core memory (;0;)))
(type (;0;) (func (param "a" u32) (param "b" u32) (result u32)))
(alias core export 0 "add" (core func (;0;)))
(func (;0;) (type 0) (canon lift (core func 0)))
(export (;1;) "add" (func 0))
)
"#
.to_string();
let wasm = wat::parse_str(wat).unwrap();
let context = test_context();
let config = Default::default();
let (mut component_types_builder, parsed_component) =
parse(&config, &wasm, &context.session).unwrap();
let component_translation =
inline(&mut component_types_builder, &parsed_component).unwrap();

assert_eq!(parsed_component.static_modules.len(), 1);
// dbg!(&component_translation.component);
let module = &parsed_component.static_modules[StaticModuleIndex::from_u32(0)].module;
// dbg!(module);
assert_eq!(module.imports.len(), 0);
assert_eq!(component_translation.trampolines.len(), 0);
// dbg!(&component_translation.component.initializers);
assert_eq!(component_translation.component.initializers.len(), 1);
// dbg!(&component_translation.component.exports);
assert_eq!(component_translation.component.exports.len(), 1);
let component_types = component_types_builder.finish();
let translator = ComponentTranslator::new(
component_types,
parsed_component.static_modules,
&config,
&context.session,
);
let ir = translator.translate(component_translation).unwrap();

// dbg!(&ir.exports());
assert!(!ir.modules().is_empty());
assert!(!ir.exports().is_empty());
let export_name_sym = Symbol::intern("add");
let export = ir.exports().get(&export_name_sym.into()).unwrap();
assert_eq!(export.function.function.as_symbol(), export_name_sym);
let expected_export_func_ty = FunctionType::new_wasm([Type::U32, Type::U32], [Type::U32]);
assert_eq!(export.function_ty, expected_export_func_ty);
}

#[test]
fn translate_simple_import() {
let wat = r#"
(component
(type (;0;)
(instance
(type (;0;) (func (param "a" u32) (param "b" u32) (result u32)))
(export (;0;) "add" (func (type 0)))
)
)
(import "miden:add/[email protected]" (instance (;0;) (type 0)))
(core module (;0;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;1;) (func))
(type (;2;) (func (param i32) (result i32)))
(import "miden:add/[email protected]" "add" (func $inc_wasm_component::bindings::miden::add::add::add::wit_import (;0;) (type 0)))
(func $inc (;1;) (type 2) (param i32) (result i32)
local.get 0
i32.const 1
call $inc_wasm_component::bindings::miden::add::add::add::wit_import
)
(memory (;0;) 17)
(global $__stack_pointer (;0;) (mut i32) i32.const 1048576)
(export "memory" (memory 0))
(export "inc" (func $inc))
)
(alias export 0 "add" (func (;0;)))
(core func (;0;) (canon lower (func 0)))
(core instance (;0;)
(export "add" (func 0))
)
(core instance (;1;) (instantiate 0
(with "miden:add/[email protected]" (instance 0))
)
)
(alias core export 1 "memory" (core memory (;0;)))
(type (;1;) (func (param "a" u32) (result u32)))
(alias core export 1 "inc" (core func (;1;)))
(func (;1;) (type 1) (canon lift (core func 1)))
(export (;1;) "inc" (func 1))
)
"#.to_string();
let wasm = wat::parse_str(wat).unwrap();
let context = test_context();
let interface_function_ident = InterfaceFunctionIdent {
interface: InterfaceIdent::from_full_ident("miden:add/[email protected]"),
function: Symbol::intern("add"),
};

let config = WasmTranslationConfig::default();
let (mut component_types_builder, parsed_component) =
parse(&config, &wasm, &context.session).unwrap();
let component_translation =
inline(&mut component_types_builder, &parsed_component).unwrap();
assert_eq!(parsed_component.static_modules.len(), 1);
let module = &parsed_component.static_modules[StaticModuleIndex::from_u32(0)].module;

// dbg!(&module.imports);
assert_eq!(module.imports.len(), 1);

// dbg!(&component_translation.trampolines);
assert_eq!(component_translation.trampolines.len(), 1);

// dbg!(&component_translation.component.initializers);
assert_eq!(component_translation.component.initializers.len(), 2);

// dbg!(&component_translation.component.imports);
assert_eq!(component_translation.component.imports.len(), 1);
// dbg!(&component_translation.component.import_types);
assert_eq!(component_translation.component.import_types.len(), 1);

// dbg!(&component_translation.component.exports);
assert_eq!(component_translation.component.exports.len(), 1);

let component_types = component_types_builder.finish();

let translator = ComponentTranslator::new(
component_types,
parsed_component.static_modules,
&config,
&context.session,
);
let ir = translator.translate(component_translation).unwrap();

// dbg!(&ir.exports());
assert!(!ir.modules().is_empty());
assert!(!ir.exports().is_empty());
assert!(!ir.imports().is_empty());
let export_name_sym = Symbol::intern("inc");
let export = ir.exports().get(&export_name_sym.into()).unwrap();
assert_eq!(export.function.function.as_symbol(), export_name_sym);
let expected_export_func_ty = FunctionType::new_wasm(vec![Type::U32], vec![Type::U32]);
assert_eq!(export.function_ty, expected_export_func_ty);
let module = ir.modules().first().unwrap().1;
// dbg!(&module.imports());
let import_info = module.imports();
dbg!(&import_info);
let function_id = *import_info
.imported(&Ident::from("miden:add/[email protected]"))
.unwrap()
.iter()
.collect::<Vec<_>>()
.first()
.cloned()
.unwrap();
dbg!(&function_id);
dbg!(ir.imports());
let component_import = ir.imports().get(&function_id).unwrap().unwrap_canon_abi_import();
assert_eq!(component_import.interface_function, interface_function_ident);
assert!(!component_import.function_ty().params.is_empty());
let expected_import_func_ty =
FunctionType::new_wasm(vec![Type::U32, Type::U32], vec![Type::U32]);
assert_eq!(component_import.function_ty(), &expected_import_func_ty);
}
}
Loading

0 comments on commit 74aabe8

Please sign in to comment.