-
Notifications
You must be signed in to change notification settings - Fork 11
/
KernelForJS.ns
1177 lines (1163 loc) · 33.8 KB
/
KernelForJS.ns
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
Newspeak3
'Root'
class KernelForJS usingVmMirror: vmm = (
(*
Kernel classes for the Javascript-based implementation of Newspeak.
Copyright 2008 Cadence Design Systems, Inc.
Copyright 2009-2010 Gilad Bracha.
Copyright 2012 SAP AG.
Copyright 2012 Google Inc.
Copyright 2013 Ryan Macnak
Licensed under the Apache License, Version 2.0 (the ''License''); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 *)
|
vmmirror = vmm.
public platform
|) (
public class Array uninstantiable = () (
public , otherSequence = (
^js call: (js propertyOf: self at: (js literal: 'concat')) with: {otherSequence asArray}
)
public asArray = (
^self
)
public asOrderedCollection = (
^platform collections List withAll: self
)
public at: index <Integer> ^<E> = (
| jsIndex |
(js prefixOperator: 'typeof ' on: index) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
jsIndex:: js operator: '-' with: index and: (js literal: 1).
(js operator: '<' with: jsIndex and: (js literal: 0)) ifTrue: [^Error signal: 'ArgumentError'].
(js operator: '>=' with: jsIndex and: (js verbatim: 'this.length')) ifTrue: [^Error signal: 'ArgumentError'].
^js propertyOf: (js ident: 'this') at: jsIndex
)
public at: index <Integer> put: value <E> ^<E> = (
| jsIndex |
(js prefixOperator: 'typeof ' on: index) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
jsIndex:: js operator: '-' with: index and: (js literal: 1).
(js operator: '<' with: jsIndex and: (js literal: 0)) ifTrue: [^Error signal: 'ArgumentError'].
(js operator: '>=' with: jsIndex and: (js verbatim: 'this.length')) ifTrue: [^Error signal: 'ArgumentError'].
^js assign: (js propertyOf: (js ident: 'this') at: (js operator: '-' with: index and: (js literal: 1)))
toBe: value
)
public binarySearchFor: el between: start and: end toCompare: compare = (
(* This does a binary search for the index such that if el was inserted before it
the receiver would remain sorted. The receiver must be sorted relative to the
comparison block. The comparison block should return true if the first block argument
cannot appear after the second block argument *)
| low high |
low:: start.
high:: end.
[ low <= high ]
whileTrue:
[ | mid |
mid:: (low + high) // 2.
(compare value: (self at: mid) value: el)
ifTrue: [ low:: mid + 1 ]
ifFalse: [ high:: mid - 1 ].
].
^low
)
public collect: block = (
^js call: (js propertyOf: self at: (js literal: 'map')) with: {block}.
)
public copyFrom: start to: end = (
^js call: (js propertyOf: self at: (js literal: 'slice')) with: {start - 1. end}
)
public copyWith: extraElement = (
| newSize ::= self size + 1. a |
a:: (Array new: newSize) replaceFrom: 1 to: self size with: self.
a at: newSize put: extraElement.
^a
)
public copyWithSize: s = (
^(Array new: s) replaceFrom: 1 to: (s min: self size) with: self
)
defaultMedianOf: a <Int> and: b <Int> and: c <Int> ^<Int> = (
(* This is an untypesafe method that only works for MutableLists of elements that
have Magnitude relationships with each other. It is used by the default sorting
method. *)
| atA <E> atB <E> atC <E> |
atA:: at: a.
atB:: at: b.
atC:: at: c.
^atB <= atA
ifFalse: [ atC <= atB
ifFalse: [ b ]
ifTrue: [ atC <= atA
ifFalse: [ c ]
ifTrue: [ a ] ] ]
ifTrue: [ atB <= atC
ifFalse: [ b ]
ifTrue: [ atA <= atC
ifFalse: [ c ]
ifTrue: [ a ] ] ]
)
public defaultSort: l <Int> to: r <Int> = (
(* This is an untypesafe method that only works for MutableLists of elements that
have Magnitude relationships with each other *)
| i <Int> j <Int> x <E> m <Int> n <Int> |
i:: l.
j:: r.
n:: ((r - l) + 1).
n == 0
ifTrue: [ ^self ].
(* Pick the partition value. For <=7 elements, use the middle element.
For more than 7 but <= 40, use a median of three elements. For > 40,
use a median of three medians of three *)
m:: (l + r) // 2.
n > 7
ifTrue: [ | pl <Int> pn <Int> s <Int> |
pl:: l.
pn:: r.
n > 40
ifTrue: [ s:: n // 8.
pl:: defaultMedianOf: pl and: pl+s and: pl+s+s.
m:: defaultMedianOf: m-s and: m and: m+s.
pn:: defaultMedianOf: (pn-s)-s and: pn-s and: pn. ].
m:: defaultMedianOf: pl and: m and: pn. ].
x:: at: m.
[i <= j]
whileTrue:
[ | ati <E> atj <E> |
[ x <= (ati:: at: i)]
whileFalse: [i:: i + 1].
[(atj:: at: j) <= x]
whileFalse: [j:: j - 1].
i <= j
ifTrue: [ at: i put: atj.
at: j put: ati.
i:: i + 1.
j:: j - 1]
].
l < j ifTrue: [defaultSort: l to: j ].
i < r ifTrue: [defaultSort: i to: r ].
)
public do: block = (
js call: (js propertyOf: self at: (js literal: 'forEach')) with: {block}.
)
public do: block separatedBy: betweenBlock = (
| firstTime ::= true. |
self do: [:element |
firstTime ifTrue: [ firstTime:: false ] ifFalse: [betweenBlock value].
block value: element].
)
public first = (
^self at: 1
)
public flatMap: map <[:E | R def]> ^<Collection[R]> = (
^self flatten collect: map
)
public flatten ^ <Array> = (
| totalSize <Integer> ::= 0. c <Array> index <Integer> ::= 1. |
self do: [:e <E> | totalSize:: totalSize + (e isKindOfCollection ifTrue: [e size] ifFalse: [1])].
c:: Array new: totalSize.
do: [:e <E> | e isKindOfCollection
ifTrue: [e do: [:x | c at: index put: x. index:: index + 1]]
ifFalse: [c at: index put: e. index:: index + 1]
].
^c
)
public includes: element = (
self do: [:each | each = element ifTrue: [^true]].
^false
)
public indexOf: element <E> ^<Integer> = (
1 to: self size do: [:index | (self at: index) = element ifTrue: [^index]].
^0
)
public inject: initialValue into: accumulator = (
^js call: (js propertyOf: self at: (js literal: 'reduce')) with: {accumulator. initialValue}.
)
public isEmpty = (
^0 == size
)
public isKindOfArray ^<Boolean> = (
^true
)
public isKindOfCollection ^<Boolean> = (
^true
)
public keysAndValuesDo: binaryBlock <[:Integer :E]> = (
1 to: self size do: [:index | binaryBlock value: index value: (self at: index)]
)
public last = (
^self at: self size
)
public printString = (
| x ::= '{'. |
#FLAG. (* This is not safe for arrays that contain themselves. *)
self do: [:ea | x:: x , ea printString] separatedBy: [x:: x, '. '].
^x , '}'
)
public replaceFrom: start to: stop with: replacement = (
(* This destructively replaces elements from start to stop in the receiver.
Answer the receiver itself. Use copyReplaceFrom:to:with: for
insertion/deletion which may alter the size of the result. *)
replacement size = (stop - start + 1)
ifFalse: [self error: 'Size of replacement doesnt match'].
^self replaceFrom: start to: stop with: replacement startingAt: 1
)
public replaceFrom: start to: stop with: replacement startingAt: repStart = (
(* This destructively replaces elements from start to stop in the receiver
starting at index, repStart, in the sequenceable collection,
replacementCollection. Answer the receiver. No range checks are
performed. *)
| index repOff |
repOff:: repStart - start.
index:: start - 1.
[(index:: index + 1) <= stop]
whileTrue: [self at: index put: (replacement at: repOff + index)]
)
public select: block = (
^js call: (js propertyOf: self at: (js literal: 'filter')) with: {block}.
)
public size ^<Integer> = (
^js propertyOf: self at: (js literal: 'length')
)
public sort: binaryBlock = (
js
call: (js propertyOf: self at: (js literal: 'sort'))
with: {js functionOf: {'a'. 'b'} body: (js block: {
js return: ((binaryBlock value: (js ident: 'a') value: (js ident: 'b'))
ifTrue: [-1] ifFalse: [1])
})}.
)
public mergeSortFrom: startIndex to: stopIndex by: aBlock = (
(* fake for squeak compatibility *)
sort: aBlock
)
) : (
public new: size <Integer> = (
(* should be private but does not work that way! Possible compiler bug. *)
^self new: size withAll: nil
)
new: size <Integer> withAll: initialElement <E> ^<Array[E]> = (
| array index |
(js prefixOperator: 'typeof ' on: size) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
(js operator: '<' with: size and: (js literal: 0)) ifTrue: [^Error signal: 'ArgumentError'].
array:: js new: (js ident: 'Array') with: {size}.
js
for: (index:: 0)
while: (js operator: '<' with: index and: size)
step: (js postfixOperator: '++' on: index)
do: (js block: {
js assign: (js propertyOf: array at: index) toBe: initialElement}).
^array
)
public with: element = (
^{element}
)
)
public class Boolean uninstantiable = () (
public & other = (
(* Evaluating conjunction. *)
^js operator: '&&' with: self and: other
)
public and: alternative <[Boolean]> ^ <Boolean> = (
(* Non-evaluating conjunction *)
^js ternaryIf: self then: (js call: alternative with: {}) else: self
)
public asString = (
^self ifTrue: ['true'] ifFalse: ['false']
)
public ifFalse: else = (
^js ternaryIf: self then: nil else: (js call: else with: {})
)
public ifFalse: else ifTrue: then = (
^js ternaryIf: self then: (js call: then with: {}) else: (js call: else with: {})
)
public ifTrue: onTrue = (
^js ternaryIf: self then: (js call: onTrue with: {}) else: nil
)
public ifTrue: then ifFalse: else = (
^js ternaryIf: self then: (js call: then with: {}) else: (js call: else with: {})
)
public isKindOfBoolean = (
^true
)
public not = (
^js prefixOperator: '!' on: self.
)
public or: alternative <[Boolean]> ^ <Boolean> = (
(* Non-evaluating disjunction *)
^js ternaryIf: self then: self else: (js call: alternative with: {})
)
public printString = (
^self ifTrue: ['true'] ifFalse: ['false']
)
public value = (
^self
)
public | other = (
(* Evaluating disjunction. *)
^js operator: '||' with: self and: other
)
) : (
)
public class ByteArray uninstantiable = (
) (
public at: index <Integer> ^<E> = (
| jsIndex |
(js prefixOperator: 'typeof ' on: index) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
jsIndex:: js operator: '-' with: index and: (js literal: 1).
(js operator: '<' with: jsIndex and: (js literal: 0)) ifTrue: [^Error signal: 'ArgumentError'].
(js operator: '>=' with: jsIndex and: (js verbatim: 'this.length')) ifTrue: [^Error signal: 'ArgumentError'].
^js propertyOf: (js ident: 'this') at: jsIndex
)
public at: index <Integer> put: value <E> ^<E> = (
| jsIndex |
(js prefixOperator: 'typeof ' on: index) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
jsIndex:: js operator: '-' with: index and: (js literal: 1).
(js operator: '<' with: jsIndex and: (js literal: 0)) ifTrue: [^Error signal: 'ArgumentError'].
(js operator: '>=' with: jsIndex and: (js verbatim: 'this.length')) ifTrue: [^Error signal: 'ArgumentError'].
^js assign: (js propertyOf: (js ident: 'this') at: (js operator: '-' with: index and: (js literal: 1)))
toBe: value
)
public isEmpty = (
^0 == size
)
public isKindOfByteArray ^<Boolean> = (
^true
)
public keysAndValuesDo: binaryBlock <[:Integer :Integer]> = (
1 to: self size do: [:index | binaryBlock value: index value: (self at: index)]
)
public size ^<Integer> = (
^js propertyOf: self at: (js literal: 'length')
)
) : (
public new: size <Integer> ^<ByteArray> = (
(js prefixOperator: 'typeof ' on: size) == 'number' ifFalse: [^Error signal: 'ArgumentError'].
size < 0 ifTrue: [^Error signal: 'ArgumentError'].
^js new: (js ident: 'Uint8Array') with: {size}
)
)
public class Class = (|
public superclass
public mixin
public enclosingObject (* Remove me *)
public enclosingObjects
public name
|) (
public isKindOfBehavior = (
^true
)
public isKindOfClass = (
^true
)
public isMeta = (
^true
)
public mixinApply: newSuperclass = (
^vmmirror
applyMixin: (js propertyOf: self mixin at: (js literal: 'runtimeMixin'))
toSuperclass: newSuperclass
withEnclosingObjects: self enclosingObjects
)
public simpleName = (
^mixin name
)
) : (
)
public class Closure uninstantiable = () (
public cull: a1 = (
^js call: (js ident: 'this') with: {a1}
)
public cull: a1 cull: a2 = (
^js call: (js ident: 'this') with: {a1. a2}
)
public cull: a1 cull: a2 cull: a3 = (
^js call: (js ident: 'this') with: {a1. a2. a3}
)
public ensure: onUnwind = (
| result |
js try: (js block: {
result:: self value.
})
catch: (js ident: 'e')
with: (js block: {
onUnwind value.
js throw: (js ident: 'e').
}).
onUnwind value.
^result
)
public isKindOfClosure ^<Boolean> = (
^true
)
public numArgs ^<Integer> = (
^js propertyOf: self at: (js literal: 'length')
)
public on: errorClass do: handler = (
(* Perhaps we should match on mixin instead of class... *)
| c |
js try: (js block: {js return: self value})
catch: (js ident: 'e')
with: (js block: {
js if: (js operator: '===' with: (js propertyOf: (js ident: 'e') at: (js literal: 'newspeakClass')) and: (js ident: 'undefined'))
then: (js block: {js throw: (js ident: 'e')}).
js for: (c:: classOf: (js ident: 'e')) while: (js operator: '!==' with: c and: nil) step: (js verbatim: '') do: (js block: {
js if: (js operator: '===' with: c and: errorClass)
then: (js block: {js return: (handler value: (js ident: 'e'))}).
c:: c superclass.
}).
js throw: (js ident: 'e').
})
)
public value = (
^js call: (js ident: 'this') with: {}
)
public value: a1 = (
^js call: (js ident: 'this') with: {a1}
)
public value: a1 value: a2 = (
^js call: (js ident: 'this') with: {a1. a2}
)
public value: a1 value: a2 value: a3 = (
^js call: (js ident: 'this') with: {a1. a2. a3}
)
public valueWithArguments: args = (
^js call: (js propertyOf: (js ident: 'this') at: (js literal: 'apply')) with: {js ident: 'this'. args}
)
public whileFalse: aBlock = (
[self value] whileFalse: [aBlock value].
^nil
)
) : (
)
public class Exception = (|
public messageText (* squeak compatibility for Minitest *)
|) (
public printString = (
nil = messageText ifTrue: [^super class name].
^super class name, ': ', messageText
)
public signal = (
js verbatim: 'this.trace = new Error().stack'.
js throw: self.
)
public signal: message = (
(* squeak compatibility for Minitest *)
messageText:: message.
^self signal
)
) : (
public signal: message = (
^(self new messageText: message) signal
)
)
public class Future computing: block <[E]> = (
(* The simplest possible future. It evalutes its closure in response to nearly every message. *)
|
private blockOrValue000 <[E] | E> ::= block.
private state000 <Symbol> ::= #unresolved.
|) (
protected doesNotUnderstand: message = (
^message sendTo: self resolve
)
public isKindOfFuture ^<Boolean> = (
^true
)
resolve = (
state000 = #resolving ifTrue: [
Error signal: 'Divergent evaluation of ', blockOrValue000 printString].
state000 = #unresolved ifTrue: [
state000: #resolving.
blockOrValue000: blockOrValue000 value.
[blockOrValue000 isKindOfFuture] whileTrue:
[blockOrValue000: blockOrValue000 resolve].
state000: #resolved.
].
^blockOrValue000
)
public yourself = (
(* See InstanceMixin>>apply:withName: *)
^self resolve
)
) : (
)
public class Message mangledSelector: s arguments: a = (
(* FIXME: This factory should not be public. *)
|
public mangledSelector = s.
public arguments = a.
|) (
public printString = (
^'#',selector
)
public selector = (
^vmmirror unmangleSelector: mangledSelector
)
public sendTo: receiver = (
js try:
(js block: {
(js return:
(js call: (js propertyOf: (js propertyOf: receiver at: mangledSelector) at: (js literal: 'apply')) with: {receiver. arguments})
)
}
)
catch: (js ident: 'e')
with: (js block: {js return: (js call: (js propertyOf: receiver at: (js literal: 'dnu')) with: {self})})
)
) : (
mangle: selector = (
^((('$', selector)
replaceAll: ":" with: "$") (* Keyword *)
replaceAll: "`" with: "$") (* Qualified *)
replaceAll: "." with: "$" (* limit temp *)
)
public selector: selector = (
^self mangledSelector: (mangle: selector) arguments: {}
)
public selector: selector arguments: arguments = (
^self mangledSelector: (mangle: selector) arguments: arguments
)
)
public class MessageNotUnderstood receiver: r message: m = Exception (|
public receiver = r.
public message = m.
|) (
public messageText = (
(* squeak compatibility for Minitest *)
^self printString
)
public printString ^<String> = (
^'MessageNotUnderstood: ', (classOf: receiver) name, ' ', message selector
)
) : (
)
public class Metaclass = (
|
public superclass
public mixin
public enclosingObject
public name
public thisClass
|) (
public isKindOfBehavior = (
^true
)
public isMeta = (
^true
)
) : (
)
public class Mixin fromRuntimeMixin: rm = (js assign: (js propertyOf: self at: (js literal: 'runtimeMixin')) toBe: rm) (
public applyTo: superclass <Class> ^<Class> = (
(* assert: [isTopLevel] message: 'Only top-level mixins may be applied directly' *)
assert: [isMeta not] message: 'Only instance-side mixins may be applied directly'.
^self applyTo: superclass withName: (superclass name, ' <: ', name)
)
public applyTo: superclass <Class> withName: name <String> ^<Class> = (
^vmmirror
applyMixin: (js propertyOf: self at: (js literal: 'runtimeMixin'))
toSuperclass: superclass
withEnclosingObjects: {nil}
)
public isMeta ^<Boolean> = (
^js operator: '===' with: (js propertyOf: (js propertyOf: self at: (js literal: 'runtimeMixin')) at: (js literal: 'meta')) and: (js ident: 'undefined')
)
public name ^<Symbol> = (
^isMeta
ifTrue: [(js propertyOf: (js propertyOf: (js propertyOf: self at: (js literal: 'runtimeMixin')) at: (js literal: 'nonMeta')) at: (js literal: 'name')), ' class']
ifFalse: [js propertyOf: (js propertyOf: self at: (js literal: 'runtimeMixin')) at: (js literal: 'name')].
)
public printString ^<String> = (
^name, ' mixin'
)
) : (
)
public class Number uninstantiable = () (
public * other = (
^js operator: '*' with: self and: other
)
public + other = (
^js operator: '+' with: self and: other
)
public - other = (
^js operator: '-' with: self and: other
)
public / other = (
^js operator: '/' with: self and: other
)
public // other = (
^(self / other) floor
)
public < other = (
^js operator: '<' with: self and: other
)
public << other = (
^js operator: '<<' with: self and: other
)
public <= other = (
^js operator: '<=' with: self and: other
)
public = other = (
^js operator: '===' with: self and: other
)
public > other = (
^js operator: '>' with: self and: other
)
public >= other = (
^js operator: '>=' with: self and: other
)
public >> other = (
^js operator: '>>' with: self and: other
)
public \\ other = (
^self - (self // other * other)
)
public abs = (
self >= 0 ifTrue: [^self] ifFalse: [^-1.0 * self]
)
public asFloat = (
^self * 1.0
)
public asInteger = (
^self truncated
)
public asString ^<String> = (
^js call: (js propertyOf: self at: (js literal: 'toString')) with: {}
)
public asStringExponential: fractionDigits <Integer> ^<String> = (
fractionDigits < 0 ifTrue: [^Error signal].
fractionDigits > 20 ifTrue: [^Error signal].
^js call: (js propertyOf: self at: (js literal: 'toExponential')) with: {fractionDigits}
)
public asStringFixed: fractionDigits <Integer> ^<String> = (
fractionDigits < 0 ifTrue: [^Error signal].
fractionDigits > 100 ifTrue: [^Error signal].
^js call: (js propertyOf: self at: (js literal: 'toFixed')) with: {fractionDigits}
)
public asStringPrecision: precision <Integer> ^<String> = (
precision < 1 ifTrue: [^Error signal].
precision > 100 ifTrue: [^Error signal].
^js call: (js propertyOf: self at: (js literal: 'toPrecision')) with: {precision}
)
public asStringRadix: radix <Integer> ^<String> = (
radix < 2 ifTrue: [^Error signal].
radix > 36 ifTrue: [^Error signal].
^js call: (js propertyOf: (js call: (js propertyOf: self at: (js literal: 'toString')) with: {radix}) at: (js literal: 'toUpperCase')) with: {}
)
public between: min and: max = (
^self <= max and: [ self >= min ]
)
public bitAnd: other = (
^js operator: '&' with: self and: other
)
public bitInvert = (
^-1 - self
)
public bitOr: other = (
^js operator: '|' with: self and: other
)
public bitXor: other = (
^js operator: '^' with: self and: other
)
public ceiling = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'ceil')) with: {self}
)
public cos = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'cos')) with: {self}
)
public exp = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'exp')) with: {self}
)
public floor = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'floor')) with: {self}
)
public hash = (
(* Truncate to signed 32-bit integer. *)
^js operator: '|' with: self and: (js literal: 0)
)
public isKindOfFloat = (
^true
)
public isKindOfInteger = (
^self = self asInteger
)
public isKindOfNumber = (
^true
)
public isNaN ^<Boolean> = (
^js call: (js ident: 'isNaN') with: {self}
)
public ln = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'log')) with: {self}
)
public log = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'log10')) with: {self}
)
public max: other = (
^self > other ifTrue: [ self ] ifFalse: [ other ]
)
public min: other = (
^self < other ifTrue: [ self ] ifFalse: [ other ]
)
public negated = (
^0 - self
)
public printString = (
^js call: (js propertyOf: self at: (js literal: 'toString')) with: {}
)
public quo: other = (
^(self / other) truncated
)
public rem: other = (
^self - ((self quo: other) * other)
)
public rounded = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'round')) with: {self}
)
public sign = (
self > 0 ifTrue: [^1].
self < 0 ifTrue: [^-1].
^0
)
public sin = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'sin')) with: {self}
)
public sqrt = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'sqrt')) with: {self}
)
public tan = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'tan')) with: {self}
)
public timesRepeat: block = (
| i ::= 0. |
js
for: (js verbatim: '')
while: (js operator: '<' with: i and: (js ident: 'this'))
step: (js postfixOperator: '++' on: i)
do: (js block: {js call: block with: {}})
)
public to: stop = (
^platform collections Interval from: self to: stop
)
public to: limit do: block = (
| i ::= self. |
js
for: (js verbatim: '')
while: (js operator: '<=' with: i and: limit)
step: (js postfixOperator: '++' on: i)
do: (js block: {js call: block with: {i}})
)
public truncated = (
^self > 0 ifTrue: [self floor] ifFalse: [self ceiling]
)
public ** exponent = (
^js call: (js propertyOf: (js ident: 'Math') at: (js literal: 'pow')) with: {self. exponent}
)
) : (
public parse: str <String> ^<Float> = (
^js call: (js ident: 'parseFloat') with: {str}
)
public parse: str <String> radix: radix <Integer> ^<Integer> = (
^js call: (js ident: 'parseInt') with: {str. radix}
)
)
public class Object = ImplementationBase () (
public = other = (
^js operator: '===' with: self and: other
)
public == other = (
^js operator: '===' with: self and: other
)
Array ^ <KernelForJS Array class> = (
^outer KernelForJS Array
)
ByteArray ^ <KernelForJS ByteArray class> = (
^outer KernelForJS ByteArray
)
Character ^ <KernelForJS Character class> = (
^outer KernelForJS String
)
Error ^ <KernelForJS Error class> = (
^outer KernelForJS Error
)
Float ^ <KernelForJS Number class> = (
^outer KernelForJS Number
)
Future ^<KernelForJS Future class> = (
^outer KernelForJS Future
)
Integer ^ <KernelForJS Number class> = (
^outer KernelForJS Number
)
Object ^ <KernelForJS Object class> = (
^outer KernelForJS Object
)
String ^ <KernelForJS String class> = (
^outer KernelForJS String
)
assert: condition <[Boolean]> message: message = (
(* Raises an error with the given message if and only if condition evaluates to false. *)
condition isKindOfClosure ifFalse: [Error signal: 'Block required'].
condition value ifFalse: [Error signal: 'Assertion failed: ', message]
)
private cannotReturn: value = (
(* compiler entry *)
Exception signal: 'CannotReturn: ', value printString.
)
protected class = (
^classOf: self
)
protected doesNotUnderstand: message = (
(0 = message arguments size) ifTrue: [(message selector startsWith: 'isKindOf') ifTrue: [^false]].
^(MessageNotUnderstood receiver: self message: message) signal
)
public hash ^<Integer> = (
(* It might be worth giving every object the hashCode field at instantiation to avoid map transitions. *)
js verbatim: 'while(this.hashCode === 0) this.hashCode = Math.random() * 0x3FFFFFF | 0'.
^js verbatim: 'this.hashCode'
)
public ifNil: nilBlock = (
^self
)
public ifNil: nilBlock ifNotNil: notNilBlock = (
^notNilBlock value: self
)
public ifNotNil: nonNilBlock = (
^nonNilBlock value: self
)
public isAlien = (
^false
)
public isExpat = (
^false
)
public isKindOfFuture = (
^false
)
public isNil = (
^false
)
public notNil = (
^true
)
public out = (
print: self printString
)
public printString = (
^'instance of ', self class name
)
public yourself = (
^self
)
public ~= other = (
^(self = other) not
)
) : (
)
public class Stopwatch = (|
private cumulativeMillis ::= 0.
private startMillis
|) (
private currentMonotonicMillis = (
^js verbatim: 'performance.now()'
)
public elapsedMicroseconds ^<Integer> = (
^elapsedMilliseconds * 1000
)
public elapsedMilliseconds ^<Integer> = (
nil = startMillis ifTrue: [^cumulativeMillis].
^cumulativeMillis + (currentMonotonicMillis - startMillis)
)
public start = (
nil = startMillis ifFalse: [^self (* Already running. *)].
startMillis:: currentMonotonicMillis
)
public stop = (
nil = startMillis ifTrue: [^self (* Already stopped. *)].
cumulativeMillis:: cumulativeMillis + (currentMonotonicMillis - startMillis).
startMillis:: nil.
)
) : (
)
public class String uninstantiable = () (
public , other = (
^js call: (js propertyOf: self at: (js literal: 'concat')) with: {other}
)
public = other = (
^js operator: '===' with: self and: other
)
public asString = (
^self
)
public asSymbol = (
^self
)
public at: index = (
^js propertyOf: (js ident: 'this') at: (js operator: '-' with: index and: (js literal: 1))
)
public copyFrom: start to: end = (
^js call: (js propertyOf: self at: (js literal: 'substring')) with: {start - 1. end}
)
public do: action = (
|
i ::= 0.
len ::= js verbatim: 'this.length'.
|
js
for: (js verbatim: '')
while: (js operator: '<' with: i and: len)
step: (js postfixOperator: '++' on: i)
do: (js block: {
js call: action with: {js propertyOf: self at: i}}).
)
public endsWith: substring = (
^js call: (js propertyOf: self at: (js literal: 'endsWith')) with: {substring}
)
public first = (
^self at: 1
)
public hash ^<Integer> = (
js var: 'l' value: (js propertyOf: (js ident: 'this') at: (js literal: 'length')).
^js ternaryIf: (js operator: '==' with: (js ident: 'l') and: (js literal: 0))
then: (js literal: 1)
else: (js verbatim: '(l * 32 ^ this.charCodeAt(0)) * 32 ^ this.charCodeAt(l - 1)')
)
public includes: object = (
self do: [:element | object = element ifTrue: [^true]].
^false
)
public indexOf: substring <String> ^ <Integer> = (
^1 + (js call: (js propertyOf: self at: (js literal: 'indexOf')) with: {substring})
)
public indexOf: substring <String> startingAt: index <Integer> ^<Integer> = (
^1 + (js call: (js propertyOf: self at: (js literal: 'indexOf')) with: {substring. index - 1})
)
public isEmpty ^ <Boolean> = (
^0 = size
)
public isKindOfString ^ <Boolean> = (
^true
)
public last = (
^self at: self size
)
public lastIndexOf: substring <String> ^<Integer> = (
^1 + (js call: (js propertyOf: self at: (js literal: 'lastIndexOf')) with: {substring})
)
public lastIndexOf: substring <String> startingAt: index <Integer> ^<Integer> = (
^1 + (js call: (js propertyOf: self at: (js literal: 'lastIndexOf')) with: {substring. index - 1})
)
public out = (
print: self.
)
public printString = (
^js verbatim: '"''"+this.toString()+"''"'
)
public replaceAll: substring with: newSubstring = (
#BOGUS. (* Ought to be able to say this.replace(target, replacement, 'g'). *)
js var: 'target' value: substring.
js var: 'replacement' value: newSubstring.
^js verbatim: 'this.replace(new RegExp(target.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), replacement)'
)
public runeAt: index = (
(* This is wrong for surrogate pairs. *)
^js call: (js propertyOf: self at: (js literal: 'charCodeAt')) with: {index - 1}
)
public size = (
^js verbatim: 'this.length'
)
public splitBy: str = (
^js call: (js propertyOf: self at: (js literal: 'split')) with: {str}
)
public startsWith: substring = (
^js call: (js propertyOf: self at: (js literal: 'startsWith')) with: {substring}
)
public withBlanksTrimmed ^ <String> = (
| startIndex ::=1. endIndex ::= size. |
isEmpty ifTrue: [^self].
[(self at: startIndex) <= 32] whileTrue: [startIndex:: startIndex + 1. startIndex > size ifTrue: [^'']].
[(self at: endIndex) <= 32] whileTrue: [endIndex:: endIndex - 1].
^copyFrom: startIndex to: endIndex
)
) : (
public cr = (
^js verbatim: '"\r"'
)
public fromRune: rune = (
^js call: (js propertyOf: (js ident: 'String') at: (js literal: 'fromCharCode')) with: {rune}
)
public fromRunes: runes = (
^js call: (js propertyOf: (js propertyOf: (js ident: 'String') at: (js literal: 'fromCharCode')) at: (js literal: 'apply')) with: {js ident: 'null'. runes asArray}.
)
public lf = (
^js verbatim: '"\n"'