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,1095 @@
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 <tuple>
11
+
12
+ #include <jsi/instrumentation.h>
13
+ #include <jsi/jsi.h>
14
+
15
+ // This file contains objects to help API users create their own
16
+ // runtime adapters, i.e. if you want to compose runtimes to add your
17
+ // own behavior.
18
+
19
+ namespace facebook {
20
+ namespace jsi {
21
+
22
+ // Use this to wrap host functions. It will pass the member runtime as
23
+ // the first arg to the callback. The first argument to the ctor
24
+ // should be the decorated runtime, not the plain one.
25
+ class DecoratedHostFunction {
26
+ public:
27
+ DecoratedHostFunction(Runtime& drt, HostFunctionType plainHF)
28
+ : drt_(drt), plainHF_(std::move(plainHF)) {}
29
+
30
+ Runtime& decoratedRuntime() {
31
+ return drt_;
32
+ }
33
+
34
+ Value
35
+ operator()(Runtime&, const Value& thisVal, const Value* args, size_t count) {
36
+ return plainHF_(decoratedRuntime(), thisVal, args, count);
37
+ }
38
+
39
+ private:
40
+ template <typename Plain, typename Base>
41
+ friend class RuntimeDecorator;
42
+
43
+ Runtime& drt_;
44
+ HostFunctionType plainHF_;
45
+ };
46
+
47
+ // From the perspective of the caller, a plain HostObject is passed to
48
+ // the decorated Runtime, and the HostObject methods expect to get
49
+ // passed that Runtime. But the plain Runtime will pass itself to its
50
+ // callback, so we need a helper here which curries the decorated
51
+ // Runtime, and calls the plain HostObject with it.
52
+ //
53
+ // If the concrete RuntimeDecorator derives DecoratedHostObject, it
54
+ // should call the base class get() and set() to invoke the plain
55
+ // HostObject functionality. The Runtime& it passes does not matter,
56
+ // as it is not used.
57
+ class DecoratedHostObject : public HostObject {
58
+ public:
59
+ DecoratedHostObject(Runtime& drt, std::shared_ptr<HostObject> plainHO)
60
+ : drt_(drt), plainHO_(plainHO) {}
61
+
62
+ // The derived class methods can call this to get a reference to the
63
+ // decorated runtime, since the rt passed to the callback will be
64
+ // the plain runtime.
65
+ Runtime& decoratedRuntime() {
66
+ return drt_;
67
+ }
68
+
69
+ Value get(Runtime&, const PropNameID& name) override {
70
+ return plainHO_->get(decoratedRuntime(), name);
71
+ }
72
+
73
+ void set(Runtime&, const PropNameID& name, const Value& value) override {
74
+ plainHO_->set(decoratedRuntime(), name, value);
75
+ }
76
+
77
+ std::vector<PropNameID> getPropertyNames(Runtime&) override {
78
+ return plainHO_->getPropertyNames(decoratedRuntime());
79
+ }
80
+
81
+ private:
82
+ template <typename Plain, typename Base>
83
+ friend class RuntimeDecorator;
84
+
85
+ Runtime& drt_;
86
+ std::shared_ptr<HostObject> plainHO_;
87
+ };
88
+
89
+ /// C++ variant on a standard Decorator pattern, using template
90
+ /// parameters. The \c Plain template parameter type is the
91
+ /// undecorated Runtime type. You can usually use \c Runtime here,
92
+ /// but if you know the concrete type ahead of time and it's final,
93
+ /// the compiler can devirtualize calls to the decorated
94
+ /// implementation. The \c Base template parameter type will be used
95
+ /// as the base class of the decorated type. Here, too, you can
96
+ /// usually use \c Runtime, but if you want the decorated type to
97
+ /// implement a derived class of Runtime, you can specify that here.
98
+ /// For an example, see threadsafe.h.
99
+ template <typename Plain = Runtime, typename Base = Runtime>
100
+ class RuntimeDecorator : public Base, private jsi::Instrumentation {
101
+ public:
102
+ Plain& plain() {
103
+ static_assert(
104
+ std::is_base_of<Runtime, Plain>::value,
105
+ "RuntimeDecorator's Plain type must derive from jsi::Runtime");
106
+ static_assert(
107
+ std::is_base_of<Runtime, Base>::value,
108
+ "RuntimeDecorator's Base type must derive from jsi::Runtime");
109
+ return plain_;
110
+ }
111
+ const Plain& plain() const {
112
+ return plain_;
113
+ }
114
+
115
+ #if JSI_VERSION >= 20
116
+ ICast* castInterface(const UUID& interfaceUUID) override {
117
+ return plain().castInterface(interfaceUUID);
118
+ }
119
+ #endif
120
+
121
+ Value evaluateJavaScript(
122
+ const std::shared_ptr<const Buffer>& buffer,
123
+ const std::string& sourceURL) override {
124
+ return plain().evaluateJavaScript(buffer, sourceURL);
125
+ }
126
+ std::shared_ptr<const PreparedJavaScript> prepareJavaScript(
127
+ const std::shared_ptr<const Buffer>& buffer,
128
+ std::string sourceURL) override {
129
+ return plain().prepareJavaScript(buffer, std::move(sourceURL));
130
+ }
131
+ Value evaluatePreparedJavaScript(
132
+ const std::shared_ptr<const PreparedJavaScript>& js) override {
133
+ return plain().evaluatePreparedJavaScript(js);
134
+ }
135
+ #if JSI_VERSION >= 12
136
+ void queueMicrotask(const jsi::Function& callback) override {
137
+ return plain().queueMicrotask(callback);
138
+ }
139
+ #endif
140
+ #if JSI_VERSION >= 4
141
+ bool drainMicrotasks(int maxMicrotasksHint) override {
142
+ return plain().drainMicrotasks(maxMicrotasksHint);
143
+ }
144
+ #endif
145
+ Object global() override {
146
+ return plain().global();
147
+ }
148
+ std::string description() override {
149
+ return plain().description();
150
+ };
151
+ bool isInspectable() override {
152
+ return plain().isInspectable();
153
+ };
154
+ Instrumentation& instrumentation() override {
155
+ return *this;
156
+ }
157
+
158
+ protected:
159
+ // plain is generally going to be a reference to an object managed
160
+ // by a derived class. We cache it here so this class can be
161
+ // concrete, and avoid making virtual calls to find the plain
162
+ // Runtime. Note that the ctor and dtor do not access through the
163
+ // reference, so passing a reference to an object before its
164
+ // lifetime has started is ok.
165
+ RuntimeDecorator(Plain& plain) : plain_(plain) {}
166
+
167
+ Runtime::PointerValue* cloneSymbol(const Runtime::PointerValue* pv) override {
168
+ return plain_.cloneSymbol(pv);
169
+ };
170
+ #if JSI_VERSION >= 6
171
+ Runtime::PointerValue* cloneBigInt(const Runtime::PointerValue* pv) override {
172
+ return plain_.cloneBigInt(pv);
173
+ };
174
+ #endif
175
+ Runtime::PointerValue* cloneString(const Runtime::PointerValue* pv) override {
176
+ return plain_.cloneString(pv);
177
+ };
178
+ Runtime::PointerValue* cloneObject(const Runtime::PointerValue* pv) override {
179
+ return plain_.cloneObject(pv);
180
+ };
181
+ Runtime::PointerValue* clonePropNameID(
182
+ const Runtime::PointerValue* pv) override {
183
+ return plain_.clonePropNameID(pv);
184
+ };
185
+
186
+ PropNameID createPropNameIDFromAscii(const char* str, size_t length)
187
+ override {
188
+ return plain_.createPropNameIDFromAscii(str, length);
189
+ };
190
+ PropNameID createPropNameIDFromUtf8(const uint8_t* utf8, size_t length)
191
+ override {
192
+ return plain_.createPropNameIDFromUtf8(utf8, length);
193
+ };
194
+ PropNameID createPropNameIDFromString(const String& str) override {
195
+ return plain_.createPropNameIDFromString(str);
196
+ };
197
+ #if JSI_VERSION >= 19
198
+ PropNameID createPropNameIDFromUtf16(const char16_t* utf16, size_t length)
199
+ override {
200
+ return plain_.createPropNameIDFromUtf16(utf16, length);
201
+ }
202
+ #endif
203
+ #if JSI_VERSION >= 5
204
+ PropNameID createPropNameIDFromSymbol(const Symbol& sym) override {
205
+ return plain_.createPropNameIDFromSymbol(sym);
206
+ };
207
+ #endif
208
+ std::string utf8(const PropNameID& id) override {
209
+ return plain_.utf8(id);
210
+ };
211
+ bool compare(const PropNameID& a, const PropNameID& b) override {
212
+ return plain_.compare(a, b);
213
+ };
214
+
215
+ std::string symbolToString(const Symbol& sym) override {
216
+ return plain_.symbolToString(sym);
217
+ }
218
+
219
+ #if JSI_VERSION >= 8
220
+ BigInt createBigIntFromInt64(int64_t value) override {
221
+ return plain_.createBigIntFromInt64(value);
222
+ }
223
+ BigInt createBigIntFromUint64(uint64_t value) override {
224
+ return plain_.createBigIntFromUint64(value);
225
+ }
226
+ bool bigintIsInt64(const BigInt& b) override {
227
+ return plain_.bigintIsInt64(b);
228
+ }
229
+ bool bigintIsUint64(const BigInt& b) override {
230
+ return plain_.bigintIsUint64(b);
231
+ }
232
+ uint64_t truncate(const BigInt& b) override {
233
+ return plain_.truncate(b);
234
+ }
235
+ String bigintToString(const BigInt& bigint, int radix) override {
236
+ return plain_.bigintToString(bigint, radix);
237
+ }
238
+ #endif
239
+
240
+ String createStringFromAscii(const char* str, size_t length) override {
241
+ return plain_.createStringFromAscii(str, length);
242
+ };
243
+ String createStringFromUtf8(const uint8_t* utf8, size_t length) override {
244
+ return plain_.createStringFromUtf8(utf8, length);
245
+ };
246
+ #if JSI_VERSION >= 19
247
+ String createStringFromUtf16(const char16_t* utf16, size_t length) override {
248
+ return plain_.createStringFromUtf16(utf16, length);
249
+ }
250
+ #endif
251
+ std::string utf8(const String& s) override {
252
+ return plain_.utf8(s);
253
+ }
254
+
255
+ #if JSI_VERSION >= 14
256
+ std::u16string utf16(const String& str) override {
257
+ return plain_.utf16(str);
258
+ }
259
+ std::u16string utf16(const PropNameID& sym) override {
260
+ return plain_.utf16(sym);
261
+ }
262
+ #endif
263
+
264
+ #if JSI_VERSION >= 16
265
+ void getStringData(
266
+ const jsi::String& str,
267
+ void* ctx,
268
+ void (
269
+ *cb)(void* ctx, bool ascii, const void* data, size_t num)) override {
270
+ plain_.getStringData(str, ctx, cb);
271
+ }
272
+
273
+ void getPropNameIdData(
274
+ const jsi::PropNameID& sym,
275
+ void* ctx,
276
+ void (
277
+ *cb)(void* ctx, bool ascii, const void* data, size_t num)) override {
278
+ plain_.getPropNameIdData(sym, ctx, cb);
279
+ }
280
+ #endif
281
+
282
+ #if JSI_VERSION >= 18
283
+ Object createObjectWithPrototype(const Value& prototype) override {
284
+ return plain_.createObjectWithPrototype(prototype);
285
+ }
286
+ #endif
287
+
288
+ Object createObject() override {
289
+ return plain_.createObject();
290
+ };
291
+
292
+ Object createObject(std::shared_ptr<HostObject> ho) override {
293
+ return plain_.createObject(
294
+ std::make_shared<DecoratedHostObject>(*this, std::move(ho)));
295
+ };
296
+ std::shared_ptr<HostObject> getHostObject(const jsi::Object& o) override {
297
+ std::shared_ptr<HostObject> dho = plain_.getHostObject(o);
298
+ return static_cast<DecoratedHostObject&>(*dho).plainHO_;
299
+ };
300
+ HostFunctionType& getHostFunction(const jsi::Function& f) override {
301
+ HostFunctionType& dhf = plain_.getHostFunction(f);
302
+ // This will fail if a cpp file including this header is not compiled
303
+ // with RTTI.
304
+ return dhf.target<DecoratedHostFunction>()->plainHF_;
305
+ };
306
+
307
+ #if JSI_VERSION >= 7
308
+ bool hasNativeState(const Object& o) override {
309
+ return plain_.hasNativeState(o);
310
+ }
311
+ std::shared_ptr<NativeState> getNativeState(const Object& o) override {
312
+ return plain_.getNativeState(o);
313
+ }
314
+ void setNativeState(const Object& o, std::shared_ptr<NativeState> state)
315
+ override {
316
+ plain_.setNativeState(o, state);
317
+ }
318
+ #endif
319
+
320
+ #if JSI_VERSION >= 11
321
+ void setExternalMemoryPressure(const Object& obj, size_t amt) override {
322
+ plain_.setExternalMemoryPressure(obj, amt);
323
+ }
324
+ #endif
325
+
326
+ #if JSI_VERSION >= 17
327
+ void setPrototypeOf(const Object& object, const Value& prototype) override {
328
+ plain_.setPrototypeOf(object, prototype);
329
+ }
330
+
331
+ Value getPrototypeOf(const Object& object) override {
332
+ return plain_.getPrototypeOf(object);
333
+ }
334
+ #endif
335
+
336
+ Value getProperty(const Object& o, const PropNameID& name) override {
337
+ return plain_.getProperty(o, name);
338
+ };
339
+ Value getProperty(const Object& o, const String& name) override {
340
+ return plain_.getProperty(o, name);
341
+ };
342
+ bool hasProperty(const Object& o, const PropNameID& name) override {
343
+ return plain_.hasProperty(o, name);
344
+ };
345
+ bool hasProperty(const Object& o, const String& name) override {
346
+ return plain_.hasProperty(o, name);
347
+ };
348
+ void setPropertyValue(
349
+ JSI_CONST_10 Object& o,
350
+ const PropNameID& name,
351
+ const Value& value) override {
352
+ plain_.setPropertyValue(o, name, value);
353
+ };
354
+ void setPropertyValue(
355
+ JSI_CONST_10 Object& o,
356
+ const String& name,
357
+ const Value& value) override {
358
+ plain_.setPropertyValue(o, name, value);
359
+ };
360
+
361
+ bool isArray(const Object& o) const override {
362
+ return plain_.isArray(o);
363
+ };
364
+ bool isArrayBuffer(const Object& o) const override {
365
+ return plain_.isArrayBuffer(o);
366
+ };
367
+ bool isFunction(const Object& o) const override {
368
+ return plain_.isFunction(o);
369
+ };
370
+ bool isHostObject(const jsi::Object& o) const override {
371
+ return plain_.isHostObject(o);
372
+ };
373
+ bool isHostFunction(const jsi::Function& f) const override {
374
+ return plain_.isHostFunction(f);
375
+ };
376
+ Array getPropertyNames(const Object& o) override {
377
+ return plain_.getPropertyNames(o);
378
+ };
379
+
380
+ WeakObject createWeakObject(const Object& o) override {
381
+ return plain_.createWeakObject(o);
382
+ };
383
+ Value lockWeakObject(JSI_NO_CONST_3 JSI_CONST_10 WeakObject& wo) override {
384
+ return plain_.lockWeakObject(wo);
385
+ };
386
+
387
+ Array createArray(size_t length) override {
388
+ return plain_.createArray(length);
389
+ };
390
+ #if JSI_VERSION >= 9
391
+ ArrayBuffer createArrayBuffer(
392
+ std::shared_ptr<MutableBuffer> buffer) override {
393
+ return plain_.createArrayBuffer(std::move(buffer));
394
+ };
395
+ #endif
396
+ size_t size(const Array& a) override {
397
+ return plain_.size(a);
398
+ };
399
+ size_t size(const ArrayBuffer& ab) override {
400
+ return plain_.size(ab);
401
+ };
402
+ uint8_t* data(const ArrayBuffer& ab) override {
403
+ return plain_.data(ab);
404
+ };
405
+ Value getValueAtIndex(const Array& a, size_t i) override {
406
+ return plain_.getValueAtIndex(a, i);
407
+ };
408
+ void setValueAtIndexImpl(JSI_CONST_10 Array& a, size_t i, const Value& value)
409
+ override {
410
+ plain_.setValueAtIndexImpl(a, i, value);
411
+ };
412
+
413
+ Function createFunctionFromHostFunction(
414
+ const PropNameID& name,
415
+ unsigned int paramCount,
416
+ HostFunctionType func) override {
417
+ return plain_.createFunctionFromHostFunction(
418
+ name, paramCount, DecoratedHostFunction(*this, std::move(func)));
419
+ };
420
+ Value call(
421
+ const Function& f,
422
+ const Value& jsThis,
423
+ const Value* args,
424
+ size_t count) override {
425
+ return plain_.call(f, jsThis, args, count);
426
+ };
427
+ Value callAsConstructor(const Function& f, const Value* args, size_t count)
428
+ override {
429
+ return plain_.callAsConstructor(f, args, count);
430
+ };
431
+
432
+ #if JSI_VERSION >= 20
433
+ void setRuntimeDataImpl(
434
+ const UUID& uuid,
435
+ const void* data,
436
+ void (*deleter)(const void* data)) override {
437
+ return plain_.setRuntimeDataImpl(uuid, data, deleter);
438
+ }
439
+
440
+ const void* getRuntimeDataImpl(const UUID& uuid) override {
441
+ return plain_.getRuntimeDataImpl(uuid);
442
+ }
443
+ #endif
444
+
445
+ // Private data for managing scopes.
446
+ Runtime::ScopeState* pushScope() override {
447
+ return plain_.pushScope();
448
+ }
449
+ void popScope(Runtime::ScopeState* ss) override {
450
+ plain_.popScope(ss);
451
+ }
452
+
453
+ bool strictEquals(const Symbol& a, const Symbol& b) const override {
454
+ return plain_.strictEquals(a, b);
455
+ };
456
+ #if JSI_VERSION >= 6
457
+ bool strictEquals(const BigInt& a, const BigInt& b) const override {
458
+ return plain_.strictEquals(a, b);
459
+ };
460
+ #endif
461
+ bool strictEquals(const String& a, const String& b) const override {
462
+ return plain_.strictEquals(a, b);
463
+ };
464
+ bool strictEquals(const Object& a, const Object& b) const override {
465
+ return plain_.strictEquals(a, b);
466
+ };
467
+
468
+ bool instanceOf(const Object& o, const Function& f) override {
469
+ return plain_.instanceOf(o, f);
470
+ };
471
+
472
+ // jsi::Instrumentation methods
473
+
474
+ std::string getRecordedGCStats() override {
475
+ return plain().instrumentation().getRecordedGCStats();
476
+ }
477
+
478
+ std::unordered_map<std::string, int64_t> getHeapInfo(
479
+ bool includeExpensive) override {
480
+ return plain().instrumentation().getHeapInfo(includeExpensive);
481
+ }
482
+
483
+ void collectGarbage(std::string cause) override {
484
+ plain().instrumentation().collectGarbage(std::move(cause));
485
+ }
486
+
487
+ void startTrackingHeapObjectStackTraces(
488
+ std::function<void(
489
+ uint64_t,
490
+ std::chrono::microseconds,
491
+ std::vector<HeapStatsUpdate>)> callback) override {
492
+ plain().instrumentation().startTrackingHeapObjectStackTraces(
493
+ std::move(callback));
494
+ }
495
+
496
+ void stopTrackingHeapObjectStackTraces() override {
497
+ plain().instrumentation().stopTrackingHeapObjectStackTraces();
498
+ }
499
+
500
+ void startHeapSampling(size_t samplingInterval) override {
501
+ plain().instrumentation().startHeapSampling(samplingInterval);
502
+ }
503
+
504
+ void stopHeapSampling(std::ostream& os) override {
505
+ plain().instrumentation().stopHeapSampling(os);
506
+ }
507
+
508
+ #if JSI_VERSION >= 13
509
+ void createSnapshotToFile(
510
+ const std::string& path,
511
+ const HeapSnapshotOptions& options) override {
512
+ plain().instrumentation().createSnapshotToFile(path, options);
513
+ }
514
+ #else
515
+ void createSnapshotToFile(const std::string& path) override {
516
+ plain().instrumentation().createSnapshotToFile(path);
517
+ }
518
+ #endif
519
+
520
+ #if JSI_VERSION >= 13
521
+ void createSnapshotToStream(
522
+ std::ostream& os,
523
+ const HeapSnapshotOptions& options) override {
524
+ plain().instrumentation().createSnapshotToStream(os, options);
525
+ }
526
+ #else
527
+ void createSnapshotToStream(std::ostream& os) override {
528
+ plain().instrumentation().createSnapshotToStream(os);
529
+ }
530
+ #endif
531
+
532
+ std::string flushAndDisableBridgeTrafficTrace() override {
533
+ return const_cast<Plain&>(plain())
534
+ .instrumentation()
535
+ .flushAndDisableBridgeTrafficTrace();
536
+ }
537
+
538
+ void writeBasicBlockProfileTraceToFile(
539
+ const std::string& fileName) const override {
540
+ const_cast<Plain&>(plain())
541
+ .instrumentation()
542
+ .writeBasicBlockProfileTraceToFile(fileName);
543
+ }
544
+
545
+ /// Dump external profiler symbols to the given file name.
546
+ void dumpProfilerSymbolsToFile(const std::string& fileName) const override {
547
+ const_cast<Plain&>(plain()).instrumentation().dumpProfilerSymbolsToFile(
548
+ fileName);
549
+ }
550
+
551
+ private:
552
+ Plain& plain_;
553
+ };
554
+
555
+ namespace detail {
556
+
557
+ // This metaprogramming allows the With type's methods to be
558
+ // optional.
559
+
560
+ template <typename T, typename U = void>
561
+ struct BeforeCaller {
562
+ static void before(T&) {}
563
+ };
564
+
565
+ template <typename T, typename U = void>
566
+ struct AfterCaller {
567
+ static void after(T&) {}
568
+ };
569
+
570
+ // decltype((void)&...) is either SFINAE, or void.
571
+ // So, if SFINAE does not happen for T, then this specialization exists
572
+ // for BeforeCaller<T, void>, and always applies. If not, only the
573
+ // default above exists, and that is used instead.
574
+ template <typename T>
575
+ struct BeforeCaller<T, decltype((void)&T::before)> {
576
+ static void before(T& t) {
577
+ t.before();
578
+ }
579
+ };
580
+
581
+ template <typename T>
582
+ struct AfterCaller<T, decltype((void)&T::after)> {
583
+ static void after(T& t) {
584
+ t.after();
585
+ }
586
+ };
587
+
588
+ // It's possible to use multiple decorators by nesting
589
+ // WithRuntimeDecorator<...>, but this specialization allows use of
590
+ // std::tuple of decorator classes instead. See testlib.cpp for an
591
+ // example.
592
+ template <typename... T>
593
+ struct BeforeCaller<std::tuple<T...>> {
594
+ static void before(std::tuple<T...>& tuple) {
595
+ all_before<0, T...>(tuple);
596
+ }
597
+
598
+ private:
599
+ template <size_t N, typename U, typename... Rest>
600
+ static void all_before(std::tuple<T...>& tuple) {
601
+ detail::BeforeCaller<U>::before(std::get<N>(tuple));
602
+ all_before<N + 1, Rest...>(tuple);
603
+ }
604
+
605
+ template <size_t N>
606
+ static void all_before(std::tuple<T...>&) {}
607
+ };
608
+
609
+ template <typename... T>
610
+ struct AfterCaller<std::tuple<T...>> {
611
+ static void after(std::tuple<T...>& tuple) {
612
+ all_after<0, T...>(tuple);
613
+ }
614
+
615
+ private:
616
+ template <size_t N, typename U, typename... Rest>
617
+ static void all_after(std::tuple<T...>& tuple) {
618
+ all_after<N + 1, Rest...>(tuple);
619
+ detail::AfterCaller<U>::after(std::get<N>(tuple));
620
+ }
621
+
622
+ template <size_t N>
623
+ static void all_after(std::tuple<T...>&) {}
624
+ };
625
+
626
+ } // namespace detail
627
+
628
+ // A decorator which implements an around idiom. A With instance is
629
+ // RAII constructed before each call to the undecorated class; the
630
+ // ctor is passed a single argument of type WithArg&. Plain and Base
631
+ // are used as in the base class.
632
+ template <typename With, typename Plain = Runtime, typename Base = Runtime>
633
+ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
634
+ public:
635
+ using RD = RuntimeDecorator<Plain, Base>;
636
+
637
+ // The reference arguments to the ctor are stored, but not used by
638
+ // the ctor, and there is no ctor, so they can be passed members of
639
+ // the derived class.
640
+ WithRuntimeDecorator(Plain& plain, With& with) : RD(plain), with_(with) {}
641
+
642
+ #if JSI_VERSION >= 20
643
+ ICast* castInterface(const UUID& interfaceUUID) override {
644
+ Around around{with_};
645
+ return RD::castInterface(interfaceUUID);
646
+ }
647
+ #endif
648
+
649
+ Value evaluateJavaScript(
650
+ const std::shared_ptr<const Buffer>& buffer,
651
+ const std::string& sourceURL) override {
652
+ Around around{with_};
653
+ return RD::evaluateJavaScript(buffer, sourceURL);
654
+ }
655
+ std::shared_ptr<const PreparedJavaScript> prepareJavaScript(
656
+ const std::shared_ptr<const Buffer>& buffer,
657
+ std::string sourceURL) override {
658
+ Around around{with_};
659
+ return RD::prepareJavaScript(buffer, std::move(sourceURL));
660
+ }
661
+ Value evaluatePreparedJavaScript(
662
+ const std::shared_ptr<const PreparedJavaScript>& js) override {
663
+ Around around{with_};
664
+ return RD::evaluatePreparedJavaScript(js);
665
+ }
666
+ #if JSI_VERSION >= 12
667
+ void queueMicrotask(const Function& callback) override {
668
+ Around around{with_};
669
+ RD::queueMicrotask(callback);
670
+ }
671
+ #endif
672
+ #if JSI_VERSION >= 4
673
+ bool drainMicrotasks(int maxMicrotasksHint) override {
674
+ Around around{with_};
675
+ return RD::drainMicrotasks(maxMicrotasksHint);
676
+ }
677
+ #endif
678
+ Object global() override {
679
+ Around around{with_};
680
+ return RD::global();
681
+ }
682
+ std::string description() override {
683
+ Around around{with_};
684
+ return RD::description();
685
+ };
686
+ bool isInspectable() override {
687
+ Around around{with_};
688
+ return RD::isInspectable();
689
+ };
690
+
691
+ // The jsi:: prefix is necessary because MSVC compiler complains C2247:
692
+ // Instrumentation is not accessible because RuntimeDecorator uses private
693
+ // to inherit from Instrumentation.
694
+ // TODO(T40821815) Consider removing this workaround when updating MSVC
695
+ jsi::Instrumentation& instrumentation() override {
696
+ Around around{with_};
697
+ return RD::instrumentation();
698
+ }
699
+
700
+ protected:
701
+ Runtime::PointerValue* cloneSymbol(const Runtime::PointerValue* pv) override {
702
+ Around around{with_};
703
+ return RD::cloneSymbol(pv);
704
+ };
705
+ #if JSI_VERSION >= 6
706
+ Runtime::PointerValue* cloneBigInt(const Runtime::PointerValue* pv) override {
707
+ Around around{with_};
708
+ return RD::cloneBigInt(pv);
709
+ };
710
+ #endif
711
+ Runtime::PointerValue* cloneString(const Runtime::PointerValue* pv) override {
712
+ Around around{with_};
713
+ return RD::cloneString(pv);
714
+ };
715
+ Runtime::PointerValue* cloneObject(const Runtime::PointerValue* pv) override {
716
+ Around around{with_};
717
+ return RD::cloneObject(pv);
718
+ };
719
+ Runtime::PointerValue* clonePropNameID(
720
+ const Runtime::PointerValue* pv) override {
721
+ Around around{with_};
722
+ return RD::clonePropNameID(pv);
723
+ };
724
+
725
+ PropNameID createPropNameIDFromAscii(const char* str, size_t length)
726
+ override {
727
+ Around around{with_};
728
+ return RD::createPropNameIDFromAscii(str, length);
729
+ };
730
+ PropNameID createPropNameIDFromUtf8(const uint8_t* utf8, size_t length)
731
+ override {
732
+ Around around{with_};
733
+ return RD::createPropNameIDFromUtf8(utf8, length);
734
+ };
735
+ #if JSI_VERSION >= 19
736
+ PropNameID createPropNameIDFromUtf16(const char16_t* utf16, size_t length)
737
+ override {
738
+ Around around{with_};
739
+ return RD::createPropNameIDFromUtf16(utf16, length);
740
+ }
741
+ #endif
742
+ PropNameID createPropNameIDFromString(const String& str) override {
743
+ Around around{with_};
744
+ return RD::createPropNameIDFromString(str);
745
+ };
746
+ #if JSI_VERSION >= 5
747
+ PropNameID createPropNameIDFromSymbol(const Symbol& sym) override {
748
+ Around around{with_};
749
+ return RD::createPropNameIDFromSymbol(sym);
750
+ };
751
+ #endif
752
+ std::string utf8(const PropNameID& id) override {
753
+ Around around{with_};
754
+ return RD::utf8(id);
755
+ };
756
+ bool compare(const PropNameID& a, const PropNameID& b) override {
757
+ Around around{with_};
758
+ return RD::compare(a, b);
759
+ };
760
+
761
+ std::string symbolToString(const Symbol& sym) override {
762
+ Around around{with_};
763
+ return RD::symbolToString(sym);
764
+ };
765
+
766
+ #if JSI_VERSION >= 8
767
+ BigInt createBigIntFromInt64(int64_t i) override {
768
+ Around around{with_};
769
+ return RD::createBigIntFromInt64(i);
770
+ };
771
+ BigInt createBigIntFromUint64(uint64_t i) override {
772
+ Around around{with_};
773
+ return RD::createBigIntFromUint64(i);
774
+ };
775
+ bool bigintIsInt64(const BigInt& bi) override {
776
+ Around around{with_};
777
+ return RD::bigintIsInt64(bi);
778
+ };
779
+ bool bigintIsUint64(const BigInt& bi) override {
780
+ Around around{with_};
781
+ return RD::bigintIsUint64(bi);
782
+ };
783
+ uint64_t truncate(const BigInt& bi) override {
784
+ Around around{with_};
785
+ return RD::truncate(bi);
786
+ };
787
+ String bigintToString(const BigInt& bi, int i) override {
788
+ Around around{with_};
789
+ return RD::bigintToString(bi, i);
790
+ };
791
+ #endif
792
+
793
+ String createStringFromAscii(const char* str, size_t length) override {
794
+ Around around{with_};
795
+ return RD::createStringFromAscii(str, length);
796
+ };
797
+ String createStringFromUtf8(const uint8_t* utf8, size_t length) override {
798
+ Around around{with_};
799
+ return RD::createStringFromUtf8(utf8, length);
800
+ };
801
+ #if JSI_VERSION >= 19
802
+ String createStringFromUtf16(const char16_t* utf16, size_t length) override {
803
+ Around around{with_};
804
+ return RD::createStringFromUtf16(utf16, length);
805
+ }
806
+ #endif
807
+ std::string utf8(const String& s) override {
808
+ Around around{with_};
809
+ return RD::utf8(s);
810
+ }
811
+
812
+ #if JSI_VERSION >= 14
813
+ std::u16string utf16(const String& str) override {
814
+ Around around{with_};
815
+ return RD::utf16(str);
816
+ }
817
+ std::u16string utf16(const PropNameID& sym) override {
818
+ Around around{with_};
819
+ return RD::utf16(sym);
820
+ }
821
+ #endif
822
+
823
+ #if JSI_VERSION >= 16
824
+ void getStringData(
825
+ const jsi::String& str,
826
+ void* ctx,
827
+ void (
828
+ *cb)(void* ctx, bool ascii, const void* data, size_t num)) override {
829
+ Around around{with_};
830
+ RD::getStringData(str, ctx, cb);
831
+ }
832
+
833
+ void getPropNameIdData(
834
+ const jsi::PropNameID& sym,
835
+ void* ctx,
836
+ void (
837
+ *cb)(void* ctx, bool ascii, const void* data, size_t num)) override {
838
+ Around around{with_};
839
+ RD::getPropNameIdData(sym, ctx, cb);
840
+ }
841
+ #endif
842
+
843
+ Value createValueFromJsonUtf8(const uint8_t* json, size_t length) override {
844
+ Around around{with_};
845
+ return RD::createValueFromJsonUtf8(json, length);
846
+ };
847
+
848
+ #if JSI_VERSION >= 18
849
+ Object createObjectWithPrototype(const Value& prototype) override {
850
+ Around around{with_};
851
+ return RD::createObjectWithPrototype(prototype);
852
+ }
853
+ #endif
854
+
855
+ Object createObject() override {
856
+ Around around{with_};
857
+ return RD::createObject();
858
+ };
859
+ Object createObject(std::shared_ptr<HostObject> ho) override {
860
+ Around around{with_};
861
+ return RD::createObject(std::move(ho));
862
+ };
863
+ std::shared_ptr<HostObject> getHostObject(const jsi::Object& o) override {
864
+ Around around{with_};
865
+ return RD::getHostObject(o);
866
+ };
867
+ HostFunctionType& getHostFunction(const jsi::Function& f) override {
868
+ Around around{with_};
869
+ return RD::getHostFunction(f);
870
+ };
871
+
872
+ #if JSI_VERSION >= 7
873
+ bool hasNativeState(const Object& o) override {
874
+ Around around{with_};
875
+ return RD::hasNativeState(o);
876
+ };
877
+ std::shared_ptr<NativeState> getNativeState(const Object& o) override {
878
+ Around around{with_};
879
+ return RD::getNativeState(o);
880
+ };
881
+ void setNativeState(const Object& o, std::shared_ptr<NativeState> state)
882
+ override {
883
+ Around around{with_};
884
+ RD::setNativeState(o, state);
885
+ };
886
+ #endif
887
+
888
+ #if JSI_VERSION >= 17
889
+ void setPrototypeOf(const Object& object, const Value& prototype) override {
890
+ Around around{with_};
891
+ RD::setPrototypeOf(object, prototype);
892
+ }
893
+
894
+ Value getPrototypeOf(const Object& object) override {
895
+ Around around{with_};
896
+ return RD::getPrototypeOf(object);
897
+ }
898
+ #endif
899
+
900
+ Value getProperty(const Object& o, const PropNameID& name) override {
901
+ Around around{with_};
902
+ return RD::getProperty(o, name);
903
+ };
904
+ Value getProperty(const Object& o, const String& name) override {
905
+ Around around{with_};
906
+ return RD::getProperty(o, name);
907
+ };
908
+ bool hasProperty(const Object& o, const PropNameID& name) override {
909
+ Around around{with_};
910
+ return RD::hasProperty(o, name);
911
+ };
912
+ bool hasProperty(const Object& o, const String& name) override {
913
+ Around around{with_};
914
+ return RD::hasProperty(o, name);
915
+ };
916
+ void setPropertyValue(
917
+ JSI_CONST_10 Object& o,
918
+ const PropNameID& name,
919
+ const Value& value) override {
920
+ Around around{with_};
921
+ RD::setPropertyValue(o, name, value);
922
+ };
923
+ void setPropertyValue(
924
+ JSI_CONST_10 Object& o,
925
+ const String& name,
926
+ const Value& value) override {
927
+ Around around{with_};
928
+ RD::setPropertyValue(o, name, value);
929
+ };
930
+
931
+ bool isArray(const Object& o) const override {
932
+ Around around{with_};
933
+ return RD::isArray(o);
934
+ };
935
+ bool isArrayBuffer(const Object& o) const override {
936
+ Around around{with_};
937
+ return RD::isArrayBuffer(o);
938
+ };
939
+ bool isFunction(const Object& o) const override {
940
+ Around around{with_};
941
+ return RD::isFunction(o);
942
+ };
943
+ bool isHostObject(const jsi::Object& o) const override {
944
+ Around around{with_};
945
+ return RD::isHostObject(o);
946
+ };
947
+ bool isHostFunction(const jsi::Function& f) const override {
948
+ Around around{with_};
949
+ return RD::isHostFunction(f);
950
+ };
951
+ Array getPropertyNames(const Object& o) override {
952
+ Around around{with_};
953
+ return RD::getPropertyNames(o);
954
+ };
955
+
956
+ WeakObject createWeakObject(const Object& o) override {
957
+ Around around{with_};
958
+ return RD::createWeakObject(o);
959
+ };
960
+ Value lockWeakObject(JSI_NO_CONST_3 JSI_CONST_10 WeakObject& wo) override {
961
+ Around around{with_};
962
+ return RD::lockWeakObject(wo);
963
+ };
964
+
965
+ Array createArray(size_t length) override {
966
+ Around around{with_};
967
+ return RD::createArray(length);
968
+ };
969
+ #if JSI_VERSION >= 9
970
+ ArrayBuffer createArrayBuffer(
971
+ std::shared_ptr<MutableBuffer> buffer) override {
972
+ return RD::createArrayBuffer(std::move(buffer));
973
+ };
974
+ #endif
975
+ size_t size(const Array& a) override {
976
+ Around around{with_};
977
+ return RD::size(a);
978
+ };
979
+ size_t size(const ArrayBuffer& ab) override {
980
+ Around around{with_};
981
+ return RD::size(ab);
982
+ };
983
+ uint8_t* data(const ArrayBuffer& ab) override {
984
+ Around around{with_};
985
+ return RD::data(ab);
986
+ };
987
+ Value getValueAtIndex(const Array& a, size_t i) override {
988
+ Around around{with_};
989
+ return RD::getValueAtIndex(a, i);
990
+ };
991
+ void setValueAtIndexImpl(JSI_CONST_10 Array& a, size_t i, const Value& value)
992
+ override {
993
+ Around around{with_};
994
+ RD::setValueAtIndexImpl(a, i, value);
995
+ };
996
+
997
+ Function createFunctionFromHostFunction(
998
+ const PropNameID& name,
999
+ unsigned int paramCount,
1000
+ HostFunctionType func) override {
1001
+ Around around{with_};
1002
+ return RD::createFunctionFromHostFunction(
1003
+ name, paramCount, std::move(func));
1004
+ };
1005
+ Value call(
1006
+ const Function& f,
1007
+ const Value& jsThis,
1008
+ const Value* args,
1009
+ size_t count) override {
1010
+ Around around{with_};
1011
+ return RD::call(f, jsThis, args, count);
1012
+ };
1013
+ Value callAsConstructor(const Function& f, const Value* args, size_t count)
1014
+ override {
1015
+ Around around{with_};
1016
+ return RD::callAsConstructor(f, args, count);
1017
+ };
1018
+
1019
+ // Private data for managing scopes.
1020
+ Runtime::ScopeState* pushScope() override {
1021
+ Around around{with_};
1022
+ return RD::pushScope();
1023
+ }
1024
+ void popScope(Runtime::ScopeState* ss) override {
1025
+ Around around{with_};
1026
+ RD::popScope(ss);
1027
+ }
1028
+
1029
+ bool strictEquals(const Symbol& a, const Symbol& b) const override {
1030
+ Around around{with_};
1031
+ return RD::strictEquals(a, b);
1032
+ };
1033
+
1034
+ #if JSI_VERSION >= 6
1035
+ bool strictEquals(const BigInt& a, const BigInt& b) const override {
1036
+ Around around{with_};
1037
+ return RD::strictEquals(a, b);
1038
+ };
1039
+ #endif
1040
+
1041
+ bool strictEquals(const String& a, const String& b) const override {
1042
+ Around around{with_};
1043
+ return RD::strictEquals(a, b);
1044
+ };
1045
+ bool strictEquals(const Object& a, const Object& b) const override {
1046
+ Around around{with_};
1047
+ return RD::strictEquals(a, b);
1048
+ };
1049
+
1050
+ bool instanceOf(const Object& o, const Function& f) override {
1051
+ Around around{with_};
1052
+ return RD::instanceOf(o, f);
1053
+ };
1054
+
1055
+ #if JSI_VERSION >= 11
1056
+ void setExternalMemoryPressure(const jsi::Object& obj, size_t amount)
1057
+ override {
1058
+ Around around{with_};
1059
+ RD::setExternalMemoryPressure(obj, amount);
1060
+ };
1061
+ #endif
1062
+
1063
+ #if JSI_VERSION >= 20
1064
+ void setRuntimeDataImpl(
1065
+ const UUID& uuid,
1066
+ const void* data,
1067
+ void (*deleter)(const void* data)) override {
1068
+ Around around{with_};
1069
+ RD::setRuntimeDataImpl(uuid, data, deleter);
1070
+ }
1071
+
1072
+ const void* getRuntimeDataImpl(const UUID& uuid) override {
1073
+ Around around{with_};
1074
+ return RD::getRuntimeDataImpl(uuid);
1075
+ }
1076
+ #endif
1077
+
1078
+ private:
1079
+ // Wrap an RAII type around With& to guarantee after always happens.
1080
+ struct Around {
1081
+ Around(With& with) : with_(with) {
1082
+ detail::BeforeCaller<With>::before(with_);
1083
+ }
1084
+ ~Around() {
1085
+ detail::AfterCaller<With>::after(with_);
1086
+ }
1087
+
1088
+ With& with_;
1089
+ };
1090
+
1091
+ With& with_;
1092
+ };
1093
+
1094
+ } // namespace jsi
1095
+ } // namespace facebook