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