[jsapigen-devel] [PATCH 3/3] Add basic usage examples
Status: Beta
Brought to you by:
tdz
From: Vincent S. <vi...@de...> - 2012-07-02 11:50:26
|
From: Vincent Sanders <vi...@co...> Add some examples partially based on examples from the manual. The common startup code may be compiled on any spidermonkey API I would have MIT licenced them all but I did cut and paste from the manual examples so I had to stick to your licence. Signed-off-by: Vincent Sanders <vi...@ky...> --- examples/Makefile | 31 +++++++++++ examples/hello-world.j | 43 +++++++++++++++ examples/hello-world.js | 1 + examples/main.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++ examples/number.j | 42 +++++++++++++++ examples/number.js | 3 ++ examples/object-con.j | 90 +++++++++++++++++++++++++++++++ examples/object-con.js | 5 ++ examples/retval.j | 50 +++++++++++++++++ examples/retval.js | 2 + examples/square.j | 39 ++++++++++++++ examples/square.js | 1 + examples/window.j | 90 +++++++++++++++++++++++++++++++ examples/window.js | 5 ++ 14 files changed, 538 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/hello-world.j create mode 100644 examples/hello-world.js create mode 100644 examples/main.c create mode 100644 examples/number.j create mode 100644 examples/number.js create mode 100644 examples/object-con.j create mode 100644 examples/object-con.js create mode 100644 examples/retval.j create mode 100644 examples/retval.js create mode 100644 examples/square.j create mode 100644 examples/square.js create mode 100644 examples/window.j create mode 100644 examples/window.js diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..d64cce5 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,31 @@ +#!/bin/make + +CFLAGS+=-DXP_UNIX -I/usr/include/mozjs -g3 -O2 -Wall -Wno-unused-variable +LDFLAGS+=-lmozjs + +.PHONY: all clean + +EXAMPLES:=hello-world number retval square object-con window + +EXAMPLE_OBJ:=$(addsuffix .o, ${EXAMPLES}) +EXAMPLE_C:=$(addsuffix .c, ${EXAMPLES}) + +all:${EXAMPLES} + +%.c:%.j + jsapigen -f $< -o $@ + +number:main.o number.o + +retval:main.o retval.o + +hello-world:main.o hello-world.o + +square:main.o square.o + +object-con:main.o object-con.o + +window:main.o window.o + +clean: + ${RM} main.o ${EXAMPLES} ${EXAMPLE_OBJ} ${EXAMPLE_C} diff --git a/examples/hello-world.j b/examples/hello-world.j new file mode 100644 index 0000000..38b7469 --- /dev/null +++ b/examples/hello-world.j @@ -0,0 +1,43 @@ +/* + * jsapi hello-world example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ +#include <stdio.h> +#include <jsapi.h> +#include <stdlib.h> + +static void +hello_world(void) +{ + printf("Hello world!\n"); +} + +%} + +function void js_hello_world : hello_world <static>; + +%< + +const char * jsfile = "hello-world.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + return JS_DefineFunctions(cx, global, jj_fs); +} + +%> diff --git a/examples/hello-world.js b/examples/hello-world.js new file mode 100644 index 0000000..ea35196 --- /dev/null +++ b/examples/hello-world.js @@ -0,0 +1 @@ +js_hello_world(); diff --git a/examples/main.c b/examples/main.c new file mode 100644 index 0000000..75b3941 --- /dev/null +++ b/examples/main.c @@ -0,0 +1,136 @@ +/* + * jsapi examples common code + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <jsapi.h> +#include <stdlib.h> + +/* each example provides these two */ +extern const char * jsfile; +extern JSBool setup_example(JSContext *cx, JSObject *global); + +static void reportError(JSContext *cx, const char *message, JSErrorReport *report) +{ + fprintf(stderr, "%s:%u:%s\n", + report->filename ? report->filename : "<no filename>", + (unsigned int) report->lineno, + message); +} + +int +main(int argc, char **argv) +{ + /* The class of the global object. */ + static JSClass global_class = { + "global", + JSCLASS_GLOBAL_FLAGS, + JS_PropertyStub, + JS_PropertyStub, + JS_PropertyStub, +#if JS_VERSION <= 180 + JS_PropertyStub, +#else + JS_StrictPropertyStub, +#endif + JS_EnumerateStub, + JS_ResolveStub, + JS_ConvertStub, + JS_FinalizeStub, + JSCLASS_NO_OPTIONAL_MEMBERS + }; + + + JSRuntime *rt; + JSContext *cx; + JSObject *global; + JSScript *script; + jsval rval; + + /* Create an instance of the engine */ +#if JS_VERSION >= 180 + JS_SetCStringsAreUTF8(); +#endif + + rt = JS_NewRuntime(1024*1024); + + if (!rt) { + exit(EXIT_FAILURE); + } + + /* Create an execution context */ + + cx = JS_NewContext(rt, 8192); + + if (!cx) { + exit(EXIT_FAILURE); + } + + JS_SetVersion(cx, JSVERSION_LATEST); + JS_SetErrorReporter(cx, reportError); + + /* Create a global object and a set of standard objects */ + +#if JS_VERSION <= 180 + global = JS_NewObject(cx, &global_class, NULL, NULL); + if (global == NULL) { + exit(EXIT_FAILURE); + } + JS_SetGlobalObject(cx, global); +#else + global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); + if (global == NULL) { + exit(EXIT_FAILURE); + } +#endif + + if (JS_InitStandardClasses(cx, global) != JS_TRUE) { + exit(EXIT_FAILURE); + } + + /* Setup example */ + if (setup_example(cx, global) != JS_TRUE) { + exit(EXIT_FAILURE); + } + + /* Execute a script */ + + + /* Compile a script file into a script object */ + + script = JS_CompileFile(cx, global, jsfile); + + if (!script) { + exit(EXIT_FAILURE); + } + + /* Execute script object */ + + JS_ExecuteScript(cx, global, script, &rval); + + /* Remove script object from memory */ +#if JS_VERSION <= 180 + JS_DestroyScript(cx, script); +#endif + + /* Cleanup */ + + JS_DestroyContext(cx); + JS_DestroyRuntime(rt); + JS_ShutDown(); + + exit(EXIT_SUCCESS); +} diff --git a/examples/number.j b/examples/number.j new file mode 100644 index 0000000..09aaf6a --- /dev/null +++ b/examples/number.j @@ -0,0 +1,42 @@ +/* + * jsapi number example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ +#include <jsapi.h> +#include <stdlib.h> +#include <stdio.h> + + static void + print_double(double x) + { + printf("The number is %f.\n", x); + } +%} + +function void js_print_double : print_double (double=1) <static>; + +%< + +const char * jsfile = "number.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + return JS_DefineFunctions(cx, global, jj_fs); +} + +%> diff --git a/examples/number.js b/examples/number.js new file mode 100644 index 0000000..86438e8 --- /dev/null +++ b/examples/number.js @@ -0,0 +1,3 @@ +js_print_double(4.5); +js_print_double(); +js_print_double("4.5"); diff --git a/examples/object-con.j b/examples/object-con.j new file mode 100644 index 0000000..933cc2d --- /dev/null +++ b/examples/object-con.j @@ -0,0 +1,90 @@ +/* + * jsapi object constructor example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ + +#include <stdlib.h> +#include <stdio.h> + +struct data { + int n; +}; + +static void * +my_class_construct(int n) +{ + struct data *This; + + This = malloc(sizeof(*This)); + + if (!This) { + perror("malloc"); + return NULL; + } + + This->n = n; + + printf("Construting with value %d\n", This->n); + + return This; + + +} + +static void +my_class_show(struct data *This) +{ + printf("Value %d\n", This->n); +} + +static void +my_class_finalize(struct data *This) +{ + if (This) { + printf("Finalising with value %d\n", This->n); + } else { + printf("Finalising with NULL!\n"); + } + free(This); +} + + +%} + +class my_class + { + construct : my_class_construct (int); + function void show : my_class_show (); + finalize : my_class_finalize; + }; + + +%< + +const char * jsfile = "object-con.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + if (!jjmy_class_init(cx, global)) { + return JS_FALSE; + } + + return JS_DefineFunctions(cx, global, jj_fs); +} + +%> diff --git a/examples/object-con.js b/examples/object-con.js new file mode 100644 index 0000000..aae5fb7 --- /dev/null +++ b/examples/object-con.js @@ -0,0 +1,5 @@ +var foo = new my_class(1); +foo.show(); + +var bar = new my_class(666); +bar.show(); diff --git a/examples/retval.j b/examples/retval.j new file mode 100644 index 0000000..786bb9e --- /dev/null +++ b/examples/retval.j @@ -0,0 +1,50 @@ +/* + * jsapi return value example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ +#include <jsapi.h> +#include <stdlib.h> +#include <stdio.h> + +static int + sum(int n, int m) + { + return n+m; + } + + static void + print_double(double x) + { + printf("The number is %f.\n", x); + } +%} + +function int js_sum : sum (int, int) <static>; +function void js_print_double : print_double (double=1) <static>; + + +%< + +const char * jsfile = "retval.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + return JS_DefineFunctions(cx, global, jj_fs); +} + +%> diff --git a/examples/retval.js b/examples/retval.js new file mode 100644 index 0000000..5fa1715 --- /dev/null +++ b/examples/retval.js @@ -0,0 +1,2 @@ +js_print_double(js_sum(1, 2)); + js_print_double(js_sum(1, js_sum(2, 3))); diff --git a/examples/square.j b/examples/square.j new file mode 100644 index 0000000..b087039 --- /dev/null +++ b/examples/square.j @@ -0,0 +1,39 @@ +/* + * jsapi square example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ + +int c_square(int n) +{ + return n*n; +} + +%} + +function int js_square : c_square (int=2) <static>; + +%< + +const char * jsfile = "square.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + return JS_DefineFunctions(cx, global, jj_fs); +} + +%> diff --git a/examples/square.js b/examples/square.js new file mode 100644 index 0000000..6b0521f --- /dev/null +++ b/examples/square.js @@ -0,0 +1 @@ +c_square(12); \ No newline at end of file diff --git a/examples/window.j b/examples/window.j new file mode 100644 index 0000000..a6c539f --- /dev/null +++ b/examples/window.j @@ -0,0 +1,90 @@ +/* + * jsapi browser window class example + * Copyright 2012 Vincent Sanders <vi...@ky...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +%{ + +#include <stdio.h> + +void jsapi_window_close() +{ + printf("%s\n", __func__); + return; +} + +void jsapi_window_stop() +{ + printf("%s\n", __func__); + return; +} + +void jsapi_window_focus() +{ + printf("%s\n", __func__); + return; +} + +void jsapi_window_blur() +{ + printf("%s\n", __func__); + return; +} + +void jsapi_window_open(void *this, char *url, char *target, char *features, int replace) +{ + printf("%s(%s,%s,%s,%d)\n", __func__, url,target,features,replace); + return; +} + +/* attribute (property) setter/getetrs */ +char *jsapi_window_status_get(void) +{ + return NULL; +} + +void jsapi_window_status_set(char *s) +{ +} + +%} + +class window { + /* window functions */ + function void close : jsapi_window_close (); + function void stop : jsapi_window_stop (); + function void focus : jsapi_window_focus (); + function void blur : jsapi_window_blur (); + function void open : jsapi_window_open (cstring=NULL, cstring=NULL, cstring=NULL, int=0); + + /* window properties */ + property cstring status: jsapi_window_status_get , jsapi_window_status_set <static>; +}; + +%< + +const char * jsfile = "window.js"; + +JSBool setup_example(JSContext *cx, JSObject *global) +{ + if (!jjwindow_init(cx, global)) { + return JS_FALSE; + } + + return JS_TRUE; +} + +%> diff --git a/examples/window.js b/examples/window.js new file mode 100644 index 0000000..5a117d1 --- /dev/null +++ b/examples/window.js @@ -0,0 +1,5 @@ +window.close(); + +window.status = "hello"; + +window.open("http://www.ebay.co.uk/"); -- 1.7.10 |