-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.base.h
190 lines (172 loc) · 5.4 KB
/
js.base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include <ChakraCore.h>
#include <algorithm>
#include <exception>
#include <stdint.h>
namespace js {
const int ErrorJsrtError = 0;
const int ErrorTypeMismatch = -1;
const int ErrorCallModeIsDenied = -2;
const int ErrorInvalidArrayIndex = -3;
const int ErrorNotImplement = -4;
const int ErrorAsioError = -5;
const int ErrorSyscall = -6;
using Int = int;
using Real = double;
using Boolean = bool;
using CharPtr = const char*;
using error_t = int;
using length_t = unsigned int;
using refcnt_t = uint32_t;
class exception_t : public std::exception {
protected:
error_t code_ = ErrorJsrtError;
public:
exception_t() = default;
exception_t(error_t code) : code_(code) {}
error_t get() { return code_; }
};
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define ERR_MSG(x) x, __FILE__ ":" STRINGIFY(__LINE__) " : " #x
#define CXX_EXCEPTION_IF(code, cond) \
{ \
do { \
if (cond) \
throw ::js::exception_t(code); \
} while (0); \
}
#define CXX_EXCEPTION_IFNOT(code,cond) CXX_EXCEPTION_IF(code,!(cond))
#define JSERR_TO_EXCEPTION(err) \
do { \
if (err) \
throw ::js::exception_t(err); \
} while (0);
struct call_info_t {
JsValueRef callee;
bool is_new;
JsValueRef self;
JsValueRef* args;
size_t argc;
void* cookie;
JsValueRef returnValue;
};
/**
* @brief
*
*/
class param_t {
protected:
JsValueRef* args = nullptr;
size_t argc = 0;
public:
param_t() = default;
param_t(const call_info_t& info, size_t i) {
args = i < info.argc ? info.args + i : nullptr;
argc = i < info.argc ? info.argc - i : 0;
}
size_t size() const { return argc; }
JsValueRef operator[](size_t index) const {
if (index >= argc)
return nullptr;
return args[index];
}
};
using more_list_ = param_t;
struct content_t {
ChakraBytePtr data = nullptr;
length_t size = 0;
uint8_t& operator[](size_t i) { return data[i]; }
template <typename T>
T * get_ptr() {
return (T*)data;
}
};
template <typename Return_> struct IF_EXCEPTION_RETURN {
Return_ defaultReturnValue;
IF_EXCEPTION_RETURN(Return_ ret) : defaultReturnValue(ret) {}
template <typename FunctionLike_>
Return_ operator<<(const FunctionLike_& function) const {
try {
return function();
}
catch (...) {
return defaultReturnValue;
}
}
template <typename FunctionLike_>
Return_ operator=(const FunctionLike_& function) const {
try {
return function();
}
catch (...) {
return defaultReturnValue;
}
}
};
/**
* @brief Durable<T> is as same as v8::Perisistent<T>.
*
* @tparam T Any class(struct) with AddRef/Release methods.
*/
template <typename T> class Durable {
protected:
T ref_ = T();
public:
Durable() {};
Durable(T ref) : ref_(ref) {
if (!ref_) return;
auto rc = ref_.AddRef();
}
Durable(const Durable& r) {
ref_ = r.ref_;
if (!ref_) return;
auto rc = ref_.AddRef();
}
Durable(Durable&& r) {
std::swap(ref_, r.ref_);
}
~Durable() {
if (!ref_) return;
ref_.Release();
}
Durable& operator=(const Durable& r) {
reset();
ref_ = r.ref_;
if (!ref_) return *this;
auto rc = ref_.AddRef();
return *this;
}
Durable& operator=(Durable&& r) {
reset();
std::swap(ref_, r.ref_);
return *this;
}
const T& get() const {
return ref_;
}
T& get() {
return ref_;
}
void reset() {
if (!ref_) return;
ref_.Release();
ref_ = T();
}
operator T() { return ref_; }
operator const T() const { return ref_; }
T* operator->() { return &ref_; }
const T* operator->() const { return &ref_; }
operator bool() const {
return (bool)ref_;
}
};
}; // namespace js
template <typename FN> struct ARG_COUNT_OF_;
template <typename R, typename This_, typename... ARGS>
struct ARG_COUNT_OF_<R(This_::*)(js::call_info_t&, ARGS...)> {
static const size_t value = sizeof...(ARGS);
};
template <typename R, typename... ARGS> struct ARG_COUNT_OF_<R(*)(js::call_info_t&, ARGS...)> {
static const size_t value = sizeof...(ARGS);
};