summaryrefslogtreecommitdiff
path: root/javascript/dukky.c
blob: 9fd208ff56a3c5581df740667d8903d79025215d (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
403
404
405
406
407
408
409
/*
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright 2015 All of us.
 *
 * 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/>.
 */

/** \file
 * Duktapeish implementation of javascript engine functions.
 */

#include "content/content.h"
#include "utils/nsoption.h"

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

#include "duktape.h"
#include "dukky.h"

#include <dom/dom.h>

static duk_ret_t dukky_populate_object(duk_context *ctx)
{
	/* ... obj args protoname nargs */
	int nargs = duk_get_int(ctx, -1);
	duk_pop(ctx);
	/* ... obj args protoname */
	duk_get_global_string(ctx, PROTO_MAGIC);
	/* .. obj args protoname prototab */
	duk_insert(ctx, -2);
	/* ... obj args prototab protoname */
	duk_get_prop(ctx, -2);
	/* ... obj args prototab {proto/undefined} */
	if (duk_is_undefined(ctx, -1)) {
		LOG("RuhRoh, couldn't find a prototype, getting htmlelement");
		duk_pop(ctx);
		duk_push_string(ctx, PROTO_NAME(html_unknown_element));
		duk_get_prop(ctx, -2);
	}
	/* ... obj args prototab proto */
	duk_dup(ctx, -1);
	/* ... obj args prototab proto proto */
	duk_set_prototype(ctx, -(nargs+4));
	/* ... obj[proto] args prototab proto */
	duk_get_prop_string(ctx, -1, INIT_MAGIC);
	/* ... obj[proto] args prototab proto initfn */
	duk_insert(ctx, -(nargs+4));
	/* ... initfn obj[proto] args prototab proto */
	duk_pop_2(ctx);
	/* ... initfn obj[proto] args */
	LOG("Call the init function");
	duk_call(ctx, nargs + 1);
	return 1; /* The object */
}

duk_ret_t dukky_create_object(duk_context *ctx, const char *name, int args)
{
	duk_ret_t ret;
	LOG("name=%s nargs=%d", name+2, args);
	/* ... args */
	duk_push_object(ctx);
	/* ... args obj */
	duk_insert(ctx, -(args+1));
	/* ... obj args */
	duk_push_string(ctx, name);
	/* ... obj args name */
	duk_push_int(ctx, args);
	/* ... obj args name nargs */
	if ((ret = duk_safe_call(ctx, dukky_populate_object, args + 3, 1))
	    != DUK_EXEC_SUCCESS)
		return ret;
	LOG("created");
	return DUK_EXEC_SUCCESS;
}

static duk_ret_t
dukky_to_string(duk_context *ctx)
{
	/* */
	duk_push_this(ctx);
	/* this */
	duk_get_prototype(ctx, -1);
	/* this proto */
	duk_get_prop_string(ctx, -1, MAGIC(klass_name));
	/* this proto classname */
	duk_push_string(ctx, "[object ");
	/* this proto classname str */
	duk_insert(ctx, -2);
	/* this proto str classname */
	duk_push_string(ctx, "]");
	/* this proto str classname str */
	duk_concat(ctx, 3);
	/* this proto str */
	return 1;
}

static duk_ret_t dukky_create_prototype(duk_context *ctx,
					duk_safe_call_function genproto,
					const char *proto_name,
					const char *klass_name)
{
	duk_int_t ret;
	duk_push_object(ctx);
	if ((ret = duk_safe_call(ctx, genproto, 1, 1)) != DUK_EXEC_SUCCESS) {
		duk_pop(ctx);
		LOG("Failed to register prototype for %s", proto_name + 2);
		return ret;
	}
	/* top of stack is the ready prototype, inject it */
	duk_push_string(ctx, klass_name);
	duk_put_prop_string(ctx, -2, MAGIC(klass_name));
	duk_push_c_function(ctx, dukky_to_string, 0);
	duk_put_prop_string(ctx, -2, "toString");
	duk_push_string(ctx, "toString");
	duk_def_prop(ctx, -2, DUK_DEFPROP_HAVE_ENUMERABLE);
	duk_put_global_string(ctx, proto_name);
	return 0;
}

duk_bool_t
dukky_push_node_stacked(duk_context *ctx)
{
	int top_at_fail = duk_get_top(ctx) - 2;
	/* ... nodeptr klass */
	duk_get_global_string(ctx, NODE_MAGIC);
	/* ... nodeptr klass nodes */
	duk_dup(ctx, -3);
	/* ... nodeptr klass nodes nodeptr */
	duk_get_prop(ctx, -2);
	/* ... nodeptr klass nodes node/undefined */
	if (duk_is_undefined(ctx, -1)) {
		/* ... nodeptr klass nodes undefined */
		duk_pop(ctx);
		/* ... nodeptr klass nodes */
		duk_push_object(ctx);
		/* ... nodeptr klass nodes obj */
		duk_dup(ctx, -4);
		/* ... nodeptr klass nodes obj nodeptr */
		duk_dup(ctx, -4);
		/* ... nodeptr klass nodes obj nodeptr klass */
		duk_push_int(ctx, 1);
		/* ... nodeptr klass nodes obj nodeptr klass 1 */
		if (duk_safe_call(ctx, dukky_populate_object, 4, 1)
		    != DUK_EXEC_SUCCESS) {
			duk_set_top(ctx, top_at_fail);
			LOG("Boo and also hiss");
			return false;
		}
		/* ... nodeptr klass nodes node */
		duk_dup(ctx, -4);
		/* ... nodeptr klass nodes node nodeptr */
		duk_dup(ctx, -2);
		/* ... nodeptr klass nodes node nodeptr node */
		duk_put_prop(ctx, -4);
		/* ... nodeptr klass nodes node */
	}
	/* ... nodeptr klass nodes node */
	duk_insert(ctx, -4);
	/* ... node nodeptr klass nodes */
	duk_pop_3(ctx);
	/* ... node */
	return true;
}

static void
dukky_push_node_klass(duk_context *ctx, struct dom_node *node)
{
	dom_node_type nodetype;
	dom_exception err;
	
	err = dom_node_get_node_type(node, &nodetype);
	if (err != DOM_NO_ERR) {
		/* Oh bum, just node then */
		duk_push_string(ctx, PROTO_NAME(node));
		return;
	}
	
	switch(nodetype) {
        case DOM_ELEMENT_NODE: {
		dom_string *namespace;
		err = dom_node_get_namespace(node, &namespace);
		if (err != DOM_NO_ERR) {
			/* Feck it, element */
			duk_push_string(ctx, PROTO_NAME(element));
			break;
		}
		if (namespace == NULL) {
			/* No namespace, -> element */
			duk_push_string(ctx, PROTO_NAME(element));
			break;
		}
		
		/* TODO: Work out how to decide between Element and HTML */
		duk_push_string(ctx, PROTO_NAME(html_unknown_element));
		
		dom_string_unref(namespace);
		break;
	}
        case DOM_TEXT_NODE:
		duk_push_string(ctx, PROTO_NAME(text));
		break;
        case DOM_COMMENT_NODE:
		duk_push_string(ctx, PROTO_NAME(comment));
		break;
        case DOM_DOCUMENT_NODE:
		duk_push_string(ctx, PROTO_NAME(document));
		break;
        case DOM_ATTRIBUTE_NODE:
        case DOM_PROCESSING_INSTRUCTION_NODE:
	case DOM_DOCUMENT_TYPE_NODE:
        case DOM_DOCUMENT_FRAGMENT_NODE:
        case DOM_NOTATION_NODE:
	case DOM_ENTITY_REFERENCE_NODE:
        case DOM_ENTITY_NODE:
        case DOM_CDATA_SECTION_NODE:
	default:
		/* Oh bum, just node then */
		duk_push_string(ctx, PROTO_NAME(node));
	}
}

duk_bool_t
dukky_push_node(duk_context *ctx, struct dom_node *node)
{
	LOG("Pushing node %p", node);
	/* First check if we can find the node */
	/* ... */
	duk_get_global_string(ctx, NODE_MAGIC);
	/* ... nodes */
	duk_push_pointer(ctx, node);
	/* ... nodes nodeptr */
	duk_get_prop(ctx, -2);
	/* ... nodes node/undefined */
	if (!duk_is_undefined(ctx, -1)) {
		/* ... nodes node */
		duk_insert(ctx, -2);
		/* ... node nodes */
		duk_pop(ctx);
		/* ... node */
		LOG("Found it memoised");
		return true;
	}
	/* ... nodes undefined */
	duk_pop_2(ctx);
	/* ... */
	/* We couldn't, so now we determine the node type and then
	 * we ask for it to be created
	 */
	duk_push_pointer(ctx, node);
	/* ... nodeptr */
	dukky_push_node_klass(ctx, node);
	/* ... nodeptr klass */
	return dukky_push_node_stacked(ctx);
}

duk_bool_t
dukky_instanceof(duk_context *ctx, const char *klass)
{
	/* ... ??? */
	if (!duk_check_type(ctx, -1, DUK_TYPE_OBJECT)) return false;
	/* ... obj */
	duk_get_global_string(ctx, PROTO_MAGIC);
	/* ... obj protos */
	duk_get_prop_string(ctx, -1, klass);
	/* ... obj protos goalproto */
	duk_get_prototype(ctx, -3);
	/* ... obj protos goalproto proto? */
	while (!duk_is_undefined(ctx, -1)) {
		if (duk_strict_equals(ctx, -1, -2)) {
			duk_pop_3(ctx);
			return true;
		}
		duk_get_prototype(ctx, -1);
		/* ... obj protos goalproto proto proto? */
		duk_replace(ctx, -2);
		/* ... obj protos goalproto proto? */
	}
	duk_pop_3(ctx);
	/* ... obj */
	return false;
}


/**************************************** js.h ******************************/
struct jscontext {
	duk_context *ctx;
	duk_context *thread;
};

#define CTX (ctx->thread)

void js_initialise(void)
{
	/* NADA for now */
	nsoption_set_bool(enable_javascript, true);
	javascript_init();
}

void js_finalise(void)
{
	/* NADA for now */
}

#define DUKKY_NEW_PROTOTYPE(klass, klass_name)					\
	dukky_create_prototype(ctx, dukky_##klass##___proto, PROTO_NAME(klass), klass_name)

jscontext *js_newcontext(int timeout, jscallback *cb, void *cbctx)
{
	duk_context *ctx;
	jscontext *ret = calloc(1, sizeof(*ret));
	LOG("Creating new JS context");
	if (ret == NULL) return NULL;
	ctx = ret->ctx = duk_create_heap_default();
	if (ret->ctx == NULL) { free(ret); return NULL; }
	/* Create the prototype stuffs */
	duk_push_global_object(ctx);
	duk_push_boolean(ctx, true);
	duk_put_prop_string(ctx, -2, "protos");
	duk_put_global_string(ctx, PROTO_MAGIC);
	/* Create prototypes here? */
	DUKKY_NEW_PROTOTYPE(event_target, "EventTarget");
	DUKKY_NEW_PROTOTYPE(window, "Window");
	DUKKY_NEW_PROTOTYPE(node, "Node");
	DUKKY_NEW_PROTOTYPE(character_data, "CharacterData");
	DUKKY_NEW_PROTOTYPE(text, "Text");
	DUKKY_NEW_PROTOTYPE(comment, "Comment");
	DUKKY_NEW_PROTOTYPE(document, "Document");
	DUKKY_NEW_PROTOTYPE(element, "Element");
	DUKKY_NEW_PROTOTYPE(html_element, "HTMLElement");
	DUKKY_NEW_PROTOTYPE(html_unknown_element, "HTMLUnknownElement");
	return ret;
}

void js_destroycontext(jscontext *ctx)
{
	LOG("Destroying context");
	duk_destroy_heap(ctx->ctx);
	free(ctx);
}

jsobject *js_newcompartment(jscontext *ctx, void *win_priv, void *doc_priv)
{
	/* Pop any active thread off */
	LOG("Yay, new compartment, win_priv=%p, doc_priv=%p", win_priv, doc_priv);
	duk_set_top(ctx->ctx, 0);
	duk_push_thread(ctx->ctx);
	ctx->thread = duk_require_context(ctx->ctx, -1);
	duk_push_int(CTX, 0);
	duk_push_int(CTX, 1);
	duk_push_int(CTX, 2);
	/* Manufacture a Window object */
	/* win_priv is a browser_window, doc_priv is an html content struct */
	duk_push_pointer(CTX, win_priv);
	duk_push_pointer(CTX, doc_priv);
	dukky_create_object(CTX, PROTO_NAME(window), 2);
	duk_push_global_object(CTX);
	duk_put_prop_string(CTX, -2, PROTO_MAGIC);
	duk_set_global_object(CTX);
	
	/* Now we need to prepare our node mapping table */
	duk_push_object(CTX);
	duk_put_global_string(CTX, NODE_MAGIC);
	
	return (jsobject *)ctx;
}

static duk_ret_t eval_top_string(duk_context *ctx)
{
	duk_eval(ctx);
	return 0;
}

bool js_exec(jscontext *ctx, const char *txt, size_t txtlen)
{
	assert(ctx);
	if (txt == NULL || txtlen == 0) return false;
	duk_set_top(CTX, 0);
	duk_push_lstring(CTX, txt, txtlen);
	LOG("Dumpy");
	DUKKY_DUMP_STACK(CTX);
	if (duk_safe_call(CTX, eval_top_string, 1, 1) == DUK_EXEC_ERROR) {
		LOG("JAVASCRIPT SPLOOF: %s", duk_safe_to_string(CTX, 0));
		return false;
	}
	if (duk_get_top(CTX) == 0) duk_push_boolean(CTX, false);
	LOG("Returning %s", duk_get_boolean(CTX, 0) ? "true" : "false");
	return duk_get_boolean(CTX, 0);
}

bool js_fire_event(jscontext *ctx, const char *type, struct dom_document *doc, struct dom_node *target)
{
	/* La La La */
	LOG("Oh arse, an event: %s", type);
	return true;
}