1use std::i32;
2
3use super::*;
4
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[repr(transparent)]
8pub struct Array <'a> (NonNullFREObject, PhantomData<&'a()>);
9impl<'a> Array<'a> {
10 const CLASS: UCStr = unsafe {UCStr::from_literal_unchecked(c"Array")};
11 pub fn get_length (self) -> u32 {
12 let mut value = u32::default();
13 let r = unsafe {FREGetArrayLength(self.as_ptr(), &mut value)};
14 debug_assert!(r.is_ok());
15 value
16 }
17 pub fn set_length (self, value: u32) -> Result<(), FfiError> {
19 let r = unsafe {FRESetArrayLength(self.as_ptr(), value)};
20 if let Ok(e) = FfiError::try_from(r) {
21 Err(e)
22 } else {Ok(())}
23 }
24 pub fn get (self, index: u32) -> Object<'a> {
25 let mut obj = std::ptr::null_mut();
26 let r = unsafe {FREGetArrayElementAt(self.as_ptr(), index, &mut obj)};
27 debug_assert!(r.is_ok());
28 debug_assert!(!obj.is_null());
29 unsafe {transmute(obj)}
30 }
31 pub fn set <O: AsObject<'a>> (self, index: u32, value: O) {
32 let r = unsafe {FRESetArrayElementAt(self.as_ptr(), index, value.as_ptr())};
33 debug_assert!(r.is_ok());
34 }
35 pub fn new (ctx: &CurrentContext<'a>, num_elements: Option<NonNegativeInt>) -> Self {
36 let mut arg = as3::null;
37 let num_elements = num_elements.map(|v|{
38 arg = int::new(ctx, v.get()).as_object();
39 std::slice::from_ref(&arg)
40 });
41 let obj = Object::new(ctx, Self::CLASS, num_elements).unwrap();
42 assert!(!obj.is_null());
43 unsafe {transmute(obj)}
44 }
45 pub fn from_slice (frt: &CurrentContext<'a>, elements: &[Object<'a>]) -> Self {
46 debug_assert!(elements.len() <= i32::MAX as usize);
47 if elements.len() == 1 && elements[0].get_type() == Type::Number {
48 let arr = Self::new(frt, NonNegativeInt::new(1));
49 arr.set(0, *unsafe {elements.get_unchecked(0)});
50 arr
51 } else {
52 let obj = Object::new(frt, Self::CLASS, Some(elements)).unwrap();
53 debug_assert!(!obj.is_null());
54 unsafe {transmute(obj)}
55 }
56 }
57 pub fn extend_from_slice (self, elements: &[Object]) -> u32 {
58 const PUSH: UCStr = unsafe {UCStr::from_literal_unchecked(c"push")};
59 self.call_method(PUSH, Some(elements))
60 .unwrap()
61 .try_into()
62 .unwrap()
63 }
64 pub fn push <O: AsObject<'a>> (self, element: O) -> u32 {
65 let element = element.as_object();
66 let elements = std::slice::from_ref(&element);
67 self.extend_from_slice(elements)
68 }
69 pub fn iter(self) -> ArrayIter<'a> {ArrayIter::new(self)}
70}
71impl<'a> IntoIterator for Array<'a> {
72 type Item = Object<'a>;
73 type IntoIter = ArrayIter<'a>;
74 fn into_iter(self) -> Self::IntoIter {self.iter()}
75}
76unsafe impl<'a> AsObject<'a> for Array<'a> {const TYPE: Type = Type::Array;}
77unsafe impl<'a> TryAs<'a, Array<'a>> for Object<'a> {}
78impl From<Array<'_>> for FREObject {fn from(value: Array) -> Self {value.as_ptr()}}
79impl<'a> From<Array<'a>> for Object<'a> {fn from(value: Array<'a>) -> Self {value.as_object()}}
80impl<'a> TryFrom<Object<'a>> for Array<'a> {type Error = Type; fn try_from (value: Object<'a>) -> Result<Self, Type> {value.try_as()}}
81impl Display for Array<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}
82
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85#[repr(transparent)]
86pub struct Vector <'a> (NonNullFREObject, PhantomData<&'a()>);
87impl<'a> Vector<'a> {
88 pub fn get_length (self) -> u32 {
89 let mut value = u32::default();
90 let r = unsafe {FREGetArrayLength(self.as_ptr(), &mut value)};
91 debug_assert!(r.is_ok());
92 value
93 }
94 pub fn set_length (self, value: u32) -> Result<(), FfiError> {
96 let r = unsafe {FRESetArrayLength(self.as_ptr(), value)};
97 if let Ok(e) = FfiError::try_from(r) {
98 Err(e)
99 } else {Ok(())}
100 }
101 pub fn get (self, index: u32) -> Result<Object<'a>, FfiError> {
103 let mut obj = std::ptr::null_mut();
104 let r = unsafe {FREGetArrayElementAt(self.as_ptr(), index, &mut obj)};
105 if let Ok(e) = FfiError::try_from(r) {
106 Err(e)
107 } else {
108 debug_assert!(!obj.is_null());
109 Ok(unsafe {transmute(obj)})
110 }
111 }
112 pub fn set <O: AsObject<'a>> (self, index: u32, value: O) -> Result<(), FfiError> {
114 let r = unsafe {FRESetArrayElementAt(self.as_ptr(), index, value.as_ptr())};
115 if let Ok(e) = FfiError::try_from(r) {
116 Err(e)
117 } else {Ok(())}
118 }
119 pub fn iter(self) -> VectorIter<'a> {VectorIter::new(self)}
120}
121impl<'a> IntoIterator for Vector<'a> {
122 type Item = Object<'a>;
123 type IntoIter = VectorIter<'a>;
124 fn into_iter(self) -> Self::IntoIter {self.iter()}
125}
126unsafe impl<'a> AsObject<'a> for Vector<'a> {const TYPE: Type = Type::Vector;}
127unsafe impl<'a> TryAs<'a, Vector<'a>> for Object<'a> {}
128impl From<Vector<'_>> for FREObject {fn from(value: Vector) -> Self {value.as_ptr()}}
129impl<'a> From<Vector<'a>> for Object<'a> {fn from(value: Vector<'a>) -> Self {value.as_object()}}
130impl<'a> TryFrom<Object<'a>> for Vector<'a> {type Error = Type; fn try_from (value: Object<'a>) -> Result<Self, Type> {value.try_as()}}
131impl Display for Vector<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}
132
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135#[repr(transparent)]
136pub struct ByteArray <'a> (NonNullFREObject, PhantomData<&'a()>);
137impl<'a> ByteArray<'a> {
138 #[allow(unused_variables)]
139 pub fn new (frt: &CurrentContext<'a>, length: u32) -> Self {
140 let ptr = FREByteArray {length, bytes: std::ptr::null_mut()};
141 let mut obj = std::ptr::null_mut();
142 let r = unsafe {FRENewByteArray(transmute(&ptr), &mut obj)};
143 debug_assert!(r.is_ok());
144 assert!(!obj.is_null());
145 unsafe {transmute(obj)}
146 }
147 #[allow(unused_variables)]
148 pub fn from_bytes (frt: &CurrentContext<'a>, bytes: impl AsRef<[u8]>) -> Self {
149 let bytes = bytes.as_ref();
150 debug_assert!(bytes.len() <= u32::MAX as usize);
151 let ptr = FREByteArray {length: bytes.len() as u32, bytes: bytes.as_ptr() as FREBytes};
152 let mut obj = std::ptr::null_mut();
153 let r = unsafe {FRENewByteArray(transmute(&ptr), &mut obj)};
154 debug_assert!(r.is_ok());
155 debug_assert!(!obj.is_null());
156 unsafe {transmute(obj)}
157 }
158 pub fn with <F, R> (self, f: F) -> R
159 where F: FnOnce (&mut [u8]) -> R + Sync
160 {
161 let mut fptr = FREByteArray {length: 0, bytes: std::ptr::null_mut()};
162 let result = unsafe {FREAcquireByteArray(self.as_ptr(), &mut fptr)};
163 debug_assert!(result.is_ok());
164 let bytes = unsafe {std::slice::from_raw_parts_mut(fptr.bytes, fptr.length as usize)};
165 let r = f(bytes);
166 let result = unsafe {FREReleaseByteArray(self.as_ptr())};
167 debug_assert!(result.is_ok());
168 r
169 }
170}
171unsafe impl<'a> AsObject<'a> for ByteArray<'a> {const TYPE: Type = Type::ByteArray;}
172unsafe impl<'a> TryAs<'a, ByteArray<'a>> for Object<'a> {}
173impl From<ByteArray<'_>> for FREObject {fn from(value: ByteArray) -> Self {value.as_ptr()}}
174impl<'a> From<ByteArray<'a>> for Object<'a> {fn from(value: ByteArray<'a>) -> Self {value.as_object()}}
175impl<'a> TryFrom<Object<'a>> for ByteArray<'a> {type Error = Type; fn try_from (value: Object<'a>) -> Result<Self, Type> {value.try_as()}}
176impl Display for ByteArray<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}
177
178
179#[derive(Debug, Clone, Copy, PartialEq, Eq)]
180#[repr(transparent)]
181pub struct BitmapData <'a> (NonNullFREObject, PhantomData<&'a()>);
182impl<'a> BitmapData<'a> {
183 pub fn with <F, R> (self, f: F) -> R
185 where F: FnOnce (BitmapDataAdapter) -> R + Sync
186 {
187 let mut descriptor = FREBitmapData2::default();
188 let result = unsafe {FREAcquireBitmapData2(self.as_ptr(), &mut descriptor)};
189 assert!(result.is_ok());
190 let r = f(unsafe {BitmapDataAdapter::new(self.as_ptr(), descriptor)});
191 let result = unsafe {FREReleaseBitmapData(self.as_ptr())};
192 debug_assert!(result.is_ok());
193 r
194 }
195}
196unsafe impl<'a> AsObject<'a> for BitmapData<'a> {const TYPE: Type = Type::BitmapData;}
197unsafe impl<'a> TryAs<'a, BitmapData<'a>> for Object<'a> {}
198impl From<BitmapData<'_>> for FREObject {fn from(value: BitmapData) -> Self {value.as_ptr()}}
199impl<'a> From<BitmapData<'a>> for Object<'a> {fn from(value: BitmapData<'a>) -> Self {value.as_object()}}
200impl<'a> TryFrom<Object<'a>> for BitmapData<'a> {type Error = Type; fn try_from (value: Object<'a>) -> Result<Self, Type> {value.try_as()}}
201impl Display for BitmapData<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}
202
203
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
205#[repr(transparent)]
206pub struct Context3D <'a> (NonNullFREObject, PhantomData<&'a()>);
207impl<'a> Context3D<'a> {
208 pub fn value (self) -> FREHandle {
209 let mut handle = FREHandle::default();
210 let r = unsafe {FREGetNativeContext3DHandle(self.as_ptr(), &mut handle)};
211 debug_assert!(r.is_ok());
212 assert!(!handle.is_null());
213 handle
214 }
215}
216unsafe impl<'a> AsObject<'a> for Context3D<'a> {const TYPE: Type = Type::Context3D;}
217impl From<Context3D<'_>> for FREObject {fn from(value: Context3D) -> Self {value.as_ptr()}}
219impl<'a> From<Context3D<'a>> for Object<'a> {fn from(value: Context3D<'a>) -> Self {value.as_object()}}
220impl Display for Context3D<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}
222
223
224#[derive(Debug, Clone, Copy, PartialEq, Eq)]
225#[repr(transparent)]
226pub struct ErrorObject <'a> (NonNullFREObject, PhantomData<&'a()>);
227impl<'a> ErrorObject<'a> {
228 #[allow(non_snake_case)]
229 pub fn get_errorID(self) -> i32 {
230 const ERROR_ID: UCStr = unsafe {UCStr::from_literal_unchecked(c"errorID")};
231 let value = self.get_property(ERROR_ID).unwrap().try_into().unwrap();
232 value
233 }
234 const MESSAGE: UCStr = unsafe {UCStr::from_literal_unchecked(c"message")};
235 pub fn get_message(self) -> Option<as3::String<'a>> {
236 let value = self.get_property(Self::MESSAGE).unwrap().try_as().ok();
237 value
238 }
239 pub fn set_message(self, value: Option<as3::String>) {
240 let r = self.set_property(Self::MESSAGE, Object::from(value));
241 debug_assert!(r.is_ok());
242 }
243 const NAME: UCStr = unsafe {UCStr::from_literal_unchecked(c"name")};
244 pub fn get_name(self) -> Option<as3::String<'a>> {
245 let value = self.get_property(Self::NAME).unwrap().try_as().ok();
246 value
247 }
248 pub fn set_name(self, value: Option<as3::String>) {
249 let r = self.set_property(Self::NAME, Object::from(value));
250 debug_assert!(r.is_ok());
251 }
252 const CLASS: UCStr = unsafe {UCStr::from_literal_unchecked(c"Error")};
253 pub fn new (frt: &CurrentContext<'a>, message: Option<&str>, id: i32) -> Self {
254 let message = message.map(|s|as3::String::new(frt, s));
255 let id = int::new(frt, id);
256 let args = vec![message.into(), id.as_object()].into_boxed_slice();
257 let err_obj= Object::new(frt, Self::CLASS, Some(args.as_ref())).unwrap();
258 assert!(!err_obj.is_null());
259 unsafe {transmute(err_obj)}
260 }
261}
262unsafe impl<'a> AsObject<'a> for ErrorObject<'a> {const TYPE: Type = Type::Error;}
263impl From<ErrorObject<'_>> for FREObject {fn from(value: ErrorObject) -> Self {value.as_ptr()}}
265impl<'a> From<ErrorObject<'a>> for Object<'a> {fn from(value: ErrorObject<'a>) -> Self {value.as_object()}}
266impl Display for ErrorObject<'_> {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {Display::fmt(&self.as_object(), f)}}