Add array methods

This commit is contained in:
Wu Yu Wei
2023-03-20 16:16:37 +08:00
parent 2abceb23da
commit 32fa2b5fc3
5 changed files with 139 additions and 34 deletions

View File

@@ -23,5 +23,7 @@ glib = "^0.16.0"
version = "0.5" version = "0.5"
[features] [features]
default = [ "v2_38" ]
dox = [ "ffi/dox" ] dox = [ "ffi/dox" ]
v2_28 = [ "ffi/v2_28" ] v2_28 = [ "ffi/v2_28" ]
v2_38 = [ "ffi/v2_38", "v2_28" ]

View File

@@ -45,6 +45,18 @@ status = "generate"
generate_builder = true generate_builder = true
name = "JavaScriptCore.Value" name = "JavaScriptCore.Value"
status = "generate" 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]] # [[object.function]]
# ignore = true # ignore = true

View File

@@ -27,18 +27,6 @@ impl Value {
pub const NONE: Option<&'static Value> = None; pub const NONE: Option<&'static Value> = None;
//#[doc(alias = "jsc_value_new_array")]
//pub fn new_array(context: &impl IsA<Context>, 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<Context>, data: /*Unimplemented*/Option<Basic: Pointer>, size: usize, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Option<Value> {
// unsafe { TODO: call ffi:jsc_value_new_array_buffer() }
//}
#[doc(alias = "jsc_value_new_array_from_garray")] #[doc(alias = "jsc_value_new_array_from_garray")]
pub fn new_array_from_garray(context: &impl IsA<Context>, array: &[Value]) -> Value { pub fn new_array_from_garray(context: &impl IsA<Context>, array: &[Value]) -> Value {
unsafe { unsafe {
@@ -188,11 +176,6 @@ if let Some(ref context) = self.context {
} }
pub trait ValueExt: 'static { 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<Basic: Pointer>;
#[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg(any(feature = "v2_38", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
#[doc(alias = "jsc_value_array_buffer_get_size")] #[doc(alias = "jsc_value_array_buffer_get_size")]
@@ -328,11 +311,6 @@ pub trait ValueExt: 'static {
#[must_use] #[must_use]
fn typed_array_get_buffer(&self) -> Option<Value>; fn typed_array_get_buffer(&self) -> Option<Value>;
//#[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<Basic: Pointer>, usize);
#[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg(any(feature = "v2_38", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
#[doc(alias = "jsc_value_typed_array_get_length")] #[doc(alias = "jsc_value_typed_array_get_length")]
@@ -355,12 +333,6 @@ pub trait ValueExt: 'static {
} }
impl<O: IsA<Value>> ValueExt for O { impl<O: IsA<Value>> 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<Basic: Pointer> {
// unsafe { TODO: call ffi:jsc_value_array_buffer_get_data() }
//}
#[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg(any(feature = "v2_38", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
fn array_buffer_get_size(&self) -> usize { fn array_buffer_get_size(&self) -> usize {
@@ -590,12 +562,6 @@ impl<O: IsA<Value>> 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<Basic: Pointer>, usize) {
// unsafe { TODO: call ffi:jsc_value_typed_array_get_data() }
//}
#[cfg(any(feature = "v2_38", feature = "dox"))] #[cfg(any(feature = "v2_38", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))] #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_38")))]
fn typed_array_get_length(&self) -> usize { fn typed_array_get_length(&self) -> usize {

View File

@@ -6,3 +6,5 @@
mod auto; mod auto;
pub use auto::{traits::*, *}; pub use auto::{traits::*, *};
mod value;
pub use value::*;

123
src/value.rs Normal file
View File

@@ -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<Context>, data: glib::Bytes) -> Option<Value> {
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<O: IsA<Value>> 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]),
}