use std::cell::RefCell;
use std::rc::Rc;
use engine::Value;
use engine::builtins::typedarray::ElementType;
use engine::interp::{Interp, JsResult, Signal};
use engine::object::{BufferStorage, JsObject};
use js_gc::Gc;
use crate::buffers::typed_array_kind;
pub const UINT8: ElementType = ElementType::Uint8;
pub fn new_array_buffer(interp: &mut Interp<'static>, bytes: Vec<u8>) -> Gc<JsObject> {
let proto = interp
.realm
.array_buffer_proto
.clone()
.unwrap_or_else(|| interp.realm.object_proto.clone());
let object = interp.new_object(Some(proto));
object.set_array_buffer(Rc::new(RefCell::new(BufferStorage::fixed(bytes))));
object
}
pub fn new_array_buffer_with_release(
interp: &mut Interp<'static>,
bytes: Vec<u8>,
release: Box<dyn FnOnce()>,
) -> Gc<JsObject> {
let proto = interp
.realm
.array_buffer_proto
.clone()
.unwrap_or_else(|| interp.realm.object_proto.clone());
let object = interp.new_object(Some(proto));
object.set_array_buffer(Rc::new(RefCell::new(BufferStorage::fixed_with_release(
bytes, release,
))));
object
}
pub fn new_view(
interp: &mut Interp<'static>,
buffer: &Gc<JsObject>,
element: ElementType,
offset: usize,
length: usize,
) -> Gc<JsObject> {
let index = engine::builtins::typedarray::ELEMENT_TYPES
.iter()
.position(|(kind, _)| *kind == element)
.unwrap_or(0);
let proto = interp
.realm
.typed_array_protos
.get(index)
.cloned()
.or_else(|| interp.realm.typed_array_proto.clone())
.unwrap_or_else(|| interp.realm.object_proto.clone());
let object = interp.new_object(Some(proto));
object.set_typed_array(buffer.clone(), element, offset, length);
object
}
pub fn construct_typed_array(
interp: &mut Interp<'static>,
element: ElementType,
args: Vec<Value>,
) -> JsResult<Value> {
let global = Value::Object(interp.realm.global.clone());
let Value::Object(constructor) = interp.read_field(&global, element.name())? else {
return Err(Signal::Throw(interp.make_error(
"TypeError",
&format!("{} is not installed", element.name()),
)));
};
let callee = Value::Object(constructor);
interp.construct(callee.clone(), callee, args)
}
pub fn kind_from_element(element: ElementType) -> i32 {
match element {
ElementType::Uint8Clamped => typed_array_kind::UINT8C,
ElementType::Int8 => typed_array_kind::INT8,
ElementType::Uint8 => typed_array_kind::UINT8,
ElementType::Int16 => typed_array_kind::INT16,
ElementType::Uint16 => typed_array_kind::UINT16,
ElementType::Int32 => typed_array_kind::INT32,
ElementType::Uint32 => typed_array_kind::UINT32,
ElementType::BigInt64 => typed_array_kind::BIG_INT64,
ElementType::BigUint64 => typed_array_kind::BIG_UINT64,
ElementType::Float16 => typed_array_kind::FLOAT16,
ElementType::Float32 => typed_array_kind::FLOAT32,
ElementType::Float64 => typed_array_kind::FLOAT64,
}
}
pub fn element_from_kind(kind: i32) -> Option<ElementType> {
Some(match kind {
typed_array_kind::UINT8C => ElementType::Uint8Clamped,
typed_array_kind::INT8 => ElementType::Int8,
typed_array_kind::UINT8 => ElementType::Uint8,
typed_array_kind::INT16 => ElementType::Int16,
typed_array_kind::UINT16 => ElementType::Uint16,
typed_array_kind::INT32 => ElementType::Int32,
typed_array_kind::UINT32 => ElementType::Uint32,
typed_array_kind::BIG_INT64 => ElementType::BigInt64,
typed_array_kind::BIG_UINT64 => ElementType::BigUint64,
typed_array_kind::FLOAT16 => ElementType::Float16,
typed_array_kind::FLOAT32 => ElementType::Float32,
typed_array_kind::FLOAT64 => ElementType::Float64,
_ => return None,
})
}