summaryrefslogtreecommitdiff
path: root/src/jsapi-libdom.c
blob: 5e994b91a868af399293b5f2ac017a11dfe45173 (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
/* binding output generator for jsapi(spidermonkey) to libdom
 *
 * This file is part of nsgenbind.
 * Published 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 <ctype.h>

#include "options.h"
#include "nsgenbind-ast.h"
#include "webidl-ast.h"
#include "jsapi-libdom.h"

#define HDR_COMMENT_SEP "\n * \n * "
#define HDR_COMMENT_PREAMBLE "/* Generated by nsgenbind from %s\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"



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_foreach_type(genbind_ast,
					 GENBIND_NODE_TYPE_WEBIDLFILE,
					 webidl_file_cb,
					 webidl_ast);

	if (res == 0) {
		res = webidl_intercalate_implements(*webidl_ast);
	}

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


static int webidl_preamble_cb(struct genbind_node *node, void *ctx)
{
	struct binding *binding = ctx;

	fprintf(binding->outfile, "%s", genbind_node_gettext(node));

	return 0;
}

static int webidl_prologue_cb(struct genbind_node *node, void *ctx)
{
	struct binding *binding = ctx;

	fprintf(binding->outfile, "%s", genbind_node_gettext(node));

	return 0;
}

static int webidl_epilogue_cb(struct genbind_node *node, void *ctx)
{
	struct binding *binding = ctx;

	fprintf(binding->outfile, "%s", genbind_node_gettext(node));

	return 0;
}


static int webidl_hdrcomments_cb(struct genbind_node *node, void *ctx)
{
	struct binding *binding = ctx;

	fprintf(binding->outfile,
		HDR_COMMENT_SEP"%s",
		genbind_node_gettext(node));

	return 0;
}

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

static int webidl_private_cb(struct genbind_node *node, void *ctx)
{
	struct binding *binding = ctx;
	struct genbind_node *ident_node;
	struct genbind_node *type_node;


	ident_node = genbind_node_find_type(genbind_node_getnode(node),
					    NULL,
					    GENBIND_NODE_TYPE_IDENT);
	if (ident_node == NULL)
		return -1; /* bad AST */

	type_node = genbind_node_find_type(genbind_node_getnode(node),
					    NULL,
					    GENBIND_NODE_TYPE_STRING);
	if (type_node == NULL)
		return -1; /* bad AST */

	fprintf(binding->outfile,
		"        %s%s;\n",
		genbind_node_gettext(type_node),
		genbind_node_gettext(ident_node));

	return 0;
}



/* section output generators */

/** Output the epilogue right at the end of the generated program */
static int
output_epilogue(struct binding *binding)
{
	genbind_node_foreach_type(binding->gb_ast,
				   GENBIND_NODE_TYPE_EPILOGUE,
				   webidl_epilogue_cb,
				   binding);

	fprintf(binding->outfile,"\n\n");

	if (binding->hdrfile) {
		binding->outfile = binding->hdrfile;

		fprintf(binding->outfile,
			"\n\n#endif /* _%s_ */\n",
			binding->hdrguard);

		binding->outfile = binding->srcfile;
	}

	return 0;
}

/** Output the prologue right before the generated function bodies */
static int
output_prologue(struct binding *binding)
{
	/* forward declare property list */
	fprintf(binding->outfile,
		"static JSPropertySpec jsclass_properties[];\n\n");

	genbind_node_foreach_type(binding->gb_ast,
				   GENBIND_NODE_TYPE_PROLOGUE,
				   webidl_prologue_cb,
				   binding);

	fprintf(binding->outfile,"\n\n");

	return 0;
}


