diff options
Diffstat (limited to 'src/jsapi-libdom.c')
-rw-r--r-- | src/jsapi-libdom.c | 104 |
1 files changed, 92 insertions, 12 deletions
diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 3e0bf08..4fbe67b 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -1,7 +1,7 @@ /* binding output generator for jsapi(spidermonkey) to libdom * * This file is part of nsgenbind. - * Licensed under the MIT License, + * Published under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org> */ @@ -17,8 +17,18 @@ #include "webidl-ast.h" #include "jsapi-libdom.h" -#define HDR_COMMENT_SEP "\n * \n * " -#define HDR_COMMENT_PREABLE "Generated by nsgenbind " +#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" + +#define HDROUTF(bndg, fmt, args...) do { \ + if (bndg->hdrfile != NULL) { \ + fprintf(bndg->hdrfile, fmt, ##args); \ + } \ + } while(0) static int webidl_file_cb(struct genbind_node *node, void *ctx) @@ -249,7 +259,18 @@ output_class_init(struct binding *binding) int res = 0; struct genbind_node *api_node; - /* class Initialisor */ + /* class Initialisor declaration */ + if (binding->hdrfile) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, + "JSObject *jsapi_InitClass_%s(JSContext *cx, JSObject *parent);\n", + binding->interface); + + binding->outfile = binding->srcfile; + } + + /* class Initialisor definition */ fprintf(binding->outfile, "JSObject *jsapi_InitClass_%s(JSContext *cx, JSObject *parent)\n" "{\n" @@ -294,7 +315,27 @@ output_class_new(struct binding *binding) int res = 0; struct genbind_node *api_node; - /* constructor */ + /* constructor declaration */ + if (binding->hdrfile) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, + "JSObject *jsapi_new_%s(JSContext *cx,\n" + "\t\tJSObject *prototype,\n" + "\t\tJSObject *parent", + binding->interface); + + genbind_node_for_each_type(binding->binding_list, + GENBIND_NODE_TYPE_BINDING_PRIVATE, + webidl_private_param_cb, + binding); + + fprintf(binding->outfile, ");"); + + binding->outfile = binding->srcfile; + } + + /* constructor definition */ fprintf(binding->outfile, "JSObject *jsapi_new_%s(JSContext *cx,\n" "\t\tJSObject *prototype,\n" @@ -436,7 +477,10 @@ output_jsclass(struct binding *binding) fprintf(binding->outfile, "static JSPropertySpec jsclass_properties[];\n\n"); - /* output the class */ + /* output the class declaration */ + HDROUTF(binding, "JSClass JSClass_%s;\n", binding->interface); + + /* output the class definition */ fprintf(binding->outfile, "JSClass JSClass_%s = {\n" "\t\"%s\",\n", @@ -559,7 +603,8 @@ output_preamble(struct binding *binding) static int output_header_comments(struct binding *binding) { - fprintf(binding->outfile, "/* "HDR_COMMENT_PREABLE); + const char *preamble = HDR_COMMENT_PREAMBLE; + fprintf(binding->outfile, preamble, options->infilename); genbind_node_for_each_type(binding->gb_ast, GENBIND_NODE_TYPE_HDRCOMMENT, @@ -567,6 +612,21 @@ output_header_comments(struct binding *binding) binding); fprintf(binding->outfile,"\n */\n\n"); + + if (binding->hdrfile != NULL) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, preamble, options->infilename); + + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_HDRCOMMENT, + webidl_hdrcomment_cb, + binding); + + fprintf(binding->outfile,"\n */\n\n"); + + binding->outfile = binding->srcfile; + } return 0; } @@ -608,14 +668,17 @@ binding_has_global(struct binding *binding) } static struct binding * -binding_new(char *outfilename, struct genbind_node *genbind_ast) +binding_new(char *outfilename, + char *hdrfilename, + struct genbind_node *genbind_ast) { struct binding *nb; struct genbind_node *binding_node; struct genbind_node *binding_list; struct genbind_node *ident_node; struct genbind_node *interface_node; - FILE *outfile ; /* output file */ + FILE *outfile = NULL; /* output source file */ + FILE *hdrfile = NULL; /* output header file */ struct webidl_node *webidl_ast = NULL; int res; @@ -659,12 +722,24 @@ binding_new(char *outfilename, struct genbind_node *genbind_ast) outfile = fopen(outfilename, "w"); } if (outfile == NULL) { - fprintf(stderr, "Error opening output %s: %s\n", + fprintf(stderr, "Error opening source output %s: %s\n", outfilename, strerror(errno)); return NULL; } + /* output header file if required */ + if (hdrfilename != NULL) { + hdrfile = fopen(hdrfilename, "w"); + if (hdrfile == NULL) { + fprintf(stderr, "Error opening header output %s: %s\n", + hdrfilename, + strerror(errno)); + fclose(outfile); + return NULL; + } + } + nb = calloc(1, sizeof(struct binding)); nb->gb_ast = genbind_ast; @@ -672,6 +747,8 @@ binding_new(char *outfilename, struct genbind_node *genbind_ast) nb->name = genbind_node_gettext(ident_node); nb->interface = genbind_node_gettext(interface_node); nb->outfile = outfile; + nb->srcfile = outfile; + nb->hdrfile = hdrfile; nb->has_private = binding_has_private(binding_list); nb->has_global = binding_has_global(nb); nb->binding_list = binding_list; @@ -692,13 +769,16 @@ binding_new(char *outfilename, struct genbind_node *genbind_ast) } -int jsapi_libdom_output(char *outfilename, struct genbind_node *genbind_ast) +int +jsapi_libdom_output(char *outfilename, + char *hdrfilename, + struct genbind_node *genbind_ast) { int res; struct binding *binding; /* get general binding information used in output */ - binding = binding_new(outfilename, genbind_ast); + binding = binding_new(outfilename, hdrfilename, genbind_ast); if (binding == NULL) { return 40; } |