Skip to content

Commit d0490ae

Browse files
blaine-arcjetsd2k
authored andcommitted
encode and decode u32
1 parent 6ead1d0 commit d0490ae

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cmd/gravity/src/codegen/exports.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ mod tests {
161161

162162
// Verify function body
163163
assert!(generated.contains("arg0 := value"));
164+
assert!(generated.contains("result0 := api.EncodeU32(arg0)"));
164165
assert!(
165-
generated.contains("i.module.ExportedFunction(\"add_number\").Call(ctx, uint64(arg0))")
166+
generated
167+
.contains("i.module.ExportedFunction(\"add_number\").Call(ctx, uint64(result0))")
166168
);
167-
assert!(generated.contains("if err0 != nil {"));
168-
assert!(generated.contains("panic(err0)"));
169-
assert!(generated.contains("results0 := raw0[0]"));
170-
assert!(generated.contains("result1 := uint32(results0)"));
171-
assert!(generated.contains("return result1"));
169+
assert!(generated.contains("if err1 != nil {"));
170+
assert!(generated.contains("panic(err1)"));
171+
assert!(generated.contains("results1 := raw1[0]"));
172+
assert!(generated.contains("result2 := api.DecodeU32(results1)"));
173+
assert!(generated.contains("return result2"));
172174
}
173175
}

cmd/gravity/src/codegen/func.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,22 @@ impl Bindgen for Func<'_> {
310310
results.push(Operand::SingleValue(value))
311311
}
312312
Instruction::I32FromU32 => {
313-
// It seems like this isn't needed because Wazero works with Go's uint32 type
313+
let tmp = self.tmp();
314+
let result = &format!("result{tmp}");
314315
let operand = &operands[0];
315-
results.push(operand.clone());
316+
quote_in! { self.body =>
317+
$['\r']
318+
$result := $(&self.go_imports.wazero_api_encode_u32)($operand)
319+
};
320+
results.push(Operand::SingleValue(result.into()));
316321
}
317322
Instruction::U32FromI32 => {
318323
let tmp = self.tmp();
319324
let result = &format!("result{tmp}");
320325
let operand = &operands[0];
321326
quote_in! { self.body =>
322327
$['\r']
323-
$result := uint32($operand)
328+
$result := $(&self.go_imports.wazero_api_decode_u32)($operand)
324329
};
325330
results.push(Operand::SingleValue(result.into()));
326331
}

cmd/gravity/src/go/imports.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct GoImports {
1414
pub wazero_api_decode_i32: Import,
1515
pub wazero_api_encode_i32: Import,
1616
pub wazero_api_decode_u32: Import,
17+
pub wazero_api_encode_u32: Import,
1718
}
1819

1920
impl Default for GoImports {
@@ -61,6 +62,10 @@ impl GoImports {
6162
"github.com/tetratelabs/wazero/api",
6263
"DecodeU32",
6364
),
65+
wazero_api_encode_u32: genco::lang::go::import(
66+
"github.com/tetratelabs/wazero/api",
67+
"EncodeU32",
68+
),
6469
}
6570
}
6671
}

0 commit comments

Comments
 (0)