From de9a7e2698e383e19933c3b876766ee5f692a581 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 9 Apr 2021 19:49:55 +0200 Subject: [PATCH] String::new_external_onebyte_static() should take `&[u8]` instead of `&str` (#658) --- src/string.rs | 8 ++------ tests/test_api.rs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/string.rs b/src/string.rs index 30a07615..8ea7d96a 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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> { - 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| { diff --git a/tests/test_api.rs b/tests/test_api.rs index 552cbeac..57944c56 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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