From eba912fe944d6c81b1a29ccc4408e015bfe81a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Juli=C3=A1n=20Espina?= Date: Mon, 19 Aug 2024 09:03:47 +0000 Subject: [PATCH] Fix `js_str` macro to correctly handle latin1 strings (#3959) --- core/macros/src/lib.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/macros/src/lib.rs b/core/macros/src/lib.rs index 027e6c0478..7803ca5185 100644 --- a/core/macros/src/lib.rs +++ b/core/macros/src/lib.rs @@ -7,6 +7,7 @@ #![cfg_attr(not(test), forbid(clippy::unwrap_used))] use proc_macro::TokenStream; +use proc_macro2::Literal; use quote::{quote, ToTokens}; use syn::{ parse::{Parse, ParseStream}, @@ -185,24 +186,25 @@ pub fn utf16(input: TokenStream) -> TokenStream { #[proc_macro] pub fn js_str(input: TokenStream) -> TokenStream { let literal = parse_macro_input!(input as LitStr); - let utf8 = literal.value(); let mut is_latin1 = true; - let utf16 = utf8 + let codepoints = literal + .value() .encode_utf16() - .inspect(|x| { - if *x > u8::MAX.into() { + .map(|x| { + if x > u8::MAX.into() { is_latin1 = false; } + Literal::u16_unsuffixed(x) }) .collect::>(); if is_latin1 { quote! { - ::boa_engine::string::JsStr::latin1(#utf8.as_bytes()) + ::boa_engine::string::JsStr::latin1([#(#codepoints),*].as_slice()) } } else { quote! { - ::boa_engine::string::JsStr::utf16([#(#utf16),*].as_slice()) + ::boa_engine::string::JsStr::utf16([#(#codepoints),*].as_slice()) } } .into()