use crate::{
ecmascript::{
Agent, ExceptionType, Function, JsResult, Object, ProtoIntrinsics,
builtins::{
ordinary::ordinary_create_from_constructor, shared_array_buffer::SharedArrayBuffer,
},
create_shared_byte_data_block,
},
engine::{Bindable, GcScope},
};
mod shared_array_buffer_constructor;
mod shared_array_buffer_prototype;
pub(crate) use shared_array_buffer_constructor::*;
pub(crate) use shared_array_buffer_prototype::*;
fn allocate_shared_array_buffer<'a>(
agent: &mut Agent,
constructor: Object,
byte_length: u64,
max_byte_length: Option<u64>,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, SharedArrayBuffer<'a>> {
let constructor = constructor.bind(gc.nogc());
if let Some(max_byte_length) = max_byte_length
&& byte_length > max_byte_length
{
return Err(agent.throw_exception_with_static_message(
ExceptionType::RangeError,
"byte length is larger than maximum byte length",
gc.into_nogc(),
));
}
let Object::SharedArrayBuffer(obj) = ordinary_create_from_constructor(
agent,
Function::try_from(constructor).unwrap().unbind(),
ProtoIntrinsics::SharedArrayBuffer,
gc.reborrow(),
)
.unbind()?
else {
unreachable!()
};
let gc = gc.into_nogc();
let obj = obj.bind(gc);
let block = unsafe { create_shared_byte_data_block(agent, byte_length, max_byte_length, gc) }?;
unsafe { obj.set_data_block(agent, block) };
Ok(obj)
}