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
#[cxx::bridge]
pub mod ffi {
    #[namespace = "facebook::jsi"]
    unsafe extern "C++" {
        include!("jsi/jsi.h");
        include!("wrapper.h");

        pub type Buffer;

        fn size(self: &Buffer) -> usize;
        unsafe fn data(self: &Buffer) -> *const u8;

        pub type StringBuffer;

        pub type PreparedJavaScript;

        #[namespace = "jsi_rs::ffi"]
        pub type ConstPreparedJavaScript;

        #[namespace = "jsi_rs::ffi"]
        fn PreparedJavaScript_asConst(
            js: &SharedPtr<PreparedJavaScript>,
        ) -> SharedPtr<ConstPreparedJavaScript>;

        pub type Instrumentation;
        pub type Scope;
        pub type JSIException;
        pub type JSError;

        pub type Runtime;

        #[namespace = "jsi_rs::ffi"]
        pub fn Runtime_evaluateJavaScript(
            _self: Pin<&mut Runtime>,
            buffer: &SharedPtr<Buffer>,
            source_url: &str,
        ) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Runtime_prepareJavaScript(
            _self: Pin<&mut Runtime>,
            buffer: &SharedPtr<Buffer>,
            source_url: &str,
        ) -> SharedPtr<ConstPreparedJavaScript>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Runtime_evaluatePreparedJavaScript(
            _self: Pin<&mut Runtime>,
            js: &SharedPtr<ConstPreparedJavaScript>,
        ) -> UniquePtr<JsiValue>;
        #[cxx_name = "drainMicrotasks"]
        pub fn drain_microtasks(self: Pin<&mut Runtime>, max_microtasks_hint: i32) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn Runtime_global(_self: Pin<&mut Runtime>) -> UniquePtr<JsiObject>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Runtime_description(_self: Pin<&mut Runtime>) -> UniquePtr<CxxString>;
        #[cxx_name = "isInspectable"]
        pub fn is_inspectable(self: Pin<&mut Runtime>) -> bool;
        pub fn instrumentation(self: Pin<&mut Runtime>) -> Pin<&mut Instrumentation>;

        pub type HostObject;

        #[namespace = "jsi_rs::ffi"]
        pub fn HostObject_get(
            _self: Pin<&mut HostObject>,
            rt: Pin<&mut Runtime>,
            name: &PropNameID,
        ) -> UniquePtr<JsiValue>;
        pub fn set(
            self: Pin<&mut HostObject>,
            rt: Pin<&mut Runtime>,
            name: &PropNameID,
            value: &JsiValue,
        );
        #[namespace = "jsi_rs::ffi"]
        pub fn HostObject_getPropertyNames(
            _self: Pin<&mut HostObject>,
            rt: Pin<&mut Runtime>,
        ) -> UniquePtr<CxxVector<PropNameID>>;

        pub type Pointer;

        pub type PropNameID;

        #[namespace = "jsi_rs::ffi"]
        pub fn PropNameID_forUtf8(rt: Pin<&mut Runtime>, str: &str) -> UniquePtr<PropNameID>;
        #[namespace = "jsi_rs::ffi"]
        pub fn PropNameID_forString(
            rt: Pin<&mut Runtime>,
            str: &JsiString,
        ) -> UniquePtr<PropNameID>;
        #[namespace = "jsi_rs::ffi"]
        pub fn PropNameID_toUtf8(_self: &PropNameID, rt: Pin<&mut Runtime>)
            -> UniquePtr<CxxString>;
        #[namespace = "jsi_rs::ffi"]
        pub fn PropNameID_compare(
            rt: Pin<&mut Runtime>,
            lhs: &PropNameID,
            rhs: &PropNameID,
        ) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn PropNameID_copy(_self: &PropNameID, rt: Pin<&mut Runtime>) -> UniquePtr<PropNameID>;

        #[cxx_name = "Symbol"]
        pub type JsiSymbol;
        #[namespace = "jsi_rs::ffi"]
        pub fn Symbol_compare(rt: Pin<&mut Runtime>, lhs: &JsiSymbol, rhs: &JsiSymbol) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn Symbol_toString(_self: &JsiSymbol, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString>;

        #[cxx_name = "String"]
        pub type JsiString;
        #[namespace = "jsi_rs::ffi"]
        pub fn String_fromUtf8(rt: Pin<&mut Runtime>, str: &str) -> UniquePtr<JsiString>;
        #[namespace = "jsi_rs::ffi"]
        pub fn String_compare(rt: Pin<&mut Runtime>, lhs: &JsiString, rhs: &JsiString) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn String_toString(_self: &JsiString, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString>;

        #[cxx_name = "Object"]
        pub type JsiObject;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_create(rt: Pin<&mut Runtime>) -> UniquePtr<JsiObject>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_createFromHostObjectShared(
            rt: Pin<&mut Runtime>,
            ho: SharedPtr<HostObject>,
        ) -> UniquePtr<JsiObject>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_createFromHostObjectUnique(
            rt: Pin<&mut Runtime>,
            ho: UniquePtr<HostObject>,
        ) -> UniquePtr<JsiObject>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_compare(rt: Pin<&mut Runtime>, lhs: &JsiObject, rhs: &JsiObject) -> bool;
        #[cxx_name = "instanceOf"]
        pub fn instance_of(self: &JsiObject, rt: Pin<&mut Runtime>, ctor: &JsiFunction) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_getProperty(
            _self: &JsiObject,
            rt: Pin<&mut Runtime>,
            prop: &PropNameID,
        ) -> UniquePtr<JsiValue>;
        #[cxx_name = "hasProperty"]
        pub fn has_property(self: &JsiObject, rt: Pin<&mut Runtime>, prop: &PropNameID) -> bool;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_setProperty(
            _self: Pin<&mut JsiObject>,
            rt: Pin<&mut Runtime>,
            prop: &PropNameID,
            value: &JsiValue,
        );
        #[cxx_name = "isArray"]
        pub fn is_array(self: &JsiObject, rt: Pin<&mut Runtime>) -> bool;
        #[cxx_name = "isArrayBuffer"]
        pub fn is_array_buffer(self: &JsiObject, rt: Pin<&mut Runtime>) -> bool;
        #[cxx_name = "isFunction"]
        pub fn is_function(self: &JsiObject, rt: Pin<&mut Runtime>) -> bool;
        // TODO: isHostObject after implementing Rust HostObject subclass
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_asArray(
            _self: &JsiObject,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiArray>>;
        // NOTICE: this method will assert if the object is not an array buffer;
        // i'm not sure if that's the same as an exception or whether it will
        // lead to UB
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_asArrayBuffer(
            _self: &JsiObject,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiArrayBuffer>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_asFunction(
            _self: &JsiObject,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiFunction>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_asHostObject(
            _self: &JsiObject,
            rt: Pin<&mut Runtime>,
        ) -> Result<SharedPtr<HostObject>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Object_getPropertyNames(
            _self: Pin<&mut JsiObject>,
            rt: Pin<&mut Runtime>,
        ) -> UniquePtr<JsiArray>;

        #[cxx_name = "WeakObject"]
        pub type JsiWeakObject;
        #[namespace = "jsi_rs::ffi"]
        pub fn WeakObject_fromObject(
            rt: Pin<&mut Runtime>,
            object: &JsiObject,
        ) -> UniquePtr<JsiWeakObject>;
        #[namespace = "jsi_rs::ffi"]
        pub fn WeakObject_lock(
            _self: Pin<&mut JsiWeakObject>,
            rt: Pin<&mut Runtime>,
        ) -> UniquePtr<JsiValue>;

        #[cxx_name = "Array"]
        pub type JsiArray;
        #[namespace = "jsi_rs::ffi"]
        pub fn Array_createWithLength(rt: Pin<&mut Runtime>, length: usize) -> UniquePtr<JsiArray>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Array_get(
            _self: &JsiArray,
            rt: Pin<&mut Runtime>,
            index: usize,
        ) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Array_set(
            _self: Pin<&mut JsiArray>,
            rt: Pin<&mut Runtime>,
            index: usize,
            value: &JsiValue,
        );
        pub fn length(self: &JsiArray, rt: Pin<&mut Runtime>) -> usize;

        #[cxx_name = "ArrayBuffer"]
        pub type JsiArrayBuffer;
        pub unsafe fn data(self: &JsiArrayBuffer, rt: Pin<&mut Runtime>) -> *mut u8;
        pub fn length(self: &JsiArrayBuffer, rt: Pin<&mut Runtime>) -> usize;

        #[cxx_name = "Function"]
        pub type JsiFunction;
        #[namespace = "jsi_rs::ffi"]
        pub fn Function_call(
            _self: &JsiFunction,
            rt: Pin<&mut Runtime>,
            args: &CxxVector<JsiValue>,
        ) -> Result<UniquePtr<JsiValue>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Function_callAsConstructor(
            _self: &JsiFunction,
            rt: Pin<&mut Runtime>,
            args: &CxxVector<JsiValue>,
        ) -> Result<UniquePtr<JsiValue>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Function_callWithThis(
            _self: &JsiFunction,
            rt: Pin<&mut Runtime>,
            thisObj: &JsiObject,
            args: &CxxVector<JsiValue>,
        ) -> Result<UniquePtr<JsiValue>>;
        #[namespace = "jsi_rs::ffi"]
        pub unsafe fn Function_createFromHostFunction(
            rt: Pin<&mut Runtime>,
            name: &PropNameID,
            param_count: u32,
            closure: *mut c_void,
        ) -> UniquePtr<JsiFunction>;
        #[cxx_name = "isHostFunction"]
        pub fn is_host_fn(self: &JsiFunction, rt: Pin<&mut Runtime>) -> bool;

        #[cxx_name = "Value"]
        pub type JsiValue;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromUndefined() -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromNull() -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromBool(b: bool) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromDouble(d: f64) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromInt(i: i32) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromString(
            rt: Pin<&mut Runtime>,
            s: UniquePtr<JsiString>,
        ) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromObject(
            rt: Pin<&mut Runtime>,
            o: UniquePtr<JsiObject>,
        ) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromSymbol(
            rt: Pin<&mut Runtime>,
            s: UniquePtr<JsiSymbol>,
        ) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_copyFromString(rt: Pin<&mut Runtime>, s: &JsiString) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_copyFromObject(rt: Pin<&mut Runtime>, o: &JsiObject) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_copyFromSymbol(rt: Pin<&mut Runtime>, s: &JsiSymbol) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_fromJson(rt: Pin<&mut Runtime>, s: &str) -> UniquePtr<JsiValue>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_compare(rt: Pin<&mut Runtime>, lhs: &JsiValue, rhs: &JsiValue) -> bool;
        #[cxx_name = "isUndefined"]
        pub fn is_undefined(self: &JsiValue) -> bool;
        #[cxx_name = "isNull"]
        pub fn is_null(self: &JsiValue) -> bool;
        #[cxx_name = "isBool"]
        pub fn is_bool(self: &JsiValue) -> bool;
        #[cxx_name = "isNumber"]
        pub fn is_number(self: &JsiValue) -> bool;
        #[cxx_name = "isString"]
        pub fn is_string(self: &JsiValue) -> bool;
        #[cxx_name = "isSymbol"]
        pub fn is_symbol(self: &JsiValue) -> bool;
        #[cxx_name = "isObject"]
        pub fn is_object(self: &JsiValue) -> bool;
        #[cxx_name = "getBool"]
        pub fn get_bool(self: &JsiValue) -> Result<bool>;
        #[cxx_name = "asNumber"]
        pub fn get_number(self: &JsiValue) -> Result<f64>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_asString(
            _self: &JsiValue,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiString>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_asObject(
            _self: &JsiValue,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiObject>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_asSymbol(
            _self: &JsiValue,
            rt: Pin<&mut Runtime>,
        ) -> Result<UniquePtr<JsiSymbol>>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_toString(_self: &JsiValue, rt: Pin<&mut Runtime>) -> UniquePtr<JsiString>;
        #[namespace = "jsi_rs::ffi"]
        pub fn Value_copy(_self: &JsiValue, rt: Pin<&mut Runtime>) -> UniquePtr<JsiValue>;
    }

    impl UniquePtr<Runtime> {}


    #[namespace = "facebook::react"]
    unsafe extern "C++" {
        pub type CallInvoker;
    }

    #[namespace = "jsi_rs::ffi"]
    unsafe extern "C++" {
        pub type c_void;

        pub unsafe fn CallInvoker_invokeSync(_self: SharedPtr<CallInvoker>, closure: *mut c_void);
        pub unsafe fn CallInvoker_invokeAsync(_self: SharedPtr<CallInvoker>, closure: *mut c_void);

        pub fn create_value_vector() -> UniquePtr<CxxVector<JsiValue>>;
        pub fn push_value_vector(vec: Pin<&mut CxxVector<JsiValue>>, item: UniquePtr<JsiValue>);
        pub fn create_prop_name_vector() -> UniquePtr<CxxVector<PropNameID>>;
        pub fn push_prop_name_vector(
            vec: Pin<&mut CxxVector<PropNameID>>,
            item: UniquePtr<PropNameID>,
        );
        pub fn pop_prop_name_vector(vec: Pin<&mut CxxVector<PropNameID>>) -> UniquePtr<PropNameID>;
    }

    #[namespace = "jsi_rs::ffi"]
    extern "Rust" {
        unsafe fn host_fn_trampoline(
            rt: Pin<&mut Runtime>,
            thisVal: &JsiValue,
            args: *const JsiValue,
            count: u32,
            stride: usize,
            closure: *mut c_void,
        ) -> Result<UniquePtr<JsiValue>>;

        unsafe fn call_invoker_trampoline(closure: *mut c_void) -> Result<()>;
    }
}

pub use ffi::*;

unsafe impl Sync for CallInvoker {}
unsafe impl Send for CallInvoker {}

pub type HostFunctionCallback<'rt> = Box<
    dyn FnMut(
            std::pin::Pin<&mut Runtime>,
            &JsiValue,
            &[&JsiValue],
        ) -> Result<cxx::UniquePtr<JsiValue>, anyhow::Error>
        + 'rt,
>;

unsafe fn host_fn_trampoline(
    rt: std::pin::Pin<&mut Runtime>,
    this: &JsiValue,
    args: *const JsiValue,
    count: u32,
    stride: usize,
    closure: *mut c_void,
) -> anyhow::Result<cxx::UniquePtr<JsiValue>> {
    let closure = closure as *mut HostFunctionCallback;
    let mut closure = Box::from_raw(closure);

    // Rust JsiValue type is just a marker type; its size according to Rust is
    // zero so we cannot construct a slice of JsiValue; instead the size of each
    // value is passed in from C++ and we do the pointer math ourselves

    let mut args_refs = Vec::with_capacity(count as usize);

    for i in 0..count {
        let ptr = (args as usize + stride * i as usize) as *const JsiValue;
        args_refs.push(&*ptr);
    }

    let res = closure(rt, this, &args_refs[..]);
    Box::leak(closure);
    res
}

pub type CallInvokerCallback<'rt> = Box<dyn FnOnce() -> anyhow::Result<()> + 'rt>;

unsafe fn call_invoker_trampoline(closure: *mut c_void) -> anyhow::Result<()> {
    let closure = Box::from_raw(closure as *mut CallInvokerCallback);
    closure()
}