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_UTIL_H
17 #define JSVM_UTIL_H
18
19 #include <array>
20 #include <cassert>
21 #include <climits>
22 #include <cstddef>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include <limits>
27 #include <memory>
28 #include <set>
29 #include <sstream>
30 #include <stack>
31 #include <string>
32 #include <string_view>
33 #include <type_traits>
34 #include <unordered_map>
35 #include <utility>
36 #include <vector>
37
38 // jsvm header
39 #include "jsvm_dfx.h"
40 #include "jsvm_log.h"
41 #include "jsvm_version.h"
42 #include "platform/platform.h"
43
44 // v8 header
45 #include "v8-inspector.h"
46 #include "v8-platform.h"
47 #include "v8-profiler.h"
48 #include "v8.h"
49
50 #ifdef __GNUC__
51 #define FORCE_INLINE __attribute__((always_inline))
52 #define FORCE_NOINLINE __attribute__((noinline))
53 #else
54 #define FORCE_INLINE
55 #define FORCE_NOINLINE
56 #endif
57
58 namespace jsvm {
59 template<typename T, size_t N>
ArraySize(const T (&)[N])60 constexpr size_t ArraySize(const T (&)[N])
61 {
62 return N;
63 }
64
65 // Get maximum size for all given types
66 template<typename... Types>
MaxSize()67 constexpr size_t MaxSize()
68 {
69 size_t max = 0;
70 ((max = sizeof(Types) > max ? sizeof(Types) : max), ...);
71 return max;
72 }
73 } // namespace jsvm
74
75 namespace v8impl {
76 template<typename T>
77 using Persistent = v8::Global<T>;
78 } // namespace v8impl
79
80 enum ByteSize : uint8_t {
81 SIZE_0_BYTES = 0,
82 SIZE_1_BYTES = 1,
83 SIZE_2_BYTES = 2,
84 SIZE_4_BYTES = 4,
85 SIZE_8_BYTES = 8,
86 };
87
88 #endif