summaryrefslogtreecommitdiff
path: root/src/duk-libdom.c
blob: bc27dfce192eeaacfd63f3bf02f558e01659ac20 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
/* 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 "interface-map.h"
#include "duk-libdom.h"

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

#define MAGICPFX "\\xFF\\xFFNETSURF_DUKTAPE_"

#define NSGENBIND_PREAMBLE \
"/* 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"\
" */"

/**
 * Output code to create a private structure
 *
 */
static int output_create_private(FILE* outf, char *class_name)
{
        fprintf(outf, "\t/* create private data and attach to instance */\n");
        fprintf(outf, "\t%s_private_t *priv = calloc(1, sizeof(*priv));\n",
                class_name);
        fprintf(outf, "\tif (priv == NULL) return 0;\n");
        fprintf(outf, "\tduk_push_pointer(ctx, priv);\n");
        fprintf(outf,
                "\tduk_put_prop_string(ctx, 0, \"%sPRIVATE\");\n\n",
                MAGICPFX);

        return 0;
}

/**
 * generate code that gets a private pointer
 */
static int output_safe_get_private(FILE* outf, char *class_name, int idx)
{
        fprintf(outf, "\t%s_private_t *priv;\n", class_name);
        fprintf(outf, "\tduk_get_prop_string(ctx, %d, \"%sPRIVATE\");\n",
                idx, MAGICPFX);
        fprintf(outf, "\tpriv = duk_get_pointer(ctx, -1);\n");
        fprintf(outf, "\tduk_pop(ctx);\n");
        fprintf(outf, "\tif (priv == NULL) return 0;\n\n");
        return 0;
}


/**
 * generate a duktape prototype name
 */
static char *get_prototype_name(const char *interface_name)
{
        char *proto_name;
        int pnamelen;
        int pfxlen;

        /* duplicate the interface name in upper case */
        pfxlen = SLEN(MAGICPFX) + SLEN("PROTOTYPE_");
        pnamelen = strlen(interface_name) + 1;

        proto_name = malloc(pnamelen + pfxlen);
        snprintf(proto_name, pnamelen + pfxlen, "%sPROTOTYPE_%s", MAGICPFX, interface_name);
        for (pnamelen-- ; pnamelen >= 0; pnamelen--) {
                proto_name[pnamelen + pfxlen] = toupper(interface_name[pnamelen]);
        }
        return proto_name;
}

/**
 * generate code that gets a prototype by name
 */
static int output_get_prototype(FILE* outf, const char *interface_name)
{
        char *proto_name;

        proto_name = get_prototype_name(interface_name);

        fprintf(outf, "\t/* get prototype */\n");
        fprintf(outf,
                "\tduk_get_global_string(ctx, \"%sPROTOTYPES\");\n", MAGICPFX);
        fprintf(outf,
                "\tduk_get_prop_string(ctx, -1, \"%s\");\n", proto_name);
        fprintf(outf, "\tduk_replace(ctx, -2);\n");

        free(proto_name);

        return 0;
}

/**
 * generate code that sets a destructor in a prototype
 */
static int output_set_destructor(FILE* outf, char *class_name, int idx)
{
        fprintf(outf, "\t/* Set the destructor */\n");
        fprintf(outf, "\tduk_dup(ctx, %d);\n", idx);
        fprintf(outf, "\tduk_push_c_function(ctx, %s_%s___destructor, 1);\n",
                DLPFX, class_name);
        fprintf(outf, "\tduk_set_finalizer(ctx, -2);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");

        return 0;
}

/**
 * generate code that sets a constructor in a prototype
 */
static int
output_set_constructor(FILE* outf, char *class_name, int idx, int argc)
{
        fprintf(outf, "\t/* Set the constructor */\n");
        fprintf(outf, "\tduk_dup(ctx, %d);\n", idx);
        fprintf(outf, "\tduk_push_c_function(ctx, %s_%s___constructor, %d);\n",
                DLPFX, class_name, 1 + argc);
        fprintf(outf, "\tduk_put_prop_string(ctx, -2, INIT_MAGIC);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");

        return 0;
}

static int
output_dump_stack(FILE* outf)
{
        if (options->dbglog) {
                /* dump stack */
                fprintf(outf, "\tduk_push_context_dump(ctx);\n");
                fprintf(outf, "\tLOG(\"Stack: %%s\", duk_to_string(ctx, -1));\n");
                fprintf(outf, "\tduk_pop(ctx);\n");
        }
        return 0;
}

/**
 * generate code that adds a method in a prototype
 */
static int
output_add_method(FILE* outf,
                  const char *class_name,
                  const char *method,
                  int argc)
{
        fprintf(outf, "\t/* Add a method */\n");
        fprintf(outf, "\tduk_dup(ctx, 0);\n");
        fprintf(outf, "\tduk_push_string(ctx, \"%s\");\n", method);
        fprintf(outf, "\tduk_push_c_function(ctx, %s_%s_%s, ",
                DLPFX, class_name, method);
        if (argc == -1) {
                fprintf(outf, "DUK_VARARGS);\n");
        } else {
                fprintf(outf, "%d);\n", argc);
        }
        output_dump_stack(outf);
        fprintf(outf, "\tduk_def_prop(ctx, -3,\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_VALUE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_WRITABLE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_CONFIGURABLE);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");

        return 0;
}

/**
 * Generate source to populate a read/write property on a prototype
 */