static int
output_api_operations(struct binding *binding)
{
	int res = 0;

	/* finalise */
	if (binding->has_private) {
		/* finalizer with private to free */
		fprintf(binding->outfile,
			"static void jsclass_finalize(JSContext *cx, JSObject *obj)\n"
			"{\n"
			"\tstruct jsclass_private *private;\n"
			"\n"
			"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
			binding->interface);

		if (options->dbglog) {
			fprintf(binding->outfile,
				"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}

		if (binding->finalise != NULL) {
			output_code_block(binding,
				genbind_node_getnode(binding->finalise));
		}

		fprintf(binding->outfile,
			"\tif (private != NULL) {\n"
			"\t\tfree(private);\n"
			"\t}\n"
			"}\n\n");
	} else if (binding->finalise != NULL) {
		/* finaliser without private data */
		fprintf(binding->outfile,
			"static void jsclass_finalize(JSContext *cx, JSObject *obj)\n"
			"{\n");

		if (options->dbglog) {
			fprintf(binding->outfile,
				"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
		}

		output_code_block(binding,
				  genbind_node_getnode(binding->finalise));

		fprintf(binding->outfile,
			"}\n\n");

	}

	/* generate class default property add implementation */
	if (binding->addproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(add, JSContext *cx, JSObject *obj, jsval *vp)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}


		output_code_block(binding,
				  genbind_node_getnode(binding->addproperty));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate class default property delete implementation */
	if (binding->delproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(del, JSContext *cx, JSObject *obj, jsval *vp)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}


		output_code_block(binding,
				  genbind_node_getnode(binding->delproperty));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate class default property get implementation */
	if (binding->getproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(get, JSContext *cx, JSObject *obj, jsval *vp)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}


		output_code_block(binding,
				  genbind_node_getnode(binding->getproperty));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate class default property set implementation */
	if (binding->setproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_STRICTPROP(set, JSContext *cx, JSObject *obj, jsval *vp)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}


		output_code_block(binding,
				  genbind_node_getnode(binding->setproperty));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate class enumerate implementation */
	if (binding->enumerate != NULL) {
		fprintf(binding->outfile,
			"static JSBool jsclass_enumerate(JSContext *cx, JSObject *obj)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}

		output_code_block(binding, genbind_node_getnode(binding->enumerate));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate class resolver implementation */
	if (binding->resolve != NULL) {
		fprintf(binding->outfile,
			"static JSBool jsclass_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp)\n"
			"{\n");

		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(cx, obj, &JSClass_%s, NULL);\n",
				binding->interface);
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", cx, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", cx, obj);\n");
			}

		}


		output_code_block(binding, genbind_node_getnode(binding->resolve));

		fprintf(binding->outfile,
			"\treturn JS_TRUE;\n"
			"}\n\n");
	}

	/* generate trace/mark entry */
	if (binding->mark != NULL) {
		fprintf(binding->outfile,
			"static JSAPI_MARKOP(jsclass_mark)\n"
			"{\n");
		if (binding->has_private) {

			fprintf(binding->outfile,
				"\tstruct jsclass_private *private;\n"
				"\n"
				"\tprivate = JS_GetInstancePrivate(JSAPI_MARKCX, obj, &JSClass_%s, NULL);\n",
				binding->interface);

			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p private:%%p\", JSAPI_MARKCX, obj, private);\n");
			}
		} else {
			if (options->dbglog) {
				fprintf(binding->outfile,
					"\tJSLOG(\"jscontext:%%p jsobject:%%p\", JSAPI_MARKCX, obj);\n");
			}

		}

		output_code_block(binding, genbind_node_getnode(binding->mark));

		fprintf(binding->outfile,
			"\tJSAPI_MARKOP_RETURN(JS_TRUE);\n"
			"}\n\n");
	}
	return res;
}

void
output_code_block(struct binding *binding, struct genbind_node *codelist)
{
	struct genbind_node *code_node;

	code_node = genbind_node_find_type(codelist,
					   NULL,
					   GENBIND_NODE_TYPE_CBLOCK);
	if (code_node != NULL) {
		fprintf(binding->outfile,
			"%s\n",
			genbind_node_gettext(code_node));
	}
}



static int
output_forward_declarations(struct binding *binding)
{
	/* forward declare add property */
	if (binding->addproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(add, JSContext *cx, JSObject *obj, jsval *vp);\n\n");
	}

	/* forward declare del property */
	if (binding->delproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(del, JSContext *cx, JSObject *obj, jsval *vp);\n\n");
	}

	/* forward declare get property */
	if (binding->getproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_PROP(get, JSContext *cx, JSObject *obj, jsval *vp);\n\n");
	}

	/* forward declare set property */
	if (binding->setproperty != NULL) {
		fprintf(binding->outfile,
			"static JSBool JSAPI_STRICTPROP(set, JSContext *cx, JSObject *obj, jsval *vp);\n\n");
	}

	/* forward declare the enumerate */
	if (binding->enumerate != NULL) {
		fprintf(binding->outfile,
			"static JSBool jsclass_enumerate(JSContext *cx, JSObject *obj);\n\n");
	}

	/* forward declare the resolver */
	if (binding->resolve != NULL) {
		fprintf(binding->outfile,
			"static JSBool jsclass_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp);\n\n");
	}

	/* forward declare the GC mark operation */
	if (binding->mark != NULL) {
		fprintf(binding->outfile,
			"static JSAPI_MARKOP(jsclass_mark);\n\n");
	}

	/* forward declare the finalizer */
	if (binding->has_private || (binding->finalise != NULL)) {
		fprintf(binding->outfile,
			"static void jsclass_finalize(JSContext *cx, JSObject *obj);\n\n");
	}

	return 0;
}



