14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_ACCESSOR_DATA_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_ACCESSOR_DATA_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_macros.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_native_pointer.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
244514f5e3Sopenharmony_ci#include "ecmascript/mem/slots.h"
254514f5e3Sopenharmony_ci#include "ecmascript/mem/visitor.h"
264514f5e3Sopenharmony_ci#include "ecmascript/record.h"
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_cinamespace panda::ecmascript {
294514f5e3Sopenharmony_ciclass InternalAccessor final : public Record {
304514f5e3Sopenharmony_cipublic:
314514f5e3Sopenharmony_ci    using InternalGetFunc = JSTaggedValue (*)(JSThread *, const JSHandle<JSObject> &);
324514f5e3Sopenharmony_ci    using InternalSetFunc = bool (*)(JSThread *, const JSHandle<JSObject> &, const JSHandle<JSTaggedValue> &, bool);
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci    CAST_CHECK(InternalAccessor, IsInternalAccessor);
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    inline bool HasSetter() const
374514f5e3Sopenharmony_ci    {
384514f5e3Sopenharmony_ci        return GetSetter() != nullptr;
394514f5e3Sopenharmony_ci    }
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    static constexpr size_t GETTER_OFFSET = Record::SIZE;
424514f5e3Sopenharmony_ci    ACCESSORS_FIXED_SIZE_FIELD(Getter, InternalGetFunc, JSTaggedType, GETTER_OFFSET, SETTER_OFFSET)
434514f5e3Sopenharmony_ci    ACCESSORS_FIXED_SIZE_FIELD(Setter, InternalSetFunc, JSTaggedType, SETTER_OFFSET, SIZE)
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    DECL_VISIT_NATIVE_FIELD(GETTER_OFFSET, SIZE)
464514f5e3Sopenharmony_ci};
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ciclass AccessorData final : public Record {
494514f5e3Sopenharmony_cipublic:
504514f5e3Sopenharmony_ci    static AccessorData *Cast(TaggedObject *object)
514514f5e3Sopenharmony_ci    {
524514f5e3Sopenharmony_ci        ASSERT(JSTaggedValue(object).IsAccessorData() || JSTaggedValue(object).IsInternalAccessor());
534514f5e3Sopenharmony_ci        return static_cast<AccessorData *>(object);
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    inline bool IsInternal() const
574514f5e3Sopenharmony_ci    {
584514f5e3Sopenharmony_ci        return GetClass()->IsInternalAccessor();
594514f5e3Sopenharmony_ci    }
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    inline bool HasSetter() const
624514f5e3Sopenharmony_ci    {
634514f5e3Sopenharmony_ci        auto setter = GetSetter();
644514f5e3Sopenharmony_ci        // When the raw data is 0, means the InternalAccessor's setter is nullptr.
654514f5e3Sopenharmony_ci        return !(setter.IsUndefined() || setter.GetRawData() == 0U);
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    JSTaggedValue CallInternalGet(JSThread *thread, const JSHandle<JSObject> &obj) const
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        ASSERT(IsInternal());
714514f5e3Sopenharmony_ci        auto getFunc = InternalAccessor::ConstCast(this)->GetGetter();
724514f5e3Sopenharmony_ci        return getFunc(thread, obj);
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    bool CallInternalSet(JSThread *thread, const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &value,
764514f5e3Sopenharmony_ci                         bool mayThrow = false) const
774514f5e3Sopenharmony_ci    {
784514f5e3Sopenharmony_ci        ASSERT(IsInternal());
794514f5e3Sopenharmony_ci        auto setFunc = InternalAccessor::ConstCast(this)->GetSetter();
804514f5e3Sopenharmony_ci        return setFunc(thread, obj, value, mayThrow);
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    static constexpr size_t GETTER_OFFSET = Record::SIZE;
844514f5e3Sopenharmony_ci    ACCESSORS(Getter, GETTER_OFFSET, SETTER_OFFSET);
854514f5e3Sopenharmony_ci    ACCESSORS(Setter, SETTER_OFFSET, SIZE);
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci    DECL_DUMP()
884514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT(GETTER_OFFSET, SIZE)
894514f5e3Sopenharmony_ci};
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_cistatic_assert(AccessorData::SIZE == InternalAccessor::SIZE &&
924514f5e3Sopenharmony_ci              AccessorData::GETTER_OFFSET == InternalAccessor::GETTER_OFFSET &&
934514f5e3Sopenharmony_ci              AccessorData::SETTER_OFFSET == InternalAccessor::SETTER_OFFSET);
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_cienum class CompletionRecordType : uint8_t {
964514f5e3Sopenharmony_ci    NORMAL = 0U,
974514f5e3Sopenharmony_ci    BREAK,
984514f5e3Sopenharmony_ci    CONTINUE,
994514f5e3Sopenharmony_ci    RETURN,
1004514f5e3Sopenharmony_ci    THROW
1014514f5e3Sopenharmony_ci};
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ciclass CompletionRecord final : public Record {
1044514f5e3Sopenharmony_cipublic:
1054514f5e3Sopenharmony_ci    static CompletionRecord *Cast(TaggedObject *object)
1064514f5e3Sopenharmony_ci    {
1074514f5e3Sopenharmony_ci        ASSERT(JSTaggedValue(object).IsCompletionRecord());
1084514f5e3Sopenharmony_ci        return static_cast<CompletionRecord *>(object);
1094514f5e3Sopenharmony_ci    }
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ci    bool IsThrow() const
1124514f5e3Sopenharmony_ci    {
1134514f5e3Sopenharmony_ci        return GetType() == CompletionRecordType::THROW;
1144514f5e3Sopenharmony_ci    }
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    static constexpr size_t VALUE_OFFSET = Record::SIZE;
1174514f5e3Sopenharmony_ci    ACCESSORS(Value, VALUE_OFFSET, BIT_FIELD_OFFSET)
1184514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
1194514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci    // define BitField
1224514f5e3Sopenharmony_ci    static constexpr size_t TYPE_BITS = 3;
1234514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, Type, CompletionRecordType, TYPE_BITS)
1244514f5e3Sopenharmony_ci
1254514f5e3Sopenharmony_ci    DECL_DUMP()
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT(VALUE_OFFSET, BIT_FIELD_OFFSET)
1284514f5e3Sopenharmony_ci};
1294514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1304514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_ACCESSOR_DATA_H
131