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

Event listeners and dispatch #1065

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,105 @@
/* 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::utils::{WrapNativeParent, Reflectable};
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JSVal, JS_ObjectIsCallable};
use js::jsapi::JS_GetProperty;
use js::{JSVAL_IS_OBJECT, JSVAL_TO_OBJECT};

use std::libc;
use std::ptr;

pub enum ExceptionHandling {
// Report any exception and don't throw it to the caller code.
eReportExceptions,
// Throw an exception to the caller code if the thrown exception is a
// binding object for a DOMError from the caller's scope, otherwise report
// it.
eRethrowContentExceptions,
// Throw any exception to the caller code.
eRethrowExceptions
}

#[deriving(Clone,Eq)]
pub struct CallbackInterface {
callback: *JSObject
}

pub trait CallbackContainer {
fn callback(&self) -> *JSObject;
}

impl CallbackContainer for CallbackInterface {
fn callback(&self) -> *JSObject {
self.callback
}
}

impl CallbackInterface {
pub fn new(callback: *JSObject) -> CallbackInterface {
CallbackInterface {
callback: callback
}
}

#[fixed_stack_segment]
pub fn GetCallableProperty(&self, cx: *JSContext, name: *libc::c_char, callable: &mut JSVal) -> bool {
unsafe {
if JS_GetProperty(cx, self.callback, name, &*callable) == 0 {
return false;
}

if !JSVAL_IS_OBJECT(*callable) ||
JS_ObjectIsCallable(cx, JSVAL_TO_OBJECT(*callable)) == 0 {
//ThrowErrorMessage(cx, MSG_NOT_CALLABLE, description.get());
return false;
}

return true;
}
}
}

pub fn GetJSObjectFromCallback<T: CallbackContainer>(callback: &T) -> *JSObject {
callback.callback()
}

#[fixed_stack_segment]
pub fn WrapCallThisObject<T: 'static + CallbackContainer + Reflectable>(cx: *JSContext,
scope: *JSObject,
p: @mut T) -> *JSObject {
let mut obj = GetJSObjectFromCallback(p);
if obj.is_null() {
obj = WrapNativeParent(cx, scope, Some(p as @mut Reflectable));
if obj.is_null() {
return ptr::null();
}
}

unsafe {
if JS_WrapObject(cx, &obj) == 0 {
return ptr::null();
}
}

return obj;
}

pub struct CallSetup {
cx: *JSContext,
handling: ExceptionHandling
}

impl CallSetup {
pub fn new(cx: *JSContext, handling: ExceptionHandling) -> CallSetup {
CallSetup {
cx: cx,
handling: handling
}
}

pub fn GetContext(&self) -> *JSContext {
self.cx
}
}
@@ -182,28 +182,21 @@ DOMInterfaces = {
},

'Event': {
'nativeType': 'AbstractEvent',
'concreteType': 'Event',
'pointerType': '',
},

'EventListener': [
{
'EventListener': {
'nativeType': 'EventListenerBinding::EventListener',
},
{
'workers': True,
}],

'EventTarget': [
{
# 'nativeType': 'nsDOMEventTargetHelper',
# 'hasInstanceInterface': 'nsIDOMEventTarget',
# 'concrete': False,
# 'prefable': True,
'EventTarget': {
'nativeType': 'AbstractEventTarget',
'concreteType': 'EventTarget',
'pointerType': '',
'needsAbstract': ['dispatchEvent']
},
#{
# 'workers': True,
# 'headerFile': 'mozilla/dom/workers/bindings/EventTarget.h',
# 'concrete': False
#}
],

'FileList': [
{
@@ -291,6 +284,9 @@ DOMInterfaces = {
}],

'MouseEvent': {
'nativeType': 'AbstractEvent',
'concreteType': 'MouseEvent',
'pointerType': '',
},

'Navigator': {
@@ -388,6 +384,9 @@ DOMInterfaces = {
}],

'UIEvent': {
'nativeType': 'AbstractEvent',
'concreteType': 'UIEvent',
'pointerType': '',
},

'ValidityState': {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.