0
0
Fork 0
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:
Bert Belder 2021-04-09 19:49:55 +02:00 committed by GitHub
parent d78cc3df87
commit de9a7e2698
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 8 deletions

View file

@ -173,16 +173,12 @@ impl String {
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 !
pub fn new_external_onebyte_static<'s>(
scope: &mut HandleScope<'s, ()>,
value: &'static str,
buffer: &'static [u8],
) -> Option<Local<'s, String>> {
let buffer: &[u8] = value.as_ref();
if buffer.is_empty() {
return None;
}
let buffer_len = buffer.len().try_into().ok()?;
unsafe {
scope.cast_local(|sd| {

View file

@ -4991,7 +4991,7 @@ fn external_strings() {
let scope = &mut v8::ContextScope::new(scope, context);
// Parse JSON from an external string
let json_static = "{\"a\": 1, \"b\": 2}";
let json_static = b"{\"a\": 1, \"b\": 2}";
let json_external =
v8::String::new_external_onebyte_static(scope, json_static).unwrap();
let maybe_value = v8::json::parse(scope, json_external);
@ -5005,7 +5005,7 @@ fn external_strings() {
// In & out
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);
assert_eq!(rust_str, "hello world");
// Externality checks