static int
output_populate_rw_property(FILE* outf, const char *class_name, const char *property)
{
        fprintf(outf, "\t/* Add read/write property */\n");
        fprintf(outf, "\tduk_dup(ctx, 0);\n");
        fprintf(outf, "\tduk_push_string(ctx, \"%s\");\n", property);
        fprintf(outf,
                "\tduk_push_c_function(ctx, %s_%s_%s_getter, 0);\n",
                DLPFX, class_name, property);
        fprintf(outf,
                "\tduk_push_c_function(ctx, %s_%s_%s_setter, 1);\n",
                DLPFX, class_name, property);
        fprintf(outf, "\tduk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_SETTER |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_CONFIGURABLE);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");

        return 0;
}

/**
 * Generate source to populate a readonly property on a prototype
 */
static int
output_populate_ro_property(FILE* outf, const char *class_name, const char *property)
{
        fprintf(outf, "\t/* Add readonly property */\n");
        fprintf(outf, "\tduk_dup(ctx, 0);\n");
        fprintf(outf, "\tduk_push_string(ctx, \"%s\");\n", property);
        fprintf(outf,
                "\tduk_push_c_function(ctx, %s_%s_%s_getter, 0);\n",
                DLPFX, class_name, property);
        fprintf(outf,
                "\tduk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_CONFIGURABLE);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");

        return 0;
}

/**
 * Generate source to add a constant int value on a prototype
 */
static int
output_prototype_constant_int(FILE *outf, const char *constant_name, int value)
{
        fprintf(outf, "\tduk_dup(ctx, 0);\n");
        fprintf(outf, "\tduk_push_string(ctx, \"%s\");\n", constant_name);
        fprintf(outf, "\tduk_push_int(ctx, %d);\n", value);
        fprintf(outf, "\tduk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_HAVE_ENUMERABLE |\n");
        fprintf(outf, "\t             DUK_DEFPROP_ENUMERABLE | DUK_DEFPROP_HAVE_CONFIGURABLE);\n");
        fprintf(outf, "\tduk_pop(ctx);\n\n");
        return 0;
}

/**
 * generate code that gets a private pointer for a method
 */
static int
output_get_method_private(FILE* outf, char *class_name)
{
        fprintf(outf, "\t/* Get private data for method */\n");
        fprintf(outf, "\t%s_private_t *priv = NULL;\n", class_name);
        fprintf(outf, "\tduk_push_this(ctx);\n");
        fprintf(outf, "\tduk_get_prop_string(ctx, -1, PRIVATE_MAGIC);\n");
        fprintf(outf, "\tpriv = duk_get_pointer(ctx, -1);\n");
        fprintf(outf, "\tduk_pop_2(ctx);\n");
        fprintf(outf, "\tif (priv == NULL) return 0; /* can do? No can do. */\n\n");
        return 0;
}

/**
 * generate preface block for nsgenbind
 */
static int output_tool_preface(FILE* outf)
{
        fprintf(outf, "%s\n", NSGENBIND_PREAMBLE);

        return 0;
}

/**
 * Generate a C class name for the interface.
 *
 * The IDL interface names are camelcase and not similar to libdom naming so it
 *  is necessary to convert them to a libdom compatible class name. This
 *  implementation is simple ASCII capable only and cannot cope with multibyte
 *  codepoints.
 *
 * The algorithm is:
 *  - copy characters to output lowering their case
 *  - if the previous character in the input name was uppercase and the current
 *    one is lowercase insert an underscore before the *previous* character.
 */
static char *gen_class_name(struct interface_map_entry *interfacee)
{
        const char *inc;
        char *outc;
        char *name;
        int wasupper;

        /* enpty strings are a bad idea */
        if ((interfacee->name == NULL) || (interfacee->name[0] == 0)) {
                return NULL;
        }

        /* allocate result buffer as twice the input length as thats the
         * absolute worst case.
         */
        name = calloc(2, strlen(interfacee->name));

        outc = name;
        inc = interfacee->name;
        wasupper = 0;

        /* first character handled separately as inserting a leading underscore
         * is undesirable
         */
        *outc++ = tolower(*inc++);
        /* copy input to output */
        while (*inc != 0) {
                /* ugly hack as html IDL is always prefixed uppercase and needs
                 * an underscore there
                 */
                if ((inc == (interfacee->name + 4)) &&
                    (interfacee->name[0] == 'H') &&
                    (interfacee->name[1] == 'T') &&
                    (interfacee->name[2] == 'M') &&
                    (interfacee->name[3] == 'L') &&
                    (islower(inc[1]) == 0)) {
                        *outc++ = '_';
                }
                if ((islower(*inc) != 0) && (wasupper != 0)) {
                        *outc = *(outc - 1);
                        *(outc - 1) = '_';
                        outc++;
                        wasupper = 0;
                } else {
                        wasupper = isupper(*inc);
                }
                *outc++ = tolower(*inc++);
        }
        return name;
}

/**
 * output character data of node of given type.
 *
 * used for pre/pro/epi/post sections
 */
static int
output_cdata(FILE* outf,
             struct genbind_node *node,
             enum genbind_node_type nodetype)
{
        char *cdata;
        cdata = genbind_node_gettext(
                genbind_node_find_type(
                        genbind_node_getnode(node),
                        NULL, nodetype));
        if (cdata != NULL) {
                fprintf(outf, "%s", cdata);
        }
        return 0;
}

