1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_PROMISE_H 17 #define ECMASCRIPT_PROMISE_H 18 19 #include "ecmascript/accessor_data.h" 20 #include "ecmascript/js_function.h" 21 #include "ecmascript/js_object.h" 22 #include "ecmascript/js_tagged_value.h" 23 #include "ecmascript/js_tagged_value-inl.h" 24 #include "ecmascript/tagged_queue.h" 25 #include "ecmascript/tagged_queue.h" 26 27 namespace panda::ecmascript { 28 enum class PromiseState : uint8_t { PENDING = 0, FULFILLED, REJECTED }; 29 enum class PromiseType : uint8_t { RESOLVE = 0, REJECT }; 30 enum class PromiseRejectionEvent : uint8_t { REJECT = 0, HANDLE }; 31 32 class PromiseReaction final : public Record { 33 public: 34 CAST_CHECK(PromiseReaction, IsPromiseReaction); 35 36 static constexpr size_t PROMISE_CAPABILITY_OFFSET = Record::SIZE; 37 ACCESSORS(PromiseCapability, PROMISE_CAPABILITY_OFFSET, HANDLER_OFFSET) 38 ACCESSORS(Handler, HANDLER_OFFSET, BIT_FIELD_OFFSET) 39 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 40 DEFINE_ALIGN_SIZE(LAST_OFFSET); 41 42 // define BitField 43 static constexpr size_t TYPE_BITS = 1; 44 FIRST_BIT_FIELD(BitField, Type, PromiseType, TYPE_BITS) 45 46 DECL_VISIT_OBJECT(PROMISE_CAPABILITY_OFFSET, BIT_FIELD_OFFSET) 47 DECL_DUMP() 48 }; 49 50 class PromiseCapability final : public Record { 51 public: 52 CAST_CHECK(PromiseCapability, IsPromiseCapability); 53 54 static constexpr size_t PROMISE_OFFSET = Record::SIZE; 55 ACCESSORS(Promise, PROMISE_OFFSET, RESOLVE_OFFSET); 56 ACCESSORS(Resolve, RESOLVE_OFFSET, REJECT_OFFSET); 57 ACCESSORS(Reject, REJECT_OFFSET, SIZE); 58 59 DECL_DUMP() 60 61 DECL_VISIT_OBJECT(PROMISE_OFFSET, SIZE) 62 }; 63 64 class PromiseIteratorRecord final : public Record { 65 public: 66 CAST_CHECK(PromiseIteratorRecord, IsPromiseIteratorRecord); 67 68 static constexpr size_t ITERATOR_OFFSET = Record::SIZE; 69 ACCESSORS(Iterator, ITERATOR_OFFSET, BIT_FIELD_OFFSET); 70 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 71 DEFINE_ALIGN_SIZE(LAST_OFFSET); 72 73 // define BitField 74 static constexpr size_t DONE_BITS = 1; 75 FIRST_BIT_FIELD(BitField, Done, bool, DONE_BITS) 76 77 DECL_VISIT_OBJECT(ITERATOR_OFFSET, BIT_FIELD_OFFSET) 78 DECL_DUMP() 79 }; 80 81 class PromiseRecord final : public Record { 82 public: 83 CAST_CHECK(PromiseRecord, IsPromiseRecord); 84 85 static constexpr size_t VALUE_OFFSET = Record::SIZE; 86 ACCESSORS(Value, VALUE_OFFSET, SIZE); 87 DECL_DUMP() 88 89 DECL_VISIT_OBJECT(VALUE_OFFSET, SIZE) 90 }; 91 92 class ResolvingFunctionsRecord final : public Record { 93 public: 94 CAST_CHECK(ResolvingFunctionsRecord, IsResolvingFunctionsRecord); 95 96 static constexpr size_t RESOLVE_FUNCTION_OFFSET = Record::SIZE; 97 ACCESSORS(ResolveFunction, RESOLVE_FUNCTION_OFFSET, REJECT_FUNCTION_OFFSET); 98 ACCESSORS(RejectFunction, REJECT_FUNCTION_OFFSET, SIZE); 99 100 DECL_DUMP() 101 102 DECL_VISIT_OBJECT(RESOLVE_FUNCTION_OFFSET, SIZE) 103 }; 104 105 class JSPromise final : public JSObject { 106 public: 107 CAST_CHECK(JSPromise, IsJSPromise); 108 109 // ES6 25.4.1.3 CreateResolvingFunctions ( promise ) 110 static JSHandle<ResolvingFunctionsRecord> CreateResolvingFunctions(JSThread *thread, 111 const JSHandle<JSPromise> &promise); 112 113 // ES6 25.4.1.4 FulfillPromise ( promise, value) 114 static JSTaggedValue FulfillPromise(JSThread *thread, const JSHandle<JSPromise> &promise, 115 const JSHandle<JSTaggedValue> &value); 116 117 // 25.4.1.5 NewPromiseCapability ( C ) 118 static JSHandle<PromiseCapability> NewPromiseCapability(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 119 120 // ES6 24.4.1.6 IsPromise (x) 121 static bool IsPromise(const JSHandle<JSTaggedValue> &value); 122 123 // ES6 25.4.1.7 RejectPromise (promise, reason) 124 static JSTaggedValue RejectPromise(JSThread *thread, const JSHandle<JSPromise> &promise, 125 const JSHandle<JSTaggedValue> &reason); 126 127 // 25.4.1.8 TriggerPromiseReactions (reactions, argument) 128 static JSTaggedValue TriggerPromiseReactions(JSThread *thread, const JSHandle<TaggedQueue> &reactions, 129 const JSHandle<JSTaggedValue> &argument); 130 131 static JSHandle<JSTaggedValue> IfThrowGetThrowValue(JSThread *thread); 132 133 static constexpr size_t PROMISE_RESULT_OFFSET = JSObject::SIZE; 134 ACCESSORS(PromiseResult, PROMISE_RESULT_OFFSET, PROMISE_FULFILL_REACTIONS_OFFSET); 135 ACCESSORS(PromiseFulfillReactions, PROMISE_FULFILL_REACTIONS_OFFSET, PROMISE_REJECT_REACTIONS_OFFSET); 136 ACCESSORS(PromiseRejectReactions, PROMISE_REJECT_REACTIONS_OFFSET, BIT_FIELD_OFFSET); 137 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 138 DEFINE_ALIGN_SIZE(LAST_OFFSET); 139 140 // define BitField 141 static constexpr size_t PROMISE_STATE_BITS = 2; 142 static constexpr size_t PROMISE_IS_HANDLED_BITS = 1; 143 FIRST_BIT_FIELD(BitField, PromiseState, PromiseState, PROMISE_STATE_BITS) 144 NEXT_BIT_FIELD(BitField, PromiseIsHandled, bool, PROMISE_IS_HANDLED_BITS, PromiseState) 145 146 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, PROMISE_RESULT_OFFSET, BIT_FIELD_OFFSET) 147 DECL_DUMP() 148 }; 149 } // namespace panda::ecmascript 150 151 #endif // ECMASCRIPT_PROMISE_H 152