Merge pull request #1056 from messense/roundtrip-str
This commit is contained in:
commit
ac25965ad3
6 changed files with 17 additions and 3 deletions
|
@ -2,7 +2,6 @@ use crate::{bindgen_prelude::*, check_status, sys, Error, Result, Status};
|
||||||
|
|
||||||
use std::ffi::{c_void, CStr};
|
use std::ffi::{c_void, CStr};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
#[cfg(feature = "latin1")]
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
@ -53,12 +52,15 @@ impl FromNapiValue for String {
|
||||||
"Failed to convert napi `string` into rust type `String`"
|
"Failed to convert napi `string` into rust type `String`"
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
match unsafe { CStr::from_ptr(buf_ptr) }.to_str() {
|
let mut ret = mem::ManuallyDrop::new(ret);
|
||||||
|
let buf_ptr = ret.as_mut_ptr();
|
||||||
|
let bytes = unsafe { Vec::from_raw_parts(buf_ptr as *mut u8, written_char_count, len) };
|
||||||
|
match String::from_utf8(bytes) {
|
||||||
Err(e) => Err(Error::new(
|
Err(e) => Err(Error::new(
|
||||||
Status::InvalidArg,
|
Status::InvalidArg,
|
||||||
format!("Failed to read utf8 string, {}", e),
|
format!("Failed to read utf8 string, {}", e),
|
||||||
)),
|
)),
|
||||||
Ok(s) => Ok(s.to_owned()),
|
Ok(s) => Ok(s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,6 +126,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export function concatStr(s: string): string␊
|
export function concatStr(s: string): string␊
|
||||||
export function concatUtf16(s: string): string␊
|
export function concatUtf16(s: string): string␊
|
||||||
export function concatLatin1(s: string): string␊
|
export function concatLatin1(s: string): string␊
|
||||||
|
export function roundtripStr(s: string): string␊
|
||||||
export function setSymbolInObj(symbol: symbol): object␊
|
export function setSymbolInObj(symbol: symbol): object␊
|
||||||
export function createSymbol(): symbol␊
|
export function createSymbol(): symbol␊
|
||||||
export function withoutAbortController(a: number, b: number): Promise<number>␊
|
export function withoutAbortController(a: number, b: number): Promise<number>␊
|
||||||
|
|
Binary file not shown.
|
@ -11,6 +11,7 @@ import {
|
||||||
concatLatin1,
|
concatLatin1,
|
||||||
concatStr,
|
concatStr,
|
||||||
concatUtf16,
|
concatUtf16,
|
||||||
|
roundtripStr,
|
||||||
getNums,
|
getNums,
|
||||||
getWords,
|
getWords,
|
||||||
sumNums,
|
sumNums,
|
||||||
|
@ -106,6 +107,10 @@ test('string', (t) => {
|
||||||
concatUtf16('JavaScript 🌳 你好 napi'),
|
concatUtf16('JavaScript 🌳 你好 napi'),
|
||||||
'JavaScript 🌳 你好 napi + Rust 🦀 string!',
|
'JavaScript 🌳 你好 napi + Rust 🦀 string!',
|
||||||
)
|
)
|
||||||
|
t.is(
|
||||||
|
roundtripStr('what up?!\u0000after the NULL'),
|
||||||
|
'what up?!\u0000after the NULL',
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('array', (t) => {
|
test('array', (t) => {
|
||||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -116,6 +116,7 @@ export function contains(source: string, target: string): boolean
|
||||||
export function concatStr(s: string): string
|
export function concatStr(s: string): string
|
||||||
export function concatUtf16(s: string): string
|
export function concatUtf16(s: string): string
|
||||||
export function concatLatin1(s: string): string
|
export function concatLatin1(s: string): string
|
||||||
|
export function roundtripStr(s: string): string
|
||||||
export function setSymbolInObj(symbol: symbol): object
|
export function setSymbolInObj(symbol: symbol): object
|
||||||
export function createSymbol(): symbol
|
export function createSymbol(): symbol
|
||||||
export function withoutAbortController(a: number, b: number): Promise<number>
|
export function withoutAbortController(a: number, b: number): Promise<number>
|
||||||
|
|
|
@ -20,3 +20,8 @@ fn concat_utf16(s: Utf16String) -> Utf16String {
|
||||||
fn concat_latin1(s: Latin1String) -> String {
|
fn concat_latin1(s: Latin1String) -> String {
|
||||||
format!("{} + Rust 🦀 string!", s)
|
format!("{} + Rust 🦀 string!", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn roundtrip_str(s: String) -> String {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue