Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/engine/src/object/builtins/jsarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ impl JsArray {
Self::from_object(object)
}

/// Calls `Array.prototype.values()`.
#[inline]
pub fn values(&self, context: &mut Context) -> JsResult<JsValue> {
Array::values(&self.inner.clone().into(), &[], context)
}

/// Calls `Array.prototype.splice()`.
///
/// Removes and/or inserts elements from the array, returning the removed elements.
Expand Down
51 changes: 48 additions & 3 deletions core/runtime/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ use crate::fetch::headers::JsHeaders;
use crate::fetch::request::{JsRequest, RequestInit};
use crate::fetch::response::JsResponse;
use boa_engine::class::Class;
use boa_engine::object::FunctionObjectBuilder;
use boa_engine::object::builtins::JsArray;
use boa_engine::property::PropertyDescriptor;
use boa_engine::realm::Realm;
use boa_engine::{
Context, Finalize, JsData, JsError, JsObject, JsResult, JsString, JsValue, NativeObject, Trace,
boa_module, js_error,
Context, Finalize, JsData, JsError, JsObject, JsResult, JsString, JsSymbol, JsValue,
NativeObject, Trace, boa_module, js_error, js_string, native_function::NativeFunction,
};
use either::Either;
use http::{HeaderName, HeaderValue, Request as HttpRequest, Request};
Expand Down Expand Up @@ -176,6 +179,20 @@ pub mod js_module {
#[doc(inline)]
pub use js_module::fetch;

fn headers_iterator(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let this_object = this.as_object();
let headers = this_object
.as_ref()
.and_then(JsObject::downcast_ref::<JsHeaders>)
.ok_or_else(|| {
js_error!(TypeError: "`Headers.prototype[Symbol.iterator]` requires a `Headers` object")
})?;

let entries = headers.entries(context);
let entries_array = JsArray::from_object(entries.to_object(context)?)?;
entries_array.values(context)
}

/// Register the `fetch` function in the realm, as well as ALL supporting classes.
/// Pass `None` as the realm to register globally.
///
Expand All @@ -191,7 +208,35 @@ pub fn register<F: Fetcher>(
} else {
context.insert_data(FetcherRc(Rc::new(fetcher)));
}
js_module::boa_register::<F>(realm, context)?;
js_module::boa_register::<F>(realm.clone(), context)?;

// TODO(#4688): Replace this manual `[Symbol.iterator]` wiring once `#[boa(class)]`
// supports symbol-named methods.
let headers_proto = match realm {
Some(realm) => realm.get_class::<JsHeaders>(),
None => context.get_global_class::<JsHeaders>(),
}
.ok_or_else(|| js_error!(Error: "Headers class should be registered"))?
.prototype();

let iterator = FunctionObjectBuilder::new(
context.realm(),
NativeFunction::from_fn_ptr(headers_iterator),
)
.name(js_string!("[Symbol.iterator]"))
.length(0)
.constructor(false)
.build();

headers_proto.define_property_or_throw(
JsSymbol::iterator(),
PropertyDescriptor::builder()
.value(iterator)
.writable(true)
.enumerable(false)
.configurable(true),
context,
)?;

Ok(())
}
26 changes: 26 additions & 0 deletions core/runtime/src/fetch/tests/headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::TestFetcher;
use crate::test::{TestAction, run_test_actions};

fn register(ctx: &mut boa_engine::Context) {
crate::fetch::register(TestFetcher::default(), None, ctx).expect("failed to register fetch");
}

#[test]
fn headers_are_iterable() {
run_test_actions([
TestAction::harness(),
TestAction::inspect_context(register),
TestAction::run(
r#"
const headers = new Headers([["x", "y"]]);
const entries = [...headers];
assertEq(entries.length, 1);
assertEq(entries[0][0], "x");
assertEq(entries[0][1], "y");

const map = new Map(headers);
assertEq(map.get("x"), "y");
"#,
),
]);
}
2 changes: 2 additions & 0 deletions core/runtime/src/fetch/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::rc::Rc;
#[cfg(test)]
mod e2e;
#[cfg(test)]
mod headers;
#[cfg(test)]
mod request;
#[cfg(test)]
mod response;
Expand Down
Loading