fune/servo/components/script/dom/errorevent.rs
Eduard Burtescu da092febcb servo: Merge #11872 - Replace return_address usage for rooting with stack guards and convenience macros (from eddyb:back-to-roots); r=Ms2ger
The existing `Rooted` and `RootedVec` users were migrated the the following two macros:
```rust
let x = Rooted::new(cx, value);
// Was changed to:
rooted!(in(cx) let x = value);
// Which expands to:
let mut __root = Rooted::new_unrooted(value);
let x = RootedGuard::new(cx, &mut __root);
```
```rust
let mut v = RootedVec::new();
v.extend(iterator);
// Was changed to:
rooted_vec!(let v <- iterator);
// Which expands to:
let mut __root = RootableVec::new();
let v = RootedVec::new(&mut __root, iterator);
```

The `rooted!` macro depends on servo/rust-mozjs#272.
These APIs based on two types, a container to be rooted and a rooting guard, allow implementing both `Rooted`-style rooting and `Traceable`-based rooting in stable Rust, without abusing `return_address`.

Such macros may have been tried before, but in 1.9 their hygiene is broken, they work only since 1.10.

Sadly, `Rooted` is a FFI type and completely exposed, so I cannot prevent anyone from creating their own, although all fields but the value get overwritten by `RootedGuard::new` anyway.
`RootableVec` OTOH is *guaranteed* to be empty when not rooted, which makes it harmless AFAICT.

By fixing rust-lang/rust#34227, this PR enables Servo to build with `-Zorbit`.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix rust-lang/rust#34227
- [x] These changes do not require tests because they are not functional changes

Source-Repo: https://github.com/servo/servo
Source-Revision: 80cb0cf8214fd52d2884724614c40cb278ee7575
2016-07-04 11:03:35 -07:00

137 lines
4.4 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::ErrorEventBinding;
use dom::bindings::codegen::Bindings::ErrorEventBinding::ErrorEventMethods;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{MutHeapJSVal, Root};
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::event::{Event, EventBubbles, EventCancelable};
use js::jsapi::{HandleValue, JSContext};
use js::jsval::JSVal;
use std::cell::Cell;
use string_cache::Atom;
#[dom_struct]
pub struct ErrorEvent {
event: Event,
message: DOMRefCell<DOMString>,
filename: DOMRefCell<DOMString>,
lineno: Cell<u32>,
colno: Cell<u32>,
#[ignore_heap_size_of = "Defined in rust-mozjs"]
error: MutHeapJSVal,
}
impl ErrorEvent {
fn new_inherited() -> ErrorEvent {
ErrorEvent {
event: Event::new_inherited(),
message: DOMRefCell::new(DOMString::new()),
filename: DOMRefCell::new(DOMString::new()),
lineno: Cell::new(0),
colno: Cell::new(0),
error: MutHeapJSVal::new()
}
}
pub fn new_uninitialized(global: GlobalRef) -> Root<ErrorEvent> {
reflect_dom_object(box ErrorEvent::new_inherited(),
global,
ErrorEventBinding::Wrap)
}
pub fn new(global: GlobalRef,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
message: DOMString,
filename: DOMString,
lineno: u32,
colno: u32,
error: HandleValue) -> Root<ErrorEvent> {
let ev = ErrorEvent::new_uninitialized(global);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bool::from(bubbles),
bool::from(cancelable));
*ev.message.borrow_mut() = message;
*ev.filename.borrow_mut() = filename;
ev.lineno.set(lineno);
ev.colno.set(colno);
}
ev.error.set(error.get());
ev
}
pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &ErrorEventBinding::ErrorEventInit) -> Fallible<Root<ErrorEvent>>{
let msg = match init.message.as_ref() {
Some(message) => message.clone(),
None => DOMString::new(),
};
let file_name = match init.filename.as_ref() {
Some(filename) => filename.clone(),
None => DOMString::new(),
};
let line_num = init.lineno.unwrap_or(0);
let col_num = init.colno.unwrap_or(0);
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
// Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381
rooted!(in(global.get_cx()) let error = init.error);
let event = ErrorEvent::new(global, Atom::from(type_),
bubbles, cancelable,
msg, file_name,
line_num, col_num,
error.handle());
Ok(event)
}
}
impl ErrorEventMethods for ErrorEvent {
// https://html.spec.whatwg.org/multipage/#dom-errorevent-lineno
fn Lineno(&self) -> u32 {
self.lineno.get()
}
// https://html.spec.whatwg.org/multipage/#dom-errorevent-colno
fn Colno(&self) -> u32 {
self.colno.get()
}
// https://html.spec.whatwg.org/multipage/#dom-errorevent-message
fn Message(&self) -> DOMString {
self.message.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-errorevent-filename
fn Filename(&self) -> DOMString {
self.filename.borrow().clone()
}
// https://html.spec.whatwg.org/multipage/#dom-errorevent-error
fn Error(&self, _cx: *mut JSContext) -> JSVal {
self.error.get()
}
// https://dom.spec.whatwg.org/#dom-event-istrusted
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}