Skip to content

Commit

Permalink
Modify *::get_cx methods to return a safe JSContext instead of a raw one
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Jul 24, 2019
1 parent 2c5d0a6 commit 88cacfb
Show file tree
Hide file tree
Showing 43 changed files with 306 additions and 321 deletions.
32 changes: 17 additions & 15 deletions components/script/body.rs
Expand Up @@ -13,8 +13,8 @@ use crate::dom::blob::{Blob, BlobImpl};
use crate::dom::formdata::FormData;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::script_runtime::JSContext;
use js::jsapi::Heap;
use js::jsapi::JSContext;
use js::jsapi::JSObject;
use js::jsapi::JS_ClearPendingException;
use js::jsapi::Value as JSValue;
Expand Down Expand Up @@ -122,7 +122,7 @@ fn run_package_data_algorithm<T: BodyOperations + DomObject>(
BodyType::Json => run_json_data_algorithm(cx, bytes),
BodyType::Blob => run_blob_data_algorithm(&global, bytes, mime),
BodyType::FormData => run_form_data_algorithm(&global, bytes, mime),
BodyType::ArrayBuffer => unsafe { run_array_buffer_data_algorithm(cx, bytes) },
BodyType::ArrayBuffer => run_array_buffer_data_algorithm(cx, bytes),
}
}

Expand All @@ -133,20 +133,20 @@ fn run_text_data_algorithm(bytes: Vec<u8>) -> Fallible<FetchedData> {
}

