From eeada5ac51752a9a99146be3d746aa2ec17302b9 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 25 Oct 2016 10:21:01 +1100 Subject: [PATCH 1/2] Add as_str_unchecked() to nsACString --- components/style/gecko_bindings/nsstring_vendor/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/style/gecko_bindings/nsstring_vendor/src/lib.rs b/components/style/gecko_bindings/nsstring_vendor/src/lib.rs index 7080e57cbd0..af0ba1d1918 100644 --- a/components/style/gecko_bindings/nsstring_vendor/src/lib.rs +++ b/components/style/gecko_bindings/nsstring_vendor/src/lib.rs @@ -144,6 +144,7 @@ use std::ptr; use std::mem; use std::fmt; use std::cmp; +use std::str; use std::u32; ////////////////////////////////// @@ -569,6 +570,10 @@ impl nsACString { Gecko_AppendUTF16toCString(self as *mut _, other as *const _); } } + + pub unsafe fn as_str_unchecked(&self) -> &str { + str::from_utf8_unchecked(self) + } } impl<'a> From<&'a str> for nsCString<'a> { From 3303a978b954123875f0bedeaa8c9d66c365c50e Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 25 Oct 2016 10:21:53 +1100 Subject: [PATCH 2/2] Use nsACString to pass string params for bindings --- components/style/binding_tools/regen.py | 8 ++- components/style/gecko_bindings/bindings.rs | 21 ++++---- ports/geckolib/glue.rs | 56 ++++++++------------- 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/components/style/binding_tools/regen.py b/components/style/binding_tools/regen.py index 93a1e8db0ba..00869413da6 100755 --- a/components/style/binding_tools/regen.py +++ b/components/style/binding_tools/regen.py @@ -233,7 +233,13 @@ COMPILATION_TARGETS = { # Generation of the ffi bindings. "bindings": { "target_dir": "../gecko_bindings", - "raw_lines": [], + "blacklist_types": [ + "nsACString_internal", + ], + "raw_lines": [ + "pub use nsstring::nsACString;", + "type nsACString_internal = nsACString;", + ], "flags": [ "--ignore-methods", ], diff --git a/components/style/gecko_bindings/bindings.rs b/components/style/gecko_bindings/bindings.rs index abebe1d89a9..58d18c65ab6 100644 --- a/components/style/gecko_bindings/bindings.rs +++ b/components/style/gecko_bindings/bindings.rs @@ -1,5 +1,7 @@ /* automatically generated by rust-bindgen */ +pub use nsstring::nsACString; +type nsACString_internal = nsACString; pub type ServoComputedValuesStrong = ::gecko_bindings::sugar::ownership::Strong; pub type ServoComputedValuesBorrowedOrNull<'a> = Option<&'a ServoComputedValues>; pub type ServoComputedValuesBorrowed<'a> = &'a ServoComputedValues; @@ -837,10 +839,10 @@ extern "C" { pub fn Servo_Node_ClearNodeData(node: RawGeckoNodeBorrowed); } extern "C" { - pub fn Servo_StyleSheet_FromUTF8Bytes(bytes: *const u8, length: u32, + pub fn Servo_StyleSheet_FromUTF8Bytes(data: *const nsACString_internal, parsing_mode: SheetParsingMode, - base_bytes: *const u8, - base_length: u32, + base_url: + *const nsACString_internal, base: *mut ThreadSafeURIHolder, referrer: *mut ThreadSafeURIHolder, principal: @@ -876,10 +878,9 @@ extern "C" { RawServoStyleSheetBorrowed); } extern "C" { - pub fn Servo_ParseProperty(property_bytes: *const u8, - property_length: u32, value_bytes: *const u8, - value_length: u32, base_bytes: *const u8, - base_length: u32, + pub fn Servo_ParseProperty(property: *const nsACString_internal, + value: *const nsACString_internal, + base_url: *const nsACString_internal, base: *mut ThreadSafeURIHolder, referrer: *mut ThreadSafeURIHolder, principal: *mut ThreadSafePrincipalHolder) @@ -893,7 +894,7 @@ extern "C" { -> ServoComputedValuesStrong; } extern "C" { - pub fn Servo_ParseStyleAttribute(bytes: *const u8, length: u32) + pub fn Servo_ParseStyleAttribute(data: *const nsACString_internal) -> RawServoDeclarationBlockStrong; } extern "C" { @@ -907,8 +908,8 @@ extern "C" { buffer: *mut nsString); } extern "C" { - pub fn Servo_CSSSupports(name: *const u8, name_length: u32, - value: *const u8, value_length: u32) -> bool; + pub fn Servo_CSSSupports(name: *const nsACString_internal, + value: *const nsACString_internal) -> bool; } extern "C" { pub fn Servo_ComputedValues_Get(node: RawGeckoNodeBorrowed) diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index 679295aa040..75cede9de3a 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -8,8 +8,6 @@ use env_logger; use euclid::Size2D; use parking_lot::RwLock; use std::mem::transmute; -use std::slice; -use std::str::from_utf8_unchecked; use std::sync::{Arc, Mutex}; use style::arc_ptr_eq; use style::context::{LocalStyleContextCreationInfo, ReflowGoal, SharedStyleContext}; @@ -29,6 +27,7 @@ use style::gecko_bindings::bindings::{RawServoStyleSheetStrong, ServoComputedVal use style::gecko_bindings::bindings::{ThreadSafePrincipalHolder, ThreadSafeURIHolder}; use style::gecko_bindings::bindings::Gecko_Utf8SliceToString; use style::gecko_bindings::bindings::ServoComputedValuesBorrowedOrNull; +use style::gecko_bindings::bindings::nsACString; use style::gecko_bindings::structs::{SheetParsingMode, nsIAtom}; use style::gecko_bindings::structs::ServoElementSnapshot; use style::gecko_bindings::structs::nsRestyleHint; @@ -161,16 +160,14 @@ pub extern "C" fn Servo_Node_ClearNodeData(node: RawGeckoNodeBorrowed) -> () { } #[no_mangle] -pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(bytes: *const u8, - length: u32, - mode: SheetParsingMode, - base_bytes: *const u8, - base_length: u32, - base: *mut ThreadSafeURIHolder, - referrer: *mut ThreadSafeURIHolder, - principal: *mut ThreadSafePrincipalHolder) - -> RawServoStyleSheetStrong { - let input = unsafe { from_utf8_unchecked(slice::from_raw_parts(bytes, length as usize)) }; +pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(data: *const nsACString, + mode: SheetParsingMode, + base_url: *const nsACString, + base: *mut ThreadSafeURIHolder, + referrer: *mut ThreadSafeURIHolder, + principal: *mut ThreadSafePrincipalHolder) + -> RawServoStyleSheetStrong { + let input = unsafe { data.as_ref().unwrap().as_str_unchecked() }; let origin = match mode { SheetParsingMode::eAuthorSheetFeatures => Origin::Author, @@ -178,7 +175,7 @@ pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(bytes: *const u8, SheetParsingMode::eAgentSheetFeatures => Origin::UserAgent, }; - let base_str = unsafe { from_utf8_unchecked(slice::from_raw_parts(base_bytes, base_length as usize)) }; + let base_str = unsafe { base_url.as_ref().unwrap().as_str_unchecked() }; let url = Url::parse(base_str).unwrap(); let extra_data = unsafe { ParserContextExtraData { base: Some(GeckoArcURI::new(base)), @@ -367,23 +364,14 @@ pub extern "C" fn Servo_StyleSet_Drop(data: RawServoStyleSetOwned) -> () { #[no_mangle] -pub extern "C" fn Servo_ParseProperty(property_bytes: *const u8, - property_length: u32, - value_bytes: *const u8, - value_length: u32, - base_bytes: *const u8, - base_length: u32, - base: *mut ThreadSafeURIHolder, +pub extern "C" fn Servo_ParseProperty(property: *const nsACString, value: *const nsACString, + base_url: *const nsACString, base: *mut ThreadSafeURIHolder, referrer: *mut ThreadSafeURIHolder, principal: *mut ThreadSafePrincipalHolder) -> RawServoDeclarationBlockStrong { - // All this string wrangling is temporary until the Gecko string bindings land (bug 1294742). - let name = unsafe { from_utf8_unchecked(slice::from_raw_parts(property_bytes, - property_length as usize)) }; - let value_str = unsafe { from_utf8_unchecked(slice::from_raw_parts(value_bytes, - value_length as usize)) }; - let base_str = unsafe { from_utf8_unchecked(slice::from_raw_parts(base_bytes, - base_length as usize)) }; + let name = unsafe { property.as_ref().unwrap().as_str_unchecked() }; + let value = unsafe { value.as_ref().unwrap().as_str_unchecked() }; + let base_str = unsafe { base_url.as_ref().unwrap().as_str_unchecked() }; let base_url = Url::parse(base_str).unwrap(); let extra_data = unsafe { ParserContextExtraData { base: Some(GeckoArcURI::new(base)), @@ -396,7 +384,7 @@ pub extern "C" fn Servo_ParseProperty(property_bytes: *const u8, extra_data); let mut results = vec![]; - match PropertyDeclaration::parse(name, &context, &mut Parser::new(value_str), + match PropertyDeclaration::parse(name, &context, &mut Parser::new(value), &mut results, false) { PropertyDeclarationParseResult::ValidOrIgnoredDeclaration => {}, _ => return RawServoDeclarationBlockStrong::null(), @@ -411,9 +399,8 @@ pub extern "C" fn Servo_ParseProperty(property_bytes: *const u8, } #[no_mangle] -pub extern "C" fn Servo_ParseStyleAttribute(bytes: *const u8, length: u32) - -> RawServoDeclarationBlockStrong { - let value = unsafe { from_utf8_unchecked(slice::from_raw_parts(bytes, length as usize)) }; +pub extern "C" fn Servo_ParseStyleAttribute(data: *const nsACString) -> RawServoDeclarationBlockStrong { + let value = unsafe { data.as_ref().unwrap().as_str_unchecked() }; Arc::new(RwLock::new(GeckoElement::parse_style_attribute(value))).into_strong() } @@ -467,10 +454,9 @@ pub extern "C" fn Servo_DeclarationBlock_SerializeOneValue( } #[no_mangle] -pub extern "C" fn Servo_CSSSupports(property: *const u8, property_length: u32, - value: *const u8, value_length: u32) -> bool { - let property = unsafe { from_utf8_unchecked(slice::from_raw_parts(property, property_length as usize)) }; - let value = unsafe { from_utf8_unchecked(slice::from_raw_parts(value, value_length as usize)) }; +pub extern "C" fn Servo_CSSSupports(property: *const nsACString, value: *const nsACString) -> bool { + let property = unsafe { property.as_ref().unwrap().as_str_unchecked() }; + let value = unsafe { value.as_ref().unwrap().as_str_unchecked() }; let base_url = &*DUMMY_BASE_URL; let extra_data = ParserContextExtraData::default();