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
use std::pin::Pin;

use crate::ffi::*;
use cxx::*;

impl HostObject {
    pub fn get(
        self: Pin<&mut HostObject>,
        rt: Pin<&mut Runtime>,
        name: &PropNameID,
    ) -> UniquePtr<JsiValue> {
        unsafe { HostObject_get(self, rt, name) }
    }

    pub fn get_property_names(
        self: Pin<&mut HostObject>,
        rt: Pin<&mut Runtime>,
    ) -> UniquePtr<CxxVector<PropNameID>> {
        unsafe { HostObject_getPropertyNames(self, rt) }
    }
}

#[repr(transparent)]
pub struct RustHostObject<'a>(pub Box<dyn HostObjectImpl + 'a>);

// i wanted to put these functions inside of an impl block, but this create a
// circular dependency headache on the C++ side b/c you can't access the members
// of a type before it is defined, so they're just normal functions

// functions are used by FFI interface, but Rust thinks it's dead code b/c it's
// pub(crate)
#[allow(dead_code)]
pub(crate) fn rho_get(
    rho: &mut RustHostObject,
    rt: Pin<&mut Runtime>,
    name: &PropNameID,
) -> anyhow::Result<UniquePtr<JsiValue>> {
    rho.0.get(rt, name)
}

#[allow(dead_code)]
pub(crate) fn rho_set(
    rho: &mut RustHostObject,
    rt: Pin<&mut Runtime>,
    name: &PropNameID,
    value: &JsiValue,
) -> anyhow::Result<()> {
    rho.0.set(rt, name, value)
}

#[allow(dead_code)]
pub(crate) fn rho_properties(
    rho: &mut RustHostObject,
    rt: Pin<&mut Runtime>,
) -> UniquePtr<CxxVector<PropNameID>> {
    unsafe {
        let props = rho.0.properties(rt);
        let mut vec = create_prop_name_vector();
        for prop in props {
            push_prop_name_vector(vec.pin_mut(), prop);
        }
        vec
    }
}

pub trait HostObjectImpl {
    fn get(
        &mut self,
        rt: Pin<&mut Runtime>,
        name: &PropNameID,
    ) -> anyhow::Result<UniquePtr<JsiValue>>;
    fn set(
        &mut self,
        rt: Pin<&mut Runtime>,
        name: &PropNameID,
        value: &JsiValue,
    ) -> anyhow::Result<()>;
    fn properties(&mut self, rt: Pin<&mut Runtime>) -> Vec<UniquePtr<PropNameID>>;
}

impl Runtime {
    pub fn evaluate_javascript(
        self: Pin<&mut Runtime>,
        buffer: &SharedPtr<Buffer>,
        source_url: &str,
    ) -> UniquePtr<JsiValue> {
        unsafe { Runtime_evaluateJavaScript(self, buffer, source_url) }
    }

    pub fn prepare_javascript(
        self: Pin<&mut Runtime>,
        buffer: &SharedPtr<Buffer>,
        source_url: &str,
    ) -> SharedPtr<ConstPreparedJavaScript> {
        unsafe { Runtime_prepareJavaScript(self, buffer, source_url) }
    }

    pub fn evaluate_prepared_javascript(
        self: Pin<&mut Runtime>,
        js: &SharedPtr<ConstPreparedJavaScript>,
    ) -> UniquePtr<JsiValue> {
        unsafe { Runtime_evaluatePreparedJavaScript(self, &js) }
    }

    pub fn global(self: Pin<&mut Runtime>) -> UniquePtr<JsiObject> {
        unsafe { Runtime_global(self) }
    }

    pub fn description(self: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
        unsafe { Runtime_description(self) }
    }
}

impl PropNameID {
    pub fn from_str(rt: Pin<&mut Runtime>, s: &str) -> UniquePtr<Self> {
        unsafe { PropNameID_forUtf8(rt, s) }
    }

    pub fn from_jsi_string(rt: Pin<&mut Runtime>, s: &JsiString) -> UniquePtr<Self> {
        unsafe { PropNameID_forString(rt, s) }
    }

    pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
        unsafe { PropNameID_toUtf8(self, rt) }
    }

    pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
        unsafe { PropNameID_compare(rt, self, other) }
    }
}

impl JsiSymbol {
    pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
        unsafe { Symbol_toString(self, rt) }
    }

    pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
        unsafe { Symbol_compare(rt, self, other) }
    }
}

impl JsiString {
    pub fn from_str(rt: Pin<&mut Runtime>, s: &str) -> UniquePtr<Self> {
        unsafe { String_fromUtf8(rt, s) }
    }

    pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
        unsafe { String_toString(self, rt) }
    }

    pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
        unsafe { String_compare(rt, self, other) }
    }
}

impl JsiObject {
    pub fn new(rt: Pin<&mut Runtime>) -> UniquePtr<Self> {
        unsafe { Object_create(rt) }
    }

    pub fn from_host_object(rt: Pin<&mut Runtime>, ho: SharedPtr<HostObject>) -> UniquePtr<Self> {
        unsafe { Object_createFromHostObjectShared(rt, ho) }
    }

    pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
        unsafe { Object_compare(rt, self, other) }
    }

    pub fn get_property(&self, rt: Pin<&mut Runtime>, prop: &PropNameID) -> UniquePtr<JsiValue> {
        unsafe { Object_getProperty(self, rt, prop) }
    }

    pub fn set_property(
        self: Pin<&mut Self>,
        rt: Pin<&mut Runtime>,
        prop: &PropNameID,
        value: &JsiValue,
    ) {
        unsafe { Object_setProperty(self, rt, prop, value) }
    }

    pub fn as_array(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiArray>> {
        unsafe { Object_asArray(self, rt).ok() }
    }

    pub fn as_array_buffer(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiArrayBuffer>> {
        unsafe { Object_asArrayBuffer(self, rt).ok() }
    }

    pub fn as_function(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiFunction>> {
        unsafe { Object_asFunction(self, rt).ok() }
    }

    pub fn get_property_names(self: Pin<&mut Self>, rt: Pin<&mut Runtime>) -> UniquePtr<JsiArray> {
        unsafe { Object_getPropertyNames(self, rt) }
    }
}

impl JsiValue {
    pub fn undefined() -> UniquePtr<Self> {
        unsafe { Value_fromUndefined() }
    }

    pub fn null() -> UniquePtr<Self> {
        unsafe { Value_fromNull() }
    }

    pub fn int(i: i32) -> UniquePtr<Self> {
        unsafe { Value_fromInt(i) }
    }

    pub fn bool(b: bool) -> UniquePtr<Self> {
        unsafe { Value_fromBool(b) }
    }

    pub fn double(d: f64) -> UniquePtr<Self> {
        unsafe { Value_fromDouble(d) }
    }

    pub fn object(rt: Pin<&mut Runtime>, o: &JsiObject) -> UniquePtr<Self> {
        unsafe { Value_copyFromObject(rt, o) }
    }

    pub fn symbol(rt: Pin<&mut Runtime>, s: &JsiSymbol) -> UniquePtr<Self> {
        unsafe { Value_copyFromSymbol(rt, s) }
    }

    pub fn string(rt: Pin<&mut Runtime>, s: &JsiString) -> UniquePtr<Self> {
        unsafe { Value_copyFromString(rt, s) }
    }

    pub fn from_json(rt: Pin<&mut Runtime>, json: &str) -> UniquePtr<Self> {
        unsafe { Value_fromJson(rt, json) }
    }

    pub fn as_object(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiObject>, cxx::Exception> {
        unsafe { Value_asObject(self, rt) }
    }

    pub fn as_symbol(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiSymbol>, cxx::Exception> {
        unsafe { Value_asSymbol(self, rt) }
    }

    pub fn as_string(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiString>, cxx::Exception> {
        unsafe { Value_asString(self, rt) }
    }

    pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<JsiString> {
        unsafe { Value_toString(self, rt) }
    }
}

impl JsiWeakObject {
    pub fn from_object(rt: Pin<&mut Runtime>, object: &JsiObject) -> UniquePtr<Self> {
        unsafe { WeakObject_fromObject(rt, object) }
    }

    pub fn lock(self: Pin<&mut Self>, rt: Pin<&mut Runtime>) -> UniquePtr<JsiValue> {
        unsafe { WeakObject_lock(self, rt) }
    }
}