Navigation Menu

Skip to content

Commit

Permalink
Use Heap for dictionary and union members.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Feb 16, 2017
1 parent 5eaa19b commit 8ce9ca6
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 27 deletions.
29 changes: 19 additions & 10 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -1031,21 +1031,22 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,

if type.isAny():
assert not isEnforceRange and not isClamp
assert isMember != "Union"

if isMember == "Dictionary":
# TODO: Need to properly root dictionaries
# https://github.com/servo/servo/issues/6381
declType = CGGeneric("JSVal")
declType = CGGeneric("Heap<JSVal>")

if defaultValue is None:
default = None
elif isinstance(defaultValue, IDLNullValue):
default = "NullValue()"
default = "Heap::new(NullValue())"
elif isinstance(defaultValue, IDLUndefinedValue):
default = "UndefinedValue()"
default = "Heap::new(UndefinedValue())"
else:
raise TypeError("Can't handle non-null, non-undefined default value here")
return handleOptional("${val}", declType, default)
return handleOptional("Heap::new(${val}.get())", declType, default)

declType = CGGeneric("HandleValue")

Expand All @@ -1065,13 +1066,22 @@ def wrapObjectTemplate(templateBody, nullValue, isDefinitelyObject, type,

# TODO: Need to root somehow
# https://github.com/servo/servo/issues/6382
declType = CGGeneric("*mut JSObject")
default = "ptr::null_mut()"
templateBody = wrapObjectTemplate("${val}.get().to_object()",
"ptr::null_mut()",
default,
isDefinitelyObject, type, failureCode)

if isMember in ("Dictionary", "Union"):
declType = CGGeneric("Heap<*mut JSObject>")
templateBody = "Heap::new(%s)" % templateBody
default = "Heap::new(%s)" % default
else:
# TODO: Need to root somehow
# https://github.com/servo/servo/issues/6382
declType = CGGeneric("*mut JSObject")

return handleOptional(templateBody, declType,
handleDefaultNull("ptr::null_mut()"))
handleDefaultNull(default))

if type.isDictionary():
# There are no nullable dictionaries
Expand Down Expand Up @@ -2230,6 +2240,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
'dom::types::*',
'js::error::throw_type_error',
'js::jsapi::HandleValue',
'js::jsapi::Heap',
'js::jsapi::JSContext',
'js::jsapi::JSObject',
'js::jsapi::MutableHandleValue',
Expand Down Expand Up @@ -4049,7 +4060,7 @@ def getUnionTypeTemplateVars(type, descriptorProvider):
typeName = builtinNames[type.tag()]
elif type.isObject():
name = type.name
typeName = "*mut JSObject"
typeName = "Heap<*mut JSObject>"
else:
raise TypeError("Can't handle %s in unions yet" % type)

Expand Down Expand Up @@ -5993,8 +6004,6 @@ def indent(s):
default = info.default
replacements = {"val": "rval.handle()"}
conversion = string.Template(templateBody).substitute(replacements)
if memberType.isAny():
conversion = "%s.get()" % conversion

assert (member.defaultValue is None) == (default is None)
if not member.optional:
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/bindings/iterable.rs
Expand Up @@ -15,7 +15,7 @@ use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::trace::JSTraceable;
use dom::globalscope::GlobalScope;
use js::conversions::ToJSValConvertible;
use js::jsapi::{HandleValue, JSContext, JSObject, MutableHandleObject};
use js::jsapi::{HandleValue, Heap, JSContext, JSObject, MutableHandleObject};
use js::jsval::UndefinedValue;
use std::cell::Cell;
use std::ptr;
Expand Down Expand Up @@ -116,7 +116,7 @@ fn dict_return(cx: *mut JSContext,
value: HandleValue) -> Fallible<()> {
let mut dict = unsafe { IterableKeyOrValueResult::empty(cx) };
dict.done = done;
dict.value = value.get();
dict.value.set(value.get());
rooted!(in(cx) let mut dict_value = UndefinedValue());
unsafe {
dict.to_jsval(cx, dict_value.handle_mut());
Expand All @@ -131,7 +131,7 @@ fn key_and_value_return(cx: *mut JSContext,
value: HandleValue) -> Fallible<()> {
let mut dict = unsafe { IterableKeyAndValueResult::empty(cx) };
dict.done = false;
dict.value = Some(vec![key.get(), value.get()]);
dict.value = Some(vec![Heap::new(key.get()), Heap::new(value.get())]);
rooted!(in(cx) let mut dict_value = UndefinedValue());
unsafe {
dict.to_jsval(cx, dict_value.handle_mut());
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/customevent.rs
Expand Up @@ -57,7 +57,7 @@ impl CustomEvent {
Atom::from(type_),
init.parent.bubbles,
init.parent.cancelable,
unsafe { HandleValue::from_marked_location(&init.detail) }))
init.detail.handle()))
}

