Nacre/vendor/servo-script/dom/stream/bytelengthqueuingstrategy.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept
the functions of the timers still waiting. Heavy-web reruns and cargo test still pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 20:38:47 -06:00

120 lines
4.2 KiB
Rust

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use js::context::JSContext;
use js::error::throw_type_error;
use js::jsapi::CallArgs;
use js::jsval::{JSVal, UndefinedValue};
use js::rust::HandleObject;
use script_bindings::callback::RootedCallback;
use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::codegen::Bindings::QueuingStrategyBinding::{
ByteLengthQueuingStrategyMethods, QueuingStrategyInit,
};
use crate::dom::bindings::conversions::get_property_jsval;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::DomRoot;
use crate::dom::types::GlobalScope;
use crate::native_fn;
#[dom_struct]
pub(crate) struct ByteLengthQueuingStrategy {
reflector_: Reflector,
high_water_mark: f64,
}
impl ByteLengthQueuingStrategy {
pub(crate) fn new_inherited(init: f64) -> Self {
Self {
reflector_: Reflector::new(),
high_water_mark: init,
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
init: f64,
) -> DomRoot<Self> {
reflect_dom_object_with_proto(cx, Box::new(Self::new_inherited(init)), global, proto)
}
}
impl ByteLengthQueuingStrategyMethods<crate::DomTypeHolder> for ByteLengthQueuingStrategy {
/// <https://streams.spec.whatwg.org/#blqs-constructor>
fn Constructor(
cx: &mut JSContext,
global: &GlobalScope,
proto: Option<HandleObject>,
init: &QueuingStrategyInit,
) -> DomRoot<Self> {
Self::new(cx, global, proto, init.highWaterMark)
}
/// <https://streams.spec.whatwg.org/#blqs-high-water-mark>
fn HighWaterMark(&self) -> f64 {
self.high_water_mark
}
/// <https://streams.spec.whatwg.org/#blqs-size>
fn GetSize(&self, cx: &mut js::context::JSContext) -> Fallible<RootedCallback<Function>> {
let global = self.global();
// Return this's relevant global object's byte length queuing strategy
// size function.
if let Some(fun) = global.get_byte_length_queuing_strategy_size(cx) {
return Ok(fun);
}
// Step 1. Let steps be the following steps, given chunk
// Note: See ByteLengthQueuingStrategySize instead.
// Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
// globalObject’s relevant Realm).
let fun = native_fn!(cx, byte_length_queuing_strategy_size, c"size", 1, 0);
// Step 3. Set globalObject’s byte length queuing strategy size function to
// a Function that represents a reference to F,
// with callback context equal to globalObject's relevant settings object.
global.set_byte_length_queuing_strategy_size(fun.clone());
Ok(fun)
}
}
/// <https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function>
fn byte_length_queuing_strategy_size(cx: &mut js::context::JSContext, args: CallArgs) -> bool {
// Step 1. Let steps be the following steps, given chunk:
// Step 1.1. Return ? GetV(chunk, "byteLength").
rooted!(&in(cx) let chunk = args.get(0).get());
// https://tc39.es/ecma262/#sec-getv
// Let O be ? ToObject(V).
if chunk.is_undefined() || chunk.is_null() {
throw_type_error(
cx,
c"ByteLengthQueuingStrategy size called with undefined or nulll",
);
return false;
}
if !chunk.is_object() {
// Return ? O.[[Get]]("byteLength", V).
// undefined for primitives without the property.
args.rval().set(UndefinedValue());
return true;
}
rooted!(&in(cx) let object = chunk.to_object());
// Return ? O.[[Get]](P, V).
rooted!(&in(cx) let mut byte_length = UndefinedValue());
if get_property_jsval(cx, object.handle(), c"byteLength", byte_length.handle_mut()).is_err() {
return false;
}
args.rval().set(byte_length.get());
true
}