summaryrefslogtreecommitdiff
path: root/src/duk-libdom-common.c
blob: fb97fe38032c88efb87946dae01b7051d05a6908 (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
/* duktape binding generation implementation
 *
 * This file is part of nsgenbind.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <ctype.h>

#include "options.h"
#include "utils.h"
#include "nsgenbind-ast.h"
#include "webidl-ast.h"
#include "ir.h"
#include "duk-libdom.h"

#define NSGENBIND_PREFACE                                               \
    "/* Generated by nsgenbind\n"                                       \
    " *\n"                                                              \
    " * nsgenbind is published under the MIT Licence.\n"                \
    " * nsgenbind is similar to a compiler is a purely transformative tool which\n" \
    " * explicitly makes no copyright claim on this generated output\n" \
    " */"

/* exported interface documented in duk-libdom.h */
int output_tool_preface(FILE* outf)
{
        fprintf(outf, "%s\n", NSGENBIND_PREFACE);

        return 0;
}

/* exported interface documented in duk-libdom.h */
int output_cdata(FILE* outf,
                 struct genbind_node *node,
                 enum genbind_node_type nodetype)
{
        char *cdata;
        int res = 0;

        cdata = genbind_node_gettext(
                genbind_node_find_type(
                        genbind_node_getnode(node),
                        NULL, nodetype));
        if (cdata != NULL) {
                fprintf(outf, "%s", cdata);
                res = 1;
        }
        return res;
}

/* exported interface documented in duk-libdom.h */
int output_ccode(FILE* outf, struct genbind_node *node)
{
        int *line;
        char *filename;

        line = genbind_node_getint(
                genbind_node_find_type(
                        genbind_node_getnode(node),
                        NULL, GENBIND_NODE_TYPE_LINE));

        filename = genbind_node_gettext(
                genbind_node_find_type(
                        genbind_node_getnode(node),
                        NULL, GENBIND_NODE_TYPE_FILE));

        if ((line != NULL) && (filename != NULL)) {
                fprintf(outf, "#line %d \"%s\"\n", *line, filename);
        }

        return output_cdata(outf, node, GENBIND_NODE_TYPE_CDATA);
}

/* exported interface documented in duk-libdom.h */
int output_tool_prologue(FILE* outf)
{
        char *fpath;

        fpath = genb_fpath("binding.h");
        fprintf(outf, "\n#include \"%s\"\n", fpath);
        free(fpath);

        fpath = genb_fpath("private.h");
        fprintf(outf, "#include \"%s\"\n", fpath);
        free(fpath);

        fpath = genb_fpath("prototype.h");
        fprintf(outf, "#include \"%s\"\n", fpath);
        free(fpath);

        return 0;
}


/* exported interface documented in duk-libdom.h */
int output_ctype(FILE *outf, struct genbind_node *node, bool identifier)
{
        const char *type_cdata = NULL;
        struct genbind_node *typename_node;

        typename_node = genbind_node_find_type(genbind_node_getnode(node),
                                               NULL,
                                               GENBIND_NODE_TYPE_NAME);
        while (typename_node != NULL) {
                type_cdata = genbind_node_gettext(typename_node);

                fprintf(outf, "%s", type_cdata);

                typename_node = genbind_node_find_type(
                        genbind_node_getnode(node),
                        typename_node,
                        GENBIND_NODE_TYPE_NAME);

                /* separate all but the last entry with spaces */
                if (typename_node != NULL) {
                        fputc(' ', outf);
                }
        }

        if (identifier) {
            if ((type_cdata != NULL) &&
                (type_cdata[0] != '*') &&
                (type_cdata[0] != ' ')) {
                fputc(' ', outf);
            }

            output_cdata(outf, node, GENBIND_NODE_TYPE_IDENT);
        }

        return 0;
}

/* exported interface documented in duk-libdom.h */
int output_method_cdata(FILE* outf,
                        struct genbind_node *node,
                        enum genbind_method_type sel_method_type)
{
        struct genbind_node *method;

        method = genbind_node_find_type(genbind_node_getnode(node),
                                        NULL,
                                        GENBIND_NODE_TYPE_METHOD);

        while (method != NULL) {
                enum genbind_method_type *method_type;

                method_type = (enum genbind_method_type *)genbind_node_getint(
                        genbind_node_find_type(
                                genbind_node_getnode(method),
                                NULL,
                                GENBIND_NODE_TYPE_METHOD_TYPE));
                if ((method_type != NULL) &&
                    (*method_type == sel_method_type)) {
                        output_cdata(outf, method, GENBIND_NODE_TYPE_CDATA);
                }

                method = genbind_node_find_type(genbind_node_getnode(node),
                                                method,
                                                GENBIND_NODE_TYPE_METHOD);
        }

        return 0;
}