react-native-windows 0.69.0-preview.3 → 0.69.0-preview.4

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.
Files changed (58) hide show
  1. package/Directory.Build.props +4 -0
  2. package/Folly/Folly.vcxproj +4 -5
  3. package/Libraries/Network/RCTNetworkingWinShared.js +7 -0
  4. package/Microsoft.ReactNative/Base/CoreNativeModules.cpp +3 -1
  5. package/Microsoft.ReactNative/IReactContext.cpp +17 -0
  6. package/Microsoft.ReactNative/IReactContext.h +2 -0
  7. package/Microsoft.ReactNative/IReactContext.idl +27 -0
  8. package/Microsoft.ReactNative/JsiApi.cpp +9 -0
  9. package/Microsoft.ReactNative/JsiApi.h +1 -0
  10. package/Microsoft.ReactNative/JsiApi.idl +1 -0
  11. package/Microsoft.ReactNative.Cxx/JSI/JsiAbiApi.cpp +3 -5
  12. package/Microsoft.ReactNative.Cxx/JSI/JsiAbiApi.h +1 -1
  13. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.cpp +31 -9
  14. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.h +48 -0
  15. package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems.filters +1 -1
  16. package/Microsoft.ReactNative.Managed/packages.lock.json +57 -2
  17. package/PropertySheets/Generated/PackageVersion.g.props +1 -1
  18. package/PropertySheets/JSEngine.props +2 -2
  19. package/Shared/CreateModules.h +17 -2
  20. package/Shared/InspectorPackagerConnection.cpp +6 -7
  21. package/Shared/InspectorPackagerConnection.h +1 -1
  22. package/Shared/JSI/ChakraRuntime.cpp +5 -0
  23. package/Shared/JSI/ChakraRuntime.h +1 -0
  24. package/Shared/JSI/NapiJsiV8RuntimeHolder.cpp +72 -2
  25. package/Shared/JSI/NapiJsiV8RuntimeHolder.h +2 -0
  26. package/Shared/Modules/BlobModule.cpp +376 -0
  27. package/Shared/Modules/BlobModule.h +153 -0
  28. package/Shared/Modules/CxxModuleUtilities.cpp +19 -0
  29. package/Shared/Modules/CxxModuleUtilities.h +23 -0
  30. package/Shared/Modules/FileReaderModule.cpp +156 -0
  31. package/Shared/Modules/FileReaderModule.h +54 -0
  32. package/Shared/Modules/HttpModule.cpp +72 -69
  33. package/Shared/Modules/HttpModule.h +8 -1
  34. package/Shared/Modules/IBlobPersistor.h +30 -0
  35. package/Shared/Modules/IHttpModuleProxy.h +30 -0
  36. package/Shared/Modules/IRequestBodyHandler.h +52 -0
  37. package/Shared/Modules/IResponseHandler.h +27 -0
  38. package/Shared/Modules/IUriHandler.h +37 -0
  39. package/Shared/Modules/IWebSocketModuleContentHandler.h +26 -0
  40. package/Shared/Modules/IWebSocketModuleProxy.h +22 -0
  41. package/Shared/Modules/NetworkingModule.cpp +1 -1
  42. package/Shared/Modules/WebSocketModule.cpp +92 -22
  43. package/Shared/Modules/WebSocketModule.h +27 -1
  44. package/Shared/Networking/IHttpResource.h +50 -2
  45. package/Shared/Networking/WinRTHttpResource.cpp +169 -51
  46. package/Shared/Networking/WinRTHttpResource.h +27 -8
  47. package/Shared/Networking/WinRTTypes.h +5 -2
  48. package/Shared/OInstance.cpp +22 -5
  49. package/Shared/OInstance.h +8 -4
  50. package/Shared/RuntimeOptions.cpp +6 -3
  51. package/Shared/RuntimeOptions.h +14 -3
  52. package/Shared/Shared.vcxitems +13 -0
  53. package/Shared/Shared.vcxitems.filters +40 -1
  54. package/fmt/fmt.vcxproj +4 -5
  55. package/package.json +1 -1
  56. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/JSCRuntime.cpp +0 -1480
  57. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/decorator.h +0 -753
  58. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/jsi.h +0 -1331
