jsi-sys 0.3.0-alpha.5

Low-level interface to React Native JSI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// A wrapper header b/c a bunch of JSI functions return opaque C++ types
// directly and autocxx currently isn't able to handle these headers b/c of
// stuff like nested protected structs

#ifndef JSI_WRAPPER_H
#define JSI_WRAPPER_H
#pragma once

#include <ReactCommon/CallInvoker.h>

#include <jsi/jsi.h>
#include <rust/cxx.h>

namespace jsi_rs
{
namespace ffi
{
using Buffer = ::facebook::jsi::Buffer;
using StringBuffer = ::facebook::jsi::StringBuffer;
using PreparedJavaScript = ::facebook::jsi::PreparedJavaScript;
using Symbol = ::facebook::jsi::Symbol;
using String = ::facebook::jsi::String;
using Object = ::facebook::jsi::Object;
using WeakObject = ::facebook::jsi::WeakObject;
using Array = ::facebook::jsi::Array;
using ArrayBuffer = ::facebook::jsi::ArrayBuffer;
using Function = ::facebook::jsi::Function;
using Value = ::facebook::jsi::Value;
using Instrumentation = ::facebook::jsi::Instrumentation;
using Scope = ::facebook::jsi::Scope;
using JSIException = ::facebook::jsi::JSIException;
using JSError = ::facebook::jsi::JSError;
using HostObject = ::facebook::jsi::HostObject;
using Runtime = ::facebook::jsi::Runtime;
using Pointer = ::facebook::jsi::Pointer;
using PropNameID = ::facebook::jsi::PropNameID;

using ConstPreparedJavaScript = const PreparedJavaScript;

// std::ffi::c_void is not supported by CXX
using c_void = void;

// HostObject

::std::unique_ptr<Value> HostObject_get(
    ::facebook::jsi::HostObject &self, Runtime &rt, const PropNameID &name
) noexcept
{
  Value (::facebook::jsi::HostObject::*data$)(
      facebook::jsi::Runtime &, const facebook::jsi::PropNameID &name
  ) = &::facebook::jsi::HostObject::get;
  auto value = (self.*data$)(rt, name);
  return ::std::make_unique<Value>(std::move(value));
}

::std::unique_ptr<std::vector<PropNameID>> HostObject_getPropertyNames(
    ::facebook::jsi::HostObject &self, Runtime &rt
) noexcept
{
  std::vector<PropNameID> (::facebook::jsi::HostObject::*
                               data$)(facebook::jsi::Runtime &) =
      &::facebook::jsi::HostObject::getPropertyNames;
  auto value = (self.*data$)(rt);
  return ::std::unique_ptr<std::vector<PropNameID>>(&value);
}

// Runtime

::std::shared_ptr<ConstPreparedJavaScript>
PreparedJavaScript_asConst(const ::std::shared_ptr<PreparedJavaScript> &js
) noexcept
{
  return ::std::shared_ptr<ConstPreparedJavaScript>(js);
}

::std::unique_ptr<Value> Runtime_evaluateJavaScript(
    ::facebook::jsi::Runtime &self, const std::shared_ptr<Buffer> &buffer,
    rust::Str sourceURL
) noexcept
{
  Value (::facebook::jsi::Runtime::*data$)(
      const std::shared_ptr<const Buffer> &buffer, const std::string &sourceURL
  ) = &::facebook::jsi::Runtime::evaluateJavaScript;
  auto value = (self.*data$)(buffer, std::string(sourceURL));
  return ::std::make_unique<Value>(std::move(value));
}

::std::shared_ptr<ConstPreparedJavaScript> Runtime_prepareJavaScript(
    ::facebook::jsi::Runtime &self, const std::shared_ptr<Buffer> &buffer,
    rust::Str sourceURL
) noexcept
{
  std::shared_ptr<ConstPreparedJavaScript> (::facebook::jsi::Runtime::*data$)(
      const std::shared_ptr<const Buffer> &buffer, const std::string sourceURL
  ) = &::facebook::jsi::Runtime::prepareJavaScript;
  return (self.*data$)(buffer, std::string(sourceURL));
}

::std::unique_ptr<Value> Runtime_evaluatePreparedJavaScript(
    ::facebook::jsi::Runtime &self,
    const ::std::shared_ptr<ConstPreparedJavaScript> &js
) noexcept
{
  Value (::facebook::jsi::Runtime::*
             data$)(const ::std::shared_ptr<ConstPreparedJavaScript> &) =
      &::facebook::jsi::Runtime::evaluatePreparedJavaScript;
  auto value = (self.*data$)(::std::shared_ptr<ConstPreparedJavaScript>(js));
  return ::std::make_unique<Value>(std::move(value));
}

::std::unique_ptr<Object> Runtime_global(::facebook::jsi::Runtime &self
) noexcept
{
  Object (::facebook::jsi::Runtime::*fp)() = &::facebook::jsi::Runtime::global;
  auto value = (self.*fp)();
  return ::std::make_unique<Object>(std::move(value));
}

::std::unique_ptr<std::string>
Runtime_description(::facebook::jsi::Runtime &self) noexcept
{
  std::string (::facebook::jsi::Runtime::*fp)() =
      &::facebook::jsi::Runtime::description;
  auto value = (self.*fp)();
  return ::std::make_unique<std::string>(std::move(value));
}

// PropNameID

::std::unique_ptr<PropNameID>
PropNameID_forUtf8(Runtime &rt, rust::Str str) noexcept
{
  auto value =
      PropNameID::forUtf8(rt, (const uint8_t *)str.data(), str.length());
  return std::make_unique<PropNameID>(std::move(value));
}

::std::unique_ptr<PropNameID>
PropNameID_forString(Runtime &rt, const String &str) noexcept
{
  auto value = PropNameID::forString(rt, str);
  return std::make_unique<PropNameID>(std::move(value));
}

::std::unique_ptr<std::string>
PropNameID_toUtf8(const PropNameID &self, Runtime &rt) noexcept
{
  std::string (::facebook::jsi::PropNameID::*fp)(Runtime &rt) const =
      &::facebook::jsi::PropNameID::utf8;
  auto value = (self.*fp)(rt);
  return std::make_unique<std::string>(std::move(value));
}

bool PropNameID_compare(
    Runtime &rt, const PropNameID &self, const PropNameID &other
) noexcept
{
  return PropNameID::compare(rt, self, other);
}

std::unique_ptr<PropNameID> PropNameID_copy(const PropNameID &self, Runtime &rt)
{
  auto val = PropNameID(rt, self);
  return std::make_unique<PropNameID>(std::move(val));
}

// Symbol

::std::unique_ptr<std::string>
Symbol_toString(const Symbol &self, Runtime &rt) noexcept
{
  std::string (::facebook::jsi::Symbol::*fp)(Runtime &rt) const =
      &::facebook::jsi::Symbol::toString;
  auto value = (self.*fp)(rt);
  return std::make_unique<std::string>(std::move(value));
}

bool Symbol_compare(
    Runtime &rt, const Symbol &self, const Symbol &other
) noexcept
{
  return Symbol::strictEquals(rt, self, other);
}

// String

::std::unique_ptr<String> String_fromUtf8(Runtime &rt, rust::Str str) noexcept
{
  auto value =
      String::createFromUtf8(rt, (const uint8_t *)str.data(), str.length());
  return std::make_unique<String>(std::move(value));
}

::std::unique_ptr<std::string>
String_toString(const String &self, Runtime &rt) noexcept
{
  std::string (::facebook::jsi::String::*fp)(Runtime &rt) const =
      &::facebook::jsi::String::utf8;
  auto value = (self.*fp)(rt);
  return std::make_unique<std::string>(std::move(value));
}

bool String_compare(
    Runtime &rt, const String &self, const String &other
) noexcept
{
  return String::strictEquals(rt, self, other);
}

// Object

::std::unique_ptr<Object> Object_create(Runtime &rt) noexcept
{
  return std::make_unique<Object>(rt);
}

::std::unique_ptr<Object> Object_createFromHostObjectShared(
    Runtime &rt, std::shared_ptr<HostObject> ho
) noexcept
{
  auto value = facebook::jsi::Object::createFromHostObject(rt, ho);
  return std::make_unique<Object>(std::move(value));
}

::std::unique_ptr<Object> Object_createFromHostObjectUnique(
    Runtime &rt, std::unique_ptr<HostObject> ho
) noexcept
{
  std::shared_ptr<HostObject> s_ho = std::move(ho);
  auto value = facebook::jsi::Object::createFromHostObject(rt, s_ho);
  return std::make_unique<Object>(std::move(value));
}

bool Object_compare(
    Runtime &rt, const Object &self, const Object &other
) noexcept
{
  return Object::strictEquals(rt, self, other);
}

std::unique_ptr<Value> Object_getProperty(
    const Object &self, Runtime &rt, const PropNameID &name
) noexcept
{
  Value (::facebook::jsi::Object::*fp)(Runtime &, const PropNameID &) const =
      &::facebook::jsi::Object::getProperty;
  auto value = (self.*fp)(rt, name);
  return std::make_unique<Value>(std::move(value));
}

void Object_setProperty(
    Object &self, Runtime &rt, const PropNameID &name, Value const &value
) noexcept
{
  void (::facebook::jsi::Object::*
            fp)(Runtime &, const PropNameID &, Value const &) const =
      &::facebook::jsi::Object::setProperty;
  (self.*fp)(rt, name, value);
}

std::unique_ptr<Array> Object_asArray(Object const &self, Runtime &rt)
{
  Array (::facebook::jsi::Object::*fp)(Runtime &) const & =
      &::facebook::jsi::Object::asArray;
  auto val = (self.*fp)(rt);
  return std::make_unique<Array>(std::move(val));
}

std::unique_ptr<ArrayBuffer>
Object_asArrayBuffer(Object const &self, Runtime &rt)
{
  ArrayBuffer (::facebook::jsi::Object::*fp)(Runtime &) const & =
      &::facebook::jsi::Object::getArrayBuffer;
  auto val = (self.*fp)(rt);
  return std::make_unique<ArrayBuffer>(std::move(val));
}

std::unique_ptr<Function> Object_asFunction(Object const &self, Runtime &rt)
{
  Function (::facebook::jsi::Object::*fp)(Runtime &) const & =
      &::facebook::jsi::Object::asFunction;
  auto val = (self.*fp)(rt);
  return std::make_unique<Function>(std::move(val));
}

std::shared_ptr<HostObject> Object_asHostObject(Object const &self, Runtime &rt)
{
  std::shared_ptr<HostObject> (::facebook::jsi::Object::*fp)(Runtime &) const =
      &::facebook::jsi::Object::asHostObject;
  return (self.*fp)(rt);
}

std::unique_ptr<Array> Object_getPropertyNames(Object &self, Runtime &rt)
{
  Array (::facebook::jsi::Object::*fp)(Runtime &) const =
      &::facebook::jsi::Object::getPropertyNames;
  auto val = (self.*fp)(rt);
  return std::make_unique<Array>(std::move(val));
}

// WeakObject

std::unique_ptr<WeakObject>
WeakObject_fromObject(Runtime &rt, Object const &object)
{
  return std::make_unique<WeakObject>(rt, object);
}

std::unique_ptr<Value> WeakObject_lock(WeakObject &self, Runtime &rt)
{
  Value (::facebook::jsi::WeakObject::*fp)(Runtime &) const =
      &::facebook::jsi::WeakObject::lock;
  auto val = (self.*fp)(rt);
  return std::make_unique<Value>(std::move(val));
}

// Array

std::unique_ptr<Array> Array_createWithLength(Runtime &rt, size_t length)
{
  return std::make_unique<Array>(rt, length);
}

std::unique_ptr<Value> Array_get(Array const &self, Runtime &rt, size_t index)
{
  Value (::facebook::jsi::Array::*fp)(Runtime &, size_t) const =
      &::facebook::jsi::Array::getValueAtIndex;
  auto val = (self.*fp)(rt, index);
  return std::make_unique<Value>(std::move(val));
}

void Array_set(Array &self, Runtime &rt, size_t index, Value const &value)
{
  void (::facebook::jsi::Array::*fp)(Runtime &, size_t, Value const &) const =
      &::facebook::jsi::Array::setValueAtIndex;
  (self.*fp)(rt, index, &value);
}

// Function

std::unique_ptr<Value>
Function_call(Function const &self, Runtime &rt, std::vector<Value> const &args)
{
  Value (::facebook::jsi::Function::*fp)(Runtime &, Value const *, size_t)
      const = &::facebook::jsi::Function::call;
  auto value = (self.*fp)(rt, args.data(), args.size());
  return std::make_unique<Value>(std::move(value));
}

std::unique_ptr<Value> Function_callWithThis(
    Function const &self, Runtime &rt, Object const &thisObj,
    std::vector<Value> const &args
)
{
  Value (::facebook::jsi::Function::*fp)(
      Runtime &, Object const &, Value const *, size_t
  ) const = &::facebook::jsi::Function::callWithThis;
  auto value = (self.*fp)(rt, thisObj, args.data(), args.size());
  return std::make_unique<Value>(std::move(value));
}

std::unique_ptr<Value> Function_callAsConstructor(
    Function const &self, Runtime &rt, std::vector<Value> const &args
)
{
  Value (::facebook::jsi::Function::*fp)(Runtime &, Value const *, size_t)
      const = &::facebook::jsi::Function::callAsConstructor;
  auto value = (self.*fp)(rt, args.data(), args.size());
  return std::make_unique<Value>(std::move(value));
}

::std::unique_ptr<::facebook::jsi::Value> host_fn_trampoline(
    ::facebook::jsi::Runtime &rt, const ::facebook::jsi::Value &thisVal,
    const ::facebook::jsi::Value *args, ::std::uint32_t count,
    ::std::size_t stride, ::jsi_rs::ffi::c_void *closure
);

std::unique_ptr<Function> Function_createFromHostFunction(
    Runtime &rt, const PropNameID &name, unsigned int paramCount, void *closure
)
{
  auto value = Function::createFromHostFunction(
      rt, name, paramCount,
      [closure](
          Runtime &rt, const Value &thisVal, const Value *args, size_t count
      ) {
        auto stride = sizeof(Value);
        auto val =
            host_fn_trampoline(rt, thisVal, args, count, stride, closure);
        return std::move(*val.release());
      }
  );
  return std::make_unique<Function>(std::move(value));
}

// Value

std::unique_ptr<Value> Value_fromUndefined()
{
  return std::make_unique<Value>();
}

std::unique_ptr<Value> Value_fromNull()
{
  return std::make_unique<Value>(std::nullptr_t());
}

std::unique_ptr<Value> Value_fromInt(int i)
{
  return std::make_unique<Value>(i);
}

std::unique_ptr<Value> Value_fromBool(bool b)
{
  return std::make_unique<Value>(b);
}

std::unique_ptr<Value> Value_fromDouble(double d)
{
  return std::make_unique<Value>(d);
}

std::unique_ptr<Value> Value_fromString(Runtime &rt, std::unique_ptr<String> s)
{
  return std::make_unique<Value>(rt, std::move(*s.release()));
}

std::unique_ptr<Value> Value_fromSymbol(Runtime &rt, std::unique_ptr<Symbol> s)
{
  return std::make_unique<Value>(rt, std::move(*s.release()));
}

std::unique_ptr<Value> Value_fromObject(Runtime &rt, std::unique_ptr<Object> o)
{
  return std::make_unique<Value>(rt, std::move(*o.release()));
}

std::unique_ptr<Value> Value_copyFromString(Runtime &rt, const String &s)
{
  return std::make_unique<Value>(rt, s);
}

std::unique_ptr<Value> Value_copyFromSymbol(Runtime &rt, const Symbol &s)
{
  return std::make_unique<Value>(rt, s);
}

std::unique_ptr<Value> Value_copyFromObject(Runtime &rt, const Object &o)
{
  return std::make_unique<Value>(rt, o);
}

std::unique_ptr<Value> Value_fromJson(Runtime &rt, rust::Str s)
{
  auto val =
      Value::createFromJsonUtf8(rt, (const uint8_t *)s.data(), s.length());
  return std::make_unique<Value>(std::move(val));
}

bool Value_compare(Runtime &rt, const Value &self, const Value &other) noexcept
{
  return Value::strictEquals(rt, self, other);
}

std::unique_ptr<String> Value_asString(const Value &self, Runtime &rt)
{
  String (::facebook::jsi::Value::*fp)(Runtime &) const & =
      &::facebook::jsi::Value::asString;
  auto val = (self.*fp)(rt);
  return std::make_unique<String>(std::move(val));
}

std::unique_ptr<Symbol> Value_asSymbol(const Value &self, Runtime &rt)
{
  Symbol (::facebook::jsi::Value::*fp)(Runtime &) const & =
      &::facebook::jsi::Value::asSymbol;
  auto val = (self.*fp)(rt);
  return std::make_unique<Symbol>(std::move(val));
}

std::unique_ptr<Object> Value_asObject(const Value &self, Runtime &rt)
{
  Object (::facebook::jsi::Value::*fp)(Runtime &) const & =
      &::facebook::jsi::Value::asObject;
  auto val = (self.*fp)(rt);
  return std::make_unique<Object>(std::move(val));
}

std::unique_ptr<String> Value_toString(const Value &self, Runtime &rt)
{
  String (::facebook::jsi::Value::*fp)(Runtime &) const =
      &::facebook::jsi::Value::toString;
  auto val = (self.*fp)(rt);
  return std::make_unique<String>(std::move(val));
}

std::unique_ptr<Value> Value_copy(const Value &self, Runtime &rt)
{
  auto val = Value(rt, self);
  return std::make_unique<Value>(std::move(val));
}

// CallInvoker

void call_invoker_trampoline(void *closure);

void CallInvoker_invokeSync(
    std::shared_ptr<facebook::react::CallInvoker> ci, void *closure
)
{
  ci->invokeSync([closure]() { call_invoker_trampoline(closure); });
}

void CallInvoker_invokeAsync(
    std::shared_ptr<facebook::react::CallInvoker> ci, void *closure
)
{
  ci->invokeAsync([closure]() { call_invoker_trampoline(closure); });
}

// Utility fns

std::unique_ptr<std::vector<Value>> create_value_vector()
{
  return std::make_unique<std::vector<Value>>();
}

void push_value_vector(std::vector<Value> &values, std::unique_ptr<Value> value)
{
  auto x = std::move(*value.release());
  values.push_back(std::move(x));
}

std::unique_ptr<std::vector<PropNameID>> create_prop_name_vector()
{
  return std::make_unique<std::vector<PropNameID>>();
}

void push_prop_name_vector(
    std::vector<PropNameID> &values, std::unique_ptr<PropNameID> value
)
{
  auto x = std::move(*value.release());
  values.push_back(std::move(x));
}

std::unique_ptr<PropNameID> pop_prop_name_vector(std::vector<PropNameID> &values
)
{
  if (values.size() > 0) {
    auto prop = std::move(values.back());
    values.pop_back();
    return std::make_unique<PropNameID>(std::move(prop));
  } else {
    return std::unique_ptr<PropNameID>(nullptr);
  }
}
} // namespace ffi
} // namespace jsi_rs

#endif