/
document.rs
196 lines (171 loc) · 7.27 KB
/
document.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* 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 std::cast;
use std::libc;
use std::ptr;
use std::result;
use dom::bindings::utils::{DOMString, rust_box, squirrel_away, str};
use dom::bindings::utils::{WrapperCache, DerivedWrapper};
use dom::bindings::utils::{jsval_to_str, WrapNewBindingObject, CacheableWrapper};
use dom::bindings::utils;
use dom::document::Document;
use dom::htmlcollection::HTMLCollection;
use js::glue::*;
use js::glue::{PROPERTY_STUB, STRICT_PROPERTY_STUB};
use js::jsapi::{JS_DefineProperties};
use js::jsapi::{JS_GetReservedSlot, JS_SetReservedSlot, JS_DefineFunctions};
use js::jsapi::{JSContext, JSVal, JSObject, JSBool, JSFreeOp, JSPropertySpec, JSPropertyOpWrapper};
use js::jsapi::{JSStrictPropertyOpWrapper, JSNativeWrapper, JSFunctionSpec};
use js::rust::{Compartment, jsobj};
use js::{JSPROP_NATIVE_ACCESSORS};
use js::{JS_ARGV, JSPROP_ENUMERATE, JSPROP_SHARED, JSVAL_NULL, JS_THIS_OBJECT, JS_SET_RVAL};
use script_task::task_from_context;
use std::libc::c_uint;
use std::ptr::null;
extern fn getDocumentElement(cx: *JSContext, _argc: c_uint, vp: *mut JSVal) -> JSBool {
unsafe {
let obj = JS_THIS_OBJECT(cx, cast::transmute(vp));
if obj.is_null() {
return 0;
}
let doc = &mut (*unwrap(obj)).payload;
let root = &mut doc.root;
assert!(root.is_element());
root.wrap(cx, ptr::null(), vp); //XXXjdm proper scope at some point
return 1;
}
}
extern fn getElementsByTagName(cx: *JSContext, _argc: c_uint, vp: *JSVal) -> JSBool {
unsafe {
let obj = JS_THIS_OBJECT(cx, vp);
let argv = JS_ARGV(cx, cast::transmute(vp));
let arg0: DOMString;
let strval = jsval_to_str(cx, (*argv.offset(0)));
if strval.is_err() {
return 0;
}
arg0 = str(strval.get());
let doc = &mut (*unwrap(obj)).payload;
let rval: Option<@mut HTMLCollection>;
rval = doc.getElementsByTagName(arg0);
if rval.is_none() {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
} else {
let cache = doc.get_wrappercache();
let rval = rval.get() as @mut CacheableWrapper;
assert!(WrapNewBindingObject(cx, cache.get_wrapper(),
rval,
cast::transmute(vp)));
}
return 1;
}
}
extern fn getElementsByName(cx: *JSContext, _argc: c_uint, vp: *JSVal) -> JSBool {
unsafe {
let obj = JS_THIS_OBJECT(cx, vp);
let argv = JS_ARGV(cx, cast::transmute(vp));
let arg0: DOMString;
let strval = jsval_to_str(cx, (*argv.offset(0)));
if strval.is_err() {
return 0;
}
arg0 = str(strval.get());
let doc = &mut (*unwrap(obj)).payload;
let rval: Option<@mut HTMLCollection>;
rval = doc.getElementsByName(arg0);
if rval.is_none() {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
} else {
let cache = doc.get_wrappercache();
let rval = rval.get() as @mut CacheableWrapper;
assert!(WrapNewBindingObject(cx, cache.get_wrapper(),
rval,
cast::transmute(vp)));
}
return 1;
}
}
unsafe fn unwrap(obj: *JSObject) -> *mut rust_box<Document> {
//TODO: some kind of check if this is a Document object
let val = JS_GetReservedSlot(obj, 0);
RUST_JSVAL_TO_PRIVATE(val) as *mut rust_box<Document>
}
extern fn finalize(_fop: *JSFreeOp, obj: *JSObject) {
debug!("document finalize!");
unsafe {
let val = JS_GetReservedSlot(obj, 0);
let _doc: @Document = cast::transmute(RUST_JSVAL_TO_PRIVATE(val));
}
}
pub fn init(compartment: @mut Compartment) {
let obj = utils::define_empty_prototype(~"Document", None, compartment);
let attrs = @~[
JSPropertySpec {
name: compartment.add_name(~"documentElement"),
tinyid: 0,
flags: (JSPROP_SHARED | JSPROP_ENUMERATE | JSPROP_NATIVE_ACCESSORS) as u8,
getter: JSPropertyOpWrapper {op: getDocumentElement, info: null()},
setter: JSStrictPropertyOpWrapper {op: null(), info: null()}},
JSPropertySpec {
name: null(),
tinyid: 0,
flags: (JSPROP_SHARED | JSPROP_ENUMERATE | JSPROP_NATIVE_ACCESSORS) as u8,
getter: JSPropertyOpWrapper {op: null(), info: null()},
setter: JSStrictPropertyOpWrapper {op: null(), info: null()}}];
compartment.global_props.push(attrs);
do attrs.as_imm_buf |specs, _len| {
unsafe {
assert!(JS_DefineProperties(compartment.cx.ptr, obj.ptr, specs) == 1);
}
}
let methods = @~[JSFunctionSpec {name: compartment.add_name(~"getElementsByTagName"),
call: JSNativeWrapper {op: getElementsByTagName, info: null()},
nargs: 0,
flags: 0,
selfHostedName: null()},
JSFunctionSpec {name: compartment.add_name(~"getElementsByName"),
call: JSNativeWrapper {op: getElementsByName, info: null()},
nargs: 0,
flags: 0,
selfHostedName: null()},
JSFunctionSpec {name: null(),
call: JSNativeWrapper {op: null(), info: null()},
nargs: 0,
flags: 0,
selfHostedName: null()}];
do methods.as_imm_buf |fns, _len| {
unsafe {
JS_DefineFunctions(compartment.cx.ptr, obj.ptr, fns);
}
}
compartment.register_class(utils::instance_jsclass(~"DocumentInstance",
finalize,
ptr::null()));
}
pub fn create(compartment: @mut Compartment, doc: @mut Document) -> *JSObject {
let instance : jsobj = result::unwrap(
compartment.new_object_with_proto(~"DocumentInstance", ~"Document",
compartment.global_obj.ptr));
doc.wrapper.set_wrapper(instance.ptr);
unsafe {
let raw_ptr: *libc::c_void = cast::transmute(squirrel_away(doc));
JS_SetReservedSlot(instance.ptr, 0, RUST_PRIVATE_TO_JSVAL(raw_ptr));
compartment.define_property(~"document", RUST_OBJECT_TO_JSVAL(instance.ptr),
GetJSClassHookStubPointer(PROPERTY_STUB) as *u8,
GetJSClassHookStubPointer(STRICT_PROPERTY_STUB) as *u8,
JSPROP_ENUMERATE);
}
instance.ptr
}
impl CacheableWrapper for Document {
fn get_wrappercache(&mut self) -> &mut WrapperCache {
unsafe { cast::transmute(&self.wrapper) }
}
fn wrap_object_shared(@mut self, cx: *JSContext, _scope: *JSObject) -> *JSObject {
let script_context = task_from_context(cx);
unsafe {
create((*script_context).js_compartment, self)
}
}
}