Repository navigation
Expand file tree
/
Copy pathcatalyst.wat
More file actions
2963 lines (2532 loc) · 81.2 KB
/
Copy pathcatalyst.wat
File metadata and controls
2963 lines (2532 loc) · 81.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; catalyst.wat: multiple simultaneous Catalyst Smalltalk virtual
;; machines, with method translation
;;
;; This source code is a decompilation of the Smalltalk
;; implementation.
;;
;; Craig Latta, July 2025
(module
;; imported JS functions
;; function 0
(import "env" "reportResult"
(func $reportResult
(param i32))) ;; result
;; JS translates method and installs translation function
;; function 1
(import "env" "translateMethodForReceiverWithIdentityHash"
(func $translateMethodForReceiverWithIdentityHash
(param (ref eq)) ;; method
(param i32))) ;; receiver identity hash
;; function 2
(import "env" "debugLog"
(func $debugLog
(param i32))) ;; level
;; linear memory for efficient byte transfer between WASM and JS
(export "bytes" (memory $0))
(export "translatedMethods" (table $translatedMethods))
;; functions exported to JS (in roughly the order JS uses them).
(export "newVirtualMachine" (func $newVirtualMachine))
(export "createMinimalObjectMemory" (func $createMinimalObjectMemory))
(export "resetMinimalMemory" (func $resetMinimalMemory))
(export "interpret" (func $interpret))
(export "byteArrayAt" (func $byteArrayAt))
(export "byteArrayLength" (func $byteArrayLength))
(export "copyByteArrayToMemory" (func $copyByteArrayToMemory))
(export "methodBytecodes" (func $methodBytecodes))
(export "setMethodFunctionIndex" (func $setMethodFunctionIndex))
(export "getMethodFunctionIndex" (func $getMethodFunctionIndex))
(export "onContextPush" (func $onContextPush))
(export "popFromContext" (func $popFromContext))
(export "valueOfSmallInteger" (func $valueOfSmallInteger))
(export "smallIntegerForValue" (func $smallIntegerForValue))
(export "classOfObject" (func $classOfObject))
(export "contextReceiver" (func $contextReceiver))
(export "methodLiterals" (func $methodLiterals))
(export "contextLiteralAt" (func $contextLiteralAt))
(elem declare func $fixMetalevelFieldsOf)
;; types defining Smalltalk classes
;;
;; To start, we only define classes with functions which must exist
;; for the virtual machine to operate (and perform a minimal demo),
;; and the superclasses of such classes which define instance
;; variables. We can dynamically add more classes and methods
;; remotely from SqueakJS later, through behavior similar to
;; $createMinimalObjectMemory, using the exported linear memory as a
;; communication channel.
;;
;; Instances of Smalltalk class SmallInteger are of built-in
;; reference type i31, each instance of every other class is of a
;; reference type using a user-defined struct type. The common
;; supertype of all those reference types is built-in reference type
;; (ref eq). The type for raw arrays of bytes has built-in type i8 as
;; its default element type, and $ByteArray is a Smalltalk object
;; type that wraps it. There's a similar relationship between
;; $wordArray and $WordArray, and between $objectArray and $Array.
;;
;; For Smalltalk, we use the terms "slots" and "methods". For WASM,
;; we use "fields" and "functions". Smalltalk source is "compiled" to
;; a struct of type $CompiledMethod. A $CompiledMethod is
;; "translated" to a WASM function. The code in this file is
;; decompiled from the methods of a working Smalltalk simulation.
;;
;; The nullable fields are only nullable because their initial values
;; aren't always knowable at creation time.
;;
;; Behind the scenes here in the virtual machine, null references in
;; fields of Smalltalk objects are given as the nil Smalltalk object
;; when anything at the Smalltalk level asks about them. This saves
;; us the effort of fixing up them up to be nil.
;;
;; When unavoidable, the special constant -1337, as a (ref i31), is
;; used to indicate an invalid Smalltalk object (usually something
;; related to a value which has yet to be initialized). Unfixable
;; errors are indicated by throwing exceptions.
(rec ;; recursive (mutually referential) type definitions
(type $objectArray
;; null is taken to be nil as far as the Smalltalk level is
;; concerned.
(array (mut (ref null eq))))
(type $byteArray
(array (mut i8)))
(type $wordArray
(array (mut i32)))
(type $Object (sub
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq))))))
(type $UndefinedObject (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq))))))
(type $Array (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
(field $array (mut (ref $objectArray))))))
(type $ByteArray (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
(field $array (ref $byteArray)))))
(type $WordArray (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
(field $array (ref $wordArray)))))
(type $VariableObject (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; $Array, $ByteArray, or $WordArray
(field $slots (mut (ref eq))))))
(type $Symbol (sub $VariableObject
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; $Array, $ByteArray, or $WordArray
(field $slots (mut (ref eq))))))
(type $Dictionary (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
(field $keys (ref $Array))
(field $values (ref $Array))
(field $count (mut i32)))))
(type $Behavior (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; a $Class or $Metaclass
(field $superclass (mut (ref null eq)))
(field $methodDictionary (ref $Dictionary))
(field $format (mut i32)))))
(type $ClassDescription (sub $Behavior
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; a $Class or $Metaclass
(field $superclass (mut (ref null eq)))
(field $methodDictionary (ref $Dictionary))
(field $format (mut i32))
(field $instanceVariableNames (ref $Array))
(field $baseID (ref $ByteArray)))))
(type $Class (sub $ClassDescription
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; a $Class or $Metaclass
(field $superclass (mut (ref null eq)))
(field $methodDictionary (ref $Dictionary))
(field $format (mut i32))
(field $instanceVariableNames (ref $Array))
(field $baseID (ref $ByteArray))
(field $subclasses (ref $Array))
(field $name (mut (ref $Symbol)))
(field $classPool (ref $Dictionary))
(field $sharedPools (ref $Array)))))
(type $Metaclass (sub $ClassDescription
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; a $Class or $Metaclass
(field $superclass (mut (ref null eq)))
(field $methodDictionary (ref $Dictionary))
(field $format (mut i32))
(field $instanceVariableNames (ref $Array))
(field $baseID (ref $ByteArray))
(field $thisClass (mut (ref $Class))))))
(type $CompiledMethod (sub $VariableObject
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
;; $Array, $ByteArray, or $WordArray
(field $slots (mut (ref eq)))
(field $literals (ref $Array))
(field $header i32)
(field $invocationCount (mut i32))
(field $functionIndex (mut i32))
(field $translationThreshold i32)
(field $isInstalled (mut i32)))))
(type $Context (sub $Object
(struct
;; $Class or $Metaclass
(field $class (mut (ref eq)))
(field $identityHash i32)
(field $nextObject (mut (ref null eq)))
(field $sender (mut (ref null $Context)))
(field $pc (mut i32))
(field $sp (mut i32))
(field $method (ref $CompiledMethod))
(field $receiver (ref eq))
(field $args (ref $Array))
(field $temps (ref $Array))
(field $stack (ref $Array)))))
(type $PICEntry
(struct
(field $selector (ref eq))
(field $receiverClass (ref eq))
(field $method (ref $CompiledMethod))
(field $hitCount (mut i32))))
(type $VirtualMachine
(struct
;; created with a null $activeContext; it's set later
(field $activeContext (mut (ref null $Context)))
(field $translationEnabled (mut i32))
(field $methodCache (mut (ref $objectArray)))
(field $functionTableBaseIndex (mut i32))
(field $translationThreshold (mut i32))
(field $nextIdentityHash (mut i32))
;; created with null $firstObject and $lastObject; they're
;; set later
(field $firstObject (mut (ref null eq)))
(field $lastObject (mut (ref null eq)))
;; frequently used for $superclass in $newSubclassOfNamed
(field $classObject (mut (ref null $Class)))
(field $classMetaclass (mut (ref null $Class)))
(field $classClass (mut (ref null $Class)))
(field $classArray (mut (ref null $Class)))
(field $classByteArray (mut (ref null $Class)))
(field $classWordArray (mut (ref null $Class)))
(field $classContext (mut (ref null $Class)))
(field $classCompiledMethod (mut (ref null $Class)))
(field $classSymbol (mut (ref null $Class)))
(field $classSmallInteger (mut (ref null $Class))))))
;; function types
(type $enumerator (func
(param (ref $VirtualMachine))
(param (ref null eq))))
;; exception types
(type $messageNotUnderstoodType (func
(param (ref $Symbol)))) ;; selector
(type $emptyStackType (func))
(type $valuesLeftOnStackType (func))
(type $outOfBoundsType (func
(param i32)))
;; exception tags
(tag $messageNotUnderstood (type $messageNotUnderstoodType))
(tag $emptyStack (type $emptyStackType))
(tag $valuesLeftOnStack (type $valuesLeftOnStackType))
(tag $outOfBounds (type $outOfBoundsType))
;; Reference-type globals are nullable due to current WASM
;; limitations, but are enforced to be non-null after object memory
;; creation.
(global $invalidObject (ref null i31) (ref.i31 (i32.const -1337)))
(global $benchmarkSelector (mut (ref null $Symbol)) (ref.null none))
;; linear memory for staging byte arrays visible to JS. See $copyByteArrayToMemory.
(memory $0 1)
;; pointer into the staging bytes
(global $byteArrayCopyPointer (mut i32) (i32.const 1024))
;; translated methods function table
(table $translatedMethods 100 funcref)
;; bytecode handlers function table
(table $bytecodeHandlers 256 funcref)
;; function 3: Return whether two $byteArrays are equivalent.
(func $byteArrayIsEquivalentTo
(param $firstObject (ref $ByteArray))
(param $secondObject (ref $ByteArray))
(result i32)
(local $firstArray (ref $byteArray))
(local $secondArray (ref $byteArray))
(local $length i32)
(local $index i32)
;; Quick reference equality check first
(if (result i32)
(ref.eq
(local.get $firstObject)
(local.get $secondObject))
(then
(return
(i32.const 1)))
(else
(local.set $firstArray
(struct.get $ByteArray $array
(local.get $firstObject)))
(local.set $secondArray
(struct.get $ByteArray $array
(local.get $secondObject)))
(if (result i32)
(ref.eq
(local.get $firstArray)
(local.get $secondArray))
(then
(return
(i32.const 1)))
(else
;; Check lengths
(if (result i32)
(i32.ne
(local.tee $length
(array.len (local.get $firstArray)))
(array.len
(local.get $secondArray)))
(then
(return
(i32.const 0)))
(else
;; Compare elements
(loop $loop (result i32)
(if (result i32)
(i32.eq
(local.get $index)
(local.get $length))
(then
;; All elements equal
(return (i32.const 1)))
(else
;; Compare current elements
(if (result i32)
(i32.ne
(array.get_u $byteArray
(local.get $firstArray)
(local.get $index))
(array.get_u $byteArray
(local.get $secondArray)
(local.get $index)))
(then
;; Elements different
(return (i32.const 0)))
(else
(local.set $index
(i32.add
(local.get $index)
(i32.const 1)))
(br $loop)))))))))))))
;; function 4
(func $symbolIsEquivalentTo
(param $firstSymbol (ref $Symbol))
(param $secondSymbol (ref $Symbol))
(result i32)
(if (result i32)
(ref.eq
(local.get $firstSymbol)
(local.get $secondSymbol))
(then
(return
(i32.const 1)))
(else
(call $byteArrayIsEquivalentTo
(ref.cast (ref $ByteArray)
(struct.get $Symbol $slots
(local.get $firstSymbol)))
(ref.cast (ref $ByteArray)
(struct.get $Symbol $slots
(local.get $secondSymbol)))))))
;; function 5
(func $newArray
(param $vm (ref $VirtualMachine))
(param $array (ref $objectArray))
(result (ref $Array))
(struct.new $Array
;; $class
(if (result (ref eq))
(ref.is_null
(struct.get $VirtualMachine $classArray
(local.get $vm)))
(then
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classArray
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
(ref.null none) ;; $nextObject to be set later
(local.get $array))) ;; $array
;; function 6
(func $newByteArray
(param $vm (ref $VirtualMachine))
(param $array (ref $byteArray))
(result (ref $ByteArray))
(struct.new $ByteArray
(if (result (ref eq))
;; class
(ref.is_null
(struct.get $VirtualMachine $classByteArray
(local.get $vm)))
(then
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classByteArray
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
(ref.null none) ;; $nextObject to be set later
(local.get $array))) ;; $array
;; function 7
(func $newWordArray
(param $vm (ref $VirtualMachine))
(param $array (ref $wordArray))
(result (ref $WordArray))
(struct.new $WordArray
;; class
(if (result (ref eq))
(ref.is_null
(struct.get $VirtualMachine $classWordArray
(local.get $vm)))
(then
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classWordArray
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
(ref.null none) ;; $nextObject to be set later
(local.get $array))) ;; $array
;; function 8
(func $newDictionary
(param $vm (ref $VirtualMachine))
(result (ref $Dictionary))
(struct.new $Dictionary
;; $class to be set later, to class Dictionary
(if (result (ref eq))
(ref.is_null
(struct.get $VirtualMachine $classObject
(local.get $vm)))
(then
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classObject
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
(ref.null none) ;; $nextObject to be set later
(call $newArray ;; $keys
(local.get $vm)
(array.new $objectArray
(ref.null none)
(i32.const 10))) ;; initial capacity 10
(call $newArray ;; $values
(local.get $vm)
(array.new $objectArray
(ref.null none)
(i32.const 10))) ;; initial capacity 10
(i32.const 0))) ;; $count
;; function 9
(func $dictionaryAtPut
(param $dictionary (ref $Dictionary))
(param $key (ref eq))
(param $value (ref eq))
(local $keys (ref $Array))
(local $values (ref $Array))
(local $count i32)
(local $capacity i32)
(local $newKeysArray (ref $objectArray))
(local $newValuesArray (ref $objectArray))
(local $index i32)
(local $existingKey (ref null eq))
(local.set $keys
(struct.get $Dictionary $keys
(local.get $dictionary)))
(local.set $values
(struct.get $Dictionary $values
(local.get $dictionary)))
(local.set $count
(struct.get $Dictionary $count
(local.get $dictionary)))
(local.set $capacity
(array.len
(struct.get $Array $array
(local.get $keys))))
;; Check if we need to grow
(if
(i32.ge_u (local.get $count) (local.get $capacity))
(then
;; Need to grow - create new arrays with capacity + 10
(local.set $newKeysArray
(array.new $objectArray
(ref.null none)
(i32.add
(local.get $capacity)
(i32.const 10))))
(local.set $newValuesArray
(array.new $objectArray
(ref.null none)
(i32.add
(local.get $capacity)
(i32.const 10))))
;; Copy existing elements
(local.set $index (i32.const 0))
(block $done_copying
(loop $copy_loop
(br_if $done_copying
(i32.ge_u
(local.get $index)
(local.get $count)))
(array.set $objectArray
(local.get $newKeysArray)
(local.get $index)
(array.get $objectArray
(struct.get $Array $array
(local.get $keys))
(local.get $index)))
(array.set $objectArray
(local.get $newValuesArray)
(local.get $index)
(array.get $objectArray
(struct.get $Array $array
(local.get $values))
(local.get $index)))
(local.set $index
(i32.add
(local.get $index)
(i32.const 1)))
(br $copy_loop)))
;; Update dictionary's underlying keys and values objectArrays.
(struct.set $Array $array
(local.get $keys)
(local.get $newKeysArray))
(struct.set $Array $array
(local.get $values)
(local.get $newValuesArray))))
;; Check if key is a Symbol and if an equivalent Symbol already exists
(if
(ref.test
(ref $Symbol)
(local.get $key))
(then
;; Key is a Symbol - check for equivalent existing Symbol
(local.set $index
(i32.const 0))
(block $check_done
(loop $check_loop
(br_if $check_done
(i32.ge_u
(local.get $index)
(local.get $count)))
;; Get existing key at index
(local.set $existingKey
(array.get $objectArray
(struct.get $Array $array
(local.get $keys))
(local.get $index)))
;; Check if existing key is also a Symbol and is equivalent
(if
(ref.test
(ref $Symbol)
(local.get $existingKey))
(then
(if (call $symbolIsEquivalentTo
(ref.cast (ref $Symbol)
(local.get $key))
(ref.cast (ref $Symbol)
(local.get $existingKey)))
(then
;; Found equivalent Symbol - don't add, just return
(return)))))
(local.set $index
(i32.add
(local.get $index)
(i32.const 1)))
(br $check_loop)))))
;; Add the new key/value pair
(array.set $objectArray
(struct.get $Array $array
(local.get $keys))
(local.get $count)
(local.get $key))
(array.set $objectArray
(struct.get $Array $array
(local.get $values))
(local.get $count)
(local.get $value))
;; Increment count
(struct.set $Dictionary $count
(local.get $dictionary)
(i32.add
(local.get $count)
(i32.const 1))))
;; function 10
(func $behaviorAtPut
(param $behavior (ref eq))
(param $key (ref eq))
(param $value (ref eq))
(call $dictionaryAtPut
(struct.get $Behavior $methodDictionary
(ref.cast (ref $Behavior)
(local.get $behavior)))
(local.get $key)
(local.get $value)))
;; function 11: Link $objects via their $nextObject fields.
(func $linkObjects
(param $vm (ref $VirtualMachine))
(param $objects (ref $objectArray))
(local $previousObject (ref null eq))
(local $nextObject (ref null eq))
(local $limit i32)
(local $index i32)
(local $scratch (ref $VirtualMachine))
(local.set $limit
(array.len
(local.get $objects)))
(local.set $index
(i32.const 0))
(loop $link
(if
(i32.eq
(local.get $index)
(local.get $limit))
(then
(return)))
(local.tee $nextObject
(array.get $objectArray
(local.get $objects)
(local.get $index)))
(if
(i32.eqz
(ref.is_null))
(then
(local.set $previousObject
(struct.get $VirtualMachine $lastObject
(local.get $vm)))
(struct.set $VirtualMachine $lastObject
(block (result (ref $VirtualMachine))
(local.set $scratch
(local.get $vm))
(struct.set $Object $nextObject
(ref.cast (ref $Object)
(local.get $previousObject))
(local.get $nextObject))
(local.get $scratch))
(local.get $nextObject))
(local.set $index
(i32.add
(local.get $index)
(i32.const 1)))
(br $link)))))
;; function 12
(func $newEmptyArray
(param $vm (ref $VirtualMachine))
(result (ref $Array))
(call $newArray
(local.get $vm)
(array.new $objectArray
(ref.null none) ;; default array element type (irrelevant here)
(i32.const 0)))) ;; empty
;; function 13: Add an element to the end of an $Array's elements, by
;; replacing the elements array.
(func $addToArray
(param $element (ref eq))
(param $array (ref $Array))
(local $newObjectArray (ref $objectArray))
(local $oldObjectArray (ref $objectArray))
(local $index i32)
(local $limit i32)
(local.set $oldObjectArray
(struct.get $Array $array
(local.get $array)))
(local.set $index
(i32.const 0))
(local.set $limit
(array.len (local.get $oldObjectArray)))
;; Create a new $objectArray that is one larger than the old
;; one.
(local.set $newObjectArray
(array.new $objectArray
(ref.null none)
(i32.add
(local.get $limit)
(i32.const 1))))
(if
(i32.gt_u
(local.get $limit)
(i32.const 0))
(then
;; Copy the contents of the old array into the new one.
(loop $loop
(array.set $objectArray
(local.get $newObjectArray)
(local.get $index)
(array.get $objectArray
(local.get $oldObjectArray)
(local.get $index)))
(br_if $loop
(i32.lt_s
(local.tee $index
(i32.add
(local.get $index)
(i32.const 1)))
(local.get $limit))))))
;; Put the new element in the last slot of the new array.
(array.set $objectArray
(local.get $newObjectArray)
(local.get $index)
(local.get $element))
;; Set the $array's $objectArray to the new array.
(struct.set $Array $array
(local.get $array)
(local.get $newObjectArray)))
;; function 14
(func $newSubclassOfNamed
(param $vm (ref $VirtualMachine))
(param $superclass (ref null $Class))
(param $name (ref $Symbol))
(result (ref $Class))
(local $class (ref $Class))
(local $metaclass (ref $Metaclass))
(local $bootstrapping i32)
(local.set $bootstrapping (i32.const 0))
(local.set $class
(struct.new $Class
;; $class to be set later, to an appropriate Metaclass.
(if (result (ref eq))
(ref.is_null
(struct.get $VirtualMachine $classObject
(local.get $vm)))
(then
(local.set $bootstrapping (i32.const 1))
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classObject
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
(ref.null none) ;; $nextObject to be set later
(local.get $superclass) ;; $superclass
(call $newDictionary ;; $methodDictionary
(local.get $vm))
(i32.const 0) ;; $format to be set later
(call $newEmptyArray ;; $instanceVariableNames
(local.get $vm))
(call $newByteArray ;; $baseID to be set later
(local.get $vm)
(array.new $byteArray
(i32.const 0)
(i32.const 16)))
(call $newEmptyArray ;; $subclasses
(local.get $vm))
(local.get $name) ;; $name
(call $newDictionary ;; $classPool
(local.get $vm))
(call $newEmptyArray ;; $sharedPools
(local.get $vm))))
(if
(i32.eqz
(ref.is_null
(local.get $superclass)))
(then
(call $addToArray
(local.get $class)
(struct.get $Class $subclasses
(local.get $superclass)))))
(local.set $metaclass
(struct.new $Metaclass
;; $class
(if (result (ref eq))
(ref.is_null
(struct.get $VirtualMachine $classMetaclass
(local.get $vm)))
(then
(local.set $bootstrapping (i32.const 1))
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(ref.cast (ref eq)
(struct.get $VirtualMachine $classMetaclass
(local.get $vm)))))
(call $nextIdentityHash ;; $identityHash
(local.get $vm))
;; $nextObject to be set later
(ref.null none)
;; $superclass
(if (result (ref null eq))
(ref.is_null (local.get $superclass))
(then
(if (result (ref null eq))
(ref.is_null
(struct.get $VirtualMachine $classClass
(local.get $vm)))
(then
(ref.cast (ref eq)
(ref.i31
(i32.const -1337))))
(else
(struct.get $VirtualMachine $classClass
(local.get $vm)))))
(else
(struct.get $Class $class
(local.get $superclass))))
;; $methodDictionary
(call $newDictionary
(local.get $vm))
(i32.const 152) ;; $format
;; $instanceVariableNames
(call $newEmptyArray
(local.get $vm))
;; $baseID to be set later (should be a $UUID)
(call $newByteArray
(local.get $vm)
(array.new $byteArray
(i32.const 0)