From 5f9e6494911b60c42247c451aa0abc986a9a3284 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 5 Mar 2014 10:50:22 +0100 Subject: [PATCH 1/4] Pass a JSContext to ToJSValConvertible::to_jsval. --- .../dom/bindings/codegen/CodegenRust.py | 2 +- .../script/dom/bindings/conversions.rs | 28 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index e3c44c707c0..43057039c43 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1582,7 +1582,7 @@ if %(resultStr)s.is_null() { if not type.isPrimitive(): raise TypeError("Need to learn to wrap %s" % type) - return (setValue("(%s).to_jsval()" % result), True) + return (setValue("(%s).to_jsval(cx)" % result), True) def wrapForType(type, descriptorProvider, templateValues): diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs index bee3a42fb09..4f6faa62912 100644 --- a/src/components/script/dom/bindings/conversions.rs +++ b/src/components/script/dom/bindings/conversions.rs @@ -11,7 +11,7 @@ use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value}; use js::glue::RUST_JS_NumberValue; pub trait ToJSValConvertible { - fn to_jsval(&self) -> JSVal; + fn to_jsval(&self, cx: *JSContext) -> JSVal; } pub trait FromJSValConvertible { @@ -32,7 +32,7 @@ unsafe fn convert_from_jsval( impl ToJSValConvertible for bool { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { BooleanValue(*self) } } @@ -45,7 +45,7 @@ impl FromJSValConvertible for bool { } impl ToJSValConvertible for i8 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { Int32Value(*self as i32) } } @@ -58,7 +58,7 @@ impl FromJSValConvertible for i8 { } impl ToJSValConvertible for u8 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { Int32Value(*self as i32) } } @@ -71,7 +71,7 @@ impl FromJSValConvertible for u8 { } impl ToJSValConvertible for i16 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { Int32Value(*self as i32) } } @@ -84,7 +84,7 @@ impl FromJSValConvertible for i16 { } impl ToJSValConvertible for u16 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { Int32Value(*self as i32) } } @@ -96,7 +96,7 @@ impl FromJSValConvertible for u16 { } impl ToJSValConvertible for i32 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { Int32Value(*self) } } @@ -108,7 +108,7 @@ impl FromJSValConvertible for i32 { } impl ToJSValConvertible for u32 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { UInt32Value(*self) } } @@ -120,7 +120,7 @@ impl FromJSValConvertible for u32 { } impl ToJSValConvertible for i64 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { unsafe { RUST_JS_NumberValue(*self as f64) } @@ -134,7 +134,7 @@ impl FromJSValConvertible for i64 { } impl ToJSValConvertible for u64 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { unsafe { RUST_JS_NumberValue(*self as f64) } @@ -148,7 +148,7 @@ impl FromJSValConvertible for u64 { } impl ToJSValConvertible for f32 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { unsafe { RUST_JS_NumberValue(*self as f64) } @@ -163,7 +163,7 @@ impl FromJSValConvertible for f32 { } impl ToJSValConvertible for f64 { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, _cx: *JSContext) -> JSVal { unsafe { RUST_JS_NumberValue(*self) } @@ -177,9 +177,9 @@ impl FromJSValConvertible for f64 { } impl ToJSValConvertible for Option { - fn to_jsval(&self) -> JSVal { + fn to_jsval(&self, cx: *JSContext) -> JSVal { match self { - &Some(ref value) => value.to_jsval(), + &Some(ref value) => value.to_jsval(cx), &None => NullValue(), } } From 476699a1142d90cf0ada164e17af880be32902d5 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 10 Mar 2014 11:19:48 +0100 Subject: [PATCH 2/4] Allow passing options to FromJSValConvertible::from_jsval. --- .../dom/bindings/codegen/CodegenRust.py | 2 +- .../script/dom/bindings/conversions.rs | 57 ++++++++++--------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 43057039c43..28ef759a70f 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1232,7 +1232,7 @@ for (uint32_t i = 0; i < length; ++i) { successVal = preSuccess + successVal + postSuccess #XXXjdm support conversionBehavior here template = ( - "match FromJSValConvertible::from_jsval(cx, ${val}) {\n" + "match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n" " Ok(v) => ${declName} = %s,\n" " Err(_) => %s\n" "}" % (successVal, failureCode)) diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs index 4f6faa62912..acceed4c6bb 100644 --- a/src/components/script/dom/bindings/conversions.rs +++ b/src/components/script/dom/bindings/conversions.rs @@ -14,8 +14,8 @@ pub trait ToJSValConvertible { fn to_jsval(&self, cx: *JSContext) -> JSVal; } -pub trait FromJSValConvertible { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result; +pub trait FromJSValConvertible { + fn from_jsval(cx: *JSContext, val: JSVal, option: T) -> Result; } @@ -37,8 +37,8 @@ impl ToJSValConvertible for bool { } } -impl FromJSValConvertible for bool { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for bool { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { let result = unsafe { convert_from_jsval(cx, val, JS_ValueToBoolean) }; result.map(|b| b != 0) } @@ -50,8 +50,8 @@ impl ToJSValConvertible for i8 { } } -impl FromJSValConvertible for i8 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for i8 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; result.map(|v| v as i8) } @@ -63,8 +63,8 @@ impl ToJSValConvertible for u8 { } } -impl FromJSValConvertible for u8 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for u8 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; result.map(|v| v as u8) } @@ -76,8 +76,8 @@ impl ToJSValConvertible for i16 { } } -impl FromJSValConvertible for i16 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for i16 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; result.map(|v| v as i16) } @@ -89,8 +89,8 @@ impl ToJSValConvertible for u16 { } } -impl FromJSValConvertible for u16 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for u16 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) } } } @@ -101,8 +101,8 @@ impl ToJSValConvertible for i32 { } } -impl FromJSValConvertible for i32 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for i32 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) } } } @@ -113,8 +113,8 @@ impl ToJSValConvertible for u32 { } } -impl FromJSValConvertible for u32 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for u32 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) } } } @@ -127,8 +127,8 @@ impl ToJSValConvertible for i64 { } } -impl FromJSValConvertible for i64 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for i64 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) } } } @@ -141,8 +141,8 @@ impl ToJSValConvertible for u64 { } } -impl FromJSValConvertible for u64 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for u64 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) } } } @@ -155,8 +155,8 @@ impl ToJSValConvertible for f32 { } } -impl FromJSValConvertible for f32 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for f32 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { let result = unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }; result.map(|f| f as f32) } @@ -170,8 +170,8 @@ impl ToJSValConvertible for f64 { } } -impl FromJSValConvertible for f64 { - fn from_jsval(cx: *JSContext, val: JSVal) -> Result { +impl FromJSValConvertible<()> for f64 { + fn from_jsval(cx: *JSContext, val: JSVal, _option: ()) -> Result { unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) } } } @@ -185,13 +185,14 @@ impl ToJSValConvertible for Option { } } -impl FromJSValConvertible for Option { - fn from_jsval(cx: *JSContext, value: JSVal) -> Result, ()> { +impl> FromJSValConvertible<()> for Option { + fn from_jsval(cx: *JSContext, value: JSVal, _: ()) -> Result, ()> { if value.is_null_or_undefined() { Ok(None) } else { - let result: Result = FromJSValConvertible::from_jsval(cx, value); - result.map(|v| Some(v)) + let option: X = Default::default(); + let result: Result = FromJSValConvertible::from_jsval(cx, value, option); + result.map(Some) } } } From 17411db8cae1fe2786986541e110516fc7ef9dcc Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 10 Mar 2014 11:40:13 +0100 Subject: [PATCH 3/4] Replace str_to_jsval and domstring_to_jsval by a ToJSValConvertible implementation. --- .../dom/bindings/codegen/CodegenRust.py | 9 +++------ .../script/dom/bindings/conversions.rs | 19 +++++++++++++++++- src/components/script/dom/bindings/utils.rs | 20 ++----------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 28ef759a70f..6b377dae22a 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1535,10 +1535,7 @@ for (uint32_t i = 0; i < length; ++i) { return (wrappingCode, False) if type.isString(): - if type.nullable(): - return (wrapAndSetPtr("*${jsvalPtr} = domstring_to_jsval(cx, %s)" % result), False) - else: - return (wrapAndSetPtr("*${jsvalPtr} = str_to_jsval(cx, %s)" % result), False) + return (setValue("(%s).to_jsval(cx)" % result), True) if type.isEnum(): if type.nullable(): @@ -5258,7 +5255,7 @@ class CGBindingRoot(CGThing): 'dom::bindings::utils::{CreateDOMGlobal, CreateInterfaceObjects2}', 'dom::bindings::utils::{ConstantSpec, cx_for_dom_object, Default}', 'dom::bindings::utils::{dom_object_slot, DOM_OBJECT_SLOT, DOMClass}', - 'dom::bindings::utils::{DOMJSClass, domstring_to_jsval, Empty}', + 'dom::bindings::utils::{DOMJSClass, Empty}', 'dom::bindings::utils::{FindEnumStringIndex, GetArrayIndexFromId}', 'dom::bindings::utils::{GetPropertyOnPrototype, GetProtoOrIfaceArray}', 'dom::bindings::utils::{GetReflector, HasPropertyOnPrototype, IntVal}', @@ -5266,7 +5263,7 @@ class CGBindingRoot(CGThing): 'dom::bindings::utils::{NativePropertyHooks}', 'dom::bindings::utils::global_object_for_js_object', 'dom::bindings::utils::{Reflectable}', - 'dom::bindings::utils::{squirrel_away_unique, str_to_jsval}', + 'dom::bindings::utils::{squirrel_away_unique}', 'dom::bindings::utils::{ThrowingConstructor, unwrap, unwrap_jsmanaged}', 'dom::bindings::utils::{unwrap_object, VoidVal, with_gc_disabled}', 'dom::bindings::utils::{with_gc_enabled, XrayResolveProperty}', diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs index acceed4c6bb..faf7d1c852e 100644 --- a/src/components/script/dom/bindings/conversions.rs +++ b/src/components/script/dom/bindings/conversions.rs @@ -2,13 +2,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use servo_util::str::DOMString; + use js::jsapi::{JSBool, JSContext}; use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64}; use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32}; use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean}; +use js::jsapi::{JS_NewUCStringCopyN}; use js::jsval::JSVal; -use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value}; +use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value, StringValue}; use js::glue::RUST_JS_NumberValue; +use std::libc; pub trait ToJSValConvertible { fn to_jsval(&self, cx: *JSContext) -> JSVal; @@ -176,6 +180,19 @@ impl FromJSValConvertible<()> for f64 { } } +impl ToJSValConvertible for DOMString { + fn to_jsval(&self, cx: *JSContext) -> JSVal { + unsafe { + let string_utf16 = self.to_utf16(); + let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t); + if jsstr.is_null() { + fail!("JS_NewUCStringCopyN failed"); + } + StringValue(&*jsstr) + } + } +} + impl ToJSValConvertible for Option { fn to_jsval(&self, cx: *JSContext) -> JSVal { match self { diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index c4506f96855..24bd910ff00 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -26,7 +26,7 @@ use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAn use js::jsapi::{JS_ObjectIsRegExp, JS_ObjectIsDate}; use js::jsapi::{JS_InternString, JS_GetFunctionObject}; use js::jsapi::{JS_HasPropertyById, JS_GetPrototype, JS_GetGlobalForObject}; -use js::jsapi::{JS_NewUCStringCopyN, JS_DefineFunctions, JS_DefineProperty}; +use js::jsapi::{JS_DefineFunctions, JS_DefineProperty}; use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot}; use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass, JSNative}; use js::jsapi::{JSFunctionSpec, JSPropertySpec, JSPropertyDescriptor}; @@ -35,7 +35,7 @@ use js::jsapi::{JSString}; use js::jsapi::{JS_AllowGC, JS_InhibitGC}; use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType; use js::jsval::JSVal; -use js::jsval::{StringValue, PrivateValue, ObjectValue, NullValue, Int32Value}; +use js::jsval::{PrivateValue, ObjectValue, NullValue, Int32Value}; use js::jsval::{UInt32Value, DoubleValue, BooleanValue, UndefinedValue}; use js::{JSPROP_ENUMERATE, JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS}; use js::{JSPROP_PERMANENT, JSID_VOID, JSPROP_NATIVE_ACCESSORS, JSPROP_GETTER}; @@ -196,22 +196,6 @@ pub fn jsval_to_domstring(cx: *JSContext, v: JSVal) -> Result, } } -pub unsafe fn str_to_jsval(cx: *JSContext, string: DOMString) -> JSVal { - let string_utf16 = string.to_utf16(); - let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t); - if jsstr.is_null() { - fail!("JS_NewUCStringCopyN failed"); - } - StringValue(&*jsstr) -} - -pub unsafe fn domstring_to_jsval(cx: *JSContext, string: Option) -> JSVal { - match string { - None => NullValue(), - Some(s) => str_to_jsval(cx, s), - } -} - // We use slot 0 for holding the raw object. This is safe for both // globals and non-globals. pub static DOM_OBJECT_SLOT: uint = 0; From 58382471d50552372d6fcc41d7f6167379f37a94 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 6 Mar 2014 10:56:35 +0100 Subject: [PATCH 4/4] Replace jsval_to_str and jsval_to_domstring with a FromJSValConvertible implementation. --- .../dom/bindings/codegen/CodegenRust.py | 39 +++++++++---------- .../script/dom/bindings/conversions.rs | 31 ++++++++++++++- src/components/script/dom/bindings/utils.rs | 35 ----------------- 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 6b377dae22a..abd75587478 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1020,30 +1020,29 @@ for (uint32_t i = 0; i < length; ++i) { } if treatNullAs not in treatAs: raise TypeError("We don't support [TreatNullAs=%s]" % treatNullAs) - nullBehavior = treatAs[treatNullAs] + if type.nullable(): + nullBehavior = "()" + else: + nullBehavior = treatAs[treatNullAs] - def getConversionCode(varName, isOptional=False): + def getConversionCode(isOptional=False): strval = "strval" if isOptional: strval = "Some(%s)" % strval - if type.nullable(): - call = "jsval_to_domstring(cx, ${val})" - else: - call = "jsval_to_str(cx, ${val}, %s)" % nullBehavior + conversionCode = ( - "let strval = %s;\n" - "if strval.is_err() {\n" - " return 0;\n" - "}\n" - "let strval = strval.unwrap();\n" - "%s = %s;" % (call, varName, strval)) + "match FromJSValConvertible::from_jsval(cx, ${val}, %s) {\n" + " Ok(strval) => ${declName} = %s,\n" + " Err(_) => return 0,\n" + "}" % (nullBehavior, strval)) + if defaultValue is None: return conversionCode if isinstance(defaultValue, IDLNullValue): assert(type.nullable()) return handleDefault(conversionCode, - "%s.SetNull()" % varName) + "${declName}.SetNull()") value = "str::from_utf8(data).to_owned()" if type.nullable(): @@ -1051,10 +1050,10 @@ for (uint32_t i = 0; i < length; ++i) { default = ( "static data: [u8, ..%s] = [ %s ];\n" - "%s = %s" % + "${declName} = %s" % (len(defaultValue.value) + 1, ", ".join(["'" + char + "' as u8" for char in defaultValue.value] + ["0"]), - varName, value)) + value)) return handleDefault(conversionCode, default) @@ -1062,7 +1061,7 @@ for (uint32_t i = 0; i < length; ++i) { # We have to make a copy, because our jsval may well not # live as long as our string needs to. declType = CGGeneric("DOMString") - return ("%s\n" % getConversionCode("${declName}"), + return ("%s\n" % getConversionCode(), declType, None, isOptional, None) declType = "DOMString" @@ -1076,8 +1075,7 @@ for (uint32_t i = 0; i < length; ++i) { return ( "%s\n" % - #"const_cast<%s&>(${declName}) = &${holderName};" % - (getConversionCode("${declName}", isOptional)), + (getConversionCode(isOptional)), CGGeneric(declType), None, #CGGeneric("FakeDependentString"), False, initialValue) @@ -5255,11 +5253,11 @@ class CGBindingRoot(CGThing): 'dom::bindings::utils::{CreateDOMGlobal, CreateInterfaceObjects2}', 'dom::bindings::utils::{ConstantSpec, cx_for_dom_object, Default}', 'dom::bindings::utils::{dom_object_slot, DOM_OBJECT_SLOT, DOMClass}', - 'dom::bindings::utils::{DOMJSClass, Empty}', + 'dom::bindings::utils::{DOMJSClass}', 'dom::bindings::utils::{FindEnumStringIndex, GetArrayIndexFromId}', 'dom::bindings::utils::{GetPropertyOnPrototype, GetProtoOrIfaceArray}', 'dom::bindings::utils::{GetReflector, HasPropertyOnPrototype, IntVal}', - 'dom::bindings::utils::{jsid_to_str, jsval_to_domstring, jsval_to_str}', + 'dom::bindings::utils::{jsid_to_str}', 'dom::bindings::utils::{NativePropertyHooks}', 'dom::bindings::utils::global_object_for_js_object', 'dom::bindings::utils::{Reflectable}', @@ -5272,6 +5270,7 @@ class CGBindingRoot(CGThing): 'dom::bindings::callback::{CallSetup,ExceptionHandling}', 'dom::bindings::callback::{WrapCallThisObject}', 'dom::bindings::conversions::{FromJSValConvertible, ToJSValConvertible}', + 'dom::bindings::conversions::{Default, Empty}', 'dom::bindings::codegen::*', 'dom::bindings::codegen::UnionTypes::*', 'dom::bindings::codegen::UnionConversions::*', diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs index faf7d1c852e..7c4f8406e56 100644 --- a/src/components/script/dom/bindings/conversions.rs +++ b/src/components/script/dom/bindings/conversions.rs @@ -2,13 +2,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use dom::bindings::utils::jsstring_to_str; use servo_util::str::DOMString; use js::jsapi::{JSBool, JSContext}; use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64}; use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32}; use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean}; -use js::jsapi::{JS_NewUCStringCopyN}; +use js::jsapi::{JS_NewUCStringCopyN, JS_ValueToString}; use js::jsval::JSVal; use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value, StringValue}; use js::glue::RUST_JS_NumberValue; @@ -193,6 +194,34 @@ impl ToJSValConvertible for DOMString { } } +#[deriving(Eq)] +pub enum StringificationBehavior { + Default, + Empty, +} + +impl Default for StringificationBehavior { + fn default() -> StringificationBehavior { + Default + } +} + +impl FromJSValConvertible for DOMString { + fn from_jsval(cx: *JSContext, value: JSVal, nullBehavior: StringificationBehavior) -> Result { + if nullBehavior == Empty && value.is_null() { + Ok(~"") + } else { + let jsstr = unsafe { JS_ValueToString(cx, value) }; + if jsstr.is_null() { + debug!("JS_ValueToString failed"); + Err(()) + } else { + Ok(jsstring_to_str(cx, jsstr)) + } + } + } +} + impl ToJSValConvertible for Option { fn to_jsval(&self, cx: *JSContext) -> JSVal { match self { diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index 24bd910ff00..a6fc3b57f6c 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -161,41 +161,6 @@ pub fn jsid_to_str(cx: *JSContext, id: jsid) -> DOMString { } } -#[deriving(Eq)] -pub enum StringificationBehavior { - Default, - Empty, -} - -pub fn jsval_to_str(cx: *JSContext, v: JSVal, - nullBehavior: StringificationBehavior) -> Result { - if v.is_null() && nullBehavior == Empty { - Ok(~"") - } else { - let jsstr = unsafe { JS_ValueToString(cx, v) }; - if jsstr.is_null() { - debug!("JS_ValueToString failed"); - Err(()) - } else { - Ok(jsstring_to_str(cx, jsstr)) - } - } -} - -pub fn jsval_to_domstring(cx: *JSContext, v: JSVal) -> Result, ()> { - if v.is_null_or_undefined() { - Ok(None) - } else { - let jsstr = unsafe { JS_ValueToString(cx, v) }; - if jsstr.is_null() { - debug!("JS_ValueToString failed"); - Err(()) - } else { - Ok(Some(jsstring_to_str(cx, jsstr))) - } - } -} - // We use slot 0 for holding the raw object. This is safe for both // globals and non-globals. pub static DOM_OBJECT_SLOT: uint = 0;