summaryrefslogtreecommitdiff
path: root/src/duk-libdom-dictionary.c
blob: 09e43797e2e2109d59110eb5d5858bbf36f571f1 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* duktape binding generation implementation
 *
 * 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 <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"

/** prefix for all generated functions */
#define DLPFX "dukky"

#define MAGICPFX "\\xFF\\xFFNETSURF_DUKTAPE_"


/**
 * get default value as a string
 */
static int
get_member_default_str(struct ir_entry *dictionarye,
                       struct ir_operation_argument_entry *membere,
                       enum webidl_type member_type,
                       char **defl_out)
{
        struct webidl_node *lit_node;
        enum webidl_node_type lit_type;
        int *lit_int;
        float *lit_flt;

        lit_node = webidl_node_getnode(
                webidl_node_find_type(
                        webidl_node_getnode(membere->node),
                        NULL,
                        WEBIDL_NODE_TYPE_OPTIONAL));
        if (lit_node == NULL) {
                *defl_out = NULL;
                return 0;
        }

        lit_type = webidl_node_gettype(lit_node);

        switch (lit_type) {

        case WEBIDL_NODE_TYPE_LITERAL_BOOL:
                if (member_type != WEBIDL_TYPE_BOOL) {
                        fprintf(stderr,
                                "Dictionary %s:%s literal boolean type mismatch\n",
                                dictionarye->name,
                                membere->name);
                        return -1;
                }
                lit_int = webidl_node_getint(lit_node);
                if (*lit_int == 0) {
                        *defl_out = strdup("false");
                } else {
                        *defl_out = strdup("true");
                }
                break;

        case WEBIDL_NODE_TYPE_LITERAL_NULL:
                *defl_out = strdup("NULL");
                break;

        case WEBIDL_NODE_TYPE_LITERAL_STRING:
                *defl_out = strdup(webidl_node_gettext(lit_node));
                break;

        case WEBIDL_NODE_TYPE_LITERAL_INT:
                lit_int = webidl_node_getint(lit_node);
                *defl_out = malloc(128);
                sprintf(*defl_out, "%d", *lit_int);
                break;

        case WEBIDL_NODE_TYPE_LITERAL_FLOAT:
                lit_flt = webidl_node_getfloat(lit_node);
                *defl_out = malloc(128);
                sprintf(*defl_out, "%f", *lit_flt);
                break;

        default:
                *defl_out = NULL;
                break;
        }

        return 0;
}

/**
 * generate a single class method for an interface operation
 */
static int
output_member_acessor(FILE* outf,
                      struct ir_entry *dictionarye,
                      struct ir_operation_argument_entry *membere)
{
        struct webidl_node *type_node;
        enum webidl_type *argument_type;
        char *defl; /* default for member */
        int res;

        type_node = webidl_node_find_type(
                webidl_node_getnode(membere->node),
                NULL,
                WEBIDL_NODE_TYPE_TYPE);

        if (type_node == NULL) {
                fprintf(stderr, "%s:%s has no type\n",
                        dictionarye->name,
                        membere->name);
                return -1;
        }

        argument_type = (enum webidl_type *)webidl_node_getint(
                webidl_node_find_type(
                        webidl_node_getnode(type_node),
                        NULL,
                        WEBIDL_NODE_TYPE_TYPE_BASE));

        if (argument_type == NULL) {
                fprintf(stderr,
                        "%s:%s has no type base\n",
                        dictionarye->name,
                        membere->name);
                return -1;
        }

        /* get default text */
        res = get_member_default_str(dictionarye, membere, *argument_type, &defl);
        if (res != 0) {
                return res;
        }

        switch (*argument_type) {

        case WEBIDL_TYPE_STRING:
                fprintf(outf,
                        "const char *\n"
                        "%s_%s_get_%s(duk_context *ctx, duk_idx_t idx)\n"
                        "{\n",
                        DLPFX, dictionarye->class_name, membere->name);

                if (defl == NULL) {
                        fprintf(outf,
                                "\tconst char *ret = NULL; /* No default */\n");
                } else {
                        fprintf(outf,
                                "\tconst char *ret = \"%s\"; /* Default value of %s */\n",
                                defl, membere->name);
                }

                fprintf(outf,
                        "\t/* ... obj@idx ... */\n"
                        "\tduk_get_prop_string(ctx, idx, \"%s\");\n"
                        "\t/* ... obj@idx ... value/undefined */\n"
                        "\tif (!duk_is_undefined(ctx, -1)) {\n"
                        "\t\t/* Note, this throws a duk_error if it's not a string */\n"
                        "\t\tret = duk_require_string(ctx, -1);\n"
                        "\t}\n"
                        "\tduk_pop(ctx);\n"
                        "\treturn ret;\n"
                        "}\n\n",
                        membere->name);

                break;

        case WEBIDL_TYPE_BOOL:
                fprintf(outf,
                        "duk_bool_t\n"
                        "%s_%s_get_%s(duk_context *ctx, duk_idx_t idx)\n"
                        "{\n",
                        DLPFX, dictionarye->class_name, membere->name);

                if (defl == NULL) {
                        fprintf(outf,
                                "\tduk_bool_t ret = false; /* No default */\n");
                } else {
                fprintf(outf,
                        "\tduk_bool_t ret = %s; /* Default value of %s */\n",
                        defl, membere->name);
                }

                fprintf(outf,
                        "\t/* ... obj@idx ... */\n"
                        "\tduk_get_prop_string(ctx, idx, \"%s\");\n"
                        "\t/* ... obj@idx ... value/undefined */\n"
                        "\tif (!duk_is_undefined(ctx, -1)) {\n"
                        "\t\t/* Note, this throws a duk_error if it's not a boolean */\n"
                        "\t\tret = duk_require_boolean(ctx, -1);\n"
                        "\t}\n"
                        "\tduk_pop(ctx);\n"
                        "\treturn ret;\n"
                        "}\n\n",
                        membere->name);

                break;

        case WEBIDL_TYPE_SHORT:
        case WEBIDL_TYPE_LONG:
        case WEBIDL_TYPE_LONGLONG:
                fprintf(outf,
                        "duk_int_t\n"
                        "%s_%s_get_%s(duk_context *ctx, duk_idx_t idx)\n"
                        "{\n",
                        DLPFX, dictionarye->class_name, membere->name);

                if (defl == NULL) {
                        fprintf(outf, "\tduk_int_t ret = 0; /* No default */\n");
                } else {
                        fprintf(outf,
                                "\tduk_int_t ret = %s; /* Default value of %s */\n",
                                defl, membere->name);
                }

                fprintf(outf,
                        "\t/* ... obj@idx ... */\n"
                        "\tduk_get_prop_string(ctx, idx, \"%s\");\n"
                        "\t/* ... obj@idx ... value/undefined */\n"
                        "\tif (!duk_is_undefined(ctx, -1)) {\n"
                        "\t\t/* Note, this throws a duk_error if it's not a int */\n"
                        "\t\tret = duk_require_int(ctx, -1);\n"
                        "\t}\n"
                        "\tduk_pop(ctx);\n"
                        "\treturn ret;\n"
                        "}\n\n",
                        membere->name);
                break;

        case WEBIDL_TYPE_FLOAT:
        case WEBIDL_TYPE_DOUBLE:
                fprintf(outf,
                        "duk_double_t\n"
                        "%s_%s_get_%s(duk_context *ctx, duk_idx_t idx)\n",
                        DLPFX, dictionarye->class_name, membere->name);

                fprintf(outf,
                        "{\n"
                        "\tduk_double_t ret = %s; /* Default value of %s */\n"
                        "\t/* ... obj@idx ... */\n"
                        "\tduk_get_prop_string(ctx, idx, \"%s\");\n",
                        defl, membere->name, membere->name);

                fprintf(outf,
                        "\t/* ... obj@idx ... value/undefined */\n"
                        "\tif (!duk_is_undefined(ctx, -1)) {\n"
                        "\t\t/* Note, this throws a duk_error if it's not a number */\n"
                        "\t\tret = duk_require_number(ctx, -1);\n"
                        "\t}\n"
                        "\tduk_pop(ctx);\n"
                        "\treturn ret;\n"
                        "}\n\n");
                break;

        default:
                WARN(WARNING_UNIMPLEMENTED,
                        "Dictionary %s:%s unhandled type (%d)",
                        dictionarye->name,
                        membere->name,
                        *argument_type);
                fprintf(outf,
                        "/* Dictionary %s:%s unhandled type (%d) */\n\n",
                        dictionarye->name,
                        membere->name,
                        *argument_type);
        }

        if (defl != NULL) {
                free(defl);
        }

        return 0;
}

