Reenable DOM bindings.

This commit is contained in:
Josh Matthews 2013-02-28 19:37:18 -05:00
parent 416e870385
commit 37488ce044
3 changed files with 105 additions and 164 deletions

View file

@ -64,19 +64,15 @@ enum Element = int;
}*/ }*/
extern fn getDocumentElement(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn getDocumentElement(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let box = unwrap(obj); let doc = &(*unwrap(obj)).payload;
let node = (*box).payload.root; *vp = RUST_OBJECT_TO_JSVAL(node::create(cx, doc.root).ptr);
let scope = (*box).payload.scope; }
*vp = RUST_OBJECT_TO_JSVAL(node::create(cx, node, scope).ptr);
return 1;
}*/
return 1; return 1;
} }
@ -87,17 +83,14 @@ unsafe fn unwrap(obj: *JSObject) -> *rust_box<Document> {
} }
extern fn finalize(_fop: *JSFreeOp, obj: *JSObject) { extern fn finalize(_fop: *JSFreeOp, obj: *JSObject) {
/*
debug!("document finalize!"); debug!("document finalize!");
unsafe { unsafe {
let val = JS_GetReservedSlot(obj, 0); let val = JS_GetReservedSlot(obj, 0);
let _doc: @Document = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(val)); let _doc: @Document = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(val));
} }
*/
} }
pub fn init(compartment: @mut Compartment, doc: @Document) { pub fn init(compartment: @mut Compartment, doc: @Document) {
/*
let obj = utils::define_empty_prototype(~"Document", None, compartment); let obj = utils::define_empty_prototype(~"Document", None, compartment);
let attrs = @~[ let attrs = @~[
@ -133,5 +126,4 @@ pub fn init(compartment: @mut Compartment, doc: @Document) {
GetJSClassHookStubPointer(PROPERTY_STUB) as *u8, GetJSClassHookStubPointer(PROPERTY_STUB) as *u8,
GetJSClassHookStubPointer(STRICT_PROPERTY_STUB) as *u8, GetJSClassHookStubPointer(STRICT_PROPERTY_STUB) as *u8,
JSPROP_ENUMERATE); JSPROP_ENUMERATE);
*/
} }

View file

@ -1,9 +1,9 @@
use content::content_task::{Content, task_from_context}; use content::content_task::{Content, task_from_context};
use dom::bindings::node::{NodeBundle, unwrap}; 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, domstring_to_jsval};
use dom::bindings::utils::{str}; use dom::bindings::utils::{str};
use dom::element::*; use dom::element::*;
use dom::node::{Node, Element}; use dom::node::{AbstractNode, Node, Element, ElementNodeTypeId};
use layout::layout_task; use layout::layout_task;
use super::utils; use super::utils;
@ -19,17 +19,14 @@ use js::{JS_ARGV, JSCLASS_HAS_RESERVED_SLOTS, JSPROP_ENUMERATE, JSPROP_SHARED, J
use js::{JS_THIS_OBJECT, JS_SET_RVAL, JSPROP_NATIVE_ACCESSORS}; use js::{JS_THIS_OBJECT, JS_SET_RVAL, JSPROP_NATIVE_ACCESSORS};
extern fn finalize(_fop: *JSFreeOp, obj: *JSObject) { extern fn finalize(_fop: *JSFreeOp, obj: *JSObject) {
/*
debug!("element finalize!"); debug!("element finalize!");
unsafe { unsafe {
let val = JS_GetReservedSlot(obj, 0); let val = JS_GetReservedSlot(obj, 0);
let _node: ~NodeBundle = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(val)); let _node: ~AbstractNode = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(val));
} }
*/
} }
pub fn init(compartment: @mut Compartment) { pub fn init(compartment: @mut Compartment) {
/*
let obj = utils::define_empty_prototype(~"Element", Some(~"Node"), compartment); let obj = utils::define_empty_prototype(~"Element", Some(~"Node"), compartment);
let attrs = @~[ let attrs = @~[
JSPropertySpec { JSPropertySpec {
@ -73,118 +70,87 @@ pub fn init(compartment: @mut Compartment) {
vec::as_imm_buf(*attrs, |specs, _len| { vec::as_imm_buf(*attrs, |specs, _len| {
JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs); JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs);
}); });
*/
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
extern fn HTMLImageElement_getWidth(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn HTMLImageElement_getWidth(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::transmute(vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
let node = (*bundle).payload.node; let width = match node.type_id() {
let scope = (*bundle).payload.scope; ElementNodeTypeId(HTMLImageElementTypeId) => {
let width = scope.write(&node, |nd| { let content = task_from_context(cx);
match &nd.kind { let node = Node::as_abstract_node(~*node);
&~Element(ref ed) => { match (*content).query_layout(layout_task::ContentBox(node)) {
match &ed.kind { Ok(rect) => rect.width,
&~HTMLImageElement(*) => { Err(()) => 0
let content = task_from_context(cx);
match (*content).query_layout(layout_task::ContentBox(node)) {
Ok(rect) => rect.width,
Err(()) => 0,
}
// TODO: if nothing is being rendered(?), return zero dimensions
}
_ => fail!(~"why is this not an image element?")
}
} }
_ => fail!(~"why is this not an element?") // TODO: if nothing is being rendered(?), return zero dimensions
} }
}); ElementNodeTypeId(_) => fail!(~"why is this not an image element?"),
_ => fail!(~"why is this not an element?")
};
*vp = RUST_INT_TO_JSVAL( *vp = RUST_INT_TO_JSVAL(
(width & (i32::max_value as int)) as libc::c_int); (width & (i32::max_value as int)) as libc::c_int);
return 1; return 1;
}*/ }
return 0;
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
extern fn HTMLImageElement_setWidth(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn HTMLImageElement_setWidth(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
do (*bundle).payload.scope.write(&(*bundle).payload.node) |nd| { match node.type_id() {
match nd.kind { ElementNodeTypeId(HTMLImageElementTypeId) => {
~Element(ref ed) => { do node.as_mut_element |elem| {
match ed.kind { let arg = ptr::offset(JS_ARGV(cx, cast::reinterpret_cast(&vp)), 0);
~HTMLImageElement(*) => { elem.set_attr(~"width", int::str(RUST_JSVAL_TO_INT(*arg) as int))
let arg = ptr::offset(JS_ARGV(cx, cast::reinterpret_cast(&vp)), 0);
ed.set_attr(~"width", int::str(RUST_JSVAL_TO_INT(*arg) as int))
}
_ => fail!(~"why is this not an image element?")
}
} }
_ => fail!(~"why is this not an element?")
} }
ElementNodeTypeId(_) => fail!(~"why is this not an image element?"),
_ => fail!(~"why is this not an element?")
}; };
return 1; return 1;
}*/ }
return 0;
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
extern fn getTagName(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn getTagName(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
do (*bundle).payload.scope.write(&(*bundle).payload.node) |nd| { do node.with_imm_element |elem| {
match nd.kind { let s = str(copy elem.tag_name);
~Element(ref ed) => { *vp = domstring_to_jsval(cx, &s);
let s = str(copy ed.tag_name); }
*vp = domstring_to_jsval(cx, &s); }
}
_ => {
//XXXjdm should probably read the spec to figure out what to do here
*vp = JSVAL_NULL;
}
}
};
}*/
return 1; return 1;
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
pub fn create(cx: *JSContext, node: Node) -> jsobj { pub fn create(cx: *JSContext, node: AbstractNode) -> jsobj {
/* let proto = match node.type_id() {
let proto = scope.write(&node, |nd| { ElementNodeTypeId(HTMLDivElementTypeId) => ~"HTMLDivElement",
match &nd.kind { ElementNodeTypeId(HTMLHeadElementTypeId) => ~"HTMLHeadElement",
&~Element(ref ed) => { ElementNodeTypeId(HTMLImageElementTypeId) => ~"HTMLImageElement",
match &ed.kind { ElementNodeTypeId(HTMLScriptElementTypeId) => ~"HTMLScriptElement",
&~HTMLDivElement(*) => ~"HTMLDivElement", ElementNodeTypeId(_) => ~"HTMLElement",
&~HTMLHeadElement(*) => ~"HTMLHeadElement", _ => fail!(~"element::create only handles elements")
&~HTMLImageElement(*) => ~"HTMLImageElement", };
&~HTMLScriptElement(*) => ~"HTMLScriptElement",
_ => ~"HTMLElement"
}
}
_ => fail!(~"element::create only handles elements")
}
});
//XXXjdm the parent should probably be the node parent instead of the global //XXXjdm the parent should probably be the node parent instead of the global
//TODO error checking //TODO error checking
@ -195,10 +161,9 @@ pub fn create(cx: *JSContext, node: Node) -> jsobj {
unsafe { unsafe {
let raw_ptr: *libc::c_void = let raw_ptr: *libc::c_void =
cast::reinterpret_cast(&squirrel_away_unique(~NodeBundle(node, scope))); cast::reinterpret_cast(&squirrel_away_unique(~node));
JS_SetReservedSlot(obj.ptr, 0, RUST_PRIVATE_TO_JSVAL(raw_ptr)); JS_SetReservedSlot(obj.ptr, 0, RUST_PRIVATE_TO_JSVAL(raw_ptr));
} }
return obj;*/ return obj;
fail!(~"stub");
} }

View file

@ -1,6 +1,7 @@
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::{str}; use dom::bindings::utils::{str, domstring_to_jsval};
use dom::node::{AbstractNode, Node}; use dom::node::{AbstractNode, Node, ElementNodeTypeId, TextNodeTypeId, CommentNodeTypeId};
use dom::node::{DoctypeNodeTypeId};
use super::element; use super::element;
use super::utils; use super::utils;
@ -21,7 +22,6 @@ use js::{JS_THIS_OBJECT, JS_SET_RVAL, JSPROP_NATIVE_ACCESSORS};
use js; use js;
pub fn init(compartment: @mut Compartment) { pub fn init(compartment: @mut Compartment) {
/*
let obj = utils::define_empty_prototype(~"Node", None, compartment); let obj = utils::define_empty_prototype(~"Node", None, compartment);
let attrs = @~[ let attrs = @~[
@ -55,117 +55,101 @@ pub fn init(compartment: @mut Compartment) {
vec::push(&mut compartment.global_props, attrs); vec::push(&mut compartment.global_props, attrs);
vec::as_imm_buf(*attrs, |specs, _len| { vec::as_imm_buf(*attrs, |specs, _len| {
JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs); JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs);
});*/ });
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
pub fn create(cx: *JSContext, node: Node) -> jsobj { pub fn create(cx: *JSContext, node: AbstractNode) -> jsobj {
/* match node.type_id() {
do scope.write(&node) |nd| { ElementNodeTypeId(_) => element::create(cx, node),
match nd.kind { TextNodeTypeId => fail!(~"no text node bindings yet"),
~Element(*) => element::create(cx, node, scope), CommentNodeTypeId => fail!(~"no comment node bindings yet"),
~Text(*) => fail!(~"no text node bindings yet"), DoctypeNodeTypeId => fail!(~"no doctype node bindings yet")
~Comment(*) => fail!(~"no comment node bindings yet"), }
~Doctype(*) => fail!(~"no doctype node bindings yet")
}
}
*/
unsafe {
transmute(0)
}
} }
pub struct NodeBundle { pub unsafe fn unwrap(obj: *JSObject) -> *rust_box<AbstractNode> {
node: AbstractNode,
}
pub fn NodeBundle(n: AbstractNode) -> NodeBundle {
NodeBundle {
node: n,
}
}
pub unsafe fn unwrap(obj: *JSObject) -> *rust_box<NodeBundle> {
let val = js::GetReservedSlot(obj, 0); let val = js::GetReservedSlot(obj, 0);
cast::reinterpret_cast(&JSVAL_TO_PRIVATE(val)) cast::reinterpret_cast(&JSVAL_TO_PRIVATE(val))
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
extern fn getFirstChild(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn getFirstChild(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
do (*bundle).payload.scope.write(&(*bundle).payload.node) |nd| { let rval = do node.with_imm_node |node| {
match nd.tree.first_child { node.getFirstChild()
Some(n) => {
let obj = create(cx, n, (*bundle).payload.scope).ptr;
*vp = RUST_OBJECT_TO_JSVAL(obj);
}
None => {
*vp = JSVAL_NULL;
}
}
}; };
}*/ match rval {
Some(n) => {
let obj = create(cx, n).ptr;
*vp = RUST_OBJECT_TO_JSVAL(obj)
}
None => *vp = JSVAL_NULL
};
}
return 1; return 1;
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
extern fn getNextSibling(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn getNextSibling(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
do (*bundle).payload.scope.write(&(*bundle).payload.node) |nd| { let rval = do node.with_imm_node |node| {
match nd.tree.next_sibling { node.getNextSibling()
Some(n) => {
let obj = create(cx, n, (*bundle).payload.scope).ptr;
*vp = RUST_OBJECT_TO_JSVAL(obj);
}
None => {
*vp = JSVAL_NULL;
}
}
}; };
}*/ match rval {
Some(n) => {
let obj = create(cx, n).ptr;
*vp = RUST_OBJECT_TO_JSVAL(obj)
}
None => *vp = JSVAL_NULL
}
}
return 1; return 1;
} }
impl NodeBundle { impl Node {
fn getNodeType() -> i32 { fn getNodeType(&self) -> i32 {
/* match self.type_id {
do self.node.read |nd| { ElementNodeTypeId(_) => 1,
match nd.kind { TextNodeTypeId => 3,
~Element(*) => 1, CommentNodeTypeId => 8,
~Text(*) => 3, DoctypeNodeTypeId => 10
~Comment(*) => 8, }
~Doctype(*) => 10
}
}*/
0
} }
}
fn getNextSibling(&self) -> Option<AbstractNode> {
self.next_sibling
}
fn getFirstChild(&self) -> Option<AbstractNode> {
self.first_child
}
}
extern fn getNodeType(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool { extern fn getNodeType(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
/*
unsafe { unsafe {
let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp)); let obj = JS_THIS_OBJECT(cx, cast::reinterpret_cast(&vp));
if obj.is_null() { if obj.is_null() {
return 0; return 0;
} }
let bundle = unwrap(obj); let node = &(*unwrap(obj)).payload;
let nodeType = (*bundle).payload.getNodeType(); let rval = do node.with_imm_node |node| {
*vp = INT_TO_JSVAL(nodeType); node.getNodeType()
}*/ };
*vp = INT_TO_JSVAL(rval);
}
return 1; return 1;
} }