mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +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,
|
invalidEnumValueFatal=True,
|
||||||
defaultValue=None,
|
defaultValue=None,
|
||||||
treatNullAs="Default",
|
treatNullAs="Default",
|
||||||
treatUndefinedAs="Default",
|
|
||||||
isEnforceRange=False,
|
isEnforceRange=False,
|
||||||
isClamp=False,
|
isClamp=False,
|
||||||
exceptionCode=None,
|
exceptionCode=None,
|
||||||
|
@ -1058,37 +1057,31 @@ for (uint32_t i = 0; i < length; ++i) {
|
||||||
assert not isEnforceRange and not isClamp
|
assert not isEnforceRange and not isClamp
|
||||||
|
|
||||||
treatAs = {
|
treatAs = {
|
||||||
"Default": "eStringify",
|
"Default": "Default",
|
||||||
"EmptyString": "eEmpty",
|
"EmptyString": "Empty",
|
||||||
"Null": "eNull"
|
|
||||||
}
|
}
|
||||||
if type.nullable():
|
if treatNullAs not in treatAs:
|
||||||
# For nullable strings null becomes a null string.
|
raise TypeError("We don't support [TreatNullAs=%s]" % treatNullAs)
|
||||||
treatNullAs = "Null"
|
|
||||||
# For nullable strings undefined becomes a null string unless
|
|
||||||
# specified otherwise.
|
|
||||||
if treatUndefinedAs == "Default":
|
|
||||||
treatUndefinedAs = "Null"
|
|
||||||
nullBehavior = treatAs[treatNullAs]
|
nullBehavior = treatAs[treatNullAs]
|
||||||
if treatUndefinedAs == "Missing":
|
|
||||||
raise TypeError("We don't support [TreatUndefinedAs=Missing]")
|
|
||||||
undefinedBehavior = treatAs[treatUndefinedAs]
|
|
||||||
|
|
||||||
def getConversionCode(varName, isOptional=False):
|
def getConversionCode(varName, isOptional=False):
|
||||||
#XXXjdm support nullBehavior and undefinedBehavior
|
strval = "strval"
|
||||||
#conversionCode = (
|
if not type.nullable():
|
||||||
# "if (!ConvertJSValueToString(cx, ${val}, ${valPtr}, %s, %s, %s)) {\n"
|
# XXX #1207 Actually pass non-nullable strings to callees.
|
||||||
# " return false;\n"
|
strval = "Some(%s)" % strval
|
||||||
# "}" % (nullBehavior, undefinedBehavior, varName))
|
|
||||||
strval = "Some(strval.unwrap())"
|
|
||||||
if isOptional:
|
if isOptional:
|
||||||
strval = "Some(%s)" % strval
|
strval = "Some(%s)" % strval
|
||||||
|
if type.nullable():
|
||||||
|
call = "jsval_to_domstring(cx, ${val})"
|
||||||
|
else:
|
||||||
|
call = "jsval_to_str(cx, ${val}, %s)" % nullBehavior
|
||||||
conversionCode = (
|
conversionCode = (
|
||||||
"let strval = jsval_to_str(cx, ${val});\n"
|
"let strval = %s;\n"
|
||||||
"if strval.is_err() {\n"
|
"if strval.is_err() {\n"
|
||||||
" return 0;\n"
|
" return 0;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"%s = %s;" % (varName, strval))
|
"let strval = strval.unwrap();\n"
|
||||||
|
"%s = %s;" % (call, varName, strval))
|
||||||
if defaultValue is None:
|
if defaultValue is None:
|
||||||
return conversionCode
|
return conversionCode
|
||||||
|
|
||||||
|
@ -1456,7 +1449,6 @@ class CGArgumentConverter(CGThing):
|
||||||
invalidEnumValueFatal=self.invalidEnumValueFatal,
|
invalidEnumValueFatal=self.invalidEnumValueFatal,
|
||||||
defaultValue=self.argument.defaultValue,
|
defaultValue=self.argument.defaultValue,
|
||||||
treatNullAs=self.argument.treatNullAs,
|
treatNullAs=self.argument.treatNullAs,
|
||||||
treatUndefinedAs=self.argument.treatUndefinedAs,
|
|
||||||
isEnforceRange=self.argument.enforceRange,
|
isEnforceRange=self.argument.enforceRange,
|
||||||
isClamp=self.argument.clamp),
|
isClamp=self.argument.clamp),
|
||||||
self.replacementVariables,
|
self.replacementVariables,
|
||||||
|
@ -3189,7 +3181,6 @@ class FakeArgument():
|
||||||
self.variadic = False
|
self.variadic = False
|
||||||
self.defaultValue = None
|
self.defaultValue = None
|
||||||
self.treatNullAs = interfaceMember.treatNullAs
|
self.treatNullAs = interfaceMember.treatNullAs
|
||||||
self.treatUndefinedAs = interfaceMember.treatUndefinedAs
|
|
||||||
self.enforceRange = False
|
self.enforceRange = False
|
||||||
self.clamp = 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.
|
# arguments[0] is the index or name of the item that we're setting.
|
||||||
argument = arguments[1]
|
argument = arguments[1]
|
||||||
template = getJSToNativeConversionTemplate(argument.type, descriptor,
|
template = getJSToNativeConversionTemplate(argument.type, descriptor,
|
||||||
treatNullAs=argument.treatNullAs,
|
treatNullAs=argument.treatNullAs)
|
||||||
treatUndefinedAs=argument.treatUndefinedAs)
|
|
||||||
templateValues = {
|
templateValues = {
|
||||||
"declName": argument.identifier.name,
|
"declName": argument.identifier.name,
|
||||||
"holderName": argument.identifier.name + "_holder",
|
"holderName": argument.identifier.name + "_holder",
|
||||||
|
@ -5692,7 +5682,7 @@ class CGCallbackInterface(CGCallback):
|
||||||
|
|
||||||
class FakeMember():
|
class FakeMember():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.treatUndefinedAs = self.treatNullAs = "Default"
|
self.treatNullAs = "Default"
|
||||||
def isStatic(self):
|
def isStatic(self):
|
||||||
return False
|
return False
|
||||||
def isAttr(self):
|
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;
|
static TOSTRING_NAME_RESERVED_SLOT: libc::size_t = 1;
|
||||||
|
|
||||||
mod jsval {
|
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::glue::{RUST_JSVAL_IS_STRING, RUST_JSVAL_TO_STRING};
|
||||||
use js::jsapi::{JSVal, JSString};
|
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]
|
#[fixed_stack_segment]
|
||||||
pub fn is_string(v: JSVal) -> bool {
|
pub fn is_string(v: JSVal) -> bool {
|
||||||
unsafe { RUST_JSVAL_IS_STRING(v) == 1 }
|
unsafe { RUST_JSVAL_IS_STRING(v) == 1 }
|
||||||
|
@ -226,24 +237,37 @@ pub fn jsid_to_str(cx: *JSContext, id: jsid) -> ~str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//XXX very incomplete
|
#[deriving(Eq)]
|
||||||
#[fixed_stack_segment]
|
pub enum StringificationBehavior {
|
||||||
pub fn jsval_to_str(cx: *JSContext, v: JSVal) -> Result<~str, ()> {
|
Default,
|
||||||
unsafe {
|
Empty,
|
||||||
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(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let length = 0;
|
#[fixed_stack_segment]
|
||||||
let chars = JS_GetStringCharsAndLength(cx, jsstr, &length);
|
pub fn jsval_to_str(cx: *JSContext, v: JSVal,
|
||||||
do vec::raw::buf_as_slice(chars, length as uint) |char_vec| {
|
nullBehavior: StringificationBehavior) -> Result<~str, ()> {
|
||||||
Ok(str::from_utf16(char_vec))
|
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