rua 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.txt +9 -8
  2. data/ext/rua.c +66 -10
  3. data/ext/rua.h +1 -0
  4. metadata +1 -1
data/README.txt CHANGED
@@ -19,22 +19,23 @@ Rua is a library for using Lua under Ruby.
19
19
  #rua.openlibs(:base, :package, :string)
20
20
  #rua.secure = false
21
21
 
22
- rua[:str] = 'xxx'
23
- rua[:num] = 100
24
- rua[:proc] = lambda do
22
+ rua.str = 'xxx'
23
+ rua.num = 100
24
+ rua.proc = lambda do
25
25
  puts 'proc called.'
26
26
  end
27
- rua[:err] = lambda do
27
+ rua.err = lambda do
28
28
  raise 'err called.'
29
29
  end
30
-
30
+ rua.Time = Time
31
31
  puts rua.eval(<<-EOS)
32
32
  print('hello Rua!')
33
33
  print(str)
34
34
  print(num)
35
35
  proc()
36
36
  err()
37
-
37
+ print(Time.new().to_s())
38
+
38
39
  f = function()
39
40
  print('f() called.')
40
41
  end
@@ -42,5 +43,5 @@ Rua is a library for using Lua under Ruby.
42
43
  return true
43
44
  EOS
44
45
 
