/+junk/c_sdl_joypad

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad

« back to all changes in this revision

Viewing changes to JSParser.c

  • Committer: Gustav Hartvigsson
  • Date: 2014-01-09 20:07:52 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140109200752-owx481zfl2n4tjsc
* added and changed little in the files Lincening information.
* Started to implement the JS parser's C file.

Show diffs side-by-side

added added

removed removed

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