static int
output_member_acessors(FILE* outf, struct ir_entry *dictionarye)
{
        int memberc;
        int res = 0;

        for (memberc = 0;
             memberc < dictionarye->u.dictionary.memberc;
             memberc++) {
                res = output_member_acessor(
                        outf,
                        dictionarye,
                        dictionarye->u.dictionary.memberv + memberc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}


/* exported function documented in duk-libdom.h */
int output_dictionary(struct ir *ir, struct ir_entry *dictionarye)
{
        FILE *ifacef;
        int res = 0;

        /* open output file */
        ifacef = genb_fopen_tmp(dictionarye->filename);
        if (ifacef == NULL) {
                return -1;
        }

        /* tool preface */
        output_tool_preface(ifacef);

        /* binding preface */
        output_method_cdata(ifacef,
                            ir->binding_node,
                            GENBIND_METHOD_TYPE_PREFACE);

        /* class preface */
        output_method_cdata(ifacef,
                            dictionarye->class,
                            GENBIND_METHOD_TYPE_PREFACE);

        /* tool prologue */
        output_tool_prologue(ifacef);

        /* binding prologue */
        output_method_cdata(ifacef,
                            ir->binding_node,
                            GENBIND_METHOD_TYPE_PROLOGUE);

        /* class prologue */
        output_method_cdata(ifacef,
                            dictionarye->class,
                            GENBIND_METHOD_TYPE_PROLOGUE);

        fprintf(ifacef, "\n");


        res = output_member_acessors(ifacef, dictionarye);
        if (res != 0) {
                goto op_error;
        }

        fprintf(ifacef, "\n");

        /* class epilogue */
        output_method_cdata(ifacef,
                            dictionarye->class,
                            GENBIND_METHOD_TYPE_EPILOGUE);

        /* binding epilogue */
        output_method_cdata(ifacef,
                            ir->binding_node,
                            GENBIND_METHOD_TYPE_EPILOGUE);

        /* class postface */
        output_method_cdata(ifacef,
                            dictionarye->class,
                            GENBIND_METHOD_TYPE_POSTFACE);

        /* binding postface */
        output_method_cdata(ifacef,
                            ir->binding_node,
                            GENBIND_METHOD_TYPE_POSTFACE);

op_error:
        genb_fclose_tmp(ifacef, dictionarye->filename);

        return res;
}

/**
 * generate a single class method declaration for an interface operation
 */
static int
output_member_declaration(FILE* outf,
                      struct ir_entry *dictionarye,
                      struct ir_operation_argument_entry *membere)
{
        struct webidl_node *type_node;
        enum webidl_type *argument_type;

        type_node = webidl_node_find_type(
                webidl_node_getnode(membere->node),
                NULL,
                WEBIDL_NODE_TYPE_TYPE);

        if (type_node == NULL) {
                fprintf(stderr, "%s:%s has no type\n",
                        dictionarye->name,
                        membere->name);
                return -1;
        }

        argument_type = (enum webidl_type *)webidl_node_getint(
                webidl_node_find_type(
                        webidl_node_getnode(type_node),
                        NULL,
                        WEBIDL_NODE_TYPE_TYPE_BASE));

        if (argument_type == NULL) {
                fprintf(stderr,
                        "%s:%s has no type base\n",
                        dictionarye->name,
                        membere->name);
                return -1;
        }


        switch (*argument_type) {

        case WEBIDL_TYPE_STRING:
                fprintf(outf,
                        "const char *%s_%s_get_%s(duk_context *ctx, duk_idx_t idx);\n",
                        DLPFX, dictionarye->class_name, membere->name);
                break;

        case WEBIDL_TYPE_BOOL:
                fprintf(outf,
                        "duk_bool_t %s_%s_get_%s(duk_context *ctx, duk_idx_t idx);\n",
                        DLPFX, dictionarye->class_name, membere->name);
                break;

        case WEBIDL_TYPE_SHORT:
        case WEBIDL_TYPE_LONG:
        case WEBIDL_TYPE_LONGLONG:
                fprintf(outf,
                        "duk_int_t %s_%s_get_%s(duk_context *ctx, duk_idx_t idx);\n",
                        DLPFX, dictionarye->class_name, membere->name);
                break;

        case WEBIDL_TYPE_FLOAT:
        case WEBIDL_TYPE_DOUBLE:
                fprintf(outf,
                        "duk_double_t %s_%s_get_%s(duk_context *ctx, duk_idx_t idx);\n",
                        DLPFX, dictionarye->class_name, membere->name);
                break;

        default:
                fprintf(outf,
                        "/* Dictionary %s:%s unhandled type (%d) */\n",
                        dictionarye->name,
                        membere->name,
                        *argument_type);
        }

        return 0;
}

/* exported function documented in duk-libdom.h */
int output_dictionary_declaration(FILE* outf, struct ir_entry *dictionarye)
{
        int memberc;
        int res = 0;

        for (memberc = 0;
             memberc < dictionarye->u.dictionary.memberc;
             memberc++) {
                res = output_member_declaration(
                        outf,
                        dictionarye,
                        dictionarye->u.dictionary.memberv + memberc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}