From 32fa2b5fc33039c19ab67024f753dbe545937096 Mon Sep 17 00:00:00 2001 From: Wu Yu Wei Date: Mon, 20 Mar 2023 16:16:37 +0800 Subject: [PATCH] Add array methods --- Cargo.toml | 2 + Gir.toml | 12 +++++ src/auto/value.rs | 34 ------------- src/lib.rs | 2 + src/value.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 34 deletions(-) create mode 100644 src/value.rs diff --git a/Cargo.toml b/Cargo.toml index a248f38..5c8aa97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,7 @@ glib = "^0.16.0" version = "0.5" [features] +default = [ "v2_38" ] dox = [ "ffi/dox" ] v2_28 = [ "ffi/v2_28" ] +v2_38 = [ "ffi/v2_38", "v2_28" ] diff --git a/Gir.toml b/Gir.toml index ba884ed..a9412eb 100644 --- a/Gir.toml +++ b/Gir.toml @@ -45,6 +45,18 @@ status = "generate" generate_builder = true name = "JavaScriptCore.Value" status = "generate" + [[object.function]] + name = "new_array" + ignore = true + [[object.function]] + name = "new_array_buffer" + manual = true + [[object.function]] + name = "array_buffer_get_data" + manual = true + [[object.function]] + name = "typed_array_get_data" + manual = true # [[object.function]] # ignore = true diff --git a/src/auto/value.rs b/src/auto/value.rs index 1d04f8f..1eb4f74 100644 --- a/src/auto/value.rs +++ b/src/auto/value.rs @@ -27,18 +27,6 @@ impl Value { pub const NONE: Option<&'static Value> = None; - //#[doc(alias = "jsc_value_new_array")] - //pub fn new_array(context: &impl IsA, first_item_type: glib::types::Type, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) -> Value { - // unsafe { TODO: call ffi:jsc_value_new_array() } - //} - - //#[cfg(any(feature = "v2_38", feature = "dox"))] - //#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] - //#[doc(alias = "jsc_value_new_array_buffer")] - //pub fn new_array_buffer(context: &impl IsA, data: /*Unimplemented*/Option, size: usize, user_data: /*Unimplemented*/Option) -> Option { - // unsafe { TODO: call ffi:jsc_value_new_array_buffer() } - //} - #[doc(alias = "jsc_value_new_array_from_garray")] pub fn new_array_from_garray(context: &impl IsA, array: &[Value]) -> Value { unsafe { @@ -188,11 +176,6 @@ if let Some(ref context) = self.context { } pub trait ValueExt: 'static { - //#[cfg(any(feature = "v2_38", feature = "dox"))] - //#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] - //#[doc(alias = "jsc_value_array_buffer_get_data")] - //fn array_buffer_get_data(&self, size: usize) -> /*Unimplemented*/Option; - #[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[doc(alias = "jsc_value_array_buffer_get_size")] @@ -328,11 +311,6 @@ pub trait ValueExt: 'static { #[must_use] fn typed_array_get_buffer(&self) -> Option; - //#[cfg(any(feature = "v2_38", feature = "dox"))] - //#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] - //#[doc(alias = "jsc_value_typed_array_get_data")] - //fn typed_array_get_data(&self) -> (/*Unimplemented*/Option, usize); - #[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[doc(alias = "jsc_value_typed_array_get_length")] @@ -355,12 +333,6 @@ pub trait ValueExt: 'static { } impl> ValueExt for O { - //#[cfg(any(feature = "v2_38", feature = "dox"))] - //#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] - //fn array_buffer_get_data(&self, size: usize) -> /*Unimplemented*/Option { - // unsafe { TODO: call ffi:jsc_value_array_buffer_get_data() } - //} - #[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] fn array_buffer_get_size(&self) -> usize { @@ -590,12 +562,6 @@ impl> ValueExt for O { } } - //#[cfg(any(feature = "v2_38", feature = "dox"))] - //#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] - //fn typed_array_get_data(&self) -> (/*Unimplemented*/Option, usize) { - // unsafe { TODO: call ffi:jsc_value_typed_array_get_data() } - //} - #[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] fn typed_array_get_length(&self) -> usize { diff --git a/src/lib.rs b/src/lib.rs index 005c8f9..ec2f084 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,5 @@ mod auto; pub use auto::{traits::*, *}; +mod value; +pub use value::*; diff --git a/src/value.rs b/src/value.rs new file mode 100644 index 0000000..a132b4d --- /dev/null +++ b/src/value.rs @@ -0,0 +1,123 @@ +use crate::Context; +use crate::Value; +use crate::ValueExt; +use glib::ffi::GBytes; +use glib::object::IsA; +use glib::translate::*; +use std::ffi::c_void; +use std::slice; + +impl Value { + #[cfg(any(feature = "v2_38", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] + #[doc(alias = "jsc_value_new_array_buffer")] + pub fn new_array_buffer(context: &impl IsA, data: glib::Bytes) -> Option { + let len = data.len(); + let ptr: *mut GBytes = data.to_glib_full(); + + unsafe extern "C" fn destroy_notify(user_data: *mut c_void) { + let data: glib::Bytes = from_glib_full(user_data as *mut GBytes); + drop(data); + } + + unsafe { + from_glib_full(ffi::jsc_value_new_array_buffer( + context.as_ref().to_glib_none().0, + ptr as _, + len, + Some(destroy_notify), + ptr as _, + )) + } + } +} + +pub trait ValueExtManual: 'static { + #[cfg(any(feature = "v2_38", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] + #[doc(alias = "jsc_value_array_buffer_get_data")] + fn array_buffer_get_data(&self) -> &[u8]; + + #[cfg(any(feature = "v2_38", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] + #[doc(alias = "jsc_value_typed_array_get_data")] + fn typed_array_get_data(&self) -> TypedArrayData; +} + +impl> ValueExtManual for O { + #[cfg(any(feature = "v2_38", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] + fn array_buffer_get_data(&self) -> &[u8] { + unsafe { + let mut len = 0; + let ptr = ffi::jsc_value_array_buffer_get_data(self.as_ref().to_glib_none().0, &mut len); + if ptr.is_null() || len == 0 { + &[] + } else { + slice::from_raw_parts(ptr as *const u8, len) + } + } + } + + #[cfg(any(feature = "v2_38", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] + fn typed_array_get_data(&self) -> TypedArrayData { + use crate::TypedArrayType::*; + unsafe { + let mut len = 0; + let ptr = ffi::jsc_value_typed_array_get_data(self.as_ref().to_glib_none().0, &mut len); + if ptr.is_null() || len == 0 { + TypedArrayData::None + } else { + match self.typed_array_get_type() { + None => TypedArrayData::None, + Int8 => TypedArrayData::Int8(slice::from_raw_parts(ptr as *const i8, len)), + Int16 => TypedArrayData::Int16(slice::from_raw_parts(ptr as *const i16, len)), + Int32 => TypedArrayData::Int32(slice::from_raw_parts(ptr as *const i32, len)), + Int64 => TypedArrayData::Int64(slice::from_raw_parts(ptr as *const i64, len)), + Uint8 => TypedArrayData::Uint8(slice::from_raw_parts(ptr as *const u8, len)), + Uint8Clamped => { + TypedArrayData::Uint8Clamped(slice::from_raw_parts(ptr as *const u8, len)) + } + Uint16 => TypedArrayData::Uint16(slice::from_raw_parts(ptr as *const u16, len)), + Uint32 => TypedArrayData::Uint32(slice::from_raw_parts(ptr as *const u32, len)), + Uint64 => TypedArrayData::Uint64(slice::from_raw_parts(ptr as *const u64, len)), + Float32 => TypedArrayData::Float32(slice::from_raw_parts(ptr as *const f32, len)), + Float64 => TypedArrayData::Float64(slice::from_raw_parts(ptr as *const f64, len)), + __Unknown(_) => TypedArrayData::None, + } + } + } + } +} + +#[cfg(any(feature = "v2_38", feature = "dox"))] +#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum TypedArrayData<'a> { + #[doc(alias = "JSC_TYPED_ARRAY_NONE")] + None, + #[doc(alias = "JSC_TYPED_ARRAY_INT8")] + Int8(&'a [i8]), + #[doc(alias = "JSC_TYPED_ARRAY_INT16")] + Int16(&'a [i16]), + #[doc(alias = "JSC_TYPED_ARRAY_INT32")] + Int32(&'a [i32]), + #[doc(alias = "JSC_TYPED_ARRAY_INT64")] + Int64(&'a [i64]), + #[doc(alias = "JSC_TYPED_ARRAY_UINT8")] + Uint8(&'a [u8]), + #[doc(alias = "JSC_TYPED_ARRAY_UINT8_CLAMPED")] + Uint8Clamped(&'a [u8]), + #[doc(alias = "JSC_TYPED_ARRAY_UINT16")] + Uint16(&'a [u16]), + #[doc(alias = "JSC_TYPED_ARRAY_UINT32")] + Uint32(&'a [u32]), + #[doc(alias = "JSC_TYPED_ARRAY_UINT64")] + Uint64(&'a [u64]), + #[doc(alias = "JSC_TYPED_ARRAY_FLOAT32")] + Float32(&'a [f32]), + #[doc(alias = "JSC_TYPED_ARRAY_FLOAT64")] + Float64(&'a [f64]), +}