mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-03-09 13:38:51 -04:00
String::new_external_onebyte_static() should take &[u8]
instead of &str
(#658)
This commit is contained in:
parent
d78cc3df87
commit
de9a7e2698
2 changed files with 4 additions and 8 deletions
|
@ -173,16 +173,12 @@ impl String {
|
||||||
Self::new_from_utf8(scope, value.as_ref(), NewStringType::Normal)
|
Self::new_from_utf8(scope, value.as_ref(), NewStringType::Normal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a v8::String from a `&'static str`,
|
// Creates a v8::String from a `&'static [u8]`,
|
||||||
// must be Latin-1 or ASCII, not UTF-8 !
|
// must be Latin-1 or ASCII, not UTF-8 !
|
||||||
pub fn new_external_onebyte_static<'s>(
|
pub fn new_external_onebyte_static<'s>(
|
||||||
scope: &mut HandleScope<'s, ()>,
|
scope: &mut HandleScope<'s, ()>,
|
||||||
value: &'static str,
|
buffer: &'static [u8],
|
||||||
) -> Option<Local<'s, String>> {
|
) -> Option<Local<'s, String>> {
|
||||||
let buffer: &[u8] = value.as_ref();
|
|
||||||
if buffer.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let buffer_len = buffer.len().try_into().ok()?;
|
let buffer_len = buffer.len().try_into().ok()?;
|
||||||
unsafe {
|
unsafe {
|
||||||
scope.cast_local(|sd| {
|
scope.cast_local(|sd| {
|
||||||
|
|
|
@ -4991,7 +4991,7 @@ fn external_strings() {
|
||||||
let scope = &mut v8::ContextScope::new(scope, context);
|
let scope = &mut v8::ContextScope::new(scope, context);
|
||||||
|
|
||||||
// Parse JSON from an external string
|
// Parse JSON from an external string
|
||||||
let json_static = "{\"a\": 1, \"b\": 2}";
|
let json_static = b"{\"a\": 1, \"b\": 2}";
|
||||||
let json_external =
|
let json_external =
|
||||||
v8::String::new_external_onebyte_static(scope, json_static).unwrap();
|
v8::String::new_external_onebyte_static(scope, json_static).unwrap();
|
||||||
let maybe_value = v8::json::parse(scope, json_external);
|
let maybe_value = v8::json::parse(scope, json_external);
|
||||||
|
@ -5005,7 +5005,7 @@ fn external_strings() {
|
||||||
|
|
||||||
// In & out
|
// In & out
|
||||||
let hello =
|
let hello =
|
||||||
v8::String::new_external_onebyte_static(scope, "hello world").unwrap();
|
v8::String::new_external_onebyte_static(scope, b"hello world").unwrap();
|
||||||
let rust_str = hello.to_rust_string_lossy(scope);
|
let rust_str = hello.to_rust_string_lossy(scope);
|
||||||
assert_eq!(rust_str, "hello world");
|
assert_eq!(rust_str, "hello world");
|
||||||
// Externality checks
|
// Externality checks
|
||||||
|
|
Loading…
Add table
Reference in a new issue