Move jsstring_to_str and jsid_to_str to conversions.rs.

This appears to be a more sensible location for them.

Relevant to #433.
This commit is contained in:
Ms2ger 2014-10-16 09:18:12 +02:00
parent 1de2fb3721
commit b60a601f56
3 changed files with 27 additions and 30 deletions

View file

@ -4530,7 +4530,6 @@ class CGBindingRoot(CGThing):
'dom::bindings::utils::{FindEnumStringIndex, GetArrayIndexFromId}', 'dom::bindings::utils::{FindEnumStringIndex, GetArrayIndexFromId}',
'dom::bindings::utils::{GetPropertyOnPrototype, GetProtoOrIfaceArray}', 'dom::bindings::utils::{GetPropertyOnPrototype, GetProtoOrIfaceArray}',
'dom::bindings::utils::{HasPropertyOnPrototype, IntVal}', 'dom::bindings::utils::{HasPropertyOnPrototype, IntVal}',
'dom::bindings::utils::{jsid_to_str}',
'dom::bindings::utils::{Reflectable}', 'dom::bindings::utils::{Reflectable}',
'dom::bindings::utils::{squirrel_away_unique}', 'dom::bindings::utils::{squirrel_away_unique}',
'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}', 'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}',
@ -4544,6 +4543,7 @@ class CGBindingRoot(CGThing):
'dom::bindings::conversions::{FromJSValConvertible, ToJSValConvertible}', 'dom::bindings::conversions::{FromJSValConvertible, ToJSValConvertible}',
'dom::bindings::conversions::IDLInterface', 'dom::bindings::conversions::IDLInterface',
'dom::bindings::conversions::{Default, Empty}', 'dom::bindings::conversions::{Default, Empty}',
'dom::bindings::conversions::jsid_to_str',
'dom::bindings::codegen::{PrototypeList, RegisterBindings, UnionTypes}', 'dom::bindings::codegen::{PrototypeList, RegisterBindings, UnionTypes}',
'dom::bindings::codegen::Bindings::*', 'dom::bindings::codegen::Bindings::*',
'dom::bindings::error::{FailureUnknown, Fallible, Error, ErrorResult}', 'dom::bindings::error::{FailureUnknown, Fallible, Error, ErrorResult}',

View file

