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
|
#ifndef __H_JS_PARSER__
#define __H_JS_PARSER__
/** @file
* This is based of MozJS/Spidermonkey from mozilla, pleace see
* .../js/jsapi.h for more information on the licenses used.
*
* 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> 2013
*
* Thanks to:
* https://btwotch.wordpress.com/2013/03/05/embedding-javascript-via-spidermonkey-into-c/
*/
#include <js/jsapi.h>
#include "Game.h"
/**
* Function prototype to be used for loading context data, this is user-defined
* so any data can be loaded into the JS context.
*/
typedef void (* GameJSParserLoadDataFunc)(GameJSParser * self, void * data);
/**
* Function that is used to dump data from the JS context, it is user-defined.
*/
typedef void * (* GameJSParserDumpDataFunc)(GameJSParser * self);
/**
* The data structure that represents the EcmaScript/JavaScript parser/engine
* that can be used when creating games.
*/
typedef struct _GameJSParser {
GameJSParserLoadDataFunc load_data_func;
GameJSParserDumpDataFunc dump_data_func;
JSRuntime * js_rt;
JSContext * js_cx;
JSObject * js_global;
JSFunctionSpec * js_global_funcs; /**< Treat as an array, end with NULL */
Game * game; /**< DO NOT FREE! */
char * game_script_file_name;
char * game_script;
} GameJSParser;
/**
* Need to know a bit about the game, like screen and renderer and such.
*
* Note that file_name and script parameters are mutually exclusive. If both are
* set, the function will return NULL, and print an error to stderr.
*/
GameJSParser * game_js_parser_new (Game * game,
char * file_name,
char * script);
/**
*
*/
void game_js_parser_free (GameJSParser * self);
/**
*
*/
void game_js_parser_set_settings_loader (GameJSParser * self,
GameJSParserLoadDataFunc func);
void game_js_parser_set_settings_dump (GameJSParser * self,
GameJSParserDumpDataFunc func);
void * game_js_parser_load_settings (GameJSParser * self, void * data);
void * game_js_parser_dump_settings (GameJSParser * self);
/**
* This just wraps JS_DefineFunction:
* https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_DefineFunction
*/
void game_js_parser_add_js_global_callback (GameJSParser * self,
const char * name,
JSNative call,
uint16 nargs,
uint16 flags);
void game_js_parser_add_js_private_callback (GameJSParser * self,
JSObject * context,
const char * name,
JSNative call,
uint16 nargs,
uint16 flags);
void game_js_parser_run (GameJSParser * self);
#endif
|