-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
builtins-promise-gen.cc
2231 lines (1852 loc) Β· 83.6 KB
/
builtins-promise-gen.cc
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
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/builtins/builtins-promise-gen.h"
#include "src/builtins/builtins-constructor-gen.h"
#include "src/builtins/builtins-iterator-gen.h"
#include "src/builtins/builtins-utils-gen.h"
#include "src/builtins/builtins.h"
#include "src/code-factory.h"
#include "src/code-stub-assembler.h"
#include "src/objects-inl.h"
namespace v8 {
namespace internal {
using compiler::Node;
Node* PromiseBuiltinsAssembler::AllocateJSPromise(Node* context) {
Node* const native_context = LoadNativeContext(context);
Node* const promise_fun =
LoadContextElement(native_context, Context::PROMISE_FUNCTION_INDEX);
Node* const initial_map =
LoadObjectField(promise_fun, JSFunction::kPrototypeOrInitialMapOffset);
Node* const instance = AllocateJSObjectFromMap(initial_map);
return instance;
}
void PromiseBuiltinsAssembler::PromiseInit(Node* promise) {
StoreObjectFieldNoWriteBarrier(promise, JSPromise::kStatusOffset,
SmiConstant(v8::Promise::kPending));
StoreObjectFieldNoWriteBarrier(promise, JSPromise::kFlagsOffset,
SmiConstant(0));
for (int i = 0; i < v8::Promise::kEmbedderFieldCount; i++) {
int offset = JSPromise::kSize + i * kPointerSize;
StoreObjectFieldNoWriteBarrier(promise, offset, SmiConstant(0));
}
}
Node* PromiseBuiltinsAssembler::AllocateAndInitJSPromise(Node* context) {
return AllocateAndInitJSPromise(context, UndefinedConstant());
}
Node* PromiseBuiltinsAssembler::AllocateAndInitJSPromise(Node* context,
Node* parent) {
Node* const instance = AllocateJSPromise(context);
PromiseInit(instance);
Label out(this);
GotoIfNot(IsPromiseHookEnabledOrDebugIsActive(), &out);
CallRuntime(Runtime::kPromiseHookInit, context, instance, parent);
Goto(&out);
BIND(&out);
return instance;
}
Node* PromiseBuiltinsAssembler::AllocateAndSetJSPromise(Node* context,
Node* status,
Node* result) {
CSA_ASSERT(this, TaggedIsSmi(status));
Node* const instance = AllocateJSPromise(context);
StoreObjectFieldNoWriteBarrier(instance, JSPromise::kStatusOffset, status);
StoreObjectFieldNoWriteBarrier(instance, JSPromise::kResultOffset, result);
StoreObjectFieldNoWriteBarrier(instance, JSPromise::kFlagsOffset,
SmiConstant(0));
for (int i = 0; i < v8::Promise::kEmbedderFieldCount; i++) {
int offset = JSPromise::kSize + i * kPointerSize;
StoreObjectFieldNoWriteBarrier(instance, offset, SmiConstant(0));
}
Label out(this);
GotoIfNot(IsPromiseHookEnabledOrDebugIsActive(), &out);
CallRuntime(Runtime::kPromiseHookInit, context, instance,
UndefinedConstant());
Goto(&out);
BIND(&out);
return instance;
}
std::pair<Node*, Node*>
PromiseBuiltinsAssembler::CreatePromiseResolvingFunctions(
Node* promise, Node* debug_event, Node* native_context) {
Node* const promise_context = CreatePromiseResolvingFunctionsContext(
promise, debug_event, native_context);
Node* const map = LoadContextElement(
native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
Node* const resolve_info =
LoadContextElement(native_context, Context::PROMISE_RESOLVE_SHARED_FUN);
Node* const resolve =
AllocateFunctionWithMapAndContext(map, resolve_info, promise_context);
Node* const reject_info =
LoadContextElement(native_context, Context::PROMISE_REJECT_SHARED_FUN);
Node* const reject =
AllocateFunctionWithMapAndContext(map, reject_info, promise_context);
return std::make_pair(resolve, reject);
}
Node* PromiseBuiltinsAssembler::NewPromiseCapability(Node* context,
Node* constructor,
Node* debug_event) {
if (debug_event == nullptr) {
debug_event = TrueConstant();
}
Label if_not_constructor(this, Label::kDeferred);
GotoIf(TaggedIsSmi(constructor), &if_not_constructor);
GotoIfNot(IsConstructorMap(LoadMap(constructor)), &if_not_constructor);
Node* native_context = LoadNativeContext(context);
Node* map = LoadRoot(Heap::kJSPromiseCapabilityMapRootIndex);
Node* capability = AllocateJSObjectFromMap(map);
StoreObjectFieldNoWriteBarrier(
capability, JSPromiseCapability::kPromiseOffset, UndefinedConstant());
StoreObjectFieldNoWriteBarrier(
capability, JSPromiseCapability::kResolveOffset, UndefinedConstant());
StoreObjectFieldNoWriteBarrier(capability, JSPromiseCapability::kRejectOffset,
UndefinedConstant());
VARIABLE(var_result, MachineRepresentation::kTagged);
var_result.Bind(capability);
Label if_builtin_promise(this), if_custom_promise(this, Label::kDeferred),
out(this);
Branch(WordEqual(constructor,
LoadContextElement(native_context,
Context::PROMISE_FUNCTION_INDEX)),
&if_builtin_promise, &if_custom_promise);
BIND(&if_builtin_promise);
{
Node* promise = AllocateJSPromise(context);
PromiseInit(promise);
StoreObjectField(capability, JSPromiseCapability::kPromiseOffset, promise);
Node* resolve = nullptr;
Node* reject = nullptr;
std::tie(resolve, reject) =
CreatePromiseResolvingFunctions(promise, debug_event, native_context);
StoreObjectField(capability, JSPromiseCapability::kResolveOffset, resolve);
StoreObjectField(capability, JSPromiseCapability::kRejectOffset, reject);
GotoIfNot(IsPromiseHookEnabledOrDebugIsActive(), &out);
CallRuntime(Runtime::kPromiseHookInit, context, promise,
UndefinedConstant());
Goto(&out);
}
BIND(&if_custom_promise);
{
Label if_notcallable(this, Label::kDeferred);
Node* executor_context =
CreatePromiseGetCapabilitiesExecutorContext(capability, native_context);
Node* executor_info = LoadContextElement(
native_context, Context::PROMISE_GET_CAPABILITIES_EXECUTOR_SHARED_FUN);
Node* function_map = LoadContextElement(
native_context, Context::STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX);
Node* executor = AllocateFunctionWithMapAndContext(
function_map, executor_info, executor_context);
Node* promise = ConstructJS(CodeFactory::Construct(isolate()), context,
constructor, executor);
Node* resolve =
LoadObjectField(capability, JSPromiseCapability::kResolveOffset);
GotoIf(TaggedIsSmi(resolve), &if_notcallable);
GotoIfNot(IsCallableMap(LoadMap(resolve)), &if_notcallable);
Node* reject =
LoadObjectField(capability, JSPromiseCapability::kRejectOffset);
GotoIf(TaggedIsSmi(reject), &if_notcallable);
GotoIfNot(IsCallableMap(LoadMap(reject)), &if_notcallable);
StoreObjectField(capability, JSPromiseCapability::kPromiseOffset, promise);
Goto(&out);
BIND(&if_notcallable);
StoreObjectField(capability, JSPromiseCapability::kPromiseOffset,
UndefinedConstant());
StoreObjectField(capability, JSPromiseCapability::kResolveOffset,
UndefinedConstant());
StoreObjectField(capability, JSPromiseCapability::kRejectOffset,
UndefinedConstant());
ThrowTypeError(context, MessageTemplate::kPromiseNonCallable);
}
BIND(&if_not_constructor);
ThrowTypeError(context, MessageTemplate::kNotConstructor, constructor);
BIND(&out);
return var_result.value();
}
void PromiseBuiltinsAssembler::InitializeFunctionContext(Node* native_context,
Node* context,
int slots) {
DCHECK_GE(slots, Context::MIN_CONTEXT_SLOTS);
StoreMapNoWriteBarrier(context, Heap::kFunctionContextMapRootIndex);
StoreObjectFieldNoWriteBarrier(context, FixedArray::kLengthOffset,
SmiConstant(slots));
Node* const empty_fn =
LoadContextElement(native_context, Context::CLOSURE_INDEX);
StoreContextElementNoWriteBarrier(context, Context::CLOSURE_INDEX, empty_fn);
StoreContextElementNoWriteBarrier(context, Context::PREVIOUS_INDEX,
UndefinedConstant());
StoreContextElementNoWriteBarrier(context, Context::EXTENSION_INDEX,
TheHoleConstant());
StoreContextElementNoWriteBarrier(context, Context::NATIVE_CONTEXT_INDEX,
native_context);
}
Node* PromiseBuiltinsAssembler::CreatePromiseContext(Node* native_context,
int slots) {
DCHECK_GE(slots, Context::MIN_CONTEXT_SLOTS);
Node* const context = AllocateInNewSpace(FixedArray::SizeFor(slots));
InitializeFunctionContext(native_context, context, slots);
return context;
}
Node* PromiseBuiltinsAssembler::CreatePromiseResolvingFunctionsContext(
Node* promise, Node* debug_event, Node* native_context) {
Node* const context =
CreatePromiseContext(native_context, kPromiseContextLength);
StoreContextElementNoWriteBarrier(context, kAlreadyVisitedSlot,
SmiConstant(0));
StoreContextElementNoWriteBarrier(context, kPromiseSlot, promise);
StoreContextElementNoWriteBarrier(context, kDebugEventSlot, debug_event);
return context;
}
Node* PromiseBuiltinsAssembler::CreatePromiseGetCapabilitiesExecutorContext(
Node* promise_capability, Node* native_context) {
int kContextLength = kCapabilitiesContextLength;
Node* context = CreatePromiseContext(native_context, kContextLength);
StoreContextElementNoWriteBarrier(context, kCapabilitySlot,
promise_capability);
return context;
}
Node* PromiseBuiltinsAssembler::ThrowIfNotJSReceiver(
Node* context, Node* value, MessageTemplate::Template msg_template,
const char* method_name) {
Label out(this), throw_exception(this, Label::kDeferred);
VARIABLE(var_value_map, MachineRepresentation::kTagged);
GotoIf(TaggedIsSmi(value), &throw_exception);
// Load the instance type of the {value}.
var_value_map.Bind(LoadMap(value));
Node* const value_instance_type = LoadMapInstanceType(var_value_map.value());
Branch(IsJSReceiverInstanceType(value_instance_type), &out, &throw_exception);
// The {value} is not a compatible receiver for this method.
BIND(&throw_exception);
ThrowTypeError(context, msg_template, method_name);
BIND(&out);
return var_value_map.value();
}
Node* PromiseBuiltinsAssembler::PromiseHasHandler(Node* promise) {
Node* const flags = LoadObjectField(promise, JSPromise::kFlagsOffset);
return IsSetWord(SmiUntag(flags), 1 << JSPromise::kHasHandlerBit);
}
void PromiseBuiltinsAssembler::PromiseSetHasHandler(Node* promise) {
Node* const flags = LoadObjectField(promise, JSPromise::kFlagsOffset);
Node* const new_flags =
SmiOr(flags, SmiConstant(1 << JSPromise::kHasHandlerBit));
StoreObjectFieldNoWriteBarrier(promise, JSPromise::kFlagsOffset, new_flags);
}
void PromiseBuiltinsAssembler::PromiseSetHandledHint(Node* promise) {
Node* const flags = LoadObjectField(promise, JSPromise::kFlagsOffset);
Node* const new_flags =
SmiOr(flags, SmiConstant(1 << JSPromise::kHandledHintBit));
StoreObjectFieldNoWriteBarrier(promise, JSPromise::kFlagsOffset, new_flags);
}
Node* PromiseBuiltinsAssembler::SpeciesConstructor(Node* context, Node* object,
Node* default_constructor) {
Isolate* isolate = this->isolate();
VARIABLE(var_result, MachineRepresentation::kTagged);
var_result.Bind(default_constructor);
// 2. Let C be ? Get(O, "constructor").
Node* const constructor =
GetProperty(context, object, isolate->factory()->constructor_string());
// 3. If C is undefined, return defaultConstructor.
Label out(this);
GotoIf(IsUndefined(constructor), &out);
// 4. If Type(C) is not Object, throw a TypeError exception.
ThrowIfNotJSReceiver(context, constructor,
MessageTemplate::kConstructorNotReceiver);
// 5. Let S be ? Get(C, @@species).
Node* const species =
GetProperty(context, constructor, isolate->factory()->species_symbol());
// 6. If S is either undefined or null, return defaultConstructor.
GotoIf(IsUndefined(species), &out);
GotoIf(WordEqual(species, NullConstant()), &out);
// 7. If IsConstructor(S) is true, return S.
Label throw_error(this);
GotoIf(TaggedIsSmi(species), &throw_error);
GotoIfNot(IsConstructorMap(LoadMap(species)), &throw_error);
var_result.Bind(species);
Goto(&out);
// 8. Throw a TypeError exception.
BIND(&throw_error);
ThrowTypeError(context, MessageTemplate::kSpeciesNotConstructor);
BIND(&out);
return var_result.value();
}
void PromiseBuiltinsAssembler::AppendPromiseCallback(int offset, Node* promise,
Node* value) {
Node* elements = LoadObjectField(promise, offset);
Node* length = LoadFixedArrayBaseLength(elements);
CodeStubAssembler::ParameterMode mode = OptimalParameterMode();
length = TaggedToParameter(length, mode);
Node* delta = IntPtrOrSmiConstant(1, mode);
Node* new_capacity = IntPtrOrSmiAdd(length, delta, mode);
const ElementsKind kind = PACKED_ELEMENTS;
const WriteBarrierMode barrier_mode = UPDATE_WRITE_BARRIER;
const CodeStubAssembler::AllocationFlags flags =
CodeStubAssembler::kAllowLargeObjectAllocation;
int additional_offset = 0;
Node* new_elements = AllocateFixedArray(kind, new_capacity, mode, flags);
CopyFixedArrayElements(kind, elements, new_elements, length, barrier_mode,
mode);
StoreFixedArrayElement(new_elements, length, value, barrier_mode,
additional_offset, mode);
StoreObjectField(promise, offset, new_elements);
}
Node* PromiseBuiltinsAssembler::InternalPromiseThen(Node* context,
Node* promise,
Node* on_resolve,
Node* on_reject) {
Isolate* isolate = this->isolate();
// 2. If IsPromise(promise) is false, throw a TypeError exception.
ThrowIfNotInstanceType(context, promise, JS_PROMISE_TYPE,
"Promise.prototype.then");
Node* const native_context = LoadNativeContext(context);
Node* const promise_fun =
LoadContextElement(native_context, Context::PROMISE_FUNCTION_INDEX);
// 3. Let C be ? SpeciesConstructor(promise, %Promise%).
Node* constructor = SpeciesConstructor(context, promise, promise_fun);
// 4. Let resultCapability be ? NewPromiseCapability(C).
Callable call_callable = CodeFactory::Call(isolate);
Label fast_promise_capability(this), promise_capability(this),
perform_promise_then(this);
VARIABLE(var_deferred_promise, MachineRepresentation::kTagged);
VARIABLE(var_deferred_on_resolve, MachineRepresentation::kTagged);
VARIABLE(var_deferred_on_reject, MachineRepresentation::kTagged);
Branch(WordEqual(promise_fun, constructor), &fast_promise_capability,
&promise_capability);
BIND(&fast_promise_capability);
{
Node* const deferred_promise = AllocateAndInitJSPromise(context, promise);
var_deferred_promise.Bind(deferred_promise);
var_deferred_on_resolve.Bind(UndefinedConstant());
var_deferred_on_reject.Bind(UndefinedConstant());
Goto(&perform_promise_then);
}
BIND(&promise_capability);
{
Node* const capability = NewPromiseCapability(context, constructor);
var_deferred_promise.Bind(
LoadObjectField(capability, JSPromiseCapability::kPromiseOffset));
var_deferred_on_resolve.Bind(
LoadObjectField(capability, JSPromiseCapability::kResolveOffset));
var_deferred_on_reject.Bind(
LoadObjectField(capability, JSPromiseCapability::kRejectOffset));
Goto(&perform_promise_then);
}
// 5. Return PerformPromiseThen(promise, onFulfilled, onRejected,
// resultCapability).
BIND(&perform_promise_then);
Node* const result = InternalPerformPromiseThen(
context, promise, on_resolve, on_reject, var_deferred_promise.value(),
var_deferred_on_resolve.value(), var_deferred_on_reject.value());
return result;
}
Node* PromiseBuiltinsAssembler::InternalPerformPromiseThen(
Node* context, Node* promise, Node* on_resolve, Node* on_reject,
Node* deferred_promise, Node* deferred_on_resolve,
Node* deferred_on_reject) {
VARIABLE(var_on_resolve, MachineRepresentation::kTagged);
VARIABLE(var_on_reject, MachineRepresentation::kTagged);
var_on_resolve.Bind(on_resolve);
var_on_reject.Bind(on_reject);
Label out(this), if_onresolvenotcallable(this), onrejectcheck(this),
append_callbacks(this);
GotoIf(TaggedIsSmi(on_resolve), &if_onresolvenotcallable);
Isolate* isolate = this->isolate();
Node* const on_resolve_map = LoadMap(on_resolve);
Branch(IsCallableMap(on_resolve_map), &onrejectcheck,
&if_onresolvenotcallable);
BIND(&if_onresolvenotcallable);
{
Node* const default_resolve_handler_symbol = HeapConstant(
isolate->factory()->promise_default_resolve_handler_symbol());
var_on_resolve.Bind(default_resolve_handler_symbol);
Goto(&onrejectcheck);
}
BIND(&onrejectcheck);
{
Label if_onrejectnotcallable(this);
GotoIf(TaggedIsSmi(on_reject), &if_onrejectnotcallable);
Node* const on_reject_map = LoadMap(on_reject);
Branch(IsCallableMap(on_reject_map), &append_callbacks,
&if_onrejectnotcallable);
BIND(&if_onrejectnotcallable);
{
Node* const default_reject_handler_symbol = HeapConstant(
isolate->factory()->promise_default_reject_handler_symbol());
var_on_reject.Bind(default_reject_handler_symbol);
Goto(&append_callbacks);
}
}
BIND(&append_callbacks);
{
Label fulfilled_check(this);
Node* const status = LoadObjectField(promise, JSPromise::kStatusOffset);
GotoIfNot(SmiEqual(status, SmiConstant(v8::Promise::kPending)),
&fulfilled_check);
Node* const existing_deferred_promise =
LoadObjectField(promise, JSPromise::kDeferredPromiseOffset);
Label if_noexistingcallbacks(this), if_existingcallbacks(this);
Branch(IsUndefined(existing_deferred_promise), &if_noexistingcallbacks,
&if_existingcallbacks);
BIND(&if_noexistingcallbacks);
{
// Store callbacks directly in the slots.
StoreObjectField(promise, JSPromise::kDeferredPromiseOffset,
deferred_promise);
StoreObjectField(promise, JSPromise::kDeferredOnResolveOffset,
deferred_on_resolve);
StoreObjectField(promise, JSPromise::kDeferredOnRejectOffset,
deferred_on_reject);
StoreObjectField(promise, JSPromise::kFulfillReactionsOffset,
var_on_resolve.value());
StoreObjectField(promise, JSPromise::kRejectReactionsOffset,
var_on_reject.value());
Goto(&out);
}
BIND(&if_existingcallbacks);
{
Label if_singlecallback(this), if_multiplecallbacks(this);
BranchIfJSObject(existing_deferred_promise, &if_singlecallback,
&if_multiplecallbacks);
BIND(&if_singlecallback);
{
// Create new FixedArrays to store callbacks, and migrate
// existing callbacks.
Node* const deferred_promise_arr =
AllocateFixedArray(PACKED_ELEMENTS, IntPtrConstant(2));
StoreFixedArrayElement(deferred_promise_arr, 0,
existing_deferred_promise);
StoreFixedArrayElement(deferred_promise_arr, 1, deferred_promise);
Node* const deferred_on_resolve_arr =
AllocateFixedArray(PACKED_ELEMENTS, IntPtrConstant(2));
StoreFixedArrayElement(
deferred_on_resolve_arr, 0,
LoadObjectField(promise, JSPromise::kDeferredOnResolveOffset));
StoreFixedArrayElement(deferred_on_resolve_arr, 1, deferred_on_resolve);
Node* const deferred_on_reject_arr =
AllocateFixedArray(PACKED_ELEMENTS, IntPtrConstant(2));
StoreFixedArrayElement(
deferred_on_reject_arr, 0,
LoadObjectField(promise, JSPromise::kDeferredOnRejectOffset));
StoreFixedArrayElement(deferred_on_reject_arr, 1, deferred_on_reject);
Node* const fulfill_reactions =
AllocateFixedArray(PACKED_ELEMENTS, IntPtrConstant(2));
StoreFixedArrayElement(
fulfill_reactions, 0,
LoadObjectField(promise, JSPromise::kFulfillReactionsOffset));
StoreFixedArrayElement(fulfill_reactions, 1, var_on_resolve.value());
Node* const reject_reactions =
AllocateFixedArray(PACKED_ELEMENTS, IntPtrConstant(2));
StoreFixedArrayElement(
reject_reactions, 0,
LoadObjectField(promise, JSPromise::kRejectReactionsOffset));
StoreFixedArrayElement(reject_reactions, 1, var_on_reject.value());
// Store new FixedArrays in promise.
StoreObjectField(promise, JSPromise::kDeferredPromiseOffset,
deferred_promise_arr);
StoreObjectField(promise, JSPromise::kDeferredOnResolveOffset,
deferred_on_resolve_arr);
StoreObjectField(promise, JSPromise::kDeferredOnRejectOffset,
deferred_on_reject_arr);
StoreObjectField(promise, JSPromise::kFulfillReactionsOffset,
fulfill_reactions);
StoreObjectField(promise, JSPromise::kRejectReactionsOffset,
reject_reactions);
Goto(&out);
}
BIND(&if_multiplecallbacks);
{
AppendPromiseCallback(JSPromise::kDeferredPromiseOffset, promise,
deferred_promise);
AppendPromiseCallback(JSPromise::kDeferredOnResolveOffset, promise,
deferred_on_resolve);
AppendPromiseCallback(JSPromise::kDeferredOnRejectOffset, promise,
deferred_on_reject);
AppendPromiseCallback(JSPromise::kFulfillReactionsOffset, promise,
var_on_resolve.value());
AppendPromiseCallback(JSPromise::kRejectReactionsOffset, promise,
var_on_reject.value());
Goto(&out);
}
}
BIND(&fulfilled_check);
{
Label reject(this);
Node* const result = LoadObjectField(promise, JSPromise::kResultOffset);
GotoIfNot(WordEqual(status, SmiConstant(v8::Promise::kFulfilled)),
&reject);
Node* info = AllocatePromiseReactionJobInfo(
result, var_on_resolve.value(), deferred_promise, deferred_on_resolve,
deferred_on_reject, context);
// TODO(gsathya): Move this to TF
CallRuntime(Runtime::kEnqueuePromiseReactionJob, context, info);
Goto(&out);
BIND(&reject);
{
Node* const has_handler = PromiseHasHandler(promise);
Label enqueue(this);
// TODO(gsathya): Fold these runtime calls and move to TF.
GotoIf(has_handler, &enqueue);
CallRuntime(Runtime::kPromiseRevokeReject, context, promise);
Goto(&enqueue);
BIND(&enqueue);
{
Node* info = AllocatePromiseReactionJobInfo(
result, var_on_reject.value(), deferred_promise,
deferred_on_resolve, deferred_on_reject, context);
// TODO(gsathya): Move this to TF
CallRuntime(Runtime::kEnqueuePromiseReactionJob, context, info);
Goto(&out);
}
}
}
}
BIND(&out);
PromiseSetHasHandler(promise);
return deferred_promise;
}
// Promise fast path implementations rely on unmodified JSPromise instances.
// We use a fairly coarse granularity for this and simply check whether both
// the promise itself is unmodified (i.e. its map has not changed) and its
// prototype is unmodified.
// TODO(gsathya): Refactor this out to prevent code dupe with builtins-regexp
void PromiseBuiltinsAssembler::BranchIfFastPath(Node* context, Node* promise,
Label* if_isunmodified,
Label* if_ismodified) {
Node* const native_context = LoadNativeContext(context);
Node* const promise_fun =
LoadContextElement(native_context, Context::PROMISE_FUNCTION_INDEX);
BranchIfFastPath(native_context, promise_fun, promise, if_isunmodified,
if_ismodified);
}
void PromiseBuiltinsAssembler::BranchIfFastPath(Node* native_context,
Node* promise_fun,
Node* promise,
Label* if_isunmodified,
Label* if_ismodified) {
CSA_ASSERT(this, IsNativeContext(native_context));
CSA_ASSERT(this,
WordEqual(promise_fun,
LoadContextElement(native_context,
Context::PROMISE_FUNCTION_INDEX)));
Node* const map = LoadMap(promise);
Node* const initial_map =
LoadObjectField(promise_fun, JSFunction::kPrototypeOrInitialMapOffset);
Node* const has_initialmap = WordEqual(map, initial_map);
GotoIfNot(has_initialmap, if_ismodified);
Node* const initial_proto_initial_map =
LoadContextElement(native_context, Context::PROMISE_PROTOTYPE_MAP_INDEX);
Node* const proto_map = LoadMap(LoadMapPrototype(map));
Node* const proto_has_initialmap =
WordEqual(proto_map, initial_proto_initial_map);
Branch(proto_has_initialmap, if_isunmodified, if_ismodified);
}
Node* PromiseBuiltinsAssembler::AllocatePromiseResolveThenableJobInfo(
Node* thenable, Node* then, Node* resolve, Node* reject, Node* context) {
Node* const info = Allocate(PromiseResolveThenableJobInfo::kSize);
StoreMapNoWriteBarrier(info,
Heap::kPromiseResolveThenableJobInfoMapRootIndex);
StoreObjectFieldNoWriteBarrier(
info, PromiseResolveThenableJobInfo::kThenableOffset, thenable);
StoreObjectFieldNoWriteBarrier(
info, PromiseResolveThenableJobInfo::kThenOffset, then);
StoreObjectFieldNoWriteBarrier(
info, PromiseResolveThenableJobInfo::kResolveOffset, resolve);
StoreObjectFieldNoWriteBarrier(
info, PromiseResolveThenableJobInfo::kRejectOffset, reject);
StoreObjectFieldNoWriteBarrier(
info, PromiseResolveThenableJobInfo::kContextOffset, context);
return info;
}
void PromiseBuiltinsAssembler::InternalResolvePromise(Node* context,
Node* promise,
Node* result) {
Isolate* isolate = this->isolate();
VARIABLE(var_reason, MachineRepresentation::kTagged);
VARIABLE(var_then, MachineRepresentation::kTagged);
Label do_enqueue(this), fulfill(this), if_cycle(this, Label::kDeferred),
if_rejectpromise(this, Label::kDeferred), out(this);
Label cycle_check(this);
GotoIfNot(IsPromiseHookEnabledOrDebugIsActive(), &cycle_check);
CallRuntime(Runtime::kPromiseHookResolve, context, promise);
Goto(&cycle_check);
BIND(&cycle_check);
// 6. If SameValue(resolution, promise) is true, then
GotoIf(SameValue(promise, result), &if_cycle);
// 7. If Type(resolution) is not Object, then
GotoIf(TaggedIsSmi(result), &fulfill);
GotoIfNot(IsJSReceiver(result), &fulfill);
Label if_nativepromise(this), if_notnativepromise(this, Label::kDeferred);
Node* const native_context = LoadNativeContext(context);
Node* const promise_fun =
LoadContextElement(native_context, Context::PROMISE_FUNCTION_INDEX);
BranchIfFastPath(native_context, promise_fun, result, &if_nativepromise,
&if_notnativepromise);
// Resolution is a native promise and if it's already resolved or
// rejected, shortcircuit the resolution procedure by directly
// reusing the value from the promise.
BIND(&if_nativepromise);
{
Node* const thenable_status =
LoadObjectField(result, JSPromise::kStatusOffset);
Node* const thenable_value =
LoadObjectField(result, JSPromise::kResultOffset);
Label if_isnotpending(this);
GotoIfNot(SmiEqual(SmiConstant(v8::Promise::kPending), thenable_status),
&if_isnotpending);
// TODO(gsathya): Use a marker here instead of the actual then
// callback, and check for the marker in PromiseResolveThenableJob
// and perform PromiseThen.
Node* const then =
LoadContextElement(native_context, Context::PROMISE_THEN_INDEX);
var_then.Bind(then);
Goto(&do_enqueue);
BIND(&if_isnotpending);
{
Label if_fulfilled(this), if_rejected(this);
Branch(SmiEqual(SmiConstant(v8::Promise::kFulfilled), thenable_status),
&if_fulfilled, &if_rejected);
BIND(&if_fulfilled);
{
PromiseFulfill(context, promise, thenable_value,
v8::Promise::kFulfilled);
PromiseSetHasHandler(promise);
Goto(&out);
}
BIND(&if_rejected);
{
Label reject(this);
Node* const has_handler = PromiseHasHandler(result);
// Promise has already been rejected, but had no handler.
// Revoke previously triggered reject event.
GotoIf(has_handler, &reject);
CallRuntime(Runtime::kPromiseRevokeReject, context, result);
Goto(&reject);
BIND(&reject);
// Don't cause a debug event as this case is forwarding a rejection.
InternalPromiseReject(context, promise, thenable_value, false);
PromiseSetHasHandler(result);
Goto(&out);
}
}
}
BIND(&if_notnativepromise);
{
// 8. Let then be Get(resolution, "then").
Node* const then =
GetProperty(context, result, isolate->factory()->then_string());
// 9. If then is an abrupt completion, then
GotoIfException(then, &if_rejectpromise, &var_reason);
// 11. If IsCallable(thenAction) is false, then
GotoIf(TaggedIsSmi(then), &fulfill);
Node* const then_map = LoadMap(then);
GotoIfNot(IsCallableMap(then_map), &fulfill);
var_then.Bind(then);
Goto(&do_enqueue);
}
BIND(&do_enqueue);
{
// TODO(gsathya): Add fast path for native promises with unmodified
// PromiseThen (which don't need these resolving functions, but
// instead can just call resolve/reject directly).
Node* resolve = nullptr;
Node* reject = nullptr;
std::tie(resolve, reject) = CreatePromiseResolvingFunctions(
promise, FalseConstant(), native_context);
Node* const info = AllocatePromiseResolveThenableJobInfo(
result, var_then.value(), resolve, reject, context);
Label enqueue(this);
GotoIfNot(IsDebugActive(), &enqueue);
GotoIf(TaggedIsSmi(result), &enqueue);
GotoIfNot(HasInstanceType(result, JS_PROMISE_TYPE), &enqueue);
// Mark the dependency of the new promise on the resolution
Node* const key =
HeapConstant(isolate->factory()->promise_handled_by_symbol());
CallRuntime(Runtime::kSetProperty, context, result, key, promise,
SmiConstant(STRICT));
Goto(&enqueue);
// 12. Perform EnqueueJob("PromiseJobs",
// PromiseResolveThenableJob, Β« promise, resolution, thenActionΒ»).
BIND(&enqueue);
// TODO(gsathya): Move this to TF
CallRuntime(Runtime::kEnqueuePromiseResolveThenableJob, context, info);
Goto(&out);
}
// 7.b Return FulfillPromise(promise, resolution).
BIND(&fulfill);
{
PromiseFulfill(context, promise, result, v8::Promise::kFulfilled);
Goto(&out);
}
BIND(&if_cycle);
{
// 6.a Let selfResolutionError be a newly created TypeError object.
Node* const message_id = SmiConstant(MessageTemplate::kPromiseCyclic);
Node* const error =
CallRuntime(Runtime::kNewTypeError, context, message_id, result);
var_reason.Bind(error);
// 6.b Return RejectPromise(promise, selfResolutionError).
Goto(&if_rejectpromise);
}
// 9.a Return RejectPromise(promise, then.[[Value]]).
BIND(&if_rejectpromise);
{
// Don't cause a debug event as this case is forwarding a rejection.
InternalPromiseReject(context, promise, var_reason.value(), false);
Goto(&out);
}
BIND(&out);
}
void PromiseBuiltinsAssembler::PromiseFulfill(
Node* context, Node* promise, Node* result,
v8::Promise::PromiseState status) {
Label do_promisereset(this), debug_async_event_enqueue_recurring(this);
Node* const status_smi = SmiConstant(static_cast<int>(status));
Node* const deferred_promise =
LoadObjectField(promise, JSPromise::kDeferredPromiseOffset);
GotoIf(IsUndefined(deferred_promise), &debug_async_event_enqueue_recurring);
Node* const tasks =
status == v8::Promise::kFulfilled
? LoadObjectField(promise, JSPromise::kFulfillReactionsOffset)
: LoadObjectField(promise, JSPromise::kRejectReactionsOffset);
Node* const deferred_on_resolve =
LoadObjectField(promise, JSPromise::kDeferredOnResolveOffset);
Node* const deferred_on_reject =
LoadObjectField(promise, JSPromise::kDeferredOnRejectOffset);
Node* const info = AllocatePromiseReactionJobInfo(
result, tasks, deferred_promise, deferred_on_resolve, deferred_on_reject,
context);
CallRuntime(Runtime::kEnqueuePromiseReactionJob, context, info);
Goto(&debug_async_event_enqueue_recurring);
BIND(&debug_async_event_enqueue_recurring);
{
GotoIfNot(IsDebugActive(), &do_promisereset);
CallRuntime(Runtime::kDebugAsyncEventEnqueueRecurring, context, promise,
status_smi);
Goto(&do_promisereset);
}
BIND(&do_promisereset);
{
StoreObjectField(promise, JSPromise::kStatusOffset, status_smi);
StoreObjectField(promise, JSPromise::kResultOffset, result);
StoreObjectFieldRoot(promise, JSPromise::kDeferredPromiseOffset,
Heap::kUndefinedValueRootIndex);
StoreObjectFieldRoot(promise, JSPromise::kDeferredOnResolveOffset,
Heap::kUndefinedValueRootIndex);
StoreObjectFieldRoot(promise, JSPromise::kDeferredOnRejectOffset,
Heap::kUndefinedValueRootIndex);
StoreObjectFieldRoot(promise, JSPromise::kFulfillReactionsOffset,
Heap::kUndefinedValueRootIndex);
StoreObjectFieldRoot(promise, JSPromise::kRejectReactionsOffset,
Heap::kUndefinedValueRootIndex);
}
}
void PromiseBuiltinsAssembler::BranchIfAccessCheckFailed(
Node* context, Node* native_context, Node* promise_constructor,
Node* executor, Label* if_noaccess) {
VARIABLE(var_executor, MachineRepresentation::kTagged);
var_executor.Bind(executor);
Label has_access(this), call_runtime(this, Label::kDeferred);
// If executor is a bound function, load the bound function until we've
// reached an actual function.
Label found_function(this), loop_over_bound_function(this, &var_executor);
Goto(&loop_over_bound_function);
BIND(&loop_over_bound_function);
{
Node* executor_type = LoadInstanceType(var_executor.value());
GotoIf(InstanceTypeEqual(executor_type, JS_FUNCTION_TYPE), &found_function);
GotoIfNot(InstanceTypeEqual(executor_type, JS_BOUND_FUNCTION_TYPE),
&call_runtime);
var_executor.Bind(LoadObjectField(
var_executor.value(), JSBoundFunction::kBoundTargetFunctionOffset));
Goto(&loop_over_bound_function);
}
// Load the context from the function and compare it to the Promise
// constructor's context. If they match, everything is fine, otherwise, bail
// out to the runtime.
BIND(&found_function);
{
Node* function_context =
LoadObjectField(var_executor.value(), JSFunction::kContextOffset);
Node* native_function_context = LoadNativeContext(function_context);
Branch(WordEqual(native_context, native_function_context), &has_access,
&call_runtime);
}
BIND(&call_runtime);
{
Branch(WordEqual(CallRuntime(Runtime::kAllowDynamicFunction, context,
promise_constructor),
BooleanConstant(true)),
&has_access, if_noaccess);
}
BIND(&has_access);
}
void PromiseBuiltinsAssembler::InternalPromiseReject(Node* context,
Node* promise, Node* value,
Node* debug_event) {
Label out(this);
GotoIfNot(IsDebugActive(), &out);
GotoIfNot(WordEqual(TrueConstant(), debug_event), &out);
CallRuntime(Runtime::kDebugPromiseReject, context, promise, value);
Goto(&out);
BIND(&out);
InternalPromiseReject(context, promise, value, false);
}
// This duplicates a lot of logic from PromiseRejectEvent in
// runtime-promise.cc
void PromiseBuiltinsAssembler::InternalPromiseReject(Node* context,
Node* promise, Node* value,
bool debug_event) {
Label fulfill(this), report_unhandledpromise(this), run_promise_hook(this);
if (debug_event) {
GotoIfNot(IsDebugActive(), &run_promise_hook);
CallRuntime(Runtime::kDebugPromiseReject, context, promise, value);
Goto(&run_promise_hook);
} else {
Goto(&run_promise_hook);
}
BIND(&run_promise_hook);
{
GotoIfNot(IsPromiseHookEnabledOrDebugIsActive(), &report_unhandledpromise);
CallRuntime(Runtime::kPromiseHookResolve, context, promise);
Goto(&report_unhandledpromise);
}
BIND(&report_unhandledpromise);
{
GotoIf(PromiseHasHandler(promise), &fulfill);
CallRuntime(Runtime::kReportPromiseReject, context, promise, value);
Goto(&fulfill);
}
BIND(&fulfill);
PromiseFulfill(context, promise, value, v8::Promise::kRejected);
}
void PromiseBuiltinsAssembler::SetForwardingHandlerIfTrue(
Node* context, Node* condition, const NodeGenerator& object) {
Label done(this);
GotoIfNot(condition, &done);
CallRuntime(Runtime::kSetProperty, context, object(),
HeapConstant(factory()->promise_forwarding_handler_symbol()),
TrueConstant(), SmiConstant(STRICT));
Goto(&done);
BIND(&done);
}
void PromiseBuiltinsAssembler::SetPromiseHandledByIfTrue(
Node* context, Node* condition, Node* promise,
const NodeGenerator& handled_by) {
Label done(this);
GotoIfNot(condition, &done);
GotoIf(TaggedIsSmi(promise), &done);
GotoIfNot(HasInstanceType(promise, JS_PROMISE_TYPE), &done);
CallRuntime(Runtime::kSetProperty, context, promise,
HeapConstant(factory()->promise_handled_by_symbol()),
handled_by(), SmiConstant(STRICT));
Goto(&done);