Skip to content

Commit 71f9f44

Browse files
johnyoberivas-ligo
authored andcommitted
review: @johnyob fixes / comments
1 parent 28a057f commit 71f9f44

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

jstz_api/src/urlpattern.rs

+38-30
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use boa_engine::{
1515
object::{builtins::JsArray, Object},
1616
property::Attribute,
1717
value::TryFromJs,
18-
Context, JsArgs, JsError, JsNativeError, JsObject, JsResult, JsString, JsValue,
19-
NativeFunction,
18+
Context, JsArgs, JsError, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
2019
};
2120
use boa_gc::{empty_trace, Finalize, GcRefMut, Trace};
2221

@@ -74,13 +73,13 @@ impl JsNativeObjectToString for UrlPattern {
7473

7574
impl UrlPattern {
7675
fn process_input(
77-
string_or_init: quirks::StringOrInit,
76+
input: UrlPatternInput,
7877
base_url: Option<String>,
7978
) -> JsResult<(
8079
InnerUrlPatternMatchInput,
8180
(quirks::StringOrInit, Option<String>),
8281
)> {
83-
match quirks::process_match_input(string_or_init, base_url.as_deref()) {
82+
match quirks::process_match_input(input.0, base_url.as_deref()) {
8483
Err(e) => Err(JsError::from_native(
8584
JsNativeError::typ().with_message(e.to_string()),
8685
)),
@@ -98,16 +97,15 @@ impl UrlPattern {
9897
base_url: Option<String>,
9998
_context: &mut Context<'_>,
10099
) -> JsResult<Self> {
101-
let UrlPatternInput(string_or_init) = input;
102-
let urlpatterninit =
103-
quirks::process_construct_pattern_input(string_or_init, base_url.as_deref())
100+
let url_pattern_init =
101+
quirks::process_construct_pattern_input(input.0, base_url.as_deref())
104102
.map_err(|_| {
105103
JsError::from_native(
106104
JsNativeError::typ()
107105
.with_message("Failed to build UrlPatternInit"),
108106
)
109107
})?;
110-
let url_pattern = InnerUrlPattern::parse(urlpatterninit).map_err(|_| {
108+
let url_pattern = InnerUrlPattern::parse(url_pattern_init).map_err(|_| {
111109
JsError::from_native(
112110
JsNativeError::typ().with_message("Failed to parse UrlPatternInit"),
113111
)
@@ -153,8 +151,7 @@ impl UrlPattern {
153151
input: UrlPatternInput,
154152
base_url: Option<String>,
155153
) -> JsResult<bool> {
156-
let UrlPatternInput(string_or_init) = input;
157-
let (url_pattern_match_input, _) = Self::process_input(string_or_init, base_url)?;
154+
let (url_pattern_match_input, _) = Self::process_input(input, base_url)?;
158155

159156
self.url_pattern
160157
.test(url_pattern_match_input)
@@ -166,9 +163,8 @@ impl UrlPattern {
166163
input: UrlPatternInput,
167164
base_url: Option<String>,
168165
) -> JsResult<Option<UrlPatternResult>> {
169-
let UrlPatternInput(string_or_init) = input;
170166
let (url_pattern_match_input, (string_or_init, base_url)) =
171-
Self::process_input(string_or_init, base_url)?;
167+
Self::process_input(input, base_url)?;
172168

173169
let mut inputs: Vec<UrlPatternInput> = Vec::new();
174170
inputs.push(UrlPatternInput(string_or_init));
@@ -329,7 +325,7 @@ impl TryFromJs for UrlPatternInit {
329325
pathname: get_optional_property!(obj, "pathname", context),
330326
search: get_optional_property!(obj, "search", context),
331327
hash: get_optional_property!(obj, "hash", context),
332-
base_url: get_optional_property!(obj, "base_url", context),
328+
base_url: get_optional_property!(obj, "baseURL", context),
333329
};
334330

335331
Ok(Self(url_pattern_init))
@@ -372,46 +368,58 @@ impl From<quirks::UrlPatternInit> for UrlPatternInput {
372368

373369
impl IntoJs for UrlPatternComponentResult {
374370
fn into_js(self, context: &mut Context<'_>) -> JsValue {
375-
let UrlPatternComponentResult(url_pattern_component_result) = self;
371+
let url_pattern_component_result = self.0;
376372
let input = url_pattern_component_result.input;
377373
let groups: Vec<(String, String)> =
378374
url_pattern_component_result.groups.into_iter().collect();
379-
// Create an object with prototype set to `Object`
375+
// Create an object with prototype set to `Object.prototype`
380376
let obj = JsObject::with_object_proto(context.intrinsics());
381377
// Add data property `input` to the object
378+
// TODO: Support error handling (using TryIntoJs)
382379
let _ = obj.create_data_property(
383-
JsString::from("input"),
380+
js_string!("input"),
384381
input.into_js(context),
385382
context,
386383
);
387384
// Add data property `groups` to the object,
388-
// which is itself another object with prototype set to `Object`
385+
// which is itself another object with prototype set to `Object.prototype`
389386
let group_obj = JsObject::with_object_proto(context.intrinsics());
390387
for (key, value) in groups.iter() {
391388
let value = value.clone().into_js(context);
392-
let _ = group_obj.create_data_property(
393-
JsString::from(key.clone()),
394-
value,
395-
context,
396-
);
389+
390+
// TODO: Support error handling (using TryIntoJs)
391+
let _ =
392+
group_obj.create_data_property(js_string!(key.clone()), value, context);
397393
}
398-
let _ = obj.create_data_property(JsString::from("groups"), group_obj, context);
394+
395+
// TODO: Support error handling (using TryIntoJs)
396+
let _ = obj.create_data_property(js_string!("groups"), group_obj, context);
399397
obj.into()
400398
}
401399
}
402400

403401
impl IntoJs for UrlPatternInit {
404402
fn into_js(self, context: &mut Context<'_>) -> JsValue {
405403
let obj = JsObject::with_object_proto(context.intrinsics());
406-
let UrlPatternInit(init) = self;
404+
let init = self.0;
407405

408406
macro_rules! create_data_properties_if_some {
409407
($obj:ident, $init:ident, $field:ident, $context:ident) => {
410-
let property_name = stringify!($field);
411408
if let Some(s) = $init.$field {
409+
// TODO: Support error handling (using TryIntoJs)
410+
let _ = $obj.create_data_property(
411+
js_string!(stringify!($field)),
412+
js_string!(s),
413+
$context,
414+
);
415+
}
416+
};
417+
($obj:ident, $init:ident, $field:ident, $property_name:literal, $context:ident) => {
418+
if let Some(s) = $init.$field {
419+
// TODO: Support error handling (using TryIntoJs)
412420
let _ = $obj.create_data_property(
413-
JsString::from(property_name),
414-
JsString::from(s),
421+
js_string!($property_name),
422+
js_string!(s),
415423
$context,
416424
);
417425
}
@@ -426,6 +434,7 @@ impl IntoJs for UrlPatternInit {
426434
create_data_properties_if_some!(obj, init, pathname, context);
427435
create_data_properties_if_some!(obj, init, search, context);
428436
create_data_properties_if_some!(obj, init, hash, context);
437+
create_data_properties_if_some!(obj, init, base_url, "baseURL", context);
429438

430439
obj.into()
431440
}
@@ -438,10 +447,9 @@ impl IntoJs for UrlPatternResult {
438447

439448
macro_rules! create_data_property {
440449
($obj:ident, $inner:ident, $field:ident, $context:ident) => {
441-
let property_name = stringify!($field);
442450
let $field = UrlPatternComponentResult($inner.$field).into_js($context);
443451
let _ = $obj.create_data_property(
444-
JsString::from(property_name),
452+
js_string!(stringify!($field)),
445453
$field,
446454
$context,
447455
);
@@ -464,7 +472,7 @@ impl IntoJs for UrlPatternResult {
464472
}
465473
array.into()
466474
};
467-
let _ = obj.create_data_property(JsString::from("inputs"), inputs, context);
475+
let _ = obj.create_data_property(js_string!("inputs"), inputs, context);
468476

469477
obj.into()
470478
}

0 commit comments

Comments
 (0)