1
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
2
* vi: set shiftwidth=2 tabstop=2 expandtab:
3
* :indentSize=2:tabSize=2:noTabs=true:
9
3
#include "game_utils.h"
10
4
#include "JSParser.h"
13
7
* This file, as the rest of the project is under MIT license.
14
8
* see http://opensource.org/licenses/MIT
16
10
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
19
/* Js/C function declarations. */
20
int js_print_error (duk_context * ctx);
23
/******************************************************************************/
16
static JSClass _js_global_class = {
17
"global", JSCLASS_GLOBAL_FLAGS,
18
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
19
JS_StrictPropertyStub,
20
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
21
JSCLASS_NO_OPTIONAL_MEMBERS
24
25
GameJSParser * game_js_parser_new (Game * game,
27
28
GameJSParser * self = malloc (sizeof(GameJSParser));
28
if (file_name != NULL && script != NULL) {
29
if (file_name != NULL && stript != NULL) {
30
"You can not use both a script and a file when creating a GameJSParser.\n"
31
"Exacly ONE of these must be used."
31
"You can not use both a script and a file when creating a GameJSParser."
32
"Exacly ONE of these must be used.\n"
34
} else if (file_name == NULL && script == NULL) {
34
game_js_parser_free (self);
36
} else if (file_name == NULL && stript == NULL) {
36
38
"WARNING: No script or file is set when constructing GameJSParser"
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.");
42
self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
45
print_error ("Could not contruct JSRunTime.");
49
self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /** Do not change. **/
52
print_error ("Could not contruct JSContext.");
53
game_js_parser_free (self);
56
self->js_global = JS_NewGlobalObject (self->js_cx, _js_global_class, NULL);
57
if (!self->js_global) {
58
print_error ( "Could not contruct the JS runtime global object.");
59
game_js_parser_free (self);
72
game_js_parser_free (self);
76
69
void game_js_parser_free (GameJSParser * self) {
77
duk_destroy_heap(self->ctx);
78
if (strlen (self->file_name)) {
70
if (self->file_name) {
79
71
free (self->file_name);
81
if (strlen (self->script)) {
82
73
free (self->script);
78
JS_DestroyContext (self->js_cx);
80
JS_DestroyRuntime (self->js_rt);
95
92
self->dump_data_func = func;
98
void game_js_parser_load_settings (GameJSParser * self, void * data) {
99
GameJSParserLoadDataFunc func = self->load_data_func;
95
void * game_js_parser_load_settings (GameJSParser * self, void * data) {
96
GameJSParserDumpDataFunc func = self->load_data_func;
100
97
func (self, data);
102
99
void * game_js_parser_dump_settings (GameJSParser * self) {
103
100
GameJSParserDumpDataFunc func = self->dump_data_func;
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);