|
1 | 1 | #ifndef NATIVE_API_UTIL_H_
|
2 | 2 | #define NATIVE_API_UTIL_H_
|
3 | 3 |
|
| 4 | +#include <string> |
| 5 | +#include <ios> |
| 6 | +#include <cstddef> |
| 7 | +#include <cwchar> |
| 8 | + |
| 9 | +#ifdef TARGET_ENGINE_V8 |
| 10 | +namespace std { |
| 11 | +template<> |
| 12 | +struct char_traits<unsigned short> { |
| 13 | + using char_type = unsigned short; |
| 14 | + using int_type = int; |
| 15 | + using off_type = streamoff; |
| 16 | + using pos_type = streampos; |
| 17 | + using state_type = mbstate_t; |
| 18 | + |
| 19 | + static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; } |
| 20 | + static void assign(char_type* s, size_t n, char_type a) noexcept { |
| 21 | + for (size_t i = 0; i < n; ++i) s[i] = a; |
| 22 | + } |
| 23 | + static bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; } |
| 24 | + static bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; } |
| 25 | + static int compare(const char_type* s1, const char_type* s2, size_t n) { |
| 26 | + for (size_t i = 0; i < n; ++i) { |
| 27 | + if (lt(s1[i], s2[i])) return -1; |
| 28 | + if (lt(s2[i], s1[i])) return 1; |
| 29 | + } |
| 30 | + return 0; |
| 31 | + } |
| 32 | + static size_t length(const char_type* s) { |
| 33 | + size_t len = 0; |
| 34 | + while (!eq(s[len], char_type(0))) ++len; |
| 35 | + return len; |
| 36 | + } |
| 37 | + static const char_type* find(const char_type* s, size_t n, const char_type& a) { |
| 38 | + for (size_t i = 0; i < n; ++i) if (eq(s[i], a)) return s + i; |
| 39 | + return nullptr; |
| 40 | + } |
| 41 | + static char_type* move(char_type* s1, const char_type* s2, size_t n) { |
| 42 | + if (n == 0) return s1; |
| 43 | + return (char_type*)memmove(s1, s2, n * sizeof(char_type)); |
| 44 | + } |
| 45 | + static char_type* copy(char_type* s1, const char_type* s2, size_t n) { |
| 46 | + if (n == 0) return s1; |
| 47 | + return (char_type*)memcpy(s1, s2, n * sizeof(char_type)); |
| 48 | + } |
| 49 | + static char_type to_char_type(int_type c) noexcept { return char_type(c); } |
| 50 | + static int_type to_int_type(char_type c) noexcept { return int_type(c); } |
| 51 | + static bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; } |
| 52 | + static int_type eof() noexcept { return static_cast<int_type>(-1); } |
| 53 | + static int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? 0 : c; } |
| 54 | +}; |
| 55 | +} |
| 56 | +#endif // TARGET_ENGINE_V8 |
| 57 | + |
4 | 58 | #include <dlfcn.h>
|
5 | 59 |
|
6 | 60 | #include <sstream>
|
|
90 | 144 |
|
91 | 145 | namespace napi_util {
|
92 | 146 |
|
| 147 | +class PersistentObject { |
| 148 | + public: |
| 149 | + PersistentObject(napi_env env, napi_value value) |
| 150 | + : env_(env), isOwnedRef_(true) { |
| 151 | + napi_create_reference(env_, value, 1, &ref_); |
| 152 | + } |
| 153 | + |
| 154 | + PersistentObject(napi_env env, napi_ref ref) |
| 155 | + : env_(env), ref_(ref), isOwnedRef_(false) { |
| 156 | + uint32_t result; |
| 157 | + napi_reference_ref(env, ref, &result); |
| 158 | + } |
| 159 | + |
| 160 | + ~PersistentObject() { |
| 161 | + if (isOwnedRef_) |
| 162 | + napi_delete_reference(env_, ref_); |
| 163 | + else { |
| 164 | + uint32_t result; |
| 165 | + napi_reference_unref(env_, ref_, &result); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + napi_value GetValue(napi_env env = nullptr) { |
| 170 | + if (env == nullptr) { |
| 171 | + env = env_; |
| 172 | + } |
| 173 | + napi_value value; |
| 174 | + napi_get_reference_value(env, ref_, &value); |
| 175 | + return value; |
| 176 | + } |
| 177 | + |
| 178 | + napi_ref GetRef() const { return ref_; } |
| 179 | + |
| 180 | + napi_env GetEnv() const { return env_; } |
| 181 | + |
| 182 | + private: |
| 183 | + bool isOwnedRef_ = false; |
| 184 | + napi_env env_; |
| 185 | + napi_ref ref_; |
| 186 | +}; |
| 187 | + |
93 | 188 | inline napi_property_descriptor desc(const char* name, napi_callback method,
|
94 | 189 | void* data = nullptr) {
|
95 | 190 | return {
|
@@ -127,6 +222,17 @@ inline std::string get_cxx_string(napi_env env, napi_value str) {
|
127 | 222 | return result;
|
128 | 223 | }
|
129 | 224 |
|
| 225 | +inline napi_value to_js_number(napi_env env, double value) { |
| 226 | + napi_value js_number; |
| 227 | + napi_create_double(env, value, &js_number); |
| 228 | + return js_number; |
| 229 | +} |
| 230 | +inline napi_value to_js_number(napi_env env, int32_t value) { |
| 231 | + napi_value js_number; |
| 232 | + napi_create_int32(env, value, &js_number); |
| 233 | + return js_number; |
| 234 | +} |
| 235 | + |
130 | 236 | inline napi_value to_js_string(napi_env env, const std::string& str) {
|
131 | 237 | napi_value js_string;
|
132 | 238 | napi_create_string_utf8(env, str.c_str(), str.length(), &js_string);
|
|
0 commit comments