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

Add trait DomObjectWrap to provide WRAP function #25624

Merged
merged 1 commit into from Mar 21, 2020
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
The table of contents is too big for display.

Always

Just for now

@@ -6,7 +6,7 @@ use crate::dom::audionode::AudioNode;
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AnalyserNodeBinding::{
self, AnalyserNodeMethods, AnalyserOptions,
AnalyserNodeMethods, AnalyserOptions,
};
use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
ChannelCountMode, ChannelInterpretation,
@@ -96,7 +96,7 @@ impl AnalyserNode {
options: &AnalyserOptions,
) -> Fallible<DomRoot<AnalyserNode>> {
let (node, recv) = AnalyserNode::new_inherited(window, context, options)?;
let object = reflect_dom_object(Box::new(node), window, AnalyserNodeBinding::Wrap);
let object = reflect_dom_object(Box::new(node), window);
let (source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
@@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods};
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::DOMString;
@@ -70,7 +70,6 @@ impl Attr {
document, local_name, value, name, namespace, prefix, owner,
)),
document,
AttrBinding::Wrap,
)
}

@@ -5,7 +5,7 @@
use crate::dom::audionode::MAX_CHANNEL_COUNT;
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::AudioBufferBinding::{
self, AudioBufferMethods, AudioBufferOptions,
AudioBufferMethods, AudioBufferOptions,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::num::Finite;
@@ -84,7 +84,7 @@ impl AudioBuffer {
initial_data: Option<&[Vec<f32>]>,
) -> DomRoot<AudioBuffer> {
let buffer = AudioBuffer::new_inherited(number_of_channels, length, sample_rate);
let buffer = reflect_dom_object(Box::new(buffer), global, AudioBufferBinding::Wrap);
let buffer = reflect_dom_object(Box::new(buffer), global);
buffer.set_initial_data(initial_data);
buffer
}
@@ -6,7 +6,6 @@ use crate::dom::audiobuffer::AudioBuffer;
use crate::dom::audioparam::AudioParam;
use crate::dom::audioscheduledsourcenode::AudioScheduledSourceNode;
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::codegen::Bindings::AudioBufferSourceNodeBinding;
use crate::dom::bindings::codegen::Bindings::AudioBufferSourceNodeBinding::AudioBufferSourceNodeMethods;
use crate::dom::bindings::codegen::Bindings::AudioBufferSourceNodeBinding::AudioBufferSourceOptions;
use crate::dom::bindings::codegen::Bindings::AudioParamBinding::AutomationRate;
@@ -100,11 +99,7 @@ impl AudioBufferSourceNode {
options: &AudioBufferSourceOptions,
) -> Fallible<DomRoot<AudioBufferSourceNode>> {
let node = AudioBufferSourceNode::new_inherited(window, context, options)?;
Ok(reflect_dom_object(
Box::new(node),
window,
AudioBufferSourceNodeBinding::Wrap,
))
Ok(reflect_dom_object(Box::new(node), window))
}

#[allow(non_snake_case)]
@@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::baseaudiocontext::{BaseAudioContext, BaseAudioContextOptions};
use crate::dom::bindings::codegen::Bindings::AudioContextBinding;
use crate::dom::bindings::codegen::Bindings::AudioContextBinding::{
AudioContextLatencyCategory, AudioContextMethods,
};
@@ -77,7 +76,7 @@ impl AudioContext {
pub fn new(window: &Window, options: &AudioContextOptions) -> DomRoot<AudioContext> {
let pipeline_id = window.pipeline_id();
let context = AudioContext::new_inherited(options, pipeline_id);
let context = reflect_dom_object(Box::new(context), window, AudioContextBinding::Wrap);
let context = reflect_dom_object(Box::new(context), window);
context.resume();
context
}
@@ -4,9 +4,7 @@

use crate::dom::audionode::{AudioNode, MAX_CHANNEL_COUNT};
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::codegen::Bindings::AudioDestinationNodeBinding::{
self, AudioDestinationNodeMethods,
};
use crate::dom::bindings::codegen::Bindings::AudioDestinationNodeBinding::AudioDestinationNodeMethods;
use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::AudioNodeOptions;
use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
ChannelCountMode, ChannelInterpretation,
@@ -46,7 +44,7 @@ impl AudioDestinationNode {
options: &AudioNodeOptions,
) -> DomRoot<AudioDestinationNode> {
let node = AudioDestinationNode::new_inherited(context, options);
reflect_dom_object(Box::new(node), global, AudioDestinationNodeBinding::Wrap)
reflect_dom_object(Box::new(node), global)
}
}

@@ -4,7 +4,7 @@

use crate::dom::audioparam::AudioParam;
use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::codegen::Bindings::AudioListenerBinding::{self, AudioListenerMethods};
use crate::dom::bindings::codegen::Bindings::AudioListenerBinding::AudioListenerMethods;
use crate::dom::bindings::codegen::Bindings::AudioParamBinding::AutomationRate;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::num::Finite;
@@ -143,7 +143,7 @@ impl AudioListener {
#[allow(unrooted_must_root)]
pub fn new(window: &Window, context: &BaseAudioContext) -> DomRoot<AudioListener> {
let node = AudioListener::new_inherited(window, context);
reflect_dom_object(Box::new(node), window, AudioListenerBinding::Wrap)
reflect_dom_object(Box::new(node), window)
}
}

@@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::baseaudiocontext::BaseAudioContext;
use crate::dom::bindings::codegen::Bindings::AudioParamBinding;
use crate::dom::bindings::codegen::Bindings::AudioParamBinding::{
AudioParamMethods, AutomationRate,
};
@@ -75,7 +74,7 @@ impl AudioParam {
min_value,
max_value,
);
reflect_dom_object(Box::new(audio_param), window, AudioParamBinding::Wrap)
reflect_dom_object(Box::new(audio_param), window)
}

