diff options
author | Vincent Sanders <vince@kyllikki.org> | 2012-10-29 13:40:13 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2012-10-29 13:40:13 +0000 |
commit | ade96212575df58bc0f714168a87d9975eea273a (patch) | |
tree | 7ff0a1888948e6e59e00eecc00d19a21fd040d9d | |
parent | 2931cd7597b8a41941e5cc3a1f869d36446c6006 (diff) | |
download | nsgenbind-ade96212575df58bc0f714168a87d9975eea273a.tar.gz nsgenbind-ade96212575df58bc0f714168a87d9975eea273a.tar.bz2 |
add internal and API to binding language
split out class operation, initialisation and new generation
-rw-r--r-- | src/jsapi-libdom.c | 54 | ||||
-rw-r--r-- | src/nsgenbind-ast.c | 8 | ||||
-rw-r--r-- | src/nsgenbind-ast.h | 2 | ||||
-rw-r--r-- | src/nsgenbind-lexer.l | 4 | ||||
-rw-r--r-- | src/nsgenbind-parser.y | 43 | ||||
-rw-r--r-- | test/data/bindings/window.bnd | 148 |
6 files changed, 250 insertions, 9 deletions
diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 866f8be..30c8d26 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -365,7 +365,6 @@ static int webidl_private_assign_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, @@ -382,7 +381,7 @@ static int webidl_private_assign_cb(struct genbind_node *node, void *ctx) static int -output_con_de_structors(struct binding *binding) +output_class_operations(struct binding *binding) { int res = 0; struct genbind_node *binding_node; @@ -416,6 +415,23 @@ output_con_de_structors(struct binding *binding) "\t*objp = NULL;\n" "\treturn JS_TRUE;\n" "}\n\n"); + return res; +} + +static int +output_class_init(struct binding *binding) +{ + int res = 0; + struct genbind_node *binding_node; + + binding_node = genbind_node_find(binding->gb_ast, + NULL, + genbind_cmp_node_type, + (void *)GENBIND_NODE_TYPE_BINDING); + + if (binding_node == NULL) { + return -1; + } /* class Initialisor */ fprintf(binding->outfile, @@ -436,6 +452,23 @@ output_con_de_structors(struct binding *binding) "\treturn jsobject;\n" "}\n\n", binding->interface); + return res; +} + +static int +output_class_new(struct binding *binding) +{ + int res = 0; + struct genbind_node *binding_node; + + binding_node = genbind_node_find(binding->gb_ast, + NULL, + genbind_cmp_node_type, + (void *)GENBIND_NODE_TYPE_BINDING); + + if (binding_node == NULL) { + return -1; + } /* constructor */ @@ -634,6 +667,11 @@ output_private_declaration(struct binding *binding) webidl_private_cb, binding); + genbind_node_for_each_type(genbind_node_getnode(binding_node), + GENBIND_NODE_TYPE_BINDING_INTERNAL, + webidl_private_cb, + binding); + fprintf(binding->outfile, "};\n\n"); @@ -782,11 +820,21 @@ int jsapi_libdom_output(char *outfilename, struct genbind_node *genbind_ast) return 12; } - res = output_con_de_structors(binding); + res = output_class_operations(binding); if (res) { return 13; } + res = output_class_init(binding); + if (res) { + return 14; + } + + res = output_class_new(binding); + if (res) { + return 15; + } + fclose(binding->outfile); return 0; diff --git a/src/nsgenbind-ast.c b/src/nsgenbind-ast.c index 646548b..e192d74 100644 --- a/src/nsgenbind-ast.c +++ b/src/nsgenbind-ast.c @@ -195,7 +195,9 @@ struct genbind_node *genbind_node_getnode(struct genbind_node *node) case GENBIND_NODE_TYPE_HDRCOMMENT: case GENBIND_NODE_TYPE_BINDING: case GENBIND_NODE_TYPE_BINDING_PRIVATE: + case GENBIND_NODE_TYPE_BINDING_INTERNAL: case GENBIND_NODE_TYPE_OPERATION: + case GENBIND_NODE_TYPE_API: return node->r.node; default: @@ -233,12 +235,18 @@ static const char *genbind_node_type_to_str(enum genbind_node_type type) case GENBIND_NODE_TYPE_BINDING_PRIVATE: return "Private"; + case GENBIND_NODE_TYPE_BINDING_INTERNAL: + return "Internal"; + case GENBIND_NODE_TYPE_BINDING_INTERFACE: return "Interface"; case GENBIND_NODE_TYPE_OPERATION: return "Operation"; + case GENBIND_NODE_TYPE_API: + return "API"; + case GENBIND_NODE_TYPE_CBLOCK: return "CBlock"; diff --git a/src/nsgenbind-ast.h b/src/nsgenbind-ast.h index b51e6b8..0006153 100644 --- a/src/nsgenbind-ast.h +++ b/src/nsgenbind-ast.h @@ -20,7 +20,9 @@ enum genbind_node_type { GENBIND_NODE_TYPE_BINDING, GENBIND_NODE_TYPE_BINDING_TYPE, GENBIND_NODE_TYPE_BINDING_PRIVATE, + GENBIND_NODE_TYPE_BINDING_INTERNAL, GENBIND_NODE_TYPE_BINDING_INTERFACE, + GENBIND_NODE_TYPE_API, GENBIND_NODE_TYPE_OPERATION, }; diff --git a/src/nsgenbind-lexer.l b/src/nsgenbind-lexer.l index 4bdc803..040d7e9 100644 --- a/src/nsgenbind-lexer.l +++ b/src/nsgenbind-lexer.l @@ -94,8 +94,12 @@ type return TOK_TYPE; private return TOK_PRIVATE; +internal return TOK_INTERNAL; + operation return TOK_OPERATION; +api return TOK_API; + {cblockopen} BEGIN(cblock); {identifier} { diff --git a/src/nsgenbind-parser.y b/src/nsgenbind-parser.y index 500f4b8..ea0a625 100644 --- a/src/nsgenbind-parser.y +++ b/src/nsgenbind-parser.y @@ -42,11 +42,13 @@ char *errtxt; %token TOK_HDR_COMMENT %token TOK_PREAMBLE +%token TOK_API %token TOK_BINDING %token TOK_OPERATION %token TOK_INTERFACE %token TOK_TYPE %token TOK_PRIVATE +%token TOK_INTERNAL %token <text> TOK_IDENTIFIER %token <text> TOK_STRING_LITERAL @@ -65,8 +67,10 @@ char *errtxt; %type <node> BindingArg %type <node> Type %type <node> Private +%type <node> Internal %type <node> Interface %type <node> Operation +%type <node> Api %% @@ -107,6 +111,8 @@ Statement Binding | Operation + | + Api ; /* [3] load a web IDL file */ @@ -162,12 +168,25 @@ Operation TOK_OPERATION TOK_IDENTIFIER CBlock { $$ = genbind_new_node(GENBIND_NODE_TYPE_OPERATION, - NULL, - genbind_new_node(GENBIND_NODE_TYPE_IDENT, - genbind_new_node(GENBIND_NODE_TYPE_CBLOCK, - NULL, - $3), - $2)); + NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, + genbind_new_node(GENBIND_NODE_TYPE_CBLOCK, + NULL, + $3), + $2)); + } + +Api + : + TOK_API TOK_IDENTIFIER CBlock + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_API, + NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, + genbind_new_node(GENBIND_NODE_TYPE_CBLOCK, + NULL, + $3), + $2)); } Binding @@ -196,6 +215,8 @@ BindingArg | Private | + Internal + | Interface ; @@ -217,6 +238,16 @@ Private } ; +Internal + : + TOK_INTERNAL TOK_STRING_LITERAL TOK_IDENTIFIER ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_INTERNAL, NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, + genbind_new_node(GENBIND_NODE_TYPE_STRING, NULL, $2), $3)); + } + ; + Interface : TOK_INTERFACE TOK_IDENTIFIER ';' diff --git a/test/data/bindings/window.bnd b/test/data/bindings/window.bnd new file mode 100644 index 0000000..08493f7 --- /dev/null +++ b/test/data/bindings/window.bnd @@ -0,0 +1,148 @@ +/* binding to generate window */ + +#include "dom.bnd" + +webidlfile "html.idl"; + +hdrcomment "Part of NetSurf Project"; + +preamble %{ + +#include <dom/dom.h> + +#include "utils/config.h" +#include "utils/log.h" + +#include "javascript/jsapi.h" +#include "javascript/jsapi/binding.h" + +%} + +operation confirm %{ + warn_user(message, NULL); +%} + +operation alert %{ + warn_user(message, NULL); +%} + +operation prompt %{ + warn_user(message, NULL); +%} + +api init %{ + JSObject *window = NULL; + JSObject *proto; + + window = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL); + if (window == NULL) { + return NULL; + } + + /** @todo reconsider global object handling. future + * editions of spidermonkey appear to be removing the + * idea of a global so we probably need to handle + * global object references internally + */ + + /* set the contexts global */ + JS_SetGlobalObject(cx, window); + + /* Populate the global object with the standard globals, like + * Object and Array. + */ + if (!JS_InitStandardClasses(cx, window)) { + return NULL; + } + + /* Initialises all the user javascript classes to make their + * prototypes available. + */ + /** @todo should we be managing these prototype objects ourselves */ + proto = jsapi_InitClass_Document(cx, window); + if (proto == NULL) { + return NULL; + } + + return window; +%} + +api new %{ + struct jsclass_private *private; + + /* @todo sort out windows that are not globals */ + assert(parent == NULL); + + /* create private data */ + private = malloc(sizeof(struct jsclass_private)); + if (private == NULL) { + return NULL; + } + private->bw = bw; + private->htmlc = htmlc; + + + /* instantiate the subclasses off the window global */ + private->document_obj = jsapi_new_Document(cx, + NULL, + window, + htmlc->document, + htmlc); + if (private->document_obj == NULL) { + free(private); + return NULL; + } + +/* + private->navigator_obj = jsapi_new_Navigator(cx, window); + if (private->navigator_obj == NULL) { + free(private); + return NULL; + } +*/ + /** @todo forms, history, location */ + + private->console_obj = jsapi_new_Console(cx, window); + if (private->console_obj == NULL) { + free(private); + return NULL; + } + + /* private pointer to browsing context */ + if (!JS_SetPrivate(cx, window, private)) + return NULL; + + /* functions */ + if (!JS_DefineFunctions(cx, window, jsfunctions_window)) { + return NULL; + } + + /* properties */ + if (!JS_DefineProperties(cx, window, jsproperties_window)) + return NULL; + + + LOG(("Created new window object %p", window)); + + return window; +%} + + +binding window { + type js_libdom; /* the binding type */ + + interface Window; /* Web IDL interface to generate */ + + /* private are parameters to constructor stored in private + * context structure. + * + * internal are value stored in private context structure but not + * passed to constructor. + */ + private "struct browser_window *" bw; + private "struct html_content *" htmlc; + internal "JSObject *" document; + internal "JSObject *" navigator; + internal "JSObject *" console; + +} |