rua 0.2.1 → 0.2.2

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 +8 -7
  2. data/ext/rua.c +60 -32
  3. data/ext/rua.h +12 -10
  4. metadata +4 -5
data/README.txt CHANGED
@@ -10,6 +10,10 @@ Rua is a library for using Lua under Ruby.
10
10
 
11
11
  http://rubyforge.org/projects/rua
12
12
 
13
+ == Install
14
+
15
+ gem install rua
16
+
13
17
  == Download
14
18
 
15
19
  http://rubyforge.org/frs/?group_id=4845
@@ -19,15 +23,12 @@ http://rubyforge.org/frs/?group_id=4845
19
23
  require 'rua'
20
24
 
21
25
  class MyClass; end
22
-
23
- error_handler = lambda do |e|
24
- p e
25
- end
26
26
 
27
- rua = Rua.new(error_handler)
28
- rua.openlibs(:all)
27
+ rua = Rua.new(:all)
29
28
  #rua.openlibs(:base, :package, :string)
30
- #rua.secure = false
29
+ rua.error_handler = lambda do |e|
30
+ p e
31
+ end
31
32
 
32
33
  rua.str = 'xxx'
33
34
  rua.num = 100
data/ext/rua.c CHANGED
@@ -1,3 +1,4 @@
1
+ /* */
1
2
  #ifdef _WIN32
2
3
  #pragma warning(disable:4311)
3
4
  #pragma warning(disable:4312)
@@ -15,7 +16,7 @@ __declspec(dllexport) void Init_rua(void);
15
16
 
16
17
  #include "rua.h"
17
18
 
18
- #define VERSION "0.2.1"
19
+ #define VERSION "0.2.2"
19
20
  #define REF_RBOBJ "self"
20
21
 