45
- f = rua[:f]
46
- f.call
46
+ rua.f.call
47
+ p rua.eval('return 1, 2, 3')
data/ext/rua.c CHANGED
@@ -80,7 +80,7 @@ void Init_rua() {
80
80
  RuaError = rb_define_class("RuaError", rb_eStandardError);
81
81
 
82
82
  rb_define_alloc_func(Rua, rua_alloc);
83
- rb_define_const(Rua, "VERSION", rb_str_new2("0.1.2"));
83
+ rb_define_const(Rua, "VERSION", rb_str_new2("0.1.3"));
84
84
  rb_define_private_method(Rua, "initialize", rua_initialize, -1);
85
85
  rb_define_method(Rua, "openlibs", rua_openlibs, -1);
86
86
  rb_define_method(Rua, "eval", rua_eval, 1);
@@ -88,6 +88,7 @@ void Init_rua() {
88
88
  rb_define_method(Rua, "[]=", rua_set, 2);
89
89
  rb_define_method(Rua, "secure", rua_get_secure, 0);
90
90
  rb_define_method(Rua, "secure=", rua_set_secure, 1);
91
+ rb_define_method(Rua, "method_missing", rua_method_missing, -1);
91
92
 
92
93
  rb_define_alloc_func(RuaFunc, rua_func_alloc);
93
94
  rb_define_private_method(RuaFunc, "initialize", rua_func_initialize, 0);
@@ -191,21 +192,38 @@ static VALUE rua_eval(VALUE self, VALUE str) {
191
192
  struct rua *p;
192
193
  VALUE retval;
193
194
  const char *errmsg;
195
+ int pretop, nresults, i;
194
196
 
195
197
  Data_Get_Struct(self, struct rua, p);
196
198
  Check_Type(str, T_STRING);
199
+ pretop = lua_gettop(p->L);
197
200
  luaL_loadstring(p->L, StringValuePtr(str));
198
201
 
199
- if (lua_pcall(p->L, 0, 1, 0) != 0) {
202
+ if (lua_pcall(p->L, 0, LUA_MULTRET, 0) != 0) {
200
203
  RuaError = rb_const_get(rb_cObject, rb_intern("RuaError"));
201
204
  errmsg = lua_tostring(p->L, -1);
202
205
  lua_pop(p->L, 1);
203
206
  rb_raise(RuaError, "%s", errmsg);
204
207
  }
205
208
 
206
- retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
207
- lua_pop(p->L, 1);
208
- return retval;
209
+ nresults = lua_gettop(p->L) - pretop;
210
+
211
+ if (nresults == 0) {
212
+ return Qnil;
213
+ } else if (nresults == 1) {
214
+ retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
215
+ lua_pop(p->L, 1);
216
+ return retval;
217
+ } else {
218
+ retval = rb_ary_new();
219
+
220
+ for (i = nresults; i > 0; i--) {
221
+ rb_ary_push(retval, rua_torbval(p->L, -i, p->error_handler, p->secure));
222
+ }
223
+
224
+ lua_pop(p->L, nresults);
225
+ return retval;
226
+ }
209
227
  }
210
228
 
211
229
  static VALUE rua_get(VALUE self, VALUE key) {
@@ -256,6 +274,28 @@ static VALUE rua_set_secure(VALUE self, VALUE secure) {
256
274
  return Qnil;
257
275
  }
258
276
 
277
+ static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self) {
278
+ const char *name;
279
+ size_t len;
280
+
281
+ name = rb_id2name(rb_to_id(argv[0]));
282
+
283
+ if (!name) {
284
+ rb_raise(rb_eRuntimeError, "fail: unknown method or property");
285
+ }
286
+
287
+ len = strlen(name);
288
+
289
+ if (argc = 2 && name[len - 1] == '=') {
290
+ argv[0] = rb_str_new(name, (long) len - 1);
291
+ return rua_set(self, argv[0], argv[1]);
292
+ } else if(argc = 1) {
293
+ return rua_get(self, argv[0]);
294
+ } else {
295
+ return rb_call_super(argc, argv);
296
+ }
297
+ }
298
+
259
299
  // ------------------------------------------------------------------
260
300
 
261
301
  static VALUE rua_func_alloc(VALUE klass) {
@@ -271,25 +311,41 @@ static VALUE rua_func_initialize(VALUE self) {
271
311
  static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
272
312
  struct rua_func *p;
273
313
  VALUE retval;
274
- int i;
314
+ int pretop, nresults, i;
275
315
  const char *errmsg;
276
316
 
277
317
  Data_Get_Struct(self, struct rua_func, p);
318
+ pretop = lua_gettop(p->L);
278
319
  lua_rawgeti(p->L, LUA_REGISTRYINDEX, p->ref);
279
320
 
280
321
  for (i = 0; i < argc; i ++) {
281
322
  rua_pushrbval(p->L, argv[i], p->error_handler, p->secure);
282
323
  }
283
324
 
284
- if (lua_pcall(p->L, argc, 1, 0) != 0) {
325
+ if (lua_pcall(p->L, argc, LUA_MULTRET, 0) != 0) {
285
326
  errmsg = lua_tostring(p->L, -1);
286
327
  lua_pop(p->L, 1);
287
328
  rb_raise(RuaError, "%s", errmsg);
288
329
  }
289
330
 
290
- retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
291
- lua_pop(p->L, 1);
292
- return retval;
331
+ nresults = lua_gettop(p->L) - pretop;
332
+
333
+ if (nresults == 0) {
334
+ return Qnil;
335
+ } else if (nresults == 1) {
336
+ retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
337
+ lua_pop(p->L, 1);
338
+ return retval;
339
+ } else {
340
+ retval = rb_ary_new();
341
+
342
+ for (i = nresults; i > 0; i--) {
343
+ rb_ary_push(retval, rua_torbval(p->L, -i, p->error_handler, p->secure));
344
+ }
345
+
346
+ lua_pop(p->L, nresults);
347
+ return retval;
348
+ }
293
349
  }
294
350
 
295
351
  // ------------------------------------------------------------------
data/ext/rua.h CHANGED
@@ -24,6 +24,7 @@ static VALUE rua_get(VALUE self, VALUE key);
24
24
  static VALUE rua_set(VALUE self, VALUE key, VALUE val);
25
25
  static VALUE rua_get_secure(VALUE self);
26
26
  static VALUE rua_set_secure(VALUE self, VALUE secure);
27
+ static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self);
27
28
 
28
29
  static VALUE rua_func_alloc(VALUE klass);
29
30
  static VALUE rua_func_initialize(VALUE self);
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: rua
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
6
+ version: 0.1.3
7
7
  date: 2007-11-14 00:00:00 +09:00
8
8
  summary: rua is a library for using Lua under Ruby.
9
9
  require_paths: