mirror of
https://github.com/servo/servo.git
synced 2025-06-24 17:14:33 +01:00
Rewrite jsval_to_str and introduce jsval_to_domstring.
This commit is contained in:
parent
255864a843
commit
793d87f07d
2 changed files with 58 additions and 44 deletions
|
@ -443,7 +443,6 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
|
|||
invalidEnumValueFatal=True,
|
||||
defaultValue=None,
|
||||
treatNullAs="Default",
|
||||
treatUndefinedAs="Default",
|
||||
isEnforceRange=False,
|
||||
isClamp=False,
|
||||
exceptionCode=None,
|
||||
|
@ -1058,37 +1057,31 @@ for (uint32_t i = 0; i < length; ++i) {
|
|||
assert not isEnforceRange and not isClamp
|
||||
|
||||
treatAs = {
|
||||
"Default": "eStringify",
|
||||
"EmptyString": "eEmpty",
|
||||
"Null": "eNull"
|
||||
"Default": "Default",
|
||||
"EmptyString": "Empty",
|
||||
}
|
||||
if type.nullable():
|
||||
# For nullable strings null becomes a null string.
|
||||
treatNullAs = "Null"
|
||||
# For nullable strings undefined becomes a null string unless
|
||||
# specified otherwise.
|
||||
if treatUndefinedAs == "Default":
|
||||
treatUndefinedAs = "Null"
|
||||
if treatNullAs not in treatAs:
|
||||
raise TypeError("We don't support [TreatNullAs=%s]" % treatNullAs)
|
||||
nullBehavior = treatAs[treatNullAs]
|
||||
if treatUndefinedAs == "Missing":
|
||||
raise TypeError("We don't support [TreatUndefinedAs=Missing]")
|
||||
undefinedBehavior = treatAs[treatUndefinedAs]
|
||||
|
||||
def getConversionCode(varName, isOptional=False):
|
||||
#XXXjdm support nullBehavior and undefinedBehavior
|
||||
#conversionCode = (
|
||||
# "if (!ConvertJSValueToString(cx, ${val}, ${valPtr}, %s, %s, %s)) {\n"
|
||||
# " return false;\n"
|
||||
# "}" % (nullBehavior, undefinedBehavior, varName))
|
||||
strval = "Some(strval.unwrap())"
|
||||
strval = "strval"
|
||||
if not type.nullable():
|
||||
# XXX #1207 Actually pass non-nullable strings to callees.
|
||||
strval = "Some(%s)" % 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 = jsval_to_str(cx, ${val});\n"
|
||||
"let strval = %s;\n"
|
||||
"if strval.is_err() {\n"
|
||||
" return 0;\n"
|
||||
"}\n"
|
||||
"%s = %s;" % (varName, strval))
|
||||
"let strval = strval.unwrap();\n"
|
||||
"%s = %s;" % (call, varName, strval))
|
||||
if defaultValue is None:
|
||||
return conversionCode
|
||||
|
||||
|
@ -1456,7 +1449,6 @@ class CGArgumentConverter(CGThing):
|
|||
invalidEnumValueFatal=self.invalidEnumValueFatal,
|
||||
defaultValue=self.argument.defaultValue,
|
||||
treatNullAs=self.argument.treatNullAs,
|
||||
treatUndefinedAs=self.argument.treatUndefinedAs,
|
||||
isEnforceRange=self.argument.enforceRange,
|
||||
isClamp=self.argument.clamp),
|
||||
self.replacementVariables,
|
||||
|
@ -3189,7 +3181,6 @@ class FakeArgument():
|
|||
self.variadic = False
|
||||
self.defaultValue = None
|
||||
self.treatNullAs = interfaceMember.treatNullAs
|
||||
self.treatUndefinedAs = interfaceMember.treatUndefinedAs
|
||||
self.enforceRange = False
|
||||
self.clamp = False
|
||||
|
||||
|
@ -4089,8 +4080,7 @@ class CGProxySpecialOperation(CGPerSignatureCall):
|
|||
# arguments[0] is the index or name of the item that we're setting.
|
||||
argument = arguments[1]
|
||||
template = getJSToNativeConversionTemplate(argument.type, descriptor,
|
||||
treatNullAs=argument.treatNullAs,
|
||||
treatUndefinedAs=argument.treatUndefinedAs)
|
||||
treatNullAs=argument.treatNullAs)
|
||||
templateValues = {
|
||||
"declName": argument.identifier.name,
|
||||
"holderName": argument.identifier.name + "_holder",
|
||||
|
@ -5692,7 +5682,7 @@ class CGCallbackInterface(CGCallback):
|
|||
|
||||
class FakeMember():
|
||||
def __init__(self):
|
||||
self.treatUndefinedAs = self.treatNullAs = "Default"
|
||||
self.treatNullAs = "Default"
|
||||
def isStatic(self):
|
||||
return False
|
||||
def isAttr(self):
|
||||
|
|
|
@ -41,9 +41,20 @@ static TOSTRING_CLASS_RESERVED_SLOT: libc::size_t = 0;
|
|||
static TOSTRING_NAME_RESERVED_SLOT: libc::size_t = 1;
|
||||
|
||||
mod jsval {
|
||||
use js::glue::{RUST_JSVAL_IS_NULL, RUST_JSVAL_IS_VOID};
|
||||
use js::glue::{RUST_JSVAL_IS_STRING, RUST_JSVAL_TO_STRING};
|
||||
use js::jsapi::{JSVal, JSString};
|
||||
|
||||
#[fixed_stack_segment]
|
||||
pub fn is_null(v: JSVal) -> bool {
|
||||
unsafe { RUST_JSVAL_IS_NULL(v) == 1 }
|
||||
}
|
||||
|
||||
#[fixed_stack_segment]
|
||||
pub fn is_undefined(v: JSVal) -> bool {
|
||||
unsafe { RUST_JSVAL_IS_VOID(v) == 1 }
|
||||
}
|
||||
|
||||
#[fixed_stack_segment]
|
||||
pub fn is_string(v: JSVal) -> bool {
|
||||
unsafe { RUST_JSVAL_IS_STRING(v) == 1 }
|
||||
|
@ -226,24 +237,37 @@ pub fn jsid_to_str(cx: *JSContext, id: jsid) -> ~str {
|
|||
}
|
||||
}
|
||||
|
||||
//XXX very incomplete
|
||||
#[fixed_stack_segment]
|
||||
pub fn jsval_to_str(cx: *JSContext, v: JSVal) -> Result<~str, ()> {
|
||||
unsafe {
|
||||
let jsstr;
|
||||
if RUST_JSVAL_IS_STRING(v) == 1 {
|
||||
jsstr = RUST_JSVAL_TO_STRING(v)
|
||||
} else {
|
||||
jsstr = JS_ValueToString(cx, v);
|
||||
if jsstr.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
#[deriving(Eq)]
|
||||
pub enum StringificationBehavior {
|
||||
Default,
|
||||
Empty,
|
||||
}
|
||||
|
||||
let length = 0;
|
||||
let chars = JS_GetStringCharsAndLength(cx, jsstr, &length);
|
||||
do vec::raw::buf_as_slice(chars, length as uint) |char_vec| {
|
||||
Ok(str::from_utf16(char_vec))
|
||||
#[fixed_stack_segment]
|
||||
pub fn jsval_to_str(cx: *JSContext, v: JSVal,
|
||||
nullBehavior: StringificationBehavior) -> Result<~str, ()> {
|
||||
if jsval::is_null(v) && nullBehavior == Empty {
|
||||
Ok(~"")
|
||||
} else {
|
||||
let jsstr = unsafe { JS_ValueToString(cx, v) };
|
||||
if jsstr.is_null() {
|
||||
Err(())
|
||||
} else {
|
||||
Ok(jsstring_to_str(cx, jsstr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[fixed_stack_segment]
|
||||
pub fn jsval_to_domstring(cx: *JSContext, v: JSVal) -> Result<DOMString, ()> {
|
||||
if jsval::is_null(v) || jsval::is_undefined(v) {
|
||||
Ok(None)
|
||||
} else {
|
||||
let jsstr = unsafe { JS_ValueToString(cx, v) };
|
||||
if jsstr.is_null() {
|
||||
Err(())
|
||||
} else {
|
||||
Ok(Some(jsstring_to_str(cx, jsstr)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue