Skip to content

Commit

Permalink
Set up global ReadableStream etc classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Jan 1, 2025
1 parent 748f62f commit 88eb7d4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions llrt_core/src/module_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl Default for ModuleBuilder {
.with_global(crate::modules::url::init)
.with_module(UrlModule)
.with_module(StreamWebModule)
.with_global(crate::modules::stream_web::init)
.with_global(crate::modules::http::init)
.with_global(crate::modules::exceptions::init)
.with_module(LlrtHexModule)
Expand Down
37 changes: 32 additions & 5 deletions modules/llrt_stream_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{cell::Cell, rc::Rc};
use llrt_utils::module::{export_default, ModuleInfo};
use queueing_strategy::{ByteLengthQueuingStrategy, CountQueuingStrategy};
use readable::{
ReadableByteStreamController, ReadableStream, ReadableStreamBYOBRequest, ReadableStreamClass,
ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader,
ReadableStreamBYOBRequest, ReadableStreamClass, ReadableStreamDefaultController,
ReadableStreamDefaultReader,
};
use rquickjs::{
Expand Down Expand Up @@ -46,36 +47,43 @@ impl<'js> FromJs<'js> for ReadableWritablePair<'js> {

pub struct StreamWebModule;

// https://nodejs.org/api/webstreams.html
impl ModuleDef for StreamWebModule {
fn declare(declare: &Declarations) -> Result<()> {
declare.declare(stringify!(ReadableStream))?;
declare.declare(stringify!(ByteLengthQueuingStrategy))?;
declare.declare(stringify!(CountQueuingStrategy))?;
declare.declare(stringify!(ReadableStreamDefaultReader))?;
declare.declare(stringify!(ReadableStreamBYOBReader))?;
declare.declare(stringify!(ReadableStreamDefaultController))?;
declare.declare(stringify!(ReadableByteStreamController))?;
declare.declare(stringify!(ReadableStreamBYOBRequest))?;

declare.declare(stringify!(WritableStream))?;
declare.declare(stringify!(WritableStreamDefaultWriter))?;
declare.declare(stringify!(WritableStreamDefaultController))?;

declare.declare(stringify!(ByteLengthQueuingStrategy))?;
declare.declare(stringify!(CountQueuingStrategy))?;

declare.declare("default")?;
Ok(())
}

fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
export_default(ctx, exports, |default| {
Class::<ReadableStream>::define(default)?;
Class::<ByteLengthQueuingStrategy>::define(default)?;
Class::<CountQueuingStrategy>::define(default)?;
Class::<ReadableStreamDefaultReader>::define(default)?;
Class::<ReadableStreamBYOBReader>::define(default)?;
Class::<ReadableStreamDefaultController>::define(default)?;
Class::<ReadableByteStreamController>::define(default)?;
Class::<ReadableStreamBYOBRequest>::define(default)?;

Class::<WritableStream>::define(default)?;
Class::<WritableStreamDefaultWriter>::define(default)?;
Class::<WritableStreamDefaultController>::define(default)?;

Class::<ByteLengthQueuingStrategy>::define(default)?;
Class::<CountQueuingStrategy>::define(default)?;

Ok(())
})?;

Expand All @@ -92,6 +100,25 @@ impl From<StreamWebModule> for ModuleInfo<StreamWebModule> {
}
}

pub fn init(ctx: &Ctx) -> Result<()> {
let globals = &ctx.globals();

// https://min-common-api.proposal.wintercg.org/#api-index
Class::<ByteLengthQueuingStrategy>::define(globals)?;
Class::<CountQueuingStrategy>::define(globals)?;

Class::<ReadableByteStreamController>::define(globals)?;
Class::<ReadableStream>::define(globals)?;
Class::<ReadableStreamBYOBReader>::define(globals)?;
Class::<ReadableStreamBYOBRequest>::define(globals)?;
Class::<ReadableStreamDefaultReader>::define(globals)?;

Class::<WritableStream>::define(globals)?;
Class::<WritableStreamDefaultController>::define(globals)?;

Ok(())
}

/// Helper type for treating an undefined value as None, but not null
#[derive(Clone)]
struct Undefined<T>(pub Option<T>);
Expand Down
2 changes: 1 addition & 1 deletion modules/llrt_stream_web/src/readable/default_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{class_from_owned_borrow_mut, is_non_negative_number, upon_promise, V

#[derive(JsLifetime, Trace)]
#[rquickjs::class]
pub(super) struct ReadableStreamDefaultController<'js> {
pub(crate) struct ReadableStreamDefaultController<'js> {
cancel_algorithm: Option<CancelAlgorithm<'js>>,
close_requested: bool,
pull_again: bool,
Expand Down
4 changes: 2 additions & 2 deletions modules/llrt_stream_web/src/readable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ mod tee;

pub(crate) use byob_reader::ReadableStreamBYOBReader;
pub(crate) use byte_controller::{ReadableByteStreamController, ReadableStreamBYOBRequest};
pub(crate) use default_controller::ReadableStreamDefaultController;
pub(crate) use default_reader::ReadableStreamDefaultReader;

use crate::readable::byob_reader::ReadableStreamBYOBReaderOwned;
use crate::readable::byte_controller::{
ReadableByteStreamControllerClass, ReadableByteStreamControllerOwned,
};
use crate::readable::default_controller::{
ReadableStreamDefaultController, ReadableStreamDefaultControllerClass,
ReadableStreamDefaultControllerOwned,
ReadableStreamDefaultControllerClass, ReadableStreamDefaultControllerOwned,
};
use crate::readable::default_reader::ReadableStreamDefaultReaderOwned;

Expand Down
9 changes: 0 additions & 9 deletions tests/unit/a-web-platform-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ const runTest = (test, done) => {
debug: process.env.DEBUG !== undefined,
});

globalThis.ReadableStream = ReadableStream;
globalThis.ByteLengthQueuingStrategy = ByteLengthQueuingStrategy;
globalThis.CountQueuingStrategy = CountQueuingStrategy;
globalThis.ReadableStreamDefaultReader = ReadableStreamDefaultReader;
globalThis.ReadableByteStreamController = ReadableByteStreamController;
globalThis.ReadableStreamBYOBRequest = ReadableStreamBYOBRequest;

globalThis.WritableStream = WritableStream;

globalThis.gc = globalThis.__gc;

context.add_completion_callback((tests, status, assertions) => {
Expand Down

0 comments on commit 88eb7d4

Please sign in to comment.