use std::rc::Rc;
use dom_struct::dom_struct;
use js::jsapi::{CallArgs, JSContext};
use js::jsval::{Int32Value, JSVal};
use js::rust::HandleObject;
use super::bindings::codegen::Bindings::FunctionBinding::Function;
use super::bindings::codegen::Bindings::QueuingStrategyBinding::{
CountQueuingStrategyMethods, QueuingStrategy, QueuingStrategyInit, QueuingStrategySize,
};
use super::bindings::import::module::{DomObject, DomRoot, Error, Fallible, Reflector};
use super::bindings::reflector::reflect_dom_object_with_proto;
use super::types::GlobalScope;
use crate::script_runtime::CanGc;
use crate::{native_fn, native_raw_obj_fn};
#[dom_struct]
pub struct CountQueuingStrategy {
reflector_: Reflector,
high_water_mark: f64,
}
impl CountQueuingStrategy {
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 CountQueuingStrategyMethods<crate::DomTypeHolder> for CountQueuingStrategy {
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_count_queuing_strategy_size() {
return Ok(fun);
}
let fun = native_fn!(count_queuing_strategy_size, c"size", 0, 0);
global.set_count_queuing_strategy_size(fun.clone());
Ok(fun)
}
}
#[allow(unsafe_code)]
pub unsafe fn count_queuing_strategy_size(_cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
let args = CallArgs::from_vp(vp, argc);
args.rval().set(Int32Value(1));
true
}
pub fn extract_high_water_mark(strategy: &QueuingStrategy, default_hwm: f64) -> Result<f64, Error> {
if strategy.highWaterMark.is_none() {
return Ok(default_hwm);
}
let high_water_mark = strategy.highWaterMark.unwrap();
if high_water_mark.is_nan() || high_water_mark < 0.0 {
return Err(Error::Range(
"High water mark must be a non-negative number.".to_string(),
));
}
Ok(high_water_mark)
}
pub fn extract_size_algorithm(strategy: &QueuingStrategy) -> Rc<QueuingStrategySize> {
if strategy.size.is_none() {
let cx = GlobalScope::get_cx();
let fun_obj = native_raw_obj_fn!(cx, count_queuing_strategy_size, c"size", 0, 0);
#[allow(unsafe_code)]
unsafe {
return QueuingStrategySize::new(cx, fun_obj);
};
}
strategy.size.as_ref().unwrap().clone()
}