summaryrefslogtreecommitdiff
path: root/src/jsapi-libdom.c
blob: 4dc2db8ea67eee5e0b4ea4d193a13bda031790c9 (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
/* binding output generator for jsapi(spidermonkey) to libdom 
 *
 * This file is part of nsgenjsbind.
 * 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 "options.h"
#include "genjsbind-ast.h"
#include "webidl-ast.h"
#include "jsapi-libdom.h"

#define HDR_COMMENT_SEP     "\n * "
#define HDR_COMMENT_PREABLE "Generated by nsgenjsapi"

struct binding {
	const char *name; /* name of the binding */
	const char *interface; /* webidl interface binding is for */
};

static int webidl_preamble_cb(struct genbind_node *node, void *ctx)
{
	FILE *outfile = ctx;
	char *txt;
	txt = genbind_node_gettext(node);
	fprintf(outfile, "%s", txt);
	return 0;
}

static int 
output_preamble(FILE *outfile, struct genbind_node *genbind_ast)
{
	genbind_node_for_each_type(genbind_ast,
				   GENBIND_NODE_TYPE_PREAMBLE, 
				   webidl_preamble_cb, 
				   outfile);
	return 0;
}

static int webidl_hdrcomments_cb(struct genbind_node *node, void *ctx)
{
	FILE *outfile = ctx;
	char *txt;
	txt = genbind_node_gettext(node);
	fprintf(outfile, HDR_COMMENT_SEP"%s",txt);
	return 0;
}

static int webidl_hdrcomment_cb(struct genbind_node *node, void *ctx)
{
	genbind_node_for_each_type(genbind_node_getnode(node),
				   GENBIND_NODE_TYPE_STRING, 
				   webidl_hdrcomments_cb, 
				   ctx);
	return 0;
}

static int 
output_header_comments(FILE *outfile, struct genbind_node *genbind_ast)
{
	fprintf(outfile, "/* "HDR_COMMENT_PREABLE);
	genbind_node_for_each_type(genbind_ast,
				   GENBIND_NODE_TYPE_HDRCOMMENT, 
				   webidl_hdrcomment_cb, 
				   outfile);
	fprintf(outfile,"\n */\n\n");
	return 0;
}

static int webidl_file_cb(struct genbind_node *node, void *ctx)
{
	struct webidl_node **webidl_ast = ctx;
	char *filename;

	filename = genbind_node_gettext(node);

	return webidl_parsefile(filename, webidl_ast);

}

static int 
read_webidl(struct genbind_node *genbind_ast, struct webidl_node **webidl_ast)
{
	int res;

	res = genbind_node_for_each_type(genbind_ast,
					 GENBIND_NODE_TYPE_WEBIDLFILE, 
					 webidl_file_cb, 
					 webidl_ast);

        /* debug dump of web idl AST */
	if (options->verbose) {
		webidl_ast_dump(*webidl_ast, 0);
	}
	return res;
}


static int 
output_function_spec(FILE *outfile, 
		     struct genbind_node *genbind_ast,
		     struct binding *binding)
{
	fprintf(outfile, "%s\n%s\n", binding->name, binding->interface);

	return 0;
}

static struct binding *binding_new(struct genbind_node *genbind_ast)
{
	struct binding *nb;
	struct genbind_node *binding_node;
	struct genbind_node *ident_node;
	struct genbind_node *interface_node;

	binding_node = genbind_node_find(genbind_ast,
					 NULL,
					 genbind_cmp_node_type,
					 (void *)GENBIND_NODE_TYPE_BINDING);

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

	ident_node = genbind_node_find(genbind_node_getnode(binding_node),
				       NULL,
				       genbind_cmp_node_type,
				       (void *)GENBIND_NODE_TYPE_IDENT);

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

	interface_node = genbind_node_find(genbind_node_getnode(binding_node),
				       NULL,
				       genbind_cmp_node_type,
				       (void *)GENBIND_NODE_TYPE_TYPE_INTERFACE);

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

	nb = calloc(1, sizeof(struct binding));

	nb->name = genbind_node_gettext(ident_node);
	nb->interface = genbind_node_gettext(interface_node);

	return nb;
}

int jsapi_libdom_output(char *outfilename, struct genbind_node *genbind_ast)
{
        FILE *outfile = NULL;
	struct webidl_node *webidl_ast = NULL;
	int res;
	struct binding *binding;

	/* walk ast and load any web IDL files required */
	res = read_webidl(genbind_ast, &webidl_ast);
	if (res != 0) {
		fprintf(stderr, "Error reading Web IDL files\n");
		return 5;
	}

	/* get general binding information used in output */
	binding = binding_new(genbind_ast);

        /* open output file */
	if (outfilename == NULL) {
		outfile = stdout;
	} else {
		outfile = fopen(outfilename, "w");
	}

	if (!outfile) {
		fprintf(stderr, "Error opening output %s: %s\n",
			outfilename, 
			strerror(errno));
		return 4;
	}

	output_header_comments(outfile, genbind_ast);

	output_preamble(outfile, genbind_ast);

	output_function_spec(outfile, genbind_ast, binding);

	//output_property_spec(outfile, genbind_ast);

	fclose(outfile);

	return 0;
}