Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update js. #8428

Merged
merged 3 commits into from Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
55 changes: 24 additions & 31 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -888,21 +888,16 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,
else:
handleInvalidEnumValueCode = "return true;"

transmute = "mem::transmute(index)"
if isMember == 'Dictionary':
transmute = 'unsafe { ' + transmute + ' }'

template = (
"match find_enum_string_index(cx, ${val}, %(values)s) {\n"
" Err(_) => { %(exceptionCode)s },\n"
" Ok(None) => { %(handleInvalidEnumValueCode)s },\n"
" Ok(Some(index)) => {\n"
" //XXXjdm need some range checks up in here.\n"
" %(transmute)s\n"
" mem::transmute(index)\n"
" },\n"
"}" % {"values": enum + "Values::strings",
"exceptionCode": exceptionCode,
"transmute": transmute,
"handleInvalidEnumValueCode": handleInvalidEnumValueCode})

if defaultValue is not None:
Expand Down Expand Up @@ -3418,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 @@ -3539,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 @@ -3653,8 +3648,8 @@ def get_match(name):
"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 @@ -3671,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 @@ -4952,10 +4947,10 @@ def memberInsert(memberInfo):

return string.Template(
"impl ${selfName} {\n"
" pub fn empty(cx: *mut JSContext) -> ${selfName} {\n"
" pub unsafe fn empty(cx: *mut JSContext) -> ${selfName} {\n"
" ${selfName}::new(cx, HandleValue::null()).unwrap()\n"
" }\n"
" pub fn new(cx: *mut JSContext, val: HandleValue) -> Result<${selfName}, ()> {\n"
" pub unsafe fn new(cx: *mut JSContext, val: HandleValue) -> Result<${selfName}, ()> {\n"
" let object = if val.get().is_null_or_undefined() {\n"
" RootedObject::new(cx, ptr::null_mut())\n"
" } else if val.get().is_object() {\n"
Expand All @@ -4972,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 @@ -5159,6 +5154,7 @@ def __init__(self, config, prefix, webIDLFile):
'js::{JSCLASS_RESERVED_SLOTS_MASK}',
'js::{JSPROP_ENUMERATE, JSPROP_SHARED}',
'js::{JSITER_OWNONLY, JSITER_HIDDEN, JSITER_SYMBOLS}',
'js::error::throw_type_error',
'js::jsapi::{JS_CallFunctionValue, JS_GetClass, JS_GetGlobalForObject}',
'js::jsapi::{JS_GetObjectPrototype, JS_GetProperty, JS_GetPropertyById}',
'js::jsapi::{JS_GetPropertyDescriptorById, JS_GetReservedSlot}',
Expand Down Expand Up @@ -5222,7 +5218,6 @@ def __init__(self, config, prefix, webIDLFile):
'dom::bindings::error::{Fallible, Error, ErrorResult}',
'dom::bindings::error::Error::JSFailed',
'dom::bindings::error::throw_dom_exception',
'dom::bindings::error::throw_type_error',
'dom::bindings::proxyhandler',
'dom::bindings::proxyhandler::{fill_property_descriptor, get_expando_object}',
'dom::bindings::proxyhandler::{get_property_descriptor}',
Expand Down Expand Up @@ -5464,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 @@ -5568,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 @@ -5714,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 @@ -5737,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 @@ -5767,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