/+junk/c_sdl_joypad_ducktape

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

« back to all changes in this revision

Viewing changes to JSParser.c

  • Committer: Gustav Hartvigsson
  • Date: 2014-01-09 20:50:51 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140109205051-4j1veaw5fwux8591
created a generic error print function that should print nice looking
error messages, supports formating...
Not tested.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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:
4
 
 */
5
 
 
6
1
#include <stdio.h>
7
2
#include <string.h>
8
 
#include <stdlib.h>
9
3
#include "game_utils.h"
10
4
#include "JSParser.h"
11
5
 
12
 
/*
 
6
/**
13
7
 * This file, as the rest of the project is under MIT license.
14
8
 * see http://opensource.org/licenses/MIT
15
9
 *
16
10
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
17
11
 */
18
12
 
19
 
/* Js/C function declarations. */
20
 
int js_print_error (duk_context * ctx);
21
 
 
22
 
 
23
 
/******************************************************************************/
 
13
/*
 
14
  The global JS Class.
 
15
*/
 
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
 
22
};
 
23
 
 
24
 
24
25
GameJSParser * game_js_parser_new (Game * game,
25
26
                                   char * file_name,
26
27
                                   char * script) {
27
28
  GameJSParser * self = malloc (sizeof(GameJSParser));
28
 
  if (file_name != NULL && script != NULL) {
 
29
  if (file_name != NULL && stript != NULL) {
29
30
    print_error (
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"
32
33
    );
33
 
    goto error;
34
 
  } else if (file_name == NULL && script == NULL) {
 
34
    game_js_parser_free (self);
 
35
    return NULL;
 
36
  } else if (file_name == NULL && stript == NULL) {
35
37
    print_error (
36
38
      "WARNING: No script or file is set when constructing GameJSParser"
37
39
    );
38
40
  }
39
41
  
40
 
  /* Functions to be added to the environment,
41
 
   * realised at the end of this file.
42
 
   */
43
 
  const duk_function_list_entry js_funcs[] = {
44
 
    {"print_error", js_print_error, 1},
45
 
    {NULL,NULL,0}
46
 
  };
47
 
  duk_put_function_list (self->ctx, -1, js_funcs);
48
 
  
49
 
  /* Set some stuffs and evaluates the scripts */
50
 
  if (file_name) {
51
 
    self->file_name = file_name;
52
 
    duk_eval_file (self->ctx, self->file_name);
53
 
    duk_pop (self->ctx);
54
 
  }
55
 
  if (script) {
56
 
    self->script = script;
57
 
    duk_eval_string (self->ctx, self->script);
58
 
    duk_pop (self->ctx);
59
 
  }
60
 
  
61
 
  self->ctx = duk_create_heap_default ();
62
 
  if (!self->ctx) {
63
 
    print_error ("Could not Initialise Duktape Context.");
64
 
    goto error;
 
42
  self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
 
43
  
 
44
  if (!self->js_rt) {
 
45
    print_error ("Could not contruct JSRunTime.");
 
46
    return NULL;
 
47
  }
 
48
  
 
49
  self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /** Do not change. **/
 
50
  
 
51
  if (!self->js_cx) {
 
52
    print_error ("Could not contruct JSContext.");
 
53
    game_js_parser_free (self);
 
54
    return NULL;
 
55
  }
 
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);
 
60
    return NULL;
65
61
  }
66
62
  
67
63
  self->game = game;
68
64
  
69
65
  return self;
70
66
  
71
 
  error:
72
 
  game_js_parser_free (self);
73
 
  return NULL;
74
67
}
75
68
 
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);
80
 
  }
81
 
  if (strlen (self->script)) {
 
72
  } else {
82
73
    free (self->script);
83
74
  }
84
75
  
 
76
  if (self->js_rt) {
 
77
    if (self->js_cx) {
 
78
      JS_DestroyContext (self->js_cx);
 
79
    }
 
80
    JS_DestroyRuntime (self->js_rt);
 
81
  }
85
82
  free (self);
86
83
}
87
84
 
95
92
  self->dump_data_func = func;
96
93
}
97
94
 
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);
101
98
}
102
99
void * game_js_parser_dump_settings (GameJSParser * self) {
103
100
  GameJSParserDumpDataFunc func = self->dump_data_func;
104
 
  return func (self);
105
 
}
106
 
 
107
 
void game_js_parser_run (GameJSParser * self) {
108
 
  
109
 
  
110
 
}
111
 
 
112
 
/******************************************************************************/
113
 
int js_print_error (duk_context * ctx) {
114
 
  /* function print_error (str) */
115
 
  
116
 
  const char * buf = duk_get_string (ctx, 0);
117
 
  if (buf) {
118
 
    print_error (buf);
119
 
    return 0;
120
 
  }
121
 
  return -1;
122
 
}
123
 
 
 
101
  return  func (self);
 
102
}