1use std::pin::Pin;
2
3use crate::ffi::*;
4use cxx::*;
5
6impl HostObject {
7 pub fn get(
8 self: Pin<&mut HostObject>,
9 rt: Pin<&mut Runtime>,
10 name: &PropNameID,
11 ) -> UniquePtr<JsiValue> {
12 unsafe { HostObject_get(self, rt, name) }
13 }
14
15 pub fn get_property_names(
16 self: Pin<&mut HostObject>,
17 rt: Pin<&mut Runtime>,
18 ) -> UniquePtr<CxxVector<PropNameID>> {
19 unsafe { HostObject_getPropertyNames(self, rt) }
20 }
21}
22
23#[repr(transparent)]
24pub struct RustHostObject<'a>(pub Box<dyn HostObjectImpl + 'a>);
25
26#[allow(dead_code)]
33pub(crate) fn rho_get(
34 rho: &mut RustHostObject,
35 rt: Pin<&mut Runtime>,
36 name: &PropNameID,
37) -> anyhow::Result<UniquePtr<JsiValue>> {
38 rho.0.get(rt, name)
39}
40
41#[allow(dead_code)]
42pub(crate) fn rho_set(
43 rho: &mut RustHostObject,
44 rt: Pin<&mut Runtime>,
45 name: &PropNameID,
46 value: &JsiValue,
47) -> anyhow::Result<()> {
48 rho.0.set(rt, name, value)
49}
50
51#[allow(dead_code)]
52pub(crate) fn rho_properties(
53 rho: &mut RustHostObject,
54 rt: Pin<&mut Runtime>,
55) -> UniquePtr<CxxVector<PropNameID>> {
56 unsafe {
57 let props = rho.0.properties(rt);
58 let mut vec = create_prop_name_vector();
59 for prop in props {
60 push_prop_name_vector(vec.pin_mut(), prop);
61 }
62 vec
63 }
64}
65
66pub trait HostObjectImpl {
67 fn get(
68 &mut self,
69 rt: Pin<&mut Runtime>,
70 name: &PropNameID,
71 ) -> anyhow::Result<UniquePtr<JsiValue>>;
72 fn set(
73 &mut self,
74 rt: Pin<&mut Runtime>,
75 name: &PropNameID,
76 value: &JsiValue,
77 ) -> anyhow::Result<()>;
78 fn properties(&mut self, rt: Pin<&mut Runtime>) -> Vec<UniquePtr<PropNameID>>;
79}
80
81impl Runtime {
82 pub fn evaluate_javascript(
83 self: Pin<&mut Runtime>,
84 buffer: &SharedPtr<Buffer>,
85 source_url: &str,
86 ) -> UniquePtr<JsiValue> {
87 unsafe { Runtime_evaluateJavaScript(self, buffer, source_url) }
88 }
89
90 pub fn prepare_javascript(
91 self: Pin<&mut Runtime>,
92 buffer: &SharedPtr<Buffer>,
93 source_url: &str,
94 ) -> SharedPtr<ConstPreparedJavaScript> {
95 unsafe { Runtime_prepareJavaScript(self, buffer, source_url) }
96 }
97
98 pub fn evaluate_prepared_javascript(
99 self: Pin<&mut Runtime>,
100 js: &SharedPtr<ConstPreparedJavaScript>,
101 ) -> UniquePtr<JsiValue> {
102 unsafe { Runtime_evaluatePreparedJavaScript(self, &js) }
103 }
104
105 pub fn global(self: Pin<&mut Runtime>) -> UniquePtr<JsiObject> {
106 unsafe { Runtime_global(self) }
107 }
108
109 pub fn description(self: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
110 unsafe { Runtime_description(self) }
111 }
112}
113
114impl PropNameID {
115 pub fn from_str(rt: Pin<&mut Runtime>, s: &str) -> UniquePtr<Self> {
116 unsafe { PropNameID_forUtf8(rt, s) }
117 }
118
119 pub fn from_jsi_string(rt: Pin<&mut Runtime>, s: &JsiString) -> UniquePtr<Self> {
120 unsafe { PropNameID_forString(rt, s) }
121 }
122
123 pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
124 unsafe { PropNameID_toUtf8(self, rt) }
125 }
126
127 pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
128 unsafe { PropNameID_compare(rt, self, other) }
129 }
130}
131
132impl JsiSymbol {
133 pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
134 unsafe { Symbol_toString(self, rt) }
135 }
136
137 pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
138 unsafe { Symbol_compare(rt, self, other) }
139 }
140}
141
142impl JsiString {
143 pub fn from_str(rt: Pin<&mut Runtime>, s: &str) -> UniquePtr<Self> {
144 unsafe { String_fromUtf8(rt, s) }
145 }
146
147 pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<CxxString> {
148 unsafe { String_toString(self, rt) }
149 }
150
151 pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
152 unsafe { String_compare(rt, self, other) }
153 }
154}
155
156impl JsiObject {
157 pub fn new(rt: Pin<&mut Runtime>) -> UniquePtr<Self> {
158 unsafe { Object_create(rt) }
159 }
160
161 pub fn from_host_object(rt: Pin<&mut Runtime>, ho: SharedPtr<HostObject>) -> UniquePtr<Self> {
162 unsafe { Object_createFromHostObjectShared(rt, ho) }
163 }
164
165 pub fn compare(&self, other: &Self, rt: Pin<&mut Runtime>) -> bool {
166 unsafe { Object_compare(rt, self, other) }
167 }
168
169 pub fn get_property(&self, rt: Pin<&mut Runtime>, prop: &PropNameID) -> UniquePtr<JsiValue> {
170 unsafe { Object_getProperty(self, rt, prop) }
171 }
172
173 pub fn set_property(
174 self: Pin<&mut Self>,
175 rt: Pin<&mut Runtime>,
176 prop: &PropNameID,
177 value: &JsiValue,
178 ) {
179 unsafe { Object_setProperty(self, rt, prop, value) }
180 }
181
182 pub fn as_array(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiArray>> {
183 unsafe { Object_asArray(self, rt).ok() }
184 }
185
186 pub fn as_array_buffer(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiArrayBuffer>> {
187 unsafe { Object_asArrayBuffer(self, rt).ok() }
188 }
189
190 pub fn as_function(&self, rt: Pin<&mut Runtime>) -> Option<UniquePtr<JsiFunction>> {
191 unsafe { Object_asFunction(self, rt).ok() }
192 }
193
194 pub fn get_property_names(self: Pin<&mut Self>, rt: Pin<&mut Runtime>) -> UniquePtr<JsiArray> {
195 unsafe { Object_getPropertyNames(self, rt) }
196 }
197}
198
199impl JsiValue {
200 pub fn undefined() -> UniquePtr<Self> {
201 unsafe { Value_fromUndefined() }
202 }
203
204 pub fn null() -> UniquePtr<Self> {
205 unsafe { Value_fromNull() }
206 }
207
208 pub fn int(i: i32) -> UniquePtr<Self> {
209 unsafe { Value_fromInt(i) }
210 }
211
212 pub fn bool(b: bool) -> UniquePtr<Self> {
213 unsafe { Value_fromBool(b) }
214 }
215
216 pub fn double(d: f64) -> UniquePtr<Self> {
217 unsafe { Value_fromDouble(d) }
218 }
219
220 pub fn object(rt: Pin<&mut Runtime>, o: &JsiObject) -> UniquePtr<Self> {
221 unsafe { Value_copyFromObject(rt, o) }
222 }
223
224 pub fn symbol(rt: Pin<&mut Runtime>, s: &JsiSymbol) -> UniquePtr<Self> {
225 unsafe { Value_copyFromSymbol(rt, s) }
226 }
227
228 pub fn string(rt: Pin<&mut Runtime>, s: &JsiString) -> UniquePtr<Self> {
229 unsafe { Value_copyFromString(rt, s) }
230 }
231
232 pub fn from_json(rt: Pin<&mut Runtime>, json: &str) -> UniquePtr<Self> {
233 unsafe { Value_fromJson(rt, json) }
234 }
235
236 pub fn as_object(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiObject>, cxx::Exception> {
237 unsafe { Value_asObject(self, rt) }
238 }
239
240 pub fn as_symbol(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiSymbol>, cxx::Exception> {
241 unsafe { Value_asSymbol(self, rt) }
242 }
243
244 pub fn as_string(&self, rt: Pin<&mut Runtime>) -> Result<UniquePtr<JsiString>, cxx::Exception> {
245 unsafe { Value_asString(self, rt) }
246 }
247
248 pub fn to_string(&self, rt: Pin<&mut Runtime>) -> UniquePtr<JsiString> {
249 unsafe { Value_toString(self, rt) }
250 }
251}
252
253impl JsiWeakObject {
254 pub fn from_object(rt: Pin<&mut Runtime>, object: &JsiObject) -> UniquePtr<Self> {
255 unsafe { WeakObject_fromObject(rt, object) }
256 }
257
258 pub fn lock(self: Pin<&mut Self>, rt: Pin<&mut Runtime>) -> UniquePtr<JsiValue> {
259 unsafe { WeakObject_lock(self, rt) }
260 }
261}