react-native-windows 0.80.0 → 0.80.5
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 +1 -1
- package/Microsoft.ReactNative/Fabric/Composition/DebuggerUIIsland.cpp +169 -0
- package/Microsoft.ReactNative/Fabric/Composition/DebuggerUIIsland.h +42 -0
- package/Microsoft.ReactNative/Fabric/Composition/DebuggingOverlayComponentView.cpp +1 -1
- package/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp +60 -33
- package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp +68 -1
- package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.h +9 -0
- package/Microsoft.ReactNative/Fabric/Composition/TextDrawing.cpp +5 -37
- package/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp +5 -3
- package/Microsoft.ReactNative/Fabric/FabricUIManagerModule.cpp +6 -1
- package/Microsoft.ReactNative/Fabric/WindowsImageManager.cpp +14 -1
- package/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/PlatformColorUtils.cpp +0 -17
- package/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/PlatformColorUtils.h +0 -3
- package/Microsoft.ReactNative/Fabric/platform/react/threading/TaskDispatchThread.cpp +75 -24
- package/Microsoft.ReactNative/Fabric/platform/react/threading/TaskDispatchThread.h +4 -25
- package/Microsoft.ReactNative/JsiApi.cpp +1 -1
- package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +1 -0
- package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters +3 -0
- package/Microsoft.ReactNative/ReactHost/DebuggerNotifications.h +54 -0
- package/Microsoft.ReactNative/ReactHost/React.h +11 -4
- package/Microsoft.ReactNative/ReactHost/ReactHost.cpp +195 -29
- package/Microsoft.ReactNative/ReactHost/ReactHost.h +22 -4
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +24 -5
- package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.h +1 -1
- package/Microsoft.ReactNative/ReactRootView.cpp +108 -0
- package/Microsoft.ReactNative/ReactRootView.h +6 -0
- package/Microsoft.ReactNative/Views/DevMenu.cpp +1 -1
- package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems +1 -1
- package/PropertySheets/Generated/PackageVersion.g.props +3 -3
- package/PropertySheets/JSEngine.props +1 -1
- package/PropertySheets/React.Cpp.props +2 -2
- package/ReactCommon/ReactCommon.vcxproj +18 -1
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/NativeToJsBridge.cpp +1 -1
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/test/testlib.cpp +4 -4
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.cpp +23 -9
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.h +16 -0
- package/ReactCommon/cgmanifest.json +1 -1
- package/Scripts/Tfs/Layout-MSRN-Headers.ps1 +2 -0
- package/Shared/DevServerHelper.h +13 -3
- package/Shared/DevSettings.h +7 -0
- package/Shared/DevSupportManager.cpp +79 -20
- package/Shared/DevSupportManager.h +7 -19
- package/Shared/Hermes/HermesRuntimeAgentDelegate.cpp +99 -0
- package/Shared/Hermes/HermesRuntimeAgentDelegate.h +81 -0
- package/Shared/Hermes/HermesRuntimeTargetDelegate.cpp +263 -0
- package/Shared/Hermes/HermesRuntimeTargetDelegate.h +77 -0
- package/Shared/HermesRuntimeHolder.cpp +29 -111
- package/Shared/HermesRuntimeHolder.h +214 -32
- package/Shared/IDevSupportManager.h +5 -2
- package/Shared/Inspector/ReactInspectorPackagerConnectionDelegate.cpp +108 -0
- package/Shared/Inspector/ReactInspectorPackagerConnectionDelegate.h +19 -0
- package/Shared/Inspector/ReactInspectorThread.h +18 -0
- package/Shared/JSI/RuntimeHolder.h +5 -2
- package/Shared/OInstance.cpp +44 -27
- package/Shared/Shared.vcxitems +27 -17
- package/Shared/Shared.vcxitems.filters +33 -15
- package/package.json +4 -4
- package/Microsoft.ReactNative.Cxx/ApiLoaders/JSRuntimeApi.cpp +0 -79
- package/Microsoft.ReactNative.Cxx/ApiLoaders/JSRuntimeApi.h +0 -51
- package/Microsoft.ReactNative.Cxx/ApiLoaders/JSRuntimeApi.inc +0 -50
- package/Microsoft.ReactNative.Cxx/ApiLoaders/NodeApi.cpp +0 -41
- package/Microsoft.ReactNative.Cxx/ApiLoaders/NodeApi.h +0 -127
- package/Microsoft.ReactNative.Cxx/ApiLoaders/NodeApi.inc +0 -125
- package/Microsoft.ReactNative.Cxx/ApiLoaders/NodeApi_posix.cpp +0 -16
- package/Microsoft.ReactNative.Cxx/ApiLoaders/NodeApi_win.cpp +0 -23
- package/Microsoft.ReactNative.Cxx/JSI/decorator.h +0 -1054
- package/Microsoft.ReactNative.Cxx/JSI/instrumentation.h +0 -145
- package/Microsoft.ReactNative.Cxx/JSI/jsi-inl.h +0 -372
- package/Microsoft.ReactNative.Cxx/JSI/jsi.cpp +0 -797
- package/Microsoft.ReactNative.Cxx/JSI/jsi.h +0 -1799
- package/Microsoft.ReactNative.Cxx/JSI/threadsafe.h +0 -79
- package/Microsoft.ReactNative.Cxx/NodeApiJsiRuntime.cpp +0 -3531
- package/Microsoft.ReactNative.Cxx/NodeApiJsiRuntime.h +0 -38
- package/Microsoft.ReactNative.Cxx/node-api/js_native_api.h +0 -614
- package/Microsoft.ReactNative.Cxx/node-api/js_native_api_types.h +0 -212
- package/Microsoft.ReactNative.Cxx/node-api/js_runtime_api.h +0 -199
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.cpp +0 -78
- package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.h +0 -196
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jserrorhandler/JsErrorHandler.cpp +0 -429
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsitooling/react/runtime/JSRuntimeFactory.cpp +0 -45
- package/ReactCommon/TEMP_UntilReactCommonUpdate/jsitooling/react/runtime/JSRuntimeFactory.h +0 -91
- package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/ReactInstance.cpp +0 -670
- package/Shared/InspectorPackagerConnection.cpp +0 -232
- package/Shared/InspectorPackagerConnection.h +0 -61
- /package/Shared/{HermesSamplingProfiler.cpp → Hermes/HermesSamplingProfiler.cpp} +0 -0
- /package/Shared/{HermesSamplingProfiler.h → Hermes/HermesSamplingProfiler.h} +0 -0
|
@@ -1,1799 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
#pragma once
|
|
9
|
-
|
|
10
|
-
#include <cassert>
|
|
11
|
-
#include <cstring>
|
|
12
|
-
#include <exception>
|
|
13
|
-
#include <functional>
|
|
14
|
-
#include <memory>
|
|
15
|
-
#include <string>
|
|
16
|
-
#include <vector>
|
|
17
|
-
|
|
18
|
-
// JSI version defines set of features available in the API.
|
|
19
|
-
// Each significant API change must be under a new version.
|
|
20
|
-
// The JSI_VERSION can be provided as a parameter to compiler
|
|
21
|
-
// or in the optional "jsi_version.h" file.
|
|
22
|
-
|
|
23
|
-
#ifndef JSI_VERSION
|
|
24
|
-
#if defined(__has_include) && __has_include(<jsi/jsi-version.h>)
|
|
25
|
-
#include <jsi/jsi-version.h>
|
|
26
|
-
#endif
|
|
27
|
-
#endif
|
|
28
|
-
|
|
29
|
-
#ifndef JSI_VERSION
|
|
30
|
-
// Use the latest version by default
|
|
31
|
-
#define JSI_VERSION 19
|
|
32
|
-
#endif
|
|
33
|
-
|
|
34
|
-
#if JSI_VERSION >= 3
|
|
35
|
-
#define JSI_NO_CONST_3
|
|
36
|
-
#else
|
|
37
|
-
#define JSI_NO_CONST_3 const
|
|
38
|
-
#endif
|
|
39
|
-
|
|
40
|
-
#if JSI_VERSION >= 10
|
|
41
|
-
#define JSI_CONST_10 const
|
|
42
|
-
#else
|
|
43
|
-
#define JSI_CONST_10
|
|
44
|
-
#endif
|
|
45
|
-
|
|
46
|
-
#if JSI_VERSION >= 15
|
|
47
|
-
#define JSI_NOEXCEPT_15 noexcept
|
|
48
|
-
#else
|
|
49
|
-
#define JSI_NOEXCEPT_15
|
|
50
|
-
#endif
|
|
51
|
-
|
|
52
|
-
#ifndef JSI_EXPORT
|
|
53
|
-
#ifdef _MSC_VER
|
|
54
|
-
#ifdef CREATE_SHARED_LIBRARY
|
|
55
|
-
#define JSI_EXPORT __declspec(dllexport)
|
|
56
|
-
#else
|
|
57
|
-
#define JSI_EXPORT
|
|
58
|
-
#endif // CREATE_SHARED_LIBRARY
|
|
59
|
-
#else // _MSC_VER
|
|
60
|
-
#define JSI_EXPORT __attribute__((visibility("default")))
|
|
61
|
-
#endif // _MSC_VER
|
|
62
|
-
#endif // !defined(JSI_EXPORT)
|
|
63
|
-
|
|
64
|
-
class FBJSRuntime;
|
|
65
|
-
namespace facebook {
|
|
66
|
-
namespace jsi {
|
|
67
|
-
|
|
68
|
-
/// Base class for buffers of data or bytecode that need to be passed to the
|
|
69
|
-
/// runtime. The buffer is expected to be fully immutable, so the result of
|
|
70
|
-
/// size(), data(), and the contents of the pointer returned by data() must not
|
|
71
|
-
/// change after construction.
|
|
72
|
-
class JSI_EXPORT Buffer {
|
|
73
|
-
public:
|
|
74
|
-
virtual ~Buffer();
|
|
75
|
-
virtual size_t size() const = 0;
|
|
76
|
-
virtual const uint8_t* data() const = 0;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
class JSI_EXPORT StringBuffer : public Buffer {
|
|
80
|
-
public:
|
|
81
|
-
StringBuffer(std::string s) : s_(std::move(s)) {}
|
|
82
|
-
size_t size() const override {
|
|
83
|
-
return s_.size();
|
|
84
|
-
}
|
|
85
|
-
const uint8_t* data() const override {
|
|
86
|
-
return reinterpret_cast<const uint8_t*>(s_.data());
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private:
|
|
90
|
-
std::string s_;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
#if JSI_VERSION >= 9
|
|
94
|
-
/// Base class for buffers of data that need to be passed to the runtime. The
|
|
95
|
-
/// result of size() and data() must not change after construction. However, the
|
|
96
|
-
/// region pointed to by data() may be modified by the user or the runtime. The
|
|
97
|
-
/// user must ensure that access to the contents of the buffer is properly
|
|
98
|
-
/// synchronised.
|
|
99
|
-
class JSI_EXPORT MutableBuffer {
|
|
100
|
-
public:
|
|
101
|
-
virtual ~MutableBuffer();
|
|
102
|
-
virtual size_t size() const = 0;
|
|
103
|
-
virtual uint8_t* data() = 0;
|
|
104
|
-
};
|
|
105
|
-
#endif
|
|
106
|
-
|
|
107
|
-
/// PreparedJavaScript is a base class representing JavaScript which is in a
|
|
108
|
-
/// form optimized for execution, in a runtime-specific way. Construct one via
|
|
109
|
-
/// jsi::Runtime::prepareJavaScript().
|
|
110
|
-
/// ** This is an experimental API that is subject to change. **
|
|
111
|
-
class JSI_EXPORT PreparedJavaScript {
|
|
112
|
-
protected:
|
|
113
|
-
PreparedJavaScript() = default;
|
|
114
|
-
|
|
115
|
-
public:
|
|
116
|
-
virtual ~PreparedJavaScript() = 0;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
class Runtime;
|
|
120
|
-
class Pointer;
|
|
121
|
-
class PropNameID;
|
|
122
|
-
class Symbol;
|
|
123
|
-
#if JSI_VERSION >= 6
|
|
124
|
-
class BigInt;
|
|
125
|
-
#endif
|
|
126
|
-
class String;
|
|
127
|
-
class Object;
|
|
128
|
-
class WeakObject;
|
|
129
|
-
class Array;
|
|
130
|
-
class ArrayBuffer;
|
|
131
|
-
class Function;
|
|
132
|
-
class Value;
|
|
133
|
-
class Instrumentation;
|
|
134
|
-
class Scope;
|
|
135
|
-
class JSIException;
|
|
136
|
-
class JSError;
|
|
137
|
-
|
|
138
|
-
/// A function which has this type can be registered as a function
|
|
139
|
-
/// callable from JavaScript using Function::createFromHostFunction().
|
|
140
|
-
/// When the function is called, args will point to the arguments, and
|
|
141
|
-
/// count will indicate how many arguments are passed. The function
|
|
142
|
-
/// can return a Value to the caller, or throw an exception. If a C++
|
|
143
|
-
/// exception is thrown, a JS Error will be created and thrown into
|
|
144
|
-
/// JS; if the C++ exception extends std::exception, the Error's
|
|
145
|
-
/// message will be whatever what() returns. Note that it is undefined whether
|
|
146
|
-
/// HostFunctions may or may not be called in strict mode; that is `thisVal`
|
|
147
|
-
/// can be any value - it will not necessarily be coerced to an object or
|
|
148
|
-
/// or set to the global object.
|
|
149
|
-
using HostFunctionType = std::function<
|
|
150
|
-
Value(Runtime& rt, const Value& thisVal, const Value* args, size_t count)>;
|
|
151
|
-
|
|
152
|
-
/// An object which implements this interface can be registered as an
|
|
153
|
-
/// Object with the JS runtime.
|
|
154
|
-
class JSI_EXPORT HostObject {
|
|
155
|
-
public:
|
|
156
|
-
// The C++ object's dtor will be called when the GC finalizes this
|
|
157
|
-
// object. (This may be as late as when the Runtime is shut down.)
|
|
158
|
-
// You have no control over which thread it is called on. This will
|
|
159
|
-
// be called from inside the GC, so it is unsafe to do any VM
|
|
160
|
-
// operations which require a Runtime&. Derived classes' dtors
|
|
161
|
-
// should also avoid doing anything expensive. Calling the dtor on
|
|
162
|
-
// a jsi object is explicitly ok. If you want to do JS operations,
|
|
163
|
-
// or any nontrivial work, you should add it to a work queue, and
|
|
164
|
-
// manage it externally.
|
|
165
|
-
virtual ~HostObject();
|
|
166
|
-
|
|
167
|
-
// When JS wants a property with a given name from the HostObject,
|
|
168
|
-
// it will call this method. If it throws an exception, the call
|
|
169
|
-
// will throw a JS \c Error object. By default this returns undefined.
|
|
170
|
-
// \return the value for the property.
|
|
171
|
-
virtual Value get(Runtime&, const PropNameID& name);
|
|
172
|
-
|
|
173
|
-
// When JS wants to set a property with a given name on the HostObject,
|
|
174
|
-
// it will call this method. If it throws an exception, the call will
|
|
175
|
-
// throw a JS \c Error object. By default this throws a type error exception
|
|
176
|
-
// mimicking the behavior of a frozen object in strict mode.
|
|
177
|
-
virtual void set(Runtime&, const PropNameID& name, const Value& value);
|
|
178
|
-
|
|
179
|
-
// When JS wants a list of property names for the HostObject, it will
|
|
180
|
-
// call this method. If it throws an exception, the call will throw a
|
|
181
|
-
// JS \c Error object. The default implementation returns empty vector.
|
|
182
|
-
virtual std::vector<PropNameID> getPropertyNames(Runtime& rt);
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
#if JSI_VERSION >= 7
|
|
186
|
-
/// Native state (and destructor) that can be attached to any JS object
|
|
187
|
-
/// using setNativeState.
|
|
188
|
-
class JSI_EXPORT NativeState {
|
|
189
|
-
public:
|
|
190
|
-
virtual ~NativeState();
|
|
191
|
-
};
|
|
192
|
-
#endif
|
|
193
|
-
|
|
194
|
-
/// Represents a JS runtime. Movable, but not copyable. Note that
|
|
195
|
-
/// this object may not be thread-aware, but cannot be used safely from
|
|
196
|
-
/// multiple threads at once. The application is responsible for
|
|
197
|
-
/// ensuring that it is used safely. This could mean using the
|
|
198
|
-
/// Runtime from a single thread, using a mutex, doing all work on a
|
|
199
|
-
/// serial queue, etc. This restriction applies to the methods of
|
|
200
|
-
/// this class, and any method in the API which take a Runtime& as an
|
|
201
|
-
/// argument. Destructors (all but ~Scope), operators, or other methods
|
|
202
|
-
/// which do not take Runtime& as an argument are safe to call from any
|
|
203
|
-
/// thread, but it is still forbidden to make write operations on a single
|
|
204
|
-
/// instance of any class from more than one thread. In addition, to
|
|
205
|
-
/// make shutdown safe, destruction of objects associated with the Runtime
|
|
206
|
-
/// must be destroyed before the Runtime is destroyed, or from the
|
|
207
|
-
/// destructor of a managed HostObject or HostFunction. Informally, this
|
|
208
|
-
/// means that the main source of unsafe behavior is to hold a jsi object
|
|
209
|
-
/// in a non-Runtime-managed object, and not clean it up before the Runtime
|
|
210
|
-
/// is shut down. If your lifecycle is such that avoiding this is hard,
|
|
211
|
-
/// you will probably need to do use your own locks.
|
|
212
|
-
class JSI_EXPORT Runtime {
|
|
213
|
-
public:
|
|
214
|
-
virtual ~Runtime();
|
|
215
|
-
|
|
216
|
-
/// Evaluates the given JavaScript \c buffer. \c sourceURL is used
|
|
217
|
-
/// to annotate the stack trace if there is an exception. The
|
|
218
|
-
/// contents may be utf8-encoded JS source code, or binary bytecode
|
|
219
|
-
/// whose format is specific to the implementation. If the input
|
|
220
|
-
/// format is unknown, or evaluation causes an error, a JSIException
|
|
221
|
-
/// will be thrown.
|
|
222
|
-
/// Note this function should ONLY be used when there isn't another means
|
|
223
|
-
/// through the JSI API. For example, it will be much slower to use this to
|
|
224
|
-
/// call a global function than using the JSI APIs to read the function
|
|
225
|
-
/// property from the global object and then calling it explicitly.
|
|
226
|
-
virtual Value evaluateJavaScript(
|
|
227
|
-
const std::shared_ptr<const Buffer>& buffer,
|
|
228
|
-
const std::string& sourceURL) = 0;
|
|
229
|
-
|
|
230
|
-
/// Prepares to evaluate the given JavaScript \c buffer by processing it into
|
|
231
|
-
/// a form optimized for execution. This may include pre-parsing, compiling,
|
|
232
|
-
/// etc. If the input is invalid (for example, cannot be parsed), a
|
|
233
|
-
/// JSIException will be thrown. The resulting object is tied to the
|
|
234
|
-
/// particular concrete type of Runtime from which it was created. It may be
|
|
235
|
-
/// used (via evaluatePreparedJavaScript) in any Runtime of the same concrete
|
|
236
|
-
/// type.
|
|
237
|
-
/// The PreparedJavaScript object may be passed to multiple VM instances, so
|
|
238
|
-
/// they can all share and benefit from the prepared script.
|
|
239
|
-
/// As with evaluateJavaScript(), using JavaScript code should be avoided
|
|
240
|
-
/// when the JSI API is sufficient.
|
|
241
|
-
virtual std::shared_ptr<const PreparedJavaScript> prepareJavaScript(
|
|
242
|
-
const std::shared_ptr<const Buffer>& buffer,
|
|
243
|
-
std::string sourceURL) = 0;
|
|
244
|
-
|
|
245
|
-
/// Evaluates a PreparedJavaScript. If evaluation causes an error, a
|
|
246
|
-
/// JSIException will be thrown.
|
|
247
|
-
/// As with evaluateJavaScript(), using JavaScript code should be avoided
|
|
248
|
-
/// when the JSI API is sufficient.
|
|
249
|
-
virtual Value evaluatePreparedJavaScript(
|
|
250
|
-
const std::shared_ptr<const PreparedJavaScript>& js) = 0;
|
|
251
|
-
|
|
252
|
-
#if JSI_VERSION >= 12
|
|
253
|
-
/// Queues a microtask in the JavaScript VM internal Microtask (a.k.a. Job in
|
|
254
|
-
/// ECMA262) queue, to be executed when the host drains microtasks in
|
|
255
|
-
/// its event loop implementation.
|
|
256
|
-
///
|
|
257
|
-
/// \param callback a function to be executed as a microtask.
|
|
258
|
-
virtual void queueMicrotask(const jsi::Function& callback) = 0;
|
|
259
|
-
#endif
|
|
260
|
-
|
|
261
|
-
#if JSI_VERSION >= 4
|
|
262
|
-
/// Drain the JavaScript VM internal Microtask (a.k.a. Job in ECMA262) queue.
|
|
263
|
-
///
|
|
264
|
-
/// \param maxMicrotasksHint a hint to tell an implementation that it should
|
|
265
|
-
/// make a best effort not execute more than the given number. It's default
|
|
266
|
-
/// to -1 for infinity (unbounded execution).
|
|
267
|
-
/// \return true if the queue is drained or false if there is more work to do.
|
|
268
|
-
///
|
|
269
|
-
/// When there were exceptions thrown from the execution of microtasks,
|
|
270
|
-
/// implementations shall discard the exceptional jobs. An implementation may
|
|
271
|
-
/// \throw a \c JSError object to signal the hosts to handle. In that case, an
|
|
272
|
-
/// implementation may or may not suspend the draining.
|
|
273
|
-
///
|
|
274
|
-
/// Hosts may call this function again to resume the draining if it was
|
|
275
|
-
/// suspended due to either exceptions or the \p maxMicrotasksHint bound.
|
|
276
|
-
/// E.g. a host may repetitively invoke this function until the queue is
|
|
277
|
-
/// drained to implement the "microtask checkpoint" defined in WHATWG HTML
|
|
278
|
-
/// event loop: https://html.spec.whatwg.org/C#perform-a-microtask-checkpoint.
|
|
279
|
-
///
|
|
280
|
-
/// Note that error propagation is only a concern if a host needs to implement
|
|
281
|
-
/// `queueMicrotask`, a recent API that allows enqueueing arbitrary functions
|
|
282
|
-
/// (hence may throw) as microtasks. Exceptions from ECMA-262 Promise Jobs are
|
|
283
|
-
/// handled internally to VMs and are never propagated to hosts.
|
|
284
|
-
///
|
|
285
|
-
/// This API offers some queue management to hosts at its best effort due to
|
|
286
|
-
/// different behaviors and limitations imposed by different VMs and APIs. By
|
|
287
|
-
/// the time this is written, An implementation may swallow exceptions (JSC),
|
|
288
|
-
/// may not pause (V8), and may not support bounded executions.
|
|
289
|
-
virtual bool drainMicrotasks(int maxMicrotasksHint = -1) = 0;
|
|
290
|
-
#endif
|
|
291
|
-
|
|
292
|
-
/// \return the global object
|
|
293
|
-
virtual Object global() = 0;
|
|
294
|
-
|
|
295
|
-
/// \return a short printable description of the instance. It should
|
|
296
|
-
/// at least include some human-readable indication of the runtime
|
|
297
|
-
/// implementation. This should only be used by logging, debugging,
|
|
298
|
-
/// and other developer-facing callers.
|
|
299
|
-
virtual std::string description() = 0;
|
|
300
|
-
|
|
301
|
-
/// \return whether or not the underlying runtime supports debugging via the
|
|
302
|
-
/// Chrome remote debugging protocol.
|
|
303
|
-
///
|
|
304
|
-
/// NOTE: the API for determining whether a runtime is debuggable and
|
|
305
|
-
/// registering a runtime with the debugger is still in flux, so please don't
|
|
306
|
-
/// use this API unless you know what you're doing.
|
|
307
|
-
virtual bool isInspectable() = 0;
|
|
308
|
-
|
|
309
|
-
/// \return an interface to extract metrics from this \c Runtime. The default
|
|
310
|
-
/// implementation of this function returns an \c Instrumentation instance
|
|
311
|
-
/// which returns no metrics.
|
|
312
|
-
virtual Instrumentation& instrumentation();
|
|
313
|
-
|
|
314
|
-
protected:
|
|
315
|
-
friend class Pointer;
|
|
316
|
-
friend class PropNameID;
|
|
317
|
-
friend class Symbol;
|
|
318
|
-
#if JSI_VERSION >= 6
|
|
319
|
-
friend class BigInt;
|
|
320
|
-
#endif
|
|
321
|
-
friend class String;
|
|
322
|
-
friend class Object;
|
|
323
|
-
friend class WeakObject;
|
|
324
|
-
friend class Array;
|
|
325
|
-
friend class ArrayBuffer;
|
|
326
|
-
friend class Function;
|
|
327
|
-
friend class Value;
|
|
328
|
-
friend class Scope;
|
|
329
|
-
friend class JSError;
|
|
330
|
-
|
|
331
|
-
// Potential optimization: avoid the cloneFoo() virtual dispatch,
|
|
332
|
-
// and instead just fix the number of fields, and copy them, since
|
|
333
|
-
// in practice they are trivially copyable. Sufficient use of
|
|
334
|
-
// rvalue arguments/methods would also reduce the number of clones.
|
|
335
|
-
|
|
336
|
-
struct PointerValue {
|
|
337
|
-
virtual void invalidate() JSI_NOEXCEPT_15 = 0;
|
|
338
|
-
|
|
339
|
-
protected:
|
|
340
|
-
virtual ~PointerValue() = default;
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
virtual PointerValue* cloneSymbol(const Runtime::PointerValue* pv) = 0;
|
|
344
|
-
#if JSI_VERSION >= 6
|
|
345
|
-
virtual PointerValue* cloneBigInt(const Runtime::PointerValue* pv) = 0;
|
|
346
|
-
#endif
|
|
347
|
-
virtual PointerValue* cloneString(const Runtime::PointerValue* pv) = 0;
|
|
348
|
-
virtual PointerValue* cloneObject(const Runtime::PointerValue* pv) = 0;
|
|
349
|
-
virtual PointerValue* clonePropNameID(const Runtime::PointerValue* pv) = 0;
|
|
350
|
-
|
|
351
|
-
virtual PropNameID createPropNameIDFromAscii(
|
|
352
|
-
const char* str,
|
|
353
|
-
size_t length) = 0;
|
|
354
|
-
virtual PropNameID createPropNameIDFromUtf8(
|
|
355
|
-
const uint8_t* utf8,
|
|
356
|
-
size_t length) = 0;
|
|
357
|
-
#if JSI_VERSION >= 19
|
|
358
|
-
virtual PropNameID createPropNameIDFromUtf16(
|
|
359
|
-
const char16_t* utf16,
|
|
360
|
-
size_t length);
|
|
361
|
-
#endif
|
|
362
|
-
virtual PropNameID createPropNameIDFromString(const String& str) = 0;
|
|
363
|
-
#if JSI_VERSION >= 5
|
|
364
|
-
virtual PropNameID createPropNameIDFromSymbol(const Symbol& sym) = 0;
|
|
365
|
-
#endif
|
|
366
|
-
virtual std::string utf8(const PropNameID&) = 0;
|
|
367
|
-
virtual bool compare(const PropNameID&, const PropNameID&) = 0;
|
|
368
|
-
|
|
369
|
-
virtual std::string symbolToString(const Symbol&) = 0;
|
|
370
|
-
|
|
371
|
-
#if JSI_VERSION >= 8
|
|
372
|
-
virtual BigInt createBigIntFromInt64(int64_t) = 0;
|
|
373
|
-
virtual BigInt createBigIntFromUint64(uint64_t) = 0;
|
|
374
|
-
virtual bool bigintIsInt64(const BigInt&) = 0;
|
|
375
|
-
virtual bool bigintIsUint64(const BigInt&) = 0;
|
|
376
|
-
virtual uint64_t truncate(const BigInt&) = 0;
|
|
377
|
-
virtual String bigintToString(const BigInt&, int) = 0;
|
|
378
|
-
#endif
|
|
379
|
-
|
|
380
|
-
virtual String createStringFromAscii(const char* str, size_t length) = 0;
|
|
381
|
-
virtual String createStringFromUtf8(const uint8_t* utf8, size_t length) = 0;
|
|
382
|
-
#if JSI_VERSION >= 19
|
|
383
|
-
virtual String createStringFromUtf16(const char16_t* utf16, size_t length);
|
|
384
|
-
#endif
|
|
385
|
-
virtual std::string utf8(const String&) = 0;
|
|
386
|
-
|
|
387
|
-
// \return a \c Value created from a utf8-encoded JSON string. The default
|
|
388
|
-
// implementation creates a \c String and invokes JSON.parse.
|
|
389
|
-
#if JSI_VERSION >= 2
|
|
390
|
-
virtual Value createValueFromJsonUtf8(const uint8_t* json, size_t length);
|
|
391
|
-
#endif
|
|
392
|
-
|
|
393
|
-
virtual Object createObject() = 0;
|
|
394
|
-
virtual Object createObject(std::shared_ptr<HostObject> ho) = 0;
|
|
395
|
-
virtual std::shared_ptr<HostObject> getHostObject(const jsi::Object&) = 0;
|
|
396
|
-
virtual HostFunctionType& getHostFunction(const jsi::Function&) = 0;
|
|
397
|
-
|
|
398
|
-
#if JSI_VERSION >= 18
|
|
399
|
-
// Creates a new Object with the custom prototype
|
|
400
|
-
virtual Object createObjectWithPrototype(const Value& prototype);
|
|
401
|
-
#endif
|
|
402
|
-
|
|
403
|
-
#if JSI_VERSION >= 7
|
|
404
|
-
virtual bool hasNativeState(const jsi::Object&) = 0;
|
|
405
|
-
virtual std::shared_ptr<NativeState> getNativeState(const jsi::Object&) = 0;
|
|
406
|
-
virtual void setNativeState(
|
|
407
|
-
const jsi::Object&,
|
|
408
|
-
std::shared_ptr<NativeState> state) = 0;
|
|
409
|
-
#endif
|
|
410
|
-
|
|
411
|
-
#if JSI_VERSION >= 17
|
|
412
|
-
virtual void setPrototypeOf(const Object& object, const Value& prototype);
|
|
413
|
-
virtual Value getPrototypeOf(const Object& object);
|
|
414
|
-
#endif
|
|
415
|
-
|
|
416
|
-
virtual Value getProperty(const Object&, const PropNameID& name) = 0;
|
|
417
|
-
virtual Value getProperty(const Object&, const String& name) = 0;
|
|
418
|
-
virtual bool hasProperty(const Object&, const PropNameID& name) = 0;
|
|
419
|
-
virtual bool hasProperty(const Object&, const String& name) = 0;
|
|
420
|
-
virtual void setPropertyValue(
|
|
421
|
-
JSI_CONST_10 Object&,
|
|
422
|
-
const PropNameID& name,
|
|
423
|
-
const Value& value) = 0;
|
|
424
|
-
virtual void setPropertyValue(
|
|
425
|
-
JSI_CONST_10 Object&,
|
|
426
|
-
const String& name,
|
|
427
|
-
const Value& value) = 0;
|
|
428
|
-
|
|
429
|
-
virtual bool isArray(const Object&) const = 0;
|
|
430
|
-
virtual bool isArrayBuffer(const Object&) const = 0;
|
|
431
|
-
virtual bool isFunction(const Object&) const = 0;
|
|
432
|
-
virtual bool isHostObject(const jsi::Object&) const = 0;
|
|
433
|
-
virtual bool isHostFunction(const jsi::Function&) const = 0;
|
|
434
|
-
virtual Array getPropertyNames(const Object&) = 0;
|
|
435
|
-
|
|
436
|
-
virtual WeakObject createWeakObject(const Object&) = 0;
|
|
437
|
-
virtual Value lockWeakObject(JSI_NO_CONST_3 JSI_CONST_10 WeakObject&) = 0;
|
|
438
|
-
|
|
439
|
-
virtual Array createArray(size_t length) = 0;
|
|
440
|
-
#if JSI_VERSION >= 9
|
|
441
|
-
virtual ArrayBuffer createArrayBuffer(
|
|
442
|
-
std::shared_ptr<MutableBuffer> buffer) = 0;
|
|
443
|
-
#endif
|
|
444
|
-
virtual size_t size(const Array&) = 0;
|
|
445
|
-
virtual size_t size(const ArrayBuffer&) = 0;
|
|
446
|
-
virtual uint8_t* data(const ArrayBuffer&) = 0;
|
|
447
|
-
virtual Value getValueAtIndex(const Array&, size_t i) = 0;
|
|
448
|
-
virtual void
|
|
449
|
-
setValueAtIndexImpl(JSI_CONST_10 Array&, size_t i, const Value& value) = 0;
|
|
450
|
-
|
|
451
|
-
virtual Function createFunctionFromHostFunction(
|
|
452
|
-
const PropNameID& name,
|
|
453
|
-
unsigned int paramCount,
|
|
454
|
-
HostFunctionType func) = 0;
|
|
455
|
-
virtual Value call(
|
|
456
|
-
const Function&,
|
|
457
|
-
const Value& jsThis,
|
|
458
|
-
const Value* args,
|
|
459
|
-
size_t count) = 0;
|
|
460
|
-
virtual Value
|
|
461
|
-
callAsConstructor(const Function&, const Value* args, size_t count) = 0;
|
|
462
|
-
|
|
463
|
-
// Private data for managing scopes.
|
|
464
|
-
struct ScopeState;
|
|
465
|
-
virtual ScopeState* pushScope();
|
|
466
|
-
virtual void popScope(ScopeState*);
|
|
467
|
-
|
|
468
|
-
virtual bool strictEquals(const Symbol& a, const Symbol& b) const = 0;
|
|
469
|
-
#if JSI_VERSION >= 6
|
|
470
|
-
virtual bool strictEquals(const BigInt& a, const BigInt& b) const = 0;
|
|
471
|
-
#endif
|
|
472
|
-
virtual bool strictEquals(const String& a, const String& b) const = 0;
|
|
473
|
-
virtual bool strictEquals(const Object& a, const Object& b) const = 0;
|
|
474
|
-
|
|
475
|
-
virtual bool instanceOf(const Object& o, const Function& f) = 0;
|
|
476
|
-
|
|
477
|
-
#if JSI_VERSION >= 11
|
|
478
|
-
/// See Object::setExternalMemoryPressure.
|
|
479
|
-
virtual void setExternalMemoryPressure(
|
|
480
|
-
const jsi::Object& obj,
|
|
481
|
-
size_t amount) = 0;
|
|
482
|
-
#endif
|
|
483
|
-
|
|
484
|
-
#if JSI_VERSION >= 14
|
|
485
|
-
virtual std::u16string utf16(const String& str);
|
|
486
|
-
virtual std::u16string utf16(const PropNameID& sym);
|
|
487
|
-
#endif
|
|
488
|
-
|
|
489
|
-
#if JSI_VERSION >= 16
|
|
490
|
-
/// Invokes the provided callback \p cb with the String content in \p str.
|
|
491
|
-
/// The callback must take in three arguments: bool ascii, const void* data,
|
|
492
|
-
/// and size_t num, respectively. \p ascii indicates whether the \p data
|
|
493
|
-
/// passed to the callback should be interpreted as a pointer to a sequence of
|
|
494
|
-
/// \p num ASCII characters or UTF16 characters. Depending on the internal
|
|
495
|
-
/// representation of the string, the function may invoke the callback
|
|
496
|
-
/// multiple times, with a different format on each invocation. The callback
|
|
497
|
-
/// must not access runtime functionality, as any operation on the runtime may
|
|
498
|
-
/// invalidate the data pointers.
|
|
499
|
-
virtual void getStringData(
|
|
500
|
-
const jsi::String& str,
|
|
501
|
-
void* ctx,
|
|
502
|
-
void (*cb)(void* ctx, bool ascii, const void* data, size_t num));
|
|
503
|
-
|
|
504
|
-
/// Invokes the provided callback \p cb with the PropNameID content in \p sym.
|
|
505
|
-
/// The callback must take in three arguments: bool ascii, const void* data,
|
|
506
|
-
/// and size_t num, respectively. \p ascii indicates whether the \p data
|
|
507
|
-
/// passed to the callback should be interpreted as a pointer to a sequence of
|
|
508
|
-
/// \p num ASCII characters or UTF16 characters. Depending on the internal
|
|
509
|
-
/// representation of the string, the function may invoke the callback
|
|
510
|
-
/// multiple times, with a different format on each invocation. The callback
|
|
511
|
-
/// must not access runtime functionality, as any operation on the runtime may
|
|
512
|
-
/// invalidate the data pointers.
|
|
513
|
-
virtual void getPropNameIdData(
|
|
514
|
-
const jsi::PropNameID& sym,
|
|
515
|
-
void* ctx,
|
|
516
|
-
void (*cb)(void* ctx, bool ascii, const void* data, size_t num));
|
|
517
|
-
#endif
|
|
518
|
-
|
|
519
|
-
// These exist so derived classes can access the private parts of
|
|
520
|
-
// Value, Symbol, String, and Object, which are all friends of Runtime.
|
|
521
|
-
template <typename T>
|
|
522
|
-
static T make(PointerValue* pv);
|
|
523
|
-
#if JSI_VERSION >= 3
|
|
524
|
-
static PointerValue* getPointerValue(Pointer& pointer);
|
|
525
|
-
#endif
|
|
526
|
-
static const PointerValue* getPointerValue(const Pointer& pointer);
|
|
527
|
-
static const PointerValue* getPointerValue(const Value& value);
|
|
528
|
-
|
|
529
|
-
friend class ::FBJSRuntime;
|
|
530
|
-
template <typename Plain, typename Base>
|
|
531
|
-
friend class RuntimeDecorator;
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
// Base class for pointer-storing types.
|
|
535
|
-
class JSI_EXPORT Pointer {
|
|
536
|
-
protected:
|
|
537
|
-
explicit Pointer(Pointer&& other) JSI_NOEXCEPT_15 : ptr_(other.ptr_) {
|
|
538
|
-
other.ptr_ = nullptr;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
~Pointer() {
|
|
542
|
-
if (ptr_) {
|
|
543
|
-
ptr_->invalidate();
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
Pointer& operator=(Pointer&& other) JSI_NOEXCEPT_15;
|
|
548
|
-
|
|
549
|
-
friend class Runtime;
|
|
550
|
-
friend class Value;
|
|
551
|
-
|
|
552
|
-
explicit Pointer(Runtime::PointerValue* ptr) : ptr_(ptr) {}
|
|
553
|
-
|
|
554
|
-
typename Runtime::PointerValue* ptr_;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
/// Represents something that can be a JS property key. Movable, not copyable.
|
|
558
|
-
class JSI_EXPORT PropNameID : public Pointer {
|
|
559
|
-
public:
|
|
560
|
-
using Pointer::Pointer;
|
|
561
|
-
|
|
562
|
-
PropNameID(Runtime& runtime, const PropNameID& other)
|
|
563
|
-
: Pointer(runtime.clonePropNameID(other.ptr_)) {}
|
|
564
|
-
|
|
565
|
-
PropNameID(PropNameID&& other) = default;
|
|
566
|
-
PropNameID& operator=(PropNameID&& other) = default;
|
|
567
|
-
|
|
568
|
-
/// Create a JS property name id from ascii values. The data is
|
|
569
|
-
/// copied.
|
|
570
|
-
static PropNameID forAscii(Runtime& runtime, const char* str, size_t length) {
|
|
571
|
-
return runtime.createPropNameIDFromAscii(str, length);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
/// Create a property name id from a nul-terminated C ascii name. The data is
|
|
575
|
-
/// copied.
|
|
576
|
-
static PropNameID forAscii(Runtime& runtime, const char* str) {
|
|
577
|
-
return forAscii(runtime, str, strlen(str));
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
/// Create a PropNameID from a C++ string. The string is copied.
|
|
581
|
-
static PropNameID forAscii(Runtime& runtime, const std::string& str) {
|
|
582
|
-
return forAscii(runtime, str.c_str(), str.size());
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
/// Create a PropNameID from utf8 values. The data is copied.
|
|
586
|
-
/// Results are undefined if \p utf8 contains invalid code points.
|
|
587
|
-
static PropNameID
|
|
588
|
-
forUtf8(Runtime& runtime, const uint8_t* utf8, size_t length) {
|
|
589
|
-
return runtime.createPropNameIDFromUtf8(utf8, length);
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/// Create a PropNameID from utf8-encoded octets stored in a
|
|
593
|
-
/// std::string. The string data is transformed and copied.
|
|
594
|
-
/// Results are undefined if \p utf8 contains invalid code points.
|
|
595
|
-
static PropNameID forUtf8(Runtime& runtime, const std::string& utf8) {
|
|
596
|
-
return runtime.createPropNameIDFromUtf8(
|
|
597
|
-
reinterpret_cast<const uint8_t*>(utf8.data()), utf8.size());
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
#if JSI_VERSION >= 19
|
|
601
|
-
/// Given a series of UTF-16 encoded code units, create a PropNameId. The
|
|
602
|
-
/// input may contain unpaired surrogates, which will be interpreted as a code
|
|
603
|
-
/// point of the same value.
|
|
604
|
-
static PropNameID
|
|
605
|
-
forUtf16(Runtime& runtime, const char16_t* utf16, size_t length) {
|
|
606
|
-
return runtime.createPropNameIDFromUtf16(utf16, length);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
/// Given a series of UTF-16 encoded code units stored inside std::u16string,
|
|
610
|
-
/// create a PropNameId. The input may contain unpaired surrogates, which
|
|
611
|
-
/// will be interpreted as a code point of the same value.
|
|
612
|
-
static PropNameID forUtf16(Runtime& runtime, const std::u16string& str) {
|
|
613
|
-
return runtime.createPropNameIDFromUtf16(str.data(), str.size());
|
|
614
|
-
}
|
|
615
|
-
#endif
|
|
616
|
-
|
|
617
|
-
/// Create a PropNameID from a JS string.
|
|
618
|
-
static PropNameID forString(Runtime& runtime, const jsi::String& str) {
|
|
619
|
-
return runtime.createPropNameIDFromString(str);
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
#if JSI_VERSION >= 5
|
|
623
|
-
/// Create a PropNameID from a JS symbol.
|
|
624
|
-
static PropNameID forSymbol(Runtime& runtime, const jsi::Symbol& sym) {
|
|
625
|
-
return runtime.createPropNameIDFromSymbol(sym);
|
|
626
|
-
}
|
|
627
|
-
#endif
|
|
628
|
-
|
|
629
|
-
// Creates a vector of PropNameIDs constructed from given arguments.
|
|
630
|
-
template <typename... Args>
|
|
631
|
-
static std::vector<PropNameID> names(Runtime& runtime, Args&&... args);
|
|
632
|
-
|
|
633
|
-
// Creates a vector of given PropNameIDs.
|
|
634
|
-
template <size_t N>
|
|
635
|
-
static std::vector<PropNameID> names(PropNameID (&&propertyNames)[N]);
|
|
636
|
-
|
|
637
|
-
/// Copies the data in a PropNameID as utf8 into a C++ string.
|
|
638
|
-
std::string utf8(Runtime& runtime) const {
|
|
639
|
-
return runtime.utf8(*this);
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
#if JSI_VERSION >= 14
|
|
643
|
-
/// Copies the data in a PropNameID as utf16 into a C++ string.
|
|
644
|
-
std::u16string utf16(Runtime& runtime) const {
|
|
645
|
-
return runtime.utf16(*this);
|
|
646
|
-
}
|
|
647
|
-
#endif
|
|
648
|
-
|
|
649
|
-
#if JSI_VERSION >= 16
|
|
650
|
-
/// Invokes the user provided callback to process the content in PropNameId.
|
|
651
|
-
/// The callback must take in three arguments: bool ascii, const void* data,
|
|
652
|
-
/// and size_t num, respectively. \p ascii indicates whether the \p data
|
|
653
|
-
/// passed to the callback should be interpreted as a pointer to a sequence of
|
|
654
|
-
/// \p num ASCII characters or UTF16 characters. The function may invoke the
|
|
655
|
-
/// callback multiple times, with a different format on each invocation. The
|
|
656
|
-
/// callback must not access runtime functionality, as any operation on the
|
|
657
|
-
/// runtime may invalidate the data pointers.
|
|
658
|
-
template <typename CB>
|
|
659
|
-
void getPropNameIdData(Runtime& runtime, CB& cb) const {
|
|
660
|
-
runtime.getPropNameIdData(
|
|
661
|
-
*this, &cb, [](void* ctx, bool ascii, const void* data, size_t num) {
|
|
662
|
-
(*((CB*)ctx))(ascii, data, num);
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
#endif
|
|
666
|
-
|
|
667
|
-
static bool compare(
|
|
668
|
-
Runtime& runtime,
|
|
669
|
-
const jsi::PropNameID& a,
|
|
670
|
-
const jsi::PropNameID& b) {
|
|
671
|
-
return runtime.compare(a, b);
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
friend class Runtime;
|
|
675
|
-
friend class Value;
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
/// Represents a JS Symbol (es6). Movable, not copyable.
|
|
679
|
-
/// TODO T40778724: this is a limited implementation sufficient for
|
|
680
|
-
/// the debugger not to crash when a Symbol is a property in an Object
|
|
681
|
-
/// or element in an array. Complete support for creating will come
|
|
682
|
-
/// later.
|
|
683
|
-
class JSI_EXPORT Symbol : public Pointer {
|
|
684
|
-
public:
|
|
685
|
-
using Pointer::Pointer;
|
|
686
|
-
|
|
687
|
-
Symbol(Symbol&& other) = default;
|
|
688
|
-
Symbol& operator=(Symbol&& other) = default;
|
|
689
|
-
|
|
690
|
-
/// \return whether a and b refer to the same symbol.
|
|
691
|
-
static bool strictEquals(Runtime& runtime, const Symbol& a, const Symbol& b) {
|
|
692
|
-
return runtime.strictEquals(a, b);
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
/// Converts a Symbol into a C++ string as JS .toString would. The output
|
|
696
|
-
/// will look like \c Symbol(description) .
|
|
697
|
-
std::string toString(Runtime& runtime) const {
|
|
698
|
-
return runtime.symbolToString(*this);
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
friend class Runtime;
|
|
702
|
-
friend class Value;
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
#if JSI_VERSION >= 6
|
|
706
|
-
/// Represents a JS BigInt. Movable, not copyable.
|
|
707
|
-
class JSI_EXPORT BigInt : public Pointer {
|
|
708
|
-
public:
|
|
709
|
-
using Pointer::Pointer;
|
|
710
|
-
|
|
711
|
-
BigInt(BigInt&& other) = default;
|
|
712
|
-
BigInt& operator=(BigInt&& other) = default;
|
|
713
|
-
|
|
714
|
-
#if JSI_VERSION >= 8
|
|
715
|
-
/// Create a BigInt representing the signed 64-bit \p value.
|
|
716
|
-
static BigInt fromInt64(Runtime& runtime, int64_t value) {
|
|
717
|
-
return runtime.createBigIntFromInt64(value);
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/// Create a BigInt representing the unsigned 64-bit \p value.
|
|
721
|
-
static BigInt fromUint64(Runtime& runtime, uint64_t value) {
|
|
722
|
-
return runtime.createBigIntFromUint64(value);
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
/// \return whether a === b.
|
|
726
|
-
static bool strictEquals(Runtime& runtime, const BigInt& a, const BigInt& b) {
|
|
727
|
-
return runtime.strictEquals(a, b);
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
/// \returns This bigint truncated to a signed 64-bit integer.
|
|
731
|
-
int64_t getInt64(Runtime& runtime) const {
|
|
732
|
-
return runtime.truncate(*this);
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
/// \returns Whether this bigint can be losslessly converted to int64_t.
|
|
736
|
-
bool isInt64(Runtime& runtime) const {
|
|
737
|
-
return runtime.bigintIsInt64(*this);
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
/// \returns This bigint truncated to a signed 64-bit integer. Throws a
|
|
741
|
-
/// JSIException if the truncation is lossy.
|
|
742
|
-
int64_t asInt64(Runtime& runtime) const;
|
|
743
|
-
|
|
744
|
-
/// \returns This bigint truncated to an unsigned 64-bit integer.
|
|
745
|
-
uint64_t getUint64(Runtime& runtime) const {
|
|
746
|
-
return runtime.truncate(*this);
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/// \returns Whether this bigint can be losslessly converted to uint64_t.
|
|
750
|
-
bool isUint64(Runtime& runtime) const {
|
|
751
|
-
return runtime.bigintIsUint64(*this);
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
/// \returns This bigint truncated to an unsigned 64-bit integer. Throws a
|
|
755
|
-
/// JSIException if the truncation is lossy.
|
|
756
|
-
uint64_t asUint64(Runtime& runtime) const;
|
|
757
|
-
|
|
758
|
-
/// \returns this BigInt converted to a String in base \p radix. Throws a
|
|
759
|
-
/// JSIException if radix is not in the [2, 36] range.
|
|
760
|
-
inline String toString(Runtime& runtime, int radix = 10) const;
|
|
761
|
-
#endif
|
|
762
|
-
|
|
763
|
-
friend class Runtime;
|
|
764
|
-
friend class Value;
|
|
765
|
-
};
|
|
766
|
-
#endif
|
|
767
|
-
|
|
768
|
-
/// Represents a JS String. Movable, not copyable.
|
|
769
|
-
class JSI_EXPORT String : public Pointer {
|
|
770
|
-
public:
|
|
771
|
-
using Pointer::Pointer;
|
|
772
|
-
|
|
773
|
-
String(String&& other) = default;
|
|
774
|
-
String& operator=(String&& other) = default;
|
|
775
|
-
|
|
776
|
-
/// Create a JS string from ascii values. The string data is
|
|
777
|
-
/// copied.
|
|
778
|
-
static String
|
|
779
|
-
createFromAscii(Runtime& runtime, const char* str, size_t length) {
|
|
780
|
-
return runtime.createStringFromAscii(str, length);
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
/// Create a JS string from a nul-terminated C ascii string. The
|
|
784
|
-
/// string data is copied.
|
|
785
|
-
static String createFromAscii(Runtime& runtime, const char* str) {
|
|
786
|
-
return createFromAscii(runtime, str, strlen(str));
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
/// Create a JS string from a C++ string. The string data is
|
|
790
|
-
/// copied.
|
|
791
|
-
static String createFromAscii(Runtime& runtime, const std::string& str) {
|
|
792
|
-
return createFromAscii(runtime, str.c_str(), str.size());
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
/// Create a JS string from utf8-encoded octets. The string data is
|
|
796
|
-
/// transformed and copied. Results are undefined if \p utf8 contains invalid
|
|
797
|
-
/// code points.
|
|
798
|
-
static String
|
|
799
|
-
createFromUtf8(Runtime& runtime, const uint8_t* utf8, size_t length) {
|
|
800
|
-
return runtime.createStringFromUtf8(utf8, length);
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
/// Create a JS string from utf8-encoded octets stored in a
|
|
804
|
-
/// std::string. The string data is transformed and copied. Results are
|
|
805
|
-
/// undefined if \p utf8 contains invalid code points.
|
|
806
|
-
static String createFromUtf8(Runtime& runtime, const std::string& utf8) {
|
|
807
|
-
return runtime.createStringFromUtf8(
|
|
808
|
-
reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
#if JSI_VERSION >= 19
|
|
812
|
-
/// Given a series of UTF-16 encoded code units, create a JS String. The input
|
|
813
|
-
/// may contain unpaired surrogates, which will be interpreted as a code point
|
|
814
|
-
/// of the same value.
|
|
815
|
-
static String
|
|
816
|
-
createFromUtf16(Runtime& runtime, const char16_t* utf16, size_t length) {
|
|
817
|
-
return runtime.createStringFromUtf16(utf16, length);
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
/// Given a series of UTF-16 encoded code units stored inside std::u16string,
|
|
821
|
-
/// create a JS String. The input may contain unpaired surrogates, which will
|
|
822
|
-
/// be interpreted as a code point of the same value.
|
|
823
|
-
static String createFromUtf16(Runtime& runtime, const std::u16string& utf16) {
|
|
824
|
-
return runtime.createStringFromUtf16(utf16.data(), utf16.length());
|
|
825
|
-
}
|
|
826
|
-
#endif
|
|
827
|
-
|
|
828
|
-
/// \return whether a and b contain the same characters.
|
|
829
|
-
static bool strictEquals(Runtime& runtime, const String& a, const String& b) {
|
|
830
|
-
return runtime.strictEquals(a, b);
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
/// Copies the data in a JS string as utf8 into a C++ string.
|
|
834
|
-
std::string utf8(Runtime& runtime) const {
|
|
835
|
-
return runtime.utf8(*this);
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
#if JSI_VERSION >= 14
|
|
839
|
-
/// Copies the data in a JS string as utf16 into a C++ string.
|
|
840
|
-
std::u16string utf16(Runtime& runtime) const {
|
|
841
|
-
return runtime.utf16(*this);
|
|
842
|
-
}
|
|
843
|
-
#endif
|
|
844
|
-
|
|
845
|
-
#if JSI_VERSION >= 16
|
|
846
|
-
/// Invokes the user provided callback to process content in String. The
|
|
847
|
-
/// callback must take in three arguments: bool ascii, const void* data, and
|
|
848
|
-
/// size_t num, respectively. \p ascii indicates whether the \p data passed to
|
|
849
|
-
/// the callback should be interpreted as a pointer to a sequence of \p num
|
|
850
|
-
/// ASCII characters or UTF16 characters. The function may invoke the callback
|
|
851
|
-
/// multiple times, with a different format on each invocation. The callback
|
|
852
|
-
/// must not access runtime functionality, as any operation on the runtime may
|
|
853
|
-
/// invalidate the data pointers.
|
|
854
|
-
template <typename CB>
|
|
855
|
-
void getStringData(Runtime& runtime, CB& cb) const {
|
|
856
|
-
runtime.getStringData(
|
|
857
|
-
*this, &cb, [](void* ctx, bool ascii, const void* data, size_t num) {
|
|
858
|
-
(*((CB*)ctx))(ascii, data, num);
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
#endif
|
|
862
|
-
|
|
863
|
-
friend class Runtime;
|
|
864
|
-
friend class Value;
|
|
865
|
-
};
|
|
866
|
-
|
|
867
|
-
class Array;
|
|
868
|
-
class Function;
|
|
869
|
-
|
|
870
|
-
/// Represents a JS Object. Movable, not copyable.
|
|
871
|
-
class JSI_EXPORT Object : public Pointer {
|
|
872
|
-
public:
|
|
873
|
-
using Pointer::Pointer;
|
|
874
|
-
|
|
875
|
-
Object(Object&& other) = default;
|
|
876
|
-
Object& operator=(Object&& other) = default;
|
|
877
|
-
|
|
878
|
-
/// Creates a new Object instance, like '{}' in JS.
|
|
879
|
-
Object(Runtime& runtime) : Object(runtime.createObject()) {}
|
|
880
|
-
|
|
881
|
-
static Object createFromHostObject(
|
|
882
|
-
Runtime& runtime,
|
|
883
|
-
std::shared_ptr<HostObject> ho) {
|
|
884
|
-
return runtime.createObject(ho);
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
#if JSI_VERSION >= 18
|
|
888
|
-
/// Creates a new Object with the custom prototype
|
|
889
|
-
static Object create(Runtime& runtime, const Value& prototype) {
|
|
890
|
-
return runtime.createObjectWithPrototype(prototype);
|
|
891
|
-
}
|
|
892
|
-
#endif
|
|
893
|
-
|
|
894
|
-
/// \return whether this and \c obj are the same JSObject or not.
|
|
895
|
-
static bool strictEquals(Runtime& runtime, const Object& a, const Object& b) {
|
|
896
|
-
return runtime.strictEquals(a, b);
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
/// \return the result of `this instanceOf ctor` in JS.
|
|
900
|
-
bool instanceOf(Runtime& rt, const Function& ctor) JSI_CONST_10 {
|
|
901
|
-
return rt.instanceOf(*this, ctor);
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
#if JSI_VERSION >= 17
|
|
905
|
-
/// Sets \p prototype as the prototype of the object. The prototype must be
|
|
906
|
-
/// either an Object or null. If the prototype was not set successfully, this
|
|
907
|
-
/// method will throw.
|
|
908
|
-
void setPrototype(Runtime& runtime, const Value& prototype) const {
|
|
909
|
-
return runtime.setPrototypeOf(*this, prototype);
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
/// \return the prototype of the object
|
|
913
|
-
inline Value getPrototype(Runtime& runtime) const;
|
|
914
|
-
#endif
|
|
915
|
-
|
|
916
|
-
/// \return the property of the object with the given ascii name.
|
|
917
|
-
/// If the name isn't a property on the object, returns the
|
|
918
|
-
/// undefined value.
|
|
919
|
-
Value getProperty(Runtime& runtime, const char* name) const;
|
|
920
|
-
|
|
921
|
-
/// \return the property of the object with the String name.
|
|
922
|
-
/// If the name isn't a property on the object, returns the
|
|
923
|
-
/// undefined value.
|
|
924
|
-
Value getProperty(Runtime& runtime, const String& name) const;
|
|
925
|
-
|
|
926
|
-
/// \return the property of the object with the given JS PropNameID
|
|
927
|
-
/// name. If the name isn't a property on the object, returns the
|
|
928
|
-
/// undefined value.
|
|
929
|
-
Value getProperty(Runtime& runtime, const PropNameID& name) const;
|
|
930
|
-
|
|
931
|
-
/// \return true if and only if the object has a property with the
|
|
932
|
-
/// given ascii name.
|
|
933
|
-
bool hasProperty(Runtime& runtime, const char* name) const;
|
|
934
|
-
|
|
935
|
-
/// \return true if and only if the object has a property with the
|
|
936
|
-
/// given String name.
|
|
937
|
-
bool hasProperty(Runtime& runtime, const String& name) const;
|
|
938
|
-
|
|
939
|
-
/// \return true if and only if the object has a property with the
|
|
940
|
-
/// given PropNameID name.
|
|
941
|
-
bool hasProperty(Runtime& runtime, const PropNameID& name) const;
|
|
942
|
-
|
|
943
|
-
/// Sets the property value from a Value or anything which can be
|
|
944
|
-
/// used to make one: nullptr_t, bool, double, int, const char*,
|
|
945
|
-
/// String, or Object.
|
|
946
|
-
template <typename T>
|
|
947
|
-
void setProperty(Runtime& runtime, const char* name, T&& value) JSI_CONST_10;
|
|
948
|
-
|
|
949
|
-
/// Sets the property value from a Value or anything which can be
|
|
950
|
-
/// used to make one: nullptr_t, bool, double, int, const char*,
|
|
951
|
-
/// String, or Object.
|
|
952
|
-
template <typename T>
|
|
953
|
-
void setProperty(Runtime& runtime, const String& name, T&& value)
|
|
954
|
-
JSI_CONST_10;
|
|
955
|
-
|
|
956
|
-
/// Sets the property value from a Value or anything which can be
|
|
957
|
-
/// used to make one: nullptr_t, bool, double, int, const char*,
|
|
958
|
-
/// String, or Object.
|
|
959
|
-
template <typename T>
|
|
960
|
-
void setProperty(Runtime& runtime, const PropNameID& name, T&& value)
|
|
961
|
-
JSI_CONST_10;
|
|
962
|
-
|
|
963
|
-
/// \return true iff JS \c Array.isArray() would return \c true. If
|
|
964
|
-
/// so, then \c getArray() will succeed.
|
|
965
|
-
bool isArray(Runtime& runtime) const {
|
|
966
|
-
return runtime.isArray(*this);
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
/// \return true iff the Object is an ArrayBuffer. If so, then \c
|
|
970
|
-
/// getArrayBuffer() will succeed.
|
|
971
|
-
bool isArrayBuffer(Runtime& runtime) const {
|
|
972
|
-
return runtime.isArrayBuffer(*this);
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
/// \return true iff the Object is callable. If so, then \c
|
|
976
|
-
/// getFunction will succeed.
|
|
977
|
-
bool isFunction(Runtime& runtime) const {
|
|
978
|
-
return runtime.isFunction(*this);
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
/// \return true iff the Object was initialized with \c createFromHostObject
|
|
982
|
-
/// and the HostObject passed is of type \c T. If returns \c true then
|
|
983
|
-
/// \c getHostObject<T> will succeed.
|
|
984
|
-
template <typename T = HostObject>
|
|
985
|
-
bool isHostObject(Runtime& runtime) const;
|
|
986
|
-
|
|
987
|
-
/// \return an Array instance which refers to the same underlying
|
|
988
|
-
/// object. If \c isArray() would return false, this will assert.
|
|
989
|
-
Array getArray(Runtime& runtime) const&;
|
|
990
|
-
|
|
991
|
-
/// \return an Array instance which refers to the same underlying
|
|
992
|
-
/// object. If \c isArray() would return false, this will assert.
|
|
993
|
-
Array getArray(Runtime& runtime) &&;
|
|
994
|
-
|
|
995
|
-
/// \return an Array instance which refers to the same underlying
|
|
996
|
-
/// object. If \c isArray() would return false, this will throw
|
|
997
|
-
/// JSIException.
|
|
998
|
-
Array asArray(Runtime& runtime) const&;
|
|
999
|
-
|
|
1000
|
-
/// \return an Array instance which refers to the same underlying
|
|
1001
|
-
/// object. If \c isArray() would return false, this will throw
|
|
1002
|
-
/// JSIException.
|
|
1003
|
-
Array asArray(Runtime& runtime) &&;
|
|
1004
|
-
|
|
1005
|
-
/// \return an ArrayBuffer instance which refers to the same underlying
|
|
1006
|
-
/// object. If \c isArrayBuffer() would return false, this will assert.
|
|
1007
|
-
ArrayBuffer getArrayBuffer(Runtime& runtime) const&;
|
|
1008
|
-
|
|
1009
|
-
/// \return an ArrayBuffer instance which refers to the same underlying
|
|
1010
|
-
/// object. If \c isArrayBuffer() would return false, this will assert.
|
|
1011
|
-
ArrayBuffer getArrayBuffer(Runtime& runtime) &&;
|
|
1012
|
-
|
|
1013
|
-
/// \return a Function instance which refers to the same underlying
|
|
1014
|
-
/// object. If \c isFunction() would return false, this will assert.
|
|
1015
|
-
Function getFunction(Runtime& runtime) const&;
|
|
1016
|
-
|
|
1017
|
-
/// \return a Function instance which refers to the same underlying
|
|
1018
|
-
/// object. If \c isFunction() would return false, this will assert.
|
|
1019
|
-
Function getFunction(Runtime& runtime) &&;
|
|
1020
|
-
|
|
1021
|
-
/// \return a Function instance which refers to the same underlying
|
|
1022
|
-
/// object. If \c isFunction() would return false, this will throw
|
|
1023
|
-
/// JSIException.
|
|
1024
|
-
Function asFunction(Runtime& runtime) const&;
|
|
1025
|
-
|
|
1026
|
-
/// \return a Function instance which refers to the same underlying
|
|
1027
|
-
/// object. If \c isFunction() would return false, this will throw
|
|
1028
|
-
/// JSIException.
|
|
1029
|
-
Function asFunction(Runtime& runtime) &&;
|
|
1030
|
-
|
|
1031
|
-
/// \return a shared_ptr<T> which refers to the same underlying
|
|
1032
|
-
/// \c HostObject that was used to create this object. If \c isHostObject<T>
|
|
1033
|
-
/// is false, this will assert. Note that this does a type check and will
|
|
1034
|
-
/// assert if the underlying HostObject isn't of type \c T
|
|
1035
|
-
template <typename T = HostObject>
|
|
1036
|
-
std::shared_ptr<T> getHostObject(Runtime& runtime) const;
|
|
1037
|
-
|
|
1038
|
-
/// \return a shared_ptr<T> which refers to the same underlying
|
|
1039
|
-
/// \c HostObject that was used to create this object. If \c isHostObject<T>
|
|
1040
|
-
/// is false, this will throw.
|
|
1041
|
-
template <typename T = HostObject>
|
|
1042
|
-
std::shared_ptr<T> asHostObject(Runtime& runtime) const;
|
|
1043
|
-
|
|
1044
|
-
#if JSI_VERSION >= 7
|
|
1045
|
-
/// \return whether this object has native state of type T previously set by
|
|
1046
|
-
/// \c setNativeState.
|
|
1047
|
-
template <typename T = NativeState>
|
|
1048
|
-
bool hasNativeState(Runtime& runtime) const;
|
|
1049
|
-
|
|
1050
|
-
/// \return a shared_ptr to the state previously set by \c setNativeState.
|
|
1051
|
-
/// If \c hasNativeState<T> is false, this will assert. Note that this does a
|
|
1052
|
-
/// type check and will assert if the native state isn't of type \c T
|
|
1053
|
-
template <typename T = NativeState>
|
|
1054
|
-
std::shared_ptr<T> getNativeState(Runtime& runtime) const;
|
|
1055
|
-
|
|
1056
|
-
/// Set the internal native state property of this object, overwriting any old
|
|
1057
|
-
/// value. Creates a new shared_ptr to the object managed by \p state, which
|
|
1058
|
-
/// will live until the value at this property becomes unreachable.
|
|
1059
|
-
///
|
|
1060
|
-
/// Throws a type error if this object is a proxy or host object.
|
|
1061
|
-
void setNativeState(Runtime& runtime, std::shared_ptr<NativeState> state)
|
|
1062
|
-
const;
|
|
1063
|
-
#endif
|
|
1064
|
-
|
|
1065
|
-
/// \return same as \c getProperty(name).asObject(), except with
|
|
1066
|
-
/// a better exception message.
|
|
1067
|
-
Object getPropertyAsObject(Runtime& runtime, const char* name) const;
|
|
1068
|
-
|
|
1069
|
-
/// \return similar to \c
|
|
1070
|
-
/// getProperty(name).getObject().getFunction(), except it will
|
|
1071
|
-
/// throw JSIException instead of asserting if the property is
|
|
1072
|
-
/// not an object, or the object is not callable.
|
|
1073
|
-
Function getPropertyAsFunction(Runtime& runtime, const char* name) const;
|
|
1074
|
-
|
|
1075
|
-
/// \return an Array consisting of all enumerable property names in
|
|
1076
|
-
/// the object and its prototype chain. All values in the return
|
|
1077
|
-
/// will be isString(). (This is probably not optimal, but it
|
|
1078
|
-
/// works. I only need it in one place.)
|
|
1079
|
-
Array getPropertyNames(Runtime& runtime) const;
|
|
1080
|
-
|
|
1081
|
-
#if JSI_VERSION >= 11
|
|
1082
|
-
/// Inform the runtime that there is additional memory associated with a given
|
|
1083
|
-
/// JavaScript object that is not visible to the GC. This can be used if an
|
|
1084
|
-
/// object is known to retain some native memory, and may be used to guide
|
|
1085
|
-
/// decisions about when to run garbage collection.
|
|
1086
|
-
/// This method may be invoked multiple times on an object, and subsequent
|
|
1087
|
-
/// calls will overwrite any previously set value. Once the object is garbage
|
|
1088
|
-
/// collected, the associated external memory will be considered freed and may
|
|
1089
|
-
/// no longer factor into GC decisions.
|
|
1090
|
-
void setExternalMemoryPressure(Runtime& runtime, size_t amt) const;
|
|
1091
|
-
#endif
|
|
1092
|
-
|
|
1093
|
-
protected:
|
|
1094
|
-
void setPropertyValue(
|
|
1095
|
-
Runtime& runtime,
|
|
1096
|
-
const String& name,
|
|
1097
|
-
const Value& value) JSI_CONST_10 {
|
|
1098
|
-
return runtime.setPropertyValue(*this, name, value);
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
void setPropertyValue(
|
|
1102
|
-
Runtime& runtime,
|
|
1103
|
-
const PropNameID& name,
|
|
1104
|
-
const Value& value) JSI_CONST_10 {
|
|
1105
|
-
return runtime.setPropertyValue(*this, name, value);
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
friend class Runtime;
|
|
1109
|
-
friend class Value;
|
|
1110
|
-
};
|
|
1111
|
-
|
|
1112
|
-
/// Represents a weak reference to a JS Object. If the only reference
|
|
1113
|
-
/// to an Object are these, the object is eligible for GC. Method
|
|
1114
|
-
/// names are inspired by C++ weak_ptr. Movable, not copyable.
|
|
1115
|
-
class JSI_EXPORT WeakObject : public Pointer {
|
|
1116
|
-
public:
|
|
1117
|
-
using Pointer::Pointer;
|
|
1118
|
-
|
|
1119
|
-
WeakObject(WeakObject&& other) = default;
|
|
1120
|
-
WeakObject& operator=(WeakObject&& other) = default;
|
|
1121
|
-
|
|
1122
|
-
/// Create a WeakObject from an Object.
|
|
1123
|
-
WeakObject(Runtime& runtime, const Object& o)
|
|
1124
|
-
: WeakObject(runtime.createWeakObject(o)) {}
|
|
1125
|
-
|
|
1126
|
-
/// \return a Value representing the underlying Object if it is still valid;
|
|
1127
|
-
/// otherwise returns \c undefined. Note that this method has nothing to do
|
|
1128
|
-
/// with threads or concurrency. The name is based on std::weak_ptr::lock()
|
|
1129
|
-
/// which serves a similar purpose.
|
|
1130
|
-
Value lock(Runtime& runtime) JSI_CONST_10;
|
|
1131
|
-
|
|
1132
|
-
friend class Runtime;
|
|
1133
|
-
};
|
|
1134
|
-
|
|
1135
|
-
/// Represents a JS Object which can be efficiently used as an array
|
|
1136
|
-
/// with integral indices.
|
|
1137
|
-
class JSI_EXPORT Array : public Object {
|
|
1138
|
-
public:
|
|
1139
|
-
Array(Array&&) = default;
|
|
1140
|
-
/// Creates a new Array instance, with \c length undefined elements.
|
|
1141
|
-
Array(Runtime& runtime, size_t length) : Array(runtime.createArray(length)) {}
|
|
1142
|
-
|
|
1143
|
-
Array& operator=(Array&&) = default;
|
|
1144
|
-
|
|
1145
|
-
/// \return the size of the Array, according to its length property.
|
|
1146
|
-
/// (C++ naming convention)
|
|
1147
|
-
size_t size(Runtime& runtime) const {
|
|
1148
|
-
return runtime.size(*this);
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
/// \return the size of the Array, according to its length property.
|
|
1152
|
-
/// (JS naming convention)
|
|
1153
|
-
size_t length(Runtime& runtime) const {
|
|
1154
|
-
return size(runtime);
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
/// \return the property of the array at index \c i. If there is no
|
|
1158
|
-
/// such property, returns the undefined value. If \c i is out of
|
|
1159
|
-
/// range [ 0..\c length ] throws a JSIException.
|
|
1160
|
-
Value getValueAtIndex(Runtime& runtime, size_t i) const;
|
|
1161
|
-
|
|
1162
|
-
/// Sets the property of the array at index \c i. The argument
|
|
1163
|
-
/// value behaves as with Object::setProperty(). If \c i is out of
|
|
1164
|
-
/// range [ 0..\c length ] throws a JSIException.
|
|
1165
|
-
template <typename T>
|
|
1166
|
-
void setValueAtIndex(Runtime& runtime, size_t i, T&& value) JSI_CONST_10;
|
|
1167
|
-
|
|
1168
|
-
/// There is no current API for changing the size of an array once
|
|
1169
|
-
/// created. We'll probably need that eventually.
|
|
1170
|
-
|
|
1171
|
-
/// Creates a new Array instance from provided values
|
|
1172
|
-
template <typename... Args>
|
|
1173
|
-
static Array createWithElements(Runtime&, Args&&... args);
|
|
1174
|
-
|
|
1175
|
-
/// Creates a new Array instance from initializer list.
|
|
1176
|
-
static Array createWithElements(
|
|
1177
|
-
Runtime& runtime,
|
|
1178
|
-
std::initializer_list<Value> elements);
|
|
1179
|
-
|
|
1180
|
-
private:
|
|
1181
|
-
friend class Object;
|
|
1182
|
-
friend class Value;
|
|
1183
|
-
friend class Runtime;
|
|
1184
|
-
|
|
1185
|
-
void setValueAtIndexImpl(Runtime& runtime, size_t i, const Value& value)
|
|
1186
|
-
JSI_CONST_10 {
|
|
1187
|
-
return runtime.setValueAtIndexImpl(*this, i, value);
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
Array(Runtime::PointerValue* value) : Object(value) {}
|
|
1191
|
-
};
|
|
1192
|
-
|
|
1193
|
-
/// Represents a JSArrayBuffer
|
|
1194
|
-
class JSI_EXPORT ArrayBuffer : public Object {
|
|
1195
|
-
public:
|
|
1196
|
-
ArrayBuffer(ArrayBuffer&&) = default;
|
|
1197
|
-
ArrayBuffer& operator=(ArrayBuffer&&) = default;
|
|
1198
|
-
|
|
1199
|
-
#if JSI_VERSION >= 9
|
|
1200
|
-
ArrayBuffer(Runtime& runtime, std::shared_ptr<MutableBuffer> buffer)
|
|
1201
|
-
: ArrayBuffer(runtime.createArrayBuffer(std::move(buffer))) {}
|
|
1202
|
-
#endif
|
|
1203
|
-
|
|
1204
|
-
/// \return the size of the ArrayBuffer storage. This is not affected by
|
|
1205
|
-
/// overriding the byteLength property.
|
|
1206
|
-
/// (C++ naming convention)
|
|
1207
|
-
size_t size(Runtime& runtime) const {
|
|
1208
|
-
return runtime.size(*this);
|
|
1209
|
-
}
|
|
1210
|
-
|
|
1211
|
-
size_t length(Runtime& runtime) const {
|
|
1212
|
-
return runtime.size(*this);
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
uint8_t* data(Runtime& runtime) JSI_CONST_10 {
|
|
1216
|
-
return runtime.data(*this);
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
private:
|
|
1220
|
-
friend class Object;
|
|
1221
|
-
friend class Value;
|
|
1222
|
-
friend class Runtime;
|
|
1223
|
-
|
|
1224
|
-
ArrayBuffer(Runtime::PointerValue* value) : Object(value) {}
|
|
1225
|
-
};
|
|
1226
|
-
|
|
1227
|
-
/// Represents a JS Object which is guaranteed to be Callable.
|
|
1228
|
-
class JSI_EXPORT Function : public Object {
|
|
1229
|
-
public:
|
|
1230
|
-
Function(Function&&) = default;
|
|
1231
|
-
Function& operator=(Function&&) = default;
|
|
1232
|
-
|
|
1233
|
-
/// Create a function which, when invoked, calls C++ code. If the
|
|
1234
|
-
/// function throws an exception, a JS Error will be created and
|
|
1235
|
-
/// thrown.
|
|
1236
|
-
/// \param name the name property for the function.
|
|
1237
|
-
/// \param paramCount the length property for the function, which
|
|
1238
|
-
/// may not be the number of arguments the function is passed.
|
|
1239
|
-
/// \note The std::function's dtor will be called when the GC finalizes this
|
|
1240
|
-
/// function. As with HostObject, this may be as late as when the Runtime is
|
|
1241
|
-
/// shut down, and may occur on an arbitrary thread. If the function contains
|
|
1242
|
-
/// any captured values, you are responsible for ensuring that their
|
|
1243
|
-
/// destructors are safe to call on any thread.
|
|
1244
|
-
static Function createFromHostFunction(
|
|
1245
|
-
Runtime& runtime,
|
|
1246
|
-
const jsi::PropNameID& name,
|
|
1247
|
-
unsigned int paramCount,
|
|
1248
|
-
jsi::HostFunctionType func);
|
|
1249
|
-
|
|
1250
|
-
/// Calls the function with \c count \c args. The \c this value of the JS
|
|
1251
|
-
/// function will not be set by the C++ caller, similar to calling
|
|
1252
|
-
/// Function.prototype.apply(undefined, args) in JS.
|
|
1253
|
-
/// \b Note: as with Function.prototype.apply, \c this may not always be
|
|
1254
|
-
/// \c undefined in the function itself. If the function is non-strict,
|
|
1255
|
-
/// \c this will be set to the global object.
|
|
1256
|
-
Value call(Runtime& runtime, const Value* args, size_t count) const;
|
|
1257
|
-
|
|
1258
|
-
/// Calls the function with a \c std::initializer_list of Value
|
|
1259
|
-
/// arguments. The \c this value of the JS function will not be set by the
|
|
1260
|
-
/// C++ caller, similar to calling Function.prototype.apply(undefined, args)
|
|
1261
|
-
/// in JS.
|
|
1262
|
-
/// \b Note: as with Function.prototype.apply, \c this may not always be
|
|
1263
|
-
/// \c undefined in the function itself. If the function is non-strict,
|
|
1264
|
-
/// \c this will be set to the global object.
|
|
1265
|
-
Value call(Runtime& runtime, std::initializer_list<Value> args) const;
|
|
1266
|
-
|
|
1267
|
-
/// Calls the function with any number of arguments similarly to
|
|
1268
|
-
/// Object::setProperty(). The \c this value of the JS function will not be
|
|
1269
|
-
/// set by the C++ caller, similar to calling
|
|
1270
|
-
/// Function.prototype.call(undefined, ...args) in JS.
|
|
1271
|
-
/// \b Note: as with Function.prototype.call, \c this may not always be
|
|
1272
|
-
/// \c undefined in the function itself. If the function is non-strict,
|
|
1273
|
-
/// \c this will be set to the global object.
|
|
1274
|
-
template <typename... Args>
|
|
1275
|
-
Value call(Runtime& runtime, Args&&... args) const;
|
|
1276
|
-
|
|
1277
|
-
/// Calls the function with \c count \c args and \c jsThis value passed
|
|
1278
|
-
/// as the \c this value.
|
|
1279
|
-
Value callWithThis(
|
|
1280
|
-
Runtime& Runtime,
|
|
1281
|
-
const Object& jsThis,
|
|
1282
|
-
const Value* args,
|
|
1283
|
-
size_t count) const;
|
|
1284
|
-
|
|
1285
|
-
/// Calls the function with a \c std::initializer_list of Value
|
|
1286
|
-
/// arguments and \c jsThis passed as the \c this value.
|
|
1287
|
-
Value callWithThis(
|
|
1288
|
-
Runtime& runtime,
|
|
1289
|
-
const Object& jsThis,
|
|
1290
|
-
std::initializer_list<Value> args) const;
|
|
1291
|
-
|
|
1292
|
-
/// Calls the function with any number of arguments similarly to
|
|
1293
|
-
/// Object::setProperty(), and with \c jsThis passed as the \c this value.
|
|
1294
|
-
template <typename... Args>
|
|
1295
|
-
Value callWithThis(Runtime& runtime, const Object& jsThis, Args&&... args)
|
|
1296
|
-
const;
|
|
1297
|
-
|
|
1298
|
-
/// Calls the function as a constructor with \c count \c args. Equivalent
|
|
1299
|
-
/// to calling `new Func` where `Func` is the js function reqresented by
|
|
1300
|
-
/// this.
|
|
1301
|
-
Value callAsConstructor(Runtime& runtime, const Value* args, size_t count)
|
|
1302
|
-
const;
|
|
1303
|
-
|
|
1304
|
-
/// Same as above `callAsConstructor`, except use an initializer_list to
|
|
1305
|
-
/// supply the arguments.
|
|
1306
|
-
Value callAsConstructor(Runtime& runtime, std::initializer_list<Value> args)
|
|
1307
|
-
const;
|
|
1308
|
-
|
|
1309
|
-
/// Same as above `callAsConstructor`, but automatically converts/wraps
|
|
1310
|
-
/// any argument with a jsi Value.
|
|
1311
|
-
template <typename... Args>
|
|
1312
|
-
Value callAsConstructor(Runtime& runtime, Args&&... args) const;
|
|
1313
|
-
|
|
1314
|
-
/// Returns whether this was created with Function::createFromHostFunction.
|
|
1315
|
-
/// If true then you can use getHostFunction to get the underlying
|
|
1316
|
-
/// HostFunctionType.
|
|
1317
|
-
bool isHostFunction(Runtime& runtime) const {
|
|
1318
|
-
return runtime.isHostFunction(*this);
|
|
1319
|
-
}
|
|
1320
|
-
|
|
1321
|
-
/// Returns the underlying HostFunctionType iff isHostFunction returns true
|
|
1322
|
-
/// and asserts otherwise. You can use this to use std::function<>::target
|
|
1323
|
-
/// to get the object that was passed to create the HostFunctionType.
|
|
1324
|
-
///
|
|
1325
|
-
/// Note: The reference returned is borrowed from the JS object underlying
|
|
1326
|
-
/// \c this, and thus only lasts as long as the object underlying
|
|
1327
|
-
/// \c this does.
|
|
1328
|
-
HostFunctionType& getHostFunction(Runtime& runtime) const {
|
|
1329
|
-
assert(isHostFunction(runtime));
|
|
1330
|
-
return runtime.getHostFunction(*this);
|
|
1331
|
-
}
|
|
1332
|
-
|
|
1333
|
-
private:
|
|
1334
|
-
friend class Object;
|
|
1335
|
-
friend class Value;
|
|
1336
|
-
friend class Runtime;
|
|
1337
|
-
|
|
1338
|
-
Function(Runtime::PointerValue* value) : Object(value) {}
|
|
1339
|
-
};
|
|
1340
|
-
|
|
1341
|
-
/// Represents any JS Value (undefined, null, boolean, number, symbol,
|
|
1342
|
-
/// string, or object). Movable, or explicitly copyable (has no copy
|
|
1343
|
-
/// ctor).
|
|
1344
|
-
class JSI_EXPORT Value {
|
|
1345
|
-
public:
|
|
1346
|
-
/// Default ctor creates an \c undefined JS value.
|
|
1347
|
-
Value() JSI_NOEXCEPT_15 : Value(UndefinedKind) {}
|
|
1348
|
-
|
|
1349
|
-
/// Creates a \c null JS value.
|
|
1350
|
-
/* implicit */ Value(std::nullptr_t) : kind_(NullKind) {}
|
|
1351
|
-
|
|
1352
|
-
/// Creates a boolean JS value.
|
|
1353
|
-
/* implicit */ Value(bool b) : Value(BooleanKind) {
|
|
1354
|
-
data_.boolean = b;
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
/// Creates a number JS value.
|
|
1358
|
-
/* implicit */ Value(double d) : Value(NumberKind) {
|
|
1359
|
-
data_.number = d;
|
|
1360
|
-
}
|
|
1361
|
-
|
|
1362
|
-
/// Creates a number JS value.
|
|
1363
|
-
/* implicit */ Value(int i) : Value(NumberKind) {
|
|
1364
|
-
data_.number = i;
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
|
-
/// Moves a Symbol, String, or Object rvalue into a new JS value.
|
|
1368
|
-
template <
|
|
1369
|
-
typename T,
|
|
1370
|
-
typename = std::enable_if_t<
|
|
1371
|
-
std::is_base_of<Symbol, T>::value ||
|
|
1372
|
-
#if JSI_VERSION >= 6
|
|
1373
|
-
std::is_base_of<BigInt, T>::value ||
|
|
1374
|
-
#endif
|
|
1375
|
-
std::is_base_of<String, T>::value ||
|
|
1376
|
-
std::is_base_of<Object, T>::value>>
|
|
1377
|
-
/* implicit */ Value(T&& other) : Value(kindOf(other)) {
|
|
1378
|
-
new (&data_.pointer) T(std::move(other));
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
/// Value("foo") will treat foo as a bool. This makes doing that a
|
|
1382
|
-
/// compile error.
|
|
1383
|
-
template <typename T = void>
|
|
1384
|
-
Value(const char*) {
|
|
1385
|
-
static_assert(
|
|
1386
|
-
!std::is_same<void, T>::value,
|
|
1387
|
-
"Value cannot be constructed directly from const char*");
|
|
1388
|
-
}
|
|
1389
|
-
|
|
1390
|
-
Value(Value&& other) JSI_NOEXCEPT_15;
|
|
1391
|
-
|
|
1392
|
-
/// Copies a Symbol lvalue into a new JS value.
|
|
1393
|
-
Value(Runtime& runtime, const Symbol& sym) : Value(SymbolKind) {
|
|
1394
|
-
new (&data_.pointer) Symbol(runtime.cloneSymbol(sym.ptr_));
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
#if JSI_VERSION >= 6
|
|
1398
|
-
/// Copies a BigInt lvalue into a new JS value.
|
|
1399
|
-
Value(Runtime& runtime, const BigInt& bigint) : Value(BigIntKind) {
|
|
1400
|
-
new (&data_.pointer) BigInt(runtime.cloneBigInt(bigint.ptr_));
|
|
1401
|
-
}
|
|
1402
|
-
#endif
|
|
1403
|
-
|
|
1404
|
-
/// Copies a String lvalue into a new JS value.
|
|
1405
|
-
Value(Runtime& runtime, const String& str) : Value(StringKind) {
|
|
1406
|
-
new (&data_.pointer) String(runtime.cloneString(str.ptr_));
|
|
1407
|
-
}
|
|
1408
|
-
|
|
1409
|
-
/// Copies a Object lvalue into a new JS value.
|
|
1410
|
-
Value(Runtime& runtime, const Object& obj) : Value(ObjectKind) {
|
|
1411
|
-
new (&data_.pointer) Object(runtime.cloneObject(obj.ptr_));
|
|
1412
|
-
}
|
|
1413
|
-
|
|
1414
|
-
/// Creates a JS value from another Value lvalue.
|
|
1415
|
-
Value(Runtime& runtime, const Value& value);
|
|
1416
|
-
|
|
1417
|
-
/// Value(rt, "foo") will treat foo as a bool. This makes doing
|
|
1418
|
-
/// that a compile error.
|
|
1419
|
-
template <typename T = void>
|
|
1420
|
-
Value(Runtime&, const char*) {
|
|
1421
|
-
static_assert(
|
|
1422
|
-
!std::is_same<T, void>::value,
|
|
1423
|
-
"Value cannot be constructed directly from const char*");
|
|
1424
|
-
}
|
|
1425
|
-
|
|
1426
|
-
~Value();
|
|
1427
|
-
// \return the undefined \c Value.
|
|
1428
|
-
static Value undefined() {
|
|
1429
|
-
return Value();
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1432
|
-
// \return the null \c Value.
|
|
1433
|
-
static Value null() {
|
|
1434
|
-
return Value(nullptr);
|
|
1435
|
-
}
|
|
1436
|
-
|
|
1437
|
-
// \return a \c Value created from a utf8-encoded JSON string.
|
|
1438
|
-
static Value
|
|
1439
|
-
createFromJsonUtf8(Runtime& runtime, const uint8_t* json, size_t length)
|
|
1440
|
-
#if JSI_VERSION >= 2
|
|
1441
|
-
{
|
|
1442
|
-
return runtime.createValueFromJsonUtf8(json, length);
|
|
1443
|
-
}
|
|
1444
|
-
#else
|
|
1445
|
-
;
|
|
1446
|
-
#endif
|
|
1447
|
-
|
|
1448
|
-
/// \return according to the Strict Equality Comparison algorithm, see:
|
|
1449
|
-
/// https://262.ecma-international.org/11.0/#sec-strict-equality-comparison
|
|
1450
|
-
static bool strictEquals(Runtime& runtime, const Value& a, const Value& b);
|
|
1451
|
-
|
|
1452
|
-
Value& operator=(Value&& other) JSI_NOEXCEPT_15 {
|
|
1453
|
-
this->~Value();
|
|
1454
|
-
new (this) Value(std::move(other));
|
|
1455
|
-
return *this;
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
bool isUndefined() const {
|
|
1459
|
-
return kind_ == UndefinedKind;
|
|
1460
|
-
}
|
|
1461
|
-
|
|
1462
|
-
bool isNull() const {
|
|
1463
|
-
return kind_ == NullKind;
|
|
1464
|
-
}
|
|
1465
|
-
|
|
1466
|
-
bool isBool() const {
|
|
1467
|
-
return kind_ == BooleanKind;
|
|
1468
|
-
}
|
|
1469
|
-
|
|
1470
|
-
bool isNumber() const {
|
|
1471
|
-
return kind_ == NumberKind;
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
bool isString() const {
|
|
1475
|
-
return kind_ == StringKind;
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
#if JSI_VERSION >= 6
|
|
1479
|
-
bool isBigInt() const {
|
|
1480
|
-
return kind_ == BigIntKind;
|
|
1481
|
-
}
|
|
1482
|
-
#endif
|
|
1483
|
-
|
|
1484
|
-
bool isSymbol() const {
|
|
1485
|
-
return kind_ == SymbolKind;
|
|
1486
|
-
}
|
|
1487
|
-
|
|
1488
|
-
bool isObject() const {
|
|
1489
|
-
return kind_ == ObjectKind;
|
|
1490
|
-
}
|
|
1491
|
-
|
|
1492
|
-
/// \return the boolean value, or asserts if not a boolean.
|
|
1493
|
-
bool getBool() const {
|
|
1494
|
-
assert(isBool());
|
|
1495
|
-
return data_.boolean;
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
/// \return the boolean value, or throws JSIException if not a
|
|
1499
|
-
/// boolean.
|
|
1500
|
-
bool asBool() const;
|
|
1501
|
-
|
|
1502
|
-
/// \return the number value, or asserts if not a number.
|
|
1503
|
-
double getNumber() const {
|
|
1504
|
-
assert(isNumber());
|
|
1505
|
-
return data_.number;
|
|
1506
|
-
}
|
|
1507
|
-
|
|
1508
|
-
/// \return the number value, or throws JSIException if not a
|
|
1509
|
-
/// number.
|
|
1510
|
-
double asNumber() const;
|
|
1511
|
-
|
|
1512
|
-
/// \return the Symbol value, or asserts if not a symbol.
|
|
1513
|
-
Symbol getSymbol(Runtime& runtime) const& {
|
|
1514
|
-
assert(isSymbol());
|
|
1515
|
-
return Symbol(runtime.cloneSymbol(data_.pointer.ptr_));
|
|
1516
|
-
}
|
|
1517
|
-
|
|
1518
|
-
/// \return the Symbol value, or asserts if not a symbol.
|
|
1519
|
-
/// Can be used on rvalue references to avoid cloning more symbols.
|
|
1520
|
-
Symbol getSymbol(Runtime&) && {
|
|
1521
|
-
assert(isSymbol());
|
|
1522
|
-
auto ptr = data_.pointer.ptr_;
|
|
1523
|
-
data_.pointer.ptr_ = nullptr;
|
|
1524
|
-
return static_cast<Symbol>(ptr);
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
|
-
/// \return the Symbol value, or throws JSIException if not a
|
|
1528
|
-
/// symbol
|
|
1529
|
-
Symbol asSymbol(Runtime& runtime) const&;
|
|
1530
|
-
Symbol asSymbol(Runtime& runtime) &&;
|
|
1531
|
-
|
|
1532
|
-
#if JSI_VERSION >= 6
|
|
1533
|
-
/// \return the BigInt value, or asserts if not a bigint.
|
|
1534
|
-
BigInt getBigInt(Runtime& runtime) const& {
|
|
1535
|
-
assert(isBigInt());
|
|
1536
|
-
return BigInt(runtime.cloneBigInt(data_.pointer.ptr_));
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
/// \return the BigInt value, or asserts if not a bigint.
|
|
1540
|
-
/// Can be used on rvalue references to avoid cloning more bigints.
|
|
1541
|
-
BigInt getBigInt(Runtime&) && {
|
|
1542
|
-
assert(isBigInt());
|
|
1543
|
-
auto ptr = data_.pointer.ptr_;
|
|
1544
|
-
data_.pointer.ptr_ = nullptr;
|
|
1545
|
-
return static_cast<BigInt>(ptr);
|
|
1546
|
-
}
|
|
1547
|
-
|
|
1548
|
-
/// \return the BigInt value, or throws JSIException if not a
|
|
1549
|
-
/// bigint
|
|
1550
|
-
BigInt asBigInt(Runtime& runtime) const&;
|
|
1551
|
-
BigInt asBigInt(Runtime& runtime) &&;
|
|
1552
|
-
#endif
|
|
1553
|
-
|
|
1554
|
-
/// \return the String value, or asserts if not a string.
|
|
1555
|
-
String getString(Runtime& runtime) const& {
|
|
1556
|
-
assert(isString());
|
|
1557
|
-
return String(runtime.cloneString(data_.pointer.ptr_));
|
|
1558
|
-
}
|
|
1559
|
-
|
|
1560
|
-
/// \return the String value, or asserts if not a string.
|
|
1561
|
-
/// Can be used on rvalue references to avoid cloning more strings.
|
|
1562
|
-
String getString(Runtime&) && {
|
|
1563
|
-
assert(isString());
|
|
1564
|
-
auto ptr = data_.pointer.ptr_;
|
|
1565
|
-
data_.pointer.ptr_ = nullptr;
|
|
1566
|
-
return static_cast<String>(ptr);
|
|
1567
|
-
}
|
|
1568
|
-
|
|
1569
|
-
/// \return the String value, or throws JSIException if not a
|
|
1570
|
-
/// string.
|
|
1571
|
-
String asString(Runtime& runtime) const&;
|
|
1572
|
-
String asString(Runtime& runtime) &&;
|
|
1573
|
-
|
|
1574
|
-
/// \return the Object value, or asserts if not an object.
|
|
1575
|
-
Object getObject(Runtime& runtime) const& {
|
|
1576
|
-
assert(isObject());
|
|
1577
|
-
return Object(runtime.cloneObject(data_.pointer.ptr_));
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
/// \return the Object value, or asserts if not an object.
|
|
1581
|
-
/// Can be used on rvalue references to avoid cloning more objects.
|
|
1582
|
-
Object getObject(Runtime&) && {
|
|
1583
|
-
assert(isObject());
|
|
1584
|
-
auto ptr = data_.pointer.ptr_;
|
|
1585
|
-
data_.pointer.ptr_ = nullptr;
|
|
1586
|
-
return static_cast<Object>(ptr);
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
/// \return the Object value, or throws JSIException if not an
|
|
1590
|
-
/// object.
|
|
1591
|
-
Object asObject(Runtime& runtime) const&;
|
|
1592
|
-
Object asObject(Runtime& runtime) &&;
|
|
1593
|
-
|
|
1594
|
-
// \return a String like JS .toString() would do.
|
|
1595
|
-
String toString(Runtime& runtime) const;
|
|
1596
|
-
|
|
1597
|
-
private:
|
|
1598
|
-
friend class Runtime;
|
|
1599
|
-
|
|
1600
|
-
enum ValueKind {
|
|
1601
|
-
UndefinedKind,
|
|
1602
|
-
NullKind,
|
|
1603
|
-
BooleanKind,
|
|
1604
|
-
NumberKind,
|
|
1605
|
-
SymbolKind,
|
|
1606
|
-
#if JSI_VERSION >= 6
|
|
1607
|
-
BigIntKind,
|
|
1608
|
-
#endif
|
|
1609
|
-
StringKind,
|
|
1610
|
-
ObjectKind,
|
|
1611
|
-
PointerKind = SymbolKind,
|
|
1612
|
-
};
|
|
1613
|
-
|
|
1614
|
-
union Data {
|
|
1615
|
-
// Value's ctor and dtor will manage the lifecycle of the contained Data.
|
|
1616
|
-
Data() {
|
|
1617
|
-
static_assert(
|
|
1618
|
-
sizeof(Data) == sizeof(uint64_t),
|
|
1619
|
-
"Value data should fit in a 64-bit register");
|
|
1620
|
-
}
|
|
1621
|
-
~Data() {}
|
|
1622
|
-
|
|
1623
|
-
// scalars
|
|
1624
|
-
bool boolean;
|
|
1625
|
-
double number;
|
|
1626
|
-
// pointers
|
|
1627
|
-
Pointer pointer; // Symbol, String, Object, Array, Function
|
|
1628
|
-
};
|
|
1629
|
-
|
|
1630
|
-
Value(ValueKind kind) : kind_(kind) {}
|
|
1631
|
-
|
|
1632
|
-
constexpr static ValueKind kindOf(const Symbol&) {
|
|
1633
|
-
return SymbolKind;
|
|
1634
|
-
}
|
|
1635
|
-
#if JSI_VERSION >= 6
|
|
1636
|
-
constexpr static ValueKind kindOf(const BigInt&) {
|
|
1637
|
-
return BigIntKind;
|
|
1638
|
-
}
|
|
1639
|
-
#endif
|
|
1640
|
-
constexpr static ValueKind kindOf(const String&) {
|
|
1641
|
-
return StringKind;
|
|
1642
|
-
}
|
|
1643
|
-
constexpr static ValueKind kindOf(const Object&) {
|
|
1644
|
-
return ObjectKind;
|
|
1645
|
-
}
|
|
1646
|
-
|
|
1647
|
-
ValueKind kind_;
|
|
1648
|
-
Data data_;
|
|
1649
|
-
|
|
1650
|
-
// In the future: Value becomes NaN-boxed. See T40538354.
|
|
1651
|
-
};
|
|
1652
|
-
|
|
1653
|
-
/// Not movable and not copyable RAII marker advising the underlying
|
|
1654
|
-
/// JavaScript VM to track resources allocated since creation until
|
|
1655
|
-
/// destruction so that they can be recycled eagerly when the Scope
|
|
1656
|
-
/// goes out of scope instead of floating in the air until the next
|
|
1657
|
-
/// garbage collection or any other delayed release occurs.
|
|
1658
|
-
///
|
|
1659
|
-
/// This API should be treated only as advice, implementations can
|
|
1660
|
-
/// choose to ignore the fact that Scopes are created or destroyed.
|
|
1661
|
-
///
|
|
1662
|
-
/// This class is an exception to the rule allowing destructors to be
|
|
1663
|
-
/// called without proper synchronization (see Runtime documentation).
|
|
1664
|
-
/// The whole point of this class is to enable all sorts of clean ups
|
|
1665
|
-
/// when the destructor is called and this proper synchronization is
|
|
1666
|
-
/// required at that time.
|
|
1667
|
-
///
|
|
1668
|
-
/// Instances of this class are intended to be created as automatic stack
|
|
1669
|
-
/// variables in which case destructor calls don't require any additional
|
|
1670
|
-
/// locking, provided that the lock (if any) is managed with RAII helpers.
|
|
1671
|
-
class JSI_EXPORT Scope {
|
|
1672
|
-
public:
|
|
1673
|
-
explicit Scope(Runtime& rt) : rt_(rt), prv_(rt.pushScope()) {}
|
|
1674
|
-
~Scope() {
|
|
1675
|
-
rt_.popScope(prv_);
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
Scope(const Scope&) = delete;
|
|
1679
|
-
Scope(Scope&&) = delete;
|
|
1680
|
-
|
|
1681
|
-
Scope& operator=(const Scope&) = delete;
|
|
1682
|
-
Scope& operator=(Scope&&) = delete;
|
|
1683
|
-
|
|
1684
|
-
template <typename F>
|
|
1685
|
-
static auto callInNewScope(Runtime& rt, F f) -> decltype(f()) {
|
|
1686
|
-
Scope s(rt);
|
|
1687
|
-
return f();
|
|
1688
|
-
}
|
|
1689
|
-
|
|
1690
|
-
private:
|
|
1691
|
-
Runtime& rt_;
|
|
1692
|
-
Runtime::ScopeState* prv_;
|
|
1693
|
-
};
|
|
1694
|
-
|
|
1695
|
-
/// Base class for jsi exceptions
|
|
1696
|
-
class JSI_EXPORT JSIException : public std::exception {
|
|
1697
|
-
protected:
|
|
1698
|
-
JSIException() {}
|
|
1699
|
-
JSIException(std::string what) : what_(std::move(what)) {}
|
|
1700
|
-
|
|
1701
|
-
public:
|
|
1702
|
-
JSIException(const JSIException&) = default;
|
|
1703
|
-
|
|
1704
|
-
virtual const char* what() const noexcept override {
|
|
1705
|
-
return what_.c_str();
|
|
1706
|
-
}
|
|
1707
|
-
|
|
1708
|
-
virtual ~JSIException() override;
|
|
1709
|
-
|
|
1710
|
-
protected:
|
|
1711
|
-
std::string what_;
|
|
1712
|
-
};
|
|
1713
|
-
|
|
1714
|
-
/// This exception will be thrown by API functions on errors not related to
|
|
1715
|
-
/// JavaScript execution.
|
|
1716
|
-
class JSI_EXPORT JSINativeException : public JSIException {
|
|
1717
|
-
public:
|
|
1718
|
-
JSINativeException(std::string what) : JSIException(std::move(what)) {}
|
|
1719
|
-
|
|
1720
|
-
JSINativeException(const JSINativeException&) = default;
|
|
1721
|
-
|
|
1722
|
-
virtual ~JSINativeException();
|
|
1723
|
-
};
|
|
1724
|
-
|
|
1725
|
-
/// This exception will be thrown by API functions whenever a JS
|
|
1726
|
-
/// operation causes an exception as described by the spec, or as
|
|
1727
|
-
/// otherwise described.
|
|
1728
|
-
class JSI_EXPORT JSError : public JSIException {
|
|
1729
|
-
public:
|
|
1730
|
-
/// Creates a JSError referring to provided \c value
|
|
1731
|
-
JSError(Runtime& r, Value&& value);
|
|
1732
|
-
|
|
1733
|
-
/// Creates a JSError referring to new \c Error instance capturing current
|
|
1734
|
-
/// JavaScript stack. The error message property is set to given \c message.
|
|
1735
|
-
JSError(Runtime& rt, std::string message);
|
|
1736
|
-
|
|
1737
|
-
/// Creates a JSError referring to new \c Error instance capturing current
|
|
1738
|
-
/// JavaScript stack. The error message property is set to given \c message.
|
|
1739
|
-
JSError(Runtime& rt, const char* message)
|
|
1740
|
-
: JSError(rt, std::string(message)) {}
|
|
1741
|
-
|
|
1742
|
-
/// Creates a JSError referring to a JavaScript Object having message and
|
|
1743
|
-
/// stack properties set to provided values.
|
|
1744
|
-
JSError(Runtime& rt, std::string message, std::string stack);
|
|
1745
|
-
|
|
1746
|
-
/// Creates a JSError referring to provided value and what string
|
|
1747
|
-
/// set to provided message. This argument order is a bit weird,
|
|
1748
|
-
/// but necessary to avoid ambiguity with the above.
|
|
1749
|
-
JSError(std::string what, Runtime& rt, Value&& value);
|
|
1750
|
-
|
|
1751
|
-
/// Creates a JSError referring to the provided value, message and stack. This
|
|
1752
|
-
/// constructor does not take a Runtime parameter, and therefore cannot result
|
|
1753
|
-
/// in recursively invoking the JSError constructor.
|
|
1754
|
-
JSError(Value&& value, std::string message, std::string stack);
|
|
1755
|
-
|
|
1756
|
-
JSError(const JSError&) = default;
|
|
1757
|
-
|
|
1758
|
-
virtual ~JSError();
|
|
1759
|
-
|
|
1760
|
-
const std::string& getStack() const {
|
|
1761
|
-
return stack_;
|
|
1762
|
-
}
|
|
1763
|
-
|
|
1764
|
-
const std::string& getMessage() const {
|
|
1765
|
-
return message_;
|
|
1766
|
-
}
|
|
1767
|
-
|
|
1768
|
-
const jsi::Value& value() const {
|
|
1769
|
-
assert(value_);
|
|
1770
|
-
return *value_;
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
// TODO: (vmoroz) Can we remove it considering that we have the new JSError
|
|
1774
|
-
// constructor?
|
|
1775
|
-
// In V8's case, creating an Error object in JS doesn't record the callstack.
|
|
1776
|
-
// To preserve it, we need a way to manually add the stack here and on the JS
|
|
1777
|
-
// side.
|
|
1778
|
-
void setStack(std::string stack) {
|
|
1779
|
-
stack_ = std::move(stack);
|
|
1780
|
-
what_ = message_ + "\n\n" + stack_;
|
|
1781
|
-
}
|
|
1782
|
-
|
|
1783
|
-
private:
|
|
1784
|
-
// This initializes the value_ member and does some other
|
|
1785
|
-
// validation, so it must be called by every branch through the
|
|
1786
|
-
// constructors.
|
|
1787
|
-
void setValue(Runtime& rt, Value&& value);
|
|
1788
|
-
|
|
1789
|
-
// This needs to be on the heap, because throw requires the object
|
|
1790
|
-
// be copyable, and Value is not.
|
|
1791
|
-
std::shared_ptr<jsi::Value> value_;
|
|
1792
|
-
std::string message_;
|
|
1793
|
-
std::string stack_;
|
|
1794
|
-
};
|
|
1795
|
-
|
|
1796
|
-
} // namespace jsi
|
|
1797
|
-
} // namespace facebook
|
|
1798
|
-
|
|
1799
|
-
#include <jsi/jsi-inl.h>
|