rua 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ext/extconf.rb +6 -0
- data/ext/rua.c +523 -0
- data/ext/rua.h +44 -0
- metadata +48 -0
data/ext/extconf.rb
ADDED
data/ext/rua.c
ADDED
@@ -0,0 +1,523 @@
|
|
1
|
+
#ifdef _WIN32
|
2
|
+
#pragma warning(disable:4311)
|
3
|
+
#pragma warning(disable:4312)
|
4
|
+
__declspec(dllexport) void Init_rua(void);
|
5
|
+
#endif
|
6
|
+
|
7
|
+
#include <stddef.h>
|
8
|
+
#include <string.h>
|
9
|
+
|
10
|
+
#include "ruby.h"
|
11
|
+
|
12
|
+
#include "lua.h"
|
13
|
+
#include "lualib.h"
|
14
|
+
#include "lauxlib.h"
|
15
|
+
|
16
|
+
#include "rua.h"
|
17
|
+
|
18
|
+
static const char *insecure_methods[] = {
|
19
|
+
" __id__",
|
20
|
+
"__send__",
|
21
|
+
"class",
|
22
|
+
"extend",
|
23
|
+
"freeze",
|
24
|
+
"id",
|
25
|
+
"instance_eval",
|
26
|
+
"instance_variable_defined?",
|
27
|
+
"instance_variable_get",
|
28
|
+
"instance_variable_set",
|
29
|
+
"instance_variables",
|
30
|
+
"method",
|
31
|
+
"method_missing",
|
32
|
+
"methods",
|
33
|
+
"private_methods",
|
34
|
+
"protected_methods",
|
35
|
+
"public_methods",
|
36
|
+
"respond_to?",
|
37
|
+
"send",
|
38
|
+
"singleton_methods",
|
39
|
+
"taint",
|
40
|
+
"type",
|
41
|
+
"untaint",
|
42
|
+
"to_ary",
|
43
|
+
"to_hash",
|
44
|
+
"to_int",
|
45
|
+
"to_str"
|
46
|
+
};
|
47
|
+
|
48
|
+
static const int insecure_method_num = sizeof(insecure_methods) / sizeof(char*);
|
49
|
+
|
50
|
+
void Init_rua() {
|
51
|
+
VALUE Rua, RuaFunc, RuaError;
|
52
|
+
|
53
|
+
Rua = rb_define_class("Rua", rb_cObject);
|
54
|
+
RuaFunc = rb_define_class("RuaFunc", rb_cObject);
|
55
|
+
RuaError = rb_define_class("RuaError", rb_eStandardError);
|
56
|
+
|
57
|
+
rb_define_alloc_func(Rua, rua_alloc);
|
58
|
+
rb_define_private_method(Rua, "initialize", rua_initialize, -1);
|
59
|
+
rb_define_method(Rua, "openlibs", rua_openlibs, -1);
|
60
|
+
rb_define_method(Rua, "eval", rua_eval, 1);
|
61
|
+
rb_define_method(Rua, "[]", rua_get, 1);
|
62
|
+
rb_define_method(Rua, "[]=", rua_set, 2);
|
63
|
+
rb_define_method(Rua, "secure", rua_get_secure, 0);
|
64
|
+
rb_define_method(Rua, "secure=", rua_set_secure, 1);
|
65
|
+
|
66
|
+
rb_define_alloc_func(RuaFunc, rua_func_alloc);
|
67
|
+
rb_define_private_method(RuaFunc, "initialize", rua_func_initialize, 0);
|
68
|
+
rb_define_method(RuaFunc, "call", rua_func_call, -1);
|
69
|
+
}
|
70
|
+
|
71
|
+
// ------------------------------------------------------------------
|
72
|
+
|
73
|
+
static VALUE rua_alloc(VALUE klass) {
|
74
|
+
struct rua *p = ALLOC(struct rua);
|
75
|
+
|
76
|
+
return Data_Wrap_Struct(klass, 0, rua_free, p);
|
77
|
+
}
|
78
|
+
|
79
|
+
static void rua_free(struct rua *p) {
|
80
|
+
if (p->L) {
|
81
|
+
lua_close(p->L);
|
82
|
+
}
|
83
|
+
|
84
|
+
free(p);
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
|
88
|
+
struct rua *p;
|
89
|
+
VALUE error_handler, klass, klassname;
|
90
|
+
|
91
|
+
Data_Get_Struct(self, struct rua, p);
|
92
|
+
p->L = NULL;
|
93
|
+
|
94
|
+
if (rb_scan_args(argc, argv, "01", &error_handler) < 1) {
|
95
|
+
error_handler = Qnil;
|
96
|
+
} else if (!rua_obj_is_executable(error_handler)) {
|
97
|
+
klass = rb_obj_class(error_handler);
|
98
|
+
klassname = rb_class_name(klass);
|
99
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc or Method)", StringValuePtr(klassname));
|
100
|
+
}
|
101
|
+
|
102
|
+
p->L = lua_open();
|
103
|
+
p->secure = 1;
|
104
|
+
p->error_handler = error_handler;
|
105
|
+
return Qnil;
|
106
|
+
}
|
107
|
+
|
108
|
+
static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
|
109
|
+
struct rua *p;
|
110
|
+
VALUE arg, s_all, s_base, s_package, s_string, s_table, s_math, s_io, s_debug;
|
111
|
+
int i;
|
112
|
+
|
113
|
+
if (argc < 1) {
|
114
|
+
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
|
115
|
+
}
|
116
|
+
|
117
|
+
for (i = 0; i < argc; i++) {
|
118
|
+
Check_Type(argv[i], T_SYMBOL);
|
119
|
+
}
|
120
|
+
|
121
|
+
Data_Get_Struct(self, struct rua, p);
|
122
|
+
|
123
|
+
s_all = ID2SYM(rb_intern("all"));
|
124
|
+
s_base = ID2SYM(rb_intern("base"));
|
125
|
+
s_package = ID2SYM(rb_intern("package"));
|
126
|
+
s_string = ID2SYM(rb_intern("string"));
|
127
|
+
s_table = ID2SYM(rb_intern("table"));
|
128
|
+
s_math = ID2SYM(rb_intern("math"));
|
129
|
+
s_io = ID2SYM(rb_intern("io"));
|
130
|
+
s_debug = ID2SYM(rb_intern("debug"));
|
131
|
+
|
132
|
+
for (i = 0; i < argc; i++) {
|
133
|
+
arg = argv[i];
|
134
|
+
|
135
|
+
if (s_all == arg) {
|
136
|
+
luaL_openlibs(p->L);
|
137
|
+
} else if (s_base == arg) {
|
138
|
+
lua_pushcfunction(p->L, luaopen_base);
|
139
|
+
lua_call(p->L, 0, 0);
|
140
|
+
} else if (s_package == arg) {
|
141
|
+
lua_pushcfunction(p->L, luaopen_package);
|
142
|
+
lua_call(p->L, 0, 0);
|
143
|
+
} else if (s_string == arg) {
|
144
|
+
lua_pushcfunction(p->L, luaopen_string);
|
145
|
+
lua_call(p->L, 0, 0);
|
146
|
+
} else if (s_table == arg) {
|
147
|
+
lua_pushcfunction(p->L, luaopen_table);
|
148
|
+
lua_call(p->L, 0, 0);
|
149
|
+
} else if (s_math == arg) {
|
150
|
+
lua_pushcfunction(p->L, luaopen_math);
|
151
|
+
lua_call(p->L, 0, 0);
|
152
|
+
} else if (s_io == arg) {
|
153
|
+
lua_pushcfunction(p->L, luaopen_io);
|
154
|
+
lua_call(p->L, 0, 0);
|
155
|
+
} else if (s_debug == arg) {
|
156
|
+
lua_pushcfunction(p->L, luaopen_debug);
|
157
|
+
lua_call(p->L, 0, 0);
|
158
|
+
} else {
|
159
|
+
arg = rb_check_convert_type(arg, T_STRING, "String", "to_s");
|
160
|
+
rb_raise(rb_eArgError, "unknown library '%s' (available: base, package, table, math, io, debug, all)", StringValuePtr(arg));
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
return Qnil;
|
165
|
+
}
|
166
|
+
|
167
|
+
static VALUE rua_eval(VALUE self, VALUE str) {
|
168
|
+
struct rua *p;
|
169
|
+
VALUE retval, RuaError;
|
170
|
+
const char *errmsg;
|
171
|
+
|
172
|
+
Data_Get_Struct(self, struct rua, p);
|
173
|
+
Check_Type(str, T_STRING);
|
174
|
+
luaL_loadstring(p->L, StringValuePtr(str));
|
175
|
+
|
176
|
+
if (lua_pcall(p->L, 0, 1, 0) != 0) {
|
177
|
+
RuaError = rb_const_get(rb_cObject, rb_intern("RuaError"));
|
178
|
+
errmsg = lua_tostring(p->L, -1);
|
179
|
+
lua_pop(p->L, 1);
|
180
|
+
rb_raise(RuaError, "%s", errmsg);
|
181
|
+
}
|
182
|
+
|
183
|
+
retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
|
184
|
+
lua_pop(p->L, 1);
|
185
|
+
return retval;
|
186
|
+
}
|
187
|
+
|
188
|
+
static VALUE rua_get(VALUE self, VALUE key) {
|
189
|
+
struct rua *p;
|
190
|
+
VALUE retval;
|
191
|
+
|
192
|
+
Data_Get_Struct(self, struct rua, p);
|
193
|
+
key = rb_check_convert_type(key, T_STRING, "String", "to_s");
|
194
|
+
lua_getglobal(p->L, StringValuePtr(key));
|
195
|
+
retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
|
196
|
+
lua_pop(p->L, 1);
|
197
|
+
return retval;
|
198
|
+
}
|
199
|
+
|
200
|
+
static VALUE rua_set(VALUE self, VALUE key, VALUE val) {
|
201
|
+
struct rua *p;
|
202
|
+
VALUE RuaError, inspect, klass, klassname;
|
203
|
+
|
204
|
+
Data_Get_Struct(self, struct rua, p);
|
205
|
+
|
206
|
+
if (p->secure && rb_obj_is_kind_of(val, rb_cModule)) {
|
207
|
+
RuaError = rb_const_get(rb_cObject, rb_intern("RuaError"));
|
208
|
+
inspect = rb_check_convert_type(val, T_STRING, "String", "inspect");
|
209
|
+
klass = rb_obj_class(val);
|
210
|
+
klassname = rb_class_name(klass);
|
211
|
+
rb_raise(RuaError, "set insecure value %s (%s)", StringValuePtr(inspect), StringValuePtr(klassname));
|
212
|
+
}
|
213
|
+
|
214
|
+
key = rb_check_convert_type(key, T_STRING, "String", "to_s");
|
215
|
+
rua_pushrbval(p->L, val, p->error_handler, p->secure);
|
216
|
+
lua_setglobal(p->L, StringValuePtr(key));
|
217
|
+
return Qnil;
|
218
|
+
}
|
219
|
+
|
220
|
+
static VALUE rua_get_secure(VALUE self) {
|
221
|
+
struct rua *p;
|
222
|
+
|
223
|
+
Data_Get_Struct(self, struct rua, p);
|
224
|
+
return p->secure ? Qtrue : Qfalse;
|
225
|
+
}
|
226
|
+
|
227
|
+
static VALUE rua_set_secure(VALUE self, VALUE secure) {
|
228
|
+
struct rua *p;
|
229
|
+
VALUE klass, klassname;
|
230
|
+
|
231
|
+
Data_Get_Struct(self, struct rua, p);
|
232
|
+
|
233
|
+
switch (TYPE(secure)) {
|
234
|
+
case T_TRUE: p->secure = 1; break;
|
235
|
+
case T_FALSE: p->secure = 0; break;
|
236
|
+
default:
|
237
|
+
klass = rb_obj_class(secure);
|
238
|
+
klassname = rb_class_name(klass);
|
239
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", StringValuePtr(klassname));
|
240
|
+
break;
|
241
|
+
}
|
242
|
+
|
243
|
+
return Qnil;
|
244
|
+
}
|
245
|
+
|
246
|
+
// ------------------------------------------------------------------
|
247
|
+
|
248
|
+
static VALUE rua_func_alloc(VALUE klass) {
|
249
|
+
struct rua_func *p = ALLOC(struct rua_func);
|
250
|
+
|
251
|
+
return Data_Wrap_Struct(klass, 0, -1, p);
|
252
|
+
}
|
253
|
+
|
254
|
+
static VALUE rua_func_initialize(VALUE self) {
|
255
|
+
return Qnil;
|
256
|
+
}
|
257
|
+
|
258
|
+
static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
|
259
|
+
struct rua_func *p;
|
260
|
+
VALUE retval, RuaError;
|
261
|
+
int i;
|
262
|
+
const char *errmsg;
|
263
|
+
|
264
|
+
Data_Get_Struct(self, struct rua_func, p);
|
265
|
+
lua_rawgeti(p->L, LUA_REGISTRYINDEX, p->ref);
|
266
|
+
|
267
|
+
for (i = 0; i < argc; i ++) {
|
268
|
+
rua_pushrbval(p->L, argv[i], p->error_handler, p->secure);
|
269
|
+
}
|
270
|
+
|
271
|
+
if (lua_pcall(p->L, argc, 1, 0) != 0) {
|
272
|
+
RuaError = rb_const_get(rb_cObject, rb_intern("RuaError"));
|
273
|
+
errmsg = lua_tostring(p->L, -1);
|
274
|
+
lua_pop(p->L, 1);
|
275
|
+
rb_raise(RuaError, "%s", errmsg);
|
276
|
+
}
|
277
|
+
|
278
|
+
retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
|
279
|
+
lua_pop(p->L, 1);
|
280
|
+
return retval;
|
281
|
+
}
|
282
|
+
|
283
|
+
// ------------------------------------------------------------------
|
284
|
+
|
285
|
+
static VALUE rua_torbval(lua_State *L, int idx, VALUE error_handler, int secure) {
|
286
|
+
VALUE rbval = Qnil;
|
287
|
+
|
288
|
+
switch (lua_type(L, idx)) {
|
289
|
+
case LUA_TNUMBER:
|
290
|
+
rbval = rb_float_new(lua_tonumber(L, idx));
|
291
|
+
break;
|
292
|
+
|
293
|
+
case LUA_TBOOLEAN:
|
294
|
+
rbval = lua_toboolean(L, idx) ? Qtrue : Qfalse;
|
295
|
+
break;
|
296
|
+
|
297
|
+
case LUA_TSTRING:
|
298
|
+
rbval = rb_str_new2(lua_tostring(L, idx));
|
299
|
+
break;
|
300
|
+
|
301
|
+
case LUA_TTABLE:
|
302
|
+
rbval = rua_tohash(L, idx, error_handler, secure);
|
303
|
+
break;
|
304
|
+
|
305
|
+
case LUA_TFUNCTION:
|
306
|
+
rbval = rua_toruafunc(L, idx, error_handler, secure);
|
307
|
+
break;
|
308
|
+
|
309
|
+
case LUA_TLIGHTUSERDATA:
|
310
|
+
rbval = (VALUE) lua_touserdata(L, idx);
|
311
|
+
break;
|
312
|
+
}
|
313
|
+
|
314
|
+
return rbval;
|
315
|
+
}
|
316
|
+
|
317
|
+
static VALUE rua_tohash(lua_State *L, int idx, VALUE error_handler, int secure) {
|
318
|
+
VALUE hash, key, val;
|
319
|
+
int tblidx;
|
320
|
+
|
321
|
+
lua_pushvalue(L, idx);
|
322
|
+
tblidx = lua_gettop(L);
|
323
|
+
hash = rb_hash_new();
|
324
|
+
lua_pushnil(L);
|
325
|
+
|
326
|
+
while (lua_next(L, tblidx) != 0) {
|
327
|
+
key = rua_torbval(L, -2, error_handler, secure);
|
328
|
+
val = rua_torbval(L, -1, error_handler, secure);
|
329
|
+
rb_hash_aset(hash, key, val);
|
330
|
+
lua_pop(L, 1);
|
331
|
+
}
|
332
|
+
|
333
|
+
lua_pop(L, 1);
|
334
|
+
return hash;
|
335
|
+
}
|
336
|
+
|
337
|
+
static void rua_pushrbval(lua_State *L, VALUE rbval, VALUE error_handler, int secure) {
|
338
|
+
struct rua_func *p;
|
339
|
+
VALUE RuaFunc, inspect, klass, klassname;
|
340
|
+
|
341
|
+
switch (TYPE(rbval)) {
|
342
|
+
case T_NIL:
|
343
|
+
lua_pushnil(L);
|
344
|
+
break;
|
345
|
+
|
346
|
+
case T_FLOAT:
|
347
|
+
case T_FIXNUM:
|
348
|
+
case T_BIGNUM:
|
349
|
+
lua_pushnumber(L, rb_num2dbl(rbval));
|
350
|
+
break;
|
351
|
+
|
352
|
+
case T_STRING:
|
353
|
+
lua_pushstring(L, StringValuePtr(rbval));
|
354
|
+
break;
|
355
|
+
|
356
|
+
case T_TRUE:
|
357
|
+
lua_pushboolean(L, 1);
|
358
|
+
break;
|
359
|
+
|
360
|
+
case T_FALSE:
|
361
|
+
lua_pushboolean(L, 0);
|
362
|
+
break;
|
363
|
+
|
364
|
+
case T_ARRAY:
|
365
|
+
rua_newtable_from_ary(L, rbval, error_handler, secure);
|
366
|
+
break;
|
367
|
+
|
368
|
+
case T_HASH:
|
369
|
+
rua_newtable_from_hash(L, rbval, error_handler, secure);
|
370
|
+
break;
|
371
|
+
|
372
|
+
default:
|
373
|
+
RuaFunc = rb_const_get(rb_cObject, rb_intern("RuaFunc"));
|
374
|
+
|
375
|
+
if (secure && rb_obj_is_kind_of(rbval, rb_cModule)) {
|
376
|
+
inspect = rb_check_convert_type(rbval, T_STRING, "String", "inspect");
|
377
|
+
klass = rb_obj_class(rbval);
|
378
|
+
klassname = rb_class_name(klass);
|
379
|
+
rb_fatal("convert insecure value %s (%s)", StringValuePtr(inspect), StringValuePtr(klassname));
|
380
|
+
} else if (rb_obj_is_kind_of(rbval, RuaFunc)) {
|
381
|
+
Data_Get_Struct(rbval, struct rua_func, p);
|
382
|
+
lua_rawgeti(L, LUA_REGISTRYINDEX, p->ref);
|
383
|
+
} else if (rua_obj_is_executable(rbval)) {
|
384
|
+
lua_pushlightuserdata(L, (void *) rbval);
|
385
|
+
lua_pushlightuserdata(L, (void *) error_handler);
|
386
|
+
lua_pushlightuserdata(L, (void *) secure);
|
387
|
+
lua_pushcclosure(L, rua_proc_call, 3);
|
388
|
+
} else {
|
389
|
+
rua_newtable_from_obj(L, rbval, error_handler, secure);
|
390
|
+
}
|
391
|
+
|
392
|
+
break;
|
393
|
+
}
|
394
|
+
}
|
395
|
+
|
396
|
+
static void rua_newtable_from_ary(lua_State *L, VALUE ary, VALUE error_handler, int secure) {
|
397
|
+
VALUE entry;
|
398
|
+
int i, tblidx;
|
399
|
+
|
400
|
+
lua_newtable(L);
|
401
|
+
tblidx = lua_gettop(L);
|
402
|
+
|
403
|
+
for (i = 0; i < RARRAY(ary)->len; i++) {
|
404
|
+
entry = rb_ary_entry(ary, i);
|
405
|
+
lua_pushnumber(L, i + 1);
|
406
|
+
rua_pushrbval(L, entry, error_handler, secure);
|
407
|
+
lua_settable(L, tblidx);
|
408
|
+
}
|
409
|
+
}
|
410
|
+
|
411
|
+
static void rua_newtable_from_hash(lua_State *L, VALUE hash, VALUE error_handler, int secure) {
|
412
|
+
VALUE keys, key, val;
|
413
|
+
int i, tblidx;
|
414
|
+
|
415
|
+
lua_newtable(L);
|
416
|
+
tblidx = lua_gettop(L);
|
417
|
+
keys = rb_check_convert_type(hash, T_ARRAY, "Array", "keys");
|
418
|
+
|
419
|
+
for (i = 0; i < RARRAY(keys)->len; i++) {
|
420
|
+
key = rb_ary_entry(keys, i);
|
421
|
+
val = rb_hash_aref(hash, key);
|
422
|
+
rua_pushrbval(L, key, error_handler, secure);
|
423
|
+
rua_pushrbval(L, val, error_handler, secure);
|
424
|
+
lua_settable(L, tblidx);
|
425
|
+
}
|
426
|
+
}
|
427
|
+
|
428
|
+
static void rua_newtable_from_obj(lua_State *L, VALUE obj, VALUE error_handler, int secure) {
|
429
|
+
VALUE methods, name, method;
|
430
|
+
int i, tblidx;
|
431
|
+
|
432
|
+
lua_newtable(L);
|
433
|
+
tblidx = lua_gettop(L);
|
434
|
+
methods = rb_check_convert_type(obj, T_ARRAY, "Array", "methods");
|
435
|
+
|
436
|
+
for (i = 0; i < RARRAY(methods)->len; i++) {
|
437
|
+
name = rb_ary_entry(methods, i);
|
438
|
+
method = rb_funcall(obj, rb_intern("method"), 1, name);
|
439
|
+
|
440
|
+
if (secure && rua_name_is_insecure_method(StringValuePtr(name))) {
|
441
|
+
continue;
|
442
|
+
}
|
443
|
+
|
444
|
+
rua_pushrbval(L, name, error_handler, secure);
|
445
|
+
rua_pushrbval(L, method, error_handler, secure);
|
446
|
+
lua_settable(L, tblidx);
|
447
|
+
}
|
448
|
+
}
|
449
|
+
|
450
|
+
static int rua_proc_call(lua_State *L) {
|
451
|
+
VALUE proc, args, retval, error_handler, errargs, errmsg;
|
452
|
+
int i, n, status, secure;
|
453
|
+
|
454
|
+
proc = (VALUE) lua_touserdata(L, lua_upvalueindex(1));
|
455
|
+
error_handler = (VALUE) lua_touserdata(L, lua_upvalueindex(2));
|
456
|
+
secure = (int) lua_touserdata(L, lua_upvalueindex(3));
|
457
|
+
args = rb_ary_new();
|
458
|
+
n = lua_gettop(L);
|
459
|
+
|
460
|
+
for (i = 0; i < n; i++) {
|
461
|
+
rb_ary_push(args, rua_torbval(L, i + 1, error_handler, secure));
|
462
|
+
}
|
463
|
+
|
464
|
+
rb_ary_push(args, proc);
|
465
|
+
retval = rb_protect(_rua_proc_call, args, &status);
|
466
|
+
|
467
|
+
if (status != 0) {
|
468
|
+
if (rua_obj_is_executable(error_handler)) {
|
469
|
+
errargs = rb_ary_new();
|
470
|
+
rb_ary_push(errargs, ruby_errinfo);
|
471
|
+
rb_ary_push(errargs, error_handler);
|
472
|
+
retval = rb_protect(_rua_proc_call, errargs, &status);
|
473
|
+
|
474
|
+
if (status != 0) {
|
475
|
+
errmsg = rb_check_convert_type(ruby_errinfo, T_STRING, "String", "to_s");
|
476
|
+
fprintf(stderr, "warning: %s\n", StringValuePtr(errmsg));
|
477
|
+
}
|
478
|
+
} else {
|
479
|
+
retval = Qnil;
|
480
|
+
}
|
481
|
+
}
|
482
|
+
|
483
|
+
rua_pushrbval(L, retval, error_handler, secure);
|
484
|
+
return 1;
|
485
|
+
}
|
486
|
+
|
487
|
+
static VALUE _rua_proc_call(VALUE args) {
|
488
|
+
VALUE proc = rb_ary_pop(args);
|
489
|
+
|
490
|
+
return rb_apply(proc, rb_intern("call"), args);
|
491
|
+
}
|
492
|
+
|
493
|
+
static VALUE rua_toruafunc(lua_State *L, int idx, VALUE error_handler, int secure) {
|
494
|
+
struct rua_func *p;
|
495
|
+
VALUE RuaFunc, f;
|
496
|
+
|
497
|
+
RuaFunc = rb_const_get(rb_cObject, rb_intern("RuaFunc"));
|
498
|
+
f = rb_funcall(RuaFunc, rb_intern("new"), 0);
|
499
|
+
Data_Get_Struct(f, struct rua_func, p);
|
500
|
+
p->L = L;
|
501
|
+
p->error_handler = error_handler;
|
502
|
+
p->secure = secure;
|
503
|
+
lua_pushvalue(L, idx);
|
504
|
+
p->ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
505
|
+
lua_pop(L, 1);
|
506
|
+
return f;
|
507
|
+
}
|
508
|
+
|
509
|
+
static VALUE rua_obj_is_executable(VALUE obj) {
|
510
|
+
return (rb_obj_is_kind_of(obj, rb_cProc) || rb_obj_is_kind_of(obj, rb_cMethod));
|
511
|
+
}
|
512
|
+
|
513
|
+
static int rua_name_is_insecure_method(const char *name) {
|
514
|
+
int i;
|
515
|
+
|
516
|
+
for (i = 0; i < insecure_method_num; i++) {
|
517
|
+
if (strcmp(insecure_methods[i], name) == 0) {
|
518
|
+
return 1;
|
519
|
+
}
|
520
|
+
}
|
521
|
+
|
522
|
+
return 0;
|
523
|
+
}
|
data/ext/rua.h
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#ifndef __RUA_H__
|
2
|
+
#define __RUA_H__
|
3
|
+
|
4
|
+
struct rua {
|
5
|
+
lua_State *L;
|
6
|
+
VALUE error_handler;
|
7
|
+
int secure;
|
8
|
+
};
|
9
|
+
|
10
|
+
struct rua_func {
|
11
|
+
lua_State *L;
|
12
|
+
int ref;
|
13
|
+
VALUE error_handler;
|
14
|
+
int secure;
|
15
|
+
};
|
16
|
+
|
17
|
+
void Init_rua();
|
18
|
+
static VALUE rua_alloc(VALUE klass);
|
19
|
+
static void rua_free(struct rua *p);
|
20
|
+
static VALUE rua_initialize(int argc, VALUE *argv, VALUE self);
|
21
|
+
static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self);
|
22
|
+
static VALUE rua_eval(VALUE self, VALUE str);
|
23
|
+
static VALUE rua_get(VALUE self, VALUE key);
|
24
|
+
static VALUE rua_set(VALUE self, VALUE key, VALUE val);
|
25
|
+
static VALUE rua_get_secure(VALUE self);
|
26
|
+
static VALUE rua_set_secure(VALUE self, VALUE secure);
|
27
|
+
|
28
|
+
static VALUE rua_func_alloc(VALUE klass);
|
29
|
+
static VALUE rua_func_initialize(VALUE self);
|
30
|
+
static VALUE rua_func_call(int argc, VALUE *argv, VALUE self);
|
31
|
+
|
32
|
+
static VALUE rua_torbval(lua_State *L, int idx, VALUE error_handler, int secure);
|
33
|
+
static VALUE rua_tohash(lua_State *L, int idx, VALUE error_handler, int secure);
|
34
|
+
static void rua_pushrbval(lua_State *L, VALUE rbval, VALUE error_handler, int secure);
|
35
|
+
static void rua_newtable_from_ary(lua_State *L, VALUE ary, VALUE error_handler, int secure);
|
36
|
+
static void rua_newtable_from_hash(lua_State *L, VALUE hash, VALUE error_handler, int secure);
|
37
|
+
static void rua_newtable_from_obj(lua_State *L, VALUE obj, VALUE error_handler, int secure);
|
38
|
+
static int rua_proc_call(lua_State *L);
|
39
|
+
static VALUE _rua_proc_call(VALUE args);
|
40
|
+
static VALUE rua_toruafunc(lua_State *L, int idx, VALUE error_handler, int secure);
|
41
|
+
static VALUE rua_obj_is_executable(VALUE obj);
|
42
|
+
static int rua_name_is_insecure_method(const char *name);
|
43
|
+
|
44
|
+
#endif
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: rua
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-11-13 00:00:00 +09:00
|
8
|
+
summary: rua is a library for using Lua under Ruby.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sgwr_dts@yahoo.co.jp
|
12
|
+
homepage: http://rua.rubyforge.org
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- winebarrel
|
31
|
+
files:
|
32
|
+
- ext/rua.c
|
33
|
+
- ext/rua.h
|
34
|
+
- ext/extconf.rb
|
35
|
+
test_files: []
|
36
|
+
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions:
|
44
|
+
- ext/extconf.rb
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies: []
|
48
|
+
|