javascriptcore/
value_ref.rs

1// // Copyright 2013-2017, The Gtk-rs Project Developers.
2// // See the COPYRIGHT file at the top-level directory of this distribution.
3// // Licensed under the MIT license, see the LICENSE file or <https://opensource.org/licenses/MIT>
4#![cfg_attr(feature = "dox", feature(doc_cfg))]
5
6use ffi::*;
7use glib::translate::*;
8use std::ptr;
9
10use crate::GlobalContextRef;
11
12pub struct ValueRef {
13    raw: JSValueRef,
14}
15
16impl ValueRef {
17    pub fn is_boolean(&self, context: &GlobalContextRef) -> bool {
18        unsafe { JSValueIsBoolean(context.to_glib_none().0, self.raw) != 0 }
19    }
20
21    pub fn is_null(&self, context: &GlobalContextRef) -> bool {
22        unsafe { JSValueIsNull(context.to_glib_none().0, self.raw) != 0 }
23    }
24
25    pub fn is_undefined(&self, context: &GlobalContextRef) -> bool {
26        unsafe { JSValueIsUndefined(context.to_glib_none().0, self.raw) != 0 }
27    }
28
29    pub fn is_number(&self, context: &GlobalContextRef) -> bool {
30        unsafe { JSValueIsNumber(context.to_glib_none().0, self.raw) != 0 }
31    }
32
33    pub fn is_string(&self, context: &GlobalContextRef) -> bool {
34        unsafe { JSValueIsString(context.to_glib_none().0, self.raw) != 0 }
35    }
36
37    pub fn is_object(&self, context: &GlobalContextRef) -> bool {
38        unsafe { JSValueIsObject(context.to_glib_none().0, self.raw) != 0 }
39    }
40
41    pub fn is_array(&self, context: &GlobalContextRef) -> bool {
42        unsafe { JSValueIsArray(context.to_glib_none().0, self.raw) != 0 }
43    }
44
45    pub fn is_date(&self, context: &GlobalContextRef) -> bool {
46        unsafe { JSValueIsDate(context.to_glib_none().0, self.raw) != 0 }
47    }
48
49    pub fn to_number(&self, context: &GlobalContextRef) -> Option<f64> {
50        let mut exception = ptr::null_mut();
51        let result = unsafe { JSValueToNumber(context.to_glib_none().0, self.raw, &mut exception) };
52        if exception.is_null() {
53            Some(result)
54        } else {
55            None
56        }
57    }
58
59    pub fn to_boolean(&self, context: &GlobalContextRef) -> bool {
60        unsafe { JSValueToBoolean(context.to_glib_none().0, self.raw) != 0 }
61    }
62
63    pub fn to_string(&self, context: &GlobalContextRef) -> Option<String> {
64        unsafe {
65            let mut exception = ptr::null_mut();
66            let jsstring = JSValueToStringCopy(context.to_glib_none().0, self.raw, &mut exception);
67
68            if exception.is_null() {
69                let cap = JSStringGetMaximumUTF8CStringSize(jsstring);
70                let mut buf = Vec::<u8>::with_capacity(cap);
71                let len = JSStringGetUTF8CString(jsstring, buf.as_mut_ptr() as _, cap);
72                JSStringRelease(jsstring);
73                buf.set_len(len - 1);
74                String::from_utf8(buf).ok()
75            } else {
76                None
77            }
78        }
79    }
80}
81
82impl FromGlibPtrNone<JSValueRef> for ValueRef {
83    unsafe fn from_glib_none(ptr: JSValueRef) -> Self {
84        ValueRef { raw: ptr }
85    }
86}
87
88impl FromGlibPtrFull<JSValueRef> for ValueRef {
89    unsafe fn from_glib_full(ptr: JSValueRef) -> Self {
90        ValueRef { raw: ptr }
91    }
92}
93
94impl<'a> ToGlibPtr<'a, JSValueRef> for ValueRef {
95    type Storage = ();
96
97    #[inline]
98    fn to_glib_none(&self) -> Stash<'a, JSValueRef, ValueRef> {
99        Stash(self.raw, ())
100    }
101}