fn message_node(&self, message: AudioNodeMessage) {
@@ -4,7 +4,7 @@

use crate::dom::audiotracklist::AudioTrackList;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AudioTrackBinding::{self, AudioTrackMethods};
use crate::dom::bindings::codegen::Bindings::AudioTrackBinding::AudioTrackMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
@@ -55,7 +55,6 @@ impl AudioTrack {
id, kind, label, language, track_list,
)),
window,
AudioTrackBinding::Wrap,
)
}

@@ -4,7 +4,7 @@

use crate::dom::audiotrack::AudioTrack;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AudioTrackListBinding::{self, AudioTrackListMethods};
use crate::dom::bindings::codegen::Bindings::AudioTrackListBinding::AudioTrackListMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
@@ -43,7 +43,6 @@ impl AudioTrackList {
reflect_dom_object(
Box::new(AudioTrackList::new_inherited(tracks, media_element)),
window,
AudioTrackListBinding::Wrap,
)
}

@@ -5,7 +5,6 @@
#![allow(dead_code)]

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BeforeUnloadEventBinding;
use crate::dom::bindings::codegen::Bindings::BeforeUnloadEventBinding::BeforeUnloadEventMethods;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::inheritance::Castable;
@@ -33,11 +32,7 @@ impl BeforeUnloadEvent {
}

pub fn new_uninitialized(window: &Window) -> DomRoot<BeforeUnloadEvent> {
reflect_dom_object(
Box::new(BeforeUnloadEvent::new_inherited()),
window,
BeforeUnloadEventBinding::Wrap,
)
reflect_dom_object(Box::new(BeforeUnloadEvent::new_inherited()), window)
}

