1use crate::{Context, ValuePropertyFlags};
7use glib::{prelude::*, translate::*};
8use std::fmt;
9
10glib::wrapper! {
11 #[doc(alias = "JSCValue")]
12 pub struct Value(Object<ffi::JSCValue, ffi::JSCValueClass>);
13
14 match fn {
15 type_ => || ffi::jsc_value_get_type(),
16 }
17}
18
19impl Value {
20 pub const NONE: Option<&'static Value> = None;
21
22 #[doc(alias = "jsc_value_new_array_from_garray")]
35 pub fn new_array_from_garray(context: &impl IsA<Context>, array: &[Value]) -> Value {
36 unsafe {
37 from_glib_full(ffi::jsc_value_new_array_from_garray(
38 context.as_ref().to_glib_none().0,
39 array.to_glib_none().0,
40 ))
41 }
42 }
43
44 #[doc(alias = "jsc_value_new_array_from_strv")]
45 pub fn new_array_from_strv(context: &impl IsA<Context>, strv: &[&str]) -> Value {
46 unsafe {
47 from_glib_full(ffi::jsc_value_new_array_from_strv(
48 context.as_ref().to_glib_none().0,
49 strv.to_glib_none().0,
50 ))
51 }
52 }
53
54 #[doc(alias = "jsc_value_new_boolean")]
55 pub fn new_boolean(context: &impl IsA<Context>, value: bool) -> Value {
56 unsafe {
57 from_glib_full(ffi::jsc_value_new_boolean(
58 context.as_ref().to_glib_none().0,
59 value.into_glib(),
60 ))
61 }
62 }
63
64 #[cfg(any(feature = "v2_28", feature = "dox"))]
65 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_28")))]
66 #[doc(alias = "jsc_value_new_from_json")]
67 #[doc(alias = "new_from_json")]
68 pub fn from_json(context: &impl IsA<Context>, json: &str) -> Value {
69 unsafe {
70 from_glib_full(ffi::jsc_value_new_from_json(
71 context.as_ref().to_glib_none().0,
72 json.to_glib_none().0,
73 ))
74 }
75 }
76
77 #[doc(alias = "jsc_value_new_null")]
93 pub fn new_null(context: &impl IsA<Context>) -> Value {
94 unsafe { from_glib_full(ffi::jsc_value_new_null(context.as_ref().to_glib_none().0)) }
95 }
96
97 #[doc(alias = "jsc_value_new_number")]
98 pub fn new_number(context: &impl IsA<Context>, number: f64) -> Value {
99 unsafe {
100 from_glib_full(ffi::jsc_value_new_number(
101 context.as_ref().to_glib_none().0,
102 number,
103 ))
104 }
105 }
106
107 #[doc(alias = "jsc_value_new_string")]
113 pub fn new_string(context: &impl IsA<Context>, string: Option<&str>) -> Value {
114 unsafe {
115 from_glib_full(ffi::jsc_value_new_string(
116 context.as_ref().to_glib_none().0,
117 string.to_glib_none().0,
118 ))
119 }
120 }
121
122 #[doc(alias = "jsc_value_new_string_from_bytes")]
123 pub fn new_string_from_bytes(
124 context: &impl IsA<Context>,
125 bytes: Option<&glib::Bytes>,
126 ) -> Value {
127 unsafe {
128 from_glib_full(ffi::jsc_value_new_string_from_bytes(
129 context.as_ref().to_glib_none().0,
130 bytes.to_glib_none().0,
131 ))
132 }
133 }
134
135 #[doc(alias = "jsc_value_new_undefined")]
143 pub fn new_undefined(context: &impl IsA<Context>) -> Value {
144 unsafe {
145 from_glib_full(ffi::jsc_value_new_undefined(
146 context.as_ref().to_glib_none().0,
147 ))
148 }
149 }
150
151 pub fn builder() -> ValueBuilder {
156 ValueBuilder::new()
157 }
158}
159
160impl fmt::Display for Value {
161 #[inline]
162 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
163 f.write_str(&ValueExt::to_str(self))
164 }
165}
166
167#[must_use = "The builder must be built to be used"]
172pub struct ValueBuilder {
173 builder: glib::object::ObjectBuilder<'static, Value>,
174}
175
176impl ValueBuilder {
177 fn new() -> Self {
178 Self {
179 builder: glib::object::Object::builder(),
180 }
181 }
182
183 pub fn context(self, context: &impl IsA<Context>) -> Self {
184 Self {
185 builder: self.builder.property("context", context.clone().upcast()),
186 }
187 }
188
189 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
192 pub fn build(self) -> Value {
193 self.builder.build()
194 }
195}
196
197pub trait ValueExt: 'static {
198 #[cfg(any(feature = "v2_38", feature = "dox"))]
204 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
205 #[doc(alias = "jsc_value_array_buffer_get_size")]
206 fn array_buffer_get_size(&self) -> usize;
207
208 #[doc(alias = "jsc_value_constructor_callv")]
213 #[must_use]
214 fn constructor_callv(&self, parameters: &[Value]) -> Option<Value>;
215
216 #[doc(alias = "jsc_value_function_callv")]
221 #[must_use]
222 fn function_callv(&self, parameters: &[Value]) -> Option<Value>;
223
224 #[doc(alias = "jsc_value_get_context")]
225 #[doc(alias = "get_context")]
226 fn context(&self) -> Option<Context>;
227
228 #[doc(alias = "jsc_value_is_array")]
229 fn is_array(&self) -> bool;
230
231 #[cfg(any(feature = "v2_38", feature = "dox"))]
232 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
233 #[doc(alias = "jsc_value_is_array_buffer")]
234 fn is_array_buffer(&self) -> bool;
235
236 #[doc(alias = "jsc_value_is_boolean")]
237 fn is_boolean(&self) -> bool;
238
239 #[doc(alias = "jsc_value_is_constructor")]
240 fn is_constructor(&self) -> bool;
241
242 #[doc(alias = "jsc_value_is_function")]
243 fn is_function(&self) -> bool;
244
245 #[doc(alias = "jsc_value_is_null")]
246 fn is_null(&self) -> bool;
247
248 #[doc(alias = "jsc_value_is_number")]
249 fn is_number(&self) -> bool;
250
251 #[doc(alias = "jsc_value_is_object")]
252 fn is_object(&self) -> bool;
253
254 #[doc(alias = "jsc_value_is_string")]
255 fn is_string(&self) -> bool;
256
257 #[cfg(any(feature = "v2_38", feature = "dox"))]
258 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
259 #[doc(alias = "jsc_value_is_typed_array")]
260 fn is_typed_array(&self) -> bool;
261
262 #[doc(alias = "jsc_value_is_undefined")]
263 fn is_undefined(&self) -> bool;
264
265 #[doc(alias = "jsc_value_object_define_property_data")]
272 fn object_define_property_data(
273 &self,
274 property_name: &str,
275 flags: ValuePropertyFlags,
276 property_value: Option<&impl IsA<Value>>,
277 );
278
279 #[doc(alias = "jsc_value_object_delete_property")]
280 fn object_delete_property(&self, name: &str) -> bool;
281
282 #[doc(alias = "jsc_value_object_enumerate_properties")]
283 fn object_enumerate_properties(&self) -> Vec<glib::GString>;
284
285 #[doc(alias = "jsc_value_object_get_property")]
286 #[must_use]
287 fn object_get_property(&self, name: &str) -> Option<Value>;
288
289 #[doc(alias = "jsc_value_object_get_property_at_index")]
290 #[must_use]
291 fn object_get_property_at_index(&self, index: u32) -> Option<Value>;
292
293 #[doc(alias = "jsc_value_object_has_property")]
294 fn object_has_property(&self, name: &str) -> bool;
295
296 #[doc(alias = "jsc_value_object_invoke_methodv")]
301 #[must_use]
302 fn object_invoke_methodv(&self, name: &str, parameters: &[Value]) -> Option<Value>;
303
304 #[doc(alias = "jsc_value_object_is_instance_of")]
305 fn object_is_instance_of(&self, name: &str) -> bool;
306
307 #[doc(alias = "jsc_value_object_set_property")]
308 fn object_set_property(&self, name: &str, property: &impl IsA<Value>);
309
310 #[doc(alias = "jsc_value_object_set_property_at_index")]
311 fn object_set_property_at_index(&self, index: u32, property: &impl IsA<Value>);
312
313 #[doc(alias = "jsc_value_to_boolean")]
314 fn to_boolean(&self) -> bool;
315
316 #[doc(alias = "jsc_value_to_double")]
317 fn to_double(&self) -> f64;
318
319 #[doc(alias = "jsc_value_to_int32")]
320 fn to_int32(&self) -> i32;
321
322 #[cfg(any(feature = "v2_28", feature = "dox"))]
323 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_28")))]
324 #[doc(alias = "jsc_value_to_json")]
325 fn to_json(&self, indent: u32) -> Option<glib::GString>;
326
327 #[doc(alias = "jsc_value_to_string")]
328 #[doc(alias = "to_string")]
329 fn to_str(&self) -> glib::GString;
330
331 #[doc(alias = "jsc_value_to_string_as_bytes")]
332 fn to_string_as_bytes(&self) -> Option<glib::Bytes>;
333
334 #[cfg(any(feature = "v2_38", feature = "dox"))]
335 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
336 #[doc(alias = "jsc_value_typed_array_get_buffer")]
337 #[must_use]
338 fn typed_array_get_buffer(&self) -> Option<Value>;
339
340 #[cfg(any(feature = "v2_38", feature = "dox"))]
346 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
347 #[doc(alias = "jsc_value_typed_array_get_length")]
348 fn typed_array_get_length(&self) -> usize;
349
350 #[cfg(any(feature = "v2_38", feature = "dox"))]
351 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
352 #[doc(alias = "jsc_value_typed_array_get_offset")]
353 fn typed_array_get_offset(&self) -> usize;
354
355 #[cfg(any(feature = "v2_38", feature = "dox"))]
356 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
357 #[doc(alias = "jsc_value_typed_array_get_size")]
358 fn typed_array_get_size(&self) -> usize;
359
360 }
365
366impl<O: IsA<Value>> ValueExt for O {
367 #[cfg(any(feature = "v2_38", feature = "dox"))]
374 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
375 fn array_buffer_get_size(&self) -> usize {
376 unsafe { ffi::jsc_value_array_buffer_get_size(self.as_ref().to_glib_none().0) }
377 }
378
379 fn constructor_callv(&self, parameters: &[Value]) -> Option<Value> {
384 let n_parameters = parameters.len() as _;
385 unsafe {
386 from_glib_full(ffi::jsc_value_constructor_callv(
387 self.as_ref().to_glib_none().0,
388 n_parameters,
389 parameters.to_glib_none().0,
390 ))
391 }
392 }
393
394 fn function_callv(&self, parameters: &[Value]) -> Option<Value> {
399 let n_parameters = parameters.len() as _;
400 unsafe {
401 from_glib_full(ffi::jsc_value_function_callv(
402 self.as_ref().to_glib_none().0,
403 n_parameters,
404 parameters.to_glib_none().0,
405 ))
406 }
407 }
408
409 fn context(&self) -> Option<Context> {
410 unsafe { from_glib_none(ffi::jsc_value_get_context(self.as_ref().to_glib_none().0)) }
411 }
412
413 fn is_array(&self) -> bool {
414 unsafe { from_glib(ffi::jsc_value_is_array(self.as_ref().to_glib_none().0)) }
415 }
416
417 #[cfg(any(feature = "v2_38", feature = "dox"))]
418 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
419 fn is_array_buffer(&self) -> bool {
420 unsafe {
421 from_glib(ffi::jsc_value_is_array_buffer(
422 self.as_ref().to_glib_none().0,
423 ))
424 }
425 }
426
427 fn is_boolean(&self) -> bool {
428 unsafe { from_glib(ffi::jsc_value_is_boolean(self.as_ref().to_glib_none().0)) }
429 }
430
431 fn is_constructor(&self) -> bool {
432 unsafe {
433 from_glib(ffi::jsc_value_is_constructor(
434 self.as_ref().to_glib_none().0,
435 ))
436 }
437 }
438
439 fn is_function(&self) -> bool {
440 unsafe { from_glib(ffi::jsc_value_is_function(self.as_ref().to_glib_none().0)) }
441 }
442
443 fn is_null(&self) -> bool {
444 unsafe { from_glib(ffi::jsc_value_is_null(self.as_ref().to_glib_none().0)) }
445 }
446
447 fn is_number(&self) -> bool {
448 unsafe { from_glib(ffi::jsc_value_is_number(self.as_ref().to_glib_none().0)) }
449 }
450
451 fn is_object(&self) -> bool {
452 unsafe { from_glib(ffi::jsc_value_is_object(self.as_ref().to_glib_none().0)) }
453 }
454
455 fn is_string(&self) -> bool {
456 unsafe { from_glib(ffi::jsc_value_is_string(self.as_ref().to_glib_none().0)) }
457 }
458
459 #[cfg(any(feature = "v2_38", feature = "dox"))]
460 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
461 fn is_typed_array(&self) -> bool {
462 unsafe {
463 from_glib(ffi::jsc_value_is_typed_array(
464 self.as_ref().to_glib_none().0,
465 ))
466 }
467 }
468
469 fn is_undefined(&self) -> bool {
470 unsafe { from_glib(ffi::jsc_value_is_undefined(self.as_ref().to_glib_none().0)) }
471 }
472
473 fn object_define_property_data(
480 &self,
481 property_name: &str,
482 flags: ValuePropertyFlags,
483 property_value: Option<&impl IsA<Value>>,
484 ) {
485 unsafe {
486 ffi::jsc_value_object_define_property_data(
487 self.as_ref().to_glib_none().0,
488 property_name.to_glib_none().0,
489 flags.into_glib(),
490 property_value.map(|p| p.as_ref()).to_glib_none().0,
491 );
492 }
493 }
494
495 fn object_delete_property(&self, name: &str) -> bool {
496 unsafe {
497 from_glib(ffi::jsc_value_object_delete_property(
498 self.as_ref().to_glib_none().0,
499 name.to_glib_none().0,
500 ))
501 }
502 }
503
504 fn object_enumerate_properties(&self) -> Vec<glib::GString> {
505 unsafe {
506 FromGlibPtrContainer::from_glib_full(ffi::jsc_value_object_enumerate_properties(
507 self.as_ref().to_glib_none().0,
508 ))
509 }
510 }
511
512 fn object_get_property(&self, name: &str) -> Option<Value> {
513 unsafe {
514 from_glib_full(ffi::jsc_value_object_get_property(
515 self.as_ref().to_glib_none().0,
516 name.to_glib_none().0,
517 ))
518 }
519 }
520
521 fn object_get_property_at_index(&self, index: u32) -> Option<Value> {
522 unsafe {
523 from_glib_full(ffi::jsc_value_object_get_property_at_index(
524 self.as_ref().to_glib_none().0,
525 index,
526 ))
527 }
528 }
529
530 fn object_has_property(&self, name: &str) -> bool {
531 unsafe {
532 from_glib(ffi::jsc_value_object_has_property(
533 self.as_ref().to_glib_none().0,
534 name.to_glib_none().0,
535 ))
536 }
537 }
538
539 fn object_invoke_methodv(&self, name: &str, parameters: &[Value]) -> Option<Value> {
544 let n_parameters = parameters.len() as _;
545 unsafe {
546 from_glib_full(ffi::jsc_value_object_invoke_methodv(
547 self.as_ref().to_glib_none().0,
548 name.to_glib_none().0,
549 n_parameters,
550 parameters.to_glib_none().0,
551 ))
552 }
553 }
554
555 fn object_is_instance_of(&self, name: &str) -> bool {
556 unsafe {
557 from_glib(ffi::jsc_value_object_is_instance_of(
558 self.as_ref().to_glib_none().0,
559 name.to_glib_none().0,
560 ))
561 }
562 }
563
564 fn object_set_property(&self, name: &str, property: &impl IsA<Value>) {
565 unsafe {
566 ffi::jsc_value_object_set_property(
567 self.as_ref().to_glib_none().0,
568 name.to_glib_none().0,
569 property.as_ref().to_glib_none().0,
570 );
571 }
572 }
573
574 fn object_set_property_at_index(&self, index: u32, property: &impl IsA<Value>) {
575 unsafe {
576 ffi::jsc_value_object_set_property_at_index(
577 self.as_ref().to_glib_none().0,
578 index,
579 property.as_ref().to_glib_none().0,
580 );
581 }
582 }
583
584 fn to_boolean(&self) -> bool {
585 unsafe { from_glib(ffi::jsc_value_to_boolean(self.as_ref().to_glib_none().0)) }
586 }
587
588 fn to_double(&self) -> f64 {
589 unsafe { ffi::jsc_value_to_double(self.as_ref().to_glib_none().0) }
590 }
591
592 fn to_int32(&self) -> i32 {
593 unsafe { ffi::jsc_value_to_int32(self.as_ref().to_glib_none().0) }
594 }
595
596 #[cfg(any(feature = "v2_28", feature = "dox"))]
597 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_28")))]
598 fn to_json(&self, indent: u32) -> Option<glib::GString> {
599 unsafe {
600 from_glib_full(ffi::jsc_value_to_json(
601 self.as_ref().to_glib_none().0,
602 indent,
603 ))
604 }
605 }
606
607 fn to_str(&self) -> glib::GString {
608 unsafe { from_glib_full(ffi::jsc_value_to_string(self.as_ref().to_glib_none().0)) }
609 }
610
611 fn to_string_as_bytes(&self) -> Option<glib::Bytes> {
612 unsafe {
613 from_glib_full(ffi::jsc_value_to_string_as_bytes(
614 self.as_ref().to_glib_none().0,
615 ))
616 }
617 }
618
619 #[cfg(any(feature = "v2_38", feature = "dox"))]
620 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
621 fn typed_array_get_buffer(&self) -> Option<Value> {
622 unsafe {
623 from_glib_full(ffi::jsc_value_typed_array_get_buffer(
624 self.as_ref().to_glib_none().0,
625 ))
626 }
627 }
628
629 #[cfg(any(feature = "v2_38", feature = "dox"))]
636 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
637 fn typed_array_get_length(&self) -> usize {
638 unsafe { ffi::jsc_value_typed_array_get_length(self.as_ref().to_glib_none().0) }
639 }
640
641 #[cfg(any(feature = "v2_38", feature = "dox"))]
642 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
643 fn typed_array_get_offset(&self) -> usize {
644 unsafe { ffi::jsc_value_typed_array_get_offset(self.as_ref().to_glib_none().0) }
645 }
646
647 #[cfg(any(feature = "v2_38", feature = "dox"))]
648 #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
649 fn typed_array_get_size(&self) -> usize {
650 unsafe { ffi::jsc_value_typed_array_get_size(self.as_ref().to_glib_none().0) }
651 }
652
653 }