summaryrefslogtreecommitdiff
path: root/javascript/jsapi/window.bnd
blob: 45541faecfb3bb3cd69141fd4bd9137e52094c96 (plain)
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
196
197
198
199
/* binding to generate window */

#include "dom.bnd"

webidlfile "html.idl";

hdrcomment "Part of NetSurf Project";

preamble %{

#include <dom/dom.h>
        
#include "utils/config.h"
#include "utils/log.h"

#include "javascript/jsapi.h"
#include "javascript/jsapi/binding.h"

%}

binding window {
	type js_libdom; /* the binding type */

	interface Window; /* Web IDL interface to generate */

	/* private are parameters to constructor stored in private
         *  context structure.
         *
         * internal are value stored in private context structure but not
         *  passed to constructor but are considered for property 
         *  getters/setters.
         */
	private "struct browser_window *" bw;
	private "struct html_content *" htmlc;
	internal "JSObject *" document;
	internal "JSObject *" navigator;
	internal "JSObject *" console;
	internal "JSObject *" location;
}

api mark %{
	if (private != NULL) {
		if (private->document != NULL) {
			JSAPI_GCMARK(private->document);
		}
		if (private->navigator != NULL) {
			JSAPI_GCMARK(private->navigator);
		}
		if (private->console != NULL) {
			JSAPI_GCMARK(private->console);
		}
		if (private->location != NULL) {
			JSAPI_GCMARK(private->location);
		}
	}
%}

api global %{
%}

api init %{
	JSObject *user_proto;

	prototype = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL);
	if (prototype == NULL) {
		return NULL;
	}

	/** @todo reconsider global object handling. future
	 * editions of spidermonkey appear to be removing the
	 * idea of a global so we probably need to handle
	 * global object references internally
	 */

	/* set the contexts global */
	JS_SetGlobalObject(cx, prototype);

	/* Populate the global object with the standard globals, like
	 *  Object and Array.
	 */
	if (!JS_InitStandardClasses(cx, prototype)) {
		return NULL;
	}

	/* add functions to prototype */
	if (!JS_DefineFunctions(cx, prototype, jsclass_functions)) {
		return NULL;
	}

	/* add properties to prototype */
	if (!JS_DefineProperties(cx, prototype, jsclass_properties))
		return NULL;

	/* Initialises all the user javascript classes to make their
	 * prototypes available. 
	 */
	/** @todo should we be managing these prototype objects ourselves */
	user_proto = jsapi_InitClass_Document(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_Navigator(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_Location(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_Console(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_HTMLElement(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_HTMLCollection(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_NodeList(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

	user_proto = jsapi_InitClass_Text(cx, prototype);
	if (user_proto == NULL) {
		return NULL;
	}

%}

api new %{
	/* @todo sort out windows that are not globals */
	assert(parent == NULL);

        /* the window object is the global so its prototype *is* the instance */
        newobject = prototype;

	/* instantiate the subclasses off the window global */
	private->document = jsapi_new_Document(cx,
                                               NULL,
                                               newobject, 
                                               (dom_document *)dom_node_ref(htmlc->document), 
                                               htmlc);
	if (private->document == NULL) { 
		free(private);
		return NULL;
	}

	private->navigator = jsapi_new_Navigator(cx, NULL, newobject);
	if (private->navigator == NULL) {
		free(private);
		return NULL;
	}

	private->location = jsapi_new_Location(cx, NULL, newobject, bw);
	if (private->location == NULL) {
		free(private);
		return NULL;
	}

	private->console = jsapi_new_Console(cx, NULL, newobject);
	if (private->console == NULL) {
		free(private);
		return NULL;
	}

	/** @todo forms, history */

	LOG(("Created new window object %p", newobject));
%}

operation confirm %{
	warn_user(message, NULL);
%}

operation alert %{
	warn_user(message, NULL);
%}

operation prompt %{
	warn_user(message, NULL);
%}

getter window %{
	jsret = obj;
%}

getter self %{
	jsret = obj;
%}