fn init_custom_event(&self,
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/errorevent.rs
Expand Up @@ -93,7 +93,7 @@ impl ErrorEvent {

// Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381
rooted!(in(global.get_cx()) let error = init.error);
rooted!(in(global.get_cx()) let error = init.error.get());
let event = ErrorEvent::new(
global,
Atom::from(type_),
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/extendablemessageevent.rs
Expand Up @@ -50,7 +50,7 @@ impl ExtendableMessageEvent {
init: &ExtendableMessageEventBinding::ExtendableMessageEventInit)
-> Fallible<Root<ExtendableMessageEvent>> {
let global = worker.upcast::<GlobalScope>();
rooted!(in(global.get_cx()) let data = init.data);
rooted!(in(global.get_cx()) let data = init.data.get());
let ev = ExtendableMessageEvent::new(global,
Atom::from(type_),
init.parent.parent.bubbles,
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/filereader.rs
Expand Up @@ -344,7 +344,7 @@ impl FileReaderMethods for FileReader {
FileReaderResult::String(ref string) =>
StringOrObject::String(string.clone()),
FileReaderResult::ArrayBuffer(ref arr_buffer) => {
StringOrObject::Object((*arr_buffer.ptr.get()).to_object())
StringOrObject::Object(Heap::new((*arr_buffer.ptr.get()).to_object()))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/messageevent.rs
Expand Up @@ -64,7 +64,7 @@ impl MessageEvent {
-> Fallible<Root<MessageEvent>> {
// Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381
rooted!(in(global.get_cx()) let data = init.data);
rooted!(in(global.get_cx()) let data = init.data.get());
let ev = MessageEvent::new(global,
Atom::from(type_),
init.parent.bubbles,
Expand Down
3 changes: 1 addition & 2 deletions components/script/dom/popstateevent.rs
Expand Up @@ -53,7 +53,6 @@ impl PopStateEvent {
ev
}

#[allow(unsafe_code)]
pub fn Constructor(window: &Window,
type_: DOMString,
init: &PopStateEventBinding::PopStateEventInit)
Expand All @@ -62,7 +61,7 @@ impl PopStateEvent {
Atom::from(type_),
init.parent.bubbles,
init.parent.cancelable,
unsafe { HandleValue::from_marked_location(&init.state) }))
init.state.handle()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/request.rs
Expand Up @@ -139,12 +139,12 @@ impl Request {
// TODO: `environment settings object` is not implemented in Servo yet.

// Step 10
if !init.window.is_undefined() && !init.window.is_null() {
if !init.window.handle().is_null_or_undefined() {
return Err(Error::Type("Window is present and is not null".to_string()))
}

// Step 11
if !init.window.is_undefined() {
if !init.window.handle().is_undefined() {
window = Window::NoWindow;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ impl Request {
init.redirect.is_some() ||
init.referrer.is_some() ||
init.referrerPolicy.is_some() ||
!init.window.is_undefined() {
!init.window.handle().is_undefined() {
// Step 13.1
if request.mode == NetTraitsRequestMode::Navigate {
return Err(Error::Type(
Expand Down
8 changes: 4 additions & 4 deletions components/script/dom/testbinding.rs
Expand Up @@ -33,7 +33,7 @@ use dom::globalscope::GlobalScope;
use dom::promise::Promise;
use dom::promisenativehandler::{PromiseNativeHandler, Callback};
use dom::url::URL;
use js::jsapi::{HandleObject, HandleValue, JSContext, JSObject, JSAutoCompartment};
use js::jsapi::{HandleObject, HandleValue, Heap, JSContext, JSObject, JSAutoCompartment};
use js::jsapi::{JS_NewPlainObject, JS_NewUint8ClampedArray};
use js::jsval::{JSVal, NullValue};
use script_traits::MsDuration;
Expand Down Expand Up @@ -338,12 +338,12 @@ impl TestBindingMethods for TestBinding {
fn ReceiveNullableSequence(&self) -> Option<Vec<i32>> { Some(vec![1]) }
fn ReceiveTestDictionaryWithSuccessOnKeyword(&self) -> TestDictionary {
TestDictionary {
anyValue: NullValue(),
anyValue: Heap::new(NullValue()),
booleanValue: None,
byteValue: None,
dict: TestDictionaryDefaults {
UnrestrictedDoubleValue: 0.0,
anyValue: NullValue(),
anyValue: Heap::new(NullValue()),
booleanValue: false,
bytestringValue: ByteString::new(vec![]),
byteValue: 0,
Expand All @@ -359,7 +359,7 @@ impl TestBindingMethods for TestBinding {
nullableFloatValue: None,
nullableLongLongValue: None,
nullableLongValue: None,
nullableObjectValue: ptr::null_mut(),
nullableObjectValue: Heap::new(ptr::null_mut()),
nullableOctetValue: None,
nullableShortValue: None,
nullableStringValue: None,
Expand Down

0 comments on commit 8ce9ca6

Please sign in to comment.