fix(napi): utf16 value should not contains 0 terminate
This commit is contained in:
parent
c825b06277
commit
814414bc81
8 changed files with 32 additions and 16 deletions
|
@ -68,7 +68,7 @@ impl JsString {
|
|||
pub fn into_utf16(self) -> Result<JsStringUtf16> {
|
||||
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(
|
||||
|
|
|
@ -11,8 +11,11 @@ pub struct JsStringUtf16 {
|
|||
impl JsStringUtf16 {
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> Result<String> {
|
||||
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]
|
||||
|
|
|
@ -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,
|
||||
]
|
||||
|
|
Binary file not shown.
|
@ -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))
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -7,6 +7,13 @@ fn concat_string(ctx: CallContext) -> Result<JsString> {
|
|||
ctx.env.create_string_from_std(out_string)
|
||||
}
|
||||
|
||||
#[js_function(1)]
|
||||
fn concat_utf16_string(ctx: CallContext) -> Result<JsString> {
|
||||
let in_string = ctx.get::<JsString>(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<JsString> {
|
||||
let in_string = ctx.get::<JsString>(0)?;
|
||||
|
@ -25,6 +32,7 @@ fn create_latin1(ctx: CallContext) -> Result<JsString> {
|
|||
|
||||
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(())
|
||||
|
|
Loading…
Reference in a new issue