Skip to content

Commit 3f8d75e

Browse files
committed
Update demo to allow modifying grid length
1 parent 7dd35f9 commit 3f8d75e

File tree

4 files changed

+70
-55
lines changed

4 files changed

+70
-55
lines changed

test_demo/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<title>Ruby-Wasm</title>
66
</head>
77
<body>
8+
<form id="update_length">
9+
<label>Grid length: <input id="length" type="number" min="0" max="30" value="15"/></label>
10+
</form>
811
<button class="button" onclick="step()">Step</button>
912
<button class="button" id="timer" onclick="startPause()">Start</button>
1013
<pre class="unselectable" id="cells"></pre>
@@ -38,6 +41,11 @@
3841
}
3942
}
4043

44+
const updateLength = () => {
45+
let length = document.getElementById("length").value;
46+
wasm.instance.exports.update_length(length);
47+
showCells();
48+
}
4149

4250
const showCells = () => {
4351
let cells = wasm.instance.exports.get_cells();
@@ -74,6 +82,11 @@
7482
},
7583
}
7684

85+
document.getElementById("update_length").addEventListener("submit", (event) => {
86+
event.preventDefault();
87+
updateLength();
88+
})
89+
7790
addEventListener("load", () => {
7891
WebAssembly.instantiateStreaming(fetch("ruby.wasm"), imports).then(
7992
(obj) => {

test_demo/ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def grid()
2828
grid
2929
end
3030

31-
def reset_cells(length)
31+
def update_length(length)
3232
$length = length
3333
$cells = grid()
3434
end
@@ -94,5 +94,5 @@ def step()
9494
$cells = res
9595
end
9696

97-
$length = 20
97+
$length = 15
9898
$cells = grid()

test_demo/ruby.wasm

14 Bytes
Binary file not shown.

test_demo/ruby.wat

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@
7373
(global.get $main)
7474
(array.new_fixed $arr_unitype 0))))
7575
(func
76-
$reset_cells_export
77-
(export "reset_cells")
76+
$update_length_export
77+
(export "update_length")
7878
(param $length i32)
7979
(result (ref null extern))
8080
(call $unitype_to_js
81-
(call $method_Object_reset_cells
81+
(call $method_Object_update_length
8282
(global.get $main)
8383
(array.new_fixed $arr_unitype 1
8484
(call $i32_to_fixnum
@@ -155,7 +155,7 @@
155155
(drop)
156156
(global.set $length
157157
(ref.i31
158-
(i32.const 1073741844)))
158+
(i32.const 1073741839)))
159159
(ref.i31
160160
(i32.const 5))
161161
(drop)
@@ -336,7 +336,7 @@
336336
(drop)
337337
(local.get $grid))
338338
(func
339-
$method_Object_reset_cells
339+
$method_Object_update_length
340340
(type $method)
341341
(param $self (ref $obj)) (param $args (ref $arr_unitype))
342342
(result (ref eq))
@@ -433,8 +433,8 @@
433433
(local $col (ref eq))
434434
(local $count (ref eq))
435435
(local $neighbor (ref eq))
436-
(local $607a10a4-ad4c-4e7b-9727-710e2ea2b88e (ref eq))
437-
(local $b8e6befc-c15f-4c79-8249-7f60cf2bc442 (ref eq))
436+
(local $77a1d01d-37a3-43ed-9572-e0ebcd189188 (ref eq))
437+
(local $fc65ad19-ade2-4fef-802d-a0d9fc843622 (ref eq))
438438
(local $neighbor_row (ref eq))
439439
(local $neighbor_col (ref eq))
440440
(local $row_lo (ref eq))
@@ -457,10 +457,10 @@
457457
(local.set $neighbor
458458
(ref.i31
459459
(i32.const 5)))
460-
(local.set $607a10a4-ad4c-4e7b-9727-710e2ea2b88e
460+
(local.set $77a1d01d-37a3-43ed-9572-e0ebcd189188
461461
(ref.i31
462462
(i32.const 5)))
463-
(local.set $b8e6befc-c15f-4c79-8249-7f60cf2bc442
463+
(local.set $fc65ad19-ade2-4fef-802d-a0d9fc843622
464464
(ref.i31
465465
(i32.const 5)))
466466
(local.set $neighbor_row
@@ -493,7 +493,7 @@
493493
(ref.i31
494494
(i32.const 5))
495495
(drop)
496-
(local.set $607a10a4-ad4c-4e7b-9727-710e2ea2b88e
496+
(local.set $77a1d01d-37a3-43ed-9572-e0ebcd189188
497497
(array.new_fixed $arr_unitype 8
498498
(array.new_fixed $arr_unitype 2
499499
(call $sub
@@ -555,7 +555,7 @@
555555
(local.get $col)
556556
(ref.i31
557557
(i32.const 1073741825))))))
558-
(local.set $b8e6befc-c15f-4c79-8249-7f60cf2bc442
558+
(local.set $fc65ad19-ade2-4fef-802d-a0d9fc843622
559559
(call $i64_to_integer
560560
(i64.const 0)))
561561
(block $exit_for
@@ -564,21 +564,21 @@
564564
(i32.eq
565565
(i32.wrap_i64
566566
(call $integer_to_i64
567-
(local.get $b8e6befc-c15f-4c79-8249-7f60cf2bc442)))
567+
(local.get $fc65ad19-ade2-4fef-802d-a0d9fc843622)))
568568
(array.len
569569
(ref.cast (ref $arr_unitype)
570-
(local.get $607a10a4-ad4c-4e7b-9727-710e2ea2b88e))))
570+
(local.get $77a1d01d-37a3-43ed-9572-e0ebcd189188))))
571571
(then
572572
(br $exit_for))
573573
(else
574574
))
575575
(local.set $neighbor
576576
(array.get $arr_unitype
577577
(ref.cast (ref $arr_unitype)
578-
(local.get $607a10a4-ad4c-4e7b-9727-710e2ea2b88e))
578+
(local.get $77a1d01d-37a3-43ed-9572-e0ebcd189188))
579579
(i32.wrap_i64
580580
(call $integer_to_i64
581-
(local.get $b8e6befc-c15f-4c79-8249-7f60cf2bc442)))))
581+
(local.get $fc65ad19-ade2-4fef-802d-a0d9fc843622)))))
582582
(local.set $neighbor_row
583583
(array.get $arr_unitype
584584
(ref.cast (ref $arr_unitype)
@@ -708,11 +708,11 @@
708708
(else
709709
(ref.i31
710710
(i32.const 5))))
711-
(local.set $b8e6befc-c15f-4c79-8249-7f60cf2bc442
711+
(local.set $fc65ad19-ade2-4fef-802d-a0d9fc843622
712712
(call $i64_to_integer
713713
(i64.add
714714
(call $integer_to_i64
715-
(local.get $b8e6befc-c15f-4c79-8249-7f60cf2bc442))
715+
(local.get $fc65ad19-ade2-4fef-802d-a0d9fc843622))
716716
(i64.const 1))))
717717
(br $for)))
718718
(ref.i31
@@ -726,12 +726,12 @@
726726
(result (ref eq))
727727
(local $res (ref eq))
728728
(local $row (ref eq))
729-
(local $dca73f39-c2ac-4910-9c4a-d6530650ea43 (ref eq))
730-
(local $83b17551-3626-4499-add9-3dfa9c79b9f6 (ref eq))
729+
(local $fc28190d-bc61-4291-a447-dfb424814751 (ref eq))
730+
(local $046da16f-be03-495b-8110-ad107b3117ba (ref eq))
731731
(local $res_row (ref eq))
732732
(local $col (ref eq))
733-
(local $06fb6e1c-a08f-473d-994d-d010f1ad22f3 (ref eq))
734-
(local $456d4a9f-f5e8-4fb9-b848-e52aefd660a8 (ref eq))
733+
(local $12ca7c7f-7111-4282-a577-ea00a4469f80 (ref eq))
734+
(local $4f143fc5-8504-4152-a7f5-2e9cc3398204 (ref eq))
735735
(local $alive (ref eq))
736736
(local $living_neighbors (ref eq))
737737
(local $ln3 (ref eq))
@@ -743,10 +743,10 @@
743743
(local.set $row
744744
(ref.i31
745745
(i32.const 5)))
746-
(local.set $dca73f39-c2ac-4910-9c4a-d6530650ea43
746+
(local.set $fc28190d-bc61-4291-a447-dfb424814751
747747
(ref.i31
748748
(i32.const 5)))
749-
(local.set $83b17551-3626-4499-add9-3dfa9c79b9f6
749+
(local.set $046da16f-be03-495b-8110-ad107b3117ba
750750
(ref.i31
751751
(i32.const 5)))
752752
(local.set $res_row
@@ -755,10 +755,10 @@
755755
(local.set $col
756756
(ref.i31
757757
(i32.const 5)))
758-
(local.set $06fb6e1c-a08f-473d-994d-d010f1ad22f3
758+
(local.set $12ca7c7f-7111-4282-a577-ea00a4469f80
759759
(ref.i31
760760
(i32.const 5)))
761-
(local.set $456d4a9f-f5e8-4fb9-b848-e52aefd660a8
761+
(local.set $4f143fc5-8504-4152-a7f5-2e9cc3398204
762762
(ref.i31
763763
(i32.const 5)))
764764
(local.set $alive
@@ -781,13 +781,13 @@
781781
(ref.i31
782782
(i32.const 5))
783783
(drop)
784-
(local.set $dca73f39-c2ac-4910-9c4a-d6530650ea43
784+
(local.set $fc28190d-bc61-4291-a447-dfb424814751
785785
(call $call
786786
(global.get $main)
787787
(global.get $str_range)
788788
(array.new_fixed $arr_unitype 1
789789
(global.get $length))))
790-
(local.set $83b17551-3626-4499-add9-3dfa9c79b9f6
790+
(local.set $046da16f-be03-495b-8110-ad107b3117ba
791791
(call $i64_to_integer
792792
(i64.const 0)))
793793
(block $exit_for
@@ -796,33 +796,33 @@
796796
(i32.eq
797797
(i32.wrap_i64
798798
(call $integer_to_i64
799-
(local.get $83b17551-3626-4499-add9-3dfa9c79b9f6)))
799+
(local.get $046da16f-be03-495b-8110-ad107b3117ba)))
800800
(array.len
801801
(ref.cast (ref $arr_unitype)
802-
(local.get $dca73f39-c2ac-4910-9c4a-d6530650ea43))))
802+
(local.get $fc28190d-bc61-4291-a447-dfb424814751))))
803803
(then
804804
(br $exit_for))
805805
(else
806806
))
807807
(local.set $row
808808
(array.get $arr_unitype
809809
(ref.cast (ref $arr_unitype)
810-
(local.get $dca73f39-c2ac-4910-9c4a-d6530650ea43))
810+
(local.get $fc28190d-bc61-4291-a447-dfb424814751))
811811
(i32.wrap_i64
812812
(call $integer_to_i64
813-
(local.get $83b17551-3626-4499-add9-3dfa9c79b9f6)))))
813+
(local.get $046da16f-be03-495b-8110-ad107b3117ba)))))
814814
(local.set $res_row
815815
(array.new_fixed $arr_unitype 0))
816816
(ref.i31
817817
(i32.const 5))
818818
(drop)
819-
(local.set $06fb6e1c-a08f-473d-994d-d010f1ad22f3
819+
(local.set $12ca7c7f-7111-4282-a577-ea00a4469f80
820820
(call $call
821821
(global.get $main)
822822
(global.get $str_range)
823823
(array.new_fixed $arr_unitype 1
824824
(global.get $length))))
825-
(local.set $456d4a9f-f5e8-4fb9-b848-e52aefd660a8
825+
(local.set $4f143fc5-8504-4152-a7f5-2e9cc3398204
826826
(call $i64_to_integer
827827
(i64.const 0)))
828828
(block $exit_for
@@ -831,21 +831,21 @@
831831
(i32.eq
832832
(i32.wrap_i64
833833
(call $integer_to_i64
834-
(local.get $456d4a9f-f5e8-4fb9-b848-e52aefd660a8)))
834+
(local.get $4f143fc5-8504-4152-a7f5-2e9cc3398204)))
835835
(array.len
836836
(ref.cast (ref $arr_unitype)
837-
(local.get $06fb6e1c-a08f-473d-994d-d010f1ad22f3))))
837+
(local.get $12ca7c7f-7111-4282-a577-ea00a4469f80))))
838838
(then
839839
(br $exit_for))
840840
(else
841841
))
842842
(local.set $col
843843
(array.get $arr_unitype
844844
(ref.cast (ref $arr_unitype)
845-
(local.get $06fb6e1c-a08f-473d-994d-d010f1ad22f3))
845+
(local.get $12ca7c7f-7111-4282-a577-ea00a4469f80))
846846
(i32.wrap_i64
847847
(call $integer_to_i64
848-
(local.get $456d4a9f-f5e8-4fb9-b848-e52aefd660a8)))))
848+
(local.get $4f143fc5-8504-4152-a7f5-2e9cc3398204)))))
849849
(local.set $alive
850850
(call $eq_eq
851851
(array.get $arr_unitype
@@ -921,11 +921,11 @@
921921
(i32.const 1073741824))))
922922
(ref.i31
923923
(i32.const 5))))
924-
(local.set $456d4a9f-f5e8-4fb9-b848-e52aefd660a8
924+
(local.set $4f143fc5-8504-4152-a7f5-2e9cc3398204
925925
(call $i64_to_integer
926926
(i64.add
927927
(call $integer_to_i64
928-
(local.get $456d4a9f-f5e8-4fb9-b848-e52aefd660a8))
928+
(local.get $4f143fc5-8504-4152-a7f5-2e9cc3398204))
929929
(i64.const 1))))
930930
(br $for)))
931931
(ref.i31
@@ -938,11 +938,11 @@
938938
(local.get $res_row)))
939939
(ref.i31
940940
(i32.const 5))
941-
(local.set $83b17551-3626-4499-add9-3dfa9c79b9f6
941+
(local.set $046da16f-be03-495b-8110-ad107b3117ba
942942
(call $i64_to_integer
943943
(i64.add
944944
(call $integer_to_i64
945-
(local.get $83b17551-3626-4499-add9-3dfa9c79b9f6))
945+
(local.get $046da16f-be03-495b-8110-ad107b3117ba))
946946
(i64.const 1))))
947947
(br $for)))
948948
(ref.i31
@@ -1617,20 +1617,22 @@
16171617
(i32.const 114)
16181618
(i32.const 105)
16191619
(i32.const 100)))
1620-
(global $str_reset_cells
1620+
(global $str_update_length
16211621
(ref $str)
1622-
(array.new_fixed $str 11
1623-
(i32.const 114)
1624-
(i32.const 101)
1625-
(i32.const 115)
1626-
(i32.const 101)
1622+
(array.new_fixed $str 13
1623+
(i32.const 117)
1624+
(i32.const 112)
1625+
(i32.const 100)
1626+
(i32.const 97)
16271627
(i32.const 116)
1628-
(i32.const 95)
1629-
(i32.const 99)
16301628
(i32.const 101)
1629+
(i32.const 95)
16311630
(i32.const 108)
1632-
(i32.const 108)
1633-
(i32.const 115)))
1631+
(i32.const 101)
1632+
(i32.const 110)
1633+
(i32.const 103)
1634+
(i32.const 116)
1635+
(i32.const 104)))
16341636
(global $str_get_cells
16351637
(ref $str)
16361638
(array.new_fixed $str 9
@@ -1747,8 +1749,8 @@
17471749
(global.get $str_grid)
17481750
(ref.func $method_Object_grid))
17491751
(struct.new $alist_str_method_pair
1750-
(global.get $str_reset_cells)
1751-
(ref.func $method_Object_reset_cells))
1752+
(global.get $str_update_length)
1753+
(ref.func $method_Object_update_length))
17521754
(struct.new $alist_str_method_pair
17531755
(global.get $str_get_cells)
17541756
(ref.func $method_Object_get_cells))

0 commit comments

Comments
 (0)