Skip to content

Commit

Permalink
Abstracted out the to_string pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Oct 26, 2023
1 parent a5fc971 commit 95d7e59
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ node_modules/
**/.vitepress/cache
**/.vitepress/dist

**/dist

.vscode
**/dist
37 changes: 15 additions & 22 deletions jstz_api/src/url/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ mod search_params;
use std::{cmp::Ordering, ops::Deref};

use boa_engine::{
builtins::object::Object as GlobalObject, js_string, object::Object,
property::Attribute, Context, JsArgs, JsError, JsNativeError, JsObject, JsResult,
JsValue, NativeFunction,
js_string, object::Object, property::Attribute, Context, JsArgs, JsError,
JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
};
use boa_gc::{custom_trace, Finalize, GcRefMut, Trace};
use jstz_core::{
accessor,
native::{
register_global_class, Accessor, ClassBuilder, JsNativeObject, NativeClass,
js_to_string, register_global_class, Accessor, ClassBuilder, JsNativeObject,
JsNativeObjectToString, NativeClass,
},
value::IntoJs,
};
Expand All @@ -47,6 +47,15 @@ unsafe impl Trace for Url {
});
}

impl JsNativeObjectToString for Url {
fn to_string(
this: &JsNativeObject<Self>,
context: &mut Context<'_>,
) -> JsResult<JsValue> {
Ok(this.deref().href().into_js(context))
}
}

impl Url {
fn parse_url(url: String, base: Option<String>) -> Option<InnerUrl> {
let options = InnerUrl::options();
Expand Down Expand Up @@ -424,28 +433,12 @@ impl UrlClass {
)
}

fn to_string(
this: &JsValue,
_args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
Url::try_from_js(this)
.map(|url| url.href().into_js(context))
.or_else(|_| GlobalObject::to_string(this, _args, context))
.or_else(|_| {
Err(JsError::from_native(
JsNativeError::typ()
.with_message("Failed to convert `URL` into js string"),
))
})
}

fn to_json(
this: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
Self::to_string(this, args, context)
js_to_string::<Url>(this, args, context)
}

fn can_parse(
Expand Down Expand Up @@ -511,7 +504,7 @@ impl NativeClass for UrlClass {
.method(
js_string!("toString"),
0,
NativeFunction::from_fn_ptr(UrlClass::to_string),
NativeFunction::from_fn_ptr(js_to_string::<Url>),
)
.method(
js_string!("toJSON"),
Expand Down
33 changes: 13 additions & 20 deletions jstz_api/src/url/search_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use boa_engine::{
builtins,
builtins::object::Object as GlobalObject,
js_string,
builtins, js_string,
object::{builtins::JsArray, Object},
property::Attribute,
value::TryFromJs,
Expand All @@ -12,7 +10,8 @@ use boa_gc::{empty_trace, Finalize, GcRefMut, Trace};
use jstz_core::{
accessor,
native::{
register_global_class, Accessor, ClassBuilder, JsNativeObject, NativeClass,
js_to_string, register_global_class, Accessor, ClassBuilder, JsNativeObject,
JsNativeObjectToString, NativeClass,
},
value::IntoJs,
};
Expand Down Expand Up @@ -41,6 +40,15 @@ unsafe impl Trace for UrlSearchParams {
empty_trace!();
}

impl JsNativeObjectToString for UrlSearchParams {
fn to_string(
this: &JsNativeObject<Self>,
context: &mut Context<'_>,
) -> JsResult<JsValue> {
Ok(this.deref().to_string().into_js(context))
}
}

impl UrlSearchParams {
pub(crate) fn set_values(&mut self, values: Vec<(Name, Value)>) {
self.values = values
Expand Down Expand Up @@ -442,21 +450,6 @@ impl UrlSearchParamsClass {

Ok(JsValue::undefined())
}

fn to_string(
this: &JsValue,
_args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
UrlSearchParams::try_from_js(this)
.map(|search_params| search_params.to_string().into_js(context))
.or_else(|_| GlobalObject::to_string(this, _args, context))
.or_else(|_| {
Err(JsError::from_native(JsNativeError::typ().with_message(
"Failed to convert `URLSearchParams` into js string",
)))
})
}
}

impl NativeClass for UrlSearchParamsClass {
Expand Down Expand Up @@ -518,7 +511,7 @@ impl NativeClass for UrlSearchParamsClass {
.method(
js_string!("toString"),
0,
NativeFunction::from_fn_ptr(UrlSearchParamsClass::to_string),
NativeFunction::from_fn_ptr(js_to_string::<UrlSearchParams>),
);

Ok(())
Expand Down
20 changes: 20 additions & 0 deletions jstz_core/src/native.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::marker::PhantomData;

use boa_engine::{
builtins::object::Object as GlobalObject,
context::intrinsics::StandardConstructor,
js_string,
object::{
Expand Down Expand Up @@ -427,3 +428,22 @@ pub fn register_global_class<T: NativeClass>(context: &mut Context<'_>) -> JsRes

Ok(())
}

pub trait JsNativeObjectToString: NativeObject + Sized {
fn to_string(
this: &JsNativeObject<Self>,
context: &mut Context<'_>,
) -> JsResult<JsValue>;
}

pub fn js_to_string<T: JsNativeObjectToString>(
this: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
if let Ok(native_obj) = JsNativeObject::<T>::try_from(this.clone()) {
T::to_string(&native_obj, context)
} else {
GlobalObject::to_string(this, args, context)
}
}

0 comments on commit 95d7e59

Please sign in to comment.