#[allow(unsafe_code)]
fn run_json_data_algorithm(cx: *mut JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> {
fn run_json_data_algorithm(cx: JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> {
let json_text = String::from_utf8_lossy(&bytes);
let json_text: Vec<u16> = json_text.encode_utf16().collect();
rooted!(in(cx) let mut rval = UndefinedValue());
rooted!(in(*cx) let mut rval = UndefinedValue());
unsafe {
if !JS_ParseJSON(
cx,
*cx,
json_text.as_ptr(),
json_text.len() as u32,
rval.handle_mut(),
) {
rooted!(in(cx) let mut exception = UndefinedValue());
assert!(JS_GetPendingException(cx, exception.handle_mut()));
JS_ClearPendingException(cx);
rooted!(in(*cx) let mut exception = UndefinedValue());
assert!(JS_GetPendingException(*cx, exception.handle_mut()));
JS_ClearPendingException(*cx);
return Ok(FetchedData::JSException(RootedTraceableBox::from_box(
Heap::boxed(exception.get()),
)));
Expand Down Expand Up @@ -200,13 +200,15 @@ fn run_form_data_algorithm(
}

#[allow(unsafe_code)]
unsafe fn run_array_buffer_data_algorithm(
cx: *mut JSContext,
bytes: Vec<u8>,
) -> Fallible<FetchedData> {
rooted!(in(cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
let arraybuffer =
ArrayBuffer::create(cx, CreateWith::Slice(&bytes), array_buffer_ptr.handle_mut());
fn run_array_buffer_data_algorithm(cx: JSContext, bytes: Vec<u8>) -> Fallible<FetchedData> {
rooted!(in(*cx) let mut array_buffer_ptr = ptr::null_mut::<JSObject>());
let arraybuffer = unsafe {
ArrayBuffer::create(
*cx,
CreateWith::Slice(&bytes),
array_buffer_ptr.handle_mut(),
)
};
if arraybuffer.is_err() {
return Err(Error::JSFailed);
}
Expand Down
4 changes: 2 additions & 2 deletions components/script/compartments.rs
Expand Up @@ -12,7 +12,7 @@ impl AlreadyInCompartment {
#![allow(unsafe_code)]
pub fn assert(global: &GlobalScope) -> AlreadyInCompartment {
unsafe {
assert!(!GetCurrentRealmOrNull(global.get_cx()).is_null());
assert!(!GetCurrentRealmOrNull(*global.get_cx()).is_null());
}
AlreadyInCompartment(())
}
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<'a> InCompartment<'a> {

pub fn enter_realm(object: &impl DomObject) -> JSAutoRealm {
JSAutoRealm::new(
object.global().get_cx(),
*object.global().get_cx(),
object.reflector().get_jsobject().get(),
)
}
10 changes: 5 additions & 5 deletions components/script/devtools.rs
Expand Up @@ -36,7 +36,7 @@ pub fn handle_evaluate_js(global: &GlobalScope, eval: String, reply: IpcSender<E
let result = unsafe {
let cx = global.get_cx();
let _ac = enter_realm(global);
rooted!(in(cx) let mut rval = UndefinedValue());
rooted!(in(*cx) let mut rval = UndefinedValue());
global.evaluate_js_on_global_with_result(&eval, rval.handle_mut());

if rval.is_undefined() {
Expand All @@ -45,20 +45,20 @@ pub fn handle_evaluate_js(global: &GlobalScope, eval: String, reply: IpcSender<E
EvaluateJSReply::BooleanValue(rval.to_boolean())
} else if rval.is_double() || rval.is_int32() {
EvaluateJSReply::NumberValue(
match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) {
match FromJSValConvertible::from_jsval(*cx, rval.handle(), ()) {
Ok(ConversionResult::Success(v)) => v,
_ => unreachable!(),
},
)
} else if rval.is_string() {
EvaluateJSReply::StringValue(String::from(jsstring_to_str(cx, rval.to_string())))
EvaluateJSReply::StringValue(String::from(jsstring_to_str(*cx, rval.to_string())))
} else if rval.is_null() {
EvaluateJSReply::NullValue
} else {
assert!(rval.is_object());

rooted!(in(cx) let obj = rval.to_object());
let class_name = CStr::from_ptr(ObjectClassName(cx, obj.handle()));
rooted!(in(*cx) let obj = rval.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 {
Expand Down
14 changes: 7 additions & 7 deletions components/script/dom/audiobuffer.rs
Expand Up @@ -173,15 +173,15 @@ impl AudioBuffer {

// Step 2.
let channel_data = unsafe {
typedarray!(in(cx) let array: Float32Array = channel.get());
typedarray!(in(*cx) let array: Float32Array = channel.get());
if let Ok(array) = array {
let data = array.to_vec();
let mut is_shared = false;
rooted!(in (cx) let view_buffer =
JS_GetArrayBufferViewBuffer(cx, channel.handle(), &mut is_shared));
rooted!(in (*cx) let view_buffer =
JS_GetArrayBufferViewBuffer(*cx, channel.handle(), &mut is_shared));
// This buffer is always created unshared
debug_assert!(!is_shared);
let _ = DetachArrayBuffer(cx, view_buffer.handle());
let _ = DetachArrayBuffer(*cx, view_buffer.handle());
data
} else {
return None;
Expand Down Expand Up @@ -272,7 +272,7 @@ impl AudioBufferMethods for AudioBuffer {
// We either copy form js_channels or shared_channels.
let js_channel = self.js_channels.borrow()[channel_number].get();
if !js_channel.is_null() {
typedarray!(in(cx) let array: Float32Array = js_channel);
typedarray!(in(*cx) let array: Float32Array = js_channel);
if let Ok(array) = array {
let data = unsafe { array.as_slice() };
dest.extend_from_slice(&data[offset..offset + bytes_to_copy]);
Expand Down Expand Up @@ -307,7 +307,7 @@ impl AudioBufferMethods for AudioBuffer {
}

let cx = self.global().get_cx();
if unsafe { !self.restore_js_channel_data(cx) } {
if unsafe { !self.restore_js_channel_data(*cx) } {
return Err(Error::JSFailed);
}

Expand All @@ -317,7 +317,7 @@ impl AudioBufferMethods for AudioBuffer {
return Err(Error::IndexSize);
}

typedarray!(in(cx) let js_channel: Float32Array = js_channel);
typedarray!(in(*cx) let js_channel: Float32Array = js_channel);
if let Ok(mut js_channel) = js_channel {
let bytes_to_copy = min(self.length - start_in_channel, source.len() as u32) as usize;
let js_channel_data = unsafe { js_channel.as_mut_slice() };
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/bindings/callback.rs
Expand Up @@ -255,8 +255,8 @@ impl CallSetup {
let ais = callback.incumbent().map(AutoIncumbentScript::new);
CallSetup {
exception_global: global,
cx: cx,
old_realm: unsafe { EnterRealm(cx, callback.callback()) },
cx: *cx,
old_realm: unsafe { EnterRealm(*cx, callback.callback()) },
handling: handling,
entry_script: Some(aes),
incumbent_script: ais,
Expand Down
14 changes: 7 additions & 7 deletions components/script/dom/bindings/htmlconstructor.rs
Expand Up @@ -100,7 +100,7 @@ where
// Step 2 is checked in the generated caller code

// Step 3
rooted!(in(window.get_cx()) let new_target = call_args.new_target().to_object());
rooted!(in(*window.get_cx()) let new_target = call_args.new_target().to_object());
let definition = match registry.lookup_definition_by_constructor(new_target.handle()) {
Some(definition) => definition,
None => {
Expand All @@ -110,31 +110,31 @@ where
},
};

rooted!(in(window.get_cx()) let callee = UnwrapObjectStatic(call_args.callee()));
rooted!(in(*window.get_cx()) let callee = UnwrapObjectStatic(call_args.callee()));
if callee.is_null() {
return Err(Error::Security);
}

{
let _ac = JSAutoRealm::new(window.get_cx(), callee.get());
rooted!(in(window.get_cx()) let mut constructor = ptr::null_mut::<JSObject>());
rooted!(in(window.get_cx()) let global_object = CurrentGlobalOrNull(window.get_cx()));
let _ac = JSAutoRealm::new(*window.get_cx(), callee.get());
rooted!(in(*window.get_cx()) let mut constructor = ptr::null_mut::<JSObject>());
rooted!(in(*window.get_cx()) let global_object = CurrentGlobalOrNull(*window.get_cx()));

if definition.is_autonomous() {
// Step 4
// Since this element is autonomous, its active function object must be the HTMLElement

// Retrieve the constructor object for HTMLElement
HTMLElementBinding::GetConstructorObject(
SafeJSContext::from_ptr(window.get_cx()),
window.get_cx(),
global_object.handle(),
constructor.handle_mut(),
);
} else {
// Step 5
get_constructor_object_from_local_name(
definition.local_name.clone(),
window.get_cx(),
*window.get_cx(),
global_object.handle(),
constructor.handle_mut(),
);
Expand Down
12 changes: 3 additions & 9 deletions components/script/dom/bindings/reflector.rs
Expand Up @@ -7,7 +7,7 @@
use crate::dom::bindings::conversions::DerivedFrom;
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext as SafeJSContext;
use crate::script_runtime::JSContext;
use js::jsapi::{Heap, JSObject};
use js::rust::HandleObject;
use std::default::Default;
Expand All @@ -17,20 +17,14 @@ use std::default::Default;
pub fn reflect_dom_object<T, U>(
obj: Box<T>,
global: &U,
wrap_fn: unsafe fn(SafeJSContext, &GlobalScope, Box<T>) -> DomRoot<T>,
wrap_fn: unsafe fn(JSContext, &GlobalScope, Box<T>) -> DomRoot<T>,
) -> DomRoot<T>
where
T: DomObject,
U: DerivedFrom<GlobalScope>,
{
let global_scope = global.upcast();
unsafe {
wrap_fn(
SafeJSContext::from_ptr(global_scope.get_cx()),
global_scope,
obj,
)
}
unsafe { wrap_fn(global_scope.get_cx(), global_scope, obj) }
}

/// A struct to store a reference to the reflector of a DOM object.
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/bindings/structuredclone.rs
Expand Up @@ -321,7 +321,7 @@ impl StructuredCloneData {
WriteBytesToJSStructuredCloneData(data as *const u8, nbytes, scdata);

assert!(JS_ReadStructuredClone(
cx,
*cx,
scdata,
JS_STRUCTURED_CLONE_VERSION,
StructuredCloneScope::DifferentProcess,
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/create.rs
Expand Up @@ -157,9 +157,9 @@ fn create_html_element(
// Step 6.1.1
unsafe {
let _ac =
JSAutoRealm::new(cx, global.reflector().get_jsobject().get());
throw_dom_exception(cx, &global, error);
report_pending_exception(cx, true);
JSAutoRealm::new(*cx, global.reflector().get_jsobject().get());
throw_dom_exception(*cx, &global, error);
report_pending_exception(*cx, true);
}

// Step 6.1.2
Expand Down

0 comments on commit 88cacfb

Please sign in to comment.