Make GetCallableProperty more rustic.

This commit is contained in:
Ms2ger 2014-04-10 15:05:52 +02:00
parent 986664f527
commit d11316fa05
2 changed files with 16 additions and 16 deletions

View file

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

View file

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