static FILE *open_header(struct interface_map *interface_map, const char *name)
{
        FILE *hdrf;
        char *fname;
        int fnamel;

        fnamel = strlen(name) + 4;
        fname = malloc(fnamel);
        snprintf(fname, fnamel, "%s.h", name);

        /* open output file */
        hdrf = genb_fopen(fname, "w");
        free(fname);
        if (hdrf == NULL) {
                return NULL;
        }

        /* binding preface */
        output_cdata(hdrf,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_PREFACE);

        /* tool preface */
        output_tool_preface(hdrf);

        /* header guard */
        fprintf(hdrf, "\n#ifndef %s_%s_h\n", DLPFX, name);
        fprintf(hdrf, "#define %s_%s_h\n\n", DLPFX, name);

        return hdrf;
}

static int close_header(struct interface_map *interface_map, FILE *hdrf)
{
        fprintf(hdrf, "\n#endif\n");

        /* binding postface */
        output_cdata(hdrf,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_POSTFACE);

        fclose(hdrf);

        return 0;
}


/**
 * generate the interface constructor
 */
static int
output_interface_constructor(FILE* outf, struct interface_map_entry *interfacee)
{
        int init_argc;

        /* constructor definition */
        fprintf(outf,
                "static duk_ret_t %s_%s___constructor(duk_context *ctx)\n",
                DLPFX, interfacee->class_name);
        fprintf(outf,"{\n");

        output_create_private(outf, interfacee->class_name);

        /* generate call to initialisor */
        fprintf(outf,
                "\t%s_%s___init(ctx, priv",
                DLPFX, interfacee->class_name);
        for (init_argc = 1;
             init_argc <= interfacee->class_init_argc;
             init_argc++) {
                fprintf(outf, ", duk_get_pointer(ctx, %d)", init_argc);
        }
        fprintf(outf, ");\n");


        fprintf(outf, "\tduk_set_top(ctx, 1);\n");
        fprintf(outf, "\treturn 1;\n");

        fprintf(outf, "}\n\n");

        return 0;
}

/**
 * generate the interface destructor
 */
static int
output_interface_destructor(FILE* outf, struct interface_map_entry *interfacee)
{
        /* destructor definition */
        fprintf(outf,
                "static duk_ret_t %s_%s___destructor(duk_context *ctx)\n",
                DLPFX, interfacee->class_name);
        fprintf(outf,"{\n");

        output_safe_get_private(outf, interfacee->class_name, 0);

        /* generate call to finaliser */
        fprintf(outf,
                "\t%s_%s___fini(ctx, priv);\n",
                DLPFX, interfacee->class_name);

        fprintf(outf,"\tfree(priv);\n");
        fprintf(outf,"\treturn 0;\n");

        fprintf(outf, "}\n\n");

        return 0;
}

/**
 * generate an initialisor call to parent interface
 */
static int
output_interface_inherit_init(FILE* outf,
                              struct interface_map_entry *interfacee,
                              struct interface_map_entry *inherite)
{
        struct genbind_node *init_node;
        struct genbind_node *inh_init_node;
        struct genbind_node *param_node;
        struct genbind_node *inh_param_node;

        /* only need to call parent initialisor if there is one */
        if (inherite == NULL) {
                return 0;
        }

        /* find the initialisor method on the class (if any) */
        init_node = genbind_node_find_method(interfacee->class,
                                             NULL,
                                             GENBIND_METHOD_TYPE_INIT);


        inh_init_node = genbind_node_find_method(inherite->class,
                                                 NULL,
                                                 GENBIND_METHOD_TYPE_INIT);



        fprintf(outf, "\t%s_%s___init(ctx, &priv->parent",
                DLPFX, inherite->class_name);

        /* for each parameter in the parent find a matching named
         * parameter to pass and cast if necessary
         */

        inh_param_node = genbind_node_find_type(
                genbind_node_getnode(inh_init_node),
                NULL, GENBIND_NODE_TYPE_PARAMETER);
        while (inh_param_node != NULL) {
                char *param_name;
                param_name = genbind_node_gettext(
                        genbind_node_find_type(
                                genbind_node_getnode(inh_param_node),
                                NULL,
                                GENBIND_NODE_TYPE_IDENT));

                param_node = genbind_node_find_type_ident(
                        genbind_node_getnode(init_node),
                        NULL,
                        GENBIND_NODE_TYPE_PARAMETER,
                        param_name);
                if (param_node == NULL) {
                        fprintf(stderr, "class \"%s\" (interface %s) parent class \"%s\" (interface %s) initialisor requires a parameter \"%s\" with compatible identifier\n",
                                interfacee->class_name,
                                interfacee->name,
                                inherite->class_name,
                                inherite->name,
                                param_name);
                        return -1;
                } else {
                        char *param_type;
                        char *inh_param_type;

                        fprintf(outf, ", ");

                        /* cast the parameter if required */
                        param_type = genbind_node_gettext(
                                genbind_node_find_type(
                                        genbind_node_getnode(param_node),
                                        NULL,
                                        GENBIND_NODE_TYPE_TYPE));

                        inh_param_type = genbind_node_gettext(
                                genbind_node_find_type(
                                        genbind_node_getnode(inh_param_node),
                                        NULL,
                                        GENBIND_NODE_TYPE_TYPE));

                        if (strcmp(param_type, inh_param_type) != 0) {
                                fprintf(outf, "(%s)", inh_param_type);
                        }

                        /* output the parameter identifier */
                        output_cdata(outf, param_node, GENBIND_NODE_TYPE_IDENT);
                }

                inh_param_node = genbind_node_find_type(
                        genbind_node_getnode(inh_init_node),
                        inh_param_node, GENBIND_NODE_TYPE_METHOD);
        }

