forked from athanclark/purescript-subtlecrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.browser.js
2006 lines (1886 loc) · 73.8 KB
/
test.browser.js
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
// Generated by purs bundle 0.13.6
var PS = {};
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Apply"] = $PS["Control.Apply"] || {};
var exports = $PS["Control.Apply"];
var Apply = function (Functor0, apply) {
this.Functor0 = Functor0;
this.apply = apply;
};
var apply = function (dict) {
return dict.apply;
};
exports["Apply"] = Apply;
exports["apply"] = apply;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Applicative"] = $PS["Control.Applicative"] || {};
var exports = $PS["Control.Applicative"];
var Control_Apply = $PS["Control.Apply"];
var Applicative = function (Apply0, pure) {
this.Apply0 = Apply0;
this.pure = pure;
};
var pure = function (dict) {
return dict.pure;
};
var liftA1 = function (dictApplicative) {
return function (f) {
return function (a) {
return Control_Apply.apply(dictApplicative.Apply0())(pure(dictApplicative)(f))(a);
};
};
};
exports["Applicative"] = Applicative;
exports["pure"] = pure;
exports["liftA1"] = liftA1;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Function"] = $PS["Data.Function"] || {};
var exports = $PS["Data.Function"];
var flip = function (f) {
return function (b) {
return function (a) {
return f(a)(b);
};
};
};
var $$const = function (a) {
return function (v) {
return a;
};
};
exports["flip"] = flip;
exports["const"] = $$const;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Bind"] = $PS["Control.Bind"] || {};
var exports = $PS["Control.Bind"];
var Data_Function = $PS["Data.Function"];
var Discard = function (discard) {
this.discard = discard;
};
var Bind = function (Apply0, bind) {
this.Apply0 = Apply0;
this.bind = bind;
};
var discard = function (dict) {
return dict.discard;
};
var bind = function (dict) {
return dict.bind;
};
var bindFlipped = function (dictBind) {
return Data_Function.flip(bind(dictBind));
};
var discardUnit = new Discard(function (dictBind) {
return bind(dictBind);
});
exports["Bind"] = Bind;
exports["bind"] = bind;
exports["bindFlipped"] = bindFlipped;
exports["discard"] = discard;
exports["discardUnit"] = discardUnit;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Monad"] = $PS["Control.Monad"] || {};
var exports = $PS["Control.Monad"];
var Control_Applicative = $PS["Control.Applicative"];
var Control_Bind = $PS["Control.Bind"];
var Monad = function (Applicative0, Bind1) {
this.Applicative0 = Applicative0;
this.Bind1 = Bind1;
};
var ap = function (dictMonad) {
return function (f) {
return function (a) {
return Control_Bind.bind(dictMonad.Bind1())(f)(function (f$prime) {
return Control_Bind.bind(dictMonad.Bind1())(a)(function (a$prime) {
return Control_Applicative.pure(dictMonad.Applicative0())(f$prime(a$prime));
});
});
};
};
};
exports["Monad"] = Monad;
exports["ap"] = ap;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Either"] = $PS["Data.Either"] || {};
var exports = $PS["Data.Either"];
var Left = (function () {
function Left(value0) {
this.value0 = value0;
};
Left.create = function (value0) {
return new Left(value0);
};
return Left;
})();
var Right = (function () {
function Right(value0) {
this.value0 = value0;
};
Right.create = function (value0) {
return new Right(value0);
};
return Right;
})();
exports["Left"] = Left;
exports["Right"] = Right;
})(PS);
(function(exports) {
"use strict";
exports.unit = {};
})(PS["Data.Unit"] = PS["Data.Unit"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Unit"] = $PS["Data.Unit"] || {};
var exports = $PS["Data.Unit"];
var $foreign = $PS["Data.Unit"];
exports["unit"] = $foreign.unit;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Functor"] = $PS["Data.Functor"] || {};
var exports = $PS["Data.Functor"];
var Data_Function = $PS["Data.Function"];
var Data_Unit = $PS["Data.Unit"];
var Functor = function (map) {
this.map = map;
};
var map = function (dict) {
return dict.map;
};
var $$void = function (dictFunctor) {
return map(dictFunctor)(Data_Function["const"](Data_Unit.unit));
};
var voidRight = function (dictFunctor) {
return function (x) {
return map(dictFunctor)(Data_Function["const"](x));
};
};
exports["Functor"] = Functor;
exports["map"] = map;
exports["void"] = $$void;
exports["voidRight"] = voidRight;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Monad.Error.Class"] = $PS["Control.Monad.Error.Class"] || {};
var exports = $PS["Control.Monad.Error.Class"];
var Control_Applicative = $PS["Control.Applicative"];
var Data_Either = $PS["Data.Either"];
var Data_Functor = $PS["Data.Functor"];
var MonadThrow = function (Monad0, throwError) {
this.Monad0 = Monad0;
this.throwError = throwError;
};
var MonadError = function (MonadThrow0, catchError) {
this.MonadThrow0 = MonadThrow0;
this.catchError = catchError;
};
var catchError = function (dict) {
return dict.catchError;
};
var $$try = function (dictMonadError) {
return function (a) {
return catchError(dictMonadError)(Data_Functor.map(((((dictMonadError.MonadThrow0()).Monad0()).Bind1()).Apply0()).Functor0())(Data_Either.Right.create)(a))((function () {
var $17 = Control_Applicative.pure(((dictMonadError.MonadThrow0()).Monad0()).Applicative0());
return function ($18) {
return $17(Data_Either.Left.create($18));
};
})());
};
};
exports["MonadThrow"] = MonadThrow;
exports["MonadError"] = MonadError;
exports["try"] = $$try;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Crypto.Subtle.Constants.AES"] = $PS["Crypto.Subtle.Constants.AES"] || {};
var exports = $PS["Crypto.Subtle.Constants.AES"];
var l256 = 256;
var l192 = 192;
var l128 = 128;
var aesKW = "AES-KW";
var aesGCM = "AES-GCM";
var aesCTR = "AES-CTR";
var aesCBC = "AES-CBC";
exports["aesCTR"] = aesCTR;
exports["aesCBC"] = aesCBC;
exports["aesGCM"] = aesGCM;
exports["aesKW"] = aesKW;
exports["l128"] = l128;
exports["l192"] = l192;
exports["l256"] = l256;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Crypto.Subtle.Constants.EC"] = $PS["Crypto.Subtle.Constants.EC"] || {};
var exports = $PS["Crypto.Subtle.Constants.EC"];
var p521 = "P-521";
var p384 = "P-384";
var p256 = "P-256";
var ecdsa = "ECDSA";
var ecdh = "ECDH";
exports["ecdsa"] = ecdsa;
exports["ecdh"] = ecdh;
exports["p256"] = p256;
exports["p384"] = p384;
exports["p521"] = p521;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Crypto.Subtle.Constants.RSA"] = $PS["Crypto.Subtle.Constants.RSA"] || {};
var exports = $PS["Crypto.Subtle.Constants.RSA"];
var rsaPSS = "RSA-PSS";
var rsaPKCS1 = "RSASSA-PKCS1-v1_5";
var rsaOAEP = "RSA-OAEP";
exports["rsaPKCS1"] = rsaPKCS1;
exports["rsaPSS"] = rsaPSS;
exports["rsaOAEP"] = rsaOAEP;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Crypto.Subtle.Hash"] = $PS["Crypto.Subtle.Hash"] || {};
var exports = $PS["Crypto.Subtle.Hash"];
var sha512 = "SHA-512";
var sha384 = "SHA-384";
var sha256 = "SHA-256";
var sha1 = "SHA-1";
exports["sha1"] = sha1;
exports["sha256"] = sha256;
exports["sha384"] = sha384;
exports["sha512"] = sha512;
})(PS);
(function(exports) {
"use strict";
exports.generateKeyImpl = function generateKeyImpl (a,e,u) {
return crypto.subtle.generateKey(a,e,u);
};
exports.exp65537 = new Uint8Array([0x01,0x00,0x01]);
})(PS["Crypto.Subtle.Key.Generate"] = PS["Crypto.Subtle.Key.Generate"] || {});
(function(exports) {
"use strict";
exports.pureE = function (a) {
return function () {
return a;
};
};
exports.bindE = function (a) {
return function (f) {
return function () {
return f(a())();
};
};
};
})(PS["Effect"] = PS["Effect"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Effect"] = $PS["Effect"] || {};
var exports = $PS["Effect"];
var $foreign = $PS["Effect"];
var Control_Applicative = $PS["Control.Applicative"];
var Control_Apply = $PS["Control.Apply"];
var Control_Bind = $PS["Control.Bind"];
var Control_Monad = $PS["Control.Monad"];
var Data_Functor = $PS["Data.Functor"];
var monadEffect = new Control_Monad.Monad(function () {
return applicativeEffect;
}, function () {
return bindEffect;
});
var bindEffect = new Control_Bind.Bind(function () {
return applyEffect;
}, $foreign.bindE);
var applyEffect = new Control_Apply.Apply(function () {
return functorEffect;
}, Control_Monad.ap(monadEffect));
var applicativeEffect = new Control_Applicative.Applicative(function () {
return applyEffect;
}, $foreign.pureE);
var functorEffect = new Data_Functor.Functor(Control_Applicative.liftA1(applicativeEffect));
exports["functorEffect"] = functorEffect;
exports["applicativeEffect"] = applicativeEffect;
})(PS);
(function(exports) {
/* globals setImmediate, clearImmediate, setTimeout, clearTimeout */
/* jshint -W083, -W098, -W003 */
"use strict";
var Aff = function () {
// A unique value for empty.
var EMPTY = {};
/*
An awkward approximation. We elide evidence we would otherwise need in PS for
efficiency sake.
data Aff eff a
= Pure a
| Throw Error
| Catch (Aff eff a) (Error -> Aff eff a)
| Sync (Eff eff a)
| Async ((Either Error a -> Eff eff Unit) -> Eff eff (Canceler eff))
| forall b. Bind (Aff eff b) (b -> Aff eff a)
| forall b. Bracket (Aff eff b) (BracketConditions eff b) (b -> Aff eff a)
| forall b. Fork Boolean (Aff eff b) ?(Fiber eff b -> a)
| Sequential (ParAff aff a)
*/
var PURE = "Pure";
var THROW = "Throw";
var CATCH = "Catch";
var SYNC = "Sync";
var ASYNC = "Async";
var BIND = "Bind";
var BRACKET = "Bracket";
var FORK = "Fork";
var SEQ = "Sequential";
/*
data ParAff eff a
= forall b. Map (b -> a) (ParAff eff b)
| forall b. Apply (ParAff eff (b -> a)) (ParAff eff b)
| Alt (ParAff eff a) (ParAff eff a)
| ?Par (Aff eff a)
*/
var MAP = "Map";
var APPLY = "Apply";
var ALT = "Alt";
// Various constructors used in interpretation
var CONS = "Cons"; // Cons-list, for stacks
var RESUME = "Resume"; // Continue indiscriminately
var RELEASE = "Release"; // Continue with bracket finalizers
var FINALIZER = "Finalizer"; // A non-interruptible effect
var FINALIZED = "Finalized"; // Marker for finalization
var FORKED = "Forked"; // Reference to a forked fiber, with resumption stack
var FIBER = "Fiber"; // Actual fiber reference
var THUNK = "Thunk"; // Primed effect, ready to invoke
function Aff(tag, _1, _2, _3) {
this.tag = tag;
this._1 = _1;
this._2 = _2;
this._3 = _3;
}
function AffCtr(tag) {
var fn = function (_1, _2, _3) {
return new Aff(tag, _1, _2, _3);
};
fn.tag = tag;
return fn;
}
function nonCanceler(error) {
return new Aff(PURE, void 0);
}
function runEff(eff) {
try {
eff();
} catch (error) {
setTimeout(function () {
throw error;
}, 0);
}
}
function runSync(left, right, eff) {
try {
return right(eff());
} catch (error) {
return left(error);
}
}
function runAsync(left, eff, k) {
try {
return eff(k)();
} catch (error) {
k(left(error))();
return nonCanceler;
}
}
var Scheduler = function () {
var limit = 1024;
var size = 0;
var ix = 0;
var queue = new Array(limit);
var draining = false;
function drain() {
var thunk;
draining = true;
while (size !== 0) {
size--;
thunk = queue[ix];
queue[ix] = void 0;
ix = (ix + 1) % limit;
thunk();
}
draining = false;
}
return {
isDraining: function () {
return draining;
},
enqueue: function (cb) {
var i, tmp;
if (size === limit) {
tmp = draining;
drain();
draining = tmp;
}
queue[(ix + size) % limit] = cb;
size++;
if (!draining) {
drain();
}
}
};
}();
function Supervisor(util) {
var fibers = {};
var fiberId = 0;
var count = 0;
return {
register: function (fiber) {
var fid = fiberId++;
fiber.onComplete({
rethrow: true,
handler: function (result) {
return function () {
count--;
delete fibers[fid];
};
}
})();
fibers[fid] = fiber;
count++;
},
isEmpty: function () {
return count === 0;
},
killAll: function (killError, cb) {
return function () {
if (count === 0) {
return cb();
}
var killCount = 0;
var kills = {};
function kill(fid) {
kills[fid] = fibers[fid].kill(killError, function (result) {
return function () {
delete kills[fid];
killCount--;
if (util.isLeft(result) && util.fromLeft(result)) {
setTimeout(function () {
throw util.fromLeft(result);
}, 0);
}
if (killCount === 0) {
cb();
}
};
})();
}
for (var k in fibers) {
if (fibers.hasOwnProperty(k)) {
killCount++;
kill(k);
}
}
fibers = {};
fiberId = 0;
count = 0;
return function (error) {
return new Aff(SYNC, function () {
for (var k in kills) {
if (kills.hasOwnProperty(k)) {
kills[k]();
}
}
});
};
};
}
};
}
// Fiber state machine
var SUSPENDED = 0; // Suspended, pending a join.
var CONTINUE = 1; // Interpret the next instruction.
var STEP_BIND = 2; // Apply the next bind.
var STEP_RESULT = 3; // Handle potential failure from a result.
var PENDING = 4; // An async effect is running.
var RETURN = 5; // The current stack has returned.
var COMPLETED = 6; // The entire fiber has completed.
function Fiber(util, supervisor, aff) {
// Monotonically increasing tick, increased on each asynchronous turn.
var runTick = 0;
// The current branch of the state machine.
var status = SUSPENDED;
// The current point of interest for the state machine branch.
var step = aff; // Successful step
var fail = null; // Failure step
var interrupt = null; // Asynchronous interrupt
// Stack of continuations for the current fiber.
var bhead = null;
var btail = null;
// Stack of attempts and finalizers for error recovery. Every `Cons` is also
// tagged with current `interrupt` state. We use this to track which items
// should be ignored or evaluated as a result of a kill.
var attempts = null;
// A special state is needed for Bracket, because it cannot be killed. When
// we enter a bracket acquisition or finalizer, we increment the counter,
// and then decrement once complete.
var bracketCount = 0;
// Each join gets a new id so they can be revoked.
var joinId = 0;
var joins = null;
var rethrow = true;
// Each invocation of `run` requires a tick. When an asynchronous effect is
// resolved, we must check that the local tick coincides with the fiber
// tick before resuming. This prevents multiple async continuations from
// accidentally resuming the same fiber. A common example may be invoking
// the provided callback in `makeAff` more than once, but it may also be an
// async effect resuming after the fiber was already cancelled.
function run(localRunTick) {
var tmp, result, attempt;
while (true) {
tmp = null;
result = null;
attempt = null;
switch (status) {
case STEP_BIND:
status = CONTINUE;
try {
step = bhead(step);
if (btail === null) {
bhead = null;
} else {
bhead = btail._1;
btail = btail._2;
}
} catch (e) {
status = RETURN;
fail = util.left(e);
step = null;
}
break;
case STEP_RESULT:
if (util.isLeft(step)) {
status = RETURN;
fail = step;
step = null;
} else if (bhead === null) {
status = RETURN;
} else {
status = STEP_BIND;
step = util.fromRight(step);
}
break;
case CONTINUE:
switch (step.tag) {
case BIND:
if (bhead) {
btail = new Aff(CONS, bhead, btail);
}
bhead = step._2;
status = CONTINUE;
step = step._1;
break;
case PURE:
if (bhead === null) {
status = RETURN;
step = util.right(step._1);
} else {
status = STEP_BIND;
step = step._1;
}
break;
case SYNC:
status = STEP_RESULT;
step = runSync(util.left, util.right, step._1);
break;
case ASYNC:
status = PENDING;
step = runAsync(util.left, step._1, function (result) {
return function () {
if (runTick !== localRunTick) {
return;
}
runTick++;
Scheduler.enqueue(function () {
// It's possible to interrupt the fiber between enqueuing and
// resuming, so we need to check that the runTick is still
// valid.
if (runTick !== localRunTick + 1) {
return;
}
status = STEP_RESULT;
step = result;
run(runTick);
});
};
});
return;
case THROW:
status = RETURN;
fail = util.left(step._1);
step = null;
break;
// Enqueue the Catch so that we can call the error handler later on
// in case of an exception.
case CATCH:
if (bhead === null) {
attempts = new Aff(CONS, step, attempts, interrupt);
} else {
attempts = new Aff(CONS, step, new Aff(CONS, new Aff(RESUME, bhead, btail), attempts, interrupt), interrupt);
}
bhead = null;
btail = null;
status = CONTINUE;
step = step._1;
break;
// Enqueue the Bracket so that we can call the appropriate handlers
// after resource acquisition.
case BRACKET:
bracketCount++;
if (bhead === null) {
attempts = new Aff(CONS, step, attempts, interrupt);
} else {
attempts = new Aff(CONS, step, new Aff(CONS, new Aff(RESUME, bhead, btail), attempts, interrupt), interrupt);
}
bhead = null;
btail = null;
status = CONTINUE;
step = step._1;
break;
case FORK:
status = STEP_RESULT;
tmp = Fiber(util, supervisor, step._2);
if (supervisor) {
supervisor.register(tmp);
}
if (step._1) {
tmp.run();
}
step = util.right(tmp);
break;
case SEQ:
status = CONTINUE;
step = sequential(util, supervisor, step._1);
break;
}
break;
case RETURN:
bhead = null;
btail = null;
// If the current stack has returned, and we have no other stacks to
// resume or finalizers to run, the fiber has halted and we can
// invoke all join callbacks. Otherwise we need to resume.
if (attempts === null) {
status = COMPLETED;
step = interrupt || fail || step;
} else {
// The interrupt status for the enqueued item.
tmp = attempts._3;
attempt = attempts._1;
attempts = attempts._2;
switch (attempt.tag) {
// We cannot recover from an unmasked interrupt. Otherwise we should
// continue stepping, or run the exception handler if an exception
// was raised.
case CATCH:
// We should compare the interrupt status as well because we
// only want it to apply if there has been an interrupt since
// enqueuing the catch.
if (interrupt && interrupt !== tmp && bracketCount === 0) {
status = RETURN;
} else if (fail) {
status = CONTINUE;
step = attempt._2(util.fromLeft(fail));
fail = null;
}
break;
// We cannot resume from an unmasked interrupt or exception.
case RESUME:
// As with Catch, we only want to ignore in the case of an
// interrupt since enqueing the item.
if (interrupt && interrupt !== tmp && bracketCount === 0 || fail) {
status = RETURN;
} else {
bhead = attempt._1;
btail = attempt._2;
status = STEP_BIND;
step = util.fromRight(step);
}
break;
// If we have a bracket, we should enqueue the handlers,
// and continue with the success branch only if the fiber has
// not been interrupted. If the bracket acquisition failed, we
// should not run either.
case BRACKET:
bracketCount--;
if (fail === null) {
result = util.fromRight(step);
// We need to enqueue the Release with the same interrupt
// status as the Bracket that is initiating it.
attempts = new Aff(CONS, new Aff(RELEASE, attempt._2, result), attempts, tmp);
// We should only coninue as long as the interrupt status has not changed or
// we are currently within a non-interruptable finalizer.
if (interrupt === tmp || bracketCount > 0) {
status = CONTINUE;
step = attempt._3(result);
}
}
break;
// Enqueue the appropriate handler. We increase the bracket count
// because it should not be cancelled.
case RELEASE:
attempts = new Aff(CONS, new Aff(FINALIZED, step, fail), attempts, interrupt);
status = CONTINUE;
// It has only been killed if the interrupt status has changed
// since we enqueued the item, and the bracket count is 0. If the
// bracket count is non-zero then we are in a masked state so it's
// impossible to be killed.
if (interrupt && interrupt !== tmp && bracketCount === 0) {
step = attempt._1.killed(util.fromLeft(interrupt))(attempt._2);
} else if (fail) {
step = attempt._1.failed(util.fromLeft(fail))(attempt._2);
} else {
step = attempt._1.completed(util.fromRight(step))(attempt._2);
}
fail = null;
bracketCount++;
break;
case FINALIZER:
bracketCount++;
attempts = new Aff(CONS, new Aff(FINALIZED, step, fail), attempts, interrupt);
status = CONTINUE;
step = attempt._1;
break;
case FINALIZED:
bracketCount--;
status = RETURN;
step = attempt._1;
fail = attempt._2;
break;
}
}
break;
case COMPLETED:
for (var k in joins) {
if (joins.hasOwnProperty(k)) {
rethrow = rethrow && joins[k].rethrow;
runEff(joins[k].handler(step));
}
}
joins = null;
// If we have an interrupt and a fail, then the thread threw while
// running finalizers. This should always rethrow in a fresh stack.
if (interrupt && fail) {
setTimeout(function () {
throw util.fromLeft(fail);
}, 0);
// If we have an unhandled exception, and no other fiber has joined
// then we need to throw the exception in a fresh stack.
} else if (util.isLeft(step) && rethrow) {
setTimeout(function () {
// Guard on reathrow because a completely synchronous fiber can
// still have an observer which was added after-the-fact.
if (rethrow) {
throw util.fromLeft(step);
}
}, 0);
}
return;
case SUSPENDED:
status = CONTINUE;
break;
case PENDING: return;
}
}
}
function onComplete(join) {
return function () {
if (status === COMPLETED) {
rethrow = rethrow && join.rethrow;
join.handler(step)();
return function () {};
}
var jid = joinId++;
joins = joins || {};
joins[jid] = join;
return function() {
if (joins !== null) {
delete joins[jid];
}
};
};
}
function kill(error, cb) {
return function () {
if (status === COMPLETED) {
cb(util.right(void 0))();
return function () {};
}
var canceler = onComplete({
rethrow: false,
handler: function (/* unused */) {
return cb(util.right(void 0));
}
})();
switch (status) {
case SUSPENDED:
interrupt = util.left(error);
status = COMPLETED;
step = interrupt;
run(runTick);
break;
case PENDING:
if (interrupt === null) {
interrupt = util.left(error);
}
if (bracketCount === 0) {
if (status === PENDING) {
attempts = new Aff(CONS, new Aff(FINALIZER, step(error)), attempts, interrupt);
}
status = RETURN;
step = null;
fail = null;
run(++runTick);
}
break;
default:
if (interrupt === null) {
interrupt = util.left(error);
}
if (bracketCount === 0) {
status = RETURN;
step = null;
fail = null;
}
}
return canceler;
};
}
function join(cb) {
return function () {
var canceler = onComplete({
rethrow: false,
handler: cb
})();
if (status === SUSPENDED) {
run(runTick);
}
return canceler;
};
}
return {
kill: kill,
join: join,
onComplete: onComplete,
isSuspended: function () {
return status === SUSPENDED;
},
run: function () {
if (status === SUSPENDED) {
if (!Scheduler.isDraining()) {
Scheduler.enqueue(function () {
run(runTick);
});
} else {
run(runTick);
}
}
}
};
}
function runPar(util, supervisor, par, cb) {
// Table of all forked fibers.
var fiberId = 0;
var fibers = {};
// Table of currently running cancelers, as a product of `Alt` behavior.
var killId = 0;
var kills = {};
// Error used for early cancelation on Alt branches.
var early = new Error("[ParAff] Early exit");
// Error used to kill the entire tree.
var interrupt = null;
// The root pointer of the tree.
var root = EMPTY;
// Walks a tree, invoking all the cancelers. Returns the table of pending
// cancellation fibers.