react-native-windows 0.80.5 → 0.80.6

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