summaryrefslogtreecommitdiff
path: root/javascript/jsapi/window.bnd
blob: 0439bc0a19844f4ea281e0024ed09594f5316661 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* Binding to generate window interface
 *
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * Released under the terms of the MIT License,
 *         http://www.opensource.org/licenses/mit-license
 */


webidlfile "html.idl";
webidlfile "dom.idl";
webidlfile "console.idl";

hdrcomment "Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>";
hdrcomment "This file is part of NetSurf, http://www.netsurf-browser.org/";
hdrcomment "Released under the terms of the MIT License,";
hdrcomment "        http://www.opensource.org/licenses/mit-license";

preamble %{

#include <dom/dom.h>

#include "utils/config.h"
#include "utils/log.h"
#include "utils/corestrings.h"
#include "render/html_internal.h"
#include "javascript/jsapi.h"

#include "console.h"
#include "navigator.h"
#include "event.h"
#include "node.h"
#include "htmlcollection.h"
#include "nodelist.h"
#include "htmldocument.h"
#include "text.h"
#include "comment.h"
#include "htmlelement.h"
#include "window.h"
#include "location.h"

struct browser_window *jsapi_get_browser_window(JSContext *cx);

%}

prologue %{

struct browser_window *jsapi_get_browser_window(JSContext *cx)
{
	struct jsclass_private *private;

	private = JS_GetInstancePrivate(cx,
					JS_GetGlobalObject(cx),
					&JSClass_Window,
					NULL);
	if (private != NULL) {
		return private->bw;
	}

	return NULL;
}

static bool
init_user_prototypes(JSContext *cx,
		     struct jsclass_private *private,
		     JSObject *parent)
{
	/* Initialises all the user javascript classes to make their
	 * prototypes available.
	 */
	/** @todo should we be managing these prototype objects ourselves */
	private->prototype_Document = jsapi_InitClass_Document(cx, parent);
	if (private->prototype_Document == NULL) {
		return false;
	}

	private->prototype_Navigator = jsapi_InitClass_Navigator(cx, parent);
	if (private->prototype_Navigator == NULL) {
		return false;
	}

	private->prototype_Location = jsapi_InitClass_Location(cx, parent);
	if (private->prototype_Location == NULL) {
		return false;
	}

	private->prototype_Console = jsapi_InitClass_Console(cx, parent);
	if (private->prototype_Console == NULL) {
		return false;
	}

	private->prototype_HTMLElement = jsapi_InitClass_HTMLElement(cx, parent);
	if (private->prototype_HTMLElement == NULL) {
		return false;
	}

	private->prototype_HTMLCollection = jsapi_InitClass_HTMLCollection(cx, parent);
	if (private->prototype_HTMLCollection == NULL) {
		return false;
	}

	private->prototype_NodeList = jsapi_InitClass_NodeList(cx, parent);
	if (private->prototype_NodeList == NULL) {
		return false;
	}

	private->prototype_Text = jsapi_InitClass_Text(cx, parent);
	if (private->prototype_Text == NULL) {
		return false;
	}

	private->prototype_Comment = jsapi_InitClass_Comment(cx, parent);
	if (private->prototype_Comment == NULL) {
		return false;
	}

	private->prototype_Node = jsapi_InitClass_Node(cx, parent);
	if (private->prototype_Node == NULL) {
		return false;
	}

	private->prototype_Event = jsapi_InitClass_Event(cx, parent);
	if (private->prototype_Event == NULL) {
		return false;
	}
	return true;
}

%}

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

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

	private "struct browser_window *" bw;
	private "struct html_content *" htmlc;

	/* prototypes held in this object */
	internal "JSObject *" prototype_Document;
	internal "JSObject *" prototype_Navigator;
	internal "JSObject *" prototype_Location;
	internal "JSObject *" prototype_Console;
	internal "JSObject *" prototype_HTMLElement;
	internal "JSObject *" prototype_HTMLCollection;
	internal "JSObject *" prototype_NodeList;
	internal "JSObject *" prototype_Text;
	internal "JSObject *" prototype_Comment;
	internal "JSObject *" prototype_Node;
	internal "JSObject *" prototype_Event;

	/** document instantiated on first use */
	property unshared document;

	/** navigator instantiated on first use */
	property unshared navigator;

	/** console instantiated on first use */
	property unshared console;

        /** location is unshared */
	property unshared location;

	/** @todo instantiate forms, history etc. attributes */

	/* events through a single interface */
	property unshared type EventHandler;
}

