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
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include <stdio.h>
#include <string.h>
#include "JSParser.h"
/*
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) {
fprintf (stderr,
"============================================================================\n"
"You can not use both a script and a file when creating a GameJSParser.\n"
"Exacly ONE of these must be used.\n"
"============================================================================\n"
);
game_js_parser_free (self);
return NULL;
} else if (file_name == NULL && stript == NULL) {
fprintf (stderr,
"============================================================================\n"
"WARNING: No script or file is set when constructing GameJSParser\n"
"============================================================================\n"
);
} else {
game_js_parser_free (self);
return NULL;
}
self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
if (!self->js_rt) {
fprintf (stderr,
"============================================================================\n"
"Could not contruct JSRunTime.\n"
"============================================================================\n"
);
return NULL;
}
self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /** Do not change. **/
if (!self->js_cx) {
fprintf (stderr,
"============================================================================\n"
"Could not contruct JSContext.\n"
"============================================================================\n"
);
game_js_parser_free (self);
return NULL;
}
self->js_global = JS_NewGlobalObject (self->js_cx, _js_global_class, NULL);
if (!self->js_global) {
fprintf (stderr,
"============================================================================\n"
"Could not contruct JS runtime the global object.\n"
"============================================================================\n"
);
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);
}
|