From 8ce9ca624367c8e5737b8673548b230b69f4558b Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 14 Feb 2017 14:32:49 +0100 Subject: [PATCH] Use Heap for dictionary and union members. --- .../dom/bindings/codegen/CodegenRust.py | 29 ++++++++++++------- components/script/dom/bindings/iterable.rs | 6 ++-- components/script/dom/customevent.rs | 2 +- components/script/dom/errorevent.rs | 2 +- .../script/dom/extendablemessageevent.rs | 2 +- components/script/dom/filereader.rs | 2 +- components/script/dom/messageevent.rs | 2 +- components/script/dom/popstateevent.rs | 3 +- components/script/dom/request.rs | 6 ++-- components/script/dom/testbinding.rs | 8 ++--- 10 files changed, 35 insertions(+), 27 deletions(-) diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 37cab821d929..c47cf185bf25 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -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") 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") @@ -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 @@ -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', @@ -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) @@ -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: diff --git a/components/script/dom/bindings/iterable.rs b/components/script/dom/bindings/iterable.rs index 08439d165175..f10bd320b855 100644 --- a/components/script/dom/bindings/iterable.rs +++ b/components/script/dom/bindings/iterable.rs @@ -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; @@ -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()); @@ -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()); diff --git a/components/script/dom/customevent.rs b/components/script/dom/customevent.rs index f4346280cb7a..942253c30dca 100644 --- a/components/script/dom/customevent.rs +++ b/components/script/dom/customevent.rs @@ -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, diff --git a/components/script/dom/errorevent.rs b/components/script/dom/errorevent.rs index 3739d5732f36..a89a7b7e5274 100644 --- a/components/script/dom/errorevent.rs +++ b/components/script/dom/errorevent.rs @@ -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_), diff --git a/components/script/dom/extendablemessageevent.rs b/components/script/dom/extendablemessageevent.rs index a7ee5f797afe..b070007f8318 100644 --- a/components/script/dom/extendablemessageevent.rs +++ b/components/script/dom/extendablemessageevent.rs @@ -50,7 +50,7 @@ impl ExtendableMessageEvent { init: &ExtendableMessageEventBinding::ExtendableMessageEventInit) -> Fallible> { let global = worker.upcast::(); - 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, diff --git a/components/script/dom/filereader.rs b/components/script/dom/filereader.rs index f97f14480c3a..0c3030106565 100644 --- a/components/script/dom/filereader.rs +++ b/components/script/dom/filereader.rs @@ -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())) } }) } diff --git a/components/script/dom/messageevent.rs b/components/script/dom/messageevent.rs index 5b36b14f6cec..b066cf810fe2 100644 --- a/components/script/dom/messageevent.rs +++ b/components/script/dom/messageevent.rs @@ -64,7 +64,7 @@ impl MessageEvent { -> Fallible> { // 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, diff --git a/components/script/dom/popstateevent.rs b/components/script/dom/popstateevent.rs index fde05e30394f..7db2eea8fe66 100644 --- a/components/script/dom/popstateevent.rs +++ b/components/script/dom/popstateevent.rs @@ -53,7 +53,6 @@ impl PopStateEvent { ev } - #[allow(unsafe_code)] pub fn Constructor(window: &Window, type_: DOMString, init: &PopStateEventBinding::PopStateEventInit) @@ -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())) } } diff --git a/components/script/dom/request.rs b/components/script/dom/request.rs index 2b26d5e1e8c6..bf4914f82550 100644 --- a/components/script/dom/request.rs +++ b/components/script/dom/request.rs @@ -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; } @@ -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( diff --git a/components/script/dom/testbinding.rs b/components/script/dom/testbinding.rs index 4726b82d900e..677d5c611db9 100644 --- a/components/script/dom/testbinding.rs +++ b/components/script/dom/testbinding.rs @@ -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; @@ -338,12 +338,12 @@ impl TestBindingMethods for TestBinding { fn ReceiveNullableSequence(&self) -> Option> { 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, @@ -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,