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 bb1d74fd..e16ea220 100644 Binary files a/test_module/__test__/object.spec.ts.snap and b/test_module/__test__/object.spec.ts.snap differ diff --git a/test_module/__test__/string.spec.ts b/test_module/__test__/string.spec.ts index a0898cb5..17080393 100644 --- a/test_module/__test__/string.spec.ts +++ b/test_module/__test__/string.spec.ts @@ -7,6 +7,11 @@ test('should be able to concat string', (t) => { 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 f02b4e92..1669b39d 100644 Binary files a/test_module/__test__/string.spec.ts.md and b/test_module/__test__/string.spec.ts.md differ diff --git a/test_module/__test__/string.spec.ts.snap b/test_module/__test__/string.spec.ts.snap index 9e347da0..1bb78848 100644 Binary files a/test_module/__test__/string.spec.ts.snap and b/test_module/__test__/string.spec.ts.snap differ 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(())