/+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 src/JSParser.c

  • Committer: Gustav Hartvigsson
  • Date: 2014-06-15 19:55:43 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140615195543-3lxnj0fmvn0imgzr
* it compiles...

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
17
17
 */
18
18
 
19
 
/*
20
 
  The global JS Class.
21
 
*/
22
 
static JSClass _js_global_class = {
23
 
    "global", JSCLASS_GLOBAL_FLAGS,
24
 
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
25
 
    JS_StrictPropertyStub,
26
 
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
27
 
    JSCLASS_NO_OPTIONAL_MEMBERS
28
 
};
29
 
 
30
 
 
 
19
/* Js/C function declarations. */
 
20
int js_print_error (duk_context * ctx);
 
21
 
 
22
 
 
23
/******************************************************************************/
31
24
GameJSParser * game_js_parser_new (Game * game,
32
25
                                   char * file_name,
33
26
                                   char * script) {
37
30
      "You can not use both a script and a file when creating a GameJSParser.\n"
38
31
      "Exacly ONE of these must be used."
39
32
    );
40
 
    game_js_parser_free (self);
41
 
    return NULL;
 
33
    goto error;
42
34
  } else if (file_name == NULL && script == NULL) {
43
35
    print_error (
44
36
      "WARNING: No script or file is set when constructing GameJSParser"
45
37
    );
46
38
  }
47
39
  
48
 
  self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE);
49
 
  
50
 
  if (!self->js_rt) {
51
 
    print_error ("Could not contruct JSRunTime.");
52
 
    return NULL;
53
 
  }
54
 
  
55
 
  self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /* Do not change. **/
56
 
  
57
 
  if (!self->js_cx) {
58
 
    print_error ("Could not contruct JSContext.");
59
 
    game_js_parser_free (self);
60
 
    return NULL;
61
 
  }
62
 
  self->js_global = JS_NewGlobalObject (self->js_cx, &_js_global_class);
63
 
  if (!self->js_global) {
64
 
    print_error ( "Could not contruct the JS runtime global object.");
65
 
    game_js_parser_free (self);
66
 
    return NULL;
 
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;
67
65
  }
68
66
  
69
67
  self->game = game;
70
68
  
71
69
  return self;
72
70
  
 
71
  error:
 
72
  game_js_parser_free (self);
 
73
  return NULL;
73
74
}
74
75
 
75
76
void game_js_parser_free (GameJSParser * self) {
76
 
  if (self->file_name) {
 
77
  duk_destroy_heap(self->ctx);
 
78
  if (strlen (self->file_name)) {
77
79
    free (self->file_name);
78
 
  } else {
 
80
  }
 
81
  if (strlen (self->script)) {
79
82
    free (self->script);
80
83
  }
81
84
  
82
 
  if (self->js_rt) {
83
 
    if (self->js_cx) {
84
 
      JS_DestroyContext (self->js_cx);
85
 
    }
86
 
    JS_DestroyRuntime (self->js_rt);
87
 
  }
88
85
  free (self);
89
86
}
90
87
 
106
103
  GameJSParserDumpDataFunc func = self->dump_data_func;
107
104
  return func (self);
108
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