1use crate::{Context, TypedArrayType, ValuePropertyFlags, ffi};
7use glib::translate::*;
8
9glib::wrapper! {
10 #[doc(alias = "JSCValue")]
11 pub struct Value(Object<ffi::JSCValue, ffi::JSCValueClass>);
12
13 match fn {
14 type_ => || ffi::jsc_value_get_type(),
15 }
16}
17
18impl Value {
19 #[doc(alias = "jsc_value_new_array_from_garray")]
30 pub fn new_array_from_garray(context: &Context, array: &[Value]) -> Value {
31 skip_assert_initialized!();
32 unsafe {
33 from_glib_full(ffi::jsc_value_new_array_from_garray(
34 context.to_glib_none().0,
35 array.to_glib_none().0,
36 ))
37 }
38 }
39
40 #[doc(alias = "jsc_value_new_array_from_strv")]
41 pub fn new_array_from_strv(context: &Context, strv: &[&str]) -> Value {
42 skip_assert_initialized!();
43 unsafe {
44 from_glib_full(ffi::jsc_value_new_array_from_strv(
45 context.to_glib_none().0,
46 strv.to_glib_none().0,
47 ))
48 }
49 }
50
51 #[doc(alias = "jsc_value_new_boolean")]
52 pub fn new_boolean(context: &Context, value: bool) -> Value {
53 skip_assert_initialized!();
54 unsafe {
55 from_glib_full(ffi::jsc_value_new_boolean(
56 context.to_glib_none().0,
57 value.into_glib(),
58 ))
59 }
60 }
61
62 #[doc(alias = "jsc_value_new_from_json")]
63 #[doc(alias = "new_from_json")]
64 pub fn from_json(context: &Context, json: &str) -> Value {
65 skip_assert_initialized!();
66 unsafe {
67 from_glib_full(ffi::jsc_value_new_from_json(
68 context.to_glib_none().0,
69 json.to_glib_none().0,
70 ))
71 }
72 }
73
74 #[doc(alias = "jsc_value_new_null")]
90 pub fn new_null(context: &Context) -> Value {
91 skip_assert_initialized!();
92 unsafe { from_glib_full(ffi::jsc_value_new_null(context.to_glib_none().0)) }
93 }
94
95 #[doc(alias = "jsc_value_new_number")]
96 pub fn new_number(context: &Context, number: f64) -> Value {
97 skip_assert_initialized!();
98 unsafe { from_glib_full(ffi::jsc_value_new_number(context.to_glib_none().0, number)) }
99 }
100
101 #[cfg(feature = "v2_48")]
107 #[cfg_attr(docsrs, doc(cfg(feature = "v2_48")))]
108 #[doc(alias = "jsc_value_new_promise")]
109 pub fn new_promise<P: FnMut(&Value, &Value)>(context: &Context, executor: P) -> Value {
110 skip_assert_initialized!();
111 let mut executor_data: P = executor;
112 unsafe extern "C" fn executor_func<P: FnMut(&Value, &Value)>(
113 resolve: *mut ffi::JSCValue,
114 reject: *mut ffi::JSCValue,
115 user_data: glib::ffi::gpointer,
116 ) {
117 unsafe {
118 let resolve = from_glib_borrow(resolve);
119 let reject = from_glib_borrow(reject);
120 let callback = user_data as *mut P;
121 (*callback)(&resolve, &reject)
122 }
123 }
124 let executor = Some(executor_func::<P> as _);
125 let super_callback0: &mut P = &mut executor_data;
126 unsafe {
127 from_glib_full(ffi::jsc_value_new_promise(
128 context.to_glib_none().0,
129 executor,
130 super_callback0 as *mut _ as *mut _,
131 ))
132 }
133 }
134
135 #[doc(alias = "jsc_value_new_string")]
136 pub fn new_string(context: &Context, string: Option<&str>) -> Value {
137 skip_assert_initialized!();
138 unsafe {
139 from_glib_full(ffi::jsc_value_new_string(
140 context.to_glib_none().0,
141 string.to_glib_none().0,
142 ))
143 }
144 }
145
146 #[doc(alias = "jsc_value_new_string_from_bytes")]
147 pub fn new_string_from_bytes(context: &Context, bytes: Option<&glib::Bytes>) -> Value {
148 skip_assert_initialized!();
149 unsafe {
150 from_glib_full(ffi::jsc_value_new_string_from_bytes(
151 context.to_glib_none().0,
152 bytes.to_glib_none().0,
153 ))
154 }
155 }
156
157 #[doc(alias = "jsc_value_new_typed_array")]
158 pub fn new_typed_array(context: &Context, type_: TypedArrayType, length: usize) -> Value {
159 skip_assert_initialized!();
160 unsafe {
161 from_glib_full(ffi::jsc_value_new_typed_array(
162 context.to_glib_none().0,
163 type_.into_glib(),
164 length,
165 ))
166 }
167 }
168
169 #[doc(alias = "jsc_value_new_undefined")]
170 pub fn new_undefined(context: &Context) -> Value {
171 skip_assert_initialized!();
172 unsafe { from_glib_full(ffi::jsc_value_new_undefined(context.to_glib_none().0)) }
173 }
174
175 #[doc(alias = "jsc_value_array_buffer_get_size")]
176 pub fn array_buffer_get_size(&self) -> usize {
177 unsafe { ffi::jsc_value_array_buffer_get_size(self.to_glib_none().0) }
178 }
179
180 #[doc(alias = "jsc_value_constructor_callv")]
187 #[must_use]
188 pub fn constructor_callv(&self, parameters: &[Value]) -> Option<Value> {
189 let n_parameters = parameters.len() as _;
190 unsafe {
191 from_glib_full(ffi::jsc_value_constructor_callv(
192 self.to_glib_none().0,
193 n_parameters,
194 parameters.to_glib_none().0,
195 ))
196 }
197 }
198
199 #[doc(alias = "jsc_value_function_callv")]
206 #[must_use]
207 pub fn function_callv(&self, parameters: &[Value]) -> Option<Value> {
208 let n_parameters = parameters.len() as _;
209 unsafe {
210 from_glib_full(ffi::jsc_value_function_callv(
211 self.to_glib_none().0,
212 n_parameters,
213 parameters.to_glib_none().0,
214 ))
215 }
216 }
217
218 #[doc(alias = "jsc_value_get_context")]
219 #[doc(alias = "get_context")]
220 pub fn context(&self) -> Option<Context> {
221 unsafe { from_glib_none(ffi::jsc_value_get_context(self.to_glib_none().0)) }
222 }
223
224 #[doc(alias = "jsc_value_is_array")]
225 pub fn is_array(&self) -> bool {
226 unsafe { from_glib(ffi::jsc_value_is_array(self.to_glib_none().0)) }
227 }
228
229 #[doc(alias = "jsc_value_is_array_buffer")]
230 pub fn is_array_buffer(&self) -> bool {
231 unsafe { from_glib(ffi::jsc_value_is_array_buffer(self.to_glib_none().0)) }
232 }
233
234 #[doc(alias = "jsc_value_is_boolean")]
235 pub fn is_boolean(&self) -> bool {
236 unsafe { from_glib(ffi::jsc_value_is_boolean(self.to_glib_none().0)) }
237 }
238
239 #[doc(alias = "jsc_value_is_constructor")]
240 pub fn is_constructor(&self) -> bool {
241 unsafe { from_glib(ffi::jsc_value_is_constructor(self.to_glib_none().0)) }
242 }
243
244 #[doc(alias = "jsc_value_is_function")]
245 pub fn is_function(&self) -> bool {
246 unsafe { from_glib(ffi::jsc_value_is_function(self.to_glib_none().0)) }
247 }
248
249 #[doc(alias = "jsc_value_is_null")]
250 pub fn is_null(&self) -> bool {
251 unsafe { from_glib(ffi::jsc_value_is_null(self.to_glib_none().0)) }
252 }
253
254 #[doc(alias = "jsc_value_is_number")]
255 pub fn is_number(&self) -> bool {
256 unsafe { from_glib(ffi::jsc_value_is_number(self.to_glib_none().0)) }
257 }
258
259 #[doc(alias = "jsc_value_is_object")]
260 pub fn is_object(&self) -> bool {
261 unsafe { from_glib(ffi::jsc_value_is_object(self.to_glib_none().0)) }
262 }
263
264 #[doc(alias = "jsc_value_is_string")]
265 pub fn is_string(&self) -> bool {
266 unsafe { from_glib(ffi::jsc_value_is_string(self.to_glib_none().0)) }
267 }
268
269 #[doc(alias = "jsc_value_is_typed_array")]
270 pub fn is_typed_array(&self) -> bool {
271 unsafe { from_glib(ffi::jsc_value_is_typed_array(self.to_glib_none().0)) }
272 }
273
274 #[doc(alias = "jsc_value_is_undefined")]
275 pub fn is_undefined(&self) -> bool {
276 unsafe { from_glib(ffi::jsc_value_is_undefined(self.to_glib_none().0)) }
277 }
278
279 #[doc(alias = "jsc_value_new_typed_array_with_buffer")]
280 #[must_use]
281 pub fn new_typed_array_with_buffer(
282 &self,
283 type_: TypedArrayType,
284 offset: usize,
285 length: isize,
286 ) -> Option<Value> {
287 unsafe {
288 from_glib_full(ffi::jsc_value_new_typed_array_with_buffer(
289 self.to_glib_none().0,
290 type_.into_glib(),
291 offset,
292 length,
293 ))
294 }
295 }
296
297 #[doc(alias = "jsc_value_object_define_property_data")]
303 pub fn object_define_property_data(
304 &self,
305 property_name: &str,
306 flags: ValuePropertyFlags,
307 property_value: Option<&Value>,
308 ) {
309 unsafe {
310 ffi::jsc_value_object_define_property_data(
311 self.to_glib_none().0,
312 property_name.to_glib_none().0,
313 flags.into_glib(),
314 property_value.to_glib_none().0,
315 );
316 }
317 }
318
319 #[doc(alias = "jsc_value_object_delete_property")]
320 pub fn object_delete_property(&self, name: &str) -> bool {
321 unsafe {
322 from_glib(ffi::jsc_value_object_delete_property(
323 self.to_glib_none().0,
324 name.to_glib_none().0,
325 ))
326 }
327 }
328
329 #[doc(alias = "jsc_value_object_enumerate_properties")]
330 pub fn object_enumerate_properties(&self) -> Vec<glib::GString> {
331 unsafe {
332 FromGlibPtrContainer::from_glib_full(ffi::jsc_value_object_enumerate_properties(
333 self.to_glib_none().0,
334 ))
335 }
336 }
337
338 #[doc(alias = "jsc_value_object_get_property")]
339 #[must_use]
340 pub fn object_get_property(&self, name: &str) -> Option<Value> {
341 unsafe {
342 from_glib_full(ffi::jsc_value_object_get_property(
343 self.to_glib_none().0,
344 name.to_glib_none().0,
345 ))
346 }
347 }
348
349 #[doc(alias = "jsc_value_object_get_property_at_index")]
350 #[must_use]
351 pub fn object_get_property_at_index(&self, index: u32) -> Option<Value> {
352 unsafe {
353 from_glib_full(ffi::jsc_value_object_get_property_at_index(
354 self.to_glib_none().0,
355 index,
356 ))
357 }
358 }
359
360 #[doc(alias = "jsc_value_object_has_property")]
361 pub fn object_has_property(&self, name: &str) -> bool {
362 unsafe {
363 from_glib(ffi::jsc_value_object_has_property(
364 self.to_glib_none().0,
365 name.to_glib_none().0,
366 ))
367 }
368 }
369
370 #[doc(alias = "jsc_value_object_invoke_methodv")]
377 #[must_use]
378 pub fn object_invoke_methodv(&self, name: &str, parameters: &[Value]) -> Option<Value> {
379 let n_parameters = parameters.len() as _;
380 unsafe {
381 from_glib_full(ffi::jsc_value_object_invoke_methodv(
382 self.to_glib_none().0,
383 name.to_glib_none().0,
384 n_parameters,
385 parameters.to_glib_none().0,
386 ))
387 }
388 }
389
390 #[doc(alias = "jsc_value_object_is_instance_of")]
391 pub fn object_is_instance_of(&self, name: &str) -> bool {
392 unsafe {
393 from_glib(ffi::jsc_value_object_is_instance_of(
394 self.to_glib_none().0,
395 name.to_glib_none().0,
396 ))
397 }
398 }
399
400 #[doc(alias = "jsc_value_object_set_property")]
401 pub fn object_set_property(&self, name: &str, property: &Value) {
402 unsafe {
403 ffi::jsc_value_object_set_property(
404 self.to_glib_none().0,
405 name.to_glib_none().0,
406 property.to_glib_none().0,
407 );
408 }
409 }
410
411 #[doc(alias = "jsc_value_object_set_property_at_index")]
412 pub fn object_set_property_at_index(&self, index: u32, property: &Value) {
413 unsafe {
414 ffi::jsc_value_object_set_property_at_index(
415 self.to_glib_none().0,
416 index,
417 property.to_glib_none().0,
418 );
419 }
420 }
421
422 #[doc(alias = "jsc_value_to_boolean")]
423 pub fn to_boolean(&self) -> bool {
424 unsafe { from_glib(ffi::jsc_value_to_boolean(self.to_glib_none().0)) }
425 }
426
427 #[doc(alias = "jsc_value_to_double")]
428 pub fn to_double(&self) -> f64 {
429 unsafe { ffi::jsc_value_to_double(self.to_glib_none().0) }
430 }
431
432 #[doc(alias = "jsc_value_to_int32")]
433 pub fn to_int32(&self) -> i32 {
434 unsafe { ffi::jsc_value_to_int32(self.to_glib_none().0) }
435 }
436
437 #[doc(alias = "jsc_value_to_json")]
438 pub fn to_json(&self, indent: u32) -> Option<glib::GString> {
439 unsafe { from_glib_full(ffi::jsc_value_to_json(self.to_glib_none().0, indent)) }
440 }
441
442 #[doc(alias = "jsc_value_to_string")]
443 #[doc(alias = "to_string")]
444 pub fn to_str(&self) -> glib::GString {
445 unsafe { from_glib_full(ffi::jsc_value_to_string(self.to_glib_none().0)) }
446 }
447
448 #[doc(alias = "jsc_value_to_string_as_bytes")]
449 pub fn to_string_as_bytes(&self) -> Option<glib::Bytes> {
450 unsafe { from_glib_full(ffi::jsc_value_to_string_as_bytes(self.to_glib_none().0)) }
451 }
452
453 #[doc(alias = "jsc_value_typed_array_get_buffer")]
454 #[must_use]
455 pub fn typed_array_get_buffer(&self) -> Option<Value> {
456 unsafe { from_glib_full(ffi::jsc_value_typed_array_get_buffer(self.to_glib_none().0)) }
457 }
458
459 #[doc(alias = "jsc_value_typed_array_get_length")]
465 pub fn typed_array_get_length(&self) -> usize {
466 unsafe { ffi::jsc_value_typed_array_get_length(self.to_glib_none().0) }
467 }
468
469 #[doc(alias = "jsc_value_typed_array_get_offset")]
470 pub fn typed_array_get_offset(&self) -> usize {
471 unsafe { ffi::jsc_value_typed_array_get_offset(self.to_glib_none().0) }
472 }
473
474 #[doc(alias = "jsc_value_typed_array_get_size")]
475 pub fn typed_array_get_size(&self) -> usize {
476 unsafe { ffi::jsc_value_typed_array_get_size(self.to_glib_none().0) }
477 }
478
479 #[doc(alias = "jsc_value_typed_array_get_type")]
480 pub fn typed_array_get_type(&self) -> TypedArrayType {
481 unsafe { from_glib(ffi::jsc_value_typed_array_get_type(self.to_glib_none().0)) }
482 }
483}
484
485impl std::fmt::Display for Value {
486 #[inline]
487 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
488 f.write_str(&self.to_str())
489 }
490}