From 89ee501986c247ddfbad0fb4cfbe4e0f3f486cbb Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 3 Nov 2012 13:48:44 +0000 Subject: improve example and test bindings --- test/data/bindings/window.bnd | 157 +++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 70 deletions(-) (limited to 'test/data/bindings/window.bnd') 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; +%} -- cgit v1.2.3