/** generate structure definition for internal class data
 *
 * Every javascript object instance has an internal context to keep
 * track of its state in object terms this would be considered the
 * "this" pointer giving access to the classes members.
 *
 * Member access can be considered protected as all interfaces
 * (classes) and subclasses generated within this binding have access
 * to members.
 *
 * @param binding the binding to generate the structure for.
 * @return 0 on success with output written and -1 on error.
 */
static int
output_private_declaration(struct binding *binding)
{

	if (!binding->has_private) {
		return 0;
	}

	fprintf(binding->outfile, "struct jsclass_private {\n");

	genbind_node_foreach_type(binding->binding_list,
				   GENBIND_NODE_TYPE_BINDING_PRIVATE,
				   webidl_private_cb,
				   binding);

	genbind_node_foreach_type(binding->binding_list,
				   GENBIND_NODE_TYPE_BINDING_INTERNAL,
				   webidl_private_cb,
				   binding);

	fprintf(binding->outfile, "};\n\n");


	return 0;
}

static int
output_preamble(struct binding *binding)
{
	genbind_node_foreach_type(binding->gb_ast,
				   GENBIND_NODE_TYPE_PREAMBLE,
				   webidl_preamble_cb,
				   binding);

	fprintf(binding->outfile,"\n\n");

	if (binding->hdrfile) {
		binding->outfile = binding->hdrfile;

		fprintf(binding->outfile,
			"#ifndef _%s_\n"
			"#define _%s_\n\n",
			binding->hdrguard,
			binding->hdrguard);

		binding->outfile = binding->srcfile;
	}


	return 0;
}


static int
output_header_comments(struct binding *binding)
{
	const char *preamble = HDR_COMMENT_PREAMBLE;
	fprintf(binding->outfile, preamble, options->infilename);

	genbind_node_foreach_type(binding->gb_ast,
				   GENBIND_NODE_TYPE_HDRCOMMENT,
				   webidl_hdrcomment_cb,
				   binding);

	fprintf(binding->outfile,"\n */\n\n");

	if (binding->hdrfile != NULL) {
		binding->outfile = binding->hdrfile;

		fprintf(binding->outfile, preamble, options->infilename);

		genbind_node_foreach_type(binding->gb_ast,
					   GENBIND_NODE_TYPE_HDRCOMMENT,
					   webidl_hdrcomment_cb,
					   binding);

		fprintf(binding->outfile,"\n */\n\n");

		binding->outfile = binding->srcfile;
	}
	return 0;
}

static bool
binding_has_private(struct genbind_node *binding_list)
{
	struct genbind_node *node;

	node = genbind_node_find_type(binding_list,
				      NULL,
				      GENBIND_NODE_TYPE_BINDING_PRIVATE);

	if (node != NULL) {
		return true;
	}

	node = genbind_node_find_type(binding_list,
				      NULL,
				      GENBIND_NODE_TYPE_BINDING_INTERNAL);
	if (node != NULL) {
		return true;
	}
	return false;
}



/** obtain the name to use for the binding.
 *
 * @todo it should be possible to specify the binding name instead of
 * just using the name of the first interface.
 */
static const char *get_binding_name(struct genbind_node *binding_node)
{
	return genbind_node_gettext(
		genbind_node_find_type(
			genbind_node_getnode(
				genbind_node_find_type(
					genbind_node_getnode(binding_node),
					NULL,
					GENBIND_NODE_TYPE_BINDING_INTERFACE)),
			NULL,
			GENBIND_NODE_TYPE_IDENT));
}

static struct binding *
binding_new(struct options *options,
	    struct genbind_node *genbind_ast,
	    struct genbind_node *binding_node)
{
	struct binding *nb;

	int interfacec; /* numer of interfaces in the interface map */
	struct binding_interface *interfaces; /* binding interface map */

	struct genbind_node *binding_list;
	char *hdrguard = NULL;
	struct webidl_node *webidl_ast = NULL;
	int res;

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