@ -7,11 +7,12 @@
use dom::bindings::js::{JS, JSRef, Root}; use dom::bindings::js::{JS, JSRef, Root};
use dom::bindings::str::ByteString; use dom::bindings::str::ByteString;
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::bindings::utils::jsstring_to_str;
use dom::bindings::utils::unwrap_jsmanaged; use dom::bindings::utils::unwrap_jsmanaged;
use servo_util::str::DOMString; use servo_util::str::DOMString;
use js::jsapi::{JSBool, JSContext, JSObject}; use js::glue::{RUST_JSID_TO_STRING, RUST_JSID_IS_STRING};
use js::glue::RUST_JS_NumberValue;
use js::jsapi::{JSBool, JSContext, JSObject, JSString, jsid};
use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64}; use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64};
use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32}; use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32};
use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean}; use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean};
@ -21,7 +22,7 @@ use js::jsapi::{JS_WrapValue};
use js::jsval::JSVal; use js::jsval::JSVal;
use js::jsval::{UndefinedValue, NullValue, BooleanValue, Int32Value, UInt32Value}; use js::jsval::{UndefinedValue, NullValue, BooleanValue, Int32Value, UInt32Value};
use js::jsval::{StringValue, ObjectValue, ObjectOrNullValue}; use js::jsval::{StringValue, ObjectValue, ObjectOrNullValue};
use js::glue::RUST_JS_NumberValue;
use libc; use libc;
use std::default; use std::default;
use std::slice; use std::slice;
@ -252,6 +253,27 @@ impl default::Default for StringificationBehavior {
} }
} }
/// Convert the given `JSString` to a `DOMString`. Fails if the string does not
/// contain valid UTF-16.
pub fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString {
unsafe {
let mut length = 0;
let chars = JS_GetStringCharsAndLength(cx, s, &mut length);
slice::raw::buf_as_slice(chars, length as uint, |char_vec| {
String::from_utf16(char_vec).unwrap()
})
}
}
/// Convert the given `jsid` to a `DOMString`. Fails if the `jsid` is not a
/// string, or if the string does not contain valid UTF-16.
pub fn jsid_to_str(cx: *mut JSContext, id: jsid) -> DOMString {
unsafe {
assert!(RUST_JSID_IS_STRING(id) != 0);
jsstring_to_str(cx, RUST_JSID_TO_STRING(id))
}
}
impl FromJSValConvertible<StringificationBehavior> for DOMString { impl FromJSValConvertible<StringificationBehavior> for DOMString {
fn from_jsval(cx: *mut JSContext, value: JSVal, nullBehavior: StringificationBehavior) -> Result<DOMString, ()> { fn from_jsval(cx: *mut JSContext, value: JSVal, nullBehavior: StringificationBehavior) -> Result<DOMString, ()> {
if nullBehavior == Empty && value.is_null() { if nullBehavior == Empty && value.is_null() {

View file

@ -12,7 +12,6 @@ use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Temporary, Root}; use dom::bindings::js::{JS, Temporary, Root};
use dom::browsercontext; use dom::browsercontext;
use dom::window; use dom::window;
use servo_util::str::DOMString;
use libc; use libc;
use libc::c_uint; use libc::c_uint;
@ -20,11 +19,9 @@ use std::cell::Cell;
use std::mem; use std::mem;
use std::cmp::PartialEq; use std::cmp::PartialEq;
use std::ptr; use std::ptr;
use std::slice;
use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFamily}; use js::glue::{js_IsObjectProxyClass, js_IsFunctionProxyClass, IsProxyHandlerFamily};
use js::glue::{UnwrapObject, GetProxyHandlerExtra}; use js::glue::{UnwrapObject, GetProxyHandlerExtra};
use js::glue::{IsWrapper, RUST_JSID_TO_STRING, RUST_JSID_IS_INT}; use js::glue::{IsWrapper, RUST_JSID_IS_INT, RUST_JSID_TO_INT};
use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_INT};
use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewFunction}; use js::jsapi::{JS_AlreadyHasOwnProperty, JS_NewFunction};
use js::jsapi::{JS_DefineProperties, JS_ForwardGetPropertyTo}; use js::jsapi::{JS_DefineProperties, JS_ForwardGetPropertyTo};
use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAndLength}; use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAndLength};
@ -37,7 +34,6 @@ use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot};
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass}; use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass};
use js::jsapi::{JSFunctionSpec, JSPropertySpec}; use js::jsapi::{JSFunctionSpec, JSPropertySpec};
use js::jsapi::{JS_NewGlobalObject, JS_InitStandardClasses}; use js::jsapi::{JS_NewGlobalObject, JS_InitStandardClasses};
use js::jsapi::{JSString};
use js::jsapi::JS_DeletePropertyById2; use js::jsapi::JS_DeletePropertyById2;
use js::jsfriendapi::JS_ObjectToOuterObject; use js::jsfriendapi::JS_ObjectToOuterObject;
use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType; use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType;
@ -161,27 +157,6 @@ pub unsafe fn squirrel_away_unique<T>(x: Box<T>) -> *const T {
mem::transmute(x) mem::transmute(x)
} }
/// Convert the given `JSString` to a `DOMString`. Fails if the string does not
/// contain valid UTF-16.
pub fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString {
unsafe {
let mut length = 0;
let chars = JS_GetStringCharsAndLength(cx, s, &mut length);
slice::raw::buf_as_slice(chars, length as uint, |char_vec| {
String::from_utf16(char_vec).unwrap()
})
}
}
/// Convert the given `jsid` to a `DOMString`. Fails if the `jsid` is not a
/// string, or if the string does not contain valid UTF-16.
pub fn jsid_to_str(cx: *mut JSContext, id: jsid) -> DOMString {
unsafe {
assert!(RUST_JSID_IS_STRING(id) != 0);
jsstring_to_str(cx, RUST_JSID_TO_STRING(id))
}
}
/// The index of the slot wherein a pointer to the reflected DOM object is /// The index of the slot wherein a pointer to the reflected DOM object is
/// stored for non-proxy bindings. /// stored for non-proxy bindings.
// We use slot 0 for holding the raw object. This is safe for both // We use slot 0 for holding the raw object. This is safe for both