avm2: Add read_buffer and implement bytesAvailable

This commit is contained in:
sleepycatcoding 2023-07-14 15:41:45 +03:00 committed by Nathan Adams
parent 487d881793
commit 2dcc3e7930
3 changed files with 20 additions and 4 deletions

View File

@ -39,10 +39,7 @@ package flash.net {
stub_method("flash.net.Socket", "close");
}
public function get bytesAvailable():uint {
stub_getter("flash.net.Socket", "bytesAvailable");
return 0;
}
public native function get bytesAvailable():uint;
public function get bytesPending():uint {
stub_getter("flash.net.Socket", "bytesPending");

View File

@ -44,6 +44,18 @@ pub fn connect<'gc>(
Ok(Value::Undefined)
}
pub fn get_bytes_available<'gc>(
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(socket) = this.as_socket() {
return Ok(socket.read_buffer().len().into());
}
Ok(Value::Undefined)
}
pub fn get_endian<'gc>(
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,

View File

@ -8,6 +8,7 @@ use gc_arena::barrier::unlock;
use gc_arena::{lock::RefLock, Collect, Gc};
use gc_arena::{GcWeak, Mutation};
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::VecDeque;
use std::fmt;
/// A class instance allocator that allocates ShaderData objects.
@ -24,6 +25,7 @@ pub fn socket_allocator<'gc>(
// Default endianness is Big.
endian: Cell::new(Endian::Big),
handle: Cell::new(None),
read_buffer: RefCell::new(VecDeque::new()),
write_buffer: RefCell::new(vec![]),
},
))
@ -77,6 +79,10 @@ impl<'gc> SocketObject<'gc> {
self.0.handle.replace(Some(handle))
}
pub fn read_buffer(&self) -> RefMut<'_, VecDeque<u8>> {
self.0.read_buffer.borrow_mut()
}
pub fn write_bytes(&self, bytes: &[u8]) {
self.0.write_buffer.borrow_mut().extend_from_slice(bytes)
}
@ -96,6 +102,7 @@ pub struct SocketObjectData<'gc> {
#[collect(require_static)]
handle: Cell<Option<SocketHandle>>,
endian: Cell<Endian>,
read_buffer: RefCell<VecDeque<u8>>,
write_buffer: RefCell<Vec<u8>>,
}