        fprintf(outf, ");\n");

        return 0;
}

static int
output_interface_init_declaration(FILE* outf,
                                  struct interface_map_entry *interfacee,
                                  struct genbind_node *init_node)
{
        struct genbind_node *param_node;

        fprintf(outf,
                "void %s_%s___init(duk_context *ctx, %s_private_t *priv",
                DLPFX, interfacee->class_name, interfacee->class_name);

        /* count the number of arguments on the initializer */
        interfacee->class_init_argc = 0;

        /* output the paramters on the method (if any) */
        param_node = genbind_node_find_type(
                genbind_node_getnode(init_node),
                NULL, GENBIND_NODE_TYPE_PARAMETER);
        while (param_node != NULL) {
                interfacee->class_init_argc++;
                fprintf(outf, ", ");
                output_cdata(outf, param_node, GENBIND_NODE_TYPE_TYPE);
                output_cdata(outf, param_node, GENBIND_NODE_TYPE_IDENT);

                param_node = genbind_node_find_type(
                        genbind_node_getnode(init_node),
                        param_node, GENBIND_NODE_TYPE_METHOD);
        }

        fprintf(outf,")");

        return 0;
}

static int
output_interface_init(FILE* outf,
                      struct interface_map_entry *interfacee,
                      struct interface_map_entry *inherite)
{
        struct genbind_node *init_node;
        int res;

        /* find the initialisor method on the class (if any) */
        init_node = genbind_node_find_method(interfacee->class,
                                             NULL,
                                             GENBIND_METHOD_TYPE_INIT);

        /* initialisor definition */
        output_interface_init_declaration(outf, interfacee, init_node);

        fprintf(outf,"\n{\n");

        /* if this interface inherits ensure we call its initialisor */
        res = output_interface_inherit_init(outf, interfacee, inherite);
        if (res != 0) {
                return res;
        }

        /* generate log statement */
        if (options->dbglog) {
                fprintf(outf,
                        "\tLOG(\"Initialise %%p (priv=%%p)\", duk_get_heapptr(ctx, 0), priv);\n" );
        }

        /* output the initaliser code from the binding */
        output_cdata(outf, init_node, GENBIND_NODE_TYPE_CDATA);

        fprintf(outf, "}\n\n");

        return 0;

}

static int
output_interface_fini(FILE* outf,
                      struct interface_map_entry *interfacee,
                      struct interface_map_entry *inherite)
{
        struct genbind_node *fini_node;

        /* find the finaliser method on the class (if any) */
        fini_node = genbind_node_find_method(interfacee->class,
                                             NULL,
                                             GENBIND_METHOD_TYPE_FINI);

        /* finaliser definition */
        fprintf(outf,
                "void %s_%s___fini(duk_context *ctx, %s_private_t *priv)\n",
                DLPFX, interfacee->class_name, interfacee->class_name);
        fprintf(outf,"{\n");

        /* generate log statement */
        if (options->dbglog) {
                fprintf(outf,
                        "\tLOG(\"Finalise %%p\", duk_get_heapptr(ctx, 0));\n" );
        }

        /* output the finialisor code from the binding */
        output_cdata(outf, fini_node, GENBIND_NODE_TYPE_CDATA);

        /* if this interface inherits ensure we call its finaliser */
        if (inherite != NULL) {
                fprintf(outf,
                        "\t%s_%s___fini(ctx, &priv->parent);\n",
                        DLPFX, inherite->class_name);
        }
        fprintf(outf, "}\n\n");

        return 0;
}


/**
 * count the number of arguments to an operation
 *
 * \todo this needs to consider multiple lists (overloaded calls?), varadic
 *       parameters.
 *
 * \retuen number of arguments or -1 if variable
 */
static int count_operation_arguments(struct webidl_node *node)
{
        int argc;
        struct webidl_node *list_node;
        list_node = webidl_node_find_type(
                webidl_node_getnode(node),
                NULL,
                WEBIDL_NODE_TYPE_LIST);
        if (list_node == NULL) {
                /** \todo is having no argument list an error or a warning? */
                return 0;
        }
        argc = webidl_node_enumerate_type(webidl_node_getnode(list_node),
                                          WEBIDL_NODE_TYPE_OPTIONAL_ARGUMENT);
        if (argc != 0) {
                return -1;
        }
        return webidl_node_enumerate_type(webidl_node_getnode(list_node),
                                          WEBIDL_NODE_TYPE_ARGUMENT);
}

/**
 * generate a prototype add for a single class method
 */
static int
output_prototype_method(FILE* outf,
                        struct interface_map_entry *interfacee,
                        struct interface_map_operation_entry *operatione)
{
        int op_argc;

        if (operatione->overloadc > 1) {
                /* only generate a method for the first occourance of an
                 * overloaded method
                 */
                return 0;
        }

        if (operatione->name != NULL) {
                /* normal method of prototype */
                op_argc = count_operation_arguments(operatione->node);

                output_add_method(outf,
                                  interfacee->class_name,
                                  operatione->name,
                                  op_argc);
        } else {
                /* special method on prototype */
                fprintf(outf,
                     "\t/* Special method on prototype - UNIMPLEMENTED */\n\n");
        }

        return 0;
}

/**
 * generate prototype method definitions
 */
