nsgenbind
Loading...
Searching...
No Matches
duk-libdom-common.c
Go to the documentation of this file.
1/* duktape binding generation implementation
2 *
3 * This file is part of nsgenbind.
4 * Licensed under the MIT License,
5 * http://www.opensource.org/licenses/mit-license.php
6 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
7 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdbool.h>
12#include <string.h>
13#include <unistd.h>
14#include <getopt.h>
15#include <errno.h>
16#include <ctype.h>
17
18#include "options.h"
19#include "utils.h"
20#include "nsgenbind-ast.h"
21#include "webidl-ast.h"
22#include "ir.h"
23#include "output.h"
24#include "duk-libdom.h"
25
26#define NSGENBIND_PREFACE \
27 "/* Generated by nsgenbind\n" \
28 " *\n" \
29 " * nsgenbind is published under the MIT Licence.\n" \
30 " * nsgenbind is similar to a compiler is a purely transformative tool which\n" \
31 " * explicitly makes no copyright claim on this generated output\n" \
32 " */\n"
33
34/* exported interface documented in duk-libdom.h */
35int output_tool_preface(struct opctx *outc)
36{
37 outputf(outc, "%s", NSGENBIND_PREFACE);
38
39 return 0;
40}
41
42/* exported interface documented in duk-libdom.h */
43int output_cdata(struct opctx *outc,
44 struct genbind_node *node,
45 enum genbind_node_type nodetype)
46{
47 char *cdata;
48 int res = 0;
49
53 NULL, nodetype));
54 if (cdata != NULL) {
55 outputf(outc, "%s", cdata);
56 res = 1;
57 }
58 return res;
59}
60
61/* exported interface documented in duk-libdom.h */
62int output_ccode(struct opctx *outc, struct genbind_node *node)
63{
64 int res;
65 int *line;
66 char *filename;
67
72
77
78 if ((line != NULL) && (filename != NULL)) {
79 outputf(outc, "#line %d \"%s\"\n", (*line) + 1, filename);
80 res = output_cdata(outc, node, GENBIND_NODE_TYPE_CDATA);
81 output_line(outc);
82 } else {
83 res = output_cdata(outc, node, GENBIND_NODE_TYPE_CDATA);
84 }
85
86 return res;
87}
88
89/* exported interface documented in duk-libdom.h */
90int output_tool_prologue(struct opctx *outc)
91{
92 char *fpath;
93
94 fpath = genb_fpath("binding.h");
95 outputf(outc, "\n#include \"%s\"\n", fpath);
96 free(fpath);
97
98 fpath = genb_fpath("private.h");
99 outputf(outc, "#include \"%s\"\n", fpath);
100 free(fpath);
101
102 fpath = genb_fpath("prototype.h");
103 outputf(outc, "#include \"%s\"\n", fpath);
104 free(fpath);
105
106 return 0;
107}
108
109
110/* exported interface documented in duk-libdom.h */
111int output_ctype(struct opctx *outc, struct genbind_node *node, bool identifier)
112{
113 const char *type_cdata = NULL;
114 struct genbind_node *typename_node;
115
117 NULL,
119 while (typename_node != NULL) {
120 type_cdata = genbind_node_gettext(typename_node);
121
122 outputf(outc, "%s", type_cdata);
123
124 typename_node = genbind_node_find_type(
126 typename_node,
128
129 /* separate all but the last entry with spaces */
130 if (typename_node != NULL) {
131 outputc(outc, ' ');
132 }
133 }
134
135 if (identifier) {
136 if ((type_cdata != NULL) &&
137 (type_cdata[0] != '*') &&
138 (type_cdata[0] != ' ')) {
139 outputc(outc, ' ');
140 }
141
143 }
144
145 return 0;
146}
147
148/* exported interface documented in duk-libdom.h */
149int output_method_cdata(struct opctx *outc,
150 struct genbind_node *node,
151 enum genbind_method_type sel_method_type)
152{
153 struct genbind_node *method;
154
156 NULL,
158
159 while (method != NULL) {
160 enum genbind_method_type *method_type;
161
162 method_type = (enum genbind_method_type *)genbind_node_getint(
164 genbind_node_getnode(method),
165 NULL,
167 if ((method_type != NULL) &&
168 (*method_type == sel_method_type)) {
169 output_ccode(outc, method);
170 }
171
173 method,
175 }
176
177 return 0;
178}
179
180/* exported interface documented in duk-libdom.h */
181char *gen_idl2c_name(const char *idlname)
182{
183 const char *inc;
184 char *outc;
185 char *name;
186 int waslower;
187
188 /* enpty strings are a bad idea */
189 if ((idlname == NULL) || (idlname[0] == 0)) {
190 return NULL;
191 }
192
193 /* allocate result buffer as twice the input length as thats the
194 * absolute worst case.
195 */
196 name = calloc(2, strlen(idlname));
197
198 outc = name;
199 inc = idlname;
200 waslower = 1;
201
202 /* first character handled separately as inserting a leading underscore
203 * is undesirable
204 */
205 *outc++ = tolower(*inc++);
206
207 /* copy input to output */
208 while (*inc != 0) {
209 /* ugly hack as html IDL is always prefixed uppercase and needs
210 * an underscore there
211 */
212 if ((inc == (idlname + 4)) &&
213 (idlname[0] == 'H') &&
214 (idlname[1] == 'T') &&
215 (idlname[2] == 'M') &&
216 (idlname[3] == 'L') &&
217 (islower(inc[1]) == 0)) {
218 *outc++ = '_';
219 }
220 if (islower(*inc) != 0) {
221 if (waslower == 0) {
222 /* high to lower case transition */
223 if (((outc - name) <= 3) ||
224 (*(outc - 3) != '_')) {
225 *outc = *(outc - 1);
226 *(outc - 1) = '_';
227 outc++;
228 }
229 }
230 waslower = 1;
231 } else {
232 waslower = 0;
233 }
234 *outc++ = tolower(*inc++);
235 }
236 return name;
237}
int output_method_cdata(struct opctx *outc, struct genbind_node *node, enum genbind_method_type sel_method_type)
int output_cdata(struct opctx *outc, struct genbind_node *node, enum genbind_node_type nodetype)
#define NSGENBIND_PREFACE
int output_tool_prologue(struct opctx *outc)
int output_ccode(struct opctx *outc, struct genbind_node *node)
int output_tool_preface(struct opctx *outc)
int output_ctype(struct opctx *outc, struct genbind_node *node, bool identifier)
char * gen_idl2c_name(const char *idlname)
DOMString filename
Definition html.idl:1561
struct genbind_node * genbind_node_find_type(struct genbind_node *node, struct genbind_node *prev, enum genbind_node_type type)
int * genbind_node_getint(struct genbind_node *node)
struct genbind_node * genbind_node_getnode(struct genbind_node *node)
char * genbind_node_gettext(struct genbind_node *node)
genbind_method_type
genbind_node_type
@ GENBIND_NODE_TYPE_METHOD_TYPE
@ GENBIND_NODE_TYPE_NAME
@ GENBIND_NODE_TYPE_METHOD
@ GENBIND_NODE_TYPE_IDENT
@ GENBIND_NODE_TYPE_CDATA
@ GENBIND_NODE_TYPE_LINE
@ GENBIND_NODE_TYPE_FILE
void free(void *)
int outputc(struct opctx *opctx, int c)
Definition output.c:88
int output_line(struct opctx *opctx)
Definition output.c:98
int outputf(struct opctx *opctx, const char *fmt,...)
Definition output.c:66
struct genbind_node * node
Definition output.c:17
char * genb_fpath(const char *fname)
Definition utils.c:21