21
22
  static const char *insecure_methods[] = {
@@ -92,6 +93,8 @@ void Init_rua() {
92
93
  rb_define_method(Rua, "[]=", rua_set, 2);
93
94
  rb_define_method(Rua, "secure", rua_get_secure, 0);
94
95
  rb_define_method(Rua, "secure=", rua_set_secure, 1);
96
+ rb_define_method(Rua, "error_handler", rua_get_error_handler, 0);
97
+ rb_define_method(Rua, "error_handler=", rua_set_error_handler, 1);
95
98
  rb_define_method(Rua, "method_missing", rua_method_missing, -1);
96
99
 
97
100
  rb_define_alloc_func(RuaFunc, rua_func_alloc);
@@ -118,6 +121,7 @@ void Init_rua() {
118
121
 
119
122
  static VALUE rua_alloc(VALUE klass) {
120
123
  struct rua *p = ALLOC(struct rua);
124
+ p->R = ALLOC(struct rua_state);
121
125
 
122
126
  return Data_Wrap_Struct(klass, 0, rua_free, p);
123
127
  }
@@ -127,6 +131,10 @@ static void rua_free(struct rua *p) {
127
131
  lua_close(p->L);
128
132
  }
129
133
 
134
+ if (p->R) {
135
+ free(p->R);
136
+ }
137
+
130
138
  free(p);
131
139
  }
132
140
 
@@ -135,20 +143,16 @@ static void rua_free(struct rua *p) {
135
143
  */
136
144
  static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
137
145
  struct rua *p;
138
- VALUE error_handler;
139
146
 
140
147
  Data_Get_Struct(self, struct rua, p);
141
- p->L = NULL;
148
+ p->L = lua_open();
149
+ p->R->secure = 1;
150
+ p->R->error_handler = Qnil;
142
151
 
143
- if (rb_scan_args(argc, argv, "01", &error_handler) < 1) {
144
- error_handler = Qnil;
145
- } else if (!rua_obj_is_executable(error_handler)) {
146
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc or Method)", rua_classname_ptr(error_handler));
152
+ if (argc > 0) {
153
+ rua_openlibs(argc, argv, self);
147
154
  }
148
155
 
149
- p->L = lua_open();
150
- p->R.secure = 1;
151
- p->R.error_handler = error_handler;
152
156
  return Qnil;
153
157
  }
154
158
 
@@ -249,7 +253,7 @@ static VALUE rua_set(VALUE self, VALUE key, VALUE val) {
249
253
 
250
254
  Data_Get_Struct(self, struct rua, p);
251
255
 
252
- if (p->R.secure && (rb_equal(rb_cModule, val) || rb_equal(rb_cClass, val))) {
256
+ if (p->R->secure && (rb_equal(rb_cModule, val) || rb_equal(rb_cClass, val))) {
253
257
  rb_raise(RuaError, "set insecure value %s", rua_to_sptr(val));
254
258
  }
255
259
 
@@ -265,7 +269,7 @@ static VALUE rua_get_secure(VALUE self) {
265
269
  struct rua *p;
266
270
 
267
271
  Data_Get_Struct(self, struct rua, p);
268
- return p->R.secure ? Qtrue : Qfalse;
272
+ return p->R->secure ? Qtrue : Qfalse;
269
273
  }
270
274
 
271
275
  /*
@@ -277,8 +281,8 @@ static VALUE rua_set_secure(VALUE self, VALUE secure) {
277
281
  Data_Get_Struct(self, struct rua, p);
278
282
 
279
283
  switch (TYPE(secure)) {
280
- case T_TRUE: p->R.secure = 1; break;
281
- case T_FALSE: p->R.secure = 0; break;
284
+ case T_TRUE: p->R->secure = 1; break;
285
+ case T_FALSE: p->R->secure = 0; break;
282
286
  default:
283
287
  rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", rua_classname_ptr(secure));
284
288
  break;
@@ -287,6 +291,32 @@ static VALUE rua_set_secure(VALUE self, VALUE secure) {
287
291
  return Qnil;
288
292
  }
289
293
 
294
+
295
+ /*
296
+ * get error handler flag.
297
+ */
298
+ static VALUE rua_get_error_handler(VALUE self) {
299
+ struct rua *p;
300
+
301
+ Data_Get_Struct(self, struct rua, p);
302
+ return p->R->error_handler;
303
+ }
304
+
305
+ /*
306
+ * set error handler flag.
307
+ */
308
+ static VALUE rua_set_error_handler(VALUE self, VALUE error_handler) {
309
+ struct rua *p;
310
+
311
+ if (!NIL_P(error_handler) && !rua_obj_is_executable(error_handler)) {
312
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc or Method)", rua_classname_ptr(error_handler));
313
+ }
314
+
315
+ Data_Get_Struct(self, struct rua, p);
316
+ p->R->error_handler = error_handler;
317
+ return Qnil;
318
+ }
319
+
290
320
  /*
291
321
  * dispatch Rua#[], Rua#[]=.
292
322
  */
@@ -412,7 +442,7 @@ static VALUE rua_thread_resume(int argc, VALUE *argv, VALUE self) {
412
442
 
413
443
  // ------------------------------------------------------------------
414
444
 
415
- static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state R) {
445
+ static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state *R) {
416
446
  VALUE retval;
417
447
  int nresults, i;
418
448
 
@@ -436,7 +466,7 @@ static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state R) {
436
466
  }
437
467
  }
438
468
 
439
- static VALUE rua_torbval(lua_State *L, int idx, struct rua_state R) {
469
+ static VALUE rua_torbval(lua_State *L, int idx, struct rua_state *R) {
440
470
  VALUE rbval = Qnil;
441
471
 
442
472
  switch (lua_type(L, idx)) {
@@ -502,7 +532,7 @@ static VALUE rua_torbobj(lua_State *L, int idx) {
502
532
  return rbobj;
503
533
  }
504
534
 
505
- static VALUE rua_tohash(lua_State *L, int idx, struct rua_state R) {
535
+ static VALUE rua_tohash(lua_State *L, int idx, struct rua_state *R) {
506
536
  VALUE hash, key, val;
507
537
  int tblidx;
508
538
 
@@ -522,7 +552,7 @@ static VALUE rua_tohash(lua_State *L, int idx, struct rua_state R) {
522
552
  return hash;
523
553
  }
524
554
 
525
- static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state R) {
555
+ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state *R) {
526
556
  struct rua_ref *p;
527
557
 
528
558
  switch (TYPE(rbval)) {
@@ -557,7 +587,7 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state R) {
557
587
  break;
558
588
 
559
589
  default:
560
- if (R.secure && (rb_equal(rb_cModule, rbval) || rb_equal(rb_cClass, rbval))) {
590
+ if (R->secure && (rb_equal(rb_cModule, rbval) || rb_equal(rb_cClass, rbval))) {
561
591
  fprintf(stderr, "warning: convert insecure value %s", rua_to_sptr(rbval));
562
592
  lua_pushnil(L);
563
593
  } else if (rb_obj_is_kind_of(rbval, RuaFunc)) {
@@ -568,9 +598,8 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state R) {
568
598
  lua_rawgeti(L, LUA_REGISTRYINDEX, p->ref);
569
599
  } else if (rua_obj_is_executable(rbval)) {
570
600
  lua_pushlightuserdata(L, (void *) rbval);
571
- lua_pushlightuserdata(L, (void *) R.error_handler);
572
- lua_pushlightuserdata(L, (void *) R.secure);
573
- lua_pushcclosure(L, rua_proc_call, 3);
601
+ lua_pushlightuserdata(L, R);
602
+ lua_pushcclosure(L, rua_proc_call, 2);
574
603
  } else {
575
604
  rua_newtable_from_obj(L, rbval, R);
576
605
  }
@@ -579,7 +608,7 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state R) {
579
608
  }
580
609
  }
581
610
 
582
- static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state R) {
611
+ static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state *R) {
583
612
  VALUE entry;
584
613
  int i, tblidx;
585
614
 
@@ -594,7 +623,7 @@ static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state R) {
594
623
  }
595
624
  }
596
625
 
597
- static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state R) {
626
+ static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state *R) {
598
627
  VALUE keys, key, val;
599
628
  int i, tblidx;
600
629
 
@@ -611,7 +640,7 @@ static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state R)
611
640
  }
612
641
  }
613
642
 
614
- static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state R) {
643
+ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state *R) {
615
644
  VALUE methods, name, method;
616
645
  int i, tblidx;
617
646
 
@@ -627,7 +656,7 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state R) {
627
656
  name = rb_ary_entry(methods, i);
628
657
  method = rb_funcall(obj, rb_intern("method"), 1, name);
629
658
 
630
- if (R.secure && rua_name_is_insecure_method(StringValuePtr(name))) {
659
+ if (R->secure && rua_name_is_insecure_method(StringValuePtr(name))) {
631
660
  continue;
632
661
  }
633
662
 
@@ -638,13 +667,12 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state R) {
638
667
  }
639
668
 
640
669
  static int rua_proc_call(lua_State *L) {
641
- struct rua_state R;
670
+ struct rua_state *R;
642
671
  VALUE proc, args, retval, errargs;
643
672
  int i, n, status;
644
673
 
645
674
  proc = (VALUE) lua_touserdata(L, lua_upvalueindex(1));
646
- R.error_handler = (VALUE) lua_touserdata(L, lua_upvalueindex(2));
647
- R.secure = (int) lua_touserdata(L, lua_upvalueindex(3));
675
+ R = (struct rua_state *) lua_touserdata(L, lua_upvalueindex(2));
648
676
  args = rb_ary_new();
649
677
  n = lua_gettop(L);
650
678
 
@@ -656,10 +684,10 @@ static int rua_proc_call(lua_State *L) {
656
684
  retval = rb_protect(_rua_proc_call, args, &status);
657
685
 
658
686
  if (status != 0) {
659
- if (rua_obj_is_executable(R.error_handler)) {
687
+ if (rua_obj_is_executable(R->error_handler)) {
660
688
  errargs = rb_ary_new();
661
689
  rb_ary_push(errargs, ruby_errinfo);
662
- rb_ary_push(errargs, R.error_handler);
690
+ rb_ary_push(errargs, R->error_handler);
663
691
  retval = rb_protect(_rua_proc_call, errargs, &status);
664
692
 
665
693
  if (status != 0) {
@@ -680,7 +708,7 @@ static VALUE _rua_proc_call(VALUE args) {
680
708
  return rb_apply(proc, rb_intern("call"), args);
681
709
  }
682
710
 
683
- static VALUE rua_toruaobj(VALUE klass, lua_State *L, int idx, struct rua_state R) {
711
+ static VALUE rua_toruaobj(VALUE klass, lua_State *L, int idx, struct rua_state *R) {
684
712
  struct rua_ref *p;
685
713
  VALUE ruaobj;
686
714
 
data/ext/rua.h CHANGED
@@ -8,12 +8,12 @@ struct rua_state {
8
8
 
9
9
  struct rua {
10
10
  lua_State *L;
11
- struct rua_state R;
11
+ struct rua_state *R;
12
12
  };
13
13
 
14
14
  struct rua_ref {
15
15
  lua_State *L;
16
- struct rua_state R;
16
+ struct rua_state *R;
17
17
  int ref;
18
18
  };
19
19
 
@@ -27,6 +27,8 @@ static VALUE rua_get(VALUE self, VALUE key);
27
27
  static VALUE rua_set(VALUE self, VALUE key, VALUE val);
28
28
  static VALUE rua_get_secure(VALUE self);
29
29
  static VALUE rua_set_secure(VALUE self, VALUE secure);
30
+ static VALUE rua_get_error_handler(VALUE self);
31
+ static VALUE rua_set_error_handler(VALUE self, VALUE error_handler);
30
32
  static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self);
31
33
 
32
34
  static VALUE rua_func_alloc(VALUE klass);
@@ -39,18 +41,18 @@ static VALUE rua_thread_alloc(VALUE klass);
39
41
  static VALUE rua_thread_initialize(VALUE self);
40
42
  static VALUE rua_thread_resume(int argc, VALUE *argv, VALUE self);
41
43
 
42
- static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state R);
43
- static VALUE rua_torbval(lua_State *L, int idx, struct rua_state R);
44
+ static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state *R);
45
+ static VALUE rua_torbval(lua_State *L, int idx, struct rua_state *R);
44
46
  static int rua_is_rbobj(lua_State *L, int idx);
45
47
  static VALUE rua_torbobj(lua_State *L, int idx);
46
- static VALUE rua_tohash(lua_State *L, int idx, struct rua_state R);
47
- static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state R);
48
- static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state R);
49
- static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state R);
50
- static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state R);
48
+ static VALUE rua_tohash(lua_State *L, int idx, struct rua_state *R);
49
+ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state *R);
50
+ static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state *R);
51
+ static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state *R);
52
+ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state *R);
51
53
  static int rua_proc_call(lua_State *L);
52
54
  static VALUE _rua_proc_call(VALUE args);
53
- static VALUE rua_toruaobj(VALUE klass, lua_State *L, int idx, struct rua_state R);
55
+ static VALUE rua_toruaobj(VALUE klass, lua_State *L, int idx, struct rua_state *R);
54
56
  static VALUE rua_obj_is_executable(VALUE obj);
55
57
  static int rua_name_is_insecure_method(const char *name);
56
58
  static VALUE rua_to_s(VALUE v);
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: rua
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-11-18 00:00:00 +09:00
6
+ version: 0.2.2
7
+ date: 2007-11-19 00:00:00 +09:00
8
8
  summary: Rua is a library for using Lua under Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -37,11 +37,10 @@ test_files: []
37
37
 
38
38
  rdoc_options:
39
39
  - --title
40
- - Rua - RDoc Documentation
40
+ - Rua - library for using Lua under Ruby.
41
41
  extra_rdoc_files:
42
42
  - README.txt
43
43
  - ext/rua.c
44
- - ext/rua.h
45
44
  executables: []
46
45
 
47
46
  extensions: