1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#include <stdio.h>
#include <string.h>
#include "game_utils.h"
#include "JSParser.h"
/**
* This file, as the rest of the project is under MIT license.
* see http://opensource.org/licenses/MIT
*
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
*/
/*
The global JS Class.
*/
static JSClass _js_global_class = {
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
GameJSParser * game_js_parser_new (Game * game,
char * file_name,
char * script) {
GameJSParser * self = malloc (sizeof(GameJSParser));
if (file_name != NULL && stript != NULL) {
print_error (
"You can not use both a script and a file when creating a GameJSParser.\n"
"Exacly ONE of these must be used."
);
game_js_parser_free (self);
return NULL;
} else if (file_name == NULL && stript == NULL) {
print_error (
"WARNING: No script or file is set when constructing GameJSParser"
);
}
self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
if (!self->js_rt) {
print_error ("Could not contruct JSRunTime.");
return NULL;
}
self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /* Do not change. **/
if (!self->js_cx) {
print_error ("Could not contruct JSContext.");
game_js_parser_free (self);
return NULL;
}
self->js_global = JS_NewGlobalObject (self->js_cx, _js_global_class, NULL);
if (!self->js_global) {
print_error ( "Could not contruct the JS runtime global object.");
game_js_parser_free (self);
return NULL;
}
self->game = game;
return self;
}
void game_js_parser_free (GameJSParser * self) {
if (self->file_name) {
free (self->file_name);
} else {
free (self->script);
}
if (self->js_rt) {
if (self->js_cx) {
JS_DestroyContext (self->js_cx);
}
JS_DestroyRuntime (self->js_rt);
}
free (self);
}
void game_js_parser_set_settings_loader (GameJSParser * self,
GameJSParserLoadDataFunc func) {
self->load_data_func = func;
}
void game_js_parser_set_settings_dump (GameJSParser * self,
GameJSParserDumpDataFunc func) {
self->dump_data_func = func;
}
void * game_js_parser_load_settings (GameJSParser * self, void * data) {
GameJSParserDumpDataFunc func = self->load_data_func;
func (self, data);
}
void * game_js_parser_dump_settings (GameJSParser * self) {
GameJSParserDumpDataFunc func = self->dump_data_func;
return func (self);
}
|