summaryrefslogtreecommitdiff
path: root/src/jsapi-libdom-init.c
blob: 1077c2db4285a3a02dadaac7a0d041ff806f6f28 (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
/* Javascript spidemonkey API to libdom binding generation for class
 * initilisation
 *
 * This file is part of nsgenbind.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 */

#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "options.h"
#include "nsgenbind-ast.h"
#include "webidl-ast.h"
#include "jsapi-libdom.h"

static int output_cast_literal(struct binding *binding,
			 struct webidl_node *node)
{
	struct webidl_node *type_node = NULL;
	struct webidl_node *literal_node = NULL;
	struct webidl_node *type_base = NULL;
	enum webidl_type webidl_arg_type;

	type_node = webidl_node_find_type(webidl_node_getnode(node),
					 NULL,
					 WEBIDL_NODE_TYPE_TYPE);

	type_base = webidl_node_find_type(webidl_node_getnode(type_node),
					      NULL,
					      WEBIDL_NODE_TYPE_TYPE_BASE);

	webidl_arg_type = webidl_node_getint(type_base);

	switch (webidl_arg_type) {

	case WEBIDL_TYPE_BOOL:
		/* JSBool */
		literal_node = webidl_node_find_type(webidl_node_getnode(node),
					 NULL,
					 WEBIDL_NODE_TYPE_LITERAL_BOOL);
		fprintf(binding->outfile, "BOOLEAN_TO_JSVAL(JS_FALSE)");
		break;

	case WEBIDL_TYPE_FLOAT:
	case WEBIDL_TYPE_DOUBLE:
		/* double */
		literal_node = webidl_node_find_type(webidl_node_getnode(node),
					 NULL,
					 WEBIDL_NODE_TYPE_LITERAL_FLOAT);
		fprintf(binding->outfile, "DOUBLE_TO_JSVAL(0.0)");
		break;

	case WEBIDL_TYPE_LONG:
		/* int32_t  */
		literal_node = webidl_node_find_type(webidl_node_getnode(node),
					 NULL,
					 WEBIDL_NODE_TYPE_LITERAL_INT);
		fprintf(binding->outfile,
			"INT_TO_JSVAL(%d)",
			webidl_node_getint(literal_node));
		break;

	case WEBIDL_TYPE_SHORT:
		/* int16_t  */
		literal_node = webidl_node_find_type(webidl_node_getnode(node),
					 NULL,
					 WEBIDL_NODE_TYPE_LITERAL_INT);
		fprintf(binding->outfile,
			"INT_TO_JSVAL(%d)",
			webidl_node_getint(literal_node));
		break;


	case WEBIDL_TYPE_STRING:
	case WEBIDL_TYPE_BYTE:
	case WEBIDL_TYPE_OCTET:
	case WEBIDL_TYPE_LONGLONG:
	case WEBIDL_TYPE_SEQUENCE:
	case WEBIDL_TYPE_OBJECT:
	case WEBIDL_TYPE_DATE:
	case WEBIDL_TYPE_VOID:
	case WEBIDL_TYPE_USER:
	default:
		WARN(WARNING_UNIMPLEMENTED, "types not allowed as literal");
		break; /* @todo these types are not allowed here */
	}

	return 0;
}

static int webidl_const_define_cb(struct webidl_node *node, void *ctx)
{
	struct binding *binding = ctx;
	struct webidl_node *ident_node;

	ident_node = webidl_node_find_type(webidl_node_getnode(node),
					   NULL,
					   WEBIDL_NODE_TYPE_IDENT);
	if (ident_node == NULL) {
		/* Broken AST - must have ident */
		return 1;
	}

	fprintf(binding->outfile,
		"\tJS_DefineProperty(cx, "
		"prototype, "
		"\"%s\", ",
		webidl_node_gettext(ident_node));

	output_cast_literal(binding, node);

	fprintf(binding->outfile,
		", "
		"JS_PropertyStub, "
		"JS_StrictPropertyStub, "
		"JSPROP_READONLY | JSPROP_ENUMERATE | JSPROP_PERMANENT);\n\n");

	return 0;

}


/** output all the constant property defines for an interface */
static int
output_interface_consts(struct binding *binding,
			struct webidl_node *interface_node)
{
	struct webidl_node *members_node;

	/* generate property entries for each list (partial interfaces) */
	members_node = webidl_node_find_type(
					webidl_node_getnode(interface_node),
					NULL,
					WEBIDL_NODE_TYPE_LIST);

	while (members_node != NULL) {

		/* for each const emit a property define */
		webidl_node_for_each_type(webidl_node_getnode(members_node),
					  WEBIDL_NODE_TYPE_CONST,
					  webidl_const_define_cb,
					  binding);


		members_node = webidl_node_find_type(
			webidl_node_getnode(interface_node),
			members_node,
			WEBIDL_NODE_TYPE_LIST);
	}

	return 0;
}


static int generate_prototype_init(struct binding *binding, int inf)
{
	int inherit_inf;

	/* find this interfaces parent interface to inherit prototype from */
	inherit_inf = binding->interfaces[inf].inherit_idx;
	while ((inherit_inf != -1) &&
	       (binding->interfaces[inherit_inf].node == NULL)) {
		inherit_inf = binding->interfaces[inherit_inf].inherit_idx;
	}

	fprintf(binding->outfile,
		"\n"
		"\tprototype = JS_InitClass(cx, "
		"parent, ");

	/* either this init is being constructed without a chain or is
	 * using a prototype of a previously initialised class
	 */
	if (inherit_inf == -1) {
		fprintf(binding->outfile,
			"NULL, ");
	} else {
		fprintf(binding->outfile,
			"prototypes[%d], ",
			binding->interfaces[inherit_inf].output_idx);
	}

	fprintf(binding->outfile,
		"&JSClass_%s, "
		"NULL, "
		"0, "
		"NULL, "
		"NULL, "
		"NULL, "
		"NULL);\n",
		binding->interfaces[inf].name);

	/* check prototype construction */
	fprintf(binding->outfile,
		"\tif (prototype == NULL) {\n"
		"\t\treturn %d;\n"
		"\t}\n\n",
		binding->interfaces[inf].output_idx);

	/* store result */
	fprintf(binding->outfile,
		"\tprototypes[%d] = prototype;\n",
		binding->interfaces[inf].output_idx);

	/* output the consts for the interface and ancestors if necessary */
	do  {
		output_interface_consts(binding, binding->interfaces[inf].widl_node);
		inf = binding->interfaces[inf].inherit_idx;
	} while (inf != inherit_inf);

	return 0;
}

/** generate class initialisers
 *
 * Generates function to create the javascript class prototypes for
 * each interface in the binding.
 *
 */
int output_class_init(struct binding *binding)
{
	int res = 0;
	struct genbind_node *api_node;
	int inf;

	/* class Initialisor declaration */
	if (binding->hdrfile) {

		if (binding->interfacec > 1) {
			fprintf(binding->hdrfile,
				"\n#define %s_INTERFACE_COUNT %d",
				binding->name,
				binding->interfacec);
		}

		fprintf(binding->hdrfile,
			"\nint jsapi_InitClass_%s(JSContext *cx, JSObject *parent, JSObject **prototypes);\n\n",
			binding->name);


	}

	/* class Initialisor definition */
	fprintf(binding->outfile,
		"int\n"
		"jsapi_InitClass_%s(JSContext *cx, "
		"JSObject *parent, "
		"JSObject **prototypes)\n"
		"{\n"
		"\tJSObject *prototype;\n",
		binding->name);

	/* check for the binding having an init override */
	api_node = genbind_node_find_type_ident(binding->gb_ast,
						NULL,
						GENBIND_NODE_TYPE_API,
						"init");

	if (api_node != NULL) {
		output_code_block(binding, genbind_node_getnode(api_node));
	} else {
		/* generate interface init for each class in binding */
		for (inf = 0; inf < binding->interfacec; inf++) {
			/* skip generating javascript class
			 * initialisation for interfaces not in binding
			 */
			if (binding->interfaces[inf].node != NULL) {
				generate_prototype_init(binding, inf);
			}
		}

		fprintf(binding->outfile,
			"\n\treturn %d;\n",
			inf);
	}

	fprintf(binding->outfile, "}\n\n");

	return res;
}