-
Notifications
You must be signed in to change notification settings - Fork 11
/
ActorsForJS.ns
779 lines (744 loc) · 24.2 KB
/
ActorsForJS.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
Newspeak3
'Actors'
class ActorsForJS usingPlatform: p = (|
private WeakMap = p collections IdentityMap.
private List = p collections List.
private Message = p kernel Message.
private internalRefs <WeakMap[FarReference, InternalFarReference]> = WeakMap new.
public Promise = PromiseUtils new.
defaultActor = DOMActor named: 'Default actor'.
|js assign: (js ident: 'currentActor') toBe: defaultActor) (
public class Actor named: debugName <String> = (|
private (* N.B. Do not leak to subinstances. *) internalActor = DOMActor named: debugName.
|) (
public interrupt = (
unimplemented
)
public onError: onError <[:E]> = (
unimplemented.
)
public seed: value <Value> ^<FarReference[Value]> = (
(* Creating a far reference to a PassByCopy is questionable. It may be that this should be seed: receiver perform: selector with: argument. *)
(isValueObject: value) ifFalse: [Error signal: 'Cannot seed an actor with a non-Value.'].
^createFarReferenceTo: value in: internalActor for: currentActor.
)
public terminate = (
unimplemented
)
) : (
)
class DOMActor named: n = InternalActor (
(* An actor whose turns run in the DOM Context. *)
|
protected name <String> = n.
|) (
public enqueueMessage: actorMessage = (
actorMessage postedTo: self.
(* bogus - should drain an explicit queue to allow for a randomizing scheduler *)
js
call: (js propertyOf: (js ident: 'theGlobalObject') at: (js literal: 'setTimeout'))
with: {[js assign: (js ident: 'currentActor') toBe: self.
actorMessage deliverIn: self]. js literal: 0}.
)
) : (
)
protected class EventualSendReactor selector: s arguments: a resolver: r = (
(* An eventual-send to a promise.
Note that all slots contain objects that belong to the actor which performed the eventual send. *)
|
protected selector <Symbol> = s.
protected arguments <Array> = a.
protected resolver <InternalResolver> = r.
|) (
public reactBroken: error = (
(* Broken promise contagion. *)
resolver __owner == currentActor ifFalse: [halt].
(* Should this be immediate? *)
(* resolver internalBreak: error. ? *)
currentActor enqueueMessage:
(PendingDelivery
receiver: resolver
selector: #internalResolve:
arguments: {InternalBrokenRef problem: error __owner: resolver __owner}
resolver: nil)
)
public reactMoreResolved: internalRef = (
resolver internalResolve: (asInternalRef: (internalRef eventualSend: selector arguments: arguments)).
)
public reactResolved: value = (
resolver __owner == currentActor ifFalse: [halt].
resolver internalResolve:
(asInternalRef: (currentActor
eventualSendTo: value
selector: selector
arguments: arguments))
)
) : (
)
class InternalActor = () (
public currentActor = (
(* BOGUS. Polymorphic with other actor system. *)
^self
)
public eventualSendTo: receiver selector: selector arguments: arguments ^<Ref> = (
(* Sent from the compilation of an eventual send, via the same method in ProcessScheduler. *)
^(asInternalRef: receiver) eventualSend: selector arguments: arguments
)
) : (
)
class InternalBrokenRef problem: p __owner: o = InternalRef __owner: o (|
protected problem = p.
|) (
public addReactor: reactor = (
reactor reactBroken: problem.
)
public checkCycle ^<Boolean> = (
^false
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
^self externalRef (* I.e., broken with same problem. Should this wait for the next turn? *)
)
public immediateSend: message <Message> ^<Object> = (
^Error signal: 'Cannot immediate-send to a broken ref: ', message selector
)
public printOn: stream = (
stream nextPutAll: 'InternalBrokenRef: '.
problem printOn: stream.
)
public refPrintOn: stream = (
stream nextPutAll: '<broken ref: '.
problem printOn: stream.
stream nextPutAll: '>'.
)
public shorten ^<InternalRef> = (
^self
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
| localPromise = InternalLocalPromise __owner: __owner. |
currentActor enqueueMessage:
(PendingDelivery
receiver: onError
selector: #cull:
arguments: {problem}
resolver: localPromise).
^localPromise externalRef
)
public wrapFor: targetActor = (
self checkCurrentActor.
#BOGUS.
^(InternalBrokenRef problem: problem __owner: targetActor) externalRef
)
) : (
)
class InternalFarReference target: t targetsActor: ta __owner: o = InternalRef __owner: o (|
protected target = t.
protected targetsActor = ta.
|) (
public addReactor: reactor = (
reactor reactResolved: externalRef
)
public checkCycle ^<Boolean> = (
^false
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
| resolverInTarget remotePromise redirector |
resolverInTarget:: InternalLocalPromise __owner: targetsActor.
targetsActor enqueueMessage:
(PendingDelivery
receiver: target
selector: selector
arguments: (wrapArguments: arguments from: currentActor to: targetsActor)
resolver: resolverInTarget).
remotePromise:: InternalRemotePromise
target: resolverInTarget
targetsActor: targetsActor
__owner: currentActor.
redirector:: RedirectReactor
from: targetsActor
to: currentActor
resolver: remotePromise.
targetsActor enqueueMessage:
(PendingDelivery
receiver: resolverInTarget
selector: #addReactor:
arguments: {redirector}
resolver: nil).
^remotePromise externalRef
)
public immediateSend: message <Message> ^<Object> = (
^Error signal: 'Cannot immediate-send to an eventual ref: ', message selector
)
public printOn: stream = (
stream nextPutAll: 'InternalFarRef: '.
target printOn: stream.
)
public refPrintOn: stream = (
stream nextPutAll: '<far ref: '.
target printOn: stream.
stream nextPutAll: '>'.
)
public shorten ^<InternalRef> = (
^self
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
| localPromise = InternalLocalPromise __owner: __owner. |
currentActor enqueueMessage:
(PendingDelivery
receiver: onValue
selector: #cull:
arguments: {externalRef}
resolver: localPromise).
^localPromise externalRef
)
public wrapFor: targetActor = (
targetsActor == targetActor ifTrue: [^target].
^(InternalFarReference target: target targetsActor: targetsActor __owner: targetActor) externalRef.
)
) : (
)
class InternalLocalPromise __owner: o = InternalRef __owner: o (|
protected state ::= #unresolved. (* resolving. resolved *)
protected resolution <InternalRef>
protected reactors
|) (
public addReactor: reactor = (
state == #unresolved ifTrue:
[reactors == nil ifTrue: [reactors:: List new].
reactors add: reactor.
^self].
state == #resolved ifTrue:
[^resolution addReactor: reactor].
halt.
)
public checkCycle ^<Boolean> = (
state == #resolving ifTrue: [^true].
state == #unresolved ifTrue: [^false].
state == #resolved ifTrue: [^resolution checkCycle].
halt.
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
| localPromise |
self checkCurrentActor.
state == #resolved ifTrue: [^resolution eventualSend: selector arguments: arguments].
localPromise:: InternalLocalPromise __owner: __owner.
self addReactor: (EventualSendReactor selector: selector arguments: arguments resolver: localPromise).
^localPromise externalRef
)
public immediateSend: message <Message> ^<Object> = (
state == #resolved ifTrue: [^resolution immediateSend: message].
^Error signal: 'Cannot immediate-send to an eventual ref: ', message selector
)
public internalResolve: internalRef <InternalRef> = (
state == #unresolved ifFalse: [^Error signal: 'Already resolved'].
internalRef isInternalRef ifFalse: [halt].
state:: #resolving.
internalRef checkCycle
ifTrue: [resolution:: InternalBrokenRef problem: (Error new messageText: 'Cyclic resolution') __owner: __owner]
ifFalse: [resolution:: internalRef].
state:: #resolved.
nil == reactors ifFalse:
[reactors do: [:reactor | resolution addReactor: reactor].
reactors:: nil].
)
public printOn: stream = (
stream nextPutAll: 'InternalLocalPromise: '.
state printOn: stream.
)
public refPrintOn: stream = (
state == #resolved
ifTrue: [resolution refPrintOn: stream]
ifFalse: [stream nextPutAll: '<local promise>'].
)
public shorten ^<InternalRef> = (
state == #unresolved ifTrue: [^self].
state == #resolved ifTrue: [^resolution].
halt.
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
| localPromise |
self checkCurrentActor.
state == #resolved ifTrue: [^resolution whenFulfilled: onValue whenBroken: onError].
localPromise:: InternalLocalPromise __owner: __owner.
self addReactor: (WhenReactor onValue: onValue onError: onError resolver: localPromise).
^localPromise externalRef
)
public wrapFor: targetActor = (
state == #resolved ifTrue: [^resolution wrapFor: targetActor].
checkCurrentActor.
targetActor == __owner ifTrue: [halt].
^(InternalRemotePromise target: self targetsActor: currentActor __owner: targetActor) externalRef
)
) : (
)
class InternalNearRef wrapping: t __owner: o = InternalRef __owner: o (|
protected target = t.
|) (
public addReactor: reactor = (
reactor reactResolved: target.
)
public checkCycle ^<Boolean> = (
^false
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
| localPromise = InternalLocalPromise __owner: __owner. |
currentActor enqueueMessage:
(PendingDelivery
receiver: target
selector: selector
arguments: arguments
resolver: localPromise).
^localPromise externalRef
)
public immediateSend: message <Message> ^<Object> = (
^message sendTo: target
)
public printOn: stream = (
stream nextPutAll: 'InternalNearRef: '.
target printOn: stream.
)
public refPrintOn: stream = (
stream nextPutAll: '<near ref: '.
target printOn: stream.
stream nextPutAll: '>'.
)
public shorten ^<InternalRef> = (
^self
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
| localPromise = InternalLocalPromise __owner: __owner. |
currentActor enqueueMessage:
(PendingDelivery
receiver: onValue
selector: #cull:
arguments: {target}
resolver: localPromise).
^localPromise externalRef
)
public wrapFor: targetActor = (
self checkCurrentActor.
(isValueObject: target) ifTrue: [^target].
^createFarReferenceTo: target in: currentActor for: targetActor
)
) : (
)
class InternalRef __owner: o = (|
public __owner <InternalActor> = o.
|) (
protected checkCurrentActor = (
currentActor == __owner ifFalse: [halt].
)
public checkCycle ^<Boolean> = (
subclassResponsibility
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
subclassResponsibility
)
public externalRef = (
^Ref wrapping: self
)
public immediateSend: message <Message> ^<Object> = (
subclassResponsibility
)
public isInternalRef ^<Boolean> = (
^true
)
public refPrintOn: stream = (
subclassResponsibility
)
public shorten ^<InternalRef> = (
subclassResponsibility
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
subclassResponsibility
)
public wrapFor: targetActor = (
subclassResponsibility
)
) : (
)
class InternalRemotePromise target: t targetsActor: ta __owner: o = InternalRef __owner: o (|
target <InternalLocalPromise> = t.
targetsActor <InternalActor> = ta.
state ::= #unresolved.
resolution <InternalRef>
|) (
public addReactor: reactor = (
state == #resolved ifTrue: [^resolution addReactor: reactor].
state == #unresolved ifTrue: [^reactor reactMoreResolved: self].
stop5.
)
public checkCycle ^<Boolean> = (
state == #resolving ifTrue: [^true].
state == #unresolved ifTrue: [^false].
state == #resolved ifTrue: [^resolution checkCycle].
halt.
)
public eventualSend: selector <Symbol> arguments: arguments <Array> ^<Ref> = (
| resolverInTarget remotePromise redirector |
resolverInTarget:: InternalLocalPromise __owner: targetsActor.
targetsActor enqueueMessage:
(PendingDelivery
receiver: target
selector: #eventualSend:arguments:
arguments: {selector. (wrapArguments: arguments from: currentActor to: targetsActor)}
resolver: resolverInTarget).
remotePromise:: InternalRemotePromise
target: resolverInTarget
targetsActor: targetsActor
__owner: currentActor.
redirector:: RedirectReactor
from: targetsActor
to: currentActor
resolver: remotePromise.
targetsActor enqueueMessage:
(PendingDelivery
receiver: resolverInTarget
selector: #addReactor:
arguments: {redirector}
resolver: nil).
^remotePromise externalRef
)
public immediateSend: message <Message> ^<Object> = (
state == #resolved ifTrue: [^resolution immediateSend: message].
^Error signal: 'Cannot immediate-send to an eventual ref: ', message selector
)
public internalResolve: internalRef <InternalRef> = (
state == #unresolved ifFalse: [^Error signal: 'Already resolved'].
internalRef isInternalRef ifFalse: [halt].
state:: #resolving.
internalRef checkCycle
ifTrue: [resolution:: InternalBrokenRef problem: (Error new messageText: 'Cyclic resolution') __owner: __owner]
ifFalse: [resolution:: internalRef].
state:: #resolved.
)
public printOn: stream = (
stream nextPutAll: 'InternalRemotePromise: '.
state printOn: stream.
)
public redirectFrom: sourceActor to: destinationActor resolving: resolver = (
| redirector |
destinationActor == targetsActor ifTrue:
[targetsActor enqueueMessage:
(PendingDelivery
receiver: resolver
selector: #internalResolve:
arguments: {target}
resolver: nil).
^self].
redirector:: RedirectReactor
from: targetsActor
to: destinationActor
resolver: resolver.
targetsActor enqueueMessage:
(PendingDelivery
receiver: target
selector: #addReactor:
arguments: {redirector}
resolver: nil).
)
public refPrintOn: stream = (
state == #resolved
ifTrue: [resolution refPrintOn: stream]
ifFalse: [stream nextPutAll: '<remote promise>'].
)
public shorten ^<InternalRef> = (
state == #unresolved ifTrue: [^self].
state == #resolved ifTrue: [^resolution].
halt.
)
public whenFulfilled: onValue whenBroken: onError ^<Ref> = (
| localPromise redirector |
self checkCurrentActor.
state == #resolved ifTrue: [^resolution whenFulfilled: onValue whenBroken: onError].
localPromise:: InternalLocalPromise __owner: __owner.
redirector:: RedirectReactor
from: targetsActor
to: currentActor
resolver: localPromise.
targetsActor enqueueMessage:
(PendingDelivery
receiver: target
selector: #addReactor:
arguments: {redirector}
resolver: nil).
^localPromise whenFulfilled: onValue whenBroken: onError.
)
public wrapFor: targetActor = (
state == #resolved ifTrue: [^resolution wrapFor: targetActor].
targetActor == targetsActor ifTrue: [^target externalRef].
^(InternalRemotePromise target: target targetsActor: targetsActor __owner: targetActor) externalRef
)
) : (
)
protected class PendingDelivery receiver: r selector: s arguments: a resolver: y = (
(* A message in an actor's event queue. Every turn starts by sending #deliverIn:.
Note that all slots contain objects that belong to the actor in which the message is queued, even the resolver. *)
|
protected receiver <Object> = r.
protected selector <Symbol> = s.
protected arguments <Array> = a.
protected resolver <InternalResolver> = y.
|) (
public deliverIn: fastCurrentActor = (
| resolution type msg |
(* Transcript print: fastCurrentActor; space; print: self; cr. *)
msg:: Message selector: selector arguments: arguments.
[resolution:: msg sendTo: receiver.
type:: #internalResolve:]
on: Error (* BOGUS: Exception? *)
do: [:error |
error out.
resolver == nil ifTrue: [error pass].
resolution:: error.
type:: #internalBreak:].
(* Transcript show: {resolver. type. resolution}; cr. *)
nil == resolver ifTrue: [
type == #internalBreak: ifTrue: [{'silent failure'. resolution} out].
^self].
resolver internalResolve: (type == #internalResolve:
ifTrue: [asInternalRef: resolution]
ifFalse: [InternalBrokenRef problem: resolution __owner: self]).
)
public postedTo: actor = (
(*check: receiver allowedIn: actor.
arguments do: [:arg | check: arg allowedIn: actor].*)
(* selector numArgs == arguments size ifFalse: [halt]. *)
resolver == nil ifFalse: [resolver __owner == actor ifFalse: [halt]].
)
public printOn: stream = (
receiver printOn: stream.
stream nextPutAll: ' <-: '.
stream nextPutAll: selector.
stream space.
arguments isEmpty ifFalse: [arguments printOn: stream].
)
) : (
)
class PromiseUtils = () (
public all: refs <List[Promise[V, E]]> ^<Promise[List[V], E]> = (
(* Answers a promise that resolves with all the resolutions of refs, or the first problem. *)
)
public broken: problem <E> ^<Promise[nil, E]> = (
^Resolver new break: problem; promise
)
public fulfilled: resolution <V> ^<Promise[V, nil]> = (
^Resolver new fulfill: resolution; promise
)
public timeout: ref <Promise[V, E]> after: milliseconds <Integer> ^<Promise[V, E | TimeoutError]> = (
(* Answers a promise that resolves with the resolution of ref, or a TimeoutError if refs is not resolved within milliseconds. *)
#BOGUS. (* Should have proper duration type. *)
)
public when: ref <Promise[V, E]> fulfilled: onValue <[:V | V2 def]> ^<Promise[V2, E]> = (
(* Subscribe to notification of a reference being fulfilled. If ref is fulfilled, onValue will run in some turn after the turn that resolves ref, and the returned promise with be resolved with the result of onValue. If ref is broken, the returned promise with be broken with the same error. *)
^(asInternalRef: ref) whenFulfilled: onValue whenBroken: [:e | e signal] (* Should we make this the behavior of a nil catch-clause instead? *)
)
public when: ref <Promise[V, E]> fulfilled: onValue <[:V | V2 def]> broken: onError <[:E | E2 def]> ^<Promise[V2, E2]> = (
(* Subscribe to notification of a reference being fulfilled or broken. onValue or onError respectively will run in some turn after the turn that resolves ref., and the returned promise with be resolved with the result of the callback. *)
^(asInternalRef: ref) whenFulfilled: onValue whenBroken: onError
)
public when: ref <Promise> partitioned: onPartition <[:Promise | R def] | [R def]> ^<Promise[R, nil]> = (
(* Subscribe to notification of a reference breaking due to partition. *)
)
) : (
)
protected class RedirectReactor from: s to: d resolver: r = (
(* A bridge between InternalResolvers in different actors. *)
|
protected sourceActor <Actor> = s.
protected destinationActor <Actor> = d.
protected resolver <InternalResolver> = r.
|) (
public reactBroken: r = (
| msg |
currentActor == sourceActor ifFalse: [halt].
msg:: PendingDelivery
receiver: resolver
selector: #internalResolve:
arguments: {InternalBrokenRef problem: r __owner: destinationActor}
resolver: nil.
destinationActor enqueueMessage: msg.
)
public reactMoreResolved: internalRef = (
internalRef redirectFrom: sourceActor to: destinationActor resolving: resolver.
)
public reactResolved: r = (
| msg |
currentActor == sourceActor ifFalse: [halt].
msg:: PendingDelivery
receiver: resolver
selector: #internalResolve:
arguments: {asInternalRef: (wrapArgument: r from: sourceActor to: destinationActor)}
resolver: nil.
destinationActor enqueueMessage: msg.
)
) : (
)
class Ref wrapping: ref = (|
protected internalRef = ref.
|internalRefs at: self put: ref) (
protected doesNotUnderstand: message <Message> = (
(* For the debugger/inspector. Questionable. *)
(* Should probably override public method of Object as well. *)
0 = message arguments size ifTrue: [(message selector startsWith: 'is') ifTrue: [^false]].
^internalRef immediateSend: message
)
public identityHash = (
(* :bogus: Need a proper implementation of WeakMap that does not send messages to its keys. *)
^super hash
)
public isPromise = (
^true
)
public printOn: stream = (
(* BOGUS - debugging *)
internalRef refPrintOn: stream.
)
protected yourself = (
^super yourself
)
) : (
)
public class Resolver = (
(*A Resolver represents the authority to resolve a Promise and is the control facet of the promise's eventual resolution.*)
|
private (* N.B. We must not leak access to an InternalResolver via subobjects. *) internalResolver = InternalLocalPromise __owner: currentActor.
public promise <Promise[V, E]> = internalResolver externalRef.
|) (
public break: error <E> = (
internalResolver internalResolve: (InternalBrokenRef problem: error __owner: currentActor).
)
public fulfill: value <V> = (
internalResolver internalResolve: (asInternalRef: value).
)
public isResolver ^<Boolean> = (
^true
)
) : (
)
class Scheduler = () (
public eventualSendTo: r selector: s arguments: a = (
)
) : (
)
public class Timer __duration: milliseconds callback: callback repeating: repeating = (|
protected cancelled ::= false.
|repeating
ifTrue: [self schedule: callback every: milliseconds]
ifFalse: [self schedule: callback after: milliseconds]) (
public cancel = (
cancelled:: true.
nil == process ifFalse: [process terminate. process:: nil].
)
public isActive ^<Boolean> = (
cancelled ifTrue: [^false].
^nil ~= process
)
public isTimer ^<Boolean> = (
^true
)
protected schedule: callback after: ms = (
js call: (js propertyOf: (js ident: 'window') at: (js literal: 'setTimeout'))
with: {callback. ms}
)
protected schedule: callback every: ms = (
js call: (js propertyOf: (js ident: 'window') at: (js literal: 'setInterval'))
with: {callback. ms}
)
) : (
public after: duration do: callback <[]> ^<Timer> = (
^self __duration: duration callback: callback repeating: false
)
public every: duration do: callback <[:Timer]> ^<Timer> = (
^self __duration: duration callback: callback repeating: true
)
)
protected class WhenReactor onValue: v onError: e resolver: r = (
(* A when-catch for a promise.
Note that all slots contain objects that belong to the actor which sent #whenResolved:. *)
|
protected onValue <[:V] | []> = v.
protected onError <[:E] | []> = e.
protected resolver <InternalResolver> = r.
|) (
public reactBroken: value = (
resolver __owner == currentActor ifFalse: [halt].
currentActor enqueueMessage:
(PendingDelivery
receiver: onError
selector: #cull:
arguments: {value}
resolver: resolver).
)
public reactMoreResolved: internalRef = (
resolver internalResolve: (asInternalRef: (internalRef whenFulfilled: onValue whenBroken: onError)).
)
public reactResolved: value = (
resolver __owner == currentActor ifFalse: [halt].
currentActor enqueueMessage:
(PendingDelivery
receiver: onValue
selector: #cull:
arguments: {value}
resolver: resolver).
)
) : (
)
private asInternalRef: externalRefOrUnboxedNearRef = (
^(isRef: externalRefOrUnboxedNearRef)
ifTrue: [internalRefs at: externalRefOrUnboxedNearRef ifAbsent: [halt]]
ifFalse: [InternalNearRef wrapping: externalRefOrUnboxedNearRef __owner: currentActor]
)
private check: object allowedIn: actor = (
assert: [nil ~= actor] message: 'Invalid actor'.
(isRef: object) ifTrue: [object __owner == actor ifFalse: [halt]].
(* Otherwise near reference *)
)
private createFarReferenceTo: target in: targetActor for: sourceActor = (
^(InternalFarReference target: target targetsActor: targetActor __owner: sourceActor) externalRef.
)
private createRemotePromiseOf: resolverInTarget in: targetActor for: sourceActor = (
| sourceResolver redirector message remotePromise |
sourceActor == targetActor ifTrue: [halt].
sourceResolver:: InternalLocalPromise __owner: sourceActor.
sourceResolver state: #remote.
sourceResolver _targetPromise: resolverInTarget.
sourceResolver _targetActor: targetActor.
redirector:: RedirectReactor
from: targetActor
to: sourceActor
resolver: sourceResolver.
message:: PendingDelivery
receiver: resolverInTarget
selector: #addReactor:
arguments: {redirector}
resolver: nil.
targetActor enqueueMessage: message.
^Ref wrapping: sourceResolver __owner: sourceActor.
)
private currentActor = (
^js ident: 'currentActor'
)
private isRef: object <Object> ^<Boolean> = (
^Ref == (js propertyOf: object at: (js literal: 'newspeakClass'))
)
private isValueObject: object <Object> ^<Boolean> = (
nil == object ifTrue: [^true].
true == object ifTrue: [^true].
false == object ifTrue: [^true].
object isKindOfString ifTrue: [^true].
object isKindOfNumber ifTrue: [^true].
object isKindOfCharacter ifTrue: [^true].
object isKindOfBehavior ifTrue: [^isValueObject: object enclosingClass].
Promise == object ifTrue: [^true].
^false
)
private wrapArgument: argument from: sourceActor to: targetActor = (
(* [argument] lives in [sourceActor], answer the corresponding proxy that lives in [targetActor] *)
sourceActor == targetActor ifTrue: [halt. (* We can't tell which side a near reference belongs to here, so we must have decided this earlier. *) ].
^(asInternalRef: argument) wrapFor: targetActor
)
private wrapArguments: arguments from: sourceActor to: targetActor = (
^arguments collect: [:argument | wrapArgument: argument from: sourceActor to: targetActor].
)
) : (
)