use std::rc::Rc;
use dom_struct::dom_struct;
use js::gc::{HandleValue, MutableHandleValue};
use js::jsapi::{CallArgs, JSContext};
use js::jsval::JSVal;
use js::rust::HandleObject;
use super::bindings::codegen::Bindings::FunctionBinding::Function;
use super::bindings::codegen::Bindings::QueuingStrategyBinding::{
ByteLengthQueuingStrategyMethods, QueuingStrategyInit,
};
use super::bindings::import::module::{DomObject, DomRoot, Fallible, Reflector};
use super::bindings::reflector::reflect_dom_object_with_proto;
use super::types::GlobalScope;
use crate::dom::bindings::import::module::get_dictionary_property;
use crate::native_fn;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct ByteLengthQueuingStrategy {
reflector_: Reflector,
high_water_mark: f64,
}
impl ByteLengthQueuingStrategy {
pub fn new_inherited(init: f64) -> Self {
Self {
reflector_: Reflector::new(),
high_water_mark: init,
}
}
pub fn new(
global: &GlobalScope,
proto: Option<HandleObject>,
init: f64,
can_gc: CanGc,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(Box::new(Self::new_inherited(init)), global, proto, can_gc)
}
}
impl ByteLengthQueuingStrategyMethods<crate::DomTypeHolder> for ByteLengthQueuingStrategy {
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
init: &QueuingStrategyInit,
) -> DomRoot<Self> {
Self::new(global, proto, init.highWaterMark, can_gc)
}
fn HighWaterMark(&self) -> f64 {
self.high_water_mark
}
fn GetSize(&self) -> Fallible<Rc<Function>> {
let global = self.reflector_.global();
if let Some(fun) = global.get_byte_length_queuing_strategy_size() {
return Ok(fun);
}
let fun = native_fn!(byte_length_queuing_strategy_size, c"size", 1, 0);
global.set_byte_length_queuing_strategy_size(fun.clone());
Ok(fun)
}
}
#[allow(unsafe_code)]
pub unsafe fn byte_length_queuing_strategy_size(
cx: *mut JSContext,
argc: u32,
vp: *mut JSVal,
) -> bool {
let args = CallArgs::from_vp(vp, argc);
let val = HandleValue::from_raw(args.get(0));
if !val.is_object() {
return false;
}
rooted!(in(cx) let object = val.to_object());
get_dictionary_property(
cx,
object.handle(),
"byteLength",
MutableHandleValue::from_raw(args.rval()),
)
.unwrap_or(false)
}