pub fn new(
@@ -2858,6 +2858,50 @@ def define(self):
""" % {'check': check, 'name': name}


class CGDomObjectWrap(CGThing):
"""
Class for codegen of an implementation of the DomObjectWrap trait.
"""
def __init__(self, descriptor):
CGThing.__init__(self)
self.descriptor = descriptor

def define(self):
name = self.descriptor.concreteType
name = "dom::%s::%s" % (name.lower(), name)
return """\
impl DomObjectWrap for %s {
const WRAP: unsafe fn(
SafeJSContext,
&GlobalScope,
Box<Self>,
) -> Root<Dom<Self>> = Wrap;
}
""" % (name)


class CGDomObjectIteratorWrap(CGThing):
"""
Class for codegen of an implementation of the DomObjectIteratorWrap trait.
"""
def __init__(self, descriptor):
CGThing.__init__(self)
self.descriptor = descriptor

def define(self):
assert self.descriptor.interface.isIteratorInterface()
name = self.descriptor.interface.iterableInterface.identifier.name
return """\
impl DomObjectIteratorWrap for %s {
const ITER_WRAP: unsafe fn(
SafeJSContext,
&GlobalScope,
Box<IterableIterator<Self>>,
) -> Root<Dom<IterableIterator<Self>>> = Wrap;
}
""" % (name)


class CGAbstractExternMethod(CGAbstractMethod):
"""
Abstract base class for codegen of implementation-only (no
@@ -6067,6 +6111,8 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'crate::dom::bindings::namespace::create_namespace_object',
'crate::dom::bindings::reflector::MutDomObject',
'crate::dom::bindings::reflector::DomObject',
'crate::dom::bindings::reflector::DomObjectWrap',
'crate::dom::bindings::reflector::DomObjectIteratorWrap',
'crate::dom::bindings::root::Dom',
'crate::dom::bindings::root::DomRoot',
'crate::dom::bindings::root::DomSlice',
@@ -6286,6 +6332,10 @@ def reexportedName(name):
cgThings.append(CGWrapGlobalMethod(descriptor, properties))
else:
cgThings.append(CGWrapMethod(descriptor))
if descriptor.interface.isIteratorInterface():
cgThings.append(CGDomObjectIteratorWrap(descriptor))
else:
cgThings.append(CGDomObjectWrap(descriptor))
reexports.append('Wrap')

haveUnscopables = False
@@ -7445,9 +7495,7 @@ def __init__(self, descriptor, iterable, methodName):
return
CGGeneric.__init__(self, fill(
"""
let result = ${iterClass}::new(&*this,
IteratorType::${itrMethod},
super::${ifaceName}IteratorBinding::Wrap);
let result = ${iterClass}::new(&*this, IteratorType::${itrMethod});
""",
iterClass=iteratorNativeType(descriptor, True),
ifaceName=descriptor.interface.identifier.name,
@@ -9,8 +9,10 @@
use crate::dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyAndValueResult;
use crate::dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyOrValueResult;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::reflector::{
reflect_dom_object, DomObjectIteratorWrap, DomObjectWrap, Reflector,
};
use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::{JSTraceable, RootedTraceableBox};
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
@@ -51,27 +53,23 @@ pub trait Iterable {
/// An iterator over the iterable entries of a given DOM interface.
//FIXME: #12811 prevents dom_struct with type parameters
#[dom_struct]
pub struct IterableIterator<T: DomObject + JSTraceable + Iterable> {
pub struct IterableIterator<T: DomObjectIteratorWrap + JSTraceable + Iterable> {
reflector: Reflector,
iterable: Dom<T>,
type_: IteratorType,
index: Cell<u32>,
}

impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
impl<T: DomObjectIteratorWrap + JSTraceable + Iterable> IterableIterator<T> {
/// Create a new iterator instance for the provided iterable DOM interface.
pub fn new(
iterable: &T,
type_: IteratorType,
wrap: unsafe fn(JSContext, &GlobalScope, Box<IterableIterator<T>>) -> DomRoot<Self>,
) -> DomRoot<Self> {
pub fn new(iterable: &T, type_: IteratorType) -> DomRoot<Self> {
let iterator = Box::new(IterableIterator {
reflector: Reflector::new(),
type_: type_,
iterable: Dom::from_ref(iterable),
index: Cell::new(0),
});
reflect_dom_object(iterator, &*iterable.global(), wrap)
reflect_dom_object(iterator, &*iterable.global())
}

/// Return the next value from the iterable object.
@@ -119,6 +117,10 @@ impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
}
}

impl<T: DomObjectIteratorWrap + JSTraceable + Iterable> DomObjectWrap for IterableIterator<T> {
const WRAP: unsafe fn(JSContext, &GlobalScope, Box<Self>) -> Root<Dom<Self>> = T::ITER_WRAP;
}

fn dict_return(
cx: JSContext,
mut result: MutableHandleObject,
@@ -5,7 +5,8 @@
//! The `Reflector` struct.

use crate::dom::bindings::conversions::DerivedFrom;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::iterable::{Iterable, IterableIterator};
use crate::dom::bindings::root::{Dom, DomRoot, Root};
use crate::dom::bindings::trace::JSTraceable;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
@@ -15,17 +16,13 @@ use std::default::Default;

/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_dom_object<T, U>(
obj: Box<T>,
global: &U,
wrap_fn: unsafe fn(JSContext, &GlobalScope, Box<T>) -> DomRoot<T>,
) -> DomRoot<T>
pub fn reflect_dom_object<T, U>(obj: Box<T>, global: &U) -> DomRoot<T>
where
T: DomObject,
T: DomObject + DomObjectWrap,
U: DerivedFrom<GlobalScope>,
{
let global_scope = global.upcast();
unsafe { wrap_fn(global_scope.get_cx(), global_scope, obj) }
unsafe { T::WRAP(global_scope.get_cx(), global_scope, obj) }
}

/// A struct to store a reference to the reflector of a DOM object.
@@ -106,3 +103,20 @@ impl MutDomObject for Reflector {
self.set_jsobject(obj)
}
}

/// A trait to provide a function pointer to wrap function for DOM objects.
pub trait DomObjectWrap: Sized + DomObject {
/// Function pointer to the general wrap function type
const WRAP: unsafe fn(JSContext, &GlobalScope, Box<Self>) -> Root<Dom<Self>>;
}

/// A trait to provide a function pointer to wrap function for
/// DOM iterator interfaces.
pub trait DomObjectIteratorWrap: DomObjectWrap + JSTraceable + Iterable {
/// Function pointer to the wrap function for IterableIterator<T>
const ITER_WRAP: unsafe fn(
JSContext,
&GlobalScope,
Box<IterableIterator<Self>>,
) -> Root<Dom<IterableIterator<Self>>>;
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.