diff --git a/core/macros/src/lib.rs b/core/macros/src/lib.rs index f53ac93b708..0d3579b5f48 100644 --- a/core/macros/src/lib.rs +++ b/core/macros/src/lib.rs @@ -524,10 +524,10 @@ fn generate_conversion( let mut from_js_with = None; let mut field_name = rename.rename(format!("{name}")); - if let Some(attr) = field + for attr in field .attrs .into_iter() - .find(|attr| attr.path().is_ident("boa")) + .filter(|attr| attr.path().is_ident("boa")) { attr.parse_nested_meta(|meta| { if meta.path.is_ident("from_js_with") { @@ -538,11 +538,18 @@ fn generate_conversion( let value = meta.value()?; field_name = value.parse::()?.value(); Ok(()) + } else if meta.path.is_ident("into_js_with") { + let _unused = meta.value()?.parse::()?; + Ok(()) + } else if meta.path.is_ident("skip") && meta.input.is_empty() { + Ok(()) } else { Err(meta.error( "invalid syntax in the `#[boa()]` attribute. \ - Note that this attribute only accepts the following syntax: \ - `#[boa(from_js_with = \"fully::qualified::path\")]`", + Note that this attribute only accepts the following syntax: \ + \n* `#[boa(from_js_with = \"fully::qualified::path\")]` \ + \n* `#[boa(rename = \"jsPropertyName\")]` \ + ", )) } }) @@ -666,14 +673,17 @@ fn generate_obj_properties( let value = meta.value()?; prop_key = value.parse::()?.value(); Ok(()) - } else if meta.path.is_ident("skip") & meta.input.is_empty() { + } else if meta.path.is_ident("skip") && meta.input.is_empty() { skip = true; Ok(()) + } else if meta.path.is_ident("from_js_with") { + let _unused = meta.value()?.parse::()?; + Ok(()) } else { Err(meta.error( "invalid syntax in the `#[boa()]` attribute. \ - Note that this attribute only accepts the following syntax: \ - \n* `#[boa(into_js_with = \"fully::qualified::path\")]`\ + Note that this attribute only accepts the following syntax: \ + \n* `#[boa(into_js_with = \"fully::qualified::path\")]` \ \n* `#[boa(rename = \"jsPropertyName\")]` \ \n* `#[boa(skip)]` \ ", diff --git a/tests/macros/tests/derive/both.rs b/tests/macros/tests/derive/both.rs new file mode 100644 index 00000000000..bf270efa636 --- /dev/null +++ b/tests/macros/tests/derive/both.rs @@ -0,0 +1,23 @@ +#![allow(unused)] + +use boa_engine::{ + value::{TryFromJs, TryIntoJs}, + Context, JsResult, JsValue, +}; + +#[derive(TryFromJs, TryIntoJs)] +struct Blah { + #[boa(into_js_with = "my_custom_converter_to_js")] + #[boa(from_js_with = "my_custom_converter_from_js")] + x: i32, +} + +fn my_custom_converter_to_js(value: &i32, _context: &mut Context) -> JsResult { + Ok(JsValue::new(*value)) +} + +fn my_custom_converter_from_js(value: &JsValue, _context: &mut Context) -> JsResult { + value.try_js_into(_context) +} + +fn main() {}