-
Notifications
You must be signed in to change notification settings - Fork 0
/
naah.h
228 lines (177 loc) · 5.23 KB
/
naah.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef SRC_NAAH_H_
#define SRC_NAAH_H_
#include <napi.h>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
namespace naah {
namespace {
#ifdef NAPI_CPP_CUSTOM_NAMESPACE
namespace Napi = ::Napi::NAPI_CPP_CUSTOM_NAMESPACE;
#else
namespace Napi = ::Napi;
#endif
} // namespace
template <typename T, typename Enabled = void>
struct ValueTransformer {
static std::optional<T> FromJS(Napi::Value);
static Napi::Value ToJS(Napi::Env, T);
};
template <typename Ret = void>
class AsyncWork {
public:
template <typename Arg>
AsyncWork(Arg &&arg);
std::function<Ret()> task;
};
template <typename T, typename E = std::nullptr_t>
class Result {
public:
Result() = delete;
Result(T t);
Result(E e);
std::optional<T> value;
std::optional<E> error;
};
template <typename T>
Napi::Value ConvertToJS(Napi::Env env, T v);
struct Undefined {};
struct Null {};
class ArrayBuffer : public std::vector<char> {
private:
using Super = std::vector<char>;
public:
using Super::Super;
};
template <typename E, napi_typedarray_type napi_type>
class TypedArrayOf : public std::vector<E> {
private:
using Super = std::vector<E>;
public:
using Super::Super;
};
using Uint8Array = TypedArrayOf<uint8_t, napi_uint8_array>;
using Uint8ClampedArray = TypedArrayOf<uint8_t, napi_uint8_clamped_array>;
using Int8Array = TypedArrayOf<int8_t, napi_int8_array>;
using Uint16Array = TypedArrayOf<uint16_t, napi_uint16_array>;
using Int16Array = TypedArrayOf<int16_t, napi_int16_array>;
using Uint32Array = TypedArrayOf<uint32_t, napi_uint32_array>;
using Int32Array = TypedArrayOf<int32_t, napi_int32_array>;
using Float32Array = TypedArrayOf<float, napi_float32_array>;
using Float64Array = TypedArrayOf<double, napi_float64_array>;
#if NAPI_VERSION > 5
using BigUint64Array = TypedArrayOf<uint64_t, napi_biguint64_array>;
using BigInt64Array = TypedArrayOf<int64_t, napi_bigint64_array>;
#endif
class Error
#ifdef NAPI_CPP_EXCEPTIONS
: public std::exception
#endif // NAPI_CPP_EXCEPTIONS
{
public:
using JSError = Napi::Error;
explicit Error(const char *msg);
explicit Error(const std::string &msg);
#ifdef NAPI_CPP_EXCEPTIONS
const char *what() const NAPI_NOEXCEPT override;
#endif // NAPI_CPP_EXCEPTIONS
const std::string &Message() const NAPI_NOEXCEPT;
private:
std::string _message;
};
class RangeError : public Error {
public:
using JSError = Napi::RangeError;
using Error::Error;
};
class TypeError : public Error {
public:
using JSError = Napi::TypeError;
using Error::Error;
};
struct ClassMetaInfo;
namespace details {
class ScriptWrappable;
}
class Class {
private:
ClassMetaInfo *_meta_info;
friend class details::ScriptWrappable;
public:
using ConstructFn = std::unique_ptr<Class> (*)(const Napi::CallbackInfo &);
Class();
virtual ~Class();
const ClassMetaInfo *meta_info() const;
};
struct ClassMetaInfo {
const char *name;
ClassMetaInfo *parent;
Class::ConstructFn ctor;
std::vector<napi_property_descriptor> descriptors;
};
template <typename T>
class ClassRegistration {
public:
~ClassRegistration();
template <typename... CtorArgs>
ClassRegistration<T> &Constructor();
template <typename P>
ClassRegistration<T> &Inherits();
template <auto T::*fn>
ClassRegistration<T> &InstanceMethod(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
template <auto T::*getter>
ClassRegistration<T> &InstanceAccessor(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
template <auto T::*getter, auto T::*setter>
ClassRegistration<T> &InstanceAccessor(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
template <auto fn>
ClassRegistration<T> &StaticMethod(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
template <auto getter>
ClassRegistration<T> &StaticAccessor(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
template <auto getter, auto setter>
ClassRegistration<T> &StaticAccessor(
const char *name, napi_property_attributes attributes = napi_default,
void *data = nullptr);
};
struct Object {};
template <typename T>
class ObjectRegistration {
public:
template <auto T::*m>
ObjectRegistration Member(const char *name);
};
class Registration : public Napi::Addon<Registration> {
public:
template <typename T>
static void Value(const char *name, T t);
template <auto fn>
static void Function(const char *name, void *data = nullptr);
template <typename Callable>
static void Function(const char *name, Callable callable,
void *data = nullptr);
template <typename T>
static ClassRegistration<T> Class(const char *name);
template <typename T>
static ObjectRegistration<T> Object();
Registration(Napi::Env env, Napi::Object exports);
template <typename T>
Napi::Function FindClass();
private:
std::map<ClassMetaInfo *, Napi::FunctionReference> classes_;
void DefineClass(Napi::Env env, ClassMetaInfo *meta_info,
Napi::Object exports);
};
} // namespace naah
#include "naah_inl.h"
#endif // SRC_NAAH_H_