1#[cfg(feature = "v2_38")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
7use crate::TypedArrayType;
8use crate::{Context, ValuePropertyFlags};
9use glib::{prelude::*, translate::*};
10
11glib::wrapper! {
12 #[doc(alias = "JSCValue")]
13 pub struct Value(Object<ffi::JSCValue, ffi::JSCValueClass>);
14
15 match fn {
16 type_ => || ffi::jsc_value_get_type(),
17 }
18}
19
20impl Value {
21 pub const NONE: Option<&'static Value> = None;
22
23 #[doc(alias = "jsc_value_new_array_from_garray")]
24 pub fn new_array_from_garray(context: &impl IsA<Context>, array: &[Value]) -> Value {
25 unsafe {
26 from_glib_full(ffi::jsc_value_new_array_from_garray(
27 context.as_ref().to_glib_none().0,
28 array.to_glib_none().0,
29 ))
30 }
31 }
32
33 #[doc(alias = "jsc_value_new_array_from_strv")]
34 pub fn new_array_from_strv(context: &impl IsA<Context>, strv: &[&str]) -> Value {
35 unsafe {
36 from_glib_full(ffi::jsc_value_new_array_from_strv(
37 context.as_ref().to_glib_none().0,
38 strv.to_glib_none().0,
39 ))
40 }
41 }
42
43 #[doc(alias = "jsc_value_new_boolean")]
44 pub fn new_boolean(context: &impl IsA<Context>, value: bool) -> Value {
45 unsafe {
46 from_glib_full(ffi::jsc_value_new_boolean(
47 context.as_ref().to_glib_none().0,
48 value.into_glib(),
49 ))
50 }
51 }
52
53 #[cfg(feature = "v2_28")]
54 #[cfg_attr(docsrs, doc(cfg(feature = "v2_28")))]
55 #[doc(alias = "jsc_value_new_from_json")]
56 #[doc(alias = "new_from_json")]
57 pub fn from_json(context: &impl IsA<Context>, json: &str) -> Value {
58 unsafe {
59 from_glib_full(ffi::jsc_value_new_from_json(
60 context.as_ref().to_glib_none().0,
61 json.to_glib_none().0,
62 ))
63 }
64 }
65
66 #[doc(alias = "jsc_value_new_null")]
82 pub fn new_null(context: &impl IsA<Context>) -> Value {
83 unsafe { from_glib_full(ffi::jsc_value_new_null(context.as_ref().to_glib_none().0)) }
84 }
85
86 #[doc(alias = "jsc_value_new_number")]
87 pub fn new_number(context: &impl IsA<Context>, number: f64) -> Value {
88 unsafe {
89 from_glib_full(ffi::jsc_value_new_number(
90 context.as_ref().to_glib_none().0,
91 number,
92 ))
93 }
94 }
95
96 #[doc(alias = "jsc_value_new_string")]
102 pub fn new_string(context: &impl IsA<Context>, string: Option<&str>) -> Value {
103 unsafe {
104 from_glib_full(ffi::jsc_value_new_string(
105 context.as_ref().to_glib_none().0,
106 string.to_glib_none().0,
107 ))
108 }
109 }
110
111 #[doc(alias = "jsc_value_new_string_from_bytes")]
112 pub fn new_string_from_bytes(context: &impl IsA<Context>, bytes: Option<&glib::Bytes>) -> Value {
113 unsafe {
114 from_glib_full(ffi::jsc_value_new_string_from_bytes(
115 context.as_ref().to_glib_none().0,
116 bytes.to_glib_none().0,
117 ))
118 }
119 }
120
121 #[cfg(feature = "v2_38")]
122 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
123 #[doc(alias = "jsc_value_new_typed_array")]
124 pub fn new_typed_array(
125 context: &impl IsA<Context>,
126 type_: TypedArrayType,
127 length: usize,
128 ) -> Value {
129 unsafe {
130 from_glib_full(ffi::jsc_value_new_typed_array(
131 context.as_ref().to_glib_none().0,
132 type_.into_glib(),
133 length,
134 ))
135 }
136 }
137
138 #[doc(alias = "jsc_value_new_undefined")]
139 pub fn new_undefined(context: &impl IsA<Context>) -> Value {
140 unsafe {
141 from_glib_full(ffi::jsc_value_new_undefined(
142 context.as_ref().to_glib_none().0,
143 ))
144 }
145 }
146
147 pub fn builder() -> ValueBuilder {
152 ValueBuilder::new()
153 }
154}
155
156impl std::fmt::Display for Value {
157 #[inline]
158 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
159 f.write_str(&ValueExt::to_str(self))
160 }
161}
162
163#[must_use = "The builder must be built to be used"]
168pub struct ValueBuilder {
169 builder: glib::object::ObjectBuilder<'static, Value>,
170}
171
172impl ValueBuilder {
173 fn new() -> Self {
174 Self {
175 builder: glib::object::Object::builder(),
176 }
177 }
178
179 pub fn context(self, context: &impl IsA<Context>) -> Self {
180 Self {
181 builder: self.builder.property("context", context.clone().upcast()),
182 }
183 }
184
185 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
188 pub fn build(self) -> Value {
189 self.builder.build()
190 }
191}
192
193mod sealed {
194 pub trait Sealed {}
195 impl<T: super::IsA<super::Value>> Sealed for T {}
196}
197
198pub trait ValueExt: IsA<Value> + sealed::Sealed + 'static {
199 #[cfg(feature = "v2_38")]
200 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
201 #[doc(alias = "jsc_value_array_buffer_get_size")]
202 fn array_buffer_get_size(&self) -> usize {
203 unsafe { ffi::jsc_value_array_buffer_get_size(self.as_ref().to_glib_none().0) }
204 }
205
206 #[doc(alias = "jsc_value_constructor_callv")]
213 #[must_use]
214 fn constructor_callv(&self, parameters: &[Value]) -> Option<Value> {
215 let n_parameters = parameters.len() as _;
216 unsafe {
217 from_glib_full(ffi::jsc_value_constructor_callv(
218 self.as_ref().to_glib_none().0,
219 n_parameters,
220 parameters.to_glib_none().0,
221 ))
222 }
223 }
224
225 #[doc(alias = "jsc_value_function_callv")]
232 #[must_use]
233 fn function_callv(&self, parameters: &[Value]) -> Option<Value> {
234 let n_parameters = parameters.len() as _;
235 unsafe {
236 from_glib_full(ffi::jsc_value_function_callv(
237 self.as_ref().to_glib_none().0,
238 n_parameters,
239 parameters.to_glib_none().0,
240 ))
241 }
242 }
243
244 #[doc(alias = "jsc_value_get_context")]
245 #[doc(alias = "get_context")]
246 fn context(&self) -> Option<Context> {
247 unsafe { from_glib_none(ffi::jsc_value_get_context(self.as_ref().to_glib_none().0)) }
248 }
249
250 #[doc(alias = "jsc_value_is_array")]
251 fn is_array(&self) -> bool {
252 unsafe { from_glib(ffi::jsc_value_is_array(self.as_ref().to_glib_none().0)) }
253 }
254
255 #[cfg(feature = "v2_38")]
256 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
257 #[doc(alias = "jsc_value_is_array_buffer")]
258 fn is_array_buffer(&self) -> bool {
259 unsafe {
260 from_glib(ffi::jsc_value_is_array_buffer(
261 self.as_ref().to_glib_none().0,
262 ))
263 }
264 }
265
266 #[doc(alias = "jsc_value_is_boolean")]
267 fn is_boolean(&self) -> bool {
268 unsafe { from_glib(ffi::jsc_value_is_boolean(self.as_ref().to_glib_none().0)) }
269 }
270
271 #[doc(alias = "jsc_value_is_constructor")]
272 fn is_constructor(&self) -> bool {
273 unsafe {
274 from_glib(ffi::jsc_value_is_constructor(
275 self.as_ref().to_glib_none().0,
276 ))
277 }
278 }
279
280 #[doc(alias = "jsc_value_is_function")]
281 fn is_function(&self) -> bool {
282 unsafe { from_glib(ffi::jsc_value_is_function(self.as_ref().to_glib_none().0)) }
283 }
284
285 #[doc(alias = "jsc_value_is_null")]
286 fn is_null(&self) -> bool {
287 unsafe { from_glib(ffi::jsc_value_is_null(self.as_ref().to_glib_none().0)) }
288 }
289
290 #[doc(alias = "jsc_value_is_number")]
291 fn is_number(&self) -> bool {
292 unsafe { from_glib(ffi::jsc_value_is_number(self.as_ref().to_glib_none().0)) }
293 }
294
295 #[doc(alias = "jsc_value_is_object")]
296 fn is_object(&self) -> bool {
297 unsafe { from_glib(ffi::jsc_value_is_object(self.as_ref().to_glib_none().0)) }
298 }
299
300 #[doc(alias = "jsc_value_is_string")]
301 fn is_string(&self) -> bool {
302 unsafe { from_glib(ffi::jsc_value_is_string(self.as_ref().to_glib_none().0)) }
303 }
304
305 #[cfg(feature = "v2_38")]
306 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
307 #[doc(alias = "jsc_value_is_typed_array")]
308 fn is_typed_array(&self) -> bool {
309 unsafe {
310 from_glib(ffi::jsc_value_is_typed_array(
311 self.as_ref().to_glib_none().0,
312 ))
313 }
314 }
315
316 #[doc(alias = "jsc_value_is_undefined")]
317 fn is_undefined(&self) -> bool {
318 unsafe { from_glib(ffi::jsc_value_is_undefined(self.as_ref().to_glib_none().0)) }
319 }
320
321 #[cfg(feature = "v2_38")]
322 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
323 #[doc(alias = "jsc_value_new_typed_array_with_buffer")]
324 #[must_use]
325 fn new_typed_array_with_buffer(
326 &self,
327 type_: TypedArrayType,
328 offset: usize,
329 length: isize,
330 ) -> Option<Value> {
331 unsafe {
332 from_glib_full(ffi::jsc_value_new_typed_array_with_buffer(
333 self.as_ref().to_glib_none().0,
334 type_.into_glib(),
335 offset,
336 length,
337 ))
338 }
339 }
340
341 #[doc(alias = "jsc_value_object_define_property_data")]
347 fn object_define_property_data(
348 &self,
349 property_name: &str,
350 flags: ValuePropertyFlags,
351 property_value: Option<&impl IsA<Value>>,
352 ) {
353 unsafe {
354 ffi::jsc_value_object_define_property_data(
355 self.as_ref().to_glib_none().0,
356 property_name.to_glib_none().0,
357 flags.into_glib(),
358 property_value.map(|p| p.as_ref()).to_glib_none().0,
359 );
360 }
361 }
362
363 #[doc(alias = "jsc_value_object_delete_property")]
364 fn object_delete_property(&self, name: &str) -> bool {
365 unsafe {
366 from_glib(ffi::jsc_value_object_delete_property(
367 self.as_ref().to_glib_none().0,
368 name.to_glib_none().0,
369 ))
370 }
371 }
372
373 #[doc(alias = "jsc_value_object_enumerate_properties")]
374 fn object_enumerate_properties(&self) -> Vec<glib::GString> {
375 unsafe {
376 FromGlibPtrContainer::from_glib_full(ffi::jsc_value_object_enumerate_properties(
377 self.as_ref().to_glib_none().0,
378 ))
379 }
380 }
381
382 #[doc(alias = "jsc_value_object_get_property")]
383 #[must_use]
384 fn object_get_property(&self, name: &str) -> Option<Value> {
385 unsafe {
386 from_glib_full(ffi::jsc_value_object_get_property(
387 self.as_ref().to_glib_none().0,
388 name.to_glib_none().0,
389 ))
390 }
391 }
392
393 #[doc(alias = "jsc_value_object_get_property_at_index")]
394 #[must_use]
395 fn object_get_property_at_index(&self, index: u32) -> Option<Value> {
396 unsafe {
397 from_glib_full(ffi::jsc_value_object_get_property_at_index(
398 self.as_ref().to_glib_none().0,
399 index,
400 ))
401 }
402 }
403
404 #[doc(alias = "jsc_value_object_has_property")]
405 fn object_has_property(&self, name: &str) -> bool {
406 unsafe {
407 from_glib(ffi::jsc_value_object_has_property(
408 self.as_ref().to_glib_none().0,
409 name.to_glib_none().0,
410 ))
411 }
412 }
413
414 #[doc(alias = "jsc_value_object_invoke_methodv")]
421 #[must_use]
422 fn object_invoke_methodv(&self, name: &str, parameters: &[Value]) -> Option<Value> {
423 let n_parameters = parameters.len() as _;
424 unsafe {
425 from_glib_full(ffi::jsc_value_object_invoke_methodv(
426 self.as_ref().to_glib_none().0,
427 name.to_glib_none().0,
428 n_parameters,
429 parameters.to_glib_none().0,
430 ))
431 }
432 }
433
434 #[doc(alias = "jsc_value_object_is_instance_of")]
435 fn object_is_instance_of(&self, name: &str) -> bool {
436 unsafe {
437 from_glib(ffi::jsc_value_object_is_instance_of(
438 self.as_ref().to_glib_none().0,
439 name.to_glib_none().0,
440 ))
441 }
442 }
443
444 #[doc(alias = "jsc_value_object_set_property")]
445 fn object_set_property(&self, name: &str, property: &impl IsA<Value>) {
446 unsafe {
447 ffi::jsc_value_object_set_property(
448 self.as_ref().to_glib_none().0,
449 name.to_glib_none().0,
450 property.as_ref().to_glib_none().0,
451 );
452 }
453 }
454
455 #[doc(alias = "jsc_value_object_set_property_at_index")]
456 fn object_set_property_at_index(&self, index: u32, property: &impl IsA<Value>) {
457 unsafe {
458 ffi::jsc_value_object_set_property_at_index(
459 self.as_ref().to_glib_none().0,
460 index,
461 property.as_ref().to_glib_none().0,
462 );
463 }
464 }
465
466 #[doc(alias = "jsc_value_to_boolean")]
467 fn to_boolean(&self) -> bool {
468 unsafe { from_glib(ffi::jsc_value_to_boolean(self.as_ref().to_glib_none().0)) }
469 }
470
471 #[doc(alias = "jsc_value_to_double")]
472 fn to_double(&self) -> f64 {
473 unsafe { ffi::jsc_value_to_double(self.as_ref().to_glib_none().0) }
474 }
475
476 #[doc(alias = "jsc_value_to_int32")]
477 fn to_int32(&self) -> i32 {
478 unsafe { ffi::jsc_value_to_int32(self.as_ref().to_glib_none().0) }
479 }
480
481 #[cfg(feature = "v2_28")]
482 #[cfg_attr(docsrs, doc(cfg(feature = "v2_28")))]
483 #[doc(alias = "jsc_value_to_json")]
484 fn to_json(&self, indent: u32) -> Option<glib::GString> {
485 unsafe {
486 from_glib_full(ffi::jsc_value_to_json(
487 self.as_ref().to_glib_none().0,
488 indent,
489 ))
490 }
491 }
492
493 #[doc(alias = "jsc_value_to_string")]
494 #[doc(alias = "to_string")]
495 fn to_str(&self) -> glib::GString {
496 unsafe { from_glib_full(ffi::jsc_value_to_string(self.as_ref().to_glib_none().0)) }
497 }
498
499 #[doc(alias = "jsc_value_to_string_as_bytes")]
500 fn to_string_as_bytes(&self) -> Option<glib::Bytes> {
501 unsafe {
502 from_glib_full(ffi::jsc_value_to_string_as_bytes(
503 self.as_ref().to_glib_none().0,
504 ))
505 }
506 }
507
508 #[cfg(feature = "v2_38")]
509 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
510 #[doc(alias = "jsc_value_typed_array_get_buffer")]
511 #[must_use]
512 fn typed_array_get_buffer(&self) -> Option<Value> {
513 unsafe {
514 from_glib_full(ffi::jsc_value_typed_array_get_buffer(
515 self.as_ref().to_glib_none().0,
516 ))
517 }
518 }
519
520 #[cfg(feature = "v2_38")]
521 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
522 #[doc(alias = "jsc_value_typed_array_get_length")]
523 fn typed_array_get_length(&self) -> usize {
524 unsafe { ffi::jsc_value_typed_array_get_length(self.as_ref().to_glib_none().0) }
525 }
526
527 #[cfg(feature = "v2_38")]
528 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
529 #[doc(alias = "jsc_value_typed_array_get_offset")]
530 fn typed_array_get_offset(&self) -> usize {
531 unsafe { ffi::jsc_value_typed_array_get_offset(self.as_ref().to_glib_none().0) }
532 }
533
534 #[cfg(feature = "v2_38")]
535 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
536 #[doc(alias = "jsc_value_typed_array_get_size")]
537 fn typed_array_get_size(&self) -> usize {
538 unsafe { ffi::jsc_value_typed_array_get_size(self.as_ref().to_glib_none().0) }
539 }
540
541 #[cfg(feature = "v2_38")]
542 #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
543 #[doc(alias = "jsc_value_typed_array_get_type")]
544 fn typed_array_get_type(&self) -> TypedArrayType {
545 unsafe {
546 from_glib(ffi::jsc_value_typed_array_get_type(
547 self.as_ref().to_glib_none().0,
548 ))
549 }
550 }
551}
552
553impl<O: IsA<Value>> ValueExt for O {}