16
16
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
22
static JSClass _js_global_class = {
23
"global", JSCLASS_GLOBAL_FLAGS,
24
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
25
JS_StrictPropertyStub,
26
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
27
JSCLASS_NO_OPTIONAL_MEMBERS
19
/* Js/C function declarations. */
20
int js_print_error (duk_context * ctx);
23
/******************************************************************************/
31
24
GameJSParser * game_js_parser_new (Game * game,
37
30
"You can not use both a script and a file when creating a GameJSParser.\n"
38
31
"Exacly ONE of these must be used."
40
game_js_parser_free (self);
42
34
} else if (file_name == NULL && script == NULL) {
44
36
"WARNING: No script or file is set when constructing GameJSParser"
48
self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
51
print_error ("Could not contruct JSRunTime.");
55
self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /* Do not change. **/
58
print_error ("Could not contruct JSContext.");
59
game_js_parser_free (self);
62
self->js_global = JS_NewGlobalObject (self->js_cx, &_js_global_class);
63
if (!self->js_global) {
64
print_error ( "Could not contruct the JS runtime global object.");
65
game_js_parser_free (self);
40
/* Functions to be added to the environment,
41
* realised at the end of this file.
43
const duk_function_list_entry js_funcs[] = {
44
{"print_error", js_print_error, 1},
47
duk_put_function_list (self->ctx, -1, js_funcs);
49
/* Set some stuffs and evaluates the scripts */
51
self->file_name = file_name;
52
duk_eval_file (self->ctx, self->file_name);
56
self->script = script;
57
duk_eval_string (self->ctx, self->script);
61
self->ctx = duk_create_heap_default ();
63
print_error ("Could not Initialise Duktape Context.");
72
game_js_parser_free (self);
75
76
void game_js_parser_free (GameJSParser * self) {
76
if (self->file_name) {
77
duk_destroy_heap(self->ctx);
78
if (strlen (self->file_name)) {
77
79
free (self->file_name);
81
if (strlen (self->script)) {
79
82
free (self->script);
84
JS_DestroyContext (self->js_cx);
86
JS_DestroyRuntime (self->js_rt);
106
103
GameJSParserDumpDataFunc func = self->dump_data_func;
107
104
return func (self);
107
void game_js_parser_run (GameJSParser * self) {
112
/******************************************************************************/
113
int js_print_error (duk_context * ctx) {
114
/* function print_error (str) */
116
const char * buf = duk_get_string (ctx, 0);