From: Brendan Hansen Date: Thu, 17 Dec 2020 22:02:08 +0000 (-0600) Subject: added test case for array/struct robustness; small bugfix X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=1380b802dce45956df02de8c088dc20b81959be6;p=onyx.git added test case for array/struct robustness; small bugfix --- diff --git a/onyx b/onyx index adabd591..7e4f8e78 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 93528191..357fa062 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -519,7 +519,7 @@ EMIT_FUNC(assignment, AstBinaryOp* assign) { // greater than like 16 we output a loop that copies them? // - brendanfh 2020/12/16 fori (i, 0, elem_count) { - if (!type_is_structlike(rtype)) + if (!type_is_structlike(elem_type)) WIL(WI_LOCAL_GET, lptr_local); if (bh_arr_last(code).type == WI_LOCAL_SET && bh_arr_last(code).data.l == rptr_local) @@ -528,7 +528,7 @@ EMIT_FUNC(assignment, AstBinaryOp* assign) { WIL(WI_LOCAL_GET, rptr_local); emit_load_instruction(mod, &code, elem_type, i * elem_size); - if (!type_is_structlike(rtype)) { + if (!type_is_structlike(elem_type)) { emit_store_instruction(mod, &code, elem_type, i * elem_size); } else { WIL(WI_LOCAL_GET, lptr_local); diff --git a/tests/array_struct_robustness b/tests/array_struct_robustness new file mode 100644 index 00000000..038649df --- /dev/null +++ b/tests/array_struct_robustness @@ -0,0 +1,15 @@ +Array of structs on the stack. +Vec2(4039, 7782) +Array of structs from function call. +Vec2(0, 0) +Vec2(1, 1) +Vec2(4, 8) +Vec2(9, 27) +Array of structs on a struct. +Vec2(3, 9) +Vec2(3, 27) +Array of structs on a struct from function call. +Vec2(25, 125) +Vec2(100, 1000) +Array of u32. +63001 diff --git a/tests/array_struct_robustness.onyx b/tests/array_struct_robustness.onyx new file mode 100644 index 00000000..1c12ce4e --- /dev/null +++ b/tests/array_struct_robustness.onyx @@ -0,0 +1,98 @@ +#include_file "core/std/js" + +use package core + +Vec2 :: struct { x: i32; y: i32; } + +// Overload print() to print Vec2's. +proc (use v: Vec2) #add_overload print { + printf("Vec2(%i, %i)", x, y); +} + +EntityStore :: struct { + positions : [4] Vec2; + velocities : [4] Vec2; +} + +calc_vecs :: proc (n := 0) -> [4] Vec2 { + vecs : [4] Vec2; + + i := n; + for ^v: vecs { + v.x = i * i; + v.y = i * i * i; + i += 1; + } + + return vecs; +} + +main :: proc (args: [] cstr) #export "MAIN" { + // Array of structs on stack + { + println("Array of structs on the stack."); + vecs : [8] Vec2; + for ^v: vecs { + v.x = 4039; + v.y = 7782; + } + + println(vecs[3]); + } + + // Array of structs from function call + { + println("Array of structs from function call."); + vecs := calc_vecs(); + for ^v: vecs do println(*v); + } + + // Array of structs on a struct + { + println("Array of structs on a struct."); + + es : EntityStore; + es.positions = Vec2.[ + Vec2.{ 0, 0 }, + Vec2.{ 1, 1 }, + Vec2.{ 2, 4 }, + Vec2.{ 3, 9 }, + ]; + + es.velocities = Vec2.[ + Vec2.{ 0, 0 }, + Vec2.{ 1, 1 }, + Vec2.{ 2, 8 }, + Vec2.{ 3, 27 }, + ]; + + println(es.positions[3]); + println(es.velocities[3]); + } + + // Array of structs on a struct from function call + { + println("Array of structs on a struct from function call."); + + es : EntityStore; + es.positions = calc_vecs(5); + es.velocities = calc_vecs(10); + + println(es.positions[0]); + println(es.velocities[0]); + } + + // Array of u32 + { + println("Array of u32."); + + nums : [100] u32; + i := 1; + for ^n: nums { + *n = i * i; + i += 5; + } + + println(nums[50]); + } +} \ No newline at end of file