diff options
author | Vincent Sanders <vince@kyllikki.org> | 2012-11-03 13:48:44 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2012-11-03 13:48:44 +0000 |
commit | 89ee501986c247ddfbad0fb4cfbe4e0f3f486cbb (patch) | |
tree | a0317e5a373f3fea28927bc84a3a0d97fca64c91 | |
parent | 408787939032719568b43d13fd0ea05d25d52781 (diff) | |
download | nsgenbind-89ee501986c247ddfbad0fb4cfbe4e0f3f486cbb.tar.gz nsgenbind-89ee501986c247ddfbad0fb4cfbe4e0f3f486cbb.tar.bz2 |
improve example and test bindings
-rw-r--r-- | doc/example.bnd | 19 | ||||
-rw-r--r-- | test/data/bindings/window.bnd | 157 |
2 files changed, 100 insertions, 76 deletions
diff --git a/doc/example.bnd b/doc/example.bnd index f2f81fb..dfd0bb5 100644 --- a/doc/example.bnd +++ b/doc/example.bnd @@ -173,7 +173,7 @@ setter baz %{ * JSObject *jsapi_InitClass_HTMLElement(JSContext *cx, JSObject *parent) */ api init %{ - %} +%} /* implementation of the c instance creation * @@ -199,11 +199,11 @@ api init %{ * */ api new %{ - %} +%} /* additional code in the instance finalise operation. * - * The body is copied verbatum into the output + * The body is copied verbatim into the output * * Prototype is * void jsclass_finalize(JSContext *cx, JSObject *obj) @@ -211,7 +211,7 @@ api new %{ * private is available (if appropriate) and freed after the body */ api finalise %{ - %} +%} /* resolver code * @@ -219,11 +219,18 @@ api finalise %{ * JSResolveOp with JSCLASS_NEW_RESOLVE specified and must provide a * complete implementation. * - * The body is copied verbatum into the output + * The body is copied verbatim into the output * * Prototype is: * JSBool jsclass_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp) * */ api resolve %{ - %} +%} + +/* mark the generated interface as a a global object + * + * The body is discarded. + */ +api global %{ +%}
\ No newline at end of file diff --git a/test/data/bindings/window.bnd b/test/data/bindings/window.bnd index 08493f7..956932c 100644 --- a/test/data/bindings/window.bnd +++ b/test/data/bindings/window.bnd @@ -18,24 +18,32 @@ preamble %{ %} -operation confirm %{ - warn_user(message, NULL); -%} - -operation alert %{ - warn_user(message, NULL); -%} +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 but are considered for property + * getters/setters. + */ + private "struct browser_window *" bw; + private "struct html_content *" htmlc; + internal "JSObject *" document; + internal "JSObject *" navigator; + internal "JSObject *" console; + internal "JSObject *" location; +} -operation prompt %{ - warn_user(message, NULL); -%} api init %{ - JSObject *window = NULL; - JSObject *proto; + JSObject *user_proto; - window = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL); - if (window == NULL) { + prototype = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL); + if (prototype == NULL) { return NULL; } @@ -46,103 +54,112 @@ api init %{ */ /* set the contexts global */ - JS_SetGlobalObject(cx, window); + JS_SetGlobalObject(cx, prototype); /* Populate the global object with the standard globals, like * Object and Array. */ - if (!JS_InitStandardClasses(cx, window)) { + if (!JS_InitStandardClasses(cx, prototype)) { return NULL; } + /* add functions to prototype */ + if (!JS_DefineFunctions(cx, prototype, jsclass_functions)) { + return NULL; + } + + /* add properties to prototype */ + if (!JS_DefineProperties(cx, prototype, jsclass_properties)) + 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) { + user_proto = jsapi_InitClass_Document(cx, prototype); + if (user_proto == NULL) { return NULL; } - return window; -%} + user_proto = jsapi_InitClass_Navigator(cx, prototype); + if (user_proto == NULL) { + return NULL; + } -api new %{ - struct jsclass_private *private; + user_proto = jsapi_InitClass_Location(cx, prototype); + if (user_proto == NULL) { + return NULL; + } - /* @todo sort out windows that are not globals */ - assert(parent == NULL); + user_proto = jsapi_InitClass_Console(cx, prototype); + if (user_proto == NULL) { + return NULL; + } - /* create private data */ - private = malloc(sizeof(struct jsclass_private)); - if (private == NULL) { + user_proto = jsapi_InitClass_HTMLElement(cx, prototype); + if (user_proto == NULL) { return NULL; } - private->bw = bw; - private->htmlc = htmlc; +%} + +api new %{ + /* @todo sort out windows that are not globals */ + assert(parent == NULL); + + /* the window object is the global so its prototype *is* the instance */ + newobject = prototype; /* instantiate the subclasses off the window global */ - private->document_obj = jsapi_new_Document(cx, - NULL, - window, - htmlc->document, - htmlc); - if (private->document_obj == NULL) { + private->document = jsapi_new_Document(cx, + NULL, + newobject, + htmlc->document, + htmlc); + if (private->document == NULL) { free(private); return NULL; } -/* - private->navigator_obj = jsapi_new_Navigator(cx, window); - if (private->navigator_obj == NULL) { + private->navigator = jsapi_new_Navigator(cx, NULL, newobject); + if (private->navigator == NULL) { free(private); return NULL; } -*/ - /** @todo forms, history, location */ - private->console_obj = jsapi_new_Console(cx, window); - if (private->console_obj == NULL) { + private->location = jsapi_new_Location(cx, NULL, newobject, bw); + if (private->location == 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)) { + private->console = jsapi_new_Console(cx, NULL, newobject); + if (private->console == NULL) { + free(private); return NULL; } - /* properties */ - if (!JS_DefineProperties(cx, window, jsproperties_window)) - return NULL; + /** @todo forms, history */ + LOG(("Created new window object %p", newobject)); +%} - LOG(("Created new window object %p", window)); +operation confirm %{ + warn_user(message, NULL); +%} - return window; +operation alert %{ + warn_user(message, NULL); %} +operation prompt %{ + warn_user(message, NULL); +%} -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; +getter window %{ + jsret = obj; +%} -} +getter self %{ + jsret = obj; +%} |