Skip to content

Commit

Permalink
[wasm][ast] Support struct type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Dec 16, 2024
1 parent 2a84c1c commit 4cef91c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/samlang-ast/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ impl GlobalData {

pub struct Module {
pub function_type_parameter_counts: Vec<usize>,
pub type_definition: Vec<lir::TypeDefinition>,
pub global_variables: Vec<GlobalData>,
pub exported_functions: Vec<mir::FunctionName>,
pub functions: Vec<Function>,
Expand All @@ -345,6 +346,17 @@ impl Module {
collector.push_str(") (result i32)))\n");
}
}
for type_def in &self.type_definition {
collector.push_str("(type $");
type_def.name.write_encoded(&mut collector, heap, table);
collector.push_str(" (struct");
for field in &type_def.mappings {
collector.push_str(" (field ");
field.pretty_print(&mut collector, heap, table);
collector.push(')');
}
collector.push_str("))\n");
}
for d in &self.global_variables {
d.pretty_print(&mut collector);
}
Expand Down Expand Up @@ -409,6 +421,13 @@ mod tests {

let module = Module {
function_type_parameter_counts: vec![0, 1, 2, 3],
type_definition: vec![lir::TypeDefinition {
name: table.create_type_name_for_test(PStr::UPPER_F),
mappings: vec![
lir::Type::Int32,
lir::Type::Id(table.create_type_name_for_test(PStr::UPPER_F)),
],
}],
global_variables: vec![
GlobalData { constant_pointer: 1024, bytes: vec![0, 0] },
GlobalData { constant_pointer: 323, bytes: vec![3, 2] },
Expand Down Expand Up @@ -582,6 +601,7 @@ mod tests {
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(type $_F (struct (field number) (field _F)))
(data (i32.const 1024) "\00\00")
(data (i32.const 323) "\03\02")
(table $0 1 funcref)
Expand Down
1 change: 1 addition & 0 deletions crates/samlang-compiler/src/wasm_lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ pub(super) fn compile_lir_to_wasm(heap: &Heap, sources: &lir::Sources) -> wasm::
.collect::<BTreeSet<_>>()
.into_iter()
.collect_vec(),
type_definition: vec![],
global_variables,
exported_functions: sources.main_function_names.clone(),
functions: sources
Expand Down

0 comments on commit 4cef91c

Please sign in to comment.