From 814414bc813ded1d27ee47127b1af578caf7177c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 13 Aug 2021 18:39:02 +0800 Subject: [PATCH] fix(napi): utf16 value should not contains 0 terminate --- napi/src/js_values/string/mod.rs | 2 +- napi/src/js_values/string/utf16.rs | 7 ++++-- test_module/__test__/object.spec.ts.md | 26 +++++++++++------------ test_module/__test__/object.spec.ts.snap | Bin 358 -> 364 bytes test_module/__test__/string.spec.ts | 5 +++++ test_module/__test__/string.spec.ts.md | Bin 429 -> 540 bytes test_module/__test__/string.spec.ts.snap | Bin 207 -> 234 bytes test_module/src/string.rs | 8 +++++++ 8 files changed, 32 insertions(+), 16 deletions(-) diff --git a/napi/src/js_values/string/mod.rs b/napi/src/js_values/string/mod.rs index 7e3dde8c..88ddf2e6 100644 --- a/napi/src/js_values/string/mod.rs +++ b/napi/src/js_values/string/mod.rs @@ -68,7 +68,7 @@ impl JsString { pub fn into_utf16(self) -> Result { let mut written_char_count = 0usize; let len = self.utf16_len()? + 1; - let mut result = Vec::with_capacity(len); + let mut result = vec![0; len]; let buf_ptr = result.as_mut_ptr(); check_status!(unsafe { sys::napi_get_value_string_utf16( diff --git a/napi/src/js_values/string/utf16.rs b/napi/src/js_values/string/utf16.rs index 78a6202d..3d51a921 100644 --- a/napi/src/js_values/string/utf16.rs +++ b/napi/src/js_values/string/utf16.rs @@ -11,8 +11,11 @@ pub struct JsStringUtf16 { impl JsStringUtf16 { #[inline] pub fn as_str(&self) -> Result { - String::from_utf16(self.as_slice()) - .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) + if let Some((_, prefix)) = self.as_slice().split_last() { + String::from_utf16(prefix).map_err(|e| Error::new(Status::InvalidArg, format!("{}", e))) + } else { + Ok(String::new()) + } } #[inline] diff --git a/test_module/__test__/object.spec.ts.md b/test_module/__test__/object.spec.ts.md index 49446419..326e4412 100644 --- a/test_module/__test__/object.spec.ts.md +++ b/test_module/__test__/object.spec.ts.md @@ -4,28 +4,17 @@ The actual snapshot is saved in `object.spec.ts.snap`. Generated by [AVA](https://avajs.dev). -## setNamedProperty - -> Snapshot 1 - - 'RustPropertyKey' - ## setProperty > Snapshot 1 'Rust object property' -## testDeleteElement +## setNamedProperty > Snapshot 1 - [ - 0, - undefined, - undefined, - 3, - ] + 'RustPropertyKey' ## testGetPropertyNames @@ -48,3 +37,14 @@ Generated by [AVA](https://avajs.dev). undefined, 'foo', ] + +## testDeleteElement + +> Snapshot 1 + + [ + 0, + undefined, + undefined, + 3, + ] diff --git a/test_module/__test__/object.spec.ts.snap b/test_module/__test__/object.spec.ts.snap index bb1d74fd755b58dc772c488cce5dde4bc54110b8..e16ea220e45760a37d15ebdbc88653f5ba2921a6 100644 GIT binary patch literal 364 zcmV-y0h9hgRzV9MSfJdJz4J}_r6kYNOio&aL| zX_HQG|6S%4eZOqwvl%A>8Ns5jfY_gFN?AegIWLtrU-Z8%-};mhESdo{^!4IEoheDo zF76G}4@~^Cpn?%BdIX4>8Q8&gGIB7oI{;}nAPxoMG$1Ym;x-_j2E@XQY=VrejzvX@ zm5huq7C%2f10xp$BO3!F4^RaYg9sxt10y>xl*`Bn=Cd#|!UUMm6fmRlS%+k|H2Uh5V$f)Z`L{0;nP+pR!s2X*(eH z1>#sB&L!kS78D<{FrfMn;b80@WMO1RvYpugNSgt%2O(RTP;3PT1EUeD#XupZY-0e= K2X0GD0ssICu9gD; literal 358 zcmV-s0h#_mRzVGbyBWnR(u%T_*{aUzhB0R)tQSj*(%(_>i|c^db8ePGUDAj1e2wFlzY zivxA0Br&_VH%vb;@y~(^MzH8?AXfh`enRiI%B6$yyH~b{{ETJ+K4k=p9sy!z26nKWjI0cTj3Pm$#U%>)Nm;4MB?<*a`30#(C6y>D_(3WH zpfcX62uTh`W&Fz$jItgRFqiB$OvQc^Yb$>axpM6F);D~RWLD#Ffs#; z6=Y;If{HLQ!i1Qzjgib|cK~?=h(m!m4T#HtxDAM>5weR7#V&SUREt;`8DRoUXbPCo z_$+9AR+vkf)AI9?oXTner0sy%7l>nlIG2zUSx}tF!hq^TgpJr;$im1B0CD5g { t.snapshot(bindings.concatString(fixture)) }) +test('should be able to concat utf16 string', (t) => { + const fixture = 'JavaScript 🌳 你好 napi' + t.snapshot(bindings.concatUTF16String(fixture)) +}) + test('should be able to concat latin1 string', (t) => { const fixture = '涽¾DEL' t.snapshot(bindings.concatLatin1String(fixture)) diff --git a/test_module/__test__/string.spec.ts.md b/test_module/__test__/string.spec.ts.md index f02b4e9245385697fa7fd9586b22d9f1e7eaaeef..1669b39d94d6d0feaa6a83167e6e6ac0a2ccc7f3 100644 GIT binary patch delta 36 scmZ3>Jcnh%&dIWj$=vyQ$%!Qjr6p;GW)t_zOtxV3n7BD*as#6-0P?~O4FCWD delta 17 ZcmbQkvX*(m&dC!Q<0fv-ocx|q7XU+O2YLVi diff --git a/test_module/__test__/string.spec.ts.snap b/test_module/__test__/string.spec.ts.snap index 9e347da02288394dff6abfb5709cac2cd83fd159..1bb788483eceeead316483e3b43f01e1257d9028 100644 GIT binary patch literal 234 zcmV~q6uj)uyO0`1^(fzMY&T^PZlRY064 zowRgKuC&sxUDGQkB&;uB1dH|nanbi=-8VD6&4T)FK2_=8oyQ0k-3!Fb4D4W&8Ce+w z8TGvq%Myc=i!uvJ6h6%F*{twn?}DdG_bTKi7Gx@DD+HAmgT$9LC={0zW#*+TQcp9> kp_PaBqlhRUUUq2Pp}mLpxw!g(1hE+b04N3E{wo0h0F6>;6aWAK literal 207 zcmV;=05JbSRzVr000000009E zVPIfjX3(~HqjqD3=)0dDlV_ZN(W=hK00Oo^T=YFz_svXiv!K44PgVMN=P`ms^FUZS zY3Z6=X{BGgrdLi#SYN;h7Ht5V#}3xd$jTtdsPC0nmKdB|lvz-s@L_(>W`!qv7d&0M zS0OL4AX7nGA*i$%B)+Ucp}3?dGcR2cMYHnZWrwyM+Iwi9i>nVvke~*ZLn{yM2LNV; JG1k-o005^YR*3)r diff --git a/test_module/src/string.rs b/test_module/src/string.rs index 8e3d0ddc..57d69aca 100644 --- a/test_module/src/string.rs +++ b/test_module/src/string.rs @@ -7,6 +7,13 @@ fn concat_string(ctx: CallContext) -> Result { ctx.env.create_string_from_std(out_string) } +#[js_function(1)] +fn concat_utf16_string(ctx: CallContext) -> Result { + let in_string = ctx.get::(0)?; + let out_string = format!("{} + Rust 🦀 string!", in_string.into_utf16()?.as_str()?); + ctx.env.create_string_from_std(out_string) +} + #[js_function(1)] fn concat_latin1_string(ctx: CallContext) -> Result { let in_string = ctx.get::(0)?; @@ -25,6 +32,7 @@ fn create_latin1(ctx: CallContext) -> Result { pub fn register_js(exports: &mut JsObject) -> Result<()> { exports.create_named_method("concatString", concat_string)?; + exports.create_named_method("concatUTF16String", concat_utf16_string)?; exports.create_named_method("concatLatin1String", concat_latin1_string)?; exports.create_named_method("createLatin1", create_latin1)?; Ok(())