-
Notifications
You must be signed in to change notification settings - Fork 11
/
MirrorsForJS.ns
1025 lines (992 loc) · 40.2 KB
/
MirrorsForJS.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 MirrorsForJS usingPlatform: p runtime: r vmMirror: vmm = (
(* The reflection library for the Javascript-based implementation of Newspeak.
Copyright Google Inc. 2014 - 2017
*)
|
private List = p collections List.
private Map = p collections Map.
private Mixin = p kernel Mixin.
private StringBuilder = p kernel StringBuilder.
private ImmutableMirrorGroup = (r MirrorGroups usingPlatform: p) ImmutableMirrorGroup.
private vmmirror = vmm.
private parserLib = Future computing: [r CombinatorialParsing usingPlatform: p].
private grammar = Future computing: [r NewspeakGrammar usingPlatform: p parsers: parserLib].
private asts = Future computing: [r NewspeakASTs usingPlatform: p].
private parsing = Future computing: [r NewspeakParsing usingPlatform: p grammar: grammar asts: asts].
public metadataParsing = Future computing: [r MetadataParsing usingPlatform: p].
private generation = Future computing: [r JavascriptGeneration usingPlatform: p].
private compilation = Future computing: [r Compilation usingPlatform: p asts: asts parsing: parsing generation: generation].
public compiler = Future computing: [compilation Compiler new].
|) (
class AbstractClassHeaderMirror = (
) (
public accessModifier = (
(* Ths won't work for slots. So should we set this up explicitly and store the access? If we had the mangled name we could use that to decided; might be easier to keep mangled name and unmangle it on demand *)
(source startsWith: 'public ') ifTrue: [^#public].
(source startsWith: 'private ') ifTrue: [^#private].
^#protected
)
public classComment ^ <String> = (
^(compilation parser classHeader parseString: source) classComment
)
public source ^ <String> = (
subclassResponsibility
)
) : (
)
public class ClassDeclarationBuilder forExistingMixin: m <Mixin> within: enclosing <ClassDeclarationBuilder> = (
(*
A mutable description of a class, ready for installation. This class is the abstract superclass of two alternate
implementations: IRBasedClassDeclarationBuilder and MixinBasedClassDeclarationBuilder.
The former is created based upon the compiler's intermediate representation, and is useful when producing a
complete class from source code. The latter is created based upon an existing class declaration, specifically
its mixin. This is much faster, as it does not require compilation to produce the builder. This is important
since some IDE presenters work off a builder, and requiring a complete compile may introduce a noticeable
delay (at least until the compiler gets a lot faster).
Subclasses must implement a number of methods: computeInstanceSide, computeClassSide, name and declarationData.
The first two specify how the MixinBuilders that represent the two sides of the class are computed. The name
computation also depends on the underlying structure.
The last subclass responsibility method, declarationData,
specifies how to compute other data representing the class under construction. These will differ depending
on whether the initial input for the new class is an IR (obtained by compiling source code) or a mixin (obtained
from an existing class in the runtime). The builders for the sides are stored in the instanceSide and classSide slots.
In all cases, the builder retains a copy of the existing mixin, if it exists. This the first formal parameter, m,
which is saved in the slot prvtExistingMixin.
If the class is new, m is nil.
In addition, the builder expect another builder representing the enclosing class declaration. For top level classes
this is nil.
The remaining slots are reserved for future use to support deletion of members.
Installation is always based on recompiling the current (likely modified) version of the class from source.
This is because the compiled representation statically resolves the lexical level of a here send, and adding
or removing members may impact that resolution within the class (including its nested classes).
The recompiled IR is installed, either by updating the existing mixin in the runtime, or by creating a new one,
which is stored in prvtExistingMixin so that it becomes the "existing mixin " from that point on.
Still lacking are facilities for correctly adding slots (and hence nested classes) and for delteions.
*)
|
prvtExistingMixin <Mixin> ::= m.
public enclosingClass <ClassDeclarationBuilder> = enclosing.
private instanceSideSlot <MixinBuilder>
private classSideSlot <MixinBuilder>
deletedInstanceMethods <List[Symbol]>
deletedClassMethods <List[Symbol]>
|) (
public accessModifier = (
^declarationData accessModifier
)
public classSide ^ <MixinBuilder> = (
nil = classSideSlot ifTrue: [classSideSlot:: computeClassSide].
^classSideSlot
)
public computeClassDeclIR ^ <IntermediateClassDeclaration> = (
(* Compile latest source into an IR. But this won't work for nested classes
| src = 'Newspeak3 ''Uncategorized'' ', source. |
('compiling class:', src) out.*)
^nil = enclosingClass
ifTrue: [
compiler compileClassSource: 'Newspeak3 ''Uncategorized'' ', source within: nil.
]
ifFalse: [(* compiler compileClassSource: source within: enclosingClass.
Is enclosingClass the right kind of argument?
Can we make the compiler API more uniform so we can always just make this call?
This assumes we can dispense with compiling the entire module.
*)
enclosingClass computeClassDeclIR instanceSide nestedClasses detect:
[:ncd <IntermediateClassDeclaration> | ncd qualifiedName = qualifiedName]
]
)
computeClassSide ^ <MixinBuilder> = (
subclassResponsibility
)
computeInstanceSide ^ <MixinBuilder> = (
subclassResponsibility
)
declarationData = (
subclassResponsibility
)
public install ^<ClassDeclarationMirror> = (
| writer sb <StringBuilder> src <String> runtimeMixin <JRM> cdir <IntermediateClassDeclaration> |
writer:: generation Writer new.
sb:: StringBuilder new.
cdir:: computeClassDeclIR.
writer generateSourceFor: cdir runtimeMixin on: sb. (* compute up to date JS translation *)
src:: sb asString.
src out.
runtimeMixin:: js call: (js ident: 'eval') with: {src}. (* compute up to date runtime mixin *)
prvtExistingMixin isNil (* Update based on latest mixin *)
ifTrue: [
prvtExistingMixin:: Mixin fromRuntimeMixin: runtimeMixin.
updateSourceIndicesOfNewRuntimeMixin: runtimeMixin from: cdir.
(* this is where we should recompile the enclosing class*)
]
ifFalse: [updateMixinFrom: runtimeMixin with: cdir].
^ClassDeclarationMirror reflecting: prvtExistingMixin.
)
public instanceSide ^ <MixinBuilder> = (
nil = instanceSideSlot ifTrue: [instanceSideSlot:: computeInstanceSide].
^instanceSideSlot
)
public name ^<Symbol> = (
^subclassResponsibility
)
public qualifiedName = (
^declarationData qualifiedName
)
public simpleName ^<Symbol> = (
^name
)
public source ^<String> = (
| sb = StringBuilder new. |
sb add: header source.
sb writeln: ' ( '.
instanceSide nestedClasses do: [:ea <ClassDeclarationMirror | ClassDeclarationBuilder> | sb writeln: ea source].
instanceSide methods do: [:ea <MethodMirror | MethodBuilder> | sb writeln: ea source].
sb writeln: ' ) : ( '.
classSide methods do: [:ea <MethodMirror | MethodBuilder> | sb writeln: ea source].
sb writeln: ' )'.
^sb asString
)
updateApplicationsOf: oldRuntimeMixin <JRM> from: newRuntimeMixin <JRM> by: classDeclIR <IntermediateClassDeclaration> = (
|
newRuntimeMetamixin <MJRM> = js propertyOf: newRuntimeMixin at: (js literal: 'meta').
applications <Array[JRC]> = js propertyOf: oldRuntimeMixin at: (js literal: 'applications').
|
(* TODO: handle deletes *)
(* TODO: copy nested classes also? *)
(* TODO: copy new metadata *)
applications do: [:runtimeClass <JRC> |
|
runtimeMetaclass <MJRC> = js propertyOf: runtimeClass at: (js literal: 'meta').
|
(*runtimeClass out.*)
(* Cf. IntermediateClassDeclaration copyMethods: *)
classDeclIR instanceSide methods do: [:ea <IntermediateMethod> |
| jsProName <String> jsPubName <String> |
jsPubName:: compilation names manglePublic: ea name.
jsProName:: compilation names mangleProtected: ea name.
ea isProtected ifTrue: [
js assign: (js propertyOf: runtimeClass at: jsProName) toBe: (js propertyOf: newRuntimeMixin at: jsPubName).
js assign: (js propertyOf: runtimeClass at: jsPubName) toBe: (js call: (js ident: 'dnuCatcher') with: {jsPubName}).
].
ea isPublic ifTrue: [
js assign: (js propertyOf: runtimeClass at: jsProName) toBe: (js propertyOf: newRuntimeMixin at: jsPubName).
js assign: (js propertyOf: runtimeClass at: jsPubName) toBe: (js propertyOf: newRuntimeMixin at: jsPubName).
].
].
classDeclIR classSide methods do: [:ea <IntermediateMethod> |
| jsProName <String> jsPubName <String> |
jsPubName:: compilation names manglePublic: ea name.
jsProName:: compilation names mangleProtected: ea name.
ea isProtected ifTrue: [
js assign: (js propertyOf: runtimeMetaclass at: jsProName) toBe: (js propertyOf: newRuntimeMetamixin at: jsPubName).
js assign: (js propertyOf: runtimeMetaclass at: jsPubName) toBe: (js call: (js ident: 'dnuCatcher') with: {jsPubName}).
].
ea isPublic ifTrue: [
js assign: (js propertyOf: runtimeMetaclass at: jsProName) toBe: (js propertyOf: newRuntimeMetamixin at: jsPubName).
js assign: (js propertyOf: runtimeMetaclass at: jsPubName) toBe: (js propertyOf: newRuntimeMetamixin at: jsPubName).
].
].
].
)
updateHeaderSourceForNewRuntimeMixin: runtimeMixin <JRM> from: cdir <IntermediateClassDeclaration> = (
|
headerSourceIndex <Integer> ::= js propertyOf: (js ident: 'sources') at: (js literal: 'length').
headerSource = cdir headerSource.
|
js assign: (js propertyOf: runtimeMixin at: (js literal: 'header')) toBe: headerSourceIndex.
js assign: (js propertyOf: (js ident: 'sources') at: headerSourceIndex) toBe: headerSource.
)
updateInstanceMixin: oldRuntimeMixin <JRM> from: newRuntimeMixin <JRM> by: classDeclIR <IntermediateClassDeclaration> ^ <List[Symbol]> = (
(* TODO: handle deletes
go thru old methods and see if they exist in new methods, if not add them to deleted list? and remove from old.
*)
| newMethods <List[IntermediateMethod]> = classDeclIR instanceSide methods. |
deletedInstanceMethods:: List new.
(* (js propertyOf: oldRuntimeMixin at: #methods) forEach: [:m <MM> |
(newMethods anySatisfy: [:im <IntermediateMethod> |
im name = (compilation names manglePublic: (js propertyOf: m at: #name))
])
ifFalse: [
deletedInstanceMethods add: im name.
(* remove from old; need to add delete support to JavascriptGeneration; not critical *)
].
].*)
(* TODO: copy nested classes also? *)
(* TODO: copy new metadata *)
newMethods do: [:ea <IntermediateMethod> | (* insert modified or new instance methods into mixin *)
| jsName <String> |
jsName:: compilation names manglePublic: ea name.
js assign: (js propertyOf: oldRuntimeMixin at: jsName) toBe: (js propertyOf: newRuntimeMixin at: jsName).
].
{#name. #header. #slots. #methods. #nestedClasses} do: [:jsName |
js assign: (js propertyOf: oldRuntimeMixin at: jsName) toBe: (js propertyOf: newRuntimeMixin at: jsName).
].
)
updateMetaMixin: oldRuntimeMetamixin <MJRM> from: newRuntimeMetamixin <MJRM> by: classDeclIR <IntermediateClassDeclaration> ^ <List[Symbol]> = (
| newMethods <List[IntermediateMethod]> = classDeclIR classSide methods. |
(* TODO: handle deletes *)
deletedClassMethods:: List new.
(* TODO: copy nested classes also? *)
(* TODO: copy new metadata *)
newMethods do: [:ea <IntermediateMethod> | (* insert modified or new class methods into mixin *)
| jsName <String> |
jsName:: compilation names manglePublic: ea name.
js assign: (js propertyOf: oldRuntimeMetamixin at: jsName) toBe: (js propertyOf: newRuntimeMetamixin at: jsName).
].
{#slots. #methods. #nestedClasses} do: [:jsName <Symbol> |
js assign: (js propertyOf: oldRuntimeMetamixin at: jsName) toBe: (js propertyOf: newRuntimeMetamixin at: jsName).
].
)
updateMixin: oldMixin <Mixin> from: newRuntimeMixin <JRM> by: classDeclIR <IntermediateClassDeclaration> = (
|
deletedInstanceMethods <List[Symbol]>
deletedClassMethods <List[Symbol]>
oldRuntimeMixin <JRM> = js propertyOf: oldMixin at: (js literal: 'runtimeMixin').
oldRuntimeMetamixin <MJRM> = js propertyOf: oldRuntimeMixin at: (js literal: 'meta').
newRuntimeMetamixin <MJRM> = js propertyOf: newRuntimeMixin at: (js literal: 'meta').
|
(* TODO: handle deletes *)
(* TODO: copy nested classes also? *)
(* TODO: copy new metadata *)
deletedInstanceMethods:: updateInstanceMixin: oldRuntimeMixin from: newRuntimeMixin by: classDeclIR.
deletedClassMethods:: updateMetaMixin: oldRuntimeMetamixin from: newRuntimeMetamixin by: classDeclIR.
updateApplicationsOf: oldRuntimeMixin from: newRuntimeMixin by: classDeclIR.
)
updateMixinFrom: runtimeMixin <JRM> with: cdir <IntermediateClassDeclaration> = (
|
instanceMethodSourceIndices <Map[String, Integer]> = Map new.
classMethodSourceIndices <Map[String, Integer]> = Map new.
runtimeMetamixin <MJRM> = js propertyOf: runtimeMixin at: (js literal: 'meta').
existingMixin <JRM> = js propertyOf: prvtExistingMixin at: (js literal: 'runtimeMixin').
existingMetamixin <MJRM> = js propertyOf: existingMixin at: (js literal: 'meta').
newSourceIndex <Integer> ::= js propertyOf: (js ident: 'sources') at: (js literal: 'length').
|
(* TODO: Also update source indices for header. *)
(* TODO: update access modifiers if they have changed? *)
(* collect existing mixin's source indices into a map keyed by name *)
(js propertyOf: existingMixin at: (js literal: 'methods')) do: [:ea <MM> |
instanceMethodSourceIndices at: (js propertyOf: ea at: (js literal: 'name')) put: (js propertyOf: ea at: (js literal: 'source'))
].
(js propertyOf: existingMetamixin at: (js literal: 'methods')) do: [:ea <MM> |
classMethodSourceIndices at: (js propertyOf: ea at: (js literal: 'name')) put: (js propertyOf: ea at: (js literal: 'source'))
].
(* Augment maps of source indices with entries for any new methods *)
(js propertyOf: runtimeMixin at: (js literal: 'methods')) do:
[:e <MM> | | selector |
selector:: (js propertyOf: e at: (js literal: 'name')).
instanceMethodSourceIndices at: selector ifAbsent: [instanceMethodSourceIndices at: selector put: newSourceIndex].
newSourceIndex:: newSourceIndex + 1.
].
(js propertyOf: runtimeMetamixin at: (js literal: 'methods')) do:
[:e <MM> | | selector |
selector:: (js propertyOf: e at: (js literal: 'name')).
classMethodSourceIndices at: selector ifAbsent: [classMethodSourceIndices at: selector put: newSourceIndex].
newSourceIndex:: newSourceIndex + 1.
].
(* Update global sources array with any changed or new source code *)
instanceSide methods do: [:m <MethodBuilder> | | sourceIndex <Integer> |
sourceIndex:: instanceMethodSourceIndices at: m name.
m source ~= nil ifTrue: [js assign: (js propertyOf: (js ident: 'sources') at: sourceIndex) toBe: m source].
].
classSide methods do: [:m <MethodBuilder> | | sourceIndex <Integer> |
sourceIndex:: classMethodSourceIndices at: m name.
m source ~= nil ifTrue: [js assign: (js propertyOf: (js ident: 'sources') at: sourceIndex) toBe: m source].
].
(* Ensure new runtime structure has correct source indices in its metadata*)
(* what about the name itself? And the access modifier *)
(js propertyOf: runtimeMixin at: (js literal: 'methods')) do: [:mm <MM> |
js assign: (js propertyOf: mm at: (js literal: 'source'))
toBe: (instanceMethodSourceIndices at: (js propertyOf: mm at: (js literal: 'name'))).
].
(js propertyOf: runtimeMetamixin at: (js literal: 'methods')) do: [:mm <MM> |
js assign: (js propertyOf: mm at: (js literal: 'source'))
toBe: (classMethodSourceIndices at: (js propertyOf: mm at: (js literal: 'name'))).
].
(* Update class header source index *)
js assign: (js propertyOf: runtimeMixin at: (js literal: 'header')) toBe: (js propertyOf: existingMixin at: (js literal: 'header')).
(* Update the existing runtime based on the new one *)
updateMixin: prvtExistingMixin from: runtimeMixin by: cdir
)
updateSourceIndicesOfNewRuntimeMixin: runtimeMixin <JRM> from: cdir <IntermediateClassDeclaration> = (
|
instanceMethodSourceIndices <Map[String, Integer]> = Map new.
classMethodSourceIndices <Map[String, Integer]> = Map new.
runtimeMetamixin <MJRM> = js propertyOf: runtimeMixin at: (js literal: 'meta').
newSourceIndex <Integer> ::= js propertyOf: (js ident: 'sources') at: (js literal: 'length').
|
(* TODO: update access modifiers if they have changed? *)
(* Augment maps of source indices with entries for any new methods *)
(js propertyOf: runtimeMixin at: (js literal: 'methods')) do:
[:e <MM> | | selector |
selector:: (js propertyOf: e at: (js literal: 'name')).
instanceMethodSourceIndices at: selector ifAbsent: [instanceMethodSourceIndices at: selector put: newSourceIndex].
newSourceIndex:: newSourceIndex + 1.
].
(js propertyOf: runtimeMetamixin at: (js literal: 'methods')) do:
[:e <MM> | | selector |
selector:: (js propertyOf: e at: (js literal: 'name')).
classMethodSourceIndices at: selector ifAbsent: [classMethodSourceIndices at: selector put: newSourceIndex].
newSourceIndex:: newSourceIndex + 1.
].
(* Update global sources array with any changed or new source code *)
instanceSide methods do: [:m <MethodBuilder> | | sourceIndex <Integer> |
sourceIndex:: instanceMethodSourceIndices at: m name.
m source ~= nil ifTrue: [js assign: (js propertyOf: (js ident: 'sources') at: sourceIndex) toBe: m source].
].
classSide methods do: [:m <MethodBuilder> | | sourceIndex <Integer> |
sourceIndex:: classMethodSourceIndices at: m name.
m source ~= nil ifTrue: [js assign: (js propertyOf: (js ident: 'sources') at: sourceIndex) toBe: m source].
].
(* Ensure new runtime structure has correct source indices in its metadata*)
(* what about the name itself? And the access modifier *)
(js propertyOf: runtimeMixin at: (js literal: 'methods')) do: [:mm <MM> |
js assign: (js propertyOf: mm at: (js literal: 'source'))
toBe: (instanceMethodSourceIndices at: (js propertyOf: mm at: (js literal: 'name'))).
].
(js propertyOf: runtimeMetamixin at: (js literal: 'methods')) do: [:mm <MM> |
js assign: (js propertyOf: mm at: (js literal: 'source'))
toBe: (classMethodSourceIndices at: (js propertyOf: mm at: (js literal: 'name'))).
].
(* Update class header source index *)
updateHeaderSourceForNewRuntimeMixin: runtimeMixin from: cdir.
)
) : (
public fromSource: src <String> ^<ClassDeclarationBuilder> = (
^self fromUnitSource: 'Newspeak3 ''Uncategorized'' ', src
)
public fromUnitSource: src <String> ^<ClassDeclarationBuilder> = (
| ir <IntermediateMixin> = (compiler compileClassSource: src within: nil). |
(* source indices will be BOGUS here *)
^IRBasedClassDeclarationBuilder fromIR: ir existingMixin: nil within: nil
)
public reflecting: mixin <InstanceMixin> ^<ClassDeclarationBuilder> = (
| m = ClassDeclarationMirror reflecting: mixin. |
nil = m enclosingClass ifFalse:
[^m enclosingClass asBuilder instanceSide nestedClasses findMirrorNamed: m simpleName].
^MixinBasedClassDeclarationBuilder
forExistingMixin: mixin
within: nil
)
)
class ClassDeclarationMirror reflecting: mixin = (|
private reflectee = mixin.
public instanceSide <MixinMirror> = MixinMirror reflecting: mixin.
public classSide <MixinMirror> = MixinMirror reflecting: (classMixinOf: mixin).
|) (
public accessModifier ^ <Symbol> = (
^header accessModifier
)
public applyToObject ^<ClassMirror> = (
nil = enclosingClass ifFalse: [^notTopLevel].
^ClassMirror reflecting: (reflectee applyTo: Object withName: simpleName)
)
public asBuilder ^<ClassDeclarationBuilder> = (
^ClassDeclarationBuilder reflecting: reflectee
)
public definingMixin ^<MixinMirror> = (
| ec = enclosingClass. |
nil = ec ifTrue: [^nil].
^ec instanceSide
)
public enclosingClass ^ <ClassDeclarationMirror> = (
| enclosing <Class> |
enclosing:: (js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'enclosingMixin')).
(js operator: '===' with: enclosing and: (js ident: 'undefined')) ifTrue: [^nil].
^ClassDeclarationMirror reflecting: (Mixin fromRuntimeMixin: enclosing)
)
public header ^<ClassHeaderMirror> = (
^ClassHeaderMirror reflecting: reflectee
)
public isKindOfClassDeclarationMirror ^<Boolean> = (
^true
)
public name = (
^(reflectee name splitBy: '`') last
)
public primaryFactorySelector ^ <Symbol> = (
^header primaryFactorySelector
)
public qualifiedName = (
#BOGUS. (* Remove when currentMixinUID uses a proper token. *)
^reflectee name
)
public simpleName = (
^name
)
public source ^<String> = (
| sb = StringBuilder new. |
sb add: header source.
sb writeln: ' ( '.
instanceSide nestedClasses do: [:ea | sb writeln: ea source].
instanceSide methods do: [:ea | sb writeln: ea source].
sb writeln: ' ) : ( '.
classSide methods do: [:ea | sb writeln: ea source].
sb writeln: ' )'.
^sb asString
)
) : (
)
class ClassHeaderMirror reflecting: mixin = AbstractClassHeaderMirror (|
private reflectee <Mixin> = mixin.
|) (
public isKindOfClassHeaderMirror ^<Boolean> = (
^true
)
public name = (
^(reflectee name splitBy: '`') last
)
public preamble ^ <String> = (
(* Foo factory = SuperFoo superFactory *)
| headerAst |
headerAst:: compilation parser classHeader parseString: source.
^source copyFrom: headerAst start to: headerAst superConstructorCall end
)
public primaryFactorySelector ^ <Symbol> = (
| headerAst |
headerAst:: compilation parser classHeader parseString: source.
^headerAst constructor selector
)
public selectors ^ <List[Symbol]> = (
#BIOGUS yourself. (* need to construct an index when parsing *)
^List new
)
public source ^<String> = (
| sourceIndex = js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'header'). |
^js propertyOf: (js ident: 'sources') at: sourceIndex.
)
public metadata ^ <Map[String, String]> = (
^parseMetadata metadata
)
parseMetadata ^ <MetadataParser> = (
|
src <String> = source copyFrom: 1 to: (source indexOf: '=' startingAt: 1) - 1.
metadataParser <MetadataParser> = metadataParsing MetadataParser onSource: src.
|
metadataParser gatherMetadataBackwards.
^metadataParser
)
) : (
)
public class ClassMirror reflecting: r <Class> = (|
public reflectee <Class> = r.
|) (
public = other <Object> ^<Boolean> = (
other isKindOfClassMirror ifFalse: [^false].
^js operator: '===' with: reflectee and: other reflectee
)
public allSuperclasses ^ <List[ClassMirror]> = (
| klass <Class> superclasses <List[Class]> |
superclasses:: List new.
klass:: superclass.
[klass isNil] whileFalse: [
superclasses add: klass.
klass:: klass superclass.
].
^superclasses
)
private computeMirrorGroup: mgAccessor <[:MixinMirror | MirrorGroup]> ^ <List[Mirror]> = (
(* Return a mirror group based on the mixins group and those of all superclasses. The argument mgAccessor extracts a mirror group from any mirror provided to it. *)
| mg <MirrorGroup> |
mg:: List new.
(allSuperclasses reverse add: self; yourself)
do: [:klass <ClassMirror> | mg addAll: (mgAccessor value: klass mixin)].
^mg
)
public declaration ^ <ClassDeclarationMirror> = (
(* Convenience method to make the API more usable *)
^mixin declaration
)
public enclosingObject ^<ObjectMirror> = (
^ObjectMirror reflecting: reflectee enclosingObject
)
public hash ^<Integer> = (
^(identityHashOf: reflectee) bitXor: class hash
)
public isKindOfClassMirror ^<Boolean> = (
^true
)
public isMeta ^<Boolean> = (
^reflectee isMeta
)
public methods ^ <MirrorGroup[MethodMirror]> = (
^computeMirrorGroup: [:r | r methods]
)
public mixin ^ <MixinMirror> = (
^MixinMirror reflecting: reflectee mixin
)
public name = (
^reflectee name
)
public nestedClasses ^ <MirrorGroup[ClassDeclarationMirror]> = (
^computeMirrorGroup: [:r | r nestedClasses]
)
public simpleName = (
^(reflectee name splitBy: '`') last
)
public slots ^ <MirrorGroup[SlotMirror]> = (
^computeMirrorGroup: [:r | r slots].
)
public superclass ^ <ClassMirror> = (
reflectee superclass isNil ifTrue: [^nil].
^ClassMirror reflecting: reflectee superclass
)
public lazySlots ^<MirrorGroup[LazySlotMirror]> = (
^computeMirrorGroup: [:r <MixinMirror> | r lazySlots]
)
) : (
)
class IRBasedClassDeclarationBuilder fromIR: ir <IntermediateClassDeclaration> existingMixin: m <Mixin> within: enclosing <ClassDeclarationBuilder> = ClassDeclarationBuilder forExistingMixin: m within: enclosing (
|
prvtIR <IntermediateClassDeclaration> = ir.
|) (
computeClassSide ^ <MixinBuilder> = (
| existingClassMixin = nil = prvtExistingMixin ifFalse: [classMixinOf: prvtExistingMixin]. |
^MixinBuilder for: prvtIR classSide classDeclaration: self existingMixin: existingClassMixin.
)
computeInstanceSide ^ <MixinBuilder> = (
^MixinBuilder for: prvtIR instanceSide classDeclaration: self existingMixin: prvtIR instanceSide.
)
declarationData = (
^prvtIR
)
public header ^ <ClassHeaderMirror> = (
^SourceBasedClassHeaderMirror from: prvtIR headerSource
)
public name ^<Symbol> = (
^compilation names fullyQualifiedNameToSimple: qualifiedName
)
) : (
)
class MethodBuilder reflecting: mir in: mb source: s = (|
reflecteeX <IntermediateMethod> = mir.
definingMixinX <MixinBuilder> = mb.
public source <String> = s.
|) (
public accessModifier = (
^reflecteeX accessModifier
)
public definingMixin ^ <MixinBuilder> = (
^definingMixinX
)
public name ^<Symbol> = (
^reflecteeX name
)
public simpleName ^<Symbol> = (
^reflecteeX name
)
) : (
)
class MethodMirror name: n accessModifier: a mixin: m source: s = (|
public name <Symbol> = n.
public accessModifier <Symbol> = a.
public definingMixin <MixinMirror> = m.
private sourceIndex <Integer> = s.
|) (
public isKindOfMethodMirror ^<Boolean> = (
^true
)
public selectors ^ <List[Symbol]> = (
#BIOGUS yourself. (* need to construct an index when parsing *)
^List new
)
public simpleName ^<Symbol> = (
^name
)
public source ^<String> = (
^js propertyOf: (js ident: 'sources') at: sourceIndex.
)
public metadata ^ <Map[String, String]> = (
^parseMetadata metadata
)
parseMetadata ^ <MetadataParser> = (
|
src <String> = source copyFrom: 1 to: (source indexOf: '=' startingAt: 1) - 1.
metadataParser <MetadataParser> = metadataParsing MetadataParser onSource: src.
|
metadataParser gatherMetadataBackwards.
^metadataParser
)
) : (
)
public class MixinBasedClassDeclarationBuilder forExistingMixin: m <InstanceMixin> within: enclosing <ClassDeclarationBuilder> =
ClassDeclarationBuilder forExistingMixin: m within: enclosing (
| mixinMirror = MixinMirror reflecting: m. |
) (
computeClassSide ^ <MixinBuilder> = (
| existingClassMixin = classMixinOf: prvtExistingMixin. |
^MixinBuilder for: mixinMirror declaration classSide classDeclaration: self existingMixin: existingClassMixin.
)
computeInstanceSide ^ <MixinBuilder> = (
^MixinBuilder for: mixinMirror classDeclaration: self existingMixin: prvtExistingMixin.
)
declarationData ^ <ClassDeclarationMirror> = (
^mixinMirror declaration
)
public header ^ <ClassHeaderMirror> = (
^declarationData header
)
public name ^<Symbol> = (
^declarationData name
)
) : (
)
class MixinBuilder for: dataSource <IntermediateMixin | MixinMirror> classDeclaration: cdb <ClassDeclarationBuilder> existingMixin: em <Mixin> = (
(*
A mutable description of a mixin. Used by ClassDeclarationBuilder, which is the actual unit of installation.
Instances of this class are created from dataSource, which may be either the compiler's intermediate representation for mixins,
or a mixin mirror. In all cases, a builder, cdb, for the entire class must be supplied as a reference to the overall
declaration. If the mixin, em, that is being described already exists, it too is required - otherwise em should be nil.
The working representation for the builder consists of slots representing the methods, nested classes and slots of the mixin.
These are computed upon instantiation. Changing the mixin elsewhere will have no effect on this builder once it has
been instantiated!
Arguably, it might be cleaner to separate out MixinBuilders into different implementations just like ClassDeclarationBuilders.
In practice, the only point where a difference arises is in computing nestedClasses, the set of nested class builders.
Thus, it seems simpler to factor out that decision to the method #nestedClassBuildrFor:within:, which does a test
on the data to decide what action to take.
*)
|
data <IntermediateMixin | MixinMirror> = dataSource.
public declaration <ClassDeclarationBuilder> = cdb.
public slots <MutableMirrorGroup> = MutableMirrorGroup group: ((dataSource slots reject: [:ea | ea isSynthetic])) within: self.
public methods <MutableMethodGroup> = MutableMethodGroup group: ((dataSource methods reject: [:ea | ea isSynthetic]) collect: [:ea | MethodBuilder reflecting: ea in: self source: ea source]) within: self.
public lazySlots <LazyMutableSlotGroup> = LazyMutableSlotGroup group: List new within: self.
public nestedClasses <MutableNestedClassGroup> =
MutableNestedClassGroup group: (dataSource nestedClasses collect: [:ea <IntermediateClassDeclaration | ClassDeclarationMirror> |
nestedClassBuilderFor: ea within: em]) within: self.
|) (
public canUnderstand: selector <Symbol> ^ <Boolean> = (
^methods includesMirrorNamed: selector
)
find: name <String> in: aMixin <Mixin>^ <Mixin> = (
^Mixin fromRuntimeMixin:
(js propertyOf:
(js propertyOf: aMixin at: (js literal: 'runtimeMixin')) at: (compilation names manglePublic: name)).
)
public isKindOfMixinMirror ^<Boolean> = (
^true
)
public isMeta ^ <Boolean> = (
^data isMeta
)
nestedClassBuilderFor: nc <IntermediateClassDeclaration | ClassDeclarationMirror> within: em <Mixin> ^ <ClassDeclartionBuilder> = (
data isKindOfIntermediateMixin ifTrue: [
^IRBasedClassDeclarationBuilder fromIR: nc existingMixin: (find: nc qualifiedName in: em) within: declaration
].
^MixinBasedClassDeclarationBuilder forExistingMixin: (find: nc qualifiedName in: em) within: declaration
)
) : (
public reflecting: mxn <Mixin | ClassMixin> ^<MixinBuilder> = (
^mxn isMeta
ifTrue: [(ClassDeclarationBuilder reflecting: (MixinMirror reflecting: mxn) declaration instanceSide reflectee) classSide]
ifFalse: [(ClassDeclarationBuilder reflecting: mxn) instanceSide]
)
)
class MixinMirror reflecting: mixin <Mixin> = (|
private reflectee <Mixin> = mixin.
|) (
public apply: newSuperclass <Class> withName: n <Symbol> ^ <ClassMirror> = (
^ClassMirror reflecting: (reflectee applyTo: newSuperclass withName: n)
)
public asBuilder ^<MixinBuilder> = (
^isMeta
ifTrue: [declaration asBuilder classSide]
ifFalse: [declaration asBuilder instanceSide]
)
public canUnderstand: selector <Symbol> ^ <Boolean> = (
^methods includesMirrorNamed: selector
)
public declaration ^<ClassDeclarationMirror> = (
^ClassDeclarationMirror
reflecting: (isMeta
ifFalse: [reflectee]
ifTrue: [(Mixin fromRuntimeMixin: (js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'nonMeta')))])
)
public isKindOfMixinMirror ^<Boolean> = (
^true
)
public isMeta ^<Boolean> = (
^reflectee isMeta
)
public methods ^<MirrorGroup[MethodMirror]> = (
| raw <Array[MethodMirror]> |
raw:: ((js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'methods'))
select: [:each | (js propertyOf: each at: (js literal: 'isSynthetic')) not])
collect: [:each | MethodMirror
name: (js propertyOf: each at: (js literal: 'name'))
accessModifier: (js propertyOf: each at: (js literal: 'accessModifier'))
mixin: self
source: (js propertyOf: each at: (js literal: 'source'))].
^ImmutableMirrorGroup group: raw
)
public name = (
^(reflectee name splitBy: '`') last
)
public nestedClasses ^<MirrorGroup[ClassDeclarationMirror]> = (
| raw <Array[ClassDeclarationMirror]> |
raw:: (js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'nestedClasses'))
collect: [:each | ClassDeclarationMirror reflecting:
(Mixin fromRuntimeMixin:
(js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (vmmirror mangleSelector: each)))].
^ImmutableMirrorGroup group: raw
)
public primaryFactorySelector ^ <Symbol> = (
^declaration primaryFactorySelector
)
public slots ^<MirrorGroup[SlotDeclarationMirror]> = (
| raw <Array[SlotDeclarationMirror]> |
isMeta ifTrue: [^ImmutableMirrorGroup group: {}].
raw:: ((js propertyOf: (js propertyOf: reflectee at: (js literal: 'runtimeMixin')) at: (js literal: 'slots'))
select: [:each | (js propertyOf: each at: (js literal: 'isSynthetic')) not])
collect: [:each | SlotDeclarationMirror
name: (js propertyOf: each at: (js literal: 'name'))
accessModifier: (js propertyOf: each at: (js literal: 'accessModifier'))
isMutable: (js propertyOf: each at: (js literal: 'isMutable'))
mixin: self].
^ImmutableMirrorGroup group: raw
)
public lazySlots = (
| results = List new. |
(* (* Go thru raw slots *)
mixin _slots do:
[:slotDescriptor | | slotName = slotDescriptor at: 1. i = slotName indexOf: '`cacheSlot'. |
(* select slots whose name ends with `cachedSlot *)
(i ~= 0 and: [slotName size - 9 = i]) ifTrue: [
(* and create a mirror for them *)
results add: (LazySlotMirror reflecting: (slotName copyFrom: 1 to: i -1) in: self)
]
].*)
^ImmutableMirrorGroup group: results
)
) : (
)
class MutableMethodGroup group: ms <List[Mirror]>
within: mb <MixinBuilder>
= MutableMirrorGroup group: ms within: mb () (
public addFromSource: s <String> ^ <MethodBuilder> = (
|
result <IntermediateMethod>
newM <MethodBuilder>
sourceIndex <Integer>
|
result:: compiler
compileMethodSource: s
within: definingMixin.
(* TODO: check for name conflicts *)
newM:: MethodBuilder reflecting: result in: definingMixin source: s.
removeMirrorNamed: result name.
(*mixinIR methods add: result.*)
addMirror: newM.
^newM
)
public removeMirrorNamed: m <Symbol | String>^ <Mirror> = (
| oldMirror <MethodMirror> |
oldMirror:: super removeMirrorNamed: m.
oldMirror isNil
ifFalse: [ (* we are removing an existing method *)
(*mixinIR methods removeAllSuchThat: [:ea <IntermediateMethod> | ea name = m].*)
].
^oldMirror
)
) : (
)
class MutableMirrorGroup group: mirrors <List[Mirror]> within: mb <MixinBuilder> = ImmutableMirrorGroup group: mirrors (
|
protected definingMixin <MixinBuilder> = mb.
|
) (
addMirror: m <Mirror> ^ <Mirror> = (
mirrors keysAndValuesDo: [:index <Integer> :mirror <Mirror> |
mirror simpleName = m simpleName ifTrue: [^mirrors at: index put: m]].
^mirrors addLast: m
)
public removeMirrorNamed: m <Symbol | String>^ <Mirror> = (
mirrors keysAndValuesDo: [:index :mirror |
mirror simpleName = m ifTrue: [^mirrors remove: mirror]].
^nil
)
) : (
)
class MutableNestedClassGroup group: ms within: mb = MutableMirrorGroup group: ms within: mb (
) (
) : (
)
public class ObjectMirror reflecting: r = (|
public reflectee = r.
|) (
public = other <Object> ^<Boolean> = (
other isKindOfObjectMirror ifFalse: [^false].
^js operator: '===' with: reflectee and: other reflectee
)
private compileExpression: expression <String> with: rawScope <Map[Symbol, Object]> ^<JS[Function]> = (
|
compiler
ir
dnuHandlers
writer
sb
src
func
|
compiler:: compilation Compiler new.
ir:: compiler
compileExpressionSource: expression
inMixin: getClass mixin
withScope: rawScope.
dnuHandlers:: compiler dnuHandlers.
writer:: generation Writer new.
sb:: StringBuilder new.
writer generateSourceFor: dnuHandlers on: sb.
src:: sb asString.
func:: js call: (js ident: 'eval') with: {'(',src, ')'}.
js call: func with: {js verbatim: 'Object.prototype'}.
writer:: generation Writer new.
sb:: StringBuilder new.
writer generateSourceFor: ir function on: sb.
src:: sb asString.
src out.
^js call: (js ident: 'eval') with: {'(',src, ')'}
)
public evaluate: expression <String> ^<ThreadMirror> = (
^self evaluate: expression with: Map new
)
public evaluate: expression <String> with: scope <Map[Symbol, ObjectMirror]> ^<ThreadMirror> = (
| rawScope func state result |
rawScope:: Map new.
scope keysAndValuesDo: [:key :value | rawScope at: key put: value reflectee].
func:: compileExpression: expression with: scope.
[result:: js call: (js propertyOf: func at: (js literal: 'call')) with: {reflectee. rawScope}.
state:: #fulfilled]
on: Error do: [:e | result:: e. state:: #broken].
^ThreadMirror
state: state
result: (ObjectMirror reflecting: result)
)
public getClass = (
#BOGUS. (* Wrong for bilingual objects. *)
^ClassMirror reflecting:
(js propertyOf: reflectee at: (js literal: 'newspeakClass')).
)
public getSlot: selector = (
|
mangledName = vmmirror mangleSelector: selector, '`slot'.
raw = js propertyOf: reflectee at: mangledName.
|
(js operator: '===' with: (js ident: 'undefined') and: raw) ifTrue: [halt].
^ObjectMirror reflecting: raw
)
public hash ^<Integer> = (
^(identityHashOf: reflectee) bitXor: class hash
)
public isKindOfObjectMirror ^<Boolean> = (
^true
)
public setSlot: selector to: value = (
|
mangledName = vmmirror mangleSelector: selector, '`slot'.
raw = js propertyOf: reflectee at: mangledName.
|
(js operator: '===' with: (js ident: 'undefined') and: raw) ifTrue: [^halt].
js assign: (js propertyOf: reflectee at: mangledName) toBe: value.
)
) : (
)
class SlotDeclarationMirror name: n accessModifier: a isMutable: m mixin: mxn = (|
public name <Symbol> = n.
public accessModifier <Symbol> = a.
public isMutable <Boolean> = m.
public definingMixin <MixinMirror> = mxn.
|) (
public isKindOfSlotDeclarationMirror ^<Boolean> = (
^true
)
) : (
)
class SourceBasedClassHeaderMirror from: src <String> = AbstractClassHeaderMirror (
|
public source = src.
|
) (
) : (
)
class ThreadMirror state: s result: r = (
|
protected state = s.
public result <ObjectMirror | nil> = r.
|
) (
public isBroken ^<Boolean> = (
^state = #broken
)
public isFulfilled ^<Boolean> = (
^state = #fulfilled
)
public isKindOfThreadMirror ^ <Boolean> = (
^true
)
public isSuspended ^<Boolean> = (
^state = #suspended
)
) : (
)
public class ClosureMirror reflecting: c <Closure> = (
(* Non-functional; we need changes to the compilation to support reflection on closures.
Here as a placeholder, and for API compatibility with WASM implementation.
*)
|
public reflectee <Closure> = c.
|
) (
) : (
)
public class LazySlotMirror reflecting: m <Symbol> in: em <MixinMirror> = (
(* The mirror for a particular lazy slot. Every lazy slot consist of an underlying storage location (the cache) and 2-3 methods:
a. The getter, which tests if the slot is nil, and if so, calls the init method (see (b) below) to compute the value and set the underlying storage. In any case it returns the value stored in the slot. This method is sometimes referred to as the the main method of the lazy slot.
b. The init method, in charge of doing the actual computation.
c. If the slot is mutable, there will be a third method, which is an ordinary setter.
*)
|
reflectee <Symbol> = m.
enclosingMixinMirror <MixinMirror> = em.
public mainMethod <MethodMirror> = MethodMirror reflecting: (mixin _methods select: [:mtd | mtd selector = m]) first.
|
) (
) : (
)
class LazyMutableSlotGroup group: ms <List[Mirror]>
within: mb <MixinBuilder>
= MutableMirrorGroup group: ms within: mb () (
) : (
)
classDeclIRFor: mixin within: enclosing = (
| source = 'Newspeak3 ''Uncategorized'' ', (ClassDeclarationMirror reflecting: mixin) source. |
(*('compiling class:', source) out.*)
^compiler compileClassSource: source within: enclosing
)
classMixinOf: m <InstanceMixin> ^ <ClassMixin> = (