@@ -1,1331 +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
- #ifndef JSI_EXPORT
19
- #ifdef _MSC_VER
20
- #ifdef CREATE_SHARED_LIBRARY
21
- #define JSI_EXPORT __declspec(dllexport)
22
- #else
23
- #define JSI_EXPORT
24
- #endif // CREATE_SHARED_LIBRARY
25
- #else // _MSC_VER
26
- #define JSI_EXPORT __attribute__((visibility("default")))
27
- #endif // _MSC_VER
28
- #endif // !defined(JSI_EXPORT)
29
-
30
- class FBJSRuntime;
31
- namespace facebook {
32
- namespace jsi {
33
-
34
- class JSI_EXPORT Buffer {
35
- public:
36
- virtual ~Buffer();
37
- virtual size_t size() const = 0;
38
- virtual const uint8_t* data() const = 0;
39
- };
40
-
41
- class JSI_EXPORT StringBuffer : public Buffer {
42
- public:
43
- StringBuffer(std::string s) : s_(std::move(s)) {}
44
- size_t size() const override {
45
- return s_.size();
46
- }
47
- const uint8_t* data() const override {
48
- return reinterpret_cast<const uint8_t*>(s_.data());
49
- }
50
-
51
- private:
52
- std::string s_;
53
- };
54
-
55
- /// PreparedJavaScript is a base class representing JavaScript which is in a
56
- /// form optimized for execution, in a runtime-specific way. Construct one via
57
- /// jsi::Runtime::prepareJavaScript().
58
- /// ** This is an experimental API that is subject to change. **
59
- class JSI_EXPORT PreparedJavaScript {
60
- protected:
61
- PreparedJavaScript() = default;
62
-
63
- public:
64
- virtual ~PreparedJavaScript() = 0;
65
- };
66
-
67
- class Runtime;
68
- class Pointer;
69
- class PropNameID;
70
- class Symbol;
71
- class String;
72
- class Object;
73
- class WeakObject;
74
- class Array;
75
- class ArrayBuffer;
76
- class Function;
77
- class Value;
78
- class Instrumentation;
79
- class Scope;
80
- class JSIException;
81
- class JSError;
82
-
83
- /// A function which has this type can be registered as a function
84
- /// callable from JavaScript using Function::createFromHostFunction().
85
- /// When the function is called, args will point to the arguments, and
86
- /// count will indicate how many arguments are passed. The function
87
- /// can return a Value to the caller, or throw an exception. If a C++
88
- /// exception is thrown, a JS Error will be created and thrown into
89
- /// JS; if the C++ exception extends std::exception, the Error's
90
- /// message will be whatever what() returns. Note that it is undefined whether
91
- /// HostFunctions may or may not be called in strict mode; that is `thisVal`
92
- /// can be any value - it will not necessarily be coerced to an object or
93
- /// or set to the global object.
94
- using HostFunctionType = std::function<
95
- Value(Runtime& rt, const Value& thisVal, const Value* args, size_t count)>;
96
-
97
- /// An object which implements this interface can be registered as an
98
- /// Object with the JS runtime.
99
- class JSI_EXPORT HostObject {
100
- public:
101
- // The C++ object's dtor will be called when the GC finalizes this
102
- // object. (This may be as late as when the Runtime is shut down.)
103
- // You have no control over which thread it is called on. This will
104
- // be called from inside the GC, so it is unsafe to do any VM
105
- // operations which require a Runtime&. Derived classes' dtors
106
- // should also avoid doing anything expensive. Calling the dtor on
107
- // a jsi object is explicitly ok. If you want to do JS operations,
108
- // or any nontrivial work, you should add it to a work queue, and
109
- // manage it externally.
110
- virtual ~HostObject();
111
-
112
- // When JS wants a property with a given name from the HostObject,
113
- // it will call this method. If it throws an exception, the call
114
- // will throw a JS \c Error object. By default this returns undefined.
115
- // \return the value for the property.
116
- virtual Value get(Runtime&, const PropNameID& name);
117
-
118
- // When JS wants to set a property with a given name on the HostObject,
119
- // it will call this method. If it throws an exception, the call will
120
- // throw a JS \c Error object. By default this throws a type error exception
121
- // mimicking the behavior of a frozen object in strict mode.
122
- virtual void set(Runtime&, const PropNameID& name, const Value& value);
123
-
124
- // When JS wants a list of property names for the HostObject, it will
125
- // call this method. If it throws an exception, the call will throw a
126
- // JS \c Error object. The default implementation returns empty vector.
127
- virtual std::vector<PropNameID> getPropertyNames(Runtime& rt);
128
- };
129
-
130
- /// Represents a JS runtime. Movable, but not copyable. Note that
131
- /// this object may not be thread-aware, but cannot be used safely from
132
- /// multiple threads at once. The application is responsible for
133
- /// ensuring that it is used safely. This could mean using the
134
- /// Runtime from a single thread, using a mutex, doing all work on a
135
- /// serial queue, etc. This restriction applies to the methods of
136
- /// this class, and any method in the API which take a Runtime& as an
137
- /// argument. Destructors (all but ~Scope), operators, or other methods
138
- /// which do not take Runtime& as an argument are safe to call from any
139
- /// thread, but it is still forbidden to make write operations on a single
140
- /// instance of any class from more than one thread. In addition, to
141
- /// make shutdown safe, destruction of objects associated with the Runtime
142
- /// must be destroyed before the Runtime is destroyed, or from the
143
- /// destructor of a managed HostObject or HostFunction. Informally, this
144
- /// means that the main source of unsafe behavior is to hold a jsi object
145
- /// in a non-Runtime-managed object, and not clean it up before the Runtime
146
- /// is shut down. If your lifecycle is such that avoiding this is hard,
147
- /// you will probably need to do use your own locks.
148
- class JSI_EXPORT Runtime {
149
- public:
150
- virtual ~Runtime();
151
-
152
- /// Evaluates the given JavaScript \c buffer. \c sourceURL is used
153
- /// to annotate the stack trace if there is an exception. The
154
- /// contents may be utf8-encoded JS source code, or binary bytecode
155
- /// whose format is specific to the implementation. If the input
156
- /// format is unknown, or evaluation causes an error, a JSIException
157
- /// will be thrown.
158
- /// Note this function should ONLY be used when there isn't another means
159
- /// through the JSI API. For example, it will be much slower to use this to
160
- /// call a global function than using the JSI APIs to read the function
161
- /// property from the global object and then calling it explicitly.
162
- virtual Value evaluateJavaScript(
163
- const std::shared_ptr<const Buffer>& buffer,
164
- const std::string& sourceURL) = 0;
165
-
166
- /// Prepares to evaluate the given JavaScript \c buffer by processing it into
167
- /// a form optimized for execution. This may include pre-parsing, compiling,
168
- /// etc. If the input is invalid (for example, cannot be parsed), a
169
- /// JSIException will be thrown. The resulting object is tied to the
170
- /// particular concrete type of Runtime from which it was created. It may be
171
- /// used (via evaluatePreparedJavaScript) in any Runtime of the same concrete
172
- /// type.
173
- /// The PreparedJavaScript object may be passed to multiple VM instances, so
174
- /// they can all share and benefit from the prepared script.
175
- /// As with evaluateJavaScript(), using JavaScript code should be avoided
176
- /// when the JSI API is sufficient.
177
- virtual std::shared_ptr<const PreparedJavaScript> prepareJavaScript(
178
- const std::shared_ptr<const Buffer>& buffer,
179
- std::string sourceURL) = 0;
180
-
181
- /// Evaluates a PreparedJavaScript. If evaluation causes an error, a
182
- /// JSIException will be thrown.
183
- /// As with evaluateJavaScript(), using JavaScript code should be avoided
184
- /// when the JSI API is sufficient.
185
- virtual Value evaluatePreparedJavaScript(
186
- const std::shared_ptr<const PreparedJavaScript>& js) = 0;
187
-
188
- /// Drain the JavaScript VM internal Microtask (a.k.a. Job in ECMA262) queue.
189
- ///
190
- /// \param maxMicrotasksHint a hint to tell an implementation that it should
191
- /// make a best effort not execute more than the given number. It's default
192
- /// to -1 for infinity (unbounded execution).
193
- /// \return true if the queue is drained or false if there is more work to do.
194
- ///
195
- /// When there were exceptions thrown from the execution of microtasks,
196
- /// implementations shall discard the exceptional jobs. An implementation may
197
- /// \throw a \c JSError object to signal the hosts to handle. In that case, an
198
- /// implementation may or may not suspend the draining.
199
- ///
200
- /// Hosts may call this function again to resume the draining if it was
201
- /// suspended due to either exceptions or the \p maxMicrotasksHint bound.
202
- /// E.g. a host may repetitively invoke this function until the queue is
203
- /// drained to implement the "microtask checkpoint" defined in WHATWG HTML
204
- /// event loop: https://html.spec.whatwg.org/C#perform-a-microtask-checkpoint.
205
- ///
206
- /// Note that error propagation is only a concern if a host needs to implement
207
- /// `queueMicrotask`, a recent API that allows enqueueing arbitrary functions
208
- /// (hence may throw) as microtasks. Exceptions from ECMA-262 Promise Jobs are
209
- /// handled internally to VMs and are never propagated to hosts.
210
- ///
211
- /// This API offers some queue management to hosts at its best effort due to
212
- /// different behaviors and limitations imposed by different VMs and APIs. By
213
- /// the time this is written, An implementation may swallow exceptions (JSC),
214
- /// may not pause (V8), and may not support bounded executions.
215
- virtual bool drainMicrotasks(int maxMicrotasksHint = -1) = 0;
216
-
217
- /// \return the global object
218
- virtual Object global() = 0;
219
-
220
- /// \return a short printable description of the instance. It should
221
- /// at least include some human-readable indication of the runtime
222
- /// implementation. This should only be used by logging, debugging,
223
- /// and other developer-facing callers.
224
- virtual std::string description() = 0;
225
-
226
- /// \return whether or not the underlying runtime supports debugging via the
227
- /// Chrome remote debugging protocol.
228
- ///
229
- /// NOTE: the API for determining whether a runtime is debuggable and
230
- /// registering a runtime with the debugger is still in flux, so please don't
231
- /// use this API unless you know what you're doing.
232
- virtual bool isInspectable() = 0;
233
-
234
- /// \return an interface to extract metrics from this \c Runtime. The default
235
- /// implementation of this function returns an \c Instrumentation instance
236
- /// which returns no metrics.
237
- virtual Instrumentation& instrumentation();
238
-
239
- protected:
240
- friend class Pointer;
241
- friend class PropNameID;
242
- friend class Symbol;
243
- friend class String;
244
- friend class Object;
245
- friend class WeakObject;
246
- friend class Array;
247
- friend class ArrayBuffer;
248
- friend class Function;
249
- friend class Value;
250
- friend class Scope;
251
- friend class JSError;
252
-
253
- // Potential optimization: avoid the cloneFoo() virtual dispatch,
254
- // and instead just fix the number of fields, and copy them, since
255
- // in practice they are trivially copyable. Sufficient use of
256
- // rvalue arguments/methods would also reduce the number of clones.
257
-
258
- struct PointerValue {
259
- virtual void invalidate() = 0;
260
-
261
- protected:
262
- virtual ~PointerValue() = default;
263
- };
264
-
265
- virtual PointerValue* cloneSymbol(const Runtime::PointerValue* pv) = 0;
266
- virtual PointerValue* cloneString(const Runtime::PointerValue* pv) = 0;
267
- virtual PointerValue* cloneObject(const Runtime::PointerValue* pv) = 0;
268
- virtual PointerValue* clonePropNameID(const Runtime::PointerValue* pv) = 0;
269
-
270
- virtual PropNameID createPropNameIDFromAscii(
271
- const char* str,
272
- size_t length) = 0;
273
- virtual PropNameID createPropNameIDFromUtf8(
274
- const uint8_t* utf8,
275
- size_t length) = 0;
276
- virtual PropNameID createPropNameIDFromString(const String& str) = 0;
277
- virtual std::string utf8(const PropNameID&) = 0;
278
- virtual bool compare(const PropNameID&, const PropNameID&) = 0;
279
-
280
- virtual std::string symbolToString(const Symbol&) = 0;
281
-
282
- virtual String createStringFromAscii(const char* str, size_t length) = 0;
283
- virtual String createStringFromUtf8(const uint8_t* utf8, size_t length) = 0;
284
- virtual std::string utf8(const String&) = 0;
285
-
286
- // \return a \c Value created from a utf8-encoded JSON string. The default
287
- // implementation creates a \c String and invokes JSON.parse.
288
- virtual Value createValueFromJsonUtf8(const uint8_t* json, size_t length);
289
-
290
- virtual Object createObject() = 0;
291
- virtual Object createObject(std::shared_ptr<HostObject> ho) = 0;
292
- virtual std::shared_ptr<HostObject> getHostObject(const jsi::Object&) = 0;
293
- virtual HostFunctionType& getHostFunction(const jsi::Function&) = 0;
294
-
295
- virtual Value getProperty(const Object&, const PropNameID& name) = 0;
296
- virtual Value getProperty(const Object&, const String& name) = 0;
297
- virtual bool hasProperty(const Object&, const PropNameID& name) = 0;
298
- virtual bool hasProperty(const Object&, const String& name) = 0;
299
- virtual void
300
- setPropertyValue(Object&, const PropNameID& name, const Value& value) = 0;
301
- virtual void
302
- setPropertyValue(Object&, const String& name, const Value& value) = 0;
303
-
304
- virtual bool isArray(const Object&) const = 0;
305
- virtual bool isArrayBuffer(const Object&) const = 0;
306
- virtual bool isFunction(const Object&) const = 0;
307
- virtual bool isHostObject(const jsi::Object&) const = 0;
308
- virtual bool isHostFunction(const jsi::Function&) const = 0;
309
- virtual Array getPropertyNames(const Object&) = 0;
310
-
311
- virtual WeakObject createWeakObject(const Object&) = 0;
312
- virtual Value lockWeakObject(WeakObject&) = 0;
313
-
314
- virtual Array createArray(size_t length) = 0;
315
- virtual size_t size(const Array&) = 0;
316
- virtual size_t size(const ArrayBuffer&) = 0;
317
- virtual uint8_t* data(const ArrayBuffer&) = 0;
318
- virtual Value getValueAtIndex(const Array&, size_t i) = 0;
319
- virtual void setValueAtIndexImpl(Array&, size_t i, const Value& value) = 0;
320
-
321
- virtual Function createFunctionFromHostFunction(
322
- const PropNameID& name,
323
- unsigned int paramCount,
324
- HostFunctionType func) = 0;
325
- virtual Value call(
326
- const Function&,
327
- const Value& jsThis,
328
- const Value* args,
329
- size_t count) = 0;
330
- virtual Value
331
- callAsConstructor(const Function&, const Value* args, size_t count) = 0;
332
-
333
- // Private data for managing scopes.
334
- struct ScopeState;
335
- virtual ScopeState* pushScope();
336
- virtual void popScope(ScopeState*);
337
-
338
- virtual bool strictEquals(const Symbol& a, const Symbol& b) const = 0;
339
- virtual bool strictEquals(const String& a, const String& b) const = 0;
340
- virtual bool strictEquals(const Object& a, const Object& b) const = 0;
341
-
342
- virtual bool instanceOf(const Object& o, const Function& f) = 0;
343
-
344
- // These exist so derived classes can access the private parts of
345
- // Value, Symbol, String, and Object, which are all friends of Runtime.
346
- template <typename T>
347
- static T make(PointerValue* pv);
348
- static PointerValue* getPointerValue(Pointer& pointer);
349
- static const PointerValue* getPointerValue(const Pointer& pointer);
350
- static const PointerValue* getPointerValue(const Value& value);
351
-
352
- friend class ::FBJSRuntime;
353
- template <typename Plain, typename Base>
354
- friend class RuntimeDecorator;
355
- };
356
-
357
- // Base class for pointer-storing types.
358
- class JSI_EXPORT Pointer {
359
- protected:
360
- explicit Pointer(Pointer&& other) : ptr_(other.ptr_) {
361
- other.ptr_ = nullptr;
362
- }
363
-
364
- ~Pointer() {
365
- if (ptr_) {
366
- ptr_->invalidate();
367
- }
368
- }
369
-
370
- Pointer& operator=(Pointer&& other);
371
-
372
- friend class Runtime;
373
- friend class Value;
374
-
375
- explicit Pointer(Runtime::PointerValue* ptr) : ptr_(ptr) {}
376
-
377
- typename Runtime::PointerValue* ptr_;
378
- };
379
-
380
- /// Represents something that can be a JS property key. Movable, not copyable.
381
- class JSI_EXPORT PropNameID : public Pointer {
382
- public:
383
- using Pointer::Pointer;
384
-
385
- PropNameID(Runtime& runtime, const PropNameID& other)
386
- : Pointer(runtime.clonePropNameID(other.ptr_)) {}
387
-
388
- PropNameID(PropNameID&& other) = default;
389
- PropNameID& operator=(PropNameID&& other) = default;
390
-
391
- /// Create a JS property name id from ascii values. The data is
392
- /// copied.
393
- static PropNameID forAscii(Runtime& runtime, const char* str, size_t length) {
394
- return runtime.createPropNameIDFromAscii(str, length);
395
- }
396
-
397
- /// Create a property name id from a nul-terminated C ascii name. The data is
398
- /// copied.
399
- static PropNameID forAscii(Runtime& runtime, const char* str) {
400
- return forAscii(runtime, str, strlen(str));
401
- }
402
-
403
- /// Create a PropNameID from a C++ string. The string is copied.
404
- static PropNameID forAscii(Runtime& runtime, const std::string& str) {
405
- return forAscii(runtime, str.c_str(), str.size());
406
- }
407
-
408
- /// Create a PropNameID from utf8 values. The data is copied.
409
- /// Results are undefined if \p utf8 contains invalid code points.
410
- static PropNameID
411
- forUtf8(Runtime& runtime, const uint8_t* utf8, size_t length) {
412
- return runtime.createPropNameIDFromUtf8(utf8, length);
413
- }
414
-
415
- /// Create a PropNameID from utf8-encoded octets stored in a
416
- /// std::string. The string data is transformed and copied.
417
- /// Results are undefined if \p utf8 contains invalid code points.
418
- static PropNameID forUtf8(Runtime& runtime, const std::string& utf8) {
419
- return runtime.createPropNameIDFromUtf8(
420
- reinterpret_cast<const uint8_t*>(utf8.data()), utf8.size());
421
- }
422
-
423
- /// Create a PropNameID from a JS string.
424
- static PropNameID forString(Runtime& runtime, const jsi::String& str) {
425
- return runtime.createPropNameIDFromString(str);
426
- }
427
-
428
- // Creates a vector of PropNameIDs constructed from given arguments.
429
- template <typename... Args>
430
- static std::vector<PropNameID> names(Runtime& runtime, Args&&... args);
431
-
432
- // Creates a vector of given PropNameIDs.
433
- template <size_t N>
434
- static std::vector<PropNameID> names(PropNameID(&&propertyNames)[N]);
435
-
436
- /// Copies the data in a PropNameID as utf8 into a C++ string.
437
- std::string utf8(Runtime& runtime) const {
438
- return runtime.utf8(*this);
439
- }
440
-
441
- static bool compare(
442
- Runtime& runtime,
443
- const jsi::PropNameID& a,
444
- const jsi::PropNameID& b) {
445
- return runtime.compare(a, b);
446
- }
447
-
448
- friend class Runtime;
449
- friend class Value;
450
- };
451
-
452
- /// Represents a JS Symbol (es6). Movable, not copyable.
453
- /// TODO T40778724: this is a limited implementation sufficient for
454
- /// the debugger not to crash when a Symbol is a property in an Object
455
- /// or element in an array. Complete support for creating will come
456
- /// later.
457
- class JSI_EXPORT Symbol : public Pointer {
458
- public:
459
- using Pointer::Pointer;
460
-
461
- Symbol(Symbol&& other) = default;
462
- Symbol& operator=(Symbol&& other) = default;
463
-
464
- /// \return whether a and b refer to the same symbol.
465
- static bool strictEquals(Runtime& runtime, const Symbol& a, const Symbol& b) {
466
- return runtime.strictEquals(a, b);
467
- }
468
-
469
- /// Converts a Symbol into a C++ string as JS .toString would. The output
470
- /// will look like \c Symbol(description) .
471
- std::string toString(Runtime& runtime) const {
472
- return runtime.symbolToString(*this);
473
- }
474
-
475
- friend class Runtime;
476
- friend class Value;
477
- };
478
-
479
- /// Represents a JS String. Movable, not copyable.
480
- class JSI_EXPORT String : public Pointer {
481
- public:
482
- using Pointer::Pointer;
483
-
484
- String(String&& other) = default;
485
- String& operator=(String&& other) = default;
486
-
487
- /// Create a JS string from ascii values. The string data is
488
- /// copied.
489
- static String
490
- createFromAscii(Runtime& runtime, const char* str, size_t length) {
491
- return runtime.createStringFromAscii(str, length);
492
- }
493
-
494
- /// Create a JS string from a nul-terminated C ascii string. The
495
- /// string data is copied.
496
- static String createFromAscii(Runtime& runtime, const char* str) {
497
- return createFromAscii(runtime, str, strlen(str));
498
- }
499
-
500
- /// Create a JS string from a C++ string. The string data is
501
- /// copied.
502
- static String createFromAscii(Runtime& runtime, const std::string& str) {
503
- return createFromAscii(runtime, str.c_str(), str.size());
504
- }
505
-
506
- /// Create a JS string from utf8-encoded octets. The string data is
507
- /// transformed and copied. Results are undefined if \p utf8 contains invalid
508
- /// code points.
509
- static String
510
- createFromUtf8(Runtime& runtime, const uint8_t* utf8, size_t length) {
511
- return runtime.createStringFromUtf8(utf8, length);
512
- }
513
-
514
- /// Create a JS string from utf8-encoded octets stored in a
515
- /// std::string. The string data is transformed and copied. Results are
516
- /// undefined if \p utf8 contains invalid code points.
517
- static String createFromUtf8(Runtime& runtime, const std::string& utf8) {
518
- return runtime.createStringFromUtf8(
519
- reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
520
- }
521
-
522
- /// \return whether a and b contain the same characters.
523
- static bool strictEquals(Runtime& runtime, const String& a, const String& b) {
524
- return runtime.strictEquals(a, b);
525
- }
526
-
527
- /// Copies the data in a JS string as utf8 into a C++ string.
528
- std::string utf8(Runtime& runtime) const {
529
- return runtime.utf8(*this);
530
- }
531
-
532
- friend class Runtime;
533
- friend class Value;
534
- };
535
-
536
- class Array;
537
- class Function;
538
-
539
- /// Represents a JS Object. Movable, not copyable.
540
- class JSI_EXPORT Object : public Pointer {
541
- public:
542
- using Pointer::Pointer;
543
-
544
- Object(Object&& other) = default;
545
- Object& operator=(Object&& other) = default;
546
-
547
- /// Creates a new Object instance, like '{}' in JS.
548
- Object(Runtime& runtime) : Object(runtime.createObject()) {}
549
-
550
- static Object createFromHostObject(
551
- Runtime& runtime,
552
- std::shared_ptr<HostObject> ho) {
553
- return runtime.createObject(ho);
554
- }
555
-
556
- /// \return whether this and \c obj are the same JSObject or not.
557
- static bool strictEquals(Runtime& runtime, const Object& a, const Object& b) {
558
- return runtime.strictEquals(a, b);
559
- }
560
-
561
- /// \return the result of `this instanceOf ctor` in JS.
562
- bool instanceOf(Runtime& rt, const Function& ctor) {
563
- return rt.instanceOf(*this, ctor);
564
- }
565
-
566
- /// \return the property of the object with the given ascii name.
567
- /// If the name isn't a property on the object, returns the
568
- /// undefined value.
569
- Value getProperty(Runtime& runtime, const char* name) const;
570
-
571
- /// \return the property of the object with the String name.
572
- /// If the name isn't a property on the object, returns the
573
- /// undefined value.
574
- Value getProperty(Runtime& runtime, const String& name) const;
575
-
576
- /// \return the property of the object with the given JS PropNameID
577
- /// name. If the name isn't a property on the object, returns the
578
- /// undefined value.
579
- Value getProperty(Runtime& runtime, const PropNameID& name) const;
580
-
581
- /// \return true if and only if the object has a property with the
582
- /// given ascii name.
583
- bool hasProperty(Runtime& runtime, const char* name) const;
584
-
585
- /// \return true if and only if the object has a property with the
586
- /// given String name.
587
- bool hasProperty(Runtime& runtime, const String& name) const;
588
-
589
- /// \return true if and only if the object has a property with the
590
- /// given PropNameID name.
591
- bool hasProperty(Runtime& runtime, const PropNameID& name) const;
592
-
593
- /// Sets the property value from a Value or anything which can be
594
- /// used to make one: nullptr_t, bool, double, int, const char*,
595
- /// String, or Object.
596
- template <typename T>
597
- void setProperty(Runtime& runtime, const char* name, T&& value);
598
-
599
- /// Sets the property value from a Value or anything which can be
600
- /// used to make one: nullptr_t, bool, double, int, const char*,
601
- /// String, or Object.
602
- template <typename T>
603
- void setProperty(Runtime& runtime, const String& name, T&& value);
604
-
605
- /// Sets the property value from a Value or anything which can be
606
- /// used to make one: nullptr_t, bool, double, int, const char*,
607
- /// String, or Object.
608
- template <typename T>
609
- void setProperty(Runtime& runtime, const PropNameID& name, T&& value);
610
-
611
- /// \return true iff JS \c Array.isArray() would return \c true. If
612
- /// so, then \c getArray() will succeed.
613
- bool isArray(Runtime& runtime) const {
614
- return runtime.isArray(*this);
615
- }
616
-
617
- /// \return true iff the Object is an ArrayBuffer. If so, then \c
618
- /// getArrayBuffer() will succeed.
619
- bool isArrayBuffer(Runtime& runtime) const {
620
- return runtime.isArrayBuffer(*this);
621
- }
622
-
623
- /// \return true iff the Object is callable. If so, then \c
624
- /// getFunction will succeed.
625
- bool isFunction(Runtime& runtime) const {
626
- return runtime.isFunction(*this);
627
- }
628
-
629
- /// \return true iff the Object was initialized with \c createFromHostObject
630
- /// and the HostObject passed is of type \c T. If returns \c true then
631
- /// \c getHostObject<T> will succeed.
632
- template <typename T = HostObject>
633
- bool isHostObject(Runtime& runtime) const;
634
-
635
- /// \return an Array instance which refers to the same underlying
636
- /// object. If \c isArray() would return false, this will assert.
637
- Array getArray(Runtime& runtime) const&;
638
-
639
- /// \return an Array instance which refers to the same underlying
640
- /// object. If \c isArray() would return false, this will assert.
641
- Array getArray(Runtime& runtime) &&;
642
-
643
- /// \return an Array instance which refers to the same underlying
644
- /// object. If \c isArray() would return false, this will throw
645
- /// JSIException.
646
- Array asArray(Runtime& runtime) const&;
647
-
648
- /// \return an Array instance which refers to the same underlying
649
- /// object. If \c isArray() would return false, this will throw
650
- /// JSIException.
651
- Array asArray(Runtime& runtime) &&;
652
-
653
- /// \return an ArrayBuffer instance which refers to the same underlying
654
- /// object. If \c isArrayBuffer() would return false, this will assert.
655
- ArrayBuffer getArrayBuffer(Runtime& runtime) const&;
656
-
657
- /// \return an ArrayBuffer instance which refers to the same underlying
658
- /// object. If \c isArrayBuffer() would return false, this will assert.
659
- ArrayBuffer getArrayBuffer(Runtime& runtime) &&;
660
-
661
- /// \return a Function instance which refers to the same underlying
662
- /// object. If \c isFunction() would return false, this will assert.
663
- Function getFunction(Runtime& runtime) const&;
664
-
665
- /// \return a Function instance which refers to the same underlying
666
- /// object. If \c isFunction() would return false, this will assert.
667
- Function getFunction(Runtime& runtime) &&;
668
-
669
- /// \return a Function instance which refers to the same underlying
670
- /// object. If \c isFunction() would return false, this will throw
671
- /// JSIException.
672
- Function asFunction(Runtime& runtime) const&;
673
-
674
- /// \return a Function instance which refers to the same underlying
675
- /// object. If \c isFunction() would return false, this will throw
676
- /// JSIException.
677
- Function asFunction(Runtime& runtime) &&;
678
-
679
- /// \return a shared_ptr<T> which refers to the same underlying
680
- /// \c HostObject that was used to create this object. If \c isHostObject<T>
681
- /// is false, this will assert. Note that this does a type check and will
682
- /// assert if the underlying HostObject isn't of type \c T
683
- template <typename T = HostObject>
684
- std::shared_ptr<T> getHostObject(Runtime& runtime) const;
685
-
686
- /// \return a shared_ptr<T> which refers to the same underlying
687
- /// \c HostObject that was used to create this object. If \c isHostObject<T>
688
- /// is false, this will throw.
689
- template <typename T = HostObject>
690
- std::shared_ptr<T> asHostObject(Runtime& runtime) const;
691
-
692
- /// \return same as \c getProperty(name).asObject(), except with
693
- /// a better exception message.
694
- Object getPropertyAsObject(Runtime& runtime, const char* name) const;
695
-
696
- /// \return similar to \c
697
- /// getProperty(name).getObject().getFunction(), except it will
698
- /// throw JSIException instead of asserting if the property is
699
- /// not an object, or the object is not callable.
700
- Function getPropertyAsFunction(Runtime& runtime, const char* name) const;
701
-
702
- /// \return an Array consisting of all enumerable property names in
703
- /// the object and its prototype chain. All values in the return
704
- /// will be isString(). (This is probably not optimal, but it
705
- /// works. I only need it in one place.)
706
- Array getPropertyNames(Runtime& runtime) const;
707
-
708
- protected:
709
- void
710
- setPropertyValue(Runtime& runtime, const String& name, const Value& value) {
711
- return runtime.setPropertyValue(*this, name, value);
712
- }
713
-
714
- void setPropertyValue(
715
- Runtime& runtime,
716
- const PropNameID& name,
717
- const Value& value) {
718
- return runtime.setPropertyValue(*this, name, value);
719
- }
720
-
721
- friend class Runtime;
722
- friend class Value;
723
- };
724
-
725
- /// Represents a weak reference to a JS Object. If the only reference
726
- /// to an Object are these, the object is eligible for GC. Method
727
- /// names are inspired by C++ weak_ptr. Movable, not copyable.
728
- class JSI_EXPORT WeakObject : public Pointer {
729
- public:
730
- using Pointer::Pointer;
731
-
732
- WeakObject(WeakObject&& other) = default;
733
- WeakObject& operator=(WeakObject&& other) = default;
734
-
735
- /// Create a WeakObject from an Object.
736
- WeakObject(Runtime& runtime, const Object& o)
737
- : WeakObject(runtime.createWeakObject(o)) {}
738
-
739
- /// \return a Value representing the underlying Object if it is still valid;
740
- /// otherwise returns \c undefined. Note that this method has nothing to do
741
- /// with threads or concurrency. The name is based on std::weak_ptr::lock()
742
- /// which serves a similar purpose.
743
- Value lock(Runtime& runtime);
744
-
745
- friend class Runtime;
746
- };
747
-
748
- /// Represents a JS Object which can be efficiently used as an array
749
- /// with integral indices.
750
- class JSI_EXPORT Array : public Object {
751
- public:
752
- Array(Array&&) = default;
753
- /// Creates a new Array instance, with \c length undefined elements.
754
- Array(Runtime& runtime, size_t length) : Array(runtime.createArray(length)) {}
755
-
756
- Array& operator=(Array&&) = default;
757
-
758
- /// \return the size of the Array, according to its length property.
759
- /// (C++ naming convention)
760
- size_t size(Runtime& runtime) const {
761
- return runtime.size(*this);
762
- }
763
-
764
- /// \return the size of the Array, according to its length property.
765
- /// (JS naming convention)
766
- size_t length(Runtime& runtime) const {
767
- return size(runtime);
768
- }
769
-
770
- /// \return the property of the array at index \c i. If there is no
771
- /// such property, returns the undefined value. If \c i is out of
772
- /// range [ 0..\c length ] throws a JSIException.
773
- Value getValueAtIndex(Runtime& runtime, size_t i) const;
774
-
775
- /// Sets the property of the array at index \c i. The argument
776
- /// value behaves as with Object::setProperty(). If \c i is out of
777
- /// range [ 0..\c length ] throws a JSIException.
778
- template <typename T>
779
- void setValueAtIndex(Runtime& runtime, size_t i, T&& value);
780
-
781
- /// There is no current API for changing the size of an array once
782
- /// created. We'll probably need that eventually.
783
-
784
- /// Creates a new Array instance from provided values
785
- template <typename... Args>
786
- static Array createWithElements(Runtime&, Args&&... args);
787
-
788
- /// Creates a new Array instance from initializer list.
789
- static Array createWithElements(
790
- Runtime& runtime,
791
- std::initializer_list<Value> elements);
792
-
793
- private:
794
- friend class Object;
795
- friend class Value;
796
-
797
- void setValueAtIndexImpl(Runtime& runtime, size_t i, const Value& value) {
798
- return runtime.setValueAtIndexImpl(*this, i, value);
799
- }
800
-
801
- Array(Runtime::PointerValue* value) : Object(value) {}
802
- };
803
-
804
- /// Represents a JSArrayBuffer
805
- class JSI_EXPORT ArrayBuffer : public Object {
806
- public:
807
- ArrayBuffer(ArrayBuffer&&) = default;
808
- ArrayBuffer& operator=(ArrayBuffer&&) = default;
809
-
810
- /// \return the size of the ArrayBuffer, according to its byteLength property.
811
- /// (C++ naming convention)
812
- size_t size(Runtime& runtime) const {
813
- return runtime.size(*this);
814
- }
815
-
816
- size_t length(Runtime& runtime) const {
817
- return runtime.size(*this);
818
- }
819
-
820
- uint8_t* data(Runtime& runtime) {
821
- return runtime.data(*this);
822
- }
823
-
824
- private:
825
- friend class Object;
826
- friend class Value;
827
-
828
- ArrayBuffer(Runtime::PointerValue* value) : Object(value) {}
829
- };
830
-
831
- /// Represents a JS Object which is guaranteed to be Callable.
832
- class JSI_EXPORT Function : public Object {
833
- public:
834
- Function(Function&&) = default;
835
- Function& operator=(Function&&) = default;
836
-
837
- /// Create a function which, when invoked, calls C++ code. If the
838
- /// function throws an exception, a JS Error will be created and
839
- /// thrown.
840
- /// \param name the name property for the function.
841
- /// \param paramCount the length property for the function, which
842
- /// may not be the number of arguments the function is passed.
843
- static Function createFromHostFunction(
844
- Runtime& runtime,
845
- const jsi::PropNameID& name,
846
- unsigned int paramCount,
847
- jsi::HostFunctionType func);
848
-
849
- /// Calls the function with \c count \c args. The \c this value of the JS
850
- /// function will not be set by the C++ caller, similar to calling
851
- /// Function.prototype.apply(undefined, args) in JS.
852
- /// \b Note: as with Function.prototype.apply, \c this may not always be
853
- /// \c undefined in the function itself. If the function is non-strict,
854
- /// \c this will be set to the global object.
855
- Value call(Runtime& runtime, const Value* args, size_t count) const;
856
-
857
- /// Calls the function with a \c std::initializer_list of Value
858
- /// arguments. The \c this value of the JS function will not be set by the
859
- /// C++ caller, similar to calling Function.prototype.apply(undefined, args)
860
- /// in JS.
861
- /// \b Note: as with Function.prototype.apply, \c this may not always be
862
- /// \c undefined in the function itself. If the function is non-strict,
863
- /// \c this will be set to the global object.
864
- Value call(Runtime& runtime, std::initializer_list<Value> args) const;
865
-
866
- /// Calls the function with any number of arguments similarly to
867
- /// Object::setProperty(). The \c this value of the JS function will not be
868
- /// set by the C++ caller, similar to calling
869
- /// Function.prototype.call(undefined, ...args) in JS.
870
- /// \b Note: as with Function.prototype.call, \c this may not always be
871
- /// \c undefined in the function itself. If the function is non-strict,
872
- /// \c this will be set to the global object.
873
- template <typename... Args>
874
- Value call(Runtime& runtime, Args&&... args) const;
875
-
876
- /// Calls the function with \c count \c args and \c jsThis value passed
877
- /// as the \c this value.
878
- Value callWithThis(
879
- Runtime& Runtime,
880
- const Object& jsThis,
881
- const Value* args,
882
- size_t count) const;
883
-
884
- /// Calls the function with a \c std::initializer_list of Value
885
- /// arguments and \c jsThis passed as the \c this value.
886
- Value callWithThis(
887
- Runtime& runtime,
888
- const Object& jsThis,
889
- std::initializer_list<Value> args) const;
890
-
891
- /// Calls the function with any number of arguments similarly to
892
- /// Object::setProperty(), and with \c jsThis passed as the \c this value.
893
- template <typename... Args>
894
- Value callWithThis(Runtime& runtime, const Object& jsThis, Args&&... args)
895
- const;
896
-
897
- /// Calls the function as a constructor with \c count \c args. Equivalent
898
- /// to calling `new Func` where `Func` is the js function reqresented by
899
- /// this.
900
- Value callAsConstructor(Runtime& runtime, const Value* args, size_t count)
901
- const;
902
-
903
- /// Same as above `callAsConstructor`, except use an initializer_list to
904
- /// supply the arguments.
905
- Value callAsConstructor(Runtime& runtime, std::initializer_list<Value> args)
906
- const;
907
-
908
- /// Same as above `callAsConstructor`, but automatically converts/wraps
909
- /// any argument with a jsi Value.
910
- template <typename... Args>
911
- Value callAsConstructor(Runtime& runtime, Args&&... args) const;
912
-
913
- /// Returns whether this was created with Function::createFromHostFunction.
914
- /// If true then you can use getHostFunction to get the underlying
915
- /// HostFunctionType.
916
- bool isHostFunction(Runtime& runtime) const {
917
- return runtime.isHostFunction(*this);
918
- }
919
-
920
- /// Returns the underlying HostFunctionType iff isHostFunction returns true
921
- /// and asserts otherwise. You can use this to use std::function<>::target
922
- /// to get the object that was passed to create the HostFunctionType.
923
- ///
924
- /// Note: The reference returned is borrowed from the JS object underlying
925
- /// \c this, and thus only lasts as long as the object underlying
926
- /// \c this does.
927
- HostFunctionType& getHostFunction(Runtime& runtime) const {
928
- assert(isHostFunction(runtime));
929
- return runtime.getHostFunction(*this);
930
- }
931
-
932
- private:
933
- friend class Object;
934
- friend class Value;
935
-
936
- Function(Runtime::PointerValue* value) : Object(value) {}
937
- };
938
-
939
- /// Represents any JS Value (undefined, null, boolean, number, symbol,
940
- /// string, or object). Movable, or explicitly copyable (has no copy
941
- /// ctor).
942
- class JSI_EXPORT Value {
943
- public:
944
- /// Default ctor creates an \c undefined JS value.
945
- Value() : Value(UndefinedKind) {}
946
-
947
- /// Creates a \c null JS value.
948
- /* implicit */ Value(std::nullptr_t) : kind_(NullKind) {}
949
-
950
- /// Creates a boolean JS value.
951
- /* implicit */ Value(bool b) : Value(BooleanKind) {
952
- data_.boolean = b;
953
- }
954
-
955
- /// Creates a number JS value.
956
- /* implicit */ Value(double d) : Value(NumberKind) {
957
- data_.number = d;
958
- }
959
-
960
- /// Creates a number JS value.
961
- /* implicit */ Value(int i) : Value(NumberKind) {
962
- data_.number = i;
963
- }
964
-
965
- /// Moves a Symbol, String, or Object rvalue into a new JS value.
966
- template <typename T>
967
- /* implicit */ Value(T&& other) : Value(kindOf(other)) {
968
- static_assert(
969
- std::is_base_of<Symbol, T>::value ||
970
- std::is_base_of<String, T>::value ||
971
- std::is_base_of<Object, T>::value,
972
- "Value cannot be implicitly move-constructed from this type");
973
- new (&data_.pointer) T(std::move(other));
974
- }
975
-
976
- /// Value("foo") will treat foo as a bool. This makes doing that a
977
- /// compile error.
978
- template <typename T = void>
979
- Value(const char*) {
980
- static_assert(
981
- !std::is_same<void, T>::value,
982
- "Value cannot be constructed directly from const char*");
983
- }
984
-
985
- Value(Value&& value);
986
-
987
- /// Copies a Symbol lvalue into a new JS value.
988
- Value(Runtime& runtime, const Symbol& sym) : Value(SymbolKind) {
989
- new (&data_.pointer) Symbol(runtime.cloneSymbol(sym.ptr_));
990
- }
991
-
992
- /// Copies a String lvalue into a new JS value.
993
- Value(Runtime& runtime, const String& str) : Value(StringKind) {
994
- new (&data_.pointer) String(runtime.cloneString(str.ptr_));
995
- }
996
-
997
- /// Copies a Object lvalue into a new JS value.
998
- Value(Runtime& runtime, const Object& obj) : Value(ObjectKind) {
999
- new (&data_.pointer) Object(runtime.cloneObject(obj.ptr_));
1000
- }
1001
-
1002
- /// Creates a JS value from another Value lvalue.
1003
- Value(Runtime& runtime, const Value& value);
1004
-
1005
- /// Value(rt, "foo") will treat foo as a bool. This makes doing
1006
- /// that a compile error.
1007
- template <typename T = void>
1008
- Value(Runtime&, const char*) {
1009
- static_assert(
1010
- !std::is_same<T, void>::value,
1011
- "Value cannot be constructed directly from const char*");
1012
- }
1013
-
1014
- ~Value();
1015
- // \return the undefined \c Value.
1016
- static Value undefined() {
1017
- return Value();
1018
- }
1019
-
1020
- // \return the null \c Value.
1021
- static Value null() {
1022
- return Value(nullptr);
1023
- }
1024
-
1025
- // \return a \c Value created from a utf8-encoded JSON string.
1026
- static Value
1027
- createFromJsonUtf8(Runtime& runtime, const uint8_t* json, size_t length) {
1028
- return runtime.createValueFromJsonUtf8(json, length);
1029
- }
1030
-
1031
- /// \return according to the Strict Equality Comparison algorithm, see:
1032
- /// https://262.ecma-international.org/11.0/#sec-strict-equality-comparison
1033
- static bool strictEquals(Runtime& runtime, const Value& a, const Value& b);
1034
-
1035
- Value& operator=(Value&& other) {
1036
- this->~Value();
1037
- new (this) Value(std::move(other));
1038
- return *this;
1039
- }
1040
-
1041
- bool isUndefined() const {
1042
- return kind_ == UndefinedKind;
1043
- }
1044
-
1045
- bool isNull() const {
1046
- return kind_ == NullKind;
1047
- }
1048
-
1049
- bool isBool() const {
1050
- return kind_ == BooleanKind;
1051
- }
1052
-
1053
- bool isNumber() const {
1054
- return kind_ == NumberKind;
1055
- }
1056
-
1057
- bool isString() const {
1058
- return kind_ == StringKind;
1059
- }
1060
-
1061
- bool isSymbol() const {
1062
- return kind_ == SymbolKind;
1063
- }
1064
-
1065
- bool isObject() const {
1066
- return kind_ == ObjectKind;
1067
- }
1068
-
1069
- /// \return the boolean value, or asserts if not a boolean.
1070
- bool getBool() const {
1071
- assert(isBool());
1072
- return data_.boolean;
1073
- }
1074
-
1075
- /// \return the boolean value, or throws JSIException if not a
1076
- /// boolean.
1077
- bool asBool() const;
1078
-
1079
- /// \return the number value, or asserts if not a number.
1080
- double getNumber() const {
1081
- assert(isNumber());
1082
- return data_.number;
1083
- }
1084
-
1085
- /// \return the number value, or throws JSIException if not a
1086
- /// number.
1087
- double asNumber() const;
1088
-
1089
- /// \return the Symbol value, or asserts if not a symbol.
1090
- Symbol getSymbol(Runtime& runtime) const& {
1091
- assert(isSymbol());
1092
- return Symbol(runtime.cloneSymbol(data_.pointer.ptr_));
1093
- }
1094
-
1095
- /// \return the Symbol value, or asserts if not a symbol.
1096
- /// Can be used on rvalue references to avoid cloning more symbols.
1097
- Symbol getSymbol(Runtime&) && {
1098
- assert(isSymbol());
1099
- auto ptr = data_.pointer.ptr_;
1100
- data_.pointer.ptr_ = nullptr;
1101
- return static_cast<Symbol>(ptr);
1102
- }
1103
-
1104
- /// \return the Symbol value, or throws JSIException if not a
1105
- /// symbol
1106
- Symbol asSymbol(Runtime& runtime) const&;
1107
- Symbol asSymbol(Runtime& runtime) &&;
1108
-
1109
- /// \return the String value, or asserts if not a string.
1110
- String getString(Runtime& runtime) const& {
1111
- assert(isString());
1112
- return String(runtime.cloneString(data_.pointer.ptr_));
1113
- }
1114
-
1115
- /// \return the String value, or asserts if not a string.
1116
- /// Can be used on rvalue references to avoid cloning more strings.
1117
- String getString(Runtime&) && {
1118
- assert(isString());
1119
- auto ptr = data_.pointer.ptr_;
1120
- data_.pointer.ptr_ = nullptr;
1121
- return static_cast<String>(ptr);
1122
- }
1123
-
1124
- /// \return the String value, or throws JSIException if not a
1125
- /// string.
1126
- String asString(Runtime& runtime) const&;
1127
- String asString(Runtime& runtime) &&;
1128
-
1129
- /// \return the Object value, or asserts if not an object.
1130
- Object getObject(Runtime& runtime) const& {
1131
- assert(isObject());
1132
- return Object(runtime.cloneObject(data_.pointer.ptr_));
1133
- }
1134
-
1135
- /// \return the Object value, or asserts if not an object.
1136
- /// Can be used on rvalue references to avoid cloning more objects.
1137
- Object getObject(Runtime&) && {
1138
- assert(isObject());
1139
- auto ptr = data_.pointer.ptr_;
1140
- data_.pointer.ptr_ = nullptr;
1141
- return static_cast<Object>(ptr);
1142
- }
1143
-
1144
- /// \return the Object value, or throws JSIException if not an
1145
- /// object.
1146
- Object asObject(Runtime& runtime) const&;
1147
- Object asObject(Runtime& runtime) &&;
1148
-
1149
- // \return a String like JS .toString() would do.
1150
- String toString(Runtime& runtime) const;
1151
-
1152
- private:
1153
- friend class Runtime;
1154
-
1155
- enum ValueKind {
1156
- UndefinedKind,
1157
- NullKind,
1158
- BooleanKind,
1159
- NumberKind,
1160
- SymbolKind,
1161
- StringKind,
1162
- ObjectKind,
1163
- PointerKind = SymbolKind,
1164
- };
1165
-
1166
- union Data {
1167
- // Value's ctor and dtor will manage the lifecycle of the contained Data.
1168
- Data() {
1169
- static_assert(
1170
- sizeof(Data) == sizeof(uint64_t),
1171
- "Value data should fit in a 64-bit register");
1172
- }
1173
- ~Data() {}
1174
-
1175
- // scalars
1176
- bool boolean;
1177
- double number;
1178
- // pointers
1179
- Pointer pointer; // Symbol, String, Object, Array, Function
1180
- };
1181
-
1182
- Value(ValueKind kind) : kind_(kind) {}
1183
-
1184
- constexpr static ValueKind kindOf(const Symbol&) {
1185
- return SymbolKind;
1186
- }
1187
- constexpr static ValueKind kindOf(const String&) {
1188
- return StringKind;
1189
- }
1190
- constexpr static ValueKind kindOf(const Object&) {
1191
- return ObjectKind;
1192
- }
1193
-
1194
- ValueKind kind_;
1195
- Data data_;
1196
-
1197
- // In the future: Value becomes NaN-boxed. See T40538354.
1198
- };
1199
-
1200
- /// Not movable and not copyable RAII marker advising the underlying
1201
- /// JavaScript VM to track resources allocated since creation until
1202
- /// destruction so that they can be recycled eagerly when the Scope
1203
- /// goes out of scope instead of floating in the air until the next
1204
- /// garbage collection or any other delayed release occurs.
1205
- ///
1206
- /// This API should be treated only as advice, implementations can
1207
- /// choose to ignore the fact that Scopes are created or destroyed.
1208
- ///
1209
- /// This class is an exception to the rule allowing destructors to be
1210
- /// called without proper synchronization (see Runtime documentation).
1211
- /// The whole point of this class is to enable all sorts of clean ups
1212
- /// when the destructor is called and this proper synchronization is
1213
- /// required at that time.
1214
- ///
1215
- /// Instances of this class are intended to be created as automatic stack
1216
- /// variables in which case destructor calls don't require any additional
1217
- /// locking, provided that the lock (if any) is managed with RAII helpers.
1218
- class JSI_EXPORT Scope {
1219
- public:
1220
- explicit Scope(Runtime& rt) : rt_(rt), prv_(rt.pushScope()) {}
1221
- ~Scope() {
1222
- rt_.popScope(prv_);
1223
- };
1224
-
1225
- Scope(const Scope&) = delete;
1226
- Scope(Scope&&) = delete;
1227
-
1228
- Scope& operator=(const Scope&) = delete;
1229
- Scope& operator=(Scope&&) = delete;
1230
-
1231
- template <typename F>
1232
- static auto callInNewScope(Runtime& rt, F f) -> decltype(f()) {
1233
- Scope s(rt);
1234
- return f();
1235
- }
1236
-
1237
- private:
1238
- Runtime& rt_;
1239
- Runtime::ScopeState* prv_;
1240
- };
1241
-
1242
- /// Base class for jsi exceptions
1243
- class JSI_EXPORT JSIException : public std::exception {
1244
- protected:
1245
- JSIException(){};
1246
- JSIException(std::string what) : what_(std::move(what)){};
1247
-
1248
- public:
1249
- JSIException(const JSIException&) = default;
1250
-
1251
- virtual const char* what() const noexcept override {
1252
- return what_.c_str();
1253
- }
1254
-
1255
- virtual ~JSIException() override;
1256
-
1257
- protected:
1258
- std::string what_;
1259
- };
1260
-
1261
- /// This exception will be thrown by API functions on errors not related to
1262
- /// JavaScript execution.
1263
- class JSI_EXPORT JSINativeException : public JSIException {
1264
- public:
1265
- JSINativeException(std::string what) : JSIException(std::move(what)) {}
1266
-
1267
- JSINativeException(const JSINativeException&) = default;
1268
-
1269
- virtual ~JSINativeException();
1270
- };
1271
-
1272
- /// This exception will be thrown by API functions whenever a JS
1273
- /// operation causes an exception as described by the spec, or as
1274
- /// otherwise described.
1275
- class JSI_EXPORT JSError : public JSIException {
1276
- public:
1277
- /// Creates a JSError referring to provided \c value
1278
- JSError(Runtime& r, Value&& value);
1279
-
1280
- /// Creates a JSError referring to new \c Error instance capturing current
1281
- /// JavaScript stack. The error message property is set to given \c message.
1282
- JSError(Runtime& rt, std::string message);
1283
-
1284
- /// Creates a JSError referring to new \c Error instance capturing current
1285
- /// JavaScript stack. The error message property is set to given \c message.
1286
- JSError(Runtime& rt, const char* message)
1287
- : JSError(rt, std::string(message)){};
1288
-
1289
- /// Creates a JSError referring to a JavaScript Object having message and
1290
- /// stack properties set to provided values.
1291
- JSError(Runtime& rt, std::string message, std::string stack);
1292
-
1293
- /// Creates a JSError referring to provided value and what string
1294
- /// set to provided message. This argument order is a bit weird,
1295
- /// but necessary to avoid ambiguity with the above.
1296
- JSError(std::string what, Runtime& rt, Value&& value);
1297
-
1298
- JSError(const JSError&) = default;
1299
-
1300
- virtual ~JSError();
1301
-
1302
- const std::string& getStack() const {
1303
- return stack_;
1304
- }
1305
-
1306
- const std::string& getMessage() const {
1307
- return message_;
1308
- }
1309
-
1310
- const jsi::Value& value() const {
1311
- assert(value_);
1312
- return *value_;
1313
- }
1314
-
1315
- private:
1316
- // This initializes the value_ member and does some other
1317
- // validation, so it must be called by every branch through the
1318
- // constructors.
1319
- void setValue(Runtime& rt, Value&& value);
1320
-
1321
- // This needs to be on the heap, because throw requires the object
1322
- // be copyable, and Value is not.
1323
- std::shared_ptr<jsi::Value> value_;
1324
- std::string message_;
1325
- std::string stack_;
1326
- };
1327
-
1328
- } // namespace jsi
1329
- } // namespace facebook
1330
-
1331
- #include <jsi/jsi-inl.h>