react-native-windows 0.72.0-preview.7 → 0.72.0-preview.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Directory.Build.props +24 -19
- package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +2 -4
- package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters +2 -57
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +22 -17
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.h +0 -2
- package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiLoader.cpp +16 -0
- package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems +41 -12
- package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems.filters +17 -6
- package/Microsoft.ReactNative.Managed/packages.lock.json +6 -6
- package/PropertySheets/External/Microsoft.ReactNative.Uwp.CSharpApp.targets +1 -1
- package/PropertySheets/External/Microsoft.ReactNative.Uwp.CppApp.targets +1 -1
- package/PropertySheets/External/Microsoft.ReactNative.WinAppSDK.CSharpApp.targets +1 -1
- package/PropertySheets/Generated/PackageVersion.g.props +2 -2
- package/PropertySheets/JSEngine.props +4 -4
- package/PropertySheets/Warnings.props +6 -0
- package/ReactCommon/ReactCommon.vcxproj +53 -1
- package/ReactCommon/cgmanifest.json +15 -0
- package/Scripts/Tfs/Layout-MSRN-Headers.ps1 +36 -0
- package/Shared/DevSupportManager.cpp +2 -9
- package/Shared/DevSupportManager.h +2 -6
- package/Shared/HermesRuntimeHolder.cpp +318 -81
- package/Shared/HermesRuntimeHolder.h +15 -19
- package/Shared/HermesSamplingProfiler.cpp +5 -6
- package/Shared/InspectorPackagerConnection.cpp +62 -108
- package/Shared/InspectorPackagerConnection.h +9 -21
- package/Shared/JSI/ScriptStore.h +18 -20
- package/Shared/JSI/V8RuntimeHolder.cpp +262 -0
- package/Shared/JSI/V8RuntimeHolder.h +37 -0
- package/Shared/OInstance.cpp +16 -36
- package/Shared/SafeLoadLibrary.cpp +41 -0
- package/Shared/SafeLoadLibrary.h +15 -0
- package/Shared/Shared.vcxitems +21 -10
- package/Shared/Shared.vcxitems.filters +23 -30
- package/package.json +2 -2
- package/template/cs-app-WinAppSDK/proj/ExperimentalFeatures.props +1 -1
- package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.cpp +0 -2105
- package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.h +0 -73
- package/Shared/HermesShim.cpp +0 -122
- package/Shared/HermesShim.h +0 -41
- package/Shared/JSI/NapiJsiV8RuntimeHolder.cpp +0 -209
- package/Shared/JSI/NapiJsiV8RuntimeHolder.h +0 -46
- package/Shared/V8JSIRuntimeHolder.cpp +0 -71
- package/Shared/V8JSIRuntimeHolder.h +0 -58
|
@@ -1,2105 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
|
2
|
-
// Licensed under the MIT License.
|
|
3
|
-
|
|
4
|
-
#define NAPI_EXPERIMENTAL
|
|
5
|
-
|
|
6
|
-
#include "pch.h"
|
|
7
|
-
#include "NodeApiJsiRuntime.h"
|
|
8
|
-
|
|
9
|
-
// Standard Library
|
|
10
|
-
#include <array>
|
|
11
|
-
#include <sstream>
|
|
12
|
-
#include <string_view>
|
|
13
|
-
#include <unordered_set>
|
|
14
|
-
|
|
15
|
-
#pragma region Macros
|
|
16
|
-
|
|
17
|
-
// We use macros to report errors.
|
|
18
|
-
// Macros provide more flexibility to show assert and provide failure context.
|
|
19
|
-
|
|
20
|
-
// Check condition and crash process if it fails.
|
|
21
|
-
#define CHECK_ELSE_CRASH(condition, message) \
|
|
22
|
-
do { \
|
|
23
|
-
if (!(condition)) { \
|
|
24
|
-
assert(false && "Failed: " #condition && (message)); \
|
|
25
|
-
*((int *)0) = 1; \
|
|
26
|
-
} \
|
|
27
|
-
} while (false)
|
|
28
|
-
|
|
29
|
-
// Check condition and throw native exception if it fails.
|
|
30
|
-
#define CHECK_ELSE_THROW(condition, message) \
|
|
31
|
-
do { \
|
|
32
|
-
if (!(condition)) { \
|
|
33
|
-
ThrowNativeException(message); \
|
|
34
|
-
} \
|
|
35
|
-
} while (false)
|
|
36
|
-
|
|
37
|
-
// Check NAPI result and and throw JS exception if it is not napi_ok.
|
|
38
|
-
#define CHECK_NAPI(expression) \
|
|
39
|
-
do { \
|
|
40
|
-
napi_status temp_error_code_ = (expression); \
|
|
41
|
-
if (temp_error_code_ != napi_status::napi_ok) { \
|
|
42
|
-
ThrowJsException(temp_error_code_); \
|
|
43
|
-
} \
|
|
44
|
-
} while (false)
|
|
45
|
-
|
|
46
|
-
// Check NAPI result and and crash if it is not napi_ok.
|
|
47
|
-
#define CHECK_NAPI_ELSE_CRASH(expression) \
|
|
48
|
-
do { \
|
|
49
|
-
napi_status temp_error_code_ = (expression); \
|
|
50
|
-
if (temp_error_code_ != napi_status::napi_ok) { \
|
|
51
|
-
CHECK_ELSE_CRASH(false, "Failed: " #expression); \
|
|
52
|
-
} \
|
|
53
|
-
} while (false)
|
|
54
|
-
|
|
55
|
-
#pragma endregion Macros
|
|
56
|
-
|
|
57
|
-
#pragma region Declarations
|
|
58
|
-
|
|
59
|
-
#ifdef __cpp_lib_span
|
|
60
|
-
#include <span>
|
|
61
|
-
#endif // __cpp_lib_span
|
|
62
|
-
|
|
63
|
-
namespace Microsoft::JSI {
|
|
64
|
-
|
|
65
|
-
namespace {
|
|
66
|
-
|
|
67
|
-
#ifdef __cpp_lib_span
|
|
68
|
-
using std::span;
|
|
69
|
-
#else
|
|
70
|
-
/**
|
|
71
|
-
* @brief A span of values that can be used to pass arguments to a function.
|
|
72
|
-
*
|
|
73
|
-
* This should be replaced with std::span once C++ 2020 is supported.
|
|
74
|
-
*/
|
|
75
|
-
template <typename T>
|
|
76
|
-
struct span {
|
|
77
|
-
constexpr span(std::initializer_list<T> il) noexcept : m_data{const_cast<T *>(il.begin())}, m_size{il.size()} {}
|
|
78
|
-
constexpr span(T *data, size_t size) noexcept : m_data{data}, m_size{size} {}
|
|
79
|
-
|
|
80
|
-
[[nodiscard]] constexpr T *data() const noexcept {
|
|
81
|
-
return m_data;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
[[nodiscard]] constexpr size_t size() const noexcept {
|
|
85
|
-
return m_size;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
[[nodiscard]] constexpr T *begin() const noexcept {
|
|
89
|
-
return m_data;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
[[nodiscard]] constexpr T *end() const noexcept {
|
|
93
|
-
return *(m_data + m_size);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const T &operator[](size_t index) const noexcept {
|
|
97
|
-
return *(m_data + index);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private:
|
|
101
|
-
T *m_data;
|
|
102
|
-
size_t m_size;
|
|
103
|
-
};
|
|
104
|
-
#endif // __cpp_lib_span
|
|
105
|
-
|
|
106
|
-
// Implementation of N-API JSI Runtime
|
|
107
|
-
struct NapiJsiRuntime : facebook::jsi::Runtime {
|
|
108
|
-
NapiJsiRuntime(napi_env env) noexcept;
|
|
109
|
-
|
|
110
|
-
#pragma region facebook::jsi::Runtime
|
|
111
|
-
|
|
112
|
-
facebook::jsi::Value evaluateJavaScript(
|
|
113
|
-
const std::shared_ptr<const facebook::jsi::Buffer> &buffer,
|
|
114
|
-
const std::string &sourceURL) override;
|
|
115
|
-
|
|
116
|
-
std::shared_ptr<const facebook::jsi::PreparedJavaScript> prepareJavaScript(
|
|
117
|
-
const std::shared_ptr<const facebook::jsi::Buffer> &buffer,
|
|
118
|
-
std::string sourceURL) override;
|
|
119
|
-
|
|
120
|
-
facebook::jsi::Value evaluatePreparedJavaScript(
|
|
121
|
-
const std::shared_ptr<const facebook::jsi::PreparedJavaScript> &js) override;
|
|
122
|
-
|
|
123
|
-
bool drainMicrotasks(int maxMicrotasksHint = -1) override;
|
|
124
|
-
|
|
125
|
-
facebook::jsi::Object global() override;
|
|
126
|
-
|
|
127
|
-
std::string description() override;
|
|
128
|
-
|
|
129
|
-
bool isInspectable() override;
|
|
130
|
-
|
|
131
|
-
// We use the default instrumentation() implementation that returns an
|
|
132
|
-
// Instrumentation instance which returns no metrics.
|
|
133
|
-
|
|
134
|
-
protected:
|
|
135
|
-
PointerValue *cloneSymbol(const PointerValue *pointerValue) override;
|
|
136
|
-
PointerValue *cloneBigInt(const PointerValue *pointerValue) override;
|
|
137
|
-
PointerValue *cloneString(const PointerValue *pointerValue) override;
|
|
138
|
-
PointerValue *cloneObject(const PointerValue *pointerValue) override;
|
|
139
|
-
PointerValue *clonePropNameID(const PointerValue *pointerValue) override;
|
|
140
|
-
|
|
141
|
-
facebook::jsi::PropNameID createPropNameIDFromAscii(const char *str, size_t length) override;
|
|
142
|
-
facebook::jsi::PropNameID createPropNameIDFromUtf8(const uint8_t *utf8, size_t length) override;
|
|
143
|
-
facebook::jsi::PropNameID createPropNameIDFromString(const facebook::jsi::String &str) override;
|
|
144
|
-
facebook::jsi::PropNameID createPropNameIDFromSymbol(const facebook::jsi::Symbol &sym) override;
|
|
145
|
-
std::string utf8(const facebook::jsi::PropNameID &id) override;
|
|
146
|
-
bool compare(const facebook::jsi::PropNameID &lhs, const facebook::jsi::PropNameID &rhs) override;
|
|
147
|
-
|
|
148
|
-
// new
|
|
149
|
-
|
|
150
|
-
facebook::jsi::BigInt createBigIntFromInt64(int64_t value) override;
|
|
151
|
-
facebook::jsi::BigInt createBigIntFromUint64(uint64_t value) override;
|
|
152
|
-
bool bigintIsInt64(const facebook::jsi::BigInt &) override;
|
|
153
|
-
bool bigintIsUint64(const facebook::jsi::BigInt &) override;
|
|
154
|
-
uint64_t truncate(const facebook::jsi::BigInt &) override;
|
|
155
|
-
facebook::jsi::String bigintToString(const facebook::jsi::BigInt &, int) override;
|
|
156
|
-
|
|
157
|
-
bool hasNativeState(const facebook::jsi::Object &) override;
|
|
158
|
-
std::shared_ptr<facebook::jsi::NativeState> getNativeState(const facebook::jsi::Object &) override;
|
|
159
|
-
void setNativeState(const facebook::jsi::Object &, std::shared_ptr<facebook::jsi::NativeState> state) override;
|
|
160
|
-
|
|
161
|
-
facebook::jsi::ArrayBuffer createArrayBuffer(std::shared_ptr<facebook::jsi::MutableBuffer> buffer);
|
|
162
|
-
|
|
163
|
-
// end
|
|
164
|
-
|
|
165
|
-
std::string symbolToString(const facebook::jsi::Symbol &s) override;
|
|
166
|
-
|
|
167
|
-
facebook::jsi::String createStringFromAscii(const char *str, size_t length) override;
|
|
168
|
-
facebook::jsi::String createStringFromUtf8(const uint8_t *utf8, size_t length) override;
|
|
169
|
-
std::string utf8(const facebook::jsi::String &str) override;
|
|
170
|
-
|
|
171
|
-
facebook::jsi::Object createObject() override;
|
|
172
|
-
facebook::jsi::Object createObject(std::shared_ptr<facebook::jsi::HostObject> ho) override;
|
|
173
|
-
std::shared_ptr<facebook::jsi::HostObject> getHostObject(const facebook::jsi::Object &) override;
|
|
174
|
-
facebook::jsi::HostFunctionType &getHostFunction(const facebook::jsi::Function &) override;
|
|
175
|
-
|
|
176
|
-
facebook::jsi::Value getProperty(const facebook::jsi::Object &obj, const facebook::jsi::PropNameID &name) override;
|
|
177
|
-
facebook::jsi::Value getProperty(const facebook::jsi::Object &obj, const facebook::jsi::String &name) override;
|
|
178
|
-
bool hasProperty(const facebook::jsi::Object &obj, const facebook::jsi::PropNameID &name) override;
|
|
179
|
-
bool hasProperty(const facebook::jsi::Object &obj, const facebook::jsi::String &name) override;
|
|
180
|
-
void setPropertyValue(
|
|
181
|
-
const facebook::jsi::Object &obj,
|
|
182
|
-
const facebook::jsi::PropNameID &name,
|
|
183
|
-
const facebook::jsi::Value &value) override;
|
|
184
|
-
void setPropertyValue(
|
|
185
|
-
const facebook::jsi::Object &obj,
|
|
186
|
-
const facebook::jsi::String &name,
|
|
187
|
-
const facebook::jsi::Value &value) override;
|
|
188
|
-
|
|
189
|
-
bool isArray(const facebook::jsi::Object &obj) const override;
|
|
190
|
-
bool isArrayBuffer(const facebook::jsi::Object &obj) const override;
|
|
191
|
-
bool isFunction(const facebook::jsi::Object &obj) const override;
|
|
192
|
-
bool isHostObject(const facebook::jsi::Object &obj) const override;
|
|
193
|
-
bool isHostFunction(const facebook::jsi::Function &func) const override;
|
|
194
|
-
|
|
195
|
-
// Returns the names of all enumerable properties of an object.
|
|
196
|
-
// This corresponds to the properties iterated through by the JavaScript for..in loop.
|
|
197
|
-
facebook::jsi::Array getPropertyNames(const facebook::jsi::Object &obj) override;
|
|
198
|
-
|
|
199
|
-
facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object &obj) override;
|
|
200
|
-
facebook::jsi::Value lockWeakObject(const facebook::jsi::WeakObject &weakObj) override;
|
|
201
|
-
|
|
202
|
-
facebook::jsi::Array createArray(size_t length) override;
|
|
203
|
-
size_t size(const facebook::jsi::Array &arr) override;
|
|
204
|
-
size_t size(const facebook::jsi::ArrayBuffer &arrBuf) override;
|
|
205
|
-
|
|
206
|
-
// The lifetime of the buffer returned is the same as the lifetime of the ArrayBuffer.
|
|
207
|
-
// The returned buffer pointer does not count as a reference to ArrayBuffer for the purpose of garbage collection.
|
|
208
|
-
uint8_t *data(const facebook::jsi::ArrayBuffer &arrBuff) override;
|
|
209
|
-
facebook::jsi::Value getValueAtIndex(const facebook::jsi::Array &arr, size_t index) override;
|
|
210
|
-
void setValueAtIndexImpl(const facebook::jsi::Array &arr, size_t index, const facebook::jsi::Value &value) override;
|
|
211
|
-
|
|
212
|
-
facebook::jsi::Function createFunctionFromHostFunction(
|
|
213
|
-
const facebook::jsi::PropNameID &name,
|
|
214
|
-
unsigned int paramCount,
|
|
215
|
-
facebook::jsi::HostFunctionType func) override;
|
|
216
|
-
facebook::jsi::Value call(
|
|
217
|
-
const facebook::jsi::Function &func,
|
|
218
|
-
const facebook::jsi::Value &jsThis,
|
|
219
|
-
const facebook::jsi::Value *args,
|
|
220
|
-
size_t count) override;
|
|
221
|
-
facebook::jsi::Value
|
|
222
|
-
callAsConstructor(const facebook::jsi::Function &func, const facebook::jsi::Value *args, size_t count) override;
|
|
223
|
-
|
|
224
|
-
// Corresponds to napi_open_handle_scope and napi_close_handle_scope.
|
|
225
|
-
ScopeState *pushScope() override;
|
|
226
|
-
void popScope(ScopeState *) override;
|
|
227
|
-
|
|
228
|
-
bool strictEquals(const facebook::jsi::Symbol &a, const facebook::jsi::Symbol &b) const override;
|
|
229
|
-
bool strictEquals(const facebook::jsi::BigInt &a, const facebook::jsi::BigInt &b) const override;
|
|
230
|
-
bool strictEquals(const facebook::jsi::String &a, const facebook::jsi::String &b) const override;
|
|
231
|
-
bool strictEquals(const facebook::jsi::Object &a, const facebook::jsi::Object &b) const override;
|
|
232
|
-
|
|
233
|
-
bool instanceOf(const facebook::jsi::Object &obj, const facebook::jsi::Function &func) override;
|
|
234
|
-
|
|
235
|
-
#pragma endregion facebook::jsi::facebook::jsi::Runtime
|
|
236
|
-
private:
|
|
237
|
-
// Smart pointer to napi_env
|
|
238
|
-
struct EnvHolder {
|
|
239
|
-
EnvHolder(napi_env env) noexcept;
|
|
240
|
-
~EnvHolder() noexcept;
|
|
241
|
-
|
|
242
|
-
EnvHolder(const EnvHolder &) = delete;
|
|
243
|
-
EnvHolder &operator=(const EnvHolder &) = delete;
|
|
244
|
-
|
|
245
|
-
operator napi_env() const noexcept;
|
|
246
|
-
|
|
247
|
-
private:
|
|
248
|
-
napi_env m_env{};
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
// RAII struct to open and close the environment scope.
|
|
252
|
-
struct EnvScope {
|
|
253
|
-
EnvScope(napi_env env) noexcept;
|
|
254
|
-
~EnvScope() noexcept;
|
|
255
|
-
|
|
256
|
-
private:
|
|
257
|
-
napi_env m_env{};
|
|
258
|
-
napi_ext_env_scope m_envScope{};
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
// Sets the variable in the constructor and then restores its value in the destructor.
|
|
262
|
-
template <typename T>
|
|
263
|
-
struct AutoRestore {
|
|
264
|
-
AutoRestore(T *var, T value);
|
|
265
|
-
~AutoRestore();
|
|
266
|
-
|
|
267
|
-
private:
|
|
268
|
-
T *m_var;
|
|
269
|
-
T m_value;
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
// NapiRefHolder is a smart pointer to napi_ext_ref.
|
|
273
|
-
struct NapiRefHolder {
|
|
274
|
-
NapiRefHolder(std::nullptr_t = nullptr) noexcept {}
|
|
275
|
-
explicit NapiRefHolder(NapiJsiRuntime *runtime, napi_ext_ref ref) noexcept;
|
|
276
|
-
explicit NapiRefHolder(NapiJsiRuntime *runtime, napi_value value);
|
|
277
|
-
|
|
278
|
-
// The class is movable.
|
|
279
|
-
NapiRefHolder(NapiRefHolder &&other) noexcept;
|
|
280
|
-
NapiRefHolder &operator=(NapiRefHolder &&other) noexcept;
|
|
281
|
-
|
|
282
|
-
// The class is not copyable.
|
|
283
|
-
NapiRefHolder &operator=(NapiRefHolder const &other) = delete;
|
|
284
|
-
NapiRefHolder(NapiRefHolder const &other) = delete;
|
|
285
|
-
|
|
286
|
-
~NapiRefHolder() noexcept;
|
|
287
|
-
|
|
288
|
-
[[nodiscard]] napi_ext_ref CloneRef() const noexcept;
|
|
289
|
-
operator napi_value() const;
|
|
290
|
-
|
|
291
|
-
explicit operator bool() const noexcept;
|
|
292
|
-
|
|
293
|
-
private:
|
|
294
|
-
NapiJsiRuntime *m_runtime{};
|
|
295
|
-
napi_ext_ref m_ref{};
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
// NapiPointerValueView is the base class for NapiPointerValue.
|
|
299
|
-
// It holds either napi_value or napi_ext_ref. It does nothing in its invalidate() method.
|
|
300
|
-
// It is used directly by the JsiValueView, JsiValueViewArgs, and PropNameIDView classes
|
|
301
|
-
// to keep temporary PointerValues on the call stack and avoid extra memory allocations.
|
|
302
|
-
// In these cases, it is assumed that it holds a napi_value instead of napi_ext_ref.
|
|
303
|
-
struct NapiPointerValueView : PointerValue {
|
|
304
|
-
NapiPointerValueView(const NapiJsiRuntime *runtime, void *valueOrRef) noexcept;
|
|
305
|
-
void invalidate() noexcept override;
|
|
306
|
-
|
|
307
|
-
NapiPointerValueView(const NapiPointerValueView &) = delete;
|
|
308
|
-
NapiPointerValueView &operator=(const NapiPointerValueView &) = delete;
|
|
309
|
-
|
|
310
|
-
virtual napi_value GetValue() const;
|
|
311
|
-
virtual napi_ext_ref GetRef() const;
|
|
312
|
-
const NapiJsiRuntime *GetRuntime() const noexcept;
|
|
313
|
-
|
|
314
|
-
private:
|
|
315
|
-
const NapiJsiRuntime *m_runtime;
|
|
316
|
-
void *m_valueOrRef; // napi_value or napi_ext_ref
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
// NapiPointerValue is used by facebook::jsi::Pointer class and must only be used for this purpose.
|
|
320
|
-
// Every instance of NapiPointerValue should be allocated on the heap and be used as an argument
|
|
321
|
-
// to the constructor of facebook::jsi::Pointer or one of its derived classes.
|
|
322
|
-
// facebook::jsi::Pointer makes sure that the invalidate() method, which frees the heap-allocated NapiPointerValue,
|
|
323
|
-
// is called upon destruction.
|
|
324
|
-
// Since the the constructor of facebook::jsi::Pointer is protected, we usually have to invoke it through
|
|
325
|
-
// facebook::jsi::Runtime::make. The code should look something like:
|
|
326
|
-
//
|
|
327
|
-
// make<Pointer>(new NapiPointerValue(...));
|
|
328
|
-
//
|
|
329
|
-
// or you can use the helper function MakePointer().
|
|
330
|
-
struct NapiPointerValue final : NapiPointerValueView {
|
|
331
|
-
NapiPointerValue(const NapiJsiRuntime *runtime, napi_ext_ref ref);
|
|
332
|
-
NapiPointerValue(const NapiJsiRuntime *runtime, napi_value value);
|
|
333
|
-
void invalidate() noexcept override;
|
|
334
|
-
|
|
335
|
-
napi_value GetValue() const override;
|
|
336
|
-
napi_ext_ref GetRef() const override;
|
|
337
|
-
|
|
338
|
-
private:
|
|
339
|
-
// ~NapiPointerValue() must only be invoked by invalidate(). Hence, we make it private.
|
|
340
|
-
~NapiPointerValue() noexcept override;
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
// SmallBuffer keeps InplaceSize elements in place in the class, and uses heap memory for more elements.
|
|
344
|
-
template <typename T, size_t InplaceSize>
|
|
345
|
-
struct SmallBuffer {
|
|
346
|
-
SmallBuffer(size_t size) noexcept;
|
|
347
|
-
T *Data() noexcept;
|
|
348
|
-
size_t Size() const noexcept;
|
|
349
|
-
|
|
350
|
-
private:
|
|
351
|
-
size_t m_size{};
|
|
352
|
-
std::array<T, InplaceSize> m_stackData{};
|
|
353
|
-
std::unique_ptr<T[]> m_heapData{};
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
// The number of arguments that we keep on stack. We use heap if we have more arguments.
|
|
357
|
-
constexpr static size_t MaxStackArgCount = 8;
|
|
358
|
-
|
|
359
|
-
// NapiValueArgs helps optimize passing arguments to NAPI functions.
|
|
360
|
-
// If number of arguments is below or equal to MaxStackArgCount, they are kept on the call stack,
|
|
361
|
-
// otherwise arguments are allocated on the heap.
|
|
362
|
-
struct NapiValueArgs {
|
|
363
|
-
NapiValueArgs(NapiJsiRuntime &runtime, span<const facebook::jsi::Value> args);
|
|
364
|
-
operator span<napi_value>();
|
|
365
|
-
|
|
366
|
-
private:
|
|
367
|
-
SmallBuffer<napi_value, MaxStackArgCount> m_args;
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
// Helps to use the stack storage for a temporary conversion from napi_value to facebook::jsi::Value.
|
|
371
|
-
// It also helps to avoid a conversion to a relatively expensive napi_ext_ref.
|
|
372
|
-
struct JsiValueView {
|
|
373
|
-
JsiValueView(NapiJsiRuntime *runtime, napi_value jsValue);
|
|
374
|
-
operator const facebook::jsi::Value &() const noexcept;
|
|
375
|
-
|
|
376
|
-
using StoreType = std::aligned_storage_t<sizeof(NapiPointerValueView)>;
|
|
377
|
-
static facebook::jsi::Value InitValue(NapiJsiRuntime *runtime, napi_value jsValue, StoreType *store);
|
|
378
|
-
|
|
379
|
-
private:
|
|
380
|
-
StoreType m_pointerStore{};
|
|
381
|
-
facebook::jsi::Value m_value{};
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
// Helps to use stack storage for passing arguments that must be temporarily converted
|
|
385
|
-
// from napi_value to facebook::jsi::value.
|
|
386
|
-
// It helps to avoid conversion to a relatively expensive napi_ext_ref.
|
|
387
|
-
struct JsiValueViewArgs {
|
|
388
|
-
JsiValueViewArgs(NapiJsiRuntime *runtime, span<napi_value> args) noexcept;
|
|
389
|
-
const facebook::jsi::Value *Data() noexcept;
|
|
390
|
-
size_t Size() const noexcept;
|
|
391
|
-
|
|
392
|
-
private:
|
|
393
|
-
SmallBuffer<JsiValueView::StoreType, MaxStackArgCount> m_pointerStore{0};
|
|
394
|
-
SmallBuffer<facebook::jsi::Value, MaxStackArgCount> m_args{0};
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
// Helps to use stack storage for a temporary conversion from napi_value to facebook::jsi::PropNameID.
|
|
398
|
-
// It helps to avoid conversions to a relatively expensive napi_ext_ref.
|
|
399
|
-
struct PropNameIDView {
|
|
400
|
-
PropNameIDView(NapiJsiRuntime *runtime, napi_value propertyId) noexcept;
|
|
401
|
-
operator const facebook::jsi::PropNameID &() const noexcept;
|
|
402
|
-
|
|
403
|
-
using StoreType = std::aligned_storage_t<sizeof(NapiPointerValueView)>;
|
|
404
|
-
|
|
405
|
-
private:
|
|
406
|
-
StoreType m_pointerStore{};
|
|
407
|
-
facebook::jsi::PropNameID const m_propertyId;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
// Wraps up the facebook::jsi::HostFunctionType along with the NapiJsiRuntime.
|
|
411
|
-
struct HostFunctionWrapper {
|
|
412
|
-
HostFunctionWrapper(facebook::jsi::HostFunctionType &&hostFunction, NapiJsiRuntime &runtime);
|
|
413
|
-
|
|
414
|
-
// Does not suppor copying.
|
|
415
|
-
HostFunctionWrapper(const HostFunctionWrapper &) = delete;
|
|
416
|
-
HostFunctionWrapper &operator=(const HostFunctionWrapper &) = delete;
|
|
417
|
-
|
|
418
|
-
facebook::jsi::HostFunctionType &GetHostFunction() noexcept;
|
|
419
|
-
NapiJsiRuntime &GetRuntime() noexcept;
|
|
420
|
-
|
|
421
|
-
private:
|
|
422
|
-
facebook::jsi::HostFunctionType m_hostFunction;
|
|
423
|
-
NapiJsiRuntime &m_runtime;
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
// Packs the source buffer and the byte code together.
|
|
427
|
-
struct NapiPreparedJavaScript final : facebook::jsi::PreparedJavaScript {
|
|
428
|
-
NapiPreparedJavaScript(
|
|
429
|
-
std::unique_ptr<const facebook::jsi::Buffer> serializedBuffer,
|
|
430
|
-
const std::shared_ptr<const facebook::jsi::Buffer> &sourceBuffer,
|
|
431
|
-
std::string sourceUrl);
|
|
432
|
-
|
|
433
|
-
const facebook::jsi::Buffer &SerializedBuffer() const;
|
|
434
|
-
const facebook::jsi::Buffer &SourceBuffer() const;
|
|
435
|
-
const std::string &SourceUrl() const;
|
|
436
|
-
|
|
437
|
-
private:
|
|
438
|
-
std::unique_ptr<const facebook::jsi::Buffer> m_serializedBuffer;
|
|
439
|
-
std::shared_ptr<const facebook::jsi::Buffer> m_sourceBuffer;
|
|
440
|
-
std::string m_sourceUrl;
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
// Implements facebook::jsi::Buffer using std::vector<uint8_t>.
|
|
444
|
-
struct VectorBuffer final : facebook::jsi::Buffer {
|
|
445
|
-
VectorBuffer(std::vector<uint8_t> data);
|
|
446
|
-
uint8_t const *data() const override;
|
|
447
|
-
size_t size() const override;
|
|
448
|
-
|
|
449
|
-
private:
|
|
450
|
-
std::vector<uint8_t> m_data;
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
private: // Error-handling utility methods
|
|
454
|
-
[[noreturn]] void ThrowJsException(napi_status errorCode) const;
|
|
455
|
-
[[noreturn]] void ThrowNativeException(char const *errorMessage) const;
|
|
456
|
-
void RewriteErrorMessage(napi_value jsError) const;
|
|
457
|
-
template <typename TLambda>
|
|
458
|
-
auto RunInMethodContext(char const *methodName, TLambda lambda);
|
|
459
|
-
template <typename TLambda>
|
|
460
|
-
napi_value HandleCallbackExceptions(TLambda lambda) const noexcept;
|
|
461
|
-
bool SetException(napi_value error) const noexcept;
|
|
462
|
-
bool SetException(std::string_view message) const noexcept;
|
|
463
|
-
|
|
464
|
-
private: // Shared NAPI call helpers
|
|
465
|
-
napi_value RunScript(napi_value script, const char *sourceUrl);
|
|
466
|
-
napi_value RunScriptBuffer(const std::shared_ptr<const facebook::jsi::Buffer> &buffer, const char *sourceUrl);
|
|
467
|
-
std::vector<uint8_t> SerializeScript(napi_value script, const char *sourceUrl);
|
|
468
|
-
napi_value RunSerializedScript(span<const uint8_t> serialized, napi_value source, const char *sourceUrl);
|
|
469
|
-
napi_ext_ref CreateReference(napi_value value) const;
|
|
470
|
-
void ReleaseReference(napi_ext_ref ref) const;
|
|
471
|
-
napi_value GetReferenceValue(napi_ext_ref ref) const;
|
|
472
|
-
napi_valuetype TypeOf(napi_value value) const;
|
|
473
|
-
bool StrictEquals(napi_value left, napi_value right) const;
|
|
474
|
-
napi_value GetUndefined() const;
|
|
475
|
-
napi_value GetNull() const;
|
|
476
|
-
napi_value GetGlobal() const;
|
|
477
|
-
napi_value GetBoolean(bool value) const;
|
|
478
|
-
bool GetValueBool(napi_value value) const;
|
|
479
|
-
napi_value CreateInt32(int32_t value) const;
|
|
480
|
-
napi_value CreateDouble(double value) const;
|
|
481
|
-
double GetValueDouble(napi_value value) const;
|
|
482
|
-
napi_value CreateStringLatin1(std::string_view value) const;
|
|
483
|
-
napi_value CreateStringUtf8(std::string_view value) const;
|
|
484
|
-
napi_value CreateStringUtf8(const uint8_t *data, size_t length) const;
|
|
485
|
-
std::string StringToStdString(napi_value stringValue) const;
|
|
486
|
-
napi_ext_ref GetPropertyIdFromName(std::string_view value) const;
|
|
487
|
-
napi_ext_ref GetPropertyIdFromName(const uint8_t *data, size_t length) const;
|
|
488
|
-
napi_ext_ref GetPropertyIdFromName(napi_value str) const;
|
|
489
|
-
napi_ext_ref GetPropertyIdFromSymbol(napi_value sym) const;
|
|
490
|
-
std::string PropertyIdToStdString(napi_value propertyId);
|
|
491
|
-
napi_value CreateSymbol(std::string_view symbolDescription) const;
|
|
492
|
-
std::string SymbolToStdString(napi_value symbolValue);
|
|
493
|
-
napi_value CallFunction(napi_value thisArg, napi_value function, span<napi_value> args = {}) const;
|
|
494
|
-
napi_value ConstructObject(napi_value constructor, span<napi_value> args = {}) const;
|
|
495
|
-
bool InstanceOf(napi_value object, napi_value constructor) const;
|
|
496
|
-
napi_value CreateObject() const;
|
|
497
|
-
bool HasProperty(napi_value object, napi_value propertyId) const;
|
|
498
|
-
napi_value GetProperty(napi_value object, napi_value propertyId) const;
|
|
499
|
-
void SetProperty(napi_value object, napi_value propertyId, napi_value value) const;
|
|
500
|
-
void SetProperty(napi_value object, napi_value propertyId, napi_value value, napi_property_attributes attrs) const;
|
|
501
|
-
napi_value CreateArray(size_t length) const;
|
|
502
|
-
void SetElement(napi_value array, uint32_t index, napi_value value) const;
|
|
503
|
-
static napi_value __cdecl JsiHostFunctionCallback(napi_env env, napi_callback_info info) noexcept;
|
|
504
|
-
napi_value CreateExternalFunction(napi_value name, int32_t paramCount, napi_callback callback, void *callbackData);
|
|
505
|
-
napi_value CreateExternalObject(void *data, napi_finalize finalizeCallback) const;
|
|
506
|
-
template <typename T>
|
|
507
|
-
napi_value CreateExternalObject(std::unique_ptr<T> &&data) const;
|
|
508
|
-
void *GetExternalData(napi_value object) const;
|
|
509
|
-
const std::shared_ptr<facebook::jsi::HostObject> &GetJsiHostObject(napi_value obj);
|
|
510
|
-
napi_value GetHostObjectProxyHandler();
|
|
511
|
-
template <napi_value (NapiJsiRuntime::*trapMethod)(span<napi_value>), size_t argCount>
|
|
512
|
-
void SetProxyTrap(napi_value handler, napi_value propertyName);
|
|
513
|
-
napi_value HostObjectGetTrap(span<napi_value> args);
|
|
514
|
-
napi_value HostObjectSetTrap(span<napi_value> args);
|
|
515
|
-
napi_value HostObjectOwnKeysTrap(span<napi_value> args);
|
|
516
|
-
napi_value HostObjectGetOwnPropertyDescriptorTrap(span<napi_value> args);
|
|
517
|
-
|
|
518
|
-
private: // Miscellaneous utility methods
|
|
519
|
-
span<const uint8_t> ToSpan(const facebook::jsi::Buffer &buffer);
|
|
520
|
-
facebook::jsi::Value ToJsiValue(napi_value value) const;
|
|
521
|
-
napi_value GetNapiValue(const facebook::jsi::Value &value) const;
|
|
522
|
-
static NapiPointerValue *CloneNapiPointerValue(const PointerValue *pointerValue);
|
|
523
|
-
static napi_value GetNapiValue(const facebook::jsi::Pointer &p);
|
|
524
|
-
static napi_ext_ref GetNapiRef(const facebook::jsi::Pointer &p);
|
|
525
|
-
|
|
526
|
-
template <typename T, typename TValue, std::enable_if_t<std::is_base_of_v<facebook::jsi::Pointer, T>, int> = 0>
|
|
527
|
-
T MakePointer(TValue value) const;
|
|
528
|
-
|
|
529
|
-
private: // Fields
|
|
530
|
-
EnvHolder m_env;
|
|
531
|
-
|
|
532
|
-
// Property ID cache to improve execution speed.
|
|
533
|
-
struct PropertyId {
|
|
534
|
-
NapiRefHolder Error;
|
|
535
|
-
NapiRefHolder Object;
|
|
536
|
-
NapiRefHolder Proxy;
|
|
537
|
-
NapiRefHolder Symbol;
|
|
538
|
-
NapiRefHolder byteLength;
|
|
539
|
-
NapiRefHolder configurable;
|
|
540
|
-
NapiRefHolder enumerable;
|
|
541
|
-
NapiRefHolder get;
|
|
542
|
-
NapiRefHolder getOwnPropertyDescriptor;
|
|
543
|
-
NapiRefHolder hostFunctionSymbol;
|
|
544
|
-
NapiRefHolder hostObjectSymbol;
|
|
545
|
-
NapiRefHolder length;
|
|
546
|
-
NapiRefHolder message;
|
|
547
|
-
NapiRefHolder ownKeys;
|
|
548
|
-
NapiRefHolder propertyIsEnumerable;
|
|
549
|
-
NapiRefHolder prototype;
|
|
550
|
-
NapiRefHolder set;
|
|
551
|
-
NapiRefHolder toString;
|
|
552
|
-
NapiRefHolder value;
|
|
553
|
-
NapiRefHolder writable;
|
|
554
|
-
} m_propertyId;
|
|
555
|
-
|
|
556
|
-
// Cache of commonly used values.
|
|
557
|
-
struct CachedValue final {
|
|
558
|
-
NapiRefHolder Error;
|
|
559
|
-
NapiRefHolder Global;
|
|
560
|
-
NapiRefHolder False;
|
|
561
|
-
NapiRefHolder HostObjectProxyHandler;
|
|
562
|
-
NapiRefHolder Null;
|
|
563
|
-
NapiRefHolder ProxyConstructor;
|
|
564
|
-
NapiRefHolder SymbolToString;
|
|
565
|
-
NapiRefHolder True;
|
|
566
|
-
NapiRefHolder Undefined;
|
|
567
|
-
} m_value;
|
|
568
|
-
|
|
569
|
-
bool m_pendingJSError{false};
|
|
570
|
-
};
|
|
571
|
-
} // namespace
|
|
572
|
-
|
|
573
|
-
} // namespace Microsoft::JSI
|
|
574
|
-
|
|
575
|
-
#pragma endregion Declarations
|
|
576
|
-
|
|
577
|
-
using namespace facebook::jsi;
|
|
578
|
-
using namespace std::string_view_literals;
|
|
579
|
-
|
|
580
|
-
using std::shared_ptr;
|
|
581
|
-
using std::string;
|
|
582
|
-
using std::string_view;
|
|
583
|
-
using std::unique_ptr;
|
|
584
|
-
using std::vector;
|
|
585
|
-
|
|
586
|
-
namespace Microsoft::JSI {
|
|
587
|
-
|
|
588
|
-
namespace {
|
|
589
|
-
constexpr char s_description[] = "NapiJsiRuntime";
|
|
590
|
-
|
|
591
|
-
constexpr char s_refHolderError[] = "Error";
|
|
592
|
-
constexpr char s_refHolderObject[] = "Object";
|
|
593
|
-
constexpr char s_refHolderProxy[] = "Proxy";
|
|
594
|
-
constexpr char s_refHolderSymbol[] = "Symbol";
|
|
595
|
-
constexpr char s_refHolderByteLength[] = "byteLength";
|
|
596
|
-
constexpr char s_refHolderConfigurable[] = "configurable";
|
|
597
|
-
constexpr char s_refHolderEnumerable[] = "enumerable";
|
|
598
|
-
constexpr char s_refHolderGet[] = "get";
|
|
599
|
-
constexpr char s_refHolderGetOwnPropertyDescriptor[] = "getOwnPropertyDescriptor";
|
|
600
|
-
constexpr char s_refHolderHostFunctionSymbol[] = "hostFunctionSymbol";
|
|
601
|
-
constexpr char s_refHolderHostObjectSymbol[] = "hostObjectSymbol";
|
|
602
|
-
constexpr char s_refHolderLength[] = "length";
|
|
603
|
-
constexpr char s_refHolderMessage[] = "message";
|
|
604
|
-
constexpr char s_refHolderOwnKeys[] = "ownKeys";
|
|
605
|
-
constexpr char s_refHolderPropertyIsEnumerable[] = "propertyIsEnumerable";
|
|
606
|
-
constexpr char s_refHolderPrototype[] = "prototype";
|
|
607
|
-
constexpr char s_refHolderSet[] = "set";
|
|
608
|
-
constexpr char s_refHolderToString[] = "toString";
|
|
609
|
-
constexpr char s_refHolderValue[] = "value";
|
|
610
|
-
constexpr char s_refHolderWritable[] = "writable";
|
|
611
|
-
|
|
612
|
-
#pragma region NapiJsiRuntime
|
|
613
|
-
|
|
614
|
-
NapiJsiRuntime::NapiJsiRuntime(napi_env env) noexcept : m_env{env} {
|
|
615
|
-
EnvScope scope{m_env};
|
|
616
|
-
m_propertyId.Error = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderError)};
|
|
617
|
-
m_propertyId.Object = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderObject)};
|
|
618
|
-
m_propertyId.Proxy = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderProxy)};
|
|
619
|
-
m_propertyId.Symbol = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderSymbol)};
|
|
620
|
-
m_propertyId.byteLength = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderByteLength)};
|
|
621
|
-
m_propertyId.configurable = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderConfigurable)};
|
|
622
|
-
m_propertyId.enumerable = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderEnumerable)};
|
|
623
|
-
m_propertyId.get = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderGet)};
|
|
624
|
-
m_propertyId.getOwnPropertyDescriptor =
|
|
625
|
-
NapiRefHolder{this, GetPropertyIdFromName(s_refHolderGetOwnPropertyDescriptor)};
|
|
626
|
-
m_propertyId.hostFunctionSymbol = NapiRefHolder{this, CreateSymbol(s_refHolderHostFunctionSymbol)};
|
|
627
|
-
m_propertyId.hostObjectSymbol = NapiRefHolder{this, CreateSymbol(s_refHolderHostObjectSymbol)};
|
|
628
|
-
m_propertyId.length = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderLength)};
|
|
629
|
-
m_propertyId.message = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderMessage)};
|
|
630
|
-
m_propertyId.ownKeys = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderOwnKeys)};
|
|
631
|
-
m_propertyId.propertyIsEnumerable = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderPropertyIsEnumerable)};
|
|
632
|
-
m_propertyId.prototype = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderPrototype)};
|
|
633
|
-
m_propertyId.set = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderSet)};
|
|
634
|
-
m_propertyId.toString = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderToString)};
|
|
635
|
-
m_propertyId.value = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderValue)};
|
|
636
|
-
m_propertyId.writable = NapiRefHolder{this, GetPropertyIdFromName(s_refHolderWritable)};
|
|
637
|
-
|
|
638
|
-
m_value.Undefined = NapiRefHolder{this, GetUndefined()};
|
|
639
|
-
m_value.Null = NapiRefHolder{this, GetNull()};
|
|
640
|
-
m_value.True = NapiRefHolder{this, GetBoolean(true)};
|
|
641
|
-
m_value.False = NapiRefHolder{this, GetBoolean(false)};
|
|
642
|
-
m_value.Global = NapiRefHolder{this, GetGlobal()};
|
|
643
|
-
m_value.Error = NapiRefHolder{this, GetProperty(m_value.Global, m_propertyId.Error)};
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
Value NapiJsiRuntime::evaluateJavaScript(const shared_ptr<const Buffer> &buffer, const string &sourceUrl) {
|
|
647
|
-
EnvScope envScope{m_env};
|
|
648
|
-
napi_value result = RunScriptBuffer(buffer, sourceUrl.c_str());
|
|
649
|
-
return ToJsiValue(result);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
shared_ptr<const PreparedJavaScript> NapiJsiRuntime::prepareJavaScript(
|
|
653
|
-
const shared_ptr<const Buffer> &sourceBuffer,
|
|
654
|
-
string sourceUrl) {
|
|
655
|
-
EnvScope scope{m_env};
|
|
656
|
-
napi_value source = CreateStringUtf8(sourceBuffer->data(), sourceBuffer->size());
|
|
657
|
-
vector<uint8_t> serialized = SerializeScript(source, sourceUrl.c_str());
|
|
658
|
-
unique_ptr<const Buffer> serializedBuffer{new VectorBuffer{std::move(serialized)}};
|
|
659
|
-
|
|
660
|
-
return std::make_shared<NapiPreparedJavaScript>(std::move(serializedBuffer), sourceBuffer, std::move(sourceUrl));
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
Value NapiJsiRuntime::evaluatePreparedJavaScript(const shared_ptr<const PreparedJavaScript> &preparedJs) {
|
|
664
|
-
EnvScope scope{m_env};
|
|
665
|
-
auto preparedScript = static_cast<const NapiPreparedJavaScript *>(preparedJs.get());
|
|
666
|
-
napi_value source = CreateStringUtf8(preparedScript->SourceBuffer().data(), preparedScript->SourceBuffer().size());
|
|
667
|
-
napi_value result =
|
|
668
|
-
RunSerializedScript(ToSpan(preparedScript->SerializedBuffer()), source, preparedScript->SourceUrl().c_str());
|
|
669
|
-
|
|
670
|
-
return ToJsiValue(result);
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
bool NapiJsiRuntime::drainMicrotasks(int /*maxMicrotasksHint*/) {
|
|
674
|
-
return true;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
Object NapiJsiRuntime::global() {
|
|
678
|
-
EnvScope scope{m_env};
|
|
679
|
-
|
|
680
|
-
return MakePointer<Object>(m_value.Global.CloneRef());
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
string NapiJsiRuntime::description() {
|
|
684
|
-
return s_description;
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
bool NapiJsiRuntime::isInspectable() {
|
|
688
|
-
return false;
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
Runtime::PointerValue *NapiJsiRuntime::cloneSymbol(const Runtime::PointerValue *pointerValue) {
|
|
692
|
-
EnvScope scope{m_env};
|
|
693
|
-
|
|
694
|
-
return CloneNapiPointerValue(pointerValue);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
Runtime::PointerValue *NapiJsiRuntime::cloneBigInt(const Runtime::PointerValue *pointerValue) {
|
|
698
|
-
EnvScope scope{m_env};
|
|
699
|
-
|
|
700
|
-
return CloneNapiPointerValue(pointerValue);
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
Runtime::PointerValue *NapiJsiRuntime::cloneString(const Runtime::PointerValue *pointerValue) {
|
|
704
|
-
EnvScope scope{m_env};
|
|
705
|
-
|
|
706
|
-
return CloneNapiPointerValue(pointerValue);
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
Runtime::PointerValue *NapiJsiRuntime::cloneObject(const Runtime::PointerValue *pointerValue) {
|
|
710
|
-
EnvScope scope{m_env};
|
|
711
|
-
|
|
712
|
-
return CloneNapiPointerValue(pointerValue);
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
Runtime::PointerValue *NapiJsiRuntime::clonePropNameID(const Runtime::PointerValue *pointerValue) {
|
|
716
|
-
EnvScope scope{m_env};
|
|
717
|
-
|
|
718
|
-
return CloneNapiPointerValue(pointerValue);
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
PropNameID NapiJsiRuntime::createPropNameIDFromAscii(const char *str, size_t length) {
|
|
722
|
-
EnvScope scope{m_env};
|
|
723
|
-
napi_value napiStr = CreateStringLatin1({str, length});
|
|
724
|
-
napi_ext_ref uniqueStr = GetPropertyIdFromName(napiStr);
|
|
725
|
-
|
|
726
|
-
return MakePointer<PropNameID>(uniqueStr);
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
PropNameID NapiJsiRuntime::createPropNameIDFromUtf8(const uint8_t *utf8, size_t length) {
|
|
730
|
-
EnvScope scope{m_env};
|
|
731
|
-
napi_ext_ref uniqueStr = GetPropertyIdFromName(utf8, length);
|
|
732
|
-
|
|
733
|
-
return MakePointer<PropNameID>(uniqueStr);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
PropNameID NapiJsiRuntime::createPropNameIDFromString(const String &str) {
|
|
737
|
-
EnvScope scope{m_env};
|
|
738
|
-
napi_ext_ref uniqueStr = GetPropertyIdFromName(GetNapiValue(str));
|
|
739
|
-
|
|
740
|
-
return MakePointer<PropNameID>(uniqueStr);
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
PropNameID NapiJsiRuntime::createPropNameIDFromSymbol(const Symbol &sym) {
|
|
744
|
-
EnvScope envScope{m_env};
|
|
745
|
-
napi_ext_ref propSym = GetPropertyIdFromSymbol(GetNapiValue(sym));
|
|
746
|
-
return MakePointer<PropNameID>(propSym);
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
facebook::jsi::BigInt NapiJsiRuntime::createBigIntFromInt64(int64_t val) {
|
|
750
|
-
EnvScope scope{m_env};
|
|
751
|
-
throw JSINativeException("Not implemented");
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
facebook::jsi::BigInt NapiJsiRuntime::createBigIntFromUint64(uint64_t val) {
|
|
755
|
-
EnvScope scope{m_env};
|
|
756
|
-
throw JSINativeException("Not implemented");
|
|
757
|
-
;
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
bool NapiJsiRuntime::bigintIsInt64(const facebook::jsi::BigInt &) {
|
|
761
|
-
EnvScope scope{m_env};
|
|
762
|
-
throw JSINativeException("Not implemented");
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
bool NapiJsiRuntime::bigintIsUint64(const facebook::jsi::BigInt &) {
|
|
766
|
-
EnvScope scope{m_env};
|
|
767
|
-
throw JSINativeException("Not implemented");
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
uint64_t NapiJsiRuntime::truncate(const facebook::jsi::BigInt &) {
|
|
771
|
-
EnvScope scope{m_env};
|
|
772
|
-
throw JSINativeException("Not implemented");
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
String NapiJsiRuntime::bigintToString(const facebook::jsi::BigInt &, int) {
|
|
776
|
-
EnvScope scope{m_env};
|
|
777
|
-
throw JSINativeException("Not implemented");
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
bool NapiJsiRuntime::hasNativeState(const facebook::jsi::Object &) {
|
|
781
|
-
EnvScope scope{m_env};
|
|
782
|
-
throw JSINativeException("Not implemented");
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
std::shared_ptr<facebook::jsi::NativeState> NapiJsiRuntime::getNativeState(const facebook::jsi::Object &) {
|
|
786
|
-
EnvScope scope{m_env};
|
|
787
|
-
throw JSINativeException("Not implemented");
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
void NapiJsiRuntime::setNativeState(const Object &, std::shared_ptr<facebook::jsi::NativeState> state) {
|
|
791
|
-
EnvScope scope{m_env};
|
|
792
|
-
throw JSINativeException("Not implemented");
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
facebook::jsi::ArrayBuffer NapiJsiRuntime::createArrayBuffer(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) {
|
|
796
|
-
EnvScope scope{m_env};
|
|
797
|
-
throw JSINativeException("Not implemented");
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
string NapiJsiRuntime::utf8(const PropNameID &id) {
|
|
801
|
-
EnvScope scope{m_env};
|
|
802
|
-
|
|
803
|
-
return PropertyIdToStdString(GetNapiValue(id));
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
bool NapiJsiRuntime::compare(const PropNameID &lhs, const PropNameID &rhs) {
|
|
807
|
-
EnvScope scope{m_env};
|
|
808
|
-
|
|
809
|
-
return StrictEquals(GetNapiValue(lhs), GetNapiValue(rhs));
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
string NapiJsiRuntime::symbolToString(const Symbol &s) {
|
|
813
|
-
EnvScope scope{m_env};
|
|
814
|
-
if (!m_value.SymbolToString) {
|
|
815
|
-
napi_value symbolCtor = GetProperty(m_value.Global, m_propertyId.Symbol);
|
|
816
|
-
napi_value symbolPrototype = GetProperty(symbolCtor, m_propertyId.prototype);
|
|
817
|
-
m_value.SymbolToString = NapiRefHolder{this, GetProperty(symbolPrototype, m_propertyId.toString)};
|
|
818
|
-
}
|
|
819
|
-
napi_value jsString = CallFunction(GetNapiValue(s), m_value.SymbolToString, {});
|
|
820
|
-
|
|
821
|
-
return StringToStdString(jsString);
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
String NapiJsiRuntime::createStringFromAscii(const char *str, size_t length) {
|
|
825
|
-
EnvScope scope{m_env};
|
|
826
|
-
|
|
827
|
-
return MakePointer<String>(CreateStringLatin1({str, length}));
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
String NapiJsiRuntime::createStringFromUtf8(const uint8_t *str, size_t length) {
|
|
831
|
-
EnvScope scope{m_env};
|
|
832
|
-
|
|
833
|
-
return MakePointer<String>(CreateStringUtf8(str, length));
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
string NapiJsiRuntime::utf8(const String &str) {
|
|
837
|
-
EnvScope scope{m_env};
|
|
838
|
-
|
|
839
|
-
return StringToStdString(GetNapiValue(str));
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
Object NapiJsiRuntime::createObject() {
|
|
843
|
-
EnvScope scope{m_env};
|
|
844
|
-
|
|
845
|
-
return MakePointer<Object>(CreateObject());
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
Object NapiJsiRuntime::createObject(shared_ptr<HostObject> hostObject) {
|
|
849
|
-
// The hostObjectHolder keeps the hostObject as external data.
|
|
850
|
-
// Then, the hostObjectHolder is wrapped up by a Proxy object to provide access
|
|
851
|
-
// to the hostObject's get, set and getPropertyNames methods.
|
|
852
|
-
// There is a special symbol property ID, 'hostObjectSymbol', used to access the hostObjectWrapper from the Proxy.
|
|
853
|
-
EnvScope scope{m_env};
|
|
854
|
-
napi_value hostObjectHolder = CreateExternalObject(std::make_unique<shared_ptr<HostObject>>(std::move(hostObject)));
|
|
855
|
-
napi_value obj = CreateObject();
|
|
856
|
-
SetProperty(obj, m_propertyId.hostObjectSymbol, hostObjectHolder);
|
|
857
|
-
if (!m_value.ProxyConstructor) {
|
|
858
|
-
m_value.ProxyConstructor = NapiRefHolder{this, GetProperty(m_value.Global, m_propertyId.Proxy)};
|
|
859
|
-
}
|
|
860
|
-
napi_value proxy = ConstructObject(m_value.ProxyConstructor, {obj, GetHostObjectProxyHandler()});
|
|
861
|
-
|
|
862
|
-
return MakePointer<Object>(proxy);
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
shared_ptr<HostObject> NapiJsiRuntime::getHostObject(const Object &obj) {
|
|
866
|
-
EnvScope scope{m_env};
|
|
867
|
-
|
|
868
|
-
return GetJsiHostObject(GetNapiValue(obj));
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
HostFunctionType &NapiJsiRuntime::getHostFunction(const Function &func) {
|
|
872
|
-
EnvScope scope{m_env};
|
|
873
|
-
napi_value hostFunctionHolder = GetProperty(GetNapiValue(func), m_propertyId.hostFunctionSymbol);
|
|
874
|
-
if (TypeOf(hostFunctionHolder) == napi_valuetype::napi_external) {
|
|
875
|
-
return static_cast<HostFunctionWrapper *>(GetExternalData(hostFunctionHolder))->GetHostFunction();
|
|
876
|
-
} else {
|
|
877
|
-
throw JSINativeException("getHostFunction() can only be called with HostFunction.");
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
Value NapiJsiRuntime::getProperty(const Object &obj, const PropNameID &name) {
|
|
882
|
-
EnvScope scope{m_env};
|
|
883
|
-
|
|
884
|
-
return ToJsiValue(GetProperty(GetNapiValue(obj), GetNapiValue(name)));
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
Value NapiJsiRuntime::getProperty(const Object &obj, const String &name) {
|
|
888
|
-
EnvScope scope{m_env};
|
|
889
|
-
|
|
890
|
-
return ToJsiValue(GetProperty(GetNapiValue(obj), GetNapiValue(name)));
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
bool NapiJsiRuntime::hasProperty(const Object &obj, const PropNameID &name) {
|
|
894
|
-
EnvScope scope{m_env};
|
|
895
|
-
|
|
896
|
-
return HasProperty(GetNapiValue(obj), GetNapiValue(name));
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
bool NapiJsiRuntime::hasProperty(const Object &obj, const String &name) {
|
|
900
|
-
EnvScope scope{m_env};
|
|
901
|
-
|
|
902
|
-
return HasProperty(GetNapiValue(obj), GetNapiValue(name));
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
void NapiJsiRuntime::setPropertyValue(const Object &obj, const PropNameID &name, const Value &value) {
|
|
906
|
-
EnvScope scope{m_env};
|
|
907
|
-
|
|
908
|
-
SetProperty(GetNapiValue(obj), GetNapiValue(name), GetNapiValue(value));
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
void NapiJsiRuntime::setPropertyValue(const Object &obj, const String &name, const Value &value) {
|
|
912
|
-
EnvScope scope{m_env};
|
|
913
|
-
|
|
914
|
-
SetProperty(GetNapiValue(obj), GetNapiValue(name), GetNapiValue(value));
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
bool NapiJsiRuntime::isArray(const Object &obj) const {
|
|
918
|
-
EnvScope scope{m_env};
|
|
919
|
-
bool result{};
|
|
920
|
-
CHECK_NAPI(napi_is_array(m_env, GetNapiValue(obj), &result));
|
|
921
|
-
|
|
922
|
-
return result;
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
bool NapiJsiRuntime::isArrayBuffer(const Object &obj) const {
|
|
926
|
-
EnvScope scope{m_env};
|
|
927
|
-
bool result{};
|
|
928
|
-
CHECK_NAPI(napi_is_arraybuffer(m_env, GetNapiValue(obj), &result));
|
|
929
|
-
|
|
930
|
-
return result;
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
bool NapiJsiRuntime::isFunction(const Object &obj) const {
|
|
934
|
-
EnvScope scope{m_env};
|
|
935
|
-
|
|
936
|
-
return TypeOf(GetNapiValue(obj)) == napi_valuetype::napi_function;
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
bool NapiJsiRuntime::isHostObject(const Object &obj) const {
|
|
940
|
-
EnvScope scope{m_env};
|
|
941
|
-
napi_value hostObjectHolder = GetProperty(GetNapiValue(obj), m_propertyId.hostObjectSymbol);
|
|
942
|
-
|
|
943
|
-
if (TypeOf(hostObjectHolder) == napi_valuetype::napi_external) {
|
|
944
|
-
return GetExternalData(hostObjectHolder) != nullptr;
|
|
945
|
-
} else {
|
|
946
|
-
return false;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
bool NapiJsiRuntime::isHostFunction(const Function &func) const {
|
|
951
|
-
EnvScope scope{m_env};
|
|
952
|
-
napi_value hostFunctionHolder = GetProperty(GetNapiValue(func), m_propertyId.hostFunctionSymbol);
|
|
953
|
-
|
|
954
|
-
if (TypeOf(hostFunctionHolder) == napi_valuetype::napi_external) {
|
|
955
|
-
return GetExternalData(hostFunctionHolder) != nullptr;
|
|
956
|
-
} else {
|
|
957
|
-
return false;
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
Array NapiJsiRuntime::getPropertyNames(const Object &obj) {
|
|
962
|
-
EnvScope scope{m_env};
|
|
963
|
-
napi_value properties;
|
|
964
|
-
CHECK_NAPI(napi_get_all_property_names(
|
|
965
|
-
m_env,
|
|
966
|
-
GetNapiValue(obj),
|
|
967
|
-
napi_key_collection_mode::napi_key_include_prototypes,
|
|
968
|
-
napi_key_filter(napi_key_enumerable | napi_key_skip_symbols),
|
|
969
|
-
napi_key_numbers_to_strings,
|
|
970
|
-
&properties));
|
|
971
|
-
|
|
972
|
-
return MakePointer<Object>(properties).asArray(*this);
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
WeakObject NapiJsiRuntime::createWeakObject(const Object &obj) {
|
|
976
|
-
EnvScope scope{m_env};
|
|
977
|
-
napi_ext_ref weakRef{};
|
|
978
|
-
CHECK_NAPI(napi_ext_create_weak_reference(m_env, GetNapiValue(obj), &weakRef));
|
|
979
|
-
|
|
980
|
-
return MakePointer<WeakObject>(weakRef);
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
Value NapiJsiRuntime::lockWeakObject(const WeakObject &weakObject) {
|
|
984
|
-
EnvScope scope{m_env};
|
|
985
|
-
napi_value value = GetNapiValue(weakObject);
|
|
986
|
-
|
|
987
|
-
if (value) {
|
|
988
|
-
return ToJsiValue(value);
|
|
989
|
-
} else {
|
|
990
|
-
return Value::undefined();
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
Array NapiJsiRuntime::createArray(size_t length) {
|
|
995
|
-
EnvScope scope{m_env};
|
|
996
|
-
napi_value result{};
|
|
997
|
-
CHECK_NAPI(napi_create_array_with_length(m_env, length, &result));
|
|
998
|
-
|
|
999
|
-
return MakePointer<Object>(result).asArray(*this);
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
size_t NapiJsiRuntime::size(const Array &arr) {
|
|
1003
|
-
EnvScope scope{m_env};
|
|
1004
|
-
size_t result{};
|
|
1005
|
-
CHECK_NAPI(napi_get_array_length(m_env, GetNapiValue(arr), reinterpret_cast<uint32_t *>(&result)));
|
|
1006
|
-
|
|
1007
|
-
return result;
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
size_t NapiJsiRuntime::size(const ArrayBuffer &arrBuf) {
|
|
1011
|
-
EnvScope scope{m_env};
|
|
1012
|
-
size_t result{};
|
|
1013
|
-
CHECK_NAPI(napi_get_arraybuffer_info(m_env, GetNapiValue(arrBuf), nullptr, &result));
|
|
1014
|
-
|
|
1015
|
-
return result;
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
uint8_t *NapiJsiRuntime::data(const ArrayBuffer &arrBuf) {
|
|
1019
|
-
EnvScope scope{m_env};
|
|
1020
|
-
uint8_t *result{};
|
|
1021
|
-
CHECK_NAPI(napi_get_arraybuffer_info(m_env, GetNapiValue(arrBuf), reinterpret_cast<void **>(&result), nullptr));
|
|
1022
|
-
|
|
1023
|
-
return result;
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
Value NapiJsiRuntime::getValueAtIndex(const Array &arr, size_t index) {
|
|
1027
|
-
EnvScope scope{m_env};
|
|
1028
|
-
napi_value result{};
|
|
1029
|
-
CHECK_NAPI(napi_get_element(m_env, GetNapiValue(arr), static_cast<int32_t>(index), &result));
|
|
1030
|
-
|
|
1031
|
-
return ToJsiValue(result);
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
void NapiJsiRuntime::setValueAtIndexImpl(const Array &arr, size_t index, const Value &value) {
|
|
1035
|
-
EnvScope scope{m_env};
|
|
1036
|
-
|
|
1037
|
-
SetElement(GetNapiValue(arr), static_cast<uint32_t>(index), GetNapiValue(value));
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
Function
|
|
1041
|
-
NapiJsiRuntime::createFunctionFromHostFunction(const PropNameID &name, unsigned int paramCount, HostFunctionType func) {
|
|
1042
|
-
EnvScope scope{m_env};
|
|
1043
|
-
auto hostFunctionWrapper = std::make_unique<HostFunctionWrapper>(std::move(func), *this);
|
|
1044
|
-
napi_value function = CreateExternalFunction(
|
|
1045
|
-
GetNapiValue(name), static_cast<int32_t>(paramCount), JsiHostFunctionCallback, hostFunctionWrapper.get());
|
|
1046
|
-
|
|
1047
|
-
const napi_value hostFunctionHolder = CreateExternalObject(std::move(hostFunctionWrapper));
|
|
1048
|
-
SetProperty(function, m_propertyId.hostFunctionSymbol, hostFunctionHolder, napi_property_attributes::napi_default);
|
|
1049
|
-
|
|
1050
|
-
return MakePointer<Object>(function).getFunction(*this);
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
Value NapiJsiRuntime::call(const Function &func, const Value &jsThis, const Value *args, size_t count) {
|
|
1054
|
-
EnvScope scope{m_env};
|
|
1055
|
-
|
|
1056
|
-
return ToJsiValue(
|
|
1057
|
-
CallFunction(GetNapiValue(jsThis), GetNapiValue(func), NapiValueArgs(*this, span<const Value>(args, count))));
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
Value NapiJsiRuntime::callAsConstructor(const Function &func, const Value *args, size_t count) {
|
|
1061
|
-
EnvScope scope{m_env};
|
|
1062
|
-
|
|
1063
|
-
return ToJsiValue(ConstructObject(GetNapiValue(func), NapiValueArgs(*this, span<Value const>(args, count))));
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
Runtime::ScopeState *NapiJsiRuntime::pushScope() {
|
|
1067
|
-
EnvScope scope{m_env};
|
|
1068
|
-
napi_handle_scope result{};
|
|
1069
|
-
CHECK_NAPI(napi_open_handle_scope(m_env, &result));
|
|
1070
|
-
|
|
1071
|
-
return reinterpret_cast<Runtime::ScopeState *>(result);
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
void NapiJsiRuntime::popScope(Runtime::ScopeState *state) {
|
|
1075
|
-
EnvScope scope{m_env};
|
|
1076
|
-
|
|
1077
|
-
CHECK_NAPI(napi_close_handle_scope(m_env, reinterpret_cast<napi_handle_scope>(state)));
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
bool NapiJsiRuntime::strictEquals(const Symbol &a, const Symbol &b) const {
|
|
1081
|
-
EnvScope scope{m_env};
|
|
1082
|
-
|
|
1083
|
-
return StrictEquals(GetNapiValue(a), GetNapiValue(b));
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
bool NapiJsiRuntime::strictEquals(const BigInt &a, const BigInt &b) const {
|
|
1087
|
-
EnvScope scope{m_env};
|
|
1088
|
-
|
|
1089
|
-
return StrictEquals(GetNapiValue(a), GetNapiValue(b));
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
bool NapiJsiRuntime::strictEquals(const String &a, const String &b) const {
|
|
1093
|
-
EnvScope scope{m_env};
|
|
1094
|
-
|
|
1095
|
-
return StrictEquals(GetNapiValue(a), GetNapiValue(b));
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
bool NapiJsiRuntime::strictEquals(const Object &a, const Object &b) const {
|
|
1099
|
-
EnvScope scope{m_env};
|
|
1100
|
-
|
|
1101
|
-
return StrictEquals(GetNapiValue(a), GetNapiValue(b));
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
bool NapiJsiRuntime::instanceOf(const Object &obj, const Function &func) {
|
|
1105
|
-
EnvScope scope{m_env};
|
|
1106
|
-
|
|
1107
|
-
return InstanceOf(GetNapiValue(obj), GetNapiValue(func));
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
#pragma region EnvHolder
|
|
1111
|
-
|
|
1112
|
-
NapiJsiRuntime::EnvHolder::EnvHolder(napi_env env) noexcept : m_env{env} {}
|
|
1113
|
-
|
|
1114
|
-
NapiJsiRuntime::EnvHolder::~EnvHolder() noexcept {
|
|
1115
|
-
if (m_env) {
|
|
1116
|
-
CHECK_NAPI_ELSE_CRASH(napi_ext_env_unref(m_env));
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
NapiJsiRuntime::EnvHolder::operator napi_env() const noexcept {
|
|
1121
|
-
return m_env;
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
#pragma endregion EnvHolder
|
|
1125
|
-
|
|
1126
|
-
#pragma region EnvScope
|
|
1127
|
-
|
|
1128
|
-
NapiJsiRuntime::EnvScope::EnvScope(napi_env env) noexcept : m_env{env} {
|
|
1129
|
-
CHECK_NAPI_ELSE_CRASH(napi_ext_open_env_scope(m_env, &m_envScope));
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
NapiJsiRuntime::EnvScope::~EnvScope() noexcept {
|
|
1133
|
-
CHECK_NAPI_ELSE_CRASH(napi_ext_close_env_scope(m_env, m_envScope));
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
#pragma endregion EnvScope
|
|
1137
|
-
|
|
1138
|
-
#pragma region AutoRestore
|
|
1139
|
-
|
|
1140
|
-
template <typename T>
|
|
1141
|
-
NapiJsiRuntime::AutoRestore<T>::AutoRestore(T *var, T value) : m_var{var}, m_value{std::exchange(*var, value)} {}
|
|
1142
|
-
|
|
1143
|
-
template <typename T>
|
|
1144
|
-
NapiJsiRuntime::AutoRestore<T>::~AutoRestore() {
|
|
1145
|
-
*m_var = m_value;
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
#pragma endregion AutoRestore
|
|
1149
|
-
|
|
1150
|
-
#pragma region NapiRefHolder
|
|
1151
|
-
|
|
1152
|
-
NapiJsiRuntime::NapiRefHolder::NapiRefHolder(NapiJsiRuntime *runtime, napi_ext_ref ref) noexcept
|
|
1153
|
-
: m_runtime{runtime}, m_ref{ref} {}
|
|
1154
|
-
|
|
1155
|
-
NapiJsiRuntime::NapiRefHolder::NapiRefHolder(NapiJsiRuntime *runtime, napi_value value)
|
|
1156
|
-
: m_runtime{runtime}, m_ref{m_runtime->CreateReference(value)} {}
|
|
1157
|
-
|
|
1158
|
-
NapiJsiRuntime::NapiRefHolder::NapiRefHolder(NapiRefHolder &&other) noexcept
|
|
1159
|
-
: m_runtime{std::exchange(other.m_runtime, nullptr)}, m_ref{std::exchange(other.m_ref, nullptr)} {}
|
|
1160
|
-
|
|
1161
|
-
NapiJsiRuntime::NapiRefHolder &NapiJsiRuntime::NapiRefHolder::operator=(NapiRefHolder &&other) noexcept {
|
|
1162
|
-
if (this != &other) {
|
|
1163
|
-
NapiRefHolder temp{std::move(*this)};
|
|
1164
|
-
std::swap(m_runtime, other.m_runtime);
|
|
1165
|
-
std::swap(m_ref, other.m_ref);
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
|
-
return *this;
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
NapiJsiRuntime::NapiRefHolder::~NapiRefHolder() noexcept {
|
|
1172
|
-
if (m_ref) {
|
|
1173
|
-
// Clear m_ref before calling ReleaseReference on it to make sure that we always hold a valid m_ref.
|
|
1174
|
-
m_runtime->ReleaseReference(std::exchange(m_ref, nullptr));
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
[[nodiscard]] napi_ext_ref NapiJsiRuntime::NapiRefHolder::CloneRef() const noexcept {
|
|
1179
|
-
if (m_ref) {
|
|
1180
|
-
napi_ext_reference_ref(m_runtime->m_env, m_ref);
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
return m_ref;
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
NapiJsiRuntime::NapiRefHolder::operator napi_value() const {
|
|
1187
|
-
return m_runtime->GetReferenceValue(m_ref);
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
NapiJsiRuntime::NapiRefHolder::operator bool() const noexcept {
|
|
1191
|
-
return m_ref != nullptr;
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
#pragma endregion NapiRefHolder
|
|
1195
|
-
|
|
1196
|
-
#pragma region NapiPointerValueView
|
|
1197
|
-
|
|
1198
|
-
NapiJsiRuntime::NapiPointerValueView::NapiPointerValueView(NapiJsiRuntime const *runtime, void *valueOrRef) noexcept
|
|
1199
|
-
: m_runtime{runtime}, m_valueOrRef{valueOrRef} {}
|
|
1200
|
-
|
|
1201
|
-
// Intentionally do nothing.
|
|
1202
|
-
void NapiJsiRuntime::NapiPointerValueView::invalidate() noexcept {}
|
|
1203
|
-
|
|
1204
|
-
napi_value NapiJsiRuntime::NapiPointerValueView::GetValue() const {
|
|
1205
|
-
return reinterpret_cast<napi_value>(m_valueOrRef);
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
napi_ext_ref NapiJsiRuntime::NapiPointerValueView::GetRef() const {
|
|
1209
|
-
CHECK_ELSE_CRASH(false, "Not implemented");
|
|
1210
|
-
|
|
1211
|
-
return nullptr;
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
const NapiJsiRuntime *NapiJsiRuntime::NapiPointerValueView::GetRuntime() const noexcept {
|
|
1215
|
-
return m_runtime;
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
#pragma endregion NapiPointerValueView
|
|
1219
|
-
|
|
1220
|
-
#pragma region NapiPointerValue
|
|
1221
|
-
|
|
1222
|
-
NapiJsiRuntime::NapiPointerValue::NapiPointerValue(const NapiJsiRuntime *runtime, napi_ext_ref ref)
|
|
1223
|
-
: NapiPointerValueView(runtime, ref) {}
|
|
1224
|
-
|
|
1225
|
-
NapiJsiRuntime::NapiPointerValue::NapiPointerValue(const NapiJsiRuntime *runtime, napi_value value)
|
|
1226
|
-
: NapiPointerValueView(runtime, runtime->CreateReference(value)) {}
|
|
1227
|
-
|
|
1228
|
-
NapiJsiRuntime::NapiPointerValue::~NapiPointerValue() noexcept {
|
|
1229
|
-
if (napi_ext_ref ref = GetRef()) {
|
|
1230
|
-
GetRuntime()->ReleaseReference(ref);
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
void NapiJsiRuntime::NapiPointerValue::invalidate() noexcept {
|
|
1235
|
-
delete this;
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
napi_value NapiJsiRuntime::NapiPointerValue::GetValue() const {
|
|
1239
|
-
return GetRuntime()->GetReferenceValue(GetRef());
|
|
1240
|
-
}
|
|
1241
|
-
|
|
1242
|
-
napi_ext_ref NapiJsiRuntime::NapiPointerValue::GetRef() const {
|
|
1243
|
-
return reinterpret_cast<napi_ext_ref>(NapiPointerValueView::GetValue());
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
#pragma endregion NapiPointerValue
|
|
1247
|
-
|
|
1248
|
-
#pragma region SmallBuffer
|
|
1249
|
-
|
|
1250
|
-
template <typename T, size_t InplaceSize>
|
|
1251
|
-
NapiJsiRuntime::SmallBuffer<T, InplaceSize>::SmallBuffer(size_t size) noexcept
|
|
1252
|
-
: m_size{size}, m_heapData{m_size > InplaceSize ? std::make_unique<T[]>(m_size) : nullptr} {}
|
|
1253
|
-
|
|
1254
|
-
template <typename T, size_t InplaceSize>
|
|
1255
|
-
T *NapiJsiRuntime::SmallBuffer<T, InplaceSize>::Data() noexcept {
|
|
1256
|
-
return m_heapData ? m_heapData.get() : m_stackData.data();
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
template <typename T, size_t InplaceSize>
|
|
1260
|
-
size_t NapiJsiRuntime::SmallBuffer<T, InplaceSize>::Size() const noexcept {
|
|
1261
|
-
return m_size;
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
#pragma endregion SmallBuffer
|
|
1265
|
-
|
|
1266
|
-
#pragma region NapiValueArgs
|
|
1267
|
-
|
|
1268
|
-
NapiJsiRuntime::NapiValueArgs::NapiValueArgs(NapiJsiRuntime &runtime, span<const Value> args) : m_args{args.size()} {
|
|
1269
|
-
napi_value *jsArgs = m_args.Data();
|
|
1270
|
-
for (size_t i = 0; i < args.size(); ++i) {
|
|
1271
|
-
jsArgs[i] = runtime.GetNapiValue(args[i]);
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
NapiJsiRuntime::NapiValueArgs::operator span<napi_value>() {
|
|
1276
|
-
return span<napi_value>{m_args.Data(), m_args.Size()};
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
#pragma endregion NapiValueArgs
|
|
1280
|
-
|
|
1281
|
-
#pragma region JsiValueView
|
|
1282
|
-
|
|
1283
|
-
NapiJsiRuntime::JsiValueView::JsiValueView(NapiJsiRuntime *runtime, napi_value jsValue)
|
|
1284
|
-
: m_value{InitValue(runtime, jsValue, std::addressof(m_pointerStore))} {}
|
|
1285
|
-
|
|
1286
|
-
NapiJsiRuntime::JsiValueView::operator const Value &() const noexcept {
|
|
1287
|
-
return m_value;
|
|
1288
|
-
}
|
|
1289
|
-
|
|
1290
|
-
/*static*/ Value NapiJsiRuntime::JsiValueView::InitValue(NapiJsiRuntime *runtime, napi_value value, StoreType *store) {
|
|
1291
|
-
switch (runtime->TypeOf(value)) {
|
|
1292
|
-
case napi_valuetype::napi_undefined:
|
|
1293
|
-
return Value::undefined();
|
|
1294
|
-
case napi_valuetype::napi_null:
|
|
1295
|
-
return Value::null();
|
|
1296
|
-
case napi_valuetype::napi_boolean:
|
|
1297
|
-
return Value{runtime->GetValueBool(value)};
|
|
1298
|
-
case napi_valuetype::napi_number:
|
|
1299
|
-
return Value{runtime->GetValueDouble(value)};
|
|
1300
|
-
case napi_valuetype::napi_string:
|
|
1301
|
-
return make<String>(new (store) NapiPointerValueView{runtime, value});
|
|
1302
|
-
case napi_valuetype::napi_symbol:
|
|
1303
|
-
return make<Symbol>(new (store) NapiPointerValueView{runtime, value});
|
|
1304
|
-
case napi_valuetype::napi_object:
|
|
1305
|
-
case napi_valuetype::napi_function:
|
|
1306
|
-
case napi_valuetype::napi_external:
|
|
1307
|
-
case napi_valuetype::napi_bigint:
|
|
1308
|
-
return make<Object>(new (store) NapiPointerValueView{runtime, value});
|
|
1309
|
-
default:
|
|
1310
|
-
throw JSINativeException("Unexpected value type");
|
|
1311
|
-
}
|
|
1312
|
-
}
|
|
1313
|
-
|
|
1314
|
-
#pragma endregion JsiValueView
|
|
1315
|
-
|
|
1316
|
-
#pragma region JsiValueViewArgs
|
|
1317
|
-
|
|
1318
|
-
NapiJsiRuntime::JsiValueViewArgs::JsiValueViewArgs(NapiJsiRuntime *runtime, span<napi_value> args) noexcept
|
|
1319
|
-
: m_pointerStore{args.size()}, m_args{args.size()} {
|
|
1320
|
-
JsiValueView::StoreType *pointerStore = m_pointerStore.Data();
|
|
1321
|
-
Value *jsiArgs = m_args.Data();
|
|
1322
|
-
for (size_t i = 0; i < m_args.Size(); ++i) {
|
|
1323
|
-
jsiArgs[i] = JsiValueView::InitValue(runtime, args[i], std::addressof(pointerStore[i]));
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
|
|
1327
|
-
Value const *NapiJsiRuntime::JsiValueViewArgs::Data() noexcept {
|
|
1328
|
-
return m_args.Data();
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
size_t NapiJsiRuntime::JsiValueViewArgs::Size() const noexcept {
|
|
1332
|
-
return m_args.Size();
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
#pragma endregion JsiValueViewArgs
|
|
1336
|
-
|
|
1337
|
-
#pragma region PropNameIDView
|
|
1338
|
-
|
|
1339
|
-
NapiJsiRuntime::PropNameIDView::PropNameIDView(NapiJsiRuntime *runtime, napi_value propertyId) noexcept
|
|
1340
|
-
: m_propertyId{make<PropNameID>(new(std::addressof(m_pointerStore)) NapiPointerValueView{runtime, propertyId})} {}
|
|
1341
|
-
|
|
1342
|
-
NapiJsiRuntime::PropNameIDView::operator PropNameID const &() const noexcept {
|
|
1343
|
-
return m_propertyId;
|
|
1344
|
-
}
|
|
1345
|
-
|
|
1346
|
-
#pragma endregion PropNameIDView
|
|
1347
|
-
|
|
1348
|
-
#pragma region HostFunctionWrapper
|
|
1349
|
-
|
|
1350
|
-
NapiJsiRuntime::HostFunctionWrapper::HostFunctionWrapper(HostFunctionType &&type, NapiJsiRuntime &runtime)
|
|
1351
|
-
: m_hostFunction{std::move(type)}, m_runtime{runtime} {}
|
|
1352
|
-
|
|
1353
|
-
HostFunctionType &NapiJsiRuntime::HostFunctionWrapper::GetHostFunction() noexcept {
|
|
1354
|
-
return m_hostFunction;
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
NapiJsiRuntime &NapiJsiRuntime::HostFunctionWrapper::GetRuntime() noexcept {
|
|
1358
|
-
return m_runtime;
|
|
1359
|
-
}
|
|
1360
|
-
|
|
1361
|
-
#pragma endregion HostFunctionWrapper
|
|
1362
|
-
|
|
1363
|
-
#pragma region NapiPreparedJavaScript
|
|
1364
|
-
|
|
1365
|
-
NapiJsiRuntime::NapiPreparedJavaScript::NapiPreparedJavaScript(
|
|
1366
|
-
unique_ptr<const Buffer> serializedBuffer,
|
|
1367
|
-
const shared_ptr<const Buffer> &sourceBuffer,
|
|
1368
|
-
string sourceUrl)
|
|
1369
|
-
: m_sourceBuffer{sourceBuffer},
|
|
1370
|
-
m_serializedBuffer{std::move(serializedBuffer)},
|
|
1371
|
-
m_sourceUrl{std::move(sourceUrl)} {}
|
|
1372
|
-
|
|
1373
|
-
const Buffer &NapiJsiRuntime::NapiPreparedJavaScript::SerializedBuffer() const {
|
|
1374
|
-
return *m_serializedBuffer;
|
|
1375
|
-
}
|
|
1376
|
-
|
|
1377
|
-
const Buffer &NapiJsiRuntime::NapiPreparedJavaScript::SourceBuffer() const {
|
|
1378
|
-
return *m_sourceBuffer;
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
const string &NapiJsiRuntime::NapiPreparedJavaScript::SourceUrl() const {
|
|
1382
|
-
return m_sourceUrl;
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
|
-
#pragma endregion NapiPreparedJavaScript
|
|
1386
|
-
|
|
1387
|
-
#pragma region VectorBuffer
|
|
1388
|
-
|
|
1389
|
-
NapiJsiRuntime::VectorBuffer::VectorBuffer(vector<uint8_t> data) : m_data(std::move(data)) {}
|
|
1390
|
-
|
|
1391
|
-
const uint8_t *NapiJsiRuntime::VectorBuffer::data() const {
|
|
1392
|
-
return m_data.data();
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
|
-
size_t NapiJsiRuntime::VectorBuffer::size() const {
|
|
1396
|
-
return m_data.size();
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
#pragma endregion VectorBuffer
|
|
1400
|
-
|
|
1401
|
-
#pragma region error - handling
|
|
1402
|
-
|
|
1403
|
-
// Throws facebook::jsi::JSError or facebook::jsi::JSINativeException from NodeApi error.
|
|
1404
|
-
[[noreturn]] void NapiJsiRuntime::ThrowJsException(napi_status status) const {
|
|
1405
|
-
napi_value jsError{};
|
|
1406
|
-
CHECK_NAPI_ELSE_CRASH(napi_get_and_clear_last_exception(m_env, &jsError));
|
|
1407
|
-
|
|
1408
|
-
if (!m_pendingJSError && (status == napi_pending_exception || InstanceOf(jsError, m_value.Error))) {
|
|
1409
|
-
AutoRestore<bool> setValue(const_cast<bool *>(&m_pendingJSError), true);
|
|
1410
|
-
RewriteErrorMessage(jsError);
|
|
1411
|
-
|
|
1412
|
-
throw JSError(*const_cast<NapiJsiRuntime *>(this), ToJsiValue(jsError));
|
|
1413
|
-
} else {
|
|
1414
|
-
std::ostringstream errorString;
|
|
1415
|
-
errorString << "A call to NodeApi returned error code 0x" << std::hex << status << '.';
|
|
1416
|
-
|
|
1417
|
-
throw JSINativeException(errorString.str().c_str());
|
|
1418
|
-
}
|
|
1419
|
-
}
|
|
1420
|
-
|
|
1421
|
-
// Throws facebook::jsi::JSINativeException with a message.
|
|
1422
|
-
[[noreturn]] void NapiJsiRuntime::ThrowNativeException(char const *errorMessage) const {
|
|
1423
|
-
throw JSINativeException(errorMessage);
|
|
1424
|
-
}
|
|
1425
|
-
|
|
1426
|
-
// Rewrites error messages to match the JSI unit test expectations.
|
|
1427
|
-
void NapiJsiRuntime::RewriteErrorMessage(napi_value jsError) const {
|
|
1428
|
-
// The code below must work correctly even if the 'message' getter throws.
|
|
1429
|
-
// In case when it throws, we ignore that exception.
|
|
1430
|
-
napi_value message{};
|
|
1431
|
-
napi_status status = napi_get_property(m_env, jsError, m_propertyId.message, &message);
|
|
1432
|
-
if (status != napi_ok) {
|
|
1433
|
-
// If the 'message' property getter throws, then we clear the exception and ignore it.
|
|
1434
|
-
napi_value ignoreJSError{};
|
|
1435
|
-
napi_get_and_clear_last_exception(m_env, &ignoreJSError);
|
|
1436
|
-
} else if (TypeOf(message) == napi_string) {
|
|
1437
|
-
// JSI unit tests expect V8- or JSC-like messages for the stack overflow.
|
|
1438
|
-
if (StringToStdString(message) == "Out of stack space") {
|
|
1439
|
-
SetProperty(jsError, m_propertyId.message, CreateStringUtf8("RangeError : Maximum call stack size exceeded"sv));
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
// Evaluates lambda and augments exception messages with the method's name.
|
|
1445
|
-
template <typename TLambda>
|
|
1446
|
-
auto NapiJsiRuntime::RunInMethodContext(char const *methodName, TLambda lambda) {
|
|
1447
|
-
try {
|
|
1448
|
-
return lambda();
|
|
1449
|
-
} catch (JSError const &) {
|
|
1450
|
-
throw; // do not augment the JSError exceptions.
|
|
1451
|
-
} catch (std::exception const &ex) {
|
|
1452
|
-
ThrowNativeException((string{"Exception in "} + methodName + ": " + ex.what()).c_str());
|
|
1453
|
-
} catch (...) {
|
|
1454
|
-
ThrowNativeException((string{"Exception in "} + methodName + ": <unknown>").c_str());
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
// Evaluates lambda and converts all exceptions to NAPI errors.
|
|
1459
|
-
template <typename TLambda>
|
|
1460
|
-
napi_value NapiJsiRuntime::HandleCallbackExceptions(TLambda lambda) const noexcept {
|
|
1461
|
-
try {
|
|
1462
|
-
try {
|
|
1463
|
-
return lambda();
|
|
1464
|
-
} catch (JSError const &jsError) {
|
|
1465
|
-
// This block may throw exceptions
|
|
1466
|
-
SetException(GetNapiValue(jsError.value()));
|
|
1467
|
-
}
|
|
1468
|
-
} catch (std::exception const &ex) {
|
|
1469
|
-
SetException(ex.what());
|
|
1470
|
-
} catch (...) {
|
|
1471
|
-
SetException("Unexpected error");
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
return m_value.Undefined;
|
|
1475
|
-
}
|
|
1476
|
-
|
|
1477
|
-
// Throws JavaScript exception using NAPI.
|
|
1478
|
-
bool NapiJsiRuntime::SetException(napi_value error) const noexcept {
|
|
1479
|
-
// This method must not throw. We return false in case of error.
|
|
1480
|
-
return napi_throw(m_env, error) == napi_status::napi_ok;
|
|
1481
|
-
}
|
|
1482
|
-
|
|
1483
|
-
// Throws JavaScript error exception with the provided message using NAPI.
|
|
1484
|
-
bool NapiJsiRuntime::SetException(string_view message) const noexcept {
|
|
1485
|
-
// This method must not throw. We return false in case of error.
|
|
1486
|
-
return napi_throw_error(m_env, "Unknown", message.data()) == napi_status::napi_ok;
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
#pragma endregion error - handling
|
|
1490
|
-
|
|
1491
|
-
#pragma region Shared NAPI wrappers
|
|
1492
|
-
|
|
1493
|
-
// Runs script with the sourceUrl origin.
|
|
1494
|
-
napi_value NapiJsiRuntime::RunScript(napi_value script, const char *sourceUrl) {
|
|
1495
|
-
napi_value result{};
|
|
1496
|
-
CHECK_NAPI(napi_ext_run_script(m_env, script, sourceUrl, &result));
|
|
1497
|
-
|
|
1498
|
-
return result;
|
|
1499
|
-
}
|
|
1500
|
-
|
|
1501
|
-
napi_value NapiJsiRuntime::RunScriptBuffer(
|
|
1502
|
-
const std::shared_ptr<const facebook::jsi::Buffer> &buffer,
|
|
1503
|
-
const char *sourceUrl) {
|
|
1504
|
-
napi_ext_buffer napiBuffer{};
|
|
1505
|
-
napiBuffer.buffer_object = NativeObjectWrapper<std::shared_ptr<const facebook::jsi::Buffer>>::Wrap(
|
|
1506
|
-
std::shared_ptr<const facebook::jsi::Buffer>{buffer});
|
|
1507
|
-
napiBuffer.data = buffer->data();
|
|
1508
|
-
napiBuffer.byte_size = buffer->size();
|
|
1509
|
-
|
|
1510
|
-
napi_value result{};
|
|
1511
|
-
CHECK_NAPI(napi_ext_run_script_buffer(m_env, &napiBuffer, sourceUrl, &result));
|
|
1512
|
-
|
|
1513
|
-
return result;
|
|
1514
|
-
}
|
|
1515
|
-
|
|
1516
|
-
// Serializes script with the sourceUrl origin.
|
|
1517
|
-
vector<uint8_t> NapiJsiRuntime::SerializeScript(napi_value script, const char *sourceUrl) {
|
|
1518
|
-
vector<uint8_t> result;
|
|
1519
|
-
auto getBuffer = [](napi_env /*env*/, uint8_t const *buffer, size_t bufferLength, void *bufferHint) {
|
|
1520
|
-
auto data = reinterpret_cast<vector<uint8_t> *>(bufferHint);
|
|
1521
|
-
data->assign(buffer, buffer + bufferLength);
|
|
1522
|
-
};
|
|
1523
|
-
CHECK_NAPI(napi_ext_serialize_script(m_env, script, sourceUrl, getBuffer, &result));
|
|
1524
|
-
|
|
1525
|
-
return result;
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
// Runs serialized scriopt with the provided source and the sourceUrl origin.
|
|
1529
|
-
napi_value
|
|
1530
|
-
NapiJsiRuntime::RunSerializedScript(span<const uint8_t> serialized, napi_value source, const char *sourceUrl) {
|
|
1531
|
-
napi_value result{};
|
|
1532
|
-
CHECK_NAPI(napi_ext_run_serialized_script(m_env, serialized.data(), serialized.size(), source, sourceUrl, &result));
|
|
1533
|
-
|
|
1534
|
-
return result;
|
|
1535
|
-
}
|
|
1536
|
-
|
|
1537
|
-
// Creates a ref counted reference out of the napi_value.
|
|
1538
|
-
napi_ext_ref NapiJsiRuntime::CreateReference(napi_value value) const {
|
|
1539
|
-
napi_ext_ref result{};
|
|
1540
|
-
CHECK_NAPI(napi_ext_create_reference(m_env, value, &result));
|
|
1541
|
-
|
|
1542
|
-
return result;
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
// Decrements the reference ref count. It removes the reference if its ref count becomes 0.
|
|
1546
|
-
// Do not use the provided ref parameter after calling this method.
|
|
1547
|
-
void NapiJsiRuntime::ReleaseReference(napi_ext_ref ref) const {
|
|
1548
|
-
// TODO: [vmoroz] make it safe to be called from another thread per JSI spec.
|
|
1549
|
-
CHECK_NAPI(napi_ext_reference_unref(m_env, ref));
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
// Gets the napi_value from the reference.
|
|
1553
|
-
napi_value NapiJsiRuntime::GetReferenceValue(napi_ext_ref ref) const {
|
|
1554
|
-
napi_value result{};
|
|
1555
|
-
CHECK_NAPI(napi_ext_get_reference_value(m_env, ref, &result));
|
|
1556
|
-
|
|
1557
|
-
return result;
|
|
1558
|
-
}
|
|
1559
|
-
|
|
1560
|
-
// Gets type of the napi_value.
|
|
1561
|
-
napi_valuetype NapiJsiRuntime::TypeOf(napi_value value) const {
|
|
1562
|
-
napi_valuetype result{};
|
|
1563
|
-
CHECK_NAPI(napi_typeof(m_env, value, &result));
|
|
1564
|
-
|
|
1565
|
-
return result;
|
|
1566
|
-
}
|
|
1567
|
-
|
|
1568
|
-
// Returns true if two napi_values are strict equal per JavaScript rules.
|
|
1569
|
-
bool NapiJsiRuntime::StrictEquals(napi_value left, napi_value right) const {
|
|
1570
|
-
bool result{false};
|
|
1571
|
-
CHECK_NAPI(napi_strict_equals(m_env, left, right, &result));
|
|
1572
|
-
|
|
1573
|
-
return result;
|
|
1574
|
-
}
|
|
1575
|
-
|
|
1576
|
-
// Gets the napi_value for the JavaScript's undefined value.
|
|
1577
|
-
napi_value NapiJsiRuntime::GetUndefined() const {
|
|
1578
|
-
napi_value result{nullptr};
|
|
1579
|
-
CHECK_NAPI(napi_get_undefined(m_env, &result));
|
|
1580
|
-
|
|
1581
|
-
return result;
|
|
1582
|
-
}
|
|
1583
|
-
|
|
1584
|
-
// Gets the napi_value for the JavaScript's null value.
|
|
1585
|
-
napi_value NapiJsiRuntime::GetNull() const {
|
|
1586
|
-
napi_value result{nullptr};
|
|
1587
|
-
CHECK_NAPI(napi_get_null(m_env, &result));
|
|
1588
|
-
|
|
1589
|
-
return result;
|
|
1590
|
-
}
|
|
1591
|
-
|
|
1592
|
-
// Gets the napi_value for the JavaScript's global object.
|
|
1593
|
-
napi_value NapiJsiRuntime::GetGlobal() const {
|
|
1594
|
-
napi_value result{nullptr};
|
|
1595
|
-
CHECK_NAPI(napi_get_global(m_env, &result));
|
|
1596
|
-
|
|
1597
|
-
return result;
|
|
1598
|
-
}
|
|
1599
|
-
|
|
1600
|
-
// Gets the napi_value for the JavaScript's true and false values.
|
|
1601
|
-
napi_value NapiJsiRuntime::GetBoolean(bool value) const {
|
|
1602
|
-
napi_value result{nullptr};
|
|
1603
|
-
CHECK_NAPI(napi_get_boolean(m_env, value, &result));
|
|
1604
|
-
|
|
1605
|
-
return result;
|
|
1606
|
-
}
|
|
1607
|
-
|
|
1608
|
-
// Gets value of the Boolean napi_value.
|
|
1609
|
-
bool NapiJsiRuntime::GetValueBool(napi_value value) const {
|
|
1610
|
-
bool result{nullptr};
|
|
1611
|
-
CHECK_NAPI(napi_get_value_bool(m_env, value, &result));
|
|
1612
|
-
|
|
1613
|
-
return result;
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
|
-
// Creates napi_value with an int32_t value.
|
|
1617
|
-
napi_value NapiJsiRuntime::CreateInt32(int32_t value) const {
|
|
1618
|
-
napi_value result{};
|
|
1619
|
-
CHECK_NAPI(napi_create_int32(m_env, value, &result));
|
|
1620
|
-
|
|
1621
|
-
return result;
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
// Creates napi_value with a double value.
|
|
1625
|
-
napi_value NapiJsiRuntime::CreateDouble(double value) const {
|
|
1626
|
-
napi_value result{};
|
|
1627
|
-
CHECK_NAPI(napi_create_double(m_env, value, &result));
|
|
1628
|
-
|
|
1629
|
-
return result;
|
|
1630
|
-
}
|
|
1631
|
-
|
|
1632
|
-
// Gets value of the Double napi_value.
|
|
1633
|
-
double NapiJsiRuntime::GetValueDouble(napi_value value) const {
|
|
1634
|
-
double result{0};
|
|
1635
|
-
CHECK_NAPI(napi_get_value_double(m_env, value, &result));
|
|
1636
|
-
|
|
1637
|
-
return result;
|
|
1638
|
-
}
|
|
1639
|
-
|
|
1640
|
-
// Creates a napi_value string from the extended ASCII symbols that correspond to the Latin1 encoding.
|
|
1641
|
-
// Each character is a byte-sized value from 0 to 255.
|
|
1642
|
-
napi_value NapiJsiRuntime::CreateStringLatin1(string_view value) const {
|
|
1643
|
-
CHECK_ELSE_THROW(value.data(), "Cannot convert a nullptr to a JS string.");
|
|
1644
|
-
napi_value result{};
|
|
1645
|
-
CHECK_NAPI(napi_create_string_latin1(m_env, value.data(), value.size(), &result));
|
|
1646
|
-
|
|
1647
|
-
return result;
|
|
1648
|
-
}
|
|
1649
|
-
|
|
1650
|
-
// Creates a napi_value string from a UTF-8 string.
|
|
1651
|
-
napi_value NapiJsiRuntime::CreateStringUtf8(string_view value) const {
|
|
1652
|
-
CHECK_ELSE_THROW(value.data(), "Cannot convert a nullptr to a JS string.");
|
|
1653
|
-
napi_value result{};
|
|
1654
|
-
CHECK_NAPI(napi_create_string_utf8(m_env, value.data(), value.size(), &result));
|
|
1655
|
-
|
|
1656
|
-
return result;
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
|
-
// Creates a napi_value string from a UTF-8 string. Use data and length instead of string_view.
|
|
1660
|
-
napi_value NapiJsiRuntime::CreateStringUtf8(const uint8_t *data, size_t length) const {
|
|
1661
|
-
return CreateStringUtf8({reinterpret_cast<const char *>(data), length});
|
|
1662
|
-
}
|
|
1663
|
-
|
|
1664
|
-
// Gets std::string from the napi_value string.
|
|
1665
|
-
string NapiJsiRuntime::StringToStdString(napi_value stringValue) const {
|
|
1666
|
-
string result;
|
|
1667
|
-
CHECK_ELSE_THROW(
|
|
1668
|
-
TypeOf(stringValue) == napi_valuetype::napi_string,
|
|
1669
|
-
"Cannot convert a non JS string NodeApi Value to a std::string.");
|
|
1670
|
-
size_t strLength{};
|
|
1671
|
-
CHECK_NAPI(napi_get_value_string_utf8(m_env, stringValue, nullptr, 0, &strLength));
|
|
1672
|
-
result.assign(strLength, '\0');
|
|
1673
|
-
size_t copiedLength{};
|
|
1674
|
-
CHECK_NAPI(napi_get_value_string_utf8(m_env, stringValue, &result[0], result.length() + 1, &copiedLength));
|
|
1675
|
-
CHECK_ELSE_THROW(result.length() == copiedLength, "Unexpected string length");
|
|
1676
|
-
|
|
1677
|
-
return result;
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
// Gets or creates a unique string value from an UTF-8 string_view.
|
|
1681
|
-
napi_ext_ref NapiJsiRuntime::GetPropertyIdFromName(string_view value) const {
|
|
1682
|
-
napi_ext_ref ref{};
|
|
1683
|
-
CHECK_NAPI(napi_ext_get_unique_string_utf8_ref(m_env, value.data(), value.size(), &ref));
|
|
1684
|
-
|
|
1685
|
-
return ref;
|
|
1686
|
-
}
|
|
1687
|
-
|
|
1688
|
-
// Gets or creates a unique string value from an UTF-8 data/length range.
|
|
1689
|
-
napi_ext_ref NapiJsiRuntime::GetPropertyIdFromName(const uint8_t *data, size_t length) const {
|
|
1690
|
-
return GetPropertyIdFromName({reinterpret_cast<const char *>(data), length});
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
// Gets or creates a unique string value from napi_value string.
|
|
1694
|
-
napi_ext_ref NapiJsiRuntime::GetPropertyIdFromName(napi_value str) const {
|
|
1695
|
-
napi_ext_ref ref{};
|
|
1696
|
-
CHECK_NAPI(napi_ext_get_unique_string_ref(m_env, str, &ref));
|
|
1697
|
-
|
|
1698
|
-
return ref;
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1701
|
-
// Gets or creates a unique string value from napi_value string.
|
|
1702
|
-
napi_ext_ref NapiJsiRuntime::GetPropertyIdFromSymbol(napi_value sym) const {
|
|
1703
|
-
napi_ext_ref ref{};
|
|
1704
|
-
CHECK_NAPI(napi_ext_create_reference(m_env, sym, &ref));
|
|
1705
|
-
return ref;
|
|
1706
|
-
}
|
|
1707
|
-
|
|
1708
|
-
// Converts property id value to std::string.
|
|
1709
|
-
string NapiJsiRuntime::PropertyIdToStdString(napi_value propertyId) {
|
|
1710
|
-
if (TypeOf(propertyId) == napi_symbol) {
|
|
1711
|
-
return SymbolToStdString(propertyId);
|
|
1712
|
-
}
|
|
1713
|
-
|
|
1714
|
-
return StringToStdString(propertyId);
|
|
1715
|
-
}
|
|
1716
|
-
|
|
1717
|
-
// Creates a JavaScript symbol napi_value.
|
|
1718
|
-
napi_value NapiJsiRuntime::CreateSymbol(string_view symbolDescription) const {
|
|
1719
|
-
napi_value result{};
|
|
1720
|
-
napi_value description = CreateStringUtf8(symbolDescription);
|
|
1721
|
-
CHECK_NAPI(napi_create_symbol(m_env, description, &result));
|
|
1722
|
-
|
|
1723
|
-
return result;
|
|
1724
|
-
}
|
|
1725
|
-
|
|
1726
|
-
// Calls Symbol.toString() and returns it as std::string.
|
|
1727
|
-
string NapiJsiRuntime::SymbolToStdString(napi_value symbolValue) {
|
|
1728
|
-
EnvScope scope{m_env};
|
|
1729
|
-
if (!m_value.SymbolToString) {
|
|
1730
|
-
napi_value symbolCtor = GetProperty(m_value.Global, m_propertyId.Symbol);
|
|
1731
|
-
napi_value symbolPrototype = GetProperty(symbolCtor, m_propertyId.prototype);
|
|
1732
|
-
m_value.SymbolToString = NapiRefHolder{this, GetProperty(symbolPrototype, m_propertyId.toString)};
|
|
1733
|
-
}
|
|
1734
|
-
napi_value jsString = CallFunction(symbolValue, m_value.SymbolToString, {});
|
|
1735
|
-
|
|
1736
|
-
return StringToStdString(jsString);
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
// Calls a JavaScript function.
|
|
1740
|
-
napi_value NapiJsiRuntime::CallFunction(napi_value thisArg, napi_value function, span<napi_value> args) const {
|
|
1741
|
-
napi_value result{};
|
|
1742
|
-
CHECK_NAPI(napi_call_function(m_env, thisArg, function, args.size(), args.begin(), &result));
|
|
1743
|
-
|
|
1744
|
-
return result;
|
|
1745
|
-
}
|
|
1746
|
-
|
|
1747
|
-
// Constructs a new JavaScript Object using a constructor function.
|
|
1748
|
-
napi_value NapiJsiRuntime::ConstructObject(napi_value constructor, span<napi_value> args) const {
|
|
1749
|
-
napi_value result{};
|
|
1750
|
-
CHECK_NAPI(napi_new_instance(m_env, constructor, args.size(), args.begin(), &result));
|
|
1751
|
-
|
|
1752
|
-
return result;
|
|
1753
|
-
}
|
|
1754
|
-
|
|
1755
|
-
// Returns true if object was constructed using the provided constructor.
|
|
1756
|
-
bool NapiJsiRuntime::InstanceOf(napi_value object, napi_value constructor) const {
|
|
1757
|
-
bool result{false};
|
|
1758
|
-
CHECK_NAPI(napi_instanceof(m_env, object, constructor, &result));
|
|
1759
|
-
|
|
1760
|
-
return result;
|
|
1761
|
-
}
|
|
1762
|
-
|
|
1763
|
-
// Creates new JavaScript Object.
|
|
1764
|
-
napi_value NapiJsiRuntime::CreateObject() const {
|
|
1765
|
-
napi_value result{};
|
|
1766
|
-
CHECK_NAPI(napi_create_object(m_env, &result));
|
|
1767
|
-
|
|
1768
|
-
return result;
|
|
1769
|
-
}
|
|
1770
|
-
|
|
1771
|
-
// Returns true if the object has a property with the provided property ID.
|
|
1772
|
-
bool NapiJsiRuntime::HasProperty(napi_value object, napi_value propertyId) const {
|
|
1773
|
-
bool result{};
|
|
1774
|
-
CHECK_NAPI(napi_has_property(m_env, object, propertyId, &result));
|
|
1775
|
-
|
|
1776
|
-
return result;
|
|
1777
|
-
}
|
|
1778
|
-
|
|
1779
|
-
// Gets object property value.
|
|
1780
|
-
napi_value NapiJsiRuntime::GetProperty(napi_value object, napi_value propertyId) const {
|
|
1781
|
-
napi_value result{};
|
|
1782
|
-
CHECK_NAPI(napi_get_property(m_env, object, propertyId, &result));
|
|
1783
|
-
|
|
1784
|
-
return result;
|
|
1785
|
-
}
|
|
1786
|
-
|
|
1787
|
-
// Sets object property value.
|
|
1788
|
-
void NapiJsiRuntime::SetProperty(napi_value object, napi_value propertyId, napi_value value) const {
|
|
1789
|
-
CHECK_NAPI(napi_set_property(m_env, object, propertyId, value));
|
|
1790
|
-
}
|
|
1791
|
-
|
|
1792
|
-
// Sets object property value with the provided property accessibility attributes.
|
|
1793
|
-
void NapiJsiRuntime::SetProperty(
|
|
1794
|
-
napi_value object,
|
|
1795
|
-
napi_value propertyId,
|
|
1796
|
-
napi_value value,
|
|
1797
|
-
napi_property_attributes attrs) const {
|
|
1798
|
-
napi_property_descriptor descriptor{};
|
|
1799
|
-
descriptor.name = propertyId;
|
|
1800
|
-
descriptor.value = value;
|
|
1801
|
-
descriptor.attributes = attrs;
|
|
1802
|
-
CHECK_NAPI(napi_define_properties(m_env, object, 1, &descriptor));
|
|
1803
|
-
}
|
|
1804
|
-
|
|
1805
|
-
// Creates a new JavaScript Array with the provided length.
|
|
1806
|
-
napi_value NapiJsiRuntime::CreateArray(size_t length) const {
|
|
1807
|
-
napi_value result{};
|
|
1808
|
-
CHECK_NAPI(napi_create_array_with_length(m_env, length, &result));
|
|
1809
|
-
|
|
1810
|
-
return result;
|
|
1811
|
-
}
|
|
1812
|
-
|
|
1813
|
-
// Sets array element.
|
|
1814
|
-
void NapiJsiRuntime::SetElement(napi_value array, uint32_t index, napi_value value) const {
|
|
1815
|
-
CHECK_NAPI(napi_set_element(m_env, array, index, value));
|
|
1816
|
-
}
|
|
1817
|
-
|
|
1818
|
-
// The NAPI external function callback used for the JSI host function implementation.
|
|
1819
|
-
/*static*/ napi_value __cdecl NapiJsiRuntime::JsiHostFunctionCallback(napi_env env, napi_callback_info info) noexcept {
|
|
1820
|
-
HostFunctionWrapper *hostFuncWrapper{};
|
|
1821
|
-
size_t argc{};
|
|
1822
|
-
CHECK_NAPI_ELSE_CRASH(
|
|
1823
|
-
napi_get_cb_info(env, info, &argc, nullptr, nullptr, reinterpret_cast<void **>(&hostFuncWrapper)));
|
|
1824
|
-
CHECK_ELSE_CRASH(hostFuncWrapper, "Cannot find the host function");
|
|
1825
|
-
NapiJsiRuntime &runtime = hostFuncWrapper->GetRuntime();
|
|
1826
|
-
|
|
1827
|
-
return runtime.HandleCallbackExceptions([&env, &info, &argc, &runtime, &hostFuncWrapper]() {
|
|
1828
|
-
SmallBuffer<napi_value, MaxStackArgCount> napiArgs(argc);
|
|
1829
|
-
napi_value thisArg{};
|
|
1830
|
-
CHECK_NAPI_ELSE_CRASH(napi_get_cb_info(env, info, &argc, napiArgs.Data(), &thisArg, nullptr));
|
|
1831
|
-
CHECK_ELSE_CRASH(napiArgs.Size() == argc, "Wrong argument count");
|
|
1832
|
-
const JsiValueView jsiThisArg{&runtime, thisArg};
|
|
1833
|
-
JsiValueViewArgs jsiArgs(&runtime, span<napi_value>(napiArgs.Data(), napiArgs.Size()));
|
|
1834
|
-
|
|
1835
|
-
const HostFunctionType &hostFunc = hostFuncWrapper->GetHostFunction();
|
|
1836
|
-
return runtime.RunInMethodContext("HostFunction", [&hostFunc, &runtime, &jsiThisArg, &jsiArgs]() {
|
|
1837
|
-
return runtime.GetNapiValue(hostFunc(runtime, jsiThisArg, jsiArgs.Data(), jsiArgs.Size()));
|
|
1838
|
-
});
|
|
1839
|
-
});
|
|
1840
|
-
}
|
|
1841
|
-
|
|
1842
|
-
// Creates an external function.
|
|
1843
|
-
napi_value NapiJsiRuntime::CreateExternalFunction(
|
|
1844
|
-
napi_value name,
|
|
1845
|
-
int32_t paramCount,
|
|
1846
|
-
napi_callback callback,
|
|
1847
|
-
void *callbackData) {
|
|
1848
|
-
string funcName = StringToStdString(name);
|
|
1849
|
-
napi_value function{};
|
|
1850
|
-
CHECK_NAPI(napi_create_function(m_env, funcName.data(), funcName.length(), callback, callbackData, &function));
|
|
1851
|
-
SetProperty(function, m_propertyId.length, CreateInt32(paramCount), napi_property_attributes::napi_default);
|
|
1852
|
-
|
|
1853
|
-
return function;
|
|
1854
|
-
}
|
|
1855
|
-
|
|
1856
|
-
// Creates an object that wraps up external data.
|
|
1857
|
-
napi_value NapiJsiRuntime::CreateExternalObject(void *data, napi_finalize finalizeCallback) const {
|
|
1858
|
-
napi_value result{};
|
|
1859
|
-
CHECK_NAPI(napi_create_external(m_env, data, finalizeCallback, nullptr, &result));
|
|
1860
|
-
|
|
1861
|
-
return result;
|
|
1862
|
-
}
|
|
1863
|
-
|
|
1864
|
-
// Wraps up std::unique_ptr as an external object.
|
|
1865
|
-
template <typename T>
|
|
1866
|
-
napi_value NapiJsiRuntime::CreateExternalObject(unique_ptr<T> &&data) const {
|
|
1867
|
-
napi_finalize finalize = [](napi_env /*env*/, void *dataToDestroy, void * /*finalizerHint*/) {
|
|
1868
|
-
// We wrap dataToDestroy in a unique_ptr to avoid calling delete explicitly.
|
|
1869
|
-
unique_ptr<T> dataDeleter{static_cast<T *>(dataToDestroy)};
|
|
1870
|
-
};
|
|
1871
|
-
napi_value object = CreateExternalObject(data.get(), finalize);
|
|
1872
|
-
|
|
1873
|
-
// We only call data.release() after the CreateExternalObject succeeds.
|
|
1874
|
-
// Otherwise, when CreateExternalObject fails and an exception is thrown,
|
|
1875
|
-
// the memory that data used to own will be leaked.
|
|
1876
|
-
data.release();
|
|
1877
|
-
|
|
1878
|
-
return object;
|
|
1879
|
-
}
|
|
1880
|
-
|
|
1881
|
-
// Gets external data wrapped by an external object.
|
|
1882
|
-
void *NapiJsiRuntime::GetExternalData(napi_value object) const {
|
|
1883
|
-
void *result{};
|
|
1884
|
-
CHECK_NAPI(napi_get_value_external(m_env, object, &result));
|
|
1885
|
-
|
|
1886
|
-
return result;
|
|
1887
|
-
}
|
|
1888
|
-
|
|
1889
|
-
// Gets JSI host object wrapped into a napi_value object.
|
|
1890
|
-
const shared_ptr<HostObject> &NapiJsiRuntime::GetJsiHostObject(napi_value obj) {
|
|
1891
|
-
const napi_value hostObjectHolder = GetProperty(obj, m_propertyId.hostObjectSymbol);
|
|
1892
|
-
|
|
1893
|
-
if (TypeOf(hostObjectHolder) == napi_valuetype::napi_external) {
|
|
1894
|
-
if (void *data = GetExternalData(hostObjectHolder)) {
|
|
1895
|
-
return *static_cast<shared_ptr<HostObject> *>(data);
|
|
1896
|
-
}
|
|
1897
|
-
}
|
|
1898
|
-
|
|
1899
|
-
throw JSINativeException("Cannot get HostObjects.");
|
|
1900
|
-
}
|
|
1901
|
-
|
|
1902
|
-
// Gets cached or creates Proxy handler to implement the JSI host object.
|
|
1903
|
-
napi_value NapiJsiRuntime::GetHostObjectProxyHandler() {
|
|
1904
|
-
if (!m_value.HostObjectProxyHandler) {
|
|
1905
|
-
const napi_value handler = CreateObject();
|
|
1906
|
-
SetProxyTrap<&NapiJsiRuntime::HostObjectGetTrap, 3>(handler, m_propertyId.get);
|
|
1907
|
-
SetProxyTrap<&NapiJsiRuntime::HostObjectSetTrap, 4>(handler, m_propertyId.set);
|
|
1908
|
-
SetProxyTrap<&NapiJsiRuntime::HostObjectOwnKeysTrap, 1>(handler, m_propertyId.ownKeys);
|
|
1909
|
-
SetProxyTrap<&NapiJsiRuntime::HostObjectGetOwnPropertyDescriptorTrap, 2>(
|
|
1910
|
-
handler, m_propertyId.getOwnPropertyDescriptor);
|
|
1911
|
-
m_value.HostObjectProxyHandler = NapiRefHolder{this, handler};
|
|
1912
|
-
}
|
|
1913
|
-
|
|
1914
|
-
return m_value.HostObjectProxyHandler;
|
|
1915
|
-
}
|
|
1916
|
-
|
|
1917
|
-
// Sets Proxy trap method as a pointer to NapiJsiRuntime instance method.
|
|
1918
|
-
template <napi_value (NapiJsiRuntime::*trapMethod)(span<napi_value>), size_t argCount>
|
|
1919
|
-
void NapiJsiRuntime::SetProxyTrap(napi_value handler, napi_value propertyName) {
|
|
1920
|
-
napi_callback proxyTrap = [](napi_env env, napi_callback_info info) noexcept {
|
|
1921
|
-
NapiJsiRuntime *runtime{};
|
|
1922
|
-
napi_value args[argCount]{};
|
|
1923
|
-
size_t actualArgCount{argCount};
|
|
1924
|
-
CHECK_NAPI_ELSE_CRASH(
|
|
1925
|
-
napi_get_cb_info(env, info, &actualArgCount, args, nullptr, reinterpret_cast<void **>(&runtime)));
|
|
1926
|
-
CHECK_ELSE_CRASH(actualArgCount == argCount, "proxy trap requires argCount arguments.");
|
|
1927
|
-
|
|
1928
|
-
return runtime->HandleCallbackExceptions(
|
|
1929
|
-
[&runtime, &args]() { return (runtime->*trapMethod)(span<napi_value>(args, argCount)); });
|
|
1930
|
-
};
|
|
1931
|
-
|
|
1932
|
-
SetProperty(handler, propertyName, CreateExternalFunction(propertyName, argCount, proxyTrap, this));
|
|
1933
|
-
}
|
|
1934
|
-
|
|
1935
|
-
// The host object Proxy 'get' trap implementation.
|
|
1936
|
-
napi_value NapiJsiRuntime::HostObjectGetTrap(span<napi_value> args) {
|
|
1937
|
-
// args[0] - the Proxy target object.
|
|
1938
|
-
// args[1] - the name of the property to set.
|
|
1939
|
-
// args[2] - the Proxy object (unused).
|
|
1940
|
-
napi_value propertyName = args[1];
|
|
1941
|
-
if (TypeOf(propertyName) == napi_symbol && StrictEquals(propertyName, m_propertyId.hostObjectSymbol)) {
|
|
1942
|
-
// The special property to retrieve the target object.
|
|
1943
|
-
return GetProperty(args[0], m_propertyId.hostObjectSymbol);
|
|
1944
|
-
}
|
|
1945
|
-
const auto &hostObject = GetJsiHostObject(args[0]);
|
|
1946
|
-
PropNameIDView propertyId{this, propertyName};
|
|
1947
|
-
|
|
1948
|
-
return RunInMethodContext("HostObject::get", [&hostObject, &propertyId, this]() {
|
|
1949
|
-
return GetNapiValue(hostObject->get(*this, propertyId));
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1952
|
-
|
|
1953
|
-
// The host object Proxy 'set' trap implementation.
|
|
1954
|
-
napi_value NapiJsiRuntime::HostObjectSetTrap(span<napi_value> args) {
|
|
1955
|
-
// args[0] - the Proxy target object.
|
|
1956
|
-
// args[1] - the name of the property to set.
|
|
1957
|
-
// args[2] - the new value of the property to set.
|
|
1958
|
-
// args[3] - the Proxy object (unused).
|
|
1959
|
-
const auto &hostObject = GetJsiHostObject(args[0]);
|
|
1960
|
-
PropNameIDView propertyId{this, args[1]};
|
|
1961
|
-
JsiValueView value{this, args[2]};
|
|
1962
|
-
RunInMethodContext(
|
|
1963
|
-
"HostObject::set", [&hostObject, &propertyId, &value, this]() { hostObject->set(*this, propertyId, value); });
|
|
1964
|
-
|
|
1965
|
-
return static_cast<napi_value>(m_value.Undefined);
|
|
1966
|
-
}
|
|
1967
|
-
|
|
1968
|
-
// The host object Proxy 'ownKeys' trap implementation.
|
|
1969
|
-
napi_value NapiJsiRuntime::HostObjectOwnKeysTrap(span<napi_value> args) {
|
|
1970
|
-
// args[0] - the Proxy target object.
|
|
1971
|
-
const auto &hostObject = GetJsiHostObject(args[0]);
|
|
1972
|
-
|
|
1973
|
-
vector<PropNameID> ownKeys = RunInMethodContext(
|
|
1974
|
-
"HostObject::getPropertyNames", [&hostObject, this]() { return hostObject->getPropertyNames(*this); });
|
|
1975
|
-
|
|
1976
|
-
std::unordered_set<napi_ext_ref> dedupedOwnKeys{};
|
|
1977
|
-
dedupedOwnKeys.reserve(ownKeys.size());
|
|
1978
|
-
for (PropNameID const &key : ownKeys) {
|
|
1979
|
-
dedupedOwnKeys.insert(GetNapiRef(key));
|
|
1980
|
-
}
|
|
1981
|
-
|
|
1982
|
-
napi_value ownKeyArray = CreateArray(dedupedOwnKeys.size());
|
|
1983
|
-
uint32_t index = 0;
|
|
1984
|
-
for (napi_ext_ref key : dedupedOwnKeys) {
|
|
1985
|
-
SetElement(ownKeyArray, index, GetReferenceValue(key));
|
|
1986
|
-
++index;
|
|
1987
|
-
}
|
|
1988
|
-
|
|
1989
|
-
return ownKeyArray;
|
|
1990
|
-
}
|
|
1991
|
-
|
|
1992
|
-
// The host object Proxy 'getOwnPropertyDescriptor' trap implementation.
|
|
1993
|
-
napi_value NapiJsiRuntime::HostObjectGetOwnPropertyDescriptorTrap(span<napi_value> args) {
|
|
1994
|
-
// args[0] - the Proxy target object.
|
|
1995
|
-
// args[1] - the property
|
|
1996
|
-
const auto &hostObject = GetJsiHostObject(args[0]);
|
|
1997
|
-
PropNameIDView propertyId{this, args[1]};
|
|
1998
|
-
|
|
1999
|
-
return RunInMethodContext("HostObject::getOwnPropertyDescriptor", [&hostObject, &propertyId, this]() {
|
|
2000
|
-
auto getPropDescriptor = [](napi_value name, napi_value value) {
|
|
2001
|
-
return napi_property_descriptor{
|
|
2002
|
-
nullptr, name, nullptr, nullptr, nullptr, value, napi_default_jsproperty, nullptr};
|
|
2003
|
-
};
|
|
2004
|
-
napi_property_descriptor properties[]{
|
|
2005
|
-
getPropDescriptor(m_propertyId.value, GetNapiValue(hostObject->get(*this, propertyId))),
|
|
2006
|
-
getPropDescriptor(m_propertyId.writable, m_value.True),
|
|
2007
|
-
getPropDescriptor(m_propertyId.enumerable, m_value.True),
|
|
2008
|
-
getPropDescriptor(m_propertyId.configurable, m_value.True)};
|
|
2009
|
-
napi_value descriptor = CreateObject();
|
|
2010
|
-
CHECK_NAPI(napi_define_properties(m_env, descriptor, std::size(properties), properties));
|
|
2011
|
-
|
|
2012
|
-
return descriptor;
|
|
2013
|
-
});
|
|
2014
|
-
}
|
|
2015
|
-
|
|
2016
|
-
#pragma endregion Shared NAPI wrappers
|
|
2017
|
-
|
|
2018
|
-
#pragma region Miscellaneous utility methods
|
|
2019
|
-
|
|
2020
|
-
// Converts facebook::jsi::Bufer to span.
|
|
2021
|
-
span<const uint8_t> NapiJsiRuntime::ToSpan(const Buffer &buffer) {
|
|
2022
|
-
return span<const uint8_t>(buffer.data(), buffer.size());
|
|
2023
|
-
}
|
|
2024
|
-
|
|
2025
|
-
// Creates facebook::jsi::Value from napi_value.
|
|
2026
|
-
Value NapiJsiRuntime::ToJsiValue(napi_value value) const {
|
|
2027
|
-
switch (TypeOf(value)) {
|
|
2028
|
-
case napi_valuetype::napi_undefined:
|
|
2029
|
-
return Value::undefined();
|
|
2030
|
-
case napi_valuetype::napi_null:
|
|
2031
|
-
return Value::null();
|
|
2032
|
-
case napi_valuetype::napi_boolean:
|
|
2033
|
-
return Value{GetValueBool(value)};
|
|
2034
|
-
case napi_valuetype::napi_number:
|
|
2035
|
-
return Value{GetValueDouble(value)};
|
|
2036
|
-
case napi_valuetype::napi_string:
|
|
2037
|
-
return Value{MakePointer<String>(value)};
|
|
2038
|
-
case napi_valuetype::napi_symbol:
|
|
2039
|
-
return Value{MakePointer<Symbol>(value)};
|
|
2040
|
-
case napi_valuetype::napi_object:
|
|
2041
|
-
case napi_valuetype::napi_function:
|
|
2042
|
-
case napi_valuetype::napi_external:
|
|
2043
|
-
case napi_valuetype::napi_bigint:
|
|
2044
|
-
return Value{MakePointer<Object>(value)};
|
|
2045
|
-
default:
|
|
2046
|
-
throw JSINativeException("Unexpected value type");
|
|
2047
|
-
}
|
|
2048
|
-
}
|
|
2049
|
-
|
|
2050
|
-
// Gets napi_value from facebook::jsi::Value.
|
|
2051
|
-
napi_value NapiJsiRuntime::GetNapiValue(const Value &value) const {
|
|
2052
|
-
if (value.isUndefined()) {
|
|
2053
|
-
return m_value.Undefined;
|
|
2054
|
-
} else if (value.isNull()) {
|
|
2055
|
-
return m_value.Null;
|
|
2056
|
-
} else if (value.isBool()) {
|
|
2057
|
-
return value.getBool() ? m_value.True : m_value.False;
|
|
2058
|
-
} else if (value.isNumber()) {
|
|
2059
|
-
return CreateDouble(value.getNumber());
|
|
2060
|
-
} else if (value.isSymbol()) {
|
|
2061
|
-
return GetNapiValue(value.getSymbol(*const_cast<NapiJsiRuntime *>(this)));
|
|
2062
|
-
} else if (value.isString()) {
|
|
2063
|
-
return GetNapiValue(value.getString(*const_cast<NapiJsiRuntime *>(this)));
|
|
2064
|
-
} else if (value.isObject()) {
|
|
2065
|
-
return GetNapiValue(value.getObject(*const_cast<NapiJsiRuntime *>(this)));
|
|
2066
|
-
} else if (value.isBigInt()) {
|
|
2067
|
-
return GetNapiValue(value.getBigInt(*const_cast<NapiJsiRuntime *>(this)));
|
|
2068
|
-
} else {
|
|
2069
|
-
throw JSINativeException("Unexpected jsi::Value type");
|
|
2070
|
-
}
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
// Clones NapiPointerValue.
|
|
2074
|
-
/*static*/ NapiJsiRuntime::NapiPointerValue *NapiJsiRuntime::CloneNapiPointerValue(const PointerValue *pointerValue) {
|
|
2075
|
-
auto napiPointerValue = static_cast<const NapiPointerValueView *>(pointerValue);
|
|
2076
|
-
|
|
2077
|
-
return new NapiPointerValue(napiPointerValue->GetRuntime(), napiPointerValue->GetValue());
|
|
2078
|
-
}
|
|
2079
|
-
|
|
2080
|
-
// Gets napi_value from facebook::jsi::Pointer.
|
|
2081
|
-
/*static*/ napi_value NapiJsiRuntime::GetNapiValue(const Pointer &p) {
|
|
2082
|
-
return static_cast<const NapiPointerValueView *>(getPointerValue(p))->GetValue();
|
|
2083
|
-
}
|
|
2084
|
-
|
|
2085
|
-
// Gets napi_ext_ref from facebook::jsi::Pointer.
|
|
2086
|
-
/*static*/ napi_ext_ref NapiJsiRuntime::GetNapiRef(const Pointer &p) {
|
|
2087
|
-
return static_cast<const NapiPointerValueView *>(getPointerValue(p))->GetRef();
|
|
2088
|
-
}
|
|
2089
|
-
|
|
2090
|
-
// Makes new value derived from the facebook::jsi::Pointer type.
|
|
2091
|
-
template <typename T, typename TValue, std::enable_if_t<std::is_base_of_v<facebook::jsi::Pointer, T>, int>>
|
|
2092
|
-
T NapiJsiRuntime::MakePointer(TValue value) const {
|
|
2093
|
-
return make<T>(new NapiPointerValue(this, value));
|
|
2094
|
-
}
|
|
2095
|
-
|
|
2096
|
-
#pragma endregion Miscellaneous utility methods
|
|
2097
|
-
|
|
2098
|
-
#pragma endregion NapiJsiRuntime
|
|
2099
|
-
} // namespace
|
|
2100
|
-
|
|
2101
|
-
unique_ptr<Runtime> __cdecl MakeNodeApiJsiRuntime(napi_env env) noexcept {
|
|
2102
|
-
return std::make_unique<NapiJsiRuntime>(env);
|
|
2103
|
-
}
|
|
2104
|
-
|
|
2105
|
-
} // namespace Microsoft::JSI
|