diff options
author | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-10-05 18:25:42 +0100 |
---|---|---|
committer | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-10-05 18:25:42 +0100 |
commit | 4e570687aa4e70e50d7fa7d4a1818e2d3ced6de2 (patch) | |
tree | 904fed22835be1556094ded54d90a8b6e654bbaf | |
parent | fbd184fe421591851e1224518a3c9430dd37dce8 (diff) | |
download | nsgenbind-4e570687aa4e70e50d7fa7d4a1818e2d3ced6de2.tar.gz nsgenbind-4e570687aa4e70e50d7fa7d4a1818e2d3ced6de2.tar.bz2 |
mid refactor of parameter passing
-rw-r--r-- | src/jsapi-libdom.c | 325 |
1 files changed, 164 insertions, 161 deletions
diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 0ff1c58..8ad0270 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -21,35 +21,31 @@ #define HDR_COMMENT_PREABLE "Generated by nsgenjsapi" struct binding { + struct genbind_node *gb_ast; + struct webidl_node *wi_ast; const char *name; /* name of the binding */ const char *interface; /* webidl interface binding is for */ + FILE *outfile ; /* output file */ }; static int webidl_preamble_cb(struct genbind_node *node, void *ctx) { - FILE *outfile = ctx; - char *txt; - txt = genbind_node_gettext(node); - fprintf(outfile, "%s", txt); - return 0; -} + struct binding *binding = ctx; + + fprintf(binding->outfile, "%s", genbind_node_gettext(node)); -static int -output_preamble(FILE *outfile, struct genbind_node *genbind_ast) -{ - genbind_node_for_each_type(genbind_ast, - GENBIND_NODE_TYPE_PREAMBLE, - webidl_preamble_cb, - outfile); return 0; } + static int webidl_hdrcomments_cb(struct genbind_node *node, void *ctx) { - FILE *outfile = ctx; - char *txt; - txt = genbind_node_gettext(node); - fprintf(outfile, HDR_COMMENT_SEP"%s",txt); + struct binding *binding = ctx; + + fprintf(binding->outfile, + HDR_COMMENT_SEP"%s", + genbind_node_gettext(node)); + return 0; } @@ -62,17 +58,6 @@ static int webidl_hdrcomment_cb(struct genbind_node *node, void *ctx) return 0; } -static int -output_header_comments(FILE *outfile, struct genbind_node *genbind_ast) -{ - fprintf(outfile, "/* "HDR_COMMENT_PREABLE); - genbind_node_for_each_type(genbind_ast, - GENBIND_NODE_TYPE_HDRCOMMENT, - webidl_hdrcomment_cb, - outfile); - fprintf(outfile,"\n */\n\n"); - return 0; -} static int webidl_file_cb(struct genbind_node *node, void *ctx) { @@ -354,80 +339,15 @@ static int webidl_function_body_cb(struct webidl_node *node, void *ctx) JSAPI_SET_RVAL(cx, vp, JSVAL_VOID); */ - fprintf(outfile, "}\n\n"); - - } - return 0; -} - -static int -generate_function_body(FILE *outfile, - const char *interface, - struct webidl_node *webidl_ast) -{ - struct webidl_node *interface_node; - struct webidl_node *members_node; - struct webidl_node *inherit_node; - - - /* find interface in webidl with correct ident attached */ - interface_node = webidl_node_find_type_ident(webidl_ast, - WEBIDL_NODE_TYPE_INTERFACE, - interface); - - if (interface_node == NULL) { - fprintf(stderr, - "Unable to find interface %s in loaded WebIDL\n", - interface); - return -1; - } - - members_node = webidl_node_find(webidl_node_getnode(interface_node), - NULL, - webidl_cmp_node_type, - (void *)WEBIDL_NODE_TYPE_LIST); - while (members_node != NULL) { - - fprintf(outfile,"/**** %s ****/\n", interface); - /* for each function emit a JSAPI_FS()*/ - webidl_node_for_each_type(webidl_node_getnode(members_node), - WEBIDL_NODE_TYPE_OPERATION, - webidl_function_body_cb, - outfile); - - members_node = webidl_node_find(webidl_node_getnode(interface_node), - members_node, - webidl_cmp_node_type, - (void *)WEBIDL_NODE_TYPE_LIST); - } - /* check for inherited nodes and insert them too */ - inherit_node = webidl_node_find(webidl_node_getnode(interface_node), - NULL, - webidl_cmp_node_type, - (void *)WEBIDL_NODE_TYPE_INTERFACE_INHERITANCE); + fprintf(outfile, "}\n\n"); - if (inherit_node != NULL) { - return generate_function_body(outfile, - webidl_node_gettext(inherit_node), - webidl_ast); } - return 0; } -static int -output_function_body(FILE *outfile, - struct binding *binding, - struct webidl_node *webidl_ast) -{ - int res; - - res = generate_function_body(outfile, binding->interface, webidl_ast); - return res; -} static int webidl_property_body_cb(struct webidl_node *node, void *ctx) @@ -552,10 +472,10 @@ output_property_body(FILE *outfile, static int webidl_privatestr_cb(struct genbind_node *node, void *ctx) { - FILE *outfile = ctx; - char *txt; - txt = genbind_node_gettext(node); - fprintf(outfile, " %s;\n", txt); + struct binding *binding = ctx; + + fprintf(binding->outfile, " %s;\n", genbind_node_gettext(node)); + return 0; } @@ -569,16 +489,87 @@ static int webidl_private_cb(struct genbind_node *node, void *ctx) } static int -output_private_declaration(FILE *outfile, - struct binding *binding, - struct genbind_node *genbind_ast, - struct webidl_node *webidl_ast) +output_function_body(struct binding *binding, const char *interface) +{ + struct webidl_node *interface_node; + struct webidl_node *members_node; + struct webidl_node *inherit_node; + + /* find interface in webidl with correct ident attached */ + interface_node = webidl_node_find_type_ident(binding->wi_ast, + WEBIDL_NODE_TYPE_INTERFACE, + interface); + + if (interface_node == NULL) { + fprintf(stderr, + "Unable to find interface %s in loaded WebIDL\n", + interface); + return -1; + } + + members_node = webidl_node_find(webidl_node_getnode(interface_node), + NULL, + webidl_cmp_node_type, + (void *)WEBIDL_NODE_TYPE_LIST); + while (members_node != NULL) { + + fprintf(outfile,"/**** %s ****/\n", interface); + + /* for each function emit a JSAPI_FS()*/ + webidl_node_for_each_type(webidl_node_getnode(members_node), + WEBIDL_NODE_TYPE_OPERATION, + webidl_function_body_cb, + outfile); + + members_node = webidl_node_find(webidl_node_getnode(interface_node), + members_node, + webidl_cmp_node_type, + (void *)WEBIDL_NODE_TYPE_LIST); + } + + /* check for inherited nodes and insert them too */ + inherit_node = webidl_node_find(webidl_node_getnode(interface_node), + NULL, + webidl_cmp_node_type, + (void *)WEBIDL_NODE_TYPE_INTERFACE_INHERITANCE); + + if (inherit_node != NULL) { + return output_function_body(binding, + webidl_node_gettext(inherit_node)); + } + + return 0; +} + +static int +output_jsclass(struct binding *binding) +{ + fprintf(binding->outfile, + "static JSClass jsclass_object =\n" + "{\n" + " \"%s\",\n" + " JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE,\n" + " JS_PropertyStub,\n" + " JS_PropertyStub,\n" + " JS_PropertyStub,\n" + " JS_StrictPropertyStub,\n" + " JS_EnumerateStub,\n" + " (JSResolveOp)jsresove_node,\n" + " JS_ConvertStub,\n" + " jsfinalize_document,\n" + " JSCLASS_NO_OPTIONAL_MEMBERS\n" + "};\n\n", binding->interface); + return 0; +} + +static int +output_private_declaration(struct binding *binding) { struct genbind_node *binding_node; struct genbind_node *type_node; struct genbind_node *node_node; - binding_node = genbind_node_find(genbind_ast, + binding_node = genbind_node_find(binding->g_ast, NULL, genbind_cmp_node_type, (void *)GENBIND_NODE_TYPE_BINDING); @@ -601,7 +592,7 @@ output_private_declaration(FILE *outfile, genbind_node_for_each_type(genbind_node_getnode(binding_node), GENBIND_NODE_TYPE_BINDING_PRIVATE, webidl_private_cb, - outfile); + binding); fprintf(outfile, "};\n\n"); @@ -610,40 +601,44 @@ output_private_declaration(FILE *outfile, } static int -output_jsclass(FILE *outfile, - struct binding *binding, - struct webidl_node *webidl_ast) +output_preamble(struct binding *binding) { - fprintf(outfile, - "static JSClass jsclass_object =\n" - "{\n" - " \"%s\",\n" - " JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE,\n" - " JS_PropertyStub,\n" - " JS_PropertyStub,\n" - " JS_PropertyStub,\n" - " JS_StrictPropertyStub,\n" - " JS_EnumerateStub,\n" - " (JSResolveOp)jsresove_node,\n" - " JS_ConvertStub,\n" - " jsfinalize_document,\n" - " JSCLASS_NO_OPTIONAL_MEMBERS\n" - "};\n\n", binding->interface); + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_PREAMBLE, + webidl_preamble_cb, + binding); return 0; } -static struct binding *binding_new(struct genbind_node *genbind_ast) +static int +output_header_comments(struct binding *binding) +{ + fprintf(binding->outfile, "/* "HDR_COMMENT_PREABLE); + + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_HDRCOMMENT, + webidl_hdrcomment_cb, + binding); + + fprintf(binding->outfile,"\n */\n\n"); + return 0; +} + +static struct binding * +binding_new(char *outfilename, struct genbind_node *genbind_ast) { struct binding *nb; struct genbind_node *binding_node; struct genbind_node *ident_node; struct genbind_node *interface_node; + FILE *outfile ; /* output file */ + struct webidl_node *webidl_ast = NULL; + int res; binding_node = genbind_node_find(genbind_ast, NULL, genbind_cmp_node_type, (void *)GENBIND_NODE_TYPE_BINDING); - if (binding_node == NULL) { return NULL; } @@ -652,7 +647,6 @@ static struct binding *binding_new(struct genbind_node *genbind_ast) NULL, genbind_cmp_node_type, (void *)GENBIND_NODE_TYPE_IDENT); - if (ident_node == NULL) { return NULL; } @@ -661,85 +655,94 @@ static struct binding *binding_new(struct genbind_node *genbind_ast) NULL, genbind_cmp_node_type, (void *)GENBIND_NODE_TYPE_BINDING_INTERFACE); - if (interface_node == NULL) { return NULL; } - nb = calloc(1, sizeof(struct binding)); - - nb->name = genbind_node_gettext(ident_node); - nb->interface = genbind_node_gettext(interface_node); - - return nb; -} - -int jsapi_libdom_output(char *outfilename, struct genbind_node *genbind_ast) -{ - FILE *outfile = NULL; - struct webidl_node *webidl_ast = NULL; - int res; - struct binding *binding; - /* walk ast and load any web IDL files required */ res = read_webidl(genbind_ast, &webidl_ast); if (res != 0) { fprintf(stderr, "Error reading Web IDL files\n"); - return 5; + return NULL; } - /* get general binding information used in output */ - binding = binding_new(genbind_ast); - /* open output file */ if (outfilename == NULL) { outfile = stdout; } else { outfile = fopen(outfilename, "w"); } - - if (!outfile) { + if (outfile == NULL) { fprintf(stderr, "Error opening output %s: %s\n", outfilename, strerror(errno)); - return 4; + return NULL; } - output_header_comments(outfile, genbind_ast); + nb = calloc(1, sizeof(struct binding)); - output_preamble(outfile, genbind_ast); + nb->gb_ast = genbind_ast; + nb->wi_ast = webidl_ast; + nb->name = genbind_node_gettext(ident_node); + nb->interface = genbind_node_gettext(interface_node); + nb->outfile = outfile; + + return nb; +} + + +int jsapi_libdom_output(char *outfilename, struct genbind_node *genbind_ast) +{ + int res; + struct binding *binding; + + /* get general binding information used in output */ + binding = binding_new(outfilename, genbind_ast); + if (binding == NULL) { + return 4; + } - res = output_private_declaration(outfile, binding, genbind_ast, webidl_ast); + res = output_header_comments(binding); if (res) { return 5; } - res = output_jsclass(outfile, binding, webidl_ast); + res = output_preamble(binding); if (res) { - return 5; + return 6; } - res = output_function_body(outfile, binding, webidl_ast); + res = output_private_declaration(binding); if (res) { - return 5; + return 7; } - res = output_property_body(outfile, binding, webidl_ast); + res = output_jsclass(binding); if (res) { - return 6; + return 8; } - res = output_function_spec(outfile, binding, webidl_ast); + res = output_function_body(binding, binding->interface); if (res) { - return 7; + return 9; + } + + res = output_property_body(binding); + if (res) { + return 10; + } + + res = output_function_spec(binding); + if (res) { + return 11; } res = output_property_spec(outfile, binding, webidl_ast); if (res) { - return 8; + return 12; } - fclose(outfile); + fclose(binding->outfile); return 0; } |