From 84dae70606a3330dcb2a0bba7da11edf4fcdc432 Mon Sep 17 00:00:00 2001 From: Leo Unoki Date: Tue, 24 Oct 2023 11:05:19 +0100 Subject: [PATCH] Fix printing of Native class prototype --- .gitignore | 2 +- jstz_api/src/url/mod.rs | 32 ++++++++++++------------------- jstz_api/src/url/search_params.rs | 22 ++++++++++----------- jstz_core/src/native.rs | 23 ++++++++++++++++++++++ 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 55b08de44..e7ad2d909 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,4 @@ node_modules/ **/.vitepress/cache **/.vitepress/dist -**/dist +**/dist \ No newline at end of file diff --git a/jstz_api/src/url/mod.rs b/jstz_api/src/url/mod.rs index cc6e2f9e5..f77f8ed29 100644 --- a/jstz_api/src/url/mod.rs +++ b/jstz_api/src/url/mod.rs @@ -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, }; @@ -46,6 +47,15 @@ unsafe impl Trace for Url { }); } +impl JsNativeObjectToString for Url { + fn to_string( + this: &JsNativeObject, + context: &mut Context<'_>, + ) -> JsResult { + Ok(this.deref().href().into_js(context)) + } +} + impl Url { fn parse_url(url: String, base: Option) -> Option { let options = InnerUrl::options(); @@ -423,24 +433,6 @@ impl UrlClass { ) } - fn to_string( - this: &JsValue, - _args: &[JsValue], - context: &mut Context<'_>, - ) -> JsResult { - let url = Url::try_from_js(this)?; - - Ok(url.href().into_js(context)) - } - - fn to_json( - this: &JsValue, - args: &[JsValue], - context: &mut Context<'_>, - ) -> JsResult { - Self::to_string(this, args, context) - } - fn can_parse( _this: &JsValue, args: &[JsValue], @@ -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(()) diff --git a/jstz_api/src/url/search_params.rs b/jstz_api/src/url/search_params.rs index ac9a31067..9842165a9 100644 --- a/jstz_api/src/url/search_params.rs +++ b/jstz_api/src/url/search_params.rs @@ -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, }; @@ -39,6 +40,15 @@ unsafe impl Trace for UrlSearchParams { empty_trace!(); } +impl JsNativeObjectToString for UrlSearchParams { + fn to_string( + this: &JsNativeObject, + context: &mut Context<'_>, + ) -> JsResult { + Ok(this.deref().to_string().into_js(context)) + } +} + impl UrlSearchParams { pub(crate) fn set_values(&mut self, values: Vec<(Name, Value)>) { self.values = values @@ -440,16 +450,6 @@ impl UrlSearchParamsClass { Ok(JsValue::undefined()) } - - fn to_string( - this: &JsValue, - _args: &[JsValue], - context: &mut Context<'_>, - ) -> JsResult { - let search_params = UrlSearchParams::try_from_js(this)?; - - Ok(search_params.to_string().into_js(context)) - } } impl NativeClass for UrlSearchParamsClass { diff --git a/jstz_core/src/native.rs b/jstz_core/src/native.rs index 968f253d9..abbc4604b 100644 --- a/jstz_core/src/native.rs +++ b/jstz_core/src/native.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; use boa_engine::{ + builtins::object::Object as GlobalObject, context::intrinsics::StandardConstructor, js_string, object::{ @@ -384,6 +385,13 @@ fn raw_constructor( Ok(object.inner.clone()) } +pub trait JsNativeObjectToString: NativeObject + Sized { + fn to_string( + this: &JsNativeObject, + context: &mut Context<'_>, + ) -> JsResult; +} + pub trait NativeClass { /// The Rust type of the class's instances. type Instance: NativeObject + Sized; @@ -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 + where + Self::Instance: JsNativeObjectToString, + { + if let Ok(native_obj) = JsNativeObject::::try_from(this.clone()) { + Self::Instance::to_string(&native_obj, context) + } else { + GlobalObject::to_string(this, &[], context) + } + } } pub fn register_global_class(context: &mut Context<'_>) -> JsResult<()> {