diff options
author | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-12-19 19:28:29 +0000 |
---|---|---|
committer | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-12-19 19:28:29 +0000 |
commit | 841ecdf404e166945f5cc6cd320ca15dfb93b57c (patch) | |
tree | 0e5ee476df22f90c91d9631c74618bd0d8b475ca | |
parent | 8c1dc149575cfab4f3fdac91d9fd3c41be9d75bc (diff) | |
download | nsgenbind-841ecdf404e166945f5cc6cd320ca15dfb93b57c.tar.gz nsgenbind-841ecdf404e166945f5cc6cd320ca15dfb93b57c.tar.bz2 |
allow generation of header for exported interfaces and structures.
-rw-r--r-- | src/jsapi-libdom.c | 104 | ||||
-rw-r--r-- | src/jsapi-libdom.h | 9 | ||||
-rw-r--r-- | src/nsgenbind.c | 19 | ||||
-rw-r--r-- | src/options.h | 21 | ||||
-rwxr-xr-x | test/testrunner.sh | 4 |
5 files changed, 129 insertions, 28 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; } diff --git a/src/jsapi-libdom.h b/src/jsapi-libdom.h index 8af39b2..bdc941d 100644 --- a/src/jsapi-libdom.h +++ b/src/jsapi-libdom.h @@ -24,11 +24,16 @@ struct binding { struct genbind_node *finalise; /* binding api finalise node or NULL */ struct genbind_node *mark; /* binding api mark node or NULL */ - FILE *outfile ; /* output file */ + FILE *outfile ; /* file handle output should be written to, + * allows reuse of callback routines to output + * to headers and source files + */ + FILE *srcfile ; /* output source file */ + FILE *hdrfile ; /* output header file */ }; /** Generate binding between jsapi and netsurf libdom */ -int jsapi_libdom_output(char *outfile, struct genbind_node *genbind_root); +int jsapi_libdom_output(char *outfile, char *hdrfile, struct genbind_node *genbind_root); /** output code block from a node */ void output_code_block(struct binding *binding, struct genbind_node *codelist); diff --git a/src/nsgenbind.c b/src/nsgenbind.c index 0f549f9..1c5100e 100644 --- a/src/nsgenbind.c +++ b/src/nsgenbind.c @@ -30,7 +30,7 @@ static struct options* process_cmdline(int argc, char **argv) return NULL; } - while ((opt = getopt(argc, argv, "vDW::d:I:o:")) != -1) { + while ((opt = getopt(argc, argv, "vDW::d:I:o:h:")) != -1) { switch (opt) { case 'I': options->idlpath = strdup(optarg); @@ -40,6 +40,10 @@ static struct options* process_cmdline(int argc, char **argv) options->outfilename = strdup(optarg); break; + case 'h': + options->hdrfilename = strdup(optarg); + break; + case 'd': options->depfilename = strdup(optarg); break; @@ -58,7 +62,7 @@ static struct options* process_cmdline(int argc, char **argv) default: /* '?' */ fprintf(stderr, - "Usage: %s [-d depfilename] [-I idlpath] [-o filename] inputfile\n", + "Usage: %s [-v] [-D] [-W] [-d depfilename] [-I idlpath] [-o filename] [-h headerfile] inputfile\n", argv[0]); free(options); return NULL; @@ -131,10 +135,17 @@ int main(int argc, char **argv) genbind_ast_dump(genbind_root, 0); } - res = jsapi_libdom_output(options->outfilename, genbind_root); + res = jsapi_libdom_output(options->outfilename, + options->hdrfilename, + genbind_root); if (res != 0) { fprintf(stderr, "Error: output failed with code %d\n", res); - unlink(options->outfilename); + if (options->outfilename != NULL) { + unlink(options->outfilename); + } + if (options->hdrfilename != NULL) { + unlink(options->hdrfilename); + } return res; } diff --git a/src/options.h b/src/options.h index 5ded73a..ca71078 100644 --- a/src/options.h +++ b/src/options.h @@ -9,15 +9,20 @@ #ifndef nsgenbind_options_h #define nsgenbind_options_h +/** global options */ struct options { - char *outfilename; - char *infilename; - char *depfilename; - FILE *depfilehandle; - char *idlpath; - bool verbose; /* verbose processing */ - bool debug; /* debug enabled */ - unsigned int warnings; /* warning flags */ + char *infilename; /**< binding source */ + + char *outfilename; /**< output source file */ + char *hdrfilename; /**< output header file */ + + char *depfilename; /**< dependancy output*/ + FILE *depfilehandle; /**< dependancy file handle */ + char *idlpath; /**< path to IDL files */ + + bool verbose; /**< verbose processing */ + bool debug; /**< debug enabled */ + unsigned int warnings; /**< warning flags */ }; extern struct options *options; diff --git a/test/testrunner.sh b/test/testrunner.sh index 3c8a0ae..1349b84 100755 --- a/test/testrunner.sh +++ b/test/testrunner.sh @@ -28,9 +28,9 @@ for TEST in ${BINDINGTESTS};do echo -n " TEST: ${TESTNAME}......" outline - echo ${GENJSBIND} -D -v -I ${IDLDIR} -o ${BUILDDIR}/test_${TESTNAME}.c ${TEST} >>${LOGFILE} 2>&1 + echo ${GENJSBIND} -D -v -I ${IDLDIR} -o ${BUILDDIR}/test_${TESTNAME}.c -h ${BUILDDIR}/test_${TESTNAME}.h ${TEST} >>${LOGFILE} 2>&1 - ${GENJSBIND} -D -v -I ${IDLDIR} -o ${BUILDDIR}/test_${TESTNAME}.c ${TEST} >>${LOGFILE} 2>&1 + ${GENJSBIND} -D -v -I ${IDLDIR} -o ${BUILDDIR}/test_${TESTNAME}.c -h ${BUILDDIR}/test_${TESTNAME}.h ${TEST} >>${LOGFILE} 2>&1 if [ $? -eq 0 ]; then echo "PASS" |