	/* build the bindings interface (class) name map */
	if (build_interface_map(binding_node,
				webidl_ast,
				&interfacec,
				&interfaces) != 0) {
		/* the binding must have at least one interface */
		fprintf(stderr, "Error: Binding must have a valid interface\n");
		return NULL;
	}

	/* header guard */
	if (options->hdrfilename != NULL) {
		int guardlen;
		int pos;

		guardlen = strlen(options->hdrfilename);
		hdrguard = calloc(1, guardlen + 1);
		for (pos = 0; pos < guardlen; pos++) {
			if (isalpha(options->hdrfilename[pos])) {
				hdrguard[pos] = toupper(options->hdrfilename[pos]);
			} else {
				hdrguard[pos] = '_';
			}
		}
	}

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

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

	nb->gb_ast = genbind_ast;
	nb->wi_ast = webidl_ast;

	/* keep the binding list node */
	nb->binding_list = binding_list;

	/* store the interface mapping */
	nb->interfaces = interfaces;
	nb->interfacec = interfacec;

	/* @todo get rid of the interface element out of the binding
	 * struct and use the interface map instead.
	 */
	nb->name = nb->interface = get_binding_name(binding_node);
	nb->outfile = options->outfilehandle;
	nb->srcfile = options->outfilehandle;
	nb->hdrfile = options->hdrfilehandle;
	nb->hdrguard = hdrguard;
	nb->has_private = binding_has_private(binding_list);

	/* class API */
	nb->addproperty = genbind_node_find_type_ident(genbind_ast,
						      NULL,
						      GENBIND_NODE_TYPE_API,
						      "addproperty");

	nb->delproperty = genbind_node_find_type_ident(genbind_ast,
						      NULL,
						      GENBIND_NODE_TYPE_API,
						      "delproperty");

	nb->getproperty = genbind_node_find_type_ident(genbind_ast,
						      NULL,
						      GENBIND_NODE_TYPE_API,
						      "getproperty");

	nb->setproperty = genbind_node_find_type_ident(genbind_ast,
						      NULL,
						      GENBIND_NODE_TYPE_API,
						      "setproperty");

	nb->enumerate = genbind_node_find_type_ident(genbind_ast,
						     NULL,
						     GENBIND_NODE_TYPE_API,
						     "enumerate");

	nb->resolve = genbind_node_find_type_ident(genbind_ast,
						   NULL,
						   GENBIND_NODE_TYPE_API,
						   "resolve");

	nb->finalise = genbind_node_find_type_ident(genbind_ast,
						    NULL,
						    GENBIND_NODE_TYPE_API,
						    "finalise");

	nb->mark = genbind_node_find_type_ident(genbind_ast,
						NULL,
						GENBIND_NODE_TYPE_API,
						"mark");
	return nb;
}


int
jsapi_libdom_output(struct options *options,
		    struct genbind_node *genbind_ast,
		    struct genbind_node *binding_node)
{
	int res;
	struct binding *binding;

	/* get general binding information used in output */
	binding = binding_new(options, genbind_ast, binding_node);
	if (binding == NULL) {
		return 40;
	}

	/* start with comment block */
	res = output_header_comments(binding);
	if (res) {
		return 50;
	}

	res = output_preamble(binding);
	if (res) {
		return 60;
	}

	res = output_private_declaration(binding);
	if (res) {
		return 70;
	}

	res = output_forward_declarations(binding);
	if (res) {
		return 75;
	}

	res = output_jsclasses(binding);
	if (res) {
		return 80;
	}

	res = output_property_tinyid(binding);
	if (res) {
		return 85;
	}

	/* user code output just before interface code bodies emitted */
	res = output_prologue(binding);
	if (res) {
		return 89;
	}

	/* method (function) and property body generation */

	res = output_function_bodies(binding);
	if (res) {
		return 90;
	}

	res = output_property_body(binding);
	if (res) {
		return 100;
	}

	/* method (function) and property specifier generation */

	res = output_function_spec(binding);
	if (res) {
		return 110;
	}

	res = output_property_spec(binding);
	if (res) {
		return 120;
	}

	/* binding specific operations (destructors etc.) */

	res = output_api_operations(binding);
	if (res) {
		return 130;
	}

	res = output_class_init(binding);
	if (res) {
		return 140;
	}

	res = output_class_new(binding);
	if (res) {
		return 150;
	}

	res = output_epilogue(binding);
	if (res) {
		return 160;
	}

	fclose(binding->outfile);

	return 0;
}