javascriptcore/
value.rs

1use crate::Context;
2use crate::Value;
3use crate::ValueExt;
4use glib::ffi::GBytes;
5use glib::object::IsA;
6use glib::translate::*;
7use std::ffi::c_void;
8use std::slice;
9
10impl Value {
11  #[cfg(any(feature = "v2_38", docsrs))]
12  #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
13  #[doc(alias = "jsc_value_new_array_buffer")]
14  pub fn new_array_buffer(context: &impl IsA<Context>, data: glib::Bytes) -> Option<Value> {
15    let len = data.len();
16    let ptr: *mut GBytes = data.to_glib_full();
17
18    unsafe extern "C" fn destroy_notify(user_data: *mut c_void) {
19      let data: glib::Bytes = from_glib_full(user_data as *mut GBytes);
20      drop(data);
21    }
22
23    unsafe {
24      from_glib_full(ffi::jsc_value_new_array_buffer(
25        context.as_ref().to_glib_none().0,
26        ptr as _,
27        len,
28        Some(destroy_notify),
29        ptr as _,
30      ))
31    }
32  }
33}
34
35pub trait ValueExtManual: 'static {
36  #[cfg(any(feature = "v2_38", docsrs))]
37  #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
38  #[doc(alias = "jsc_value_array_buffer_get_data")]
39  fn array_buffer_get_data(&self) -> &[u8];
40
41  #[cfg(any(feature = "v2_38", docsrs))]
42  #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
43  #[doc(alias = "jsc_value_typed_array_get_data")]
44  fn typed_array_get_data(&self) -> TypedArrayData;
45}
46
47impl<O: IsA<Value>> ValueExtManual for O {
48  #[cfg(any(feature = "v2_38", docsrs))]
49  #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
50  fn array_buffer_get_data(&self) -> &[u8] {
51    unsafe {
52      let mut len = 0;
53      let ptr = ffi::jsc_value_array_buffer_get_data(self.as_ref().to_glib_none().0, &mut len);
54      if ptr.is_null() || len == 0 {
55        &[]
56      } else {
57        slice::from_raw_parts(ptr as *const u8, len)
58      }
59    }
60  }
61
62  #[cfg(any(feature = "v2_38", docsrs))]
63  #[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
64  fn typed_array_get_data(&self) -> TypedArrayData {
65    use crate::TypedArrayType::*;
66    unsafe {
67      let mut len = 0;
68      let ptr = ffi::jsc_value_typed_array_get_data(self.as_ref().to_glib_none().0, &mut len);
69      if ptr.is_null() || len == 0 {
70        TypedArrayData::None
71      } else {
72        match self.typed_array_get_type() {
73          None => TypedArrayData::None,
74          Int8 => TypedArrayData::Int8(slice::from_raw_parts(ptr as *const i8, len)),
75          Int16 => TypedArrayData::Int16(slice::from_raw_parts(ptr as *const i16, len)),
76          Int32 => TypedArrayData::Int32(slice::from_raw_parts(ptr as *const i32, len)),
77          Int64 => TypedArrayData::Int64(slice::from_raw_parts(ptr as *const i64, len)),
78          Uint8 => TypedArrayData::Uint8(slice::from_raw_parts(ptr as *const u8, len)),
79          Uint8Clamped => {
80            TypedArrayData::Uint8Clamped(slice::from_raw_parts(ptr as *const u8, len))
81          }
82          Uint16 => TypedArrayData::Uint16(slice::from_raw_parts(ptr as *const u16, len)),
83          Uint32 => TypedArrayData::Uint32(slice::from_raw_parts(ptr as *const u32, len)),
84          Uint64 => TypedArrayData::Uint64(slice::from_raw_parts(ptr as *const u64, len)),
85          Float32 => TypedArrayData::Float32(slice::from_raw_parts(ptr as *const f32, len)),
86          Float64 => TypedArrayData::Float64(slice::from_raw_parts(ptr as *const f64, len)),
87          __Unknown(_) => TypedArrayData::None,
88        }
89      }
90    }
91  }
92}
93
94#[cfg(any(feature = "v2_38", docsrs))]
95#[cfg_attr(docsrs, doc(cfg(feature = "v2_38")))]
96#[derive(Debug, Clone, Copy)]
97#[non_exhaustive]
98pub enum TypedArrayData<'a> {
99  #[doc(alias = "JSC_TYPED_ARRAY_NONE")]
100  None,
101  #[doc(alias = "JSC_TYPED_ARRAY_INT8")]
102  Int8(&'a [i8]),
103  #[doc(alias = "JSC_TYPED_ARRAY_INT16")]
104  Int16(&'a [i16]),
105  #[doc(alias = "JSC_TYPED_ARRAY_INT32")]
106  Int32(&'a [i32]),
107  #[doc(alias = "JSC_TYPED_ARRAY_INT64")]
108  Int64(&'a [i64]),
109  #[doc(alias = "JSC_TYPED_ARRAY_UINT8")]
110  Uint8(&'a [u8]),
111  #[doc(alias = "JSC_TYPED_ARRAY_UINT8_CLAMPED")]
112  Uint8Clamped(&'a [u8]),
113  #[doc(alias = "JSC_TYPED_ARRAY_UINT16")]
114  Uint16(&'a [u16]),
115  #[doc(alias = "JSC_TYPED_ARRAY_UINT32")]
116  Uint32(&'a [u32]),
117  #[doc(alias = "JSC_TYPED_ARRAY_UINT64")]
118  Uint64(&'a [u64]),
119  #[doc(alias = "JSC_TYPED_ARRAY_FLOAT32")]
120  Float32(&'a [f32]),
121  #[doc(alias = "JSC_TYPED_ARRAY_FLOAT64")]
122  Float64(&'a [f64]),
123}