Merge pull request #697 from napi-rs/fix-utf16
fix(napi): utf16 value should not contains 0 terminate
This commit is contained in:
commit
a2806f59fe
8 changed files with 32 additions and 16 deletions
|
@ -68,7 +68,7 @@ impl JsString {
|
||||||
pub fn into_utf16(self) -> Result<JsStringUtf16> {
|
pub fn into_utf16(self) -> Result<JsStringUtf16> {
|
||||||
let mut written_char_count = 0usize;
|
let mut written_char_count = 0usize;
|
||||||
let len = self.utf16_len()? + 1;
|
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();
|
let buf_ptr = result.as_mut_ptr();
|
||||||
check_status!(unsafe {
|
check_status!(unsafe {
|
||||||
sys::napi_get_value_string_utf16(
|
sys::napi_get_value_string_utf16(
|
||||||
|
|
|
@ -11,8 +11,11 @@ pub struct JsStringUtf16 {
|
||||||
impl JsStringUtf16 {
|
impl JsStringUtf16 {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_str(&self) -> Result<String> {
|
pub fn as_str(&self) -> Result<String> {
|
||||||
String::from_utf16(self.as_slice())
|
if let Some((_, prefix)) = self.as_slice().split_last() {
|
||||||
.map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))
|
String::from_utf16(prefix).map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))
|
||||||
|
} else {
|
||||||
|
Ok(String::new())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -4,28 +4,17 @@ The actual snapshot is saved in `object.spec.ts.snap`.
|
||||||
|
|
||||||
Generated by [AVA](https://avajs.dev).
|
Generated by [AVA](https://avajs.dev).
|
||||||
|
|
||||||
## setNamedProperty
|
|
||||||
|
|
||||||
> Snapshot 1
|
|
||||||
|
|
||||||
'RustPropertyKey'
|
|
||||||
|
|
||||||
## setProperty
|
## setProperty
|
||||||
|
|
||||||
> Snapshot 1
|
> Snapshot 1
|
||||||
|
|
||||||
'Rust object property'
|
'Rust object property'
|
||||||
|
|
||||||
## testDeleteElement
|
## setNamedProperty
|
||||||
|
|
||||||
> Snapshot 1
|
> Snapshot 1
|
||||||
|
|
||||||
[
|
'RustPropertyKey'
|
||||||
0,
|
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
3,
|
|
||||||
]
|
|
||||||
|
|
||||||
## testGetPropertyNames
|
## testGetPropertyNames
|
||||||
|
|
||||||
|
@ -48,3 +37,14 @@ Generated by [AVA](https://avajs.dev).
|
||||||
undefined,
|
undefined,
|
||||||
'foo',
|
'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))
|
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) => {
|
test('should be able to concat latin1 string', (t) => {
|
||||||
const fixture = '涽¾DEL'
|
const fixture = '涽¾DEL'
|
||||||
t.snapshot(bindings.concatLatin1String(fixture))
|
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)
|
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)]
|
#[js_function(1)]
|
||||||
fn concat_latin1_string(ctx: CallContext) -> Result<JsString> {
|
fn concat_latin1_string(ctx: CallContext) -> Result<JsString> {
|
||||||
let in_string = ctx.get::<JsString>(0)?;
|
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<()> {
|
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
||||||
exports.create_named_method("concatString", concat_string)?;
|
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("concatLatin1String", concat_latin1_string)?;
|
||||||
exports.create_named_method("createLatin1", create_latin1)?;
|
exports.create_named_method("createLatin1", create_latin1)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue