summaryrefslogtreecommitdiff
path: root/javascript/jsapi.c
blob: 73153fe093b2237df45f3f3c2f93632fb3ebac9e (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
/*
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include "content/content.h"
#include "javascript/content.h"
#include "javascript/js.h"

#include "utils/log.h"

static JSRuntime *rt; /* global runtime */

void js_initialise(void)
{
	/* Create a JS runtime. */

#if JS_VERSION >= 180
	JS_SetCStringsAreUTF8(); /* we prefer our runtime to be utf-8 */
#endif

	rt = JS_NewRuntime(8L * 1024L * 1024L);
	JSLOG("New runtime handle %p", rt);

	/* register script content handler */
	javascript_init();
}

void js_finalise(void)
{
	if (rt != NULL) {
		JSLOG("destroying runtime handle %p", rt);
		JS_DestroyRuntime(rt);
	}
	JS_ShutDown();
}

/* The error reporter callback. */
static void js_reportError(JSContext *cx, const char *message, JSErrorReport *report)
{
	JSLOG("%s:%u:%s",
	      report->filename ? report->filename : "<no filename>",
	      (unsigned int) report->lineno,
	      message);
}

jscontext *js_newcontext(void)
{
	JSContext *cx;

	if (rt == NULL) {
		return NULL;
	}

	cx = JS_NewContext(rt, 8192);
	if (cx == NULL) {
		return NULL;
	}
	JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT );
	JS_SetVersion(cx, JSVERSION_LATEST);
	JS_SetErrorReporter(cx, js_reportError);

	/*JS_SetGCZeal(cx, 2); */

	JSLOG("New Context %p", cx);

	return (jscontext *)cx;
}

void js_destroycontext(jscontext *ctx)
{
	JSContext *cx = (JSContext *)ctx;
	if (cx != NULL) {
		JSLOG("Destroying Context %p", cx);
		JS_DestroyContext(cx);
	}
}


/** Create new compartment to run scripts within
 *
 * This performs the following actions
 * 1. constructs a new global object by initialising a window class
 * 2. Instantiate the global a window object
 */
jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv)
{
	JSContext *cx = (JSContext *)ctx;
	JSObject *window_proto;
	JSObject *window;

	if (cx == NULL) {
		return NULL;
	}

	window_proto = jsapi_InitClass_Window(cx, NULL);
	if (window_proto == NULL) {
		JSLOG("Unable to initialise window class");
		return NULL;
	}

	window = jsapi_new_Window(cx, window_proto, NULL, win_priv, doc_priv);

	return (jsobject *)window;
}

bool js_exec(jscontext *ctx, const char *txt, size_t txtlen)
{
	JSContext *cx = (JSContext *)ctx;
	jsval rval;

	/* JSLOG("%p \"%s\"",cx ,txt); */

	if (ctx == NULL) {
		return false;
	}

	if (txt == NULL) {
		return false;
	}

	if (txtlen == 0) {
		return false;
	}

	if (JS_EvaluateScript(cx,
			      JS_GetGlobalObject(cx),
			      txt, txtlen,
			      "<head>", 0, &rval) == JS_TRUE) {

		return true;
	}

	return false;
}

dom_exception _dom_event_create(dom_document *doc, dom_event **evt);
#define dom_event_create(d, e) _dom_event_create((dom_document *)(d), (dom_event **) (e))

bool js_fire_event(jscontext *ctx, const char *type, dom_document *doc, dom_node *target)
{
	JSContext *cx = (JSContext *)ctx;
	dom_node *node = target;
	JSObject *jsevent;
	jsval rval;
	jsval argv[1];
	JSBool ret = JS_TRUE;
	dom_exception exc;
	dom_event *event;
	dom_string *type_dom;

	if (cx == NULL) {
		return false;
	}

	if (node == NULL) {
		/* deliver manufactured event to window */
		JSLOG("Dispatching event %s at window", type);

		/* create and initialise and event object */
		exc = dom_string_create((unsigned char*)type,
					strlen(type),
					&type_dom);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		exc = dom_event_create(doc, &event);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		exc = dom_event_init(event, type_dom, false, false);
		dom_string_unref(type_dom);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		jsevent = jsapi_new_Event(cx, NULL, NULL, event);
		if (jsevent == NULL) {
			return false;
		}

		/* dispatch event at the window object */
		argv[0] = OBJECT_TO_JSVAL(jsevent);

		ret = JS_CallFunctionName(cx,
					  JS_GetGlobalObject(cx),
					  "dispatchEvent",
					  1,
					  argv,
					  &rval);
	} else {
		JSLOG("Dispatching event %s at %p", type, node);

		/* create and initialise and event object */
		exc = dom_string_create((unsigned char*)type,
					strlen(type),
					&type_dom);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		exc = dom_event_create(doc, &event);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		exc = dom_event_init(event, type_dom, true, true);
		dom_string_unref(type_dom);
		if (exc != DOM_NO_ERR) {
			return false;
		}

		dom_event_target_dispatch_event(node, event, &ret);

	}

	if (ret == JS_TRUE) {
		return true;
	}
	return false;
}

struct js_dom_event_private {
	JSContext *cx; /* javascript context */
	jsval funcval; /* javascript function to call */
	struct dom_node *node; /* dom node event listening on */
	dom_string *type; /* event type */
	dom_event_listener *listener; /* the listener containing this */
};

static void
js_dom_event_listener(struct dom_event *event, void *pw)
{
	struct js_dom_event_private *private = pw;
	jsval event_argv[1];
	jsval event_rval;
	JSObject *jsevent;

	JSLOG("WOOT dom event with %p", private);

	if (!JSVAL_IS_VOID(private->funcval)) {
		jsevent = jsapi_new_Event(private->cx, NULL, NULL, event);
		if (jsevent != NULL) {

		/* dispatch event at the window object */
		event_argv[0] = OBJECT_TO_JSVAL(jsevent);

		JS_CallFunctionValue(private->cx,
				     NULL,
				     private->funcval,
				     1,
				     event_argv,
				     &event_rval);
		}
	}
}

/* add a listener to a dom node
 *
 * 1. Create a dom_event_listener From a handle_event function pointer
 *    and a private word In a document context
 *
 * 2. Register for your events on a target (dom nodes are targets)
 *    dom_event_target_add_event_listener(node, evt_name, listener,
 *    capture_or_not)
 *
 */

bool
js_dom_event_add_listener(jscontext *ctx,
			  struct dom_document *document,
			  struct dom_node *node,
			  struct dom_string *event_type_dom,
			  void *js_funcval)
{
	JSContext *cx = (JSContext *)ctx;
	dom_exception exc;
	struct js_dom_event_private *private;

	private = malloc(sizeof(struct js_dom_event_private));
	if (private == NULL) {
		return false;
	}

	exc = dom_event_listener_create(document,
					js_dom_event_listener,
					private,
					&private->listener);
	if (exc != DOM_NO_ERR) {
		return false;
	}

	private->cx = cx;
	private->funcval = *(jsval *)js_funcval;
	private->node = node;
	private->type = event_type_dom;

	JSLOG("adding %p to listener", private);

	JSAPI_ADD_VALUE_ROOT(cx, &private->funcval);
	exc = dom_event_target_add_event_listener(private->node,
						  private->type,
						  private->listener,
						  true);
	if (exc != DOM_NO_ERR) {
		JSLOG("failed to add listener");
		JSAPI_REMOVE_VALUE_ROOT(cx, &private->funcval);
	}

	return true;
}