Skip to content

Commit

Permalink
Fix printing of Native class prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Oct 27, 2023
1 parent 6dc0deb commit 84dae70
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ node_modules/
**/.vitepress/cache
**/.vitepress/dist

**/dist
**/dist
32 changes: 12 additions & 20 deletions jstz_api/src/url/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use boa_gc::{custom_trace, Finalize, GcRefMut, Trace};
use jstz_core::{
accessor,
native::{
register_global_class, Accessor, ClassBuilder, JsNativeObject, NativeClass,
register_global_class, Accessor, ClassBuilder, JsNativeObject,
JsNativeObjectToString, NativeClass,
},
value::IntoJs,
};
Expand All @@ -46,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 @@ -423,24 +433,6 @@ impl UrlClass {
)
}

fn to_string(
this: &JsValue,
_args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
let url = Url::try_from_js(this)?;

Ok(url.href().into_js(context))
}

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

fn can_parse(
_this: &JsValue,
args: &[JsValue],
Expand Down Expand Up @@ -509,7 +501,7 @@ impl NativeClass for UrlClass {
.method(
js_string!("toJSON"),
0,
NativeFunction::from_fn_ptr(UrlClass::to_json),
NativeFunction::from_fn_ptr(UrlClass::to_string),
);

Ok(())
Expand Down
22 changes: 11 additions & 11 deletions jstz_api/src/url/search_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use boa_gc::{empty_trace, Finalize, GcRefMut, Trace};
use jstz_core::{
accessor,
native::{
register_global_class, Accessor, ClassBuilder, JsNativeObject, NativeClass,
register_global_class, Accessor, ClassBuilder, JsNativeObject,
JsNativeObjectToString, NativeClass,
},
value::IntoJs,
};
Expand Down Expand Up @@ -39,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 @@ -440,16 +450,6 @@ impl UrlSearchParamsClass {

Ok(JsValue::undefined())
}

fn to_string(
this: &JsValue,
_args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
let search_params = UrlSearchParams::try_from_js(this)?;

Ok(search_params.to_string().into_js(context))
}
}

impl NativeClass for UrlSearchParamsClass {
Expand Down
23 changes: 23 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 @@ -384,6 +385,13 @@ fn raw_constructor<T: NativeClass>(
Ok(object.inner.clone())
}

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

pub trait NativeClass {
/// The Rust type of the class's instances.
type Instance: NativeObject + Sized;
Expand All @@ -406,6 +414,21 @@ pub trait NativeClass {

/// Initializes the internals and the methods of the class.
fn init(class: &mut ClassBuilder<'_, '_>) -> JsResult<()>;

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

pub fn register_global_class<T: NativeClass>(context: &mut Context<'_>) -> JsResult<()> {
Expand Down

0 comments on commit 84dae70

Please sign in to comment.