api mark %{

	if (private != NULL) {
		if (private->prototype_Document != NULL) {
			JSAPI_GCMARK(private->prototype_Document);
		}

		if (private->prototype_Navigator != NULL) {
			JSAPI_GCMARK(private->prototype_Navigator);
		}

		if (private->prototype_Location != NULL) {
			JSAPI_GCMARK(private->prototype_Location);
		}

		if (private->prototype_Console != NULL) {
			JSAPI_GCMARK(private->prototype_Console);
		}

		if (private->prototype_HTMLElement != NULL) {
			JSAPI_GCMARK(private->prototype_HTMLElement);
		}

		if (private->prototype_HTMLCollection != NULL) {
			JSAPI_GCMARK(private->prototype_HTMLCollection);
		}

		if (private->prototype_NodeList != NULL) {
			JSAPI_GCMARK(private->prototype_NodeList);
		}

		if (private->prototype_Text != NULL) {
			JSAPI_GCMARK(private->prototype_Text);
		}

		if (private->prototype_Comment != NULL) {
			JSAPI_GCMARK(private->prototype_Comment);
		}

		if (private->prototype_Node != NULL) {
			JSAPI_GCMARK(private->prototype_Node);
		}

		if (private->prototype_Event != NULL) {
			JSAPI_GCMARK(private->prototype_Event);
		}
	}
%}

api global %{
%}

api init %{
	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;

	/* as the global just got changed, force a GC run */
	JS_GC(cx);
%}

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;

	if (init_user_prototypes(cx, private, prototype) == false) {
		/* prototype initialisation failed */
		free(private);
		return NULL;
	}

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

getter document %{
	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx, vp))) {
		/* already created - return it */
		return JS_TRUE;
	}

	/* instantiate the subclasses off the window global */
	jsret = jsapi_new_Document(cx,
				   NULL,
				   NULL,
				   (dom_document *)dom_node_ref(private->htmlc->document),
				   private->htmlc);
%}

getter navigator %{
	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx, vp))) {
		/* already created - return it */
		return JS_TRUE;
	}

	jsret = jsapi_new_Navigator(cx, NULL, NULL);
%}

getter console %{
	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx, vp))) {
		/* already created - return it */
		return JS_TRUE;
	}

	jsret = jsapi_new_Console(cx, NULL, NULL);
%}

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

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

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

/* boolean dispatchEvent(Event event); */
operation dispatchEvent %{
	/* this implementation is unique to the window object as it is
	 * not a "real" dom node.
	 */

	/* caution, this must match the struct generated from event.bnd */
	struct {
		dom_event *event;
	} *event_private;
	dom_string *type_dom = NULL;
	dom_exception exc;
	jsval eventval = JSVAL_VOID;
	jsval event_argv[1];
	jsval event_rval;

	event_private = JS_GetInstancePrivate(cx, event, &JSClass_Event, NULL);
	if (event_private->event == NULL) {
		/** @todo type error? */
		jsret = JS_FALSE;
	} else {
		exc = dom_event_get_type(event_private->event, &type_dom);
		if (exc == DOM_NO_ERR) {

			if (dom_string_isequal(type_dom, corestring_dom_load)) {
				JS_GetProperty(cx, JSAPI_THIS_OBJECT(cx, vp), "onload", &eventval);
			}

			if (!JSVAL_IS_VOID(eventval)) {
				event_argv[0] = eventval;
				jsret = JS_CallFunctionValue(cx, NULL, eventval, 1, event_argv, &event_rval);
			}
		}
	}
%}

getter location %{
	if (!JSVAL_IS_VOID(JSAPI_PROP_RVAL(cx, vp))) {
		/* already created - return it */
		return JS_TRUE;
	}

/* should get the docuemnts location
	jsval loc;
	JS_GetProperty(cx, private->document, "location", &loc);
	jsret = JSVAL_TO_OBJECT(loc);
*/

	jsret = jsapi_new_Location(cx,
				   NULL,
				   NULL,
				   llcache_handle_get_url(private->htmlc->base.llcache));
%}

getter window %{
	jsret = obj;
%}

getter self %{
	jsret = obj;
%}

/* very iffy implementation */
getter top %{
	jsret = obj;
%}

getter EventHandler %{
	/* this implementation is unique to the window object as it is
	 * not a dom node.
	 */
	JSLOG("propname[%d]=\"%s\"",
	      tinyid,
	      jsclass_properties[tinyid].name);
%}

setter EventHandler %{
	/* this implementation is unique to the window object as it is
	 * not a dom node.
	 */
	JSLOG("propname[%d]=\"%s\"",
	      tinyid,
	      jsclass_properties[tinyid].name);
%}