static int
output_prototype_methods(FILE *outf, struct interface_map_entry *interfacee)
{
        int opc;
        int res = 0;

        for (opc = 0; opc < interfacee->operationc; opc++) {
                res = output_prototype_method(outf,
                                              interfacee,
                                              interfacee->operationv + opc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}


static int
output_prototype_attribute(FILE *outf,
                        struct interface_map_entry *interfacee,
                        struct interface_map_attribute_entry *attributee)
{
        if (attributee->modifier == WEBIDL_TYPE_MODIFIER_READONLY) {
                return output_populate_ro_property(outf,
                                                   interfacee->class_name,
                                                   attributee->name);
        }
        return output_populate_rw_property(outf,
                                           interfacee->class_name,
                                           attributee->name);
}

/**
 * generate prototype attribute definitions
 */
static int
output_prototype_attributes(FILE *outf, struct interface_map_entry *interfacee)
{
        int attrc;
        int res = 0;

        for (attrc = 0; attrc < interfacee->attributec; attrc++) {
                res = output_prototype_attribute(outf,interfacee,
                              interfacee->attributev + attrc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}

/**
 * output constants on the prototype
 *
 * \todo This implementation assumes the constant is a literal int and should
 * check the type node base value.
 */
static int
output_prototype_constant(FILE *outf,
                          struct interface_map_constant_entry *constante)
{
        int *value;

        value = webidl_node_getint(
                webidl_node_find_type(
                        webidl_node_getnode(constante->node),
                        NULL,
                        WEBIDL_NODE_TYPE_LITERAL_INT));

        output_prototype_constant_int(outf, constante->name, *value);

        return 0;
}

/**
 * generate prototype constant definitions
 */
static int
output_prototype_constants(FILE *outf, struct interface_map_entry *interfacee)
{
        int attrc;
        int res = 0;

        for (attrc = 0; attrc < interfacee->constantc; attrc++) {
                res = output_prototype_constant(outf,
                                                interfacee->constantv + attrc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}

/**
 * generate the interface prototype creator
 */
static int
output_interface_prototype(FILE* outf,
                           struct interface_map_entry *interfacee,
                           struct interface_map_entry *inherite)
{
        /* prototype definition */
        fprintf(outf,
                "duk_ret_t %s_%s___proto(duk_context *ctx)\n",
                DLPFX, interfacee->class_name);
        fprintf(outf,"{\n");

        /* generate prototype chaining if interface has a parent */
        if (inherite != NULL) {
                fprintf(outf,
                      "\t/* Set this prototype's prototype (left-parent) */\n");
                output_get_prototype(outf, inherite->name);
                fprintf(outf, "\tduk_set_prototype(ctx, 0);\n\n");
        }

        /* generate setting of methods */
        output_prototype_methods(outf, interfacee);

        /* generate setting of attributes */
        output_prototype_attributes(outf, interfacee);

        /* generate setting of constants */
        output_prototype_constants(outf, interfacee);

        /* generate setting of destructor */
        output_set_destructor(outf, interfacee->class_name, 0);

        /* generate setting of constructor */
        output_set_constructor(outf,
                               interfacee->class_name,
                               0,
                               interfacee->class_init_argc);

        fprintf(outf,"\treturn 1; /* The prototype object */\n");

        fprintf(outf, "}\n\n");

        return 0;
}


/**
 * generate a single class method for an interface operation
 */
static int
output_interface_operation(FILE* outf,
                           struct interface_map_entry *interfacee,
                           struct interface_map_operation_entry *operatione)
{
        if (operatione->overloadc > 1) {
                /* only generate a method for the first occourance of an
                 * overloaded method
                 */
                return 0;
        }

        if (operatione->name != NULL) {
                /* normal method definition */
                fprintf(outf,
                        "static duk_ret_t %s_%s_%s(duk_context *ctx)\n",
                        DLPFX, interfacee->class_name, operatione->name);
                fprintf(outf,"{\n");

                output_get_method_private(outf, interfacee->class_name);

                output_cdata(outf, operatione->method, GENBIND_NODE_TYPE_CDATA);

                fprintf(outf,"\treturn 0;\n");

                fprintf(outf, "}\n\n");
        } else {
                /* special method definition */
                fprintf(outf,
                        "/* Special method definition - UNIMPLEMENTED */\n\n");
        }

        return 0;
}

/**
 * generate class methods for each interface operation
 */
static int
output_interface_operations(FILE* outf, struct interface_map_entry *interfacee)
{
        int opc;
        int res = 0;

        for (opc = 0; opc < interfacee->operationc; opc++) {
                res = output_interface_operation(outf,
                                                 interfacee,
                                                 interfacee->operationv + opc);
                if (res != 0) {
                        break;
                }
        }

        return res;
}

/**
 * Generate class property getter/setter for a single attribute
 */
static int
output_interface_attribute(FILE* outf,
                           struct interface_map_entry *interfacee,
                           struct interface_map_attribute_entry *atributee)
{
        /* getter definition */
        fprintf(outf,
                "static duk_ret_t %s_%s_%s_getter(duk_context *ctx)\n",
                DLPFX, interfacee->class_name, atributee->name);
        fprintf(outf,"{\n");

        output_get_method_private(outf, interfacee->class_name);

        output_cdata(outf, atributee->getter, GENBIND_NODE_TYPE_CDATA);

        fprintf(outf,"\treturn 0;\n");

        fprintf(outf, "}\n\n");

        /* readonly attributes have no setter */
        if (atributee->modifier == WEBIDL_TYPE_MODIFIER_READONLY) {
                return 0;
        }

        /* setter definition */
        fprintf(outf,
                "static duk_ret_t %s_%s_%s_setter(duk_context *ctx)\n",
                DLPFX, interfacee->class_name, atributee->name);
        fprintf(outf,"{\n");

        output_get_method_private(outf, interfacee->class_name);

        output_cdata(outf, atributee->setter, GENBIND_NODE_TYPE_CDATA);

        fprintf(outf,"\treturn 0;\n");

        fprintf(outf, "}\n\n");

        return 0;
}

/**
 * generate class property getters and setters for each interface attribute
 */
static int
output_interface_attributes(FILE* outf,
                            struct interface_map_entry *interfacee)
{
        int attrc;

        for (attrc = 0; attrc < interfacee->attributec; attrc++) {
                output_interface_attribute(outf,
                                           interfacee,
                                           interfacee->attributev + attrc);
        }

        return 0;
}


/**
 * generate preface block for nsgenbind
 */
static 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;
}

/**
 * generate a source file to implement an interface using duk and libdom.
 */
static int output_interface(struct interface_map *interface_map,
                            struct interface_map_entry *interfacee)
{
        FILE *ifacef;
        int ifacenamelen;
        struct interface_map_entry *inherite;
        int res = 0;

        /* do not generate class for interfaces marked no output */
        if (interfacee->noobject) {
                return 0;
        }

        /* compute class name */
        interfacee->class_name = gen_class_name(interfacee);

        /* generate source filename */
        ifacenamelen = strlen(interfacee->class_name) + 4;
        interfacee->filename = malloc(ifacenamelen);
        snprintf(interfacee->filename, ifacenamelen,
                 "%s.c", interfacee->class_name);

        /* open output file */
        ifacef = genb_fopen(interfacee->filename, "w");
        if (ifacef == NULL) {
                return -1;
        }

        /* find parent interface entry */
        inherite = interface_map_inherit_entry(interface_map, interfacee);

        /* tool preface */
        output_tool_preface(ifacef);

        /* binding preface */
        output_cdata(ifacef,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_PREFACE);

        /* class preface */
        output_cdata(ifacef, interfacee->class, GENBIND_NODE_TYPE_PREFACE);

        /* tool prologue */
        output_tool_prologue(ifacef);

        /* binding prologue */
        output_cdata(ifacef,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_PROLOGUE);

        /* class prologue */
        output_cdata(ifacef, interfacee->class, GENBIND_NODE_TYPE_PROLOGUE);

        fprintf(ifacef, "\n");

        /* initialisor */
        res = output_interface_init(ifacef, interfacee, inherite);
        if (res != 0) {
                goto op_error;
        }

        /* finaliser */
        output_interface_fini(ifacef, interfacee, inherite);

        /* constructor */
        output_interface_constructor(ifacef, interfacee);

        /* destructor */
        output_interface_destructor(ifacef, interfacee);

        /* operations */
        output_interface_operations(ifacef, interfacee);

        /* attributes */
        output_interface_attributes(ifacef, interfacee);

        /* prototype */
        output_interface_prototype(ifacef, interfacee, inherite);

        fprintf(ifacef, "\n");

        /* class epilogue */
        output_cdata(ifacef, interfacee->class, GENBIND_NODE_TYPE_EPILOGUE);

        /* binding epilogue */
        output_cdata(ifacef,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_EPILOGUE);

        /* class postface */
        output_cdata(ifacef, interfacee->class, GENBIND_NODE_TYPE_POSTFACE);

        /* binding postface */
        output_cdata(ifacef,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_POSTFACE);

op_error:
        fclose(ifacef);

        return res;
}

/**
 * generate private header
 */
static int
output_private_header(struct interface_map *interface_map)
{
        int idx;
        FILE *privf;

        /* open header */
        privf = open_header(interface_map, "private");

        for (idx = 0; idx < interface_map->entryc; idx++) {
                struct interface_map_entry *interfacee;
                struct interface_map_entry *inherite;
                struct genbind_node *priv_node;

                interfacee = interface_map->entries + idx;

                /* do not generate private structs for interfaces marked no
                 * output
                 */
                if (interfacee->noobject) {
                        continue;
                }

                /* find parent interface entry */
                inherite = interface_map_inherit_entry(interface_map,
                                                       interfacee);

                fprintf(privf, "typedef struct {\n");
                if (inherite != NULL) {
                        fprintf(privf, "\t%s_private_t parent;\n",
                                inherite->class_name);
                }

                /* for each private variable on the class output it here. */
                priv_node = genbind_node_find_type(
                        genbind_node_getnode(interfacee->class),
                        NULL,
                        GENBIND_NODE_TYPE_PRIVATE);
                while (priv_node != NULL) {
                        fprintf(privf, "\t");
                        output_cdata(privf, priv_node, GENBIND_NODE_TYPE_TYPE);
                        output_cdata(privf, priv_node, GENBIND_NODE_TYPE_IDENT);
                        fprintf(privf, ";\n");

                        priv_node = genbind_node_find_type(
                                genbind_node_getnode(interfacee->class),
                                priv_node,
                                GENBIND_NODE_TYPE_PRIVATE);
                }

                fprintf(privf, "} %s_private_t;\n\n", interfacee->class_name);

        }

        close_header(interface_map, privf);

        return 0;
}

/**
 * generate prototype header
 */
static int
output_prototype_header(struct interface_map *interface_map)
{
        int idx;
        FILE *protof;

        /* open header */
        protof = open_header(interface_map, "prototype");

        for (idx = 0; idx < interface_map->entryc; idx++) {
                struct interface_map_entry *interfacee;
                struct genbind_node *init_node;

                interfacee = interface_map->entries + idx;

                /* do not generate prototype declarations for interfaces marked
                 * no output
                 */
                if (interfacee->noobject) {
                        continue;
                }

                /* prototype declaration */
                fprintf(protof, "duk_ret_t %s_%s___proto(duk_context *ctx);\n",
                        DLPFX, interfacee->class_name);

                /** \todo if the interface has no references (no other
                 * interface inherits from it) there is no reason to export
                 * the initalisor/finaliser as no other class
                 * constructor/destructor should call them. Additionally the
                 * init/fini definition should be made static.
                 */

                /* finaliser declaration */
                fprintf(protof,
                        "void %s_%s___fini(duk_context *ctx, %s_private_t *priv);\n",
                        DLPFX, interfacee->class_name, interfacee->class_name);

                /* find the initialisor method on the class (if any) */
                init_node = genbind_node_find_method(interfacee->class,
                                                     NULL,
                                                     GENBIND_METHOD_TYPE_INIT);

                /* initialisor definition */
                output_interface_init_declaration(protof, interfacee, init_node);
                fprintf(protof, ";\n\n");
        }

        close_header(interface_map, protof);

        return 0;
}

/**
 * generate makefile fragment
 */
static int
output_makefile(struct interface_map *interface_map)
{
        int idx;
        FILE *makef;

        /* open output file */
        makef = genb_fopen("Makefile", "w");
        if (makef == NULL) {
                return -1;
        }

        fprintf(makef, "# duk libdom makefile fragment\n\n");

        fprintf(makef, "NSGENBIND_SOURCES:=binding.c ");
        for (idx = 0; idx < interface_map->entryc; idx++) {
                struct interface_map_entry *interfacee;

                interfacee = interface_map->entries + idx;

                /* no source for interfaces marked no output */
                if (interfacee->noobject) {
                        continue;
                }

                fprintf(makef, "%s ", interfacee->filename);
        }
        fprintf(makef, "\nNSGENBIND_PREFIX:=%s\n", options->outdirname);

        fclose(makef);

        return 0;
}


/**
 * generate binding header
 *
 * The binding header contains all the duk-libdom specific binding interface
 * macros and definitions.
 *
 * the create prototypes interface is used to cause all the prototype creation
 * functions for all generated classes to be called in the correct order with
 * the primary global (if any) generated last.
 */
static int
output_binding_header(struct interface_map *interface_map)
{
        FILE *bindf;

        /* open header */
        bindf = open_header(interface_map, "binding");

        fprintf(bindf,
                "#define _MAGIC(S) (\"%s\" S)\n"
                "#define MAGIC(S) _MAGIC(#S)\n"
                "#define PROTO_MAGIC MAGIC(PROTOTYPES)\n"
                "#define PRIVATE_MAGIC MAGIC(PRIVATE)\n"
                "#define INIT_MAGIC MAGIC(INIT)\n"
                "#define NODE_MAGIC MAGIC(NODE_MAP)\n"
                "#define _PROTO_NAME(K) _MAGIC(\"PROTOTYPE_\" K)\n"
                "#define PROTO_NAME(K) _PROTO_NAME(#K)\n"
                "#define _PROP_NAME(K,V) _MAGIC(K \"_PROPERTY_\" V)\n"
                "#define PROP_NAME(K,V) _PROP_NAME(#K,#V)\n"
                "\n",
                MAGICPFX);

        fprintf(bindf,
                "duk_bool_t %s_instanceof(duk_context *ctx, const char *klass);\n",
                DLPFX);

        fprintf(bindf,
                "duk_ret_t %s_create_prototypes(duk_context *ctx);\n", DLPFX);

        close_header(interface_map, bindf);

        return 0;
}


/**
 * generate binding source
 *
 * The binding header contains all the duk-libdom specific binding
 * implementations.
 */
static int
output_binding_src(struct interface_map *interface_map)
{
        int idx;
        FILE *bindf;
        struct interface_map_entry *pglobale = NULL;
        char *proto_name;

        /* open output file */
        bindf = genb_fopen("binding.c", "w");
        if (bindf == NULL) {
                return -1;
        }

        /* tool preface */
        output_tool_preface(bindf);

        /* binding preface */
        output_cdata(bindf,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_PREFACE);

        output_tool_prologue(bindf);

        /* binding prologue */
        output_cdata(bindf,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_PROLOGUE);


        fprintf(bindf, "\n");

        /* instance of helper */
        fprintf(bindf,
                "duk_bool_t\n"
                "%s_instanceof(duk_context *ctx, const char *klass)\n",
                DLPFX);
        fprintf(bindf,
                "{\n"
                "\t/* ... ??? */\n"
                "\tif (!duk_check_type(ctx, -1, DUK_TYPE_OBJECT)) {\n"
                "\t\treturn false;\n"
                "\t}\n"
                "\t/* ... obj */\n"
                "\tduk_get_global_string(ctx, \"%sPROTOTYPES\");\n"
                "\t/* ... obj protos */\n"
                "\tduk_get_prop_string(ctx, -1, klass);\n"
                "\t/* ... obj protos goalproto */\n"
                "\tduk_get_prototype(ctx, -3);\n"
                "\t/* ... obj protos goalproto proto? */\n"
                "\twhile (!duk_is_undefined(ctx, -1)) {\n"
                "\t\tif (duk_strict_equals(ctx, -1, -2)) {\n"
                "\t\t\tduk_pop_3(ctx);\n"
                "\t\t\treturn true;\n"
                "\t\t}\n"
                "\t\tduk_get_prototype(ctx, -1);\n"
                "\t\t/* ... obj protos goalproto proto proto? */\n"
                "\t\tduk_replace(ctx, -2);\n"
                "\t\t/* ... obj protos goalproto proto? */\n"
                "\t}\n"
                "\tduk_pop_3(ctx);\n"
                "\t/* ... obj */\n"
                "\treturn false;\n"
                "}\n"
                "\n",
                MAGICPFX);

        /* prototype creation helper function */
        fprintf(bindf,
                "static duk_ret_t\n"
                "%s_to_string(duk_context *ctx)\n"
                "{\n"
                "\t/* */\n"
                "\tduk_push_this(ctx);\n"
                "\t/* this */\n"
                "\tduk_get_prototype(ctx, -1);\n"
                "\t/* this proto */\n"
                "\tduk_get_prop_string(ctx, -1, \"%sklass_name\");\n"
                "\t/* this proto classname */\n"
                "\tduk_push_string(ctx, \"[object \");\n"
                "\t/* this proto classname str */\n"
                "\tduk_insert(ctx, -2);\n"
                "\t/* this proto str classname */\n"
                "\tduk_push_string(ctx, \"]\");\n"
                "\t/* this proto str classname str */\n"
                "\tduk_concat(ctx, 3);\n"
                "\t/* this proto str */\n"
                "\treturn 1;\n"
                "}\n"
                "\n",
                DLPFX,
                MAGICPFX);

        fprintf(bindf,
                "static duk_ret_t %s_create_prototype(duk_context *ctx,\n",
                DLPFX);
        fprintf(bindf,
                "\t\t\t\t\tduk_safe_call_function genproto,\n"
                "\t\t\t\t\tconst char *proto_name,\n"
                "\t\t\t\t\tconst char *klass_name)\n"
                "{\n"
                "\tduk_int_t ret;\n"
                "\tduk_push_object(ctx);\n"
                "\tif ((ret = duk_safe_call(ctx, genproto, 1, 1)) != DUK_EXEC_SUCCESS) {\n"
                "\t\tduk_pop(ctx);\n"
                "\t\tLOG(\"Failed to register prototype for %%s\", proto_name + 2);\n"
                "\t\treturn ret;\n"
                "\t}\n"
                "\t/* top of stack is the ready prototype, inject it */\n"
                "\tduk_push_string(ctx, klass_name);\n"
                "\tduk_put_prop_string(ctx, -2, \"%sklass_name\");\n"
                "\tduk_push_c_function(ctx, %s_to_string, 0);\n"
                "\tduk_put_prop_string(ctx, -2, \"toString\");\n"
                "\tduk_push_string(ctx, \"toString\");\n"
                "\tduk_def_prop(ctx, -2, DUK_DEFPROP_HAVE_ENUMERABLE);\n"
                "\tduk_put_global_string(ctx, proto_name);\n"
                "\treturn DUK_ERR_NONE;\n"
                "}\n\n",
                MAGICPFX,
                DLPFX);

        /* generate prototype creation */
        fprintf(bindf,
                "duk_ret_t %s_create_prototypes(duk_context *ctx)\n", DLPFX);

        fprintf(bindf, "{\n");

        for (idx = 0; idx < interface_map->entryc; idx++) {
                struct interface_map_entry *interfacee;

                interfacee = interface_map->entries + idx;

                /* do not generate prototype calls for interfaces marked
                 * no output
                 */
                if (interfacee->noobject) {
                        continue;
                }

                if (interfacee->primary_global) {
                        pglobale = interfacee;
                        continue;
                }

                proto_name = get_prototype_name(interfacee->name);

                fprintf(bindf,
                        "\t%s_create_prototype(ctx, %s_%s___proto, \"%s\", \"%s\");\n",
                        DLPFX,
                        DLPFX,
                        interfacee->class_name,
                        proto_name,
                        interfacee->name);

                free(proto_name);
        }

        if (pglobale != NULL) {
                fprintf(bindf, "\n\t/* Global object prototype is last */\n");

                proto_name = get_prototype_name(pglobale->name);
                fprintf(bindf,
                        "\t%s_create_prototype(ctx, %s_%s___proto, \"%s\", \"%s\");\n",
                        DLPFX,
                        DLPFX,
                        pglobale->class_name,
                        proto_name,
                        pglobale->name);
                free(proto_name);
        }

        fprintf(bindf, "\n\treturn DUK_ERR_NONE;\n");

        fprintf(bindf, "}\n");

        /* binding postface */
        output_cdata(bindf,
                     interface_map->binding_node,
                     GENBIND_NODE_TYPE_POSTFACE);

        fclose(bindf);
        return 0;
}

int duk_libdom_output(struct interface_map *interface_map)
{
        int idx;
        int res = 0;

        /* generate interfaces */
        for (idx = 0; idx < interface_map->entryc; idx++) {
                res = output_interface(interface_map,
                                       interface_map->entries + idx);
                if (res != 0) {
                        goto output_err;
                }
        }

        /* generate private header */
        res = output_private_header(interface_map);
        if (res != 0) {
                goto output_err;
        }

        /* generate prototype header */
        res = output_prototype_header(interface_map);
        if (res != 0) {
                goto output_err;
        }

        /* generate binding header */
        res = output_binding_header(interface_map);
        if (res != 0) {
                goto output_err;
        }

        /* generate binding source */
        res = output_binding_src(interface_map);
        if (res != 0) {
                goto output_err;
        }

        /* generate makefile fragment */
        res = output_makefile(interface_map);

output_err:

        return res;
}