servo: Merge #2091 - Make GetCallableProperty more rustic (from Ms2ger:GetCallableProperty); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: 4683788600a8acf0f77d81117c6b1bcf1a1a618f
This commit is contained in:
Ms2ger 2014-04-10 13:58:18 -04:00
Родитель 0aa9a39476
Коммит ca81421b61
2 изменённых файлов: 16 добавлений и 16 удалений

Просмотреть файл

@ -5,7 +5,7 @@
use dom::bindings::utils::Reflectable;
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable};
use js::jsapi::{JS_GetProperty, JSTracer, JS_CallTracer};
use js::jsval::JSVal;
use js::jsval::{JSVal, UndefinedValue};
use js::JSTRACE_OBJECT;
use std::cast;
@ -61,20 +61,20 @@ impl CallbackInterface {
}
}
pub fn GetCallableProperty(&self, cx: *JSContext, name: *libc::c_char, callable: &mut JSVal) -> bool {
pub fn GetCallableProperty(&self, cx: *JSContext, name: &str) -> Result<JSVal, ()> {
let mut callable = UndefinedValue();
unsafe {
if JS_GetProperty(cx, self.callback, name, &*callable) == 0 {
return false;
if name.to_c_str().with_ref(|name| JS_GetProperty(cx, self.callback, name, &mut callable as *mut JSVal as *JSVal)) == 0 {
return Err(());
}
if !callable.is_object() ||
JS_ObjectIsCallable(cx, callable.to_object()) == 0 {
//ThrowErrorMessage(cx, MSG_NOT_CALLABLE, description.get());
return false;
return Err(());
}
return true;
}
Ok(callable)
}
}

Просмотреть файл

@ -5312,19 +5312,19 @@ class CallbackOperationBase(CallbackMethod):
"methodName": self.methodName
}
getCallableFromProp = string.Template(
'if "${methodName}".to_c_str().with_ref(|name| !self.parent.GetCallableProperty(cx, name, &mut callable)) {\n'
' return${errorReturn};\n'
'}\n').substitute(replacements)
'match self.parent.GetCallableProperty(cx, "${methodName}") {\n'
' Err(_) => return${errorReturn},\n'
' Ok(callable) => callable,\n'
'}').substitute(replacements)
if not self.singleOperation:
return 'JS::Rooted<JS::Value> callable(cx);\n' + getCallableFromProp
return (
'let isCallable = unsafe { JS_ObjectIsCallable(cx, self.parent.callback) != 0 };\n'
'let mut callable = UndefinedValue();\n'
'if isCallable {\n'
' callable = unsafe { ObjectValue(&*self.parent.callback) };\n'
'} else {\n'
'%s'
'}\n' % CGIndenter(CGGeneric(getCallableFromProp)).define())
'let callable =\n' +
CGIndenter(
CGIfElseWrapper('isCallable',
CGGeneric('unsafe { ObjectValue(&*self.parent.callback) }'),
CGGeneric(getCallableFromProp))).define() + ';\n')
class CallbackOperation(CallbackOperationBase):
"""