From 257f535bd18de9a6fc4e9d90303939706a783732 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 14 Nov 2012 02:02:02 +0000 Subject: geneate constants as fixed value properties on the prototype --- src/Makefile | 2 +- src/jsapi-libdom-const.c | 195 ++++++++++++++++++++++++++++++++++++++++++++ src/jsapi-libdom-property.c | 137 ------------------------------- src/jsapi-libdom.c | 2 + src/jsapi-libdom.h | 2 + 5 files changed, 200 insertions(+), 138 deletions(-) create mode 100644 src/jsapi-libdom-const.c diff --git a/src/Makefile b/src/Makefile index e2f2eab..e151d8f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CFLAGS := $(CFLAGS) -I$(BUILDDIR) -Isrc/ -g # Sources in this directory -DIR_SOURCES := nsgenbind.c webidl-ast.c nsgenbind-ast.c jsapi-libdom.c jsapi-libdom-operator.c jsapi-libdom-property.c +DIR_SOURCES := nsgenbind.c webidl-ast.c nsgenbind-ast.c jsapi-libdom.c jsapi-libdom-operator.c jsapi-libdom-property.c jsapi-libdom-const.c SOURCES := $(SOURCES) $(BUILDDIR)/nsgenbind-parser.c $(BUILDDIR)/nsgenbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c diff --git a/src/jsapi-libdom-const.c b/src/jsapi-libdom-const.c new file mode 100644 index 0000000..ac728c7 --- /dev/null +++ b/src/jsapi-libdom-const.c @@ -0,0 +1,195 @@ +/* const property generation + * + * This file is part of nsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#include +#include +#include +#include +#include + +#include "options.h" +#include "nsgenbind-ast.h" +#include "webidl-ast.h" +#include "jsapi-libdom.h" + +static int output_cast_literal(struct binding *binding, + struct webidl_node *node) +{ + struct webidl_node *type_node = NULL; + struct webidl_node *literal_node = NULL; + struct webidl_node *type_base = NULL; + enum webidl_type webidl_arg_type; + + type_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_TYPE); + + type_base = webidl_node_find_type(webidl_node_getnode(type_node), + NULL, + WEBIDL_NODE_TYPE_TYPE_BASE); + + webidl_arg_type = webidl_node_getint(type_base); + + switch (webidl_arg_type) { + + case WEBIDL_TYPE_BOOL: + /* JSBool */ + literal_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_LITERAL_BOOL); + fprintf(binding->outfile, "BOOLEAN_TO_JSVAL(JS_FALSE)"); + break; + + case WEBIDL_TYPE_FLOAT: + case WEBIDL_TYPE_DOUBLE: + /* double */ + literal_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_LITERAL_FLOAT); + fprintf(binding->outfile, "DOUBLE_TO_JSVAL(0.0)"); + break; + + case WEBIDL_TYPE_LONG: + /* int32_t */ + literal_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_LITERAL_INT); + fprintf(binding->outfile, + "INT_TO_JSVAL(%d)", + webidl_node_getint(literal_node)); + break; + + case WEBIDL_TYPE_SHORT: + /* int16_t */ + literal_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_LITERAL_INT); + fprintf(binding->outfile, + "INT_TO_JSVAL(%d)", + webidl_node_getint(literal_node)); + break; + + + case WEBIDL_TYPE_STRING: + case WEBIDL_TYPE_BYTE: + case WEBIDL_TYPE_OCTET: + case WEBIDL_TYPE_LONGLONG: + case WEBIDL_TYPE_SEQUENCE: + case WEBIDL_TYPE_OBJECT: + case WEBIDL_TYPE_DATE: + case WEBIDL_TYPE_VOID: + case WEBIDL_TYPE_USER: + default: + WARN(WARNING_UNIMPLEMENTED, "types not allowed as literal"); + break; /* @todo these types are not allowed here */ + } + + return 0; +} + +static int webidl_const_define_cb(struct webidl_node *node, void *ctx) +{ + struct binding *binding = ctx; + struct webidl_node *ident_node; + + ident_node = webidl_node_find_type(webidl_node_getnode(node), + NULL, + WEBIDL_NODE_TYPE_IDENT); + if (ident_node == NULL) { + /* Broken AST - must have ident */ + return 1; + } + + fprintf(binding->outfile, + "\tJS_DefineProperty(cx,\n" + "\t\tprototype,\n" + "\t\t\"%s\",\n" + "\t\t", + webidl_node_gettext(ident_node)); + + output_cast_literal(binding, node); + + fprintf(binding->outfile, + ",\n" + "\t\tJS_PropertyStub,\n" + "\t\tJS_StrictPropertyStub,\n" + "\t\tJSPROP_READONLY | JSPROP_ENUMERATE | JSPROP_PERMANENT);\n\n"); + + return 0; + +} + + +/* callback to emit implements property spec */ +static int webidl_const_spec_implements_cb(struct webidl_node *node, void *ctx) +{ + struct binding *binding = ctx; + + return output_const_defines(binding, webidl_node_gettext(node)); +} + +int +output_const_defines(struct binding *binding, const char *interface) +{ + struct webidl_node *interface_node; + struct webidl_node *members_node; + struct webidl_node *inherit_node; + int res = 0; + + /* 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; + } + + /* generate property entries for each list (partial interfaces) */ + members_node = webidl_node_find_type(webidl_node_getnode(interface_node), + NULL, + WEBIDL_NODE_TYPE_LIST); + + while (members_node != NULL) { + fprintf(binding->outfile,"\t/**** %s ****/\n", interface); + + /* for each const emit a property define */ + webidl_node_for_each_type(webidl_node_getnode(members_node), + WEBIDL_NODE_TYPE_CONST, + webidl_const_define_cb, + binding); + + + members_node = webidl_node_find_type(webidl_node_getnode(interface_node), + members_node, + 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) { + res = output_const_defines(binding, + webidl_node_gettext(inherit_node)); + } + + if (res == 0) { + res = webidl_node_for_each_type(webidl_node_getnode(interface_node), + WEBIDL_NODE_TYPE_INTERFACE_IMPLEMENTS, + webidl_const_spec_implements_cb, + binding); + } + + return res; +} diff --git a/src/jsapi-libdom-property.c b/src/jsapi-libdom-property.c index 3e66b9e..e87620e 100644 --- a/src/jsapi-libdom-property.c +++ b/src/jsapi-libdom-property.c @@ -17,27 +17,6 @@ #include "webidl-ast.h" #include "jsapi-libdom.h" -static int webidl_const_spec_cb(struct webidl_node *node, void *ctx) -{ - struct binding *binding = ctx; - struct webidl_node *ident_node; - - ident_node = webidl_node_find(webidl_node_getnode(node), - NULL, - webidl_cmp_node_type, - (void *)WEBIDL_NODE_TYPE_IDENT); - - if (ident_node == NULL) { - /* broken AST - constants must have an identifier */ - return 1; - } - - fprintf(binding->outfile, - "\tJSAPI_PS_RO(%s, 0, JSPROP_ENUMERATE | JSPROP_SHARED),\n", - webidl_node_gettext(ident_node)); - - return 0; -} static int webidl_property_spec_cb(struct webidl_node *node, void *ctx) { @@ -119,13 +98,6 @@ generate_property_spec(struct binding *binding, const char *interface) webidl_property_spec_cb, binding); - /* for each const emit a property getter */ - webidl_node_for_each_type(webidl_node_getnode(members_node), - WEBIDL_NODE_TYPE_CONST, - webidl_const_spec_cb, - binding); - - members_node = webidl_node_find_type(webidl_node_getnode(interface_node), members_node, WEBIDL_NODE_TYPE_LIST); @@ -267,82 +239,6 @@ static int output_return(struct binding *binding, return 0; } -static int output_return_literal(struct binding *binding, - struct webidl_node *node) -{ - struct webidl_node *type_node = NULL; - struct webidl_node *literal_node = NULL; - struct webidl_node *type_base = NULL; - enum webidl_type webidl_arg_type; - - type_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_TYPE); - - type_base = webidl_node_find_type(webidl_node_getnode(type_node), - NULL, - WEBIDL_NODE_TYPE_TYPE_BASE); - - webidl_arg_type = webidl_node_getint(type_base); - - switch (webidl_arg_type) { - - case WEBIDL_TYPE_BOOL: - /* JSBool */ - literal_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_LITERAL_BOOL); - fprintf(binding->outfile, - "\tJS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(JS_FALSE));\n"); - break; - - case WEBIDL_TYPE_FLOAT: - case WEBIDL_TYPE_DOUBLE: - /* double */ - literal_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_LITERAL_FLOAT); - fprintf(binding->outfile, - "\tJS_SET_RVAL(cx, vp, DOUBLE_TO_JSVAL(0.0));\n"); - break; - - case WEBIDL_TYPE_LONG: - /* int32_t */ - literal_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_LITERAL_INT); - fprintf(binding->outfile, - "\tJS_SET_RVAL(cx, vp, INT_TO_JSVAL(%d));\n", - webidl_node_getint(literal_node)); - break; - - case WEBIDL_TYPE_SHORT: - /* int16_t */ - literal_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_LITERAL_INT); - fprintf(binding->outfile, - "\tJS_SET_RVAL(cx, vp, INT_TO_JSVAL(%d));\n", - webidl_node_getint(literal_node)); - break; - - - case WEBIDL_TYPE_STRING: - case WEBIDL_TYPE_BYTE: - case WEBIDL_TYPE_OCTET: - case WEBIDL_TYPE_LONGLONG: - case WEBIDL_TYPE_SEQUENCE: - case WEBIDL_TYPE_OBJECT: - case WEBIDL_TYPE_DATE: - case WEBIDL_TYPE_VOID: - case WEBIDL_TYPE_USER: - default: - WARN(WARNING_UNIMPLEMENTED, "types not allowed as literal"); - break; /* @todo these types are not allowed here */ - } - - return 0; -} /* generate variable declaration of the correct type with appropriate default */ @@ -584,33 +480,6 @@ static int webidl_property_body_cb(struct webidl_node *node, void *ctx) return output_property_getter(binding, node, webidl_node_gettext(ident_node)); } -static int webidl_const_body_cb(struct webidl_node *node, void *ctx) -{ - struct binding *binding = ctx; - struct webidl_node *ident_node; - - ident_node = webidl_node_find_type(webidl_node_getnode(node), - NULL, - WEBIDL_NODE_TYPE_IDENT); - if (ident_node == NULL) { - /* Broken AST - must have ident */ - return 1; - } - - fprintf(binding->outfile, - "static JSBool JSAPI_PROPERTYGET(%s, JSContext *cx, JSObject *obj, jsval *vp)\n" - "{\n", - webidl_node_gettext(ident_node)); - - output_return_literal(binding, node); - - fprintf(binding->outfile, - "\treturn JS_TRUE;\n" - "}\n\n"); - - return 0; - -} /* callback to emit implements property bodys */ static int webidl_implements_cb(struct webidl_node *node, void *ctx) @@ -654,12 +523,6 @@ output_property_body(struct binding *binding, const char *interface) webidl_property_body_cb, binding); - /* for each const emit a property getter */ - webidl_node_for_each_type(webidl_node_getnode(members_node), - WEBIDL_NODE_TYPE_CONST, - webidl_const_body_cb, - binding); - members_node = webidl_node_find_type(webidl_node_getnode(interface_node), members_node, diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 5c918de..187d511 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -279,6 +279,8 @@ output_class_init(struct binding *binding) binding->interface); } + output_const_defines(binding, binding->interface); + fprintf(binding->outfile, "\treturn prototype;\n" "}\n\n"); diff --git a/src/jsapi-libdom.h b/src/jsapi-libdom.h index cca6b80..cb9f40d 100644 --- a/src/jsapi-libdom.h +++ b/src/jsapi-libdom.h @@ -49,6 +49,8 @@ int output_function_spec(struct binding *binding); int output_property_spec(struct binding *binding); int output_property_body(struct binding *binding, const char *interface); +int output_const_defines(struct binding *binding, const char *interface); + /** Generate binding between jsapi and netsurf libdom */ int jsapi_libdom_output(char *outfile, struct genbind_node *genbind_root); -- cgit v1.2.3