added test case for array/struct robustness; small bugfix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 17 Dec 2020 22:02:08 +0000 (16:02 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 17 Dec 2020 22:02:08 +0000 (16:02 -0600)
onyx
src/onyxwasm.c
tests/array_struct_robustness [new file with mode: 0644]
tests/array_struct_robustness.onyx [new file with mode: 0644]

diff --git a/onyx b/onyx
index adabd591f2cd54e4de49bf3bd92029a5c46ff051..7e4f8e784411ae235b607d6681ccdd2a0420641a 100755 (executable)
Binary files a/onyx and b/onyx differ
index 935281912ff258e005f75bd4a89bbb650e13da4a..357fa0629ae629cb3725c318fe8c9fb198b84f06 100644 (file)
@@ -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 (file)
index 0000000..038649d
--- /dev/null
@@ -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 (file)
index 0000000..1c12ce4
--- /dev/null
@@ -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