Autogenerate ClientRectList bindings.

This commit is contained in:
Josh Matthews 2013-02-24 16:51:34 -05:00
parent 3f527d8f8f
commit 77388999ef
14 changed files with 1690 additions and 78 deletions

View file

@ -21,6 +21,7 @@ use core::task::{SingleThreaded, spawn, task};
use core::io::{println, read_whole_file};
use core::ptr::null;
use core::util::replace;
use core::hashmap::linear;
use geom::size::Size2D;
use gfx::resource::image_cache_task::ImageCacheTask;
use gfx::resource::resource_task::ResourceTask;
@ -92,6 +93,7 @@ pub struct Content {
jsrt: jsrt,
cx: @Cx,
mut proxy_handlers: linear::LinearMap<uint, *libc::c_void>,
document: Option<@Document>,
window: Option<@Window>,
@ -136,6 +138,7 @@ pub fn Content(layout_task: LayoutTask,
jsrt : jsrt,
cx : cx,
proxy_handlers: linear::LinearMap::new(),
document : None,
window : None,

View file

@ -1,3 +1,7 @@
use dom::bindings::utils::{CacheableWrapper, WrapperCache};
use dom::bindings::ClientRectBinding;
use js::jsapi::{JSObject, JSContext};
pub trait ClientRect {
fn Top() -> f32;
fn Bottom() -> f32;
@ -7,14 +11,15 @@ pub trait ClientRect {
fn Height() -> f32;
}
struct ClientRectImpl {
pub struct ClientRectImpl {
mut wrapper: ~WrapperCache,
top: f32,
bottom: f32,
left: f32,
right: f32,
}
impl ClientRectImpl: ClientRect {
pub impl ClientRect for ClientRectImpl {
fn Top() -> f32 {
self.top
}
@ -38,4 +43,19 @@ impl ClientRectImpl: ClientRect {
fn Height() -> f32 {
f32::abs(self.bottom - self.top)
}
}
pub impl CacheableWrapper for ClientRectImpl {
fn get_wrapper(@self) -> *JSObject {
unsafe { cast::transmute(self.wrapper.wrapper) }
}
fn set_wrapper(@self, wrapper: *JSObject) {
unsafe { self.wrapper.wrapper = cast::transmute(wrapper); }
}
fn wrap_object(@self, cx: *JSContext, scope: *JSObject) -> *JSObject {
let mut unused = false;
ClientRectBinding::Wrap(cx, scope, self, &mut unused)
}
}

View file

@ -0,0 +1,46 @@
use content::content_task::task_from_context;
use dom::bindings::clientrect::ClientRectImpl;
use dom::bindings::ClientRectListBinding;
use dom::bindings::utils::{WrapperCache, CacheableWrapper, BindingObject};
use js::jsapi::{JSObject, JSContext};
pub trait ClientRectList {
fn Length(&self) -> u32;
fn Item(&self, index: u32) -> Option<@ClientRectImpl>;
}
pub struct ClientRectListImpl {
mut wrapper: ~WrapperCache
}
impl ClientRectList for ClientRectListImpl {
fn Length(&self) -> u32 {
0
}
fn Item(&self, index: u32) -> Option<@ClientRectImpl> {
None
}
}
pub impl CacheableWrapper for ClientRectListImpl {
fn get_wrapper(@self) -> *JSObject {
unsafe { cast::transmute(self.wrapper.wrapper) }
}
fn set_wrapper(@self, wrapper: *JSObject) {
unsafe { self.wrapper.wrapper = cast::transmute(wrapper); }
}
fn wrap_object(@self, cx: *JSContext, scope: *JSObject) -> *JSObject {
let mut unused = false;
ClientRectListBinding::Wrap(cx, scope, self, &mut unused)
}
}
pub impl BindingObject for ClientRectListImpl {
fn GetParentObject(@self, cx: *JSContext) -> @CacheableWrapper {
let content = task_from_context(cx);
unsafe { (*content).window.get() as @CacheableWrapper }
}
}

View file

@ -120,10 +120,10 @@ DOMInterfaces = {
'ClientRectList': [
{
'nativeType': 'nsClientRectList',
'headerFile': 'nsClientRect.h',
'prefable': True,
'resultNotAddRefed': [ 'item' ]
'nativeType': 'ClientRectListImpl',
#'headerFile': 'nsClientRect.h',
#'prefable': True,
#'resultNotAddRefed': [ 'item' ]
}],
'CSS2Properties': {

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
#include "ClientRectBinding.h"
#include "ClientRectListBinding.h"
#include "nsScriptNameSpaceManager.h"
namespace mozilla {
@ -11,6 +12,7 @@ Register(nsScriptNameSpaceManager* aNameSpaceManager)
aNameSpaceManager->RegisterDefineDOMInterface(NS_LITERAL_STRING(#_dom_class), _dom_class##Binding::DefineDOMInterface, _pref_check);
REGISTER_PROTO(ClientRect, nullptr);
REGISTER_PROTO(ClientRectList, nullptr);
#undef REGISTER_PROTO
}

View file

@ -0,0 +1,17 @@
use js::jsapi::JSVal;
use js::glue::bindgen::{RUST_INT_TO_JSVAL, RUST_JSVAL_TO_INT};
pub trait JSValConvertible<T> {
fn to_jsval(&self) -> JSVal;
static fn from_jsval(val: JSVal) -> Option<T>;
}
pub impl JSValConvertible<u32> for u32 {
fn to_jsval(&self) -> JSVal {
RUST_INT_TO_JSVAL(*self as i32)
}
static fn from_jsval(val: JSVal) -> Option<u32> {
Some(RUST_JSVAL_TO_INT(val) as u32)
}
}

View file

@ -1,6 +1,7 @@
use content::content_task::{Content, task_from_context};
use dom::bindings::node::unwrap;
use dom::bindings::utils::{rust_box, squirrel_away_unique, get_compartment, domstring_to_jsval};
use dom::bindings::utils::{rust_box, squirrel_away_unique, get_compartment};
use dom::bindings::utils::{domstring_to_jsval, WrapNewBindingObject};
use dom::bindings::utils::{str};
use dom::element::*;
use dom::node::{AbstractNode, Node, Element, ElementNodeTypeId};
@ -46,6 +47,15 @@ pub fn init(compartment: @mut Compartment) {
JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs);
});
let methods = @~[JSFunctionSpec {name: compartment.add_name(~"getClientRects"),
call: JSNativeWrapper {op: getClientRects, info: null()},
nargs: 0,
flags: 0,
selfHostedName: null()}];
vec::as_imm_buf(*methods, |fns, _len| {
JS_DefineFunctions(compartment.cx.ptr, obj.ptr, fns);
});
compartment.register_class(utils::instance_jsclass(~"GenericElementInstance",
finalize));
@ -72,6 +82,25 @@ pub fn init(compartment: @mut Compartment) {
});
}
/*trait Element: utils::CacheableWrapper {
fn getClientRects() -> Option<@ClientRectListImpl>;
}*/
/*extern fn getClientRects(cx: *JSContext, argc: c_uint, vp: *JSVal) -> JSBool {
unsafe {
let self: @Element =
cast::reinterpret_cast(&utils::unwrap::<ElementData>(JS_THIS_OBJECT(cx, vp)));
let rval = self.getClientRects();
if rval.is_none() {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
} else {
assert WrapNewBindingObject(cx, (self as utils::CacheableWrapper).get_wrapper(), rval.get(), cast::transmute(vp));
}
cast::forget(self);
return 1;
}
}*/
#[allow(non_implicitly_copyable_typarams)]
extern fn HTMLImageElement_getWidth(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
unsafe {

View file

@ -11,7 +11,8 @@ use js::jsapi::bindgen::{JS_ValueToString, JS_GetStringCharsZAndLength, JS_Repor
JS_GetClass, JS_GetPrototype, JS_LinkConstructorAndPrototype,
JS_AlreadyHasOwnProperty, JS_NewObject, JS_NewFunction,
JS_GetFunctionPrototype, JS_InternString, JS_GetFunctionObject,
JS_GetInternedStringCharsAndLength, JS_DefineProperties};
JS_GetInternedStringCharsAndLength, JS_DefineProperties,
JS_WrapValue};
use js::jsfriendapi::bindgen::{DefineFunctionWithReserved, GetObjectJSClass,
JS_NewObjectWithUniqueType};
use js::glue::{PROPERTY_STUB, STRICT_PROPERTY_STUB, ENUMERATE_STUB, CONVERT_STUB,
@ -39,11 +40,11 @@ extern fn InterfaceObjectToString(cx: *JSContext, argc: uint, vp: *mut JSVal) ->
let clasp: *JSClass = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(*v));
let v = GetFunctionNativeReserved(callee, TOSTRING_NAME_RESERVED_SLOT);
let jsname: *JSString = RUST_JSVAL_TO_STRING(*v);
let length = 0;
let name = JS_GetInternedStringCharsAndLength(jsname, &length);
if GetObjectJSClass(obj) != clasp {
/*let jsname: *JSString = RUST_JSVAL_TO_STRING(*v);
let length = 0;
let name = JS_GetInternedStringCharsAndLength(jsname, &length);*/
//XXXjdm figure out JSMSG madness
/*JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_INCOMPATIBLE_PROTO,
NS_ConvertUTF16toUTF8(name).get(), "toString",
@ -320,6 +321,7 @@ mod prototypes {
mod id {
pub enum Prototype {
ClientRect,
ClientRectList,
_ID_Count
}
}
@ -507,12 +509,60 @@ pub extern fn ThrowingConstructor(cx: *JSContext, argc: uint, vp: *JSVal) -> JSB
}
pub fn initialize_global(global: *JSObject) {
let protoArray = @[0 as *JSObject, ..1]; //XXXjdm number of constructors
let protoArray = @[0 as *JSObject, ..2]; //XXXjdm number of constructors
unsafe {
let box = squirrel_away(protoArray);
let inner = ptr::to_unsafe_ptr(&(*box).payload);
JS_SetReservedSlot(global,
DOM_PROTOTYPE_SLOT,
JS_SetReservedSlot(global, DOM_PROTOTYPE_SLOT,
RUST_PRIVATE_TO_JSVAL(inner as *libc::c_void));
}
}
pub trait CacheableWrapper {
fn get_wrapper(@self) -> *JSObject;
fn set_wrapper(@self, wrapper: *JSObject);
fn wrap_object(@self, cx: *JSContext, scope: *JSObject) -> *JSObject;
}
pub struct WrapperCache {
wrapper: *mut JSObject
}
pub fn WrapNewBindingObject<T: CacheableWrapper>(cx: *JSContext, scope: *JSObject,
value: @T, vp: *mut JSVal) -> bool {
unsafe {
let obj = value.get_wrapper();
if obj.is_not_null() /*&& js::GetObjectCompartment(obj) == js::GetObjectCompartment(scope)*/ {
*vp = RUST_OBJECT_TO_JSVAL(obj);
return true;
}
let obj = if obj.is_not_null() {
obj
} else {
value.wrap_object(cx, scope)
};
if obj.is_null() {
return false;
}
// MOZ_ASSERT(js::IsObjectInContextCompartment(scope, cx));
*vp = RUST_OBJECT_TO_JSVAL(obj);
return JS_WrapValue(cx, cast::transmute(vp)) != 0;
}
}
pub fn WrapNativeParent(cx: *JSContext, scope: *JSObject, p: @CacheableWrapper)
-> *JSObject {
let obj = p.get_wrapper();
if obj.is_not_null() {
return obj;
}
return ptr::null();
}
pub trait BindingObject {
fn GetParentObject(@self, cx: *JSContext) -> @CacheableWrapper;
}

View file

@ -1,7 +1,7 @@
// DOM bindings for the Window object.
use dom::bindings::node::create;
use dom::bindings::utils::{rust_box, squirrel_away, jsval_to_str};
use dom::bindings::utils::{rust_box, squirrel_away, jsval_to_str, CacheableWrapper};
use dom::node::Node;
use dom::window::{Window, TimerMessage_Fire};
use super::utils;
@ -127,4 +127,20 @@ pub fn init(compartment: @mut Compartment, win: @Window) {
compartment.define_property(~"window", RUST_OBJECT_TO_JSVAL(obj.ptr),
JS_PropertyStub, JS_StrictPropertyStub,
JSPROP_ENUMERATE);
win.set_wrapper(obj.ptr);
}
pub impl CacheableWrapper for Window {
fn get_wrapper(@self) -> *JSObject {
self.wrapper
}
fn set_wrapper(@self, wrapper: *JSObject) {
self.wrapper = wrapper;
}
fn wrap_object(@self, cx: *JSContext, scope: *JSObject) -> *JSObject {
fail!(~"should this be called?");
}
}

View file

@ -375,8 +375,11 @@ pub fn define_bindings(compartment: @mut Compartment, doc: @Document, win: @Wind
bindings::node::init(compartment);
bindings::element::init(compartment);
bindings::utils::initialize_global(compartment.global_obj.ptr);
let unused = false;
let mut unused = false;
assert bindings::ClientRectBinding::DefineDOMInterface(compartment.cx.ptr,
compartment.global_obj.ptr,
ptr::mut_addr_of(&unused));
&mut unused);
assert bindings::ClientRectListBinding::DefineDOMInterface(compartment.cx.ptr,
compartment.global_obj.ptr,
&mut unused);
}

View file

@ -1,5 +1,5 @@
use content::content_task::{ControlMsg, Timer, ExitMsg};
use js::jsapi::JSVal;
use js::jsapi::{JSVal, JSObject};
use util::task::spawn_listener;
use core::comm::{Port, Chan};
@ -14,6 +14,7 @@ pub enum TimerControlMsg {
pub struct Window {
timer_chan: Chan<TimerControlMsg>,
mut wrapper: *JSObject
}
impl Drop for Window {
@ -74,6 +75,7 @@ pub impl Window {
pub fn Window(content_chan: comm::SharedChan<ControlMsg>) -> Window {
Window {
wrapper: ptr::null(),
timer_chan: do spawn_listener |timer_port: Port<TimerControlMsg>| {
loop {
match timer_port.recv() {

View file

@ -44,9 +44,12 @@ pub mod dom {
pub mod element;
pub mod node;
pub mod utils;
pub mod conversions;
pub mod window;
pub mod clientrect;
pub mod clientrectlist;
pub mod ClientRectBinding;
pub mod ClientRectListBinding;
}
pub mod document;
pub mod element;

View file

@ -1 +1,3 @@
window.alert(ClientRect);
window.alert(ClientRectList);