mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Script: implement ReadableStreamBYOBRequest (#35074)
* Script: implement ReadableStreamBYOBReader::Read Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * implement viewed_buffer_array_byte_length and byte_length Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Correct BufferSource implemntation Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Reduce BufferSource to two variants ArrayBuffer and ArrayBufferView Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Scriptt: implement ReadableStreamBYOBRequest Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix is_detached_buffer and viewed_buffer_array_byte_length Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Use if let Some instead of unwrap() Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
parent
c0cef69108
commit
6547d35fa5
2 changed files with 68 additions and 11 deletions
|
@ -39,6 +39,19 @@ impl ReadableByteStreamController {
|
|||
) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// <https://streams.spec.whatwg.org/#readable-byte-stream-controller-respond>
|
||||
pub(crate) fn respond(&self, _bytes_written: u64) -> Fallible<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// <https://streams.spec.whatwg.org/#readable-byte-stream-controller-respond-with-new-view>
|
||||
pub(crate) fn respond_with_new_view(
|
||||
&self,
|
||||
_view: HeapBufferSource<ArrayBufferViewU8>,
|
||||
) -> Fallible<()> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadableByteStreamControllerMethods<crate::DomTypeHolder> for ReadableByteStreamController {
|
||||
|
|
|
@ -3,37 +3,81 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom_struct::dom_struct;
|
||||
use js::gc::CustomAutoRooterGuard;
|
||||
use js::jsapi::Heap;
|
||||
use js::typedarray::{ArrayBufferView, ArrayBufferViewU8};
|
||||
|
||||
use super::bindings::buffer_source::{BufferSource, HeapBufferSource};
|
||||
use crate::dom::bindings::codegen::Bindings::ReadableStreamBYOBRequestBinding::ReadableStreamBYOBRequestMethods;
|
||||
use crate::dom::bindings::import::module::{Error, Fallible};
|
||||
use crate::dom::bindings::reflector::Reflector;
|
||||
use crate::dom::bindings::root::MutNullableDom;
|
||||
use crate::dom::readablebytestreamcontroller::ReadableByteStreamController;
|
||||
use crate::dom::types::GlobalScope;
|
||||
use crate::script_runtime::JSContext as SafeJSContext;
|
||||
|
||||
/// <https://streams.spec.whatwg.org/#readablestreambyobrequest>
|
||||
#[dom_struct]
|
||||
pub(crate) struct ReadableStreamBYOBRequest {
|
||||
reflector_: Reflector,
|
||||
controller: MutNullableDom<ReadableByteStreamController>,
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
view: HeapBufferSource<ArrayBufferViewU8>,
|
||||
}
|
||||
|
||||
impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBYOBRequest {
|
||||
/// <https://streams.spec.whatwg.org/#rs-byob-request-view>
|
||||
fn GetView(&self, _cx: SafeJSContext) -> Option<js::typedarray::ArrayBufferView> {
|
||||
// TODO
|
||||
None
|
||||
// Return this.[[view]].
|
||||
self.view.buffer_to_option()
|
||||
}
|
||||
|
||||
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond>
|
||||
fn Respond(&self, _bytes_written: u64) -> Fallible<()> {
|
||||
// TODO
|
||||
Err(Error::NotFound)
|
||||
fn Respond(&self, bytes_written: u64) -> Fallible<()> {
|
||||
let cx = GlobalScope::get_cx();
|
||||
|
||||
// If this.[[controller]] is undefined, throw a TypeError exception.
|
||||
let controller = if let Some(controller) = self.controller.get() {
|
||||
controller
|
||||
} else {
|
||||
return Err(Error::Type("controller is undefined".to_owned()));
|
||||
};
|
||||
|
||||
// If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
|
||||
if self.view.is_detached_buffer(cx) {
|
||||
return Err(Error::Type("buffer is detached".to_owned()));
|
||||
}
|
||||
|
||||
// Assert: this.[[view]].[[ByteLength]] > 0.
|
||||
assert!(self.view.byte_length() > 0);
|
||||
|
||||
// Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
|
||||
assert!(self.view.viewed_buffer_array_byte_length(cx) > 0);
|
||||
|
||||
// Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
|
||||
controller.respond(bytes_written)
|
||||
}
|
||||
|
||||
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view>
|
||||
fn RespondWithNewView(
|
||||
&self,
|
||||
_view: js::gc::CustomAutoRooterGuard<js::typedarray::ArrayBufferView>,
|
||||
) -> Fallible<()> {
|
||||
// TODO
|
||||
Err(Error::NotFound)
|
||||
#[allow(unsafe_code)]
|
||||
fn RespondWithNewView(&self, view: CustomAutoRooterGuard<ArrayBufferView>) -> Fallible<()> {
|
||||
let view = HeapBufferSource::<ArrayBufferViewU8>::new(BufferSource::ArrayBufferView(
|
||||
Heap::boxed(unsafe { *view.underlying_object() }),
|
||||
));
|
||||
|
||||
// If this.[[controller]] is undefined, throw a TypeError exception.
|
||||
let controller = if let Some(controller) = self.controller.get() {
|
||||
controller
|
||||
} else {
|
||||
return Err(Error::Type("controller is undefined".to_owned()));
|
||||
};
|
||||
|
||||
// If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
|
||||
if self.view.is_detached_buffer(GlobalScope::get_cx()) {
|
||||
return Err(Error::Type("buffer is detached".to_owned()));
|
||||
}
|
||||
|
||||
// Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
|
||||
controller.respond_with_new_view(view)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue