Skip to content

Commit

Permalink
Use the conversion traits from js.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Nov 12, 2015
1 parent acb24e8 commit 6d2ae85
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 651 deletions.
13 changes: 13 additions & 0 deletions components/plugins/reflector.rs
Expand Up @@ -48,6 +48,19 @@ pub fn expand_reflector(cx: &mut ExtCtxt, span: Span, _: &MetaItem, annotatable:
impl_item.map(|it| push(Annotatable::Item(it)))
}
};

let impl_item = quote_item!(cx,
impl ::js::conversions::ToJSValConvertible for $struct_name {
#[allow(unsafe_code)]
unsafe fn to_jsval(&self,
cx: *mut ::js::jsapi::JSContext,
rval: ::js::jsapi::MutableHandleValue) {
let object = ::dom::bindings::reflector::Reflectable::reflector(self).get_jsobject();
object.to_jsval(cx, rval)
}
}
);
impl_item.map(|it| push(Annotatable::Item(it)));
} else {
cx.span_err(span, "#[dom_struct] seems to have been applied to a non-struct");
}
Expand Down
56 changes: 30 additions & 26 deletions components/script/devtools.rs
Expand Up @@ -28,33 +28,37 @@ use uuid::Uuid;

#[allow(unsafe_code)]
pub fn handle_evaluate_js(global: &GlobalRef, eval: String, reply: IpcSender<EvaluateJSReply>) {
let cx = global.get_cx();
let mut rval = RootedValue::new(cx, UndefinedValue());
global.evaluate_js_on_global_with_result(&eval, rval.handle_mut());

reply.send(if rval.ptr.is_undefined() {
EvaluateJSReply::VoidValue
} else if rval.ptr.is_boolean() {
EvaluateJSReply::BooleanValue(rval.ptr.to_boolean())
} else if rval.ptr.is_double() || rval.ptr.is_int32() {
EvaluateJSReply::NumberValue(
FromJSValConvertible::from_jsval(cx, rval.handle(), ()).unwrap())
} else if rval.ptr.is_string() {
EvaluateJSReply::StringValue(jsstring_to_str(cx, rval.ptr.to_string()).0)
} else if rval.ptr.is_null() {
EvaluateJSReply::NullValue
} else {
assert!(rval.ptr.is_object());

let obj = RootedObject::new(cx, rval.ptr.to_object());
let class_name = unsafe { CStr::from_ptr(ObjectClassName(cx, obj.handle())) };
let class_name = str::from_utf8(class_name.to_bytes()).unwrap();

EvaluateJSReply::ActorValue {
class: class_name.to_owned(),
uuid: Uuid::new_v4().to_string(),
// global.get_cx() returns a valid `JSContext` pointer, so this is safe.
let result = unsafe {
let cx = global.get_cx();
let mut rval = RootedValue::new(cx, UndefinedValue());
global.evaluate_js_on_global_with_result(&eval, rval.handle_mut());

if rval.ptr.is_undefined() {
EvaluateJSReply::VoidValue
} else if rval.ptr.is_boolean() {
EvaluateJSReply::BooleanValue(rval.ptr.to_boolean())
} else if rval.ptr.is_double() || rval.ptr.is_int32() {
EvaluateJSReply::NumberValue(
FromJSValConvertible::from_jsval(cx, rval.handle(), ()).unwrap())
} else if rval.ptr.is_string() {
EvaluateJSReply::StringValue(jsstring_to_str(cx, rval.ptr.to_string()).0)
} else if rval.ptr.is_null() {
EvaluateJSReply::NullValue
} else {
assert!(rval.ptr.is_object());

let obj = RootedObject::new(cx, rval.ptr.to_object());
let class_name = CStr::from_ptr(ObjectClassName(cx, obj.handle()));
let class_name = str::from_utf8(class_name.to_bytes()).unwrap();

EvaluateJSReply::ActorValue {
class: class_name.to_owned(),
uuid: Uuid::new_v4().to_string(),
}
}
}).unwrap();
};
reply.send(result).unwrap();
}

pub fn handle_get_root_node(page: &Rc<Page>, pipeline: PipelineId, reply: IpcSender<NodeInfo>) {
Expand Down
44 changes: 21 additions & 23 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -3413,7 +3413,7 @@ def __init__(self, enum):
];
impl ToJSValConvertible for super::%s {
fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
strings[*self as usize].to_jsval(cx, rval);
}
}
Expand Down Expand Up @@ -3534,7 +3534,7 @@ def define(self):
}
impl ToJSValConvertible for %s {
fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
match *self {
%s
}
Expand Down Expand Up @@ -3644,12 +3644,12 @@ def get_match(name):
names.append(name)

conversions.append(CGGeneric(
"unsafe { throw_not_in_union(cx, \"%s\"); }\n"
"throw_not_in_union(cx, \"%s\");\n"
"Err(())" % ", ".join(names)))
method = CGWrapper(
CGIndenter(CGList(conversions, "\n\n")),
pre="fn from_jsval(cx: *mut JSContext,\n"
" value: HandleValue, _option: ()) -> Result<%s, ()> {\n" % self.type,
pre="unsafe fn from_jsval(cx: *mut JSContext,\n"
" value: HandleValue, _option: ()) -> Result<%s, ()> {\n" % self.type,
post="\n}")
return CGWrapper(
CGIndenter(CGList([
Expand All @@ -3666,7 +3666,7 @@ def try_method(self, t):

return CGWrapper(
CGIndenter(jsConversion, 4),
pre="fn TryConvertTo%s(cx: *mut JSContext, value: HandleValue) -> %s {\n" % (t.name, returnType),
pre="unsafe fn TryConvertTo%s(cx: *mut JSContext, value: HandleValue) -> %s {\n" % (t.name, returnType),
post="\n}")

def define(self):
Expand Down Expand Up @@ -4967,8 +4967,8 @@ def memberInsert(memberInfo):
"}\n"
"\n"
"impl ToJSValConvertible for ${selfName} {\n"
" fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {\n"
" let obj = unsafe { RootedObject::new(cx, JS_NewObject(cx, ptr::null())) };\n"
" unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {\n"
" let obj = RootedObject::new(cx, JS_NewObject(cx, ptr::null()));\n"
"${insertMembers}"
" rval.set(ObjectOrNullValue(obj.ptr))\n"
" }\n"
Expand Down Expand Up @@ -5459,7 +5459,7 @@ def __init__(self, callback):
}
impl ToJSValConvertible for ${type} {
fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
self.callback().to_jsval(cx, rval);
}
}\
Expand Down Expand Up @@ -5563,10 +5563,10 @@ def getImpl(self):
"${convertArgs}"
"${doCall}"
"${returnResult}").substitute(replacements)
return CGList([
return CGWrapper(CGIndenter(CGList([
CGGeneric(pre),
CGGeneric(body),
], "\n").define()
], "\n"), 4), pre="unsafe {\n", post="\n}").define()

def getResultConversion(self):
replacements = {
Expand Down Expand Up @@ -5709,15 +5709,13 @@ def getCall(self):
replacements["argc"] = "0"
return string.Template(
"${getCallable}"
"let ok = unsafe {\n"
" let rootedThis = RootedObject::new(cx, ${thisObj});\n"
" JS_CallFunctionValue(\n"
" cx, rootedThis.handle(), callable.handle(),\n"
" &HandleValueArray {\n"
" length_: ${argc} as ::libc::size_t,\n"
" elements_: ${argv}\n"
" }, rval.handle_mut())\n"
"};\n"
"let rootedThis = RootedObject::new(cx, ${thisObj});\n"
"let ok = JS_CallFunctionValue(\n"
" cx, rootedThis.handle(), callable.handle(),\n"
" &HandleValueArray {\n"
" length_: ${argc} as ::libc::size_t,\n"
" elements_: ${argv}\n"
" }, rval.handle_mut());\n"
"if !ok {\n"
" return Err(JSFailed);\n"
"}\n").substitute(replacements)
Expand All @@ -5732,7 +5730,7 @@ def getThisObj(self):
return "aThisObj.get()"

def getCallableDecl(self):
return "let callable = RootedValue::new(cx, ObjectValue(unsafe {&*self.parent.callback()}));\n"
return "let callable = RootedValue::new(cx, ObjectValue(&*self.parent.callback()));\n"


class CallbackOperationBase(CallbackMethod):
Expand Down Expand Up @@ -5762,11 +5760,11 @@ def getCallableDecl(self):
if not self.singleOperation:
return 'JS::Rooted<JS::Value> callable(cx);\n' + getCallableFromProp
return (
'let isCallable = unsafe { IsCallable(self.parent.callback()) };\n'
'let isCallable = IsCallable(self.parent.callback());\n'
'let callable =\n' +
CGIndenter(
CGIfElseWrapper('isCallable',
CGGeneric('unsafe { RootedValue::new(cx, ObjectValue(&*self.parent.callback())) }'),
CGGeneric('RootedValue::new(cx, ObjectValue(&*self.parent.callback()))'),
CGGeneric(getCallableFromProp))).define() + ';\n')


Expand Down

0 comments on commit 6d2ae85

Please sign in to comment.