-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage_rb.c
293 lines (258 loc) · 7.38 KB
/
language_rb.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <ruby.h>
#include <stdbool.h>
#include <string.h>
#include "language.h"
#include "dict.h"
typedef struct _rb_internal {
language_t*li;
VALUE object;
dict_t*functions;
} rb_internal_t;
static rb_internal_t*global;
static int rb_reference_count = 0;
static bool initialize_rb(language_t*li, size_t mem_size)
{
if(li->internal)
return true; // already initialized
log_dbg("[rb] initializing\n");
li->internal = calloc(1, sizeof(rb_internal_t));
rb_internal_t*rb = (rb_internal_t*)li->internal;
rb->li = li;
if(rb_reference_count==0) {
ruby_init();
global = rb;
}
rb_reference_count++;
rb->object = rb_eval_string("Object");
return true;
}
static void rb_report_error(VALUE error)
{
volatile VALUE message = rb_obj_as_string(error);
char*msg = RSTRING(message)->as.heap.ptr;
if(msg && *msg) {
printf("Ruby Error:\n");
printf("%s\n", msg);
}
}
static value_t* ruby_to_value(VALUE v)
{
switch (TYPE(v)) {
case T_NIL:
return value_new_void();
case T_BIGNUM:
case T_FIXNUM:
return value_new_int32(NUM2INT(v));
case T_TRUE:
return value_new_boolean(true);
case T_FALSE:
return value_new_boolean(false);
case T_FLOAT:
return value_new_float32(NUM2DBL(v));
case T_SYMBOL:
return value_new_string(rb_id2name(SYM2ID(v)));
case T_STRING:
return value_new_string(StringValuePtr(v));
case T_ARRAY: {
/* process Array */
value_t*array = array_new();
int len = RARRAY(v)->as.heap.len;
int i;
for(i=0;i<len;i++) {
volatile VALUE item = RARRAY(v)->as.heap.ptr[i];
array_append(array, ruby_to_value(item));
}
return array;
}
default:
/* raise exception */
rb_raise(rb_eTypeError, "not valid value");
}
}
static VALUE value_to_ruby(value_t*v)
{
switch(v->type) {
case TYPE_VOID:
return Qnil;
break;
case TYPE_FLOAT32:
return rb_float_new(v->f32);
break;
case TYPE_INT32:
return INT2FIX(v->i32);
break;
case TYPE_BOOLEAN:
if(v->b) {
return Qtrue;
} else {
return Qfalse;
}
break;
case TYPE_STRING: {
return rb_str_new2(v->str);
}
break;
case TYPE_ARRAY: {
volatile VALUE a = rb_ary_new2(v->length);
int i;
for(i=0;i<v->length;i++) {
rb_ary_store(a, i, value_to_ruby(v->data[i]));
}
return a;
}
break;
default:
return Qnil;
}
}
typedef struct _ruby_dfunc {
language_t*li;
const char*script;
bool fail;
} ruby_dfunc_t;
static VALUE compile_script_internal(VALUE _dfunc)
{
ruby_dfunc_t*dfunc = (ruby_dfunc_t*)_dfunc;
language_t*li = dfunc->li;
rb_internal_t*rb = (rb_internal_t*)li->internal;
volatile VALUE ret = rb_eval_string(dfunc->script);
dfunc->fail = false;
return Qtrue;
}
static VALUE compile_script_exception(VALUE _dfunc, VALUE exc)
{
ruby_dfunc_t*dfunc = (ruby_dfunc_t*)_dfunc;
rb_report_error(exc);
dfunc->fail = true;
return Qfalse;
}
static bool compile_script_rb(language_t*li, const char*script)
{
log_dbg("[ruby] compile_script");
ruby_dfunc_t dfunc;
dfunc.li = li;
dfunc.script = script;
dfunc.fail = false;
volatile VALUE ret = rb_rescue2(compile_script_internal, (VALUE)&dfunc, compile_script_exception, (VALUE)&dfunc, rb_eException, (VALUE)0);
return !dfunc.fail;
}
static VALUE ruby_function_proxy(VALUE self, VALUE _args)
{
ID id = rb_frame_this_func();
value_t* value = dict_lookup(global->functions, (void*)id);
if(!value) {
language_error(global->li, "[ruby] couldn't retrieve constant %s", rb_id2name(id));
return Qnil;
}
if(value->type == TYPE_FUNCTION) {
log_dbg("[ruby] calling function %s", rb_id2name(id));
value_t*args = ruby_to_value(_args);
value_t*ret = value->call(value, args);
value_destroy(args);
volatile VALUE r = value_to_ruby(ret);
value_destroy(ret);
log_dbg("[rb] returning from callback");
return r;
} else {
log_dbg("[ruby] retrieving constant %s (%s)", rb_id2name(id), type_to_string(value->type));
volatile VALUE r = value_to_ruby(value);
return r;
}
return Qnil;
}
static void store_function(const char*name, value_t*value)
{
ID id = rb_intern(name);
if(!global->functions) {
global->functions = dict_new(&ptr_type);
}
dict_put(global->functions, (void*)id, value);
}
static void define_constant_rb(language_t*li, const char*name, value_t*value)
{
rb_internal_t*rb = (rb_internal_t*)li->internal;
log_dbg("[ruby] define constant %s", name);
rb_define_global_function(name, ruby_function_proxy, -2);
store_function(name, value);
}
static void define_function_rb(language_t*li, const char*name, function_t*f)
{
rb_internal_t*rb = (rb_internal_t*)li->internal;
log_dbg("[ruby] define function %s", name);
rb_define_global_function(name, ruby_function_proxy, -2);
store_function(name, f);
}
static bool is_function_rb(language_t*li, const char*name)
{
log_dbg("[ruby] is_function(%s)", name);
rb_internal_t*rb = (rb_internal_t*)li->internal;
ID id = rb_intern(name);
return rb_respond_to(rb->object, id);
}
typedef struct _ruby_fcall {
language_t*li;
const char*function_name;
value_t*args;
bool fail;
} ruby_fcall_t;
static VALUE call_function_internal(VALUE _fcall)
{
ruby_fcall_t*fcall = (ruby_fcall_t*)_fcall;
language_t*li = fcall->li;
rb_internal_t*rb = (rb_internal_t*)li->internal;
int num_args = fcall->args->length;
volatile ID fname = rb_intern(fcall->function_name);
volatile VALUE*args = alloca(sizeof(VALUE)*num_args);
int i;
for(i=0;i<num_args;i++) {
args[i] = value_to_ruby(fcall->args->data[i]);
}
volatile VALUE ret = rb_funcall2(rb->object, fname, num_args, (VALUE*)args);
return ret;
}
static VALUE call_function_exception(VALUE _fcall, VALUE exc)
{
log_dbg("[rb] call_function_exception");
ruby_fcall_t*fcall = (ruby_fcall_t*)_fcall;
rb_report_error(exc);
fcall->fail = true;
}
static value_t* call_function_rb(language_t*li, const char*name, value_t*args)
{
log_dbg("[ruby] calling function %s", name);
ruby_fcall_t fcall;
fcall.li = li;
fcall.fail = false;
fcall.args = args;
fcall.function_name = name;
volatile VALUE ret = rb_rescue(call_function_internal, (VALUE)&fcall, call_function_exception, (VALUE)&fcall);
if(fcall.fail) {
return NULL;
} else {
return ruby_to_value(ret);
}
}
static void destroy_rb(language_t* li)
{
if(li->internal) {
rb_internal_t*rb = (rb_internal_t*)li->internal;
if(--rb_reference_count == 0) {
ruby_finalize();
}
free(rb);
}
free(li);
}
language_t* ruby_interpreter_new()
{
language_t * li = calloc(1, sizeof(language_t));
li->name = "rb";
li->initialize = initialize_rb;
li->compile_script = compile_script_rb;
li->is_function = is_function_rb;
li->define_constant = define_constant_rb;
li->define_function = define_function_rb;
li->call_function = call_function_rb;
li->destroy = destroy_rb;
return li;
}