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_JSPRIMITIVEREF_H
17#define ECMASCRIPT_JSPRIMITIVEREF_H
18
19#include "ecmascript/js_hclass.h"
20#include "ecmascript/js_object.h"
21#include "ecmascript/js_tagged_value-inl.h"
22#include "ecmascript/tagged_array.h"
23
24namespace panda::ecmascript {
25enum class PrimitiveType : uint8_t {
26    PRIMITIVE_BOOLEAN = 0,
27    PRIMITIVE_NUMBER,
28    PRIMITIVE_STRING,
29    PRIMITIVE_SYMBOL,
30    PRIMITIVE_BIGINT,
31};
32
33class JSPrimitiveRef : public JSObject {
34public:
35    CAST_CHECK(JSPrimitiveRef, IsJSPrimitiveRef);
36
37    JSPrimitiveRef() = delete;
38
39    bool IsNumber() const
40    {
41        return GetValue().IsNumber();
42    }
43
44    bool IsBigInt() const
45    {
46        return GetValue().IsBigInt();
47    }
48
49    bool IsInt() const
50    {
51        return GetValue().IsInt();
52    }
53
54    bool IsBoolean() const
55    {
56        return GetValue().IsBoolean();
57    }
58
59    bool IsString() const
60    {
61        return GetValue().IsString();
62    }
63
64    bool IsSymbol() const
65    {
66        return GetValue().IsSymbol();
67    }
68
69    uint32_t GetStringLength() const
70    {
71        ASSERT(IsString());
72        return EcmaStringAccessor(GetValue()).GetLength();
73    }
74
75    // ES6 9.4.3 String Exotic Objects
76    // ES6 9.4.3.4 StringCreate( value, prototype)
77    static JSHandle<JSPrimitiveRef> StringCreate(JSThread *thread, const JSHandle<JSTaggedValue> &value,
78                                                 const JSHandle<JSTaggedValue> &newTarget);
79    static bool StringGetIndexProperty(const JSThread *thread, const JSHandle<JSObject> &obj, uint32_t index,
80                                       PropertyDescriptor *desc);
81
82    static constexpr size_t VALUE_OFFSET = JSObject::SIZE;
83    ACCESSORS(Value, VALUE_OFFSET, SIZE);
84
85    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, VALUE_OFFSET, SIZE)
86
87    DECL_DUMP()
88};
89}  // namespace panda::ecmascript
90
91#endif  // ECMASCRIPT_JSPRIMITIVEREF_H
92