1 /*
2 * Copyright (c) 2024 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 JSVM_DFX_H
17 #define JSVM_DFX_H
18
19 #include <cassert>
20
21 #include "jsvm_log.h"
22 #include "jsvm_version.h"
23 #include "platform/platform.h"
24
25 // v8 header
26 #include "v8.h"
27
28 namespace jsvm {
OnFatalError(const char * location,const char * message)29 [[noreturn]] inline void OnFatalError(const char* location, const char* message)
30 {
31 LOG(Fatal) << "JSVM Fatal Error Position : " << (location ? location : "Unkown");
32 LOG(Fatal) << "JSVM Fatal Error Message : " << (message ? message : "Unkown");
33 platform::OS::Abort();
34 }
35
36 class DebugSealHandleScope {
37 public:
38 explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
39 #ifdef DEBUG
40 : sealHandleScope(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
41 #endif
42 {}
43
44 private:
45 #ifdef DEBUG
46 v8::SealHandleScope sealHandleScope;
47 #endif
48 };
49 } // namespace jsvm
50
51 #define JSVM_FATAL(message) \
52 do { \
53 /* Make sure that this struct does not end up in inline code, but */ \
54 /* rather in a read-only data section when modifying this code. */ \
55 jsvm::OnFatalError(STRINGIFY(__FILE__) ":" STRINGIFY(__LINE__) " ", STRINGIFY(message)); \
56 } while (0)
57
58 #define UNREACHABLE(...) JSVM_FATAL("Unreachable code reached" __VA_OPT__(": ") __VA_ARGS__)
59
60 #ifdef __GNUC__
61 #define LIKELY(expr) __builtin_expect(!!(expr), 1)
62 #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
63 #else
64 #define LIKELY(expr) expr
65 #define UNLIKELY(expr) expr
66 #endif
67
68 #define CHECK(expr) \
69 do { \
70 if (UNLIKELY(!(expr))) { \
71 JSVM_FATAL(expr); \
72 } \
73 } while (0)
74
75 #define CHECK_EQ(a, b) CHECK((a) == (b))
76 #define CHECK_NE(a, b) CHECK((a) != (b))
77 #define CHECK_LE(a, b) CHECK((a) <= (b))
78 #define CHECK_GE(a, b) CHECK((a) >= (b))
79 #define CHECK_LT(a, b) CHECK((a) < (b))
80 #define CHECK_GT(a, b) CHECK((a) > (b))
81 #define CHECK_NULL(val) CHECK((val) == nullptr)
82 #define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
83 #define CHECK_IMPLIES(a, b) CHECK(!(a) || (b))
84
85 #ifdef DEBUG
86 #define DCHECK(expr) CHECK(expr)
87 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
88 #define DCHECK_NE(a, b) CHECK_NE(a, b)
89 #define DCHECK_LE(a, b) CHECK_LE(a, b)
90 #define DCHECK_GE(a, b) CHECK_GE(a, b)
91 #define DCHECK_LT(a, b) CHECK_LT(a, b)
92 #define DCHECK_GT(a, b) CHECK_GT(a, b)
93 #define DCHECK_NULL(val) CHECK_NULL(val)
94 #define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val)
95 #define DCHECK_IMPLIES(a, b) CHECK_IMPLIES(a, b)
96 #else
97 #define DCHECK(expr)
98 #define DCHECK_EQ(a, b)
99 #define DCHECK_NE(a, b)
100 #define DCHECK_LE(a, b)
101 #define DCHECK_GE(a, b)
102 #define DCHECK_LT(a, b)
103 #define DCHECK_GT(a, b)
104 #define DCHECK_NULL(val)
105 #define DCHECK_NOT_NULL(val)
106 #define DCHECK_IMPLIES(a, b)
107 #endif
108
109 #endif