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_ECMA_MACROS_H 17 #define ECMASCRIPT_ECMA_MACROS_H 18 19 #include "ecmascript/common.h" 20 #include "ecmascript/ecma_vm.h" 21 #include "ecmascript/js_tagged_value.h" 22 #include "ecmascript/js_thread.h" 23 #include "ecmascript/mem/barriers-inl.h" 24 #include "ecmascript/mem/slots.h" 25 #include "utils/logger.h" 26 27 #if (!defined PANDA_TARGET_LINUX) && (defined IS_PUBLIC_VERSION) 28 #include "bytrace.h" 29 #endif 30 31 #if defined(__cplusplus) 32 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 33 #define LOG_ECMA(type) \ 34 LOG(type, ECMASCRIPT) << __func__ << " Line:" << __LINE__ << " " // NOLINT(bugprone-lambda-function-name) 35 36 #define ECMA_GC_LOG() LOG(DEBUG, ECMASCRIPT) << " ecmascript gc log: " 37 38 #define OPTIONAL_LOG(ecmaVM, level, component) \ 39 LOG_IF(ecmaVM->IsOptionalLogEnabled(), level, component) 40 41 #if (!defined PANDA_TARGET_LINUX) && (defined IS_PUBLIC_VERSION) 42 #define ECMA_BYTRACE_NAME(tag, name) \ 43 BYTRACE_NAME(tag, name); \ 44 trace::ScopedTrace scopedTrace(name) 45 #else 46 #define ECMA_BYTRACE_NAME(tag, name) trace::ScopedTrace scopedTrace(name) 47 #endif 48 49 /* Note: We can't statically decide the element type is a primitive or heap object, especially for */ 50 /* dynamically-typed languages like JavaScript. So we simply skip the read-barrier. */ 51 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 52 #define GET_VALUE(addr, offset) Barriers::GetDynValue<JSTaggedType>((addr), (offset)) 53 54 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 55 #define SET_VALUE_WITH_BARRIER(thread, addr, offset, value) \ 56 if ((value).IsHeapObject()) { \ 57 Barriers::SetDynObject<true>(thread, addr, offset, (value).GetRawData()); \ 58 } else { \ 59 Barriers::SetDynPrimitive<JSTaggedType>(addr, offset, (value).GetRawData()); \ 60 } 61 62 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 63 #define SET_VALUE_PRIMITIVE(addr, offset, value) \ 64 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, (value).GetRawData()) 65 66 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 67 #define ACCESSORS(name, offset, endOffset) \ 68 static constexpr size_t endOffset = (offset) + JSTaggedValue::TaggedTypeSize(); \ 69 JSTaggedValue Get##name() const \ 70 { \ 71 /* Note: We can't statically decide the element type is a primitive or heap object, especially for */ \ 72 /* dynamically-typed languages like JavaScript. So we simply skip the read-barrier. */ \ 73 return JSTaggedValue(Barriers::GetDynValue<JSTaggedType>(this, offset)); \ 74 } \ 75 template<typename T> \ 76 void Set##name(const JSThread *thread, JSHandle<T> value, BarrierMode mode = WRITE_BARRIER) \ 77 { \ 78 if (mode == WRITE_BARRIER) { \ 79 if (value.GetTaggedValue().IsHeapObject()) { \ 80 Barriers::SetDynObject<true>(thread, this, offset, value.GetTaggedValue().GetRawData()); \ 81 } else { \ 82 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetTaggedValue().GetRawData()); \ 83 } \ 84 } else { \ 85 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetTaggedValue().GetRawData()); \ 86 } \ 87 } \ 88 void Set##name(const JSThread *thread, JSTaggedValue value, BarrierMode mode = WRITE_BARRIER) \ 89 { \ 90 if (mode == WRITE_BARRIER) { \ 91 if (value.IsHeapObject()) { \ 92 Barriers::SetDynObject<true>(thread, this, offset, value.GetRawData()); \ 93 } else { \ 94 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetRawData()); \ 95 } \ 96 } else { \ 97 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetRawData()); \ 98 } \ 99 } \ 100 void Set##name(JSTaggedValue value) \ 101 { \ 102 Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetRawData()); \ 103 } 104 105 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 106 #define DEFINE_ALIGN_SIZE(offset) \ 107 static constexpr size_t SIZE = ((offset) + sizeof(JSTaggedType) - 1U) & (~(sizeof(JSTaggedType) - 1U)) 108 109 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 110 #define ACCESSORS_FIXED_SIZE_FIELD(name, type, sizeType, offset, endOffset) \ 111 static_assert(sizeof(type) <= sizeof(sizeType)); \ 112 static constexpr size_t endOffset = (offset) + sizeof(sizeType); \ 113 inline void Set##name(type value) \ 114 { \ 115 Barriers::SetDynPrimitive<type>(this, offset, value); \ 116 } \ 117 inline type Get##name() const \ 118 { \ 119 return Barriers::GetDynValue<type>(this, offset); \ 120 } 121 122 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 123 #define ACCESSORS_NATIVE_FIELD(name, type, offset, endOffset) \ 124 ACCESSORS_FIXED_SIZE_FIELD(name, type *, type *, offset, endOffset) 125 126 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 127 #define ACCESSORS_PRIMITIVE_FIELD(name, type, offset, endOffset) \ 128 ACCESSORS_FIXED_SIZE_FIELD(name, type, type, offset, endOffset) 129 130 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 131 #define ACCESSORS_BIT_FIELD(name, offset, endOffset) \ 132 ACCESSORS_FIXED_SIZE_FIELD(name, uint32_t, uint32_t, offset, endOffset) \ 133 inline void Clear##name() \ 134 { \ 135 Set##name(0UL); \ 136 } 137 138 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 139 #define SET_GET_BIT_FIELD(bitFieldName, name, type) \ 140 inline type Get##name() const \ 141 { \ 142 return name##Bits::Decode(Get##bitFieldName()); \ 143 } \ 144 inline void Set##name(type t) \ 145 { \ 146 Set##bitFieldName(name##Bits::Update(Get##bitFieldName(), t)); \ 147 } 148 149 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 150 #define FIRST_BIT_FIELD(bitFieldName, name, type, bits) \ 151 using name##Bits = BitField<type, 0, bits>; \ 152 SET_GET_BIT_FIELD(bitFieldName, name, type) 153 154 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 155 #define NEXT_BIT_FIELD(bitFieldName, name, type, bits, lastName) \ 156 using name##Bits = lastName##Bits::NextField<type, bits>; \ 157 SET_GET_BIT_FIELD(bitFieldName, name, type) 158 159 #if !defined(NDEBUG) 160 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 161 #define DASSERT(cond) assert(cond) 162 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 163 #define DASSERT_PRINT(cond, message) \ 164 if (auto cond_val = (cond); UNLIKELY(!(cond_val))) { \ 165 std::cerr << (message) << std::endl; \ 166 ASSERT(#cond &&cond_val); \ 167 } 168 #else // NDEBUG 169 #define DASSERT(cond) static_cast<void>(0) // NOLINT(cppcoreguidelines-macro-usage) 170 #define DASSERT_PRINT(cond, message) static_cast<void>(0) // NOLINT(cppcoreguidelines-macro-usage) 171 #endif // !NDEBUG 172 173 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 174 #define RASSERT(cond) assert(cond) 175 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 176 #define RASSERT_PRINT(cond, message) \ 177 if (auto cond_val = cond; UNLIKELY(!(cond_val))) { \ 178 std::cerr << message << std::endl; \ 179 RASSERT(#cond &&cond_val); \ 180 } 181 182 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 183 #define RETURN_IF_ABRUPT_COMPLETION(thread) \ 184 do { \ 185 if ((thread)->HasPendingException()) { \ 186 return; \ 187 } \ 188 } while (false) 189 190 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 191 #define RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, value) \ 192 do { \ 193 if ((thread)->HasPendingException()) { \ 194 return (value); \ 195 } \ 196 } while (false) 197 198 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 199 #define RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread) \ 200 do { \ 201 if ((thread)->HasPendingException()) { \ 202 return JSTaggedValue::Exception(); \ 203 } \ 204 } while (false) 205 206 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 207 #define RETURN_HANDLE_IF_ABRUPT_COMPLETION(type, thread) \ 208 do { \ 209 if ((thread)->HasPendingException()) { \ 210 return JSHandle<type>(thread, JSTaggedValue::Exception()); \ 211 } \ 212 } while (false) 213 214 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 215 #define ASSERT_NO_ABRUPT_COMPLETION(thread) ASSERT(!(thread)->HasPendingException()); 216 217 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 218 #define THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, value) \ 219 do { \ 220 if (!(thread)->HasPendingException()) { \ 221 (thread)->SetException(error); \ 222 } \ 223 return (value); \ 224 } while (false) 225 226 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 227 #define THROW_ERROR(thread, type, message) \ 228 do { \ 229 if ((thread)->HasPendingException()) { \ 230 return; \ 231 } \ 232 ObjectFactory *_factory = (thread)->GetEcmaVM()->GetFactory(); \ 233 JSHandle<JSObject> _error = _factory->GetJSError(type, message); \ 234 (thread)->SetException(_error.GetTaggedValue()); \ 235 return; \ 236 } while (false) 237 238 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 239 #define THROW_NEW_ERROR_AND_RETURN_EXCEPTION(thread, error) \ 240 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception()); 241 242 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 243 #define SET_DATE_VALUE(name, code, isLocal) \ 244 static JSTaggedValue name(EcmaRuntimeCallInfo *argv) \ 245 { \ 246 ASSERT(argv); \ 247 JSThread *thread = argv->GetThread(); \ 248 JSHandle<JSTaggedValue> msg = GetThis(argv); \ 249 if (!msg->IsDate()) { \ 250 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a Date Object", JSTaggedValue::Exception()); \ 251 } \ 252 JSHandle<JSDate> jsDate(thread, JSDate::Cast(msg->GetTaggedObject())); \ 253 JSTaggedValue result = jsDate->SetDateValue(argv, code, isLocal); \ 254 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); \ 255 jsDate->SetTimeValue(thread, result); \ 256 return result; \ 257 } 258 259 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 260 #define DATE_TO_STRING(name) \ 261 static JSTaggedValue name(EcmaRuntimeCallInfo *argv) \ 262 { \ 263 ASSERT(argv); \ 264 JSThread *thread = argv->GetThread(); \ 265 JSHandle<JSTaggedValue> msg = GetThis(argv); \ 266 if (!msg->IsDate()) { \ 267 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a Date Object", JSTaggedValue::Exception()); \ 268 } \ 269 if (std::isnan(JSDate::Cast(msg->GetTaggedObject())->GetTimeValue().GetDouble())) { \ 270 THROW_RANGE_ERROR_AND_RETURN(thread, "range error", JSTaggedValue::Exception()); \ 271 } \ 272 return JSDate::Cast(msg->GetTaggedObject())->name(thread); \ 273 } 274 275 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 276 #define DATE_STRING(name) \ 277 static JSTaggedValue name(EcmaRuntimeCallInfo *argv) \ 278 { \ 279 ASSERT(argv); \ 280 JSThread *thread = argv->GetThread(); \ 281 JSHandle<JSTaggedValue> msg = GetThis(argv); \ 282 if (!msg->IsDate()) { \ 283 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a Date Object", JSTaggedValue::Exception()); \ 284 } \ 285 if (std::isnan(JSDate::Cast(msg->GetTaggedObject())->GetTimeValue().GetDouble())) { \ 286 return thread->GetEcmaVM()->GetFactory()->NewFromCanBeCompressString("Invalid Date").GetTaggedValue(); \ 287 } \ 288 return JSDate::Cast(msg->GetTaggedObject())->name(thread); \ 289 } 290 291 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 292 #define GET_DATE_VALUE(name, code, isLocal) \ 293 static JSTaggedValue name(EcmaRuntimeCallInfo *argv) \ 294 { \ 295 ASSERT(argv); \ 296 JSThread *thread = argv->GetThread(); \ 297 JSHandle<JSTaggedValue> msg = GetThis(argv); \ 298 if (!msg->IsDate()) { \ 299 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a Date Object", JSTaggedValue::Exception()); \ 300 } \ 301 JSHandle<JSDate> jsDate(thread, JSDate::Cast(msg->GetTaggedObject())); \ 302 double result = jsDate->GetDateValue(jsDate->GetTimeValue().GetDouble(), code, isLocal); \ 303 return GetTaggedDouble(result); \ 304 } 305 306 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 307 #define THROW_NEW_ERROR_AND_RETURN(thread, error) \ 308 do { \ 309 if (!(thread)->HasPendingException()) { \ 310 (thread)->SetException(error); \ 311 } \ 312 return; \ 313 } while (false) 314 315 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 316 #define THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, errorType, message, value) \ 317 do { \ 318 if ((thread)->HasPendingException()) { \ 319 return (value); \ 320 } \ 321 ObjectFactory *_factory = (thread)->GetEcmaVM()->GetFactory(); \ 322 JSHandle<JSObject> _error = _factory->GetJSError(errorType, message); \ 323 (thread)->SetException(_error.GetTaggedValue()); \ 324 return (value); \ 325 } while (false) 326 327 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 328 #define THROW_TYPE_ERROR_AND_RETURN(thread, message, value) \ 329 THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, ErrorType::TYPE_ERROR, message, value) 330 331 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 332 #define THROW_RANGE_ERROR_AND_RETURN(thread, message, value) \ 333 THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, ErrorType::RANGE_ERROR, message, value) 334 335 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 336 #define THROW_URI_ERROR_AND_RETURN(thread, message, value) \ 337 THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, ErrorType::URI_ERROR, message, value) 338 339 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 340 #define THROW_SYNTAX_ERROR_AND_RETURN(thread, message, value) \ 341 THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, ErrorType::SYNTAX_ERROR, message, value) 342 343 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 344 #define THROW_REFERENCE_ERROR_AND_RETURN(thread, message, value) \ 345 THROW_NEW_ERROR_WITH_MSG_AND_RETURN_VALUE(thread, ErrorType::REFERENCE_ERROR, message, value) 346 347 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 348 #define THROW_TYPE_ERROR(thread, message) \ 349 THROW_ERROR(thread, ErrorType::TYPE_ERROR, message) 350 351 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 352 #define THROW_RANGE_ERROR(thread, message) \ 353 THROW_ERROR(thread, ErrorType::RANGE_ERROR, message) 354 355 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 356 #define RETURN_REJECT_PROMISE_IF_ABRUPT(thread, value, capability) \ 357 do { \ 358 const GlobalEnvConstants *globalConst = (thread)->GlobalConstants(); \ 359 if ((value).GetTaggedValue().IsCompletionRecord()) { \ 360 JSHandle<CompletionRecord> record = JSHandle<CompletionRecord>::Cast(value); \ 361 if (record->IsThrow()) { \ 362 JSHandle<JSTaggedValue> reject(thread, (capability)->GetReject()); \ 363 JSHandle<JSTaggedValue> undefine = globalConst->GetHandledUndefined(); \ 364 InternalCallParams *arg = (thread)->GetInternalCallParams(); \ 365 arg->MakeArgv(record->GetValue()); \ 366 JSTaggedValue res = JSFunction::Call(thread, reject, undefine, 1, arg->GetArgv()); \ 367 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, res); \ 368 return (capability)->GetPromise(); \ 369 } \ 370 } \ 371 if ((thread)->HasPendingException()) { \ 372 (thread)->ClearException(); \ 373 JSHandle<JSTaggedValue> reject(thread, (capability)->GetReject()); \ 374 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined(); \ 375 InternalCallParams *arg = (thread)->GetInternalCallParams(); \ 376 arg->MakeArgv(value); \ 377 JSTaggedValue res = JSFunction::Call(thread, reject, undefined, 1, arg->GetArgv()); \ 378 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, res); \ 379 return (capability)->GetPromise(); \ 380 } \ 381 } while (false) 382 383 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 384 #define RETURN_COMPLETION_IF_ABRUPT(thread, value) \ 385 do { \ 386 if ((thread)->HasPendingException()) { \ 387 JSHandle<CompletionRecord> completionRecord = \ 388 factory->NewCompletionRecord(CompletionRecordType::THROW, value); \ 389 return (completionRecord); \ 390 } \ 391 } while (false) 392 393 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 394 #define DECL_DUMP() \ 395 void Dump(JSThread *thread, std::ostream &os) const DUMP_API_ATTR; \ 396 void Dump(JSThread *thread) const DUMP_API_ATTR \ 397 { \ 398 Dump(thread, std::cout); \ 399 } \ 400 void DumpForSnapshot(JSThread *thread, std::vector<std::pair<CString, JSTaggedValue>> &vec) const; 401 402 #endif // defined(__cplusplus) 403 404 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 405 #define DECL_CAST(TYPE) \ 406 static TYPE *Cast(ObjectHeader *object) \ 407 { \ 408 ASSERT(JSTaggedValue(object).Is##TYPE()); \ 409 return reinterpret_cast<TYPE *>(object); \ 410 } 411 412 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 413 #define DECL_VISIT_ARRAY(BEGIN_OFFSET, LENGTH) \ 414 void VisitRangeSlot(const EcmaObjectRangeVisitor &visitor) \ 415 { \ 416 size_t endOffset = (BEGIN_OFFSET) + (LENGTH) * JSTaggedValue::TaggedTypeSize(); \ 417 visitor(this, ObjectSlot(ToUintPtr(this) + (BEGIN_OFFSET)), ObjectSlot(ToUintPtr(this) + endOffset)); \ 418 } 419 420 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 421 #define DECL_VISIT_OBJECT(BEGIN_OFFSET, END_OFFSET) \ 422 void VisitRangeSlot(const EcmaObjectRangeVisitor &visitor) \ 423 { \ 424 visitor(this, ObjectSlot(ToUintPtr(this) + (BEGIN_OFFSET)), ObjectSlot(ToUintPtr(this) + (END_OFFSET))); \ 425 } 426 427 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 428 #define DECL_VISIT_OBJECT_FOR_JS_OBJECT(PARENTCLASS, BEGIN_OFFSET, END_OFFSET) \ 429 void VisitRangeSlot(const EcmaObjectRangeVisitor &visitor) \ 430 { \ 431 VisitObjects(visitor); \ 432 /* visit in object fields */ \ 433 auto objSize = this->GetClass()->GetObjectSize(); \ 434 if (objSize > SIZE) { \ 435 visitor(this, ObjectSlot(ToUintPtr(this) + SIZE), ObjectSlot(ToUintPtr(this) + objSize)); \ 436 } \ 437 } \ 438 void VisitObjects(const EcmaObjectRangeVisitor &visitor) \ 439 { \ 440 PARENTCLASS::VisitObjects(visitor); \ 441 if ((BEGIN_OFFSET) == (END_OFFSET)) { \ 442 return; \ 443 } \ 444 visitor(this, ObjectSlot(ToUintPtr(this) + (BEGIN_OFFSET)), ObjectSlot(ToUintPtr(this) + (END_OFFSET))); \ 445 } 446 447 #if ECMASCRIPT_ENABLE_CAST_CHECK 448 #define CAST_CHECK(CAST_TYPE, CHECK_METHOD) \ 449 static inline CAST_TYPE *Cast(ObjectHeader *object) \ 450 { \ 451 if (!JSTaggedValue(object).CHECK_METHOD()) { \ 452 std::abort(); \ 453 } \ 454 return static_cast<CAST_TYPE *>(object); \ 455 } \ 456 static inline const CAST_TYPE *ConstCast(const ObjectHeader *object) \ 457 { \ 458 if (!JSTaggedValue(object).CHECK_METHOD()) { \ 459 std::abort(); \ 460 } \ 461 return static_cast<const CAST_TYPE *>(object); \ 462 } 463 # else 464 #define CAST_CHECK(CAST_TYPE, CHECK_METHOD) \ 465 static inline CAST_TYPE *Cast(ObjectHeader *object) \ 466 { \ 467 ASSERT(JSTaggedValue(object).CHECK_METHOD()); \ 468 return static_cast<CAST_TYPE *>(object); \ 469 } \ 470 static const inline CAST_TYPE *ConstCast(const ObjectHeader *object) \ 471 { \ 472 ASSERT(JSTaggedValue(object).CHECK_METHOD()); \ 473 return static_cast<const CAST_TYPE *>(object); \ 474 } 475 476 #define CAST_NO_CHECK(CAST_TYPE) \ 477 static inline CAST_TYPE *Cast(ObjectHeader *object) \ 478 { \ 479 return static_cast<CAST_TYPE *>(object); \ 480 } \ 481 static const inline CAST_TYPE *ConstCast(const ObjectHeader *object) \ 482 { \ 483 return static_cast<const CAST_TYPE *>(object); \ 484 } 485 #endif 486 487 #if ECMASCRIPT_ENABLE_CAST_CHECK 488 #define CAST_CHECK_TAGGEDVALUE(CAST_TYPE, CHECK_METHOD) \ 489 static inline CAST_TYPE *Cast(JSTaggedValue value) \ 490 { \ 491 if (value.IsHeapObject() && value.GetTaggedObject()->GetClass()->CHECK_METHOD()) { \ 492 return static_cast<CAST_TYPE *>(value.GetTaggedObject()); \ 493 } \ 494 std::abort(); \ 495 } 496 # else 497 #define CAST_CHECK_TAGGEDVALUE(CAST_TYPE, CHECK_METHOD) \ 498 static inline CAST_TYPE *Cast(JSTaggedValue value) \ 499 { \ 500 ASSERT(value.IsHeapObject() && value.GetTaggedObject()->GetClass()->CHECK_METHOD()); \ 501 return static_cast<CAST_TYPE *>(value.GetTaggedObject()); \ 502 } 503 #endif 504 505 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 506 #define CHECK_DUMP_FILEDS(begin, end, num) \ 507 LOG_IF((num) != ((end) - (begin)) / JSTaggedValue::TaggedTypeSize(), FATAL, RUNTIME) \ 508 << "Fileds in obj are not in dump list. "; 509 510 #define CHECK_OBJECT_SIZE(size) \ 511 if ((size) == 0) { \ 512 LOG(FATAL, ECMASCRIPT) << __func__ << " Line: " << __LINE__ << " objectSize is " << (size); \ 513 } 514 515 #define CHECK_REGION_END(begin, end) \ 516 if ((begin) > (end)) { \ 517 LOG(FATAL, ECMASCRIPT) << __func__ << " Line: " << __LINE__ << " begin: " << (begin) << " end: " << (end); \ 518 } 519 520 #define CHECK_JS_THREAD(vm) ASSERT(vm->GetJSThread()->GetThreadId() == JSThread::GetCurrentThreadId()) 521 #endif // ECMASCRIPT_ECMA_MACROS_H 522