rua 0.3.1-mswin32 → 0.3.3-mswin32

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 +3 -2
  2. data/ext/rua.c +149 -25
  3. data/lib/i386-mswin32/rua.so +0 -0
  4. metadata +2 -2
data/README.txt CHANGED
@@ -30,9 +30,10 @@ http://storehouse.sakura.ne.jp/rua/
30
30
  #rua.openlibs(:base, :package, :string)
31
31
  #rua.secure = false
32
32
  rua.abort_by_error = false
33
- rua.error_handler = lambda do |e, ar|
33
+ rua.error_handler = lambda do |e|
34
34
  p e
35
- p ar.to_hash
35
+ p e.cause
36
+ p e.info.to_hash
36
37
  end
37
38
 
38
39
  rua.str = 'xxx'
data/ext/rua.c CHANGED
@@ -16,7 +16,7 @@ __declspec(dllexport) void Init_rua(void);
16
16
 
17
17
  #include "rua.h"
18
18
 
19
- #define VERSION "0.3.1"
19
+ #define VERSION "0.3.3"
20
20
  #define REF_RBOBJ "self"
21
21
 
22
22
  static const char *insecure_methods[] = {
@@ -96,6 +96,8 @@ void Init_rua() {
96
96
  rb_define_method(Rua, "secure=", rua_set_secure, 1);
97
97
  rb_define_method(Rua, "abort_by_error", rua_get_abort_by_error, 0);
98
98
  rb_define_method(Rua, "abort_by_error=", rua_set_abort_by_error, 1);
99
+ rb_define_method(Rua, "wrap_error", rua_get_wrap_error, 0);
100
+ rb_define_method(Rua, "wrap_error=", rua_set_wrap_error, 1);
99
101
  rb_define_method(Rua, "error_handler", rua_get_error_handler, 0);
100
102
  rb_define_method(Rua, "error_handler=", rua_set_error_handler, 1);
101
103
  rb_define_method(Rua, "method_missing", rua_method_missing, -1);
@@ -109,6 +111,10 @@ void Init_rua() {
109
111
  rb_define_private_method(RuaThread, "initialize", rua_thread_initialize, 0);
110
112
  rb_define_method(RuaThread, "resume", rua_thread_resume, -1);
111
113
 
114
+ rb_define_alloc_func(RuaError, rua_error_alloc);
115
+ rb_define_method(RuaError, "cause", rua_error_cause, 0);
116
+ rb_define_method(RuaError, "info", rua_error_info, 0);
117
+
112
118
  rb_define_alloc_func(RuaDebug, rua_debug_alloc);
113
119
  rb_define_private_method(RuaDebug, "initialize", rua_debug_initialize, 0);
114
120
  rb_define_method(RuaDebug, "name", rua_debug_name, 0);
@@ -139,14 +145,17 @@ static VALUE rua_alloc(VALUE klass) {
139
145
 
140
146
  p->R = ALLOC(struct rua_state);
141
147
  p->R->rua = Qnil;
148
+ p->R->refs = Qnil;
142
149
  p->R->error_handler = Qnil;
143
150
  p->R->secure = 1;
144
151
  p->R->abort_by_error = 1;
152
+ p->R->wrap_error = 1;
145
153
  return Data_Wrap_Struct(klass, rua_mark, rua_free, p);
146
154
  }
147
155
 
148
156
  static void rua_mark(struct rua *p) {
149
157
  rb_gc_mark(p->R->rua);
158
+ rb_gc_mark(p->R->refs);
150
159
  rb_gc_mark(p->R->error_handler);
151
160
  }
152
161
 
@@ -171,9 +180,11 @@ static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
171
180
  Data_Get_Struct(self, struct rua, p);
172
181
  p->L = lua_open();
173
182
  p->R->rua = self;
183
+ p->R->refs = rb_hash_new();
174
184
  p->R->error_handler = Qnil;
175
185
  p->R->secure = 1;
176
186
  p->R->abort_by_error = 1;
187
+ p->R->wrap_error = 1;
177
188
 
178
189
  if (argc > 0) {
179
190
  rua_openlibs(argc, argv, self);
@@ -240,6 +251,7 @@ static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
240
251
  */
241
252
  static VALUE rua_eval(VALUE self, VALUE str) {
242
253
  struct rua *p;
254
+ VALUE errinfo;
243
255
  const char *errmsg;
244
256
  int pretop;
245
257
 
@@ -249,9 +261,18 @@ static VALUE rua_eval(VALUE self, VALUE str) {
249
261
  luaL_loadstring(p->L, StringValuePtr(str));
250
262
 
251
263
  if (lua_pcall(p->L, 0, LUA_MULTRET, 0) != 0) {
252
- errmsg = lua_tostring(p->L, -1);
253
- lua_pop(p->L, 1);
254
- rb_raise(RuaError, "%s", errmsg);
264
+ if (lua_islightuserdata(p->L, -1)) {
265
+ errinfo = (VALUE) lua_touserdata(p->L, -1);
266
+ lua_pop(p->L, 1);
267
+ rb_exc_raise(errinfo);
268
+ } else if (lua_isstring(p->L, -1)){
269
+ errmsg = lua_tostring(p->L, -1);
270
+ lua_pop(p->L, 1);
271
+ rb_raise(RuaError, "%s", errmsg);
272
+ } else {
273
+ lua_pop(p->L, 1);
274
+ rb_raise(RuaError, "must not happen");
275
+ }
255
276
  }
256
277
 
257
278
  return rua_tomultiretval(p->L, pretop, p->R);
@@ -346,6 +367,35 @@ static VALUE rua_set_abort_by_error(VALUE self, VALUE abort_by_error) {
346
367
  return Qnil;
347
368
  }
348
369
 
370
+ /**
371
+ * get wrap error flag.
372
+ */
373
+ static VALUE rua_get_wrap_error(VALUE self) {
374
+ struct rua *p;
375
+
376
+ Data_Get_Struct(self, struct rua, p);
377
+ return p->R->wrap_error ? Qtrue : Qfalse;
378
+ }
379
+
380
+ /**
381
+ * set wrap error flag.
382
+ */
383
+ static VALUE rua_set_wrap_error(VALUE self, VALUE wrap_error) {
384
+ struct rua *p;
385
+
386
+ Data_Get_Struct(self, struct rua, p);
387
+
388
+ switch (TYPE(wrap_error)) {
389
+ case T_TRUE: p->R->wrap_error = 1; break;
390
+ case T_FALSE: p->R->wrap_error = 0; break;
391
+ default:
392
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", rua_classname_ptr(wrap_error));
393
+ break;
394
+ }
395
+
396
+ return Qnil;
397
+ }
398
+
349
399
  /**
350
400
  * get error handler.
351
401
  */
@@ -406,11 +456,12 @@ static VALUE rua_ref_alloc(VALUE klass) {
406
456
 
407
457
  static void rua_ref_mark(struct rua_ref *p) {
408
458
  rb_gc_mark(p->R->rua);
459
+ rb_gc_mark(p->R->refs);
409
460
  rb_gc_mark(p->R->error_handler);
410
461
  }
411
462
 
412
463
  static void rua_ref_free(struct rua_ref *p) {
413
- luaL_unref(p->L, LUA_REGISTRYINDEX, p->ref);
464
+ //luaL_unref(p->L, LUA_REGISTRYINDEX, p->ref);
414
465
 
415
466
  if (p->R) {
416
467
  free(p->R);
@@ -434,6 +485,7 @@ static VALUE rua_func_initialize(VALUE self) {
434
485
  static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
435
486
  struct rua_ref *p;
436
487
  int pretop, i;
488
+ VALUE errinfo;
437
489
  const char *errmsg;
438
490
 
439
491
  Data_Get_Struct(self, struct rua_ref, p);
@@ -445,9 +497,18 @@ static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
445
497
  }
446
498
 
447
499
  if (lua_pcall(p->L, argc, LUA_MULTRET, 0) != 0) {
448
- errmsg = lua_tostring(p->L, -1);
449
- lua_pop(p->L, 1);
450
- rb_raise(RuaError, "%s", errmsg);
500
+ if (lua_islightuserdata(p->L, -1)) {
501
+ errinfo = (VALUE) lua_touserdata(p->L, -1);
502
+ lua_pop(p->L, 1);
503
+ rb_exc_raise(errinfo);
504
+ } else if (lua_isstring(p->L, -1)){
505
+ errmsg = lua_tostring(p->L, -1);
506
+ lua_pop(p->L, 1);
507
+ rb_raise(RuaError, "%s", errmsg);
508
+ } else {
509
+ lua_pop(p->L, 1);
510
+ rb_raise(RuaError, "must not happen");
511
+ }
451
512
  }
452
513
 
453
514
  return rua_tomultiretval(p->L, pretop, p->R);
@@ -487,7 +548,7 @@ static VALUE rua_thread_initialize(VALUE self) {
487
548
  */
488
549
  static VALUE rua_thread_resume(int argc, VALUE *argv, VALUE self) {
489
550
  struct rua_ref *p;
490
- VALUE retval;
551
+ VALUE retval, errinfo;
491
552
  int pretop, i;
492
553
  const char *errmsg;
493
554
 
@@ -503,9 +564,18 @@ static VALUE rua_thread_resume(int argc, VALUE *argv, VALUE self) {
503
564
  }
504
565
 
505
566
  if (lua_pcall(p->L, argc + 1, LUA_MULTRET, 0) != 0) {
506
- errmsg = lua_tostring(p->L, -1);
507
- lua_pop(p->L, 2);
508
- rb_raise(RuaError, "%s", errmsg);
567
+ if (lua_islightuserdata(p->L, -1)) {
568
+ errinfo = (VALUE) lua_touserdata(p->L, -1);
569
+ lua_pop(p->L, 2);
570
+ rb_exc_raise(errinfo);
571
+ } else if (lua_isstring(p->L, -1)){
572
+ errmsg = lua_tostring(p->L, -1);
573
+ lua_pop(p->L, 2);
574
+ rb_raise(RuaError, "%s", errmsg);
575
+ } else {
576
+ lua_pop(p->L, 2);
577
+ rb_raise(RuaError, "must not happen");
578
+ }
509
579
  }
510
580
 
511
581
  retval = rua_tomultiretval(p->L, pretop, p->R);
@@ -654,6 +724,41 @@ static VALUE rua_debug_to_hash(VALUE self) {
654
724
 
655
725
  // ------------------------------------------------------------------
656
726
 
727
+ static VALUE rua_error_alloc(VALUE klass) {
728
+ struct rua_error *p = ALLOC(struct rua_error);
729
+
730
+ p->cause = Qnil;
731
+ p->info = Qnil;
732
+ return Data_Wrap_Struct(klass, rua_error_mark, -1, p);
733
+ }
734
+
735
+ static void rua_error_mark(struct rua_error *p) {
736
+ rb_gc_mark(p->cause);
737
+ rb_gc_mark(p->info);
738
+ }
739
+
740
+ /**
741
+ * get cause error.
742
+ */
743
+ static VALUE rua_error_cause(VALUE self) {
744
+ struct rua_error *p;
745
+
746
+ Data_Get_Struct(self, struct rua_error, p);
747
+ return p->cause;
748
+ }
749
+
750
+ /**
751
+ * get debug info.
752
+ */
753
+ static VALUE rua_error_info(VALUE self) {
754
+ struct rua_error *p;
755
+
756
+ Data_Get_Struct(self, struct rua_error, p);
757
+ return p->info;
758
+ }
759
+
760
+ // ------------------------------------------------------------------
761
+
657
762
  static VALUE rua_tomultiretval(lua_State *L, int pretop, struct rua_state *R) {
658
763
  VALUE retval;
659
764
  int nresults, i;
@@ -831,7 +936,7 @@ static void rua_newtable_from_ary(lua_State *L, VALUE ary, struct rua_state *R)
831
936
  entry = rb_ary_entry(ary, i);
832
937
  lua_pushnumber(L, i + 1);
833
938
  rua_pushrbval(L, entry, R);
834
- lua_settable(L, tblidx);
939
+ lua_rawset(L, tblidx);
835
940
  }
836
941
  }
837
942
 
@@ -848,7 +953,7 @@ static void rua_newtable_from_hash(lua_State *L, VALUE hash, struct rua_state *R
848
953
  val = rb_hash_aref(hash, key);
849
954
  rua_pushrbval(L, key, R);
850
955
  rua_pushrbval(L, val, R);
851
- lua_settable(L, tblidx);
956
+ lua_rawset(L, tblidx);
852
957
  }
853
958
  }
854
959
 
@@ -862,11 +967,13 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state *R)
862
967
 
863
968
  lua_pushstring(L, REF_RBOBJ);
864
969
  lua_pushlightuserdata(L, (void *) obj);
865
- lua_settable(L, tblidx);
970
+ lua_rawset(L, tblidx);
971
+ rb_hash_aset(R->refs, obj, Qtrue);
866
972
 
867
973
  for (i = 0; i < RARRAY(methods)->len; i++) {
868
974
  name = rb_ary_entry(methods, i);
869
975
  method = rb_funcall(obj, rb_intern("method"), 1, name);
976
+ rb_hash_aset(R->refs, method, Qtrue);
870
977
 
871
978
  if (R->secure && rua_name_is_insecure_method(StringValuePtr(name))) {
872
979
  continue;
@@ -874,13 +981,13 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state *R)
874
981
 
875
982
  rua_pushrbval(L, name, R);
876
983
  rua_pushrbval(L, method, R);
877
- lua_settable(L, tblidx);
984
+ lua_rawset(L, tblidx);
878
985
  }
879
986
  }
880
987
 
881
988
  static int rua_proc_call(lua_State *L) {
882
989
  struct rua_state *R;
883
- VALUE proc, args, last, retval, errargs, errmsg;
990
+ VALUE proc, args, last, retval, errargs, error;
884
991
  int i, n, status;
885
992
  lua_Debug ar;
886
993
 
@@ -903,16 +1010,19 @@ static int rua_proc_call(lua_State *L) {
903
1010
  }
904
1011
 
905
1012
  if (status != 0) {
906
- errmsg = rb_check_convert_type(ruby_errinfo, T_STRING, "String", "to_s");
907
-
908
- if (rua_obj_is_executable(R->error_handler)) {
909
- errargs = rb_ary_new();
910
- rb_ary_push(errargs, ruby_errinfo);
911
-
1013
+ if (R->wrap_error) {
912
1014
  if (lua_getstack(L, 1, &ar) && lua_getinfo(L, "nSlu", &ar)) {
913
- rb_ary_push(errargs, rua_toruadebug(&ar));
1015
+ error = rua_toruaerror(ruby_errinfo, rua_toruadebug(&ar));
1016
+ } else {
1017
+ error = rua_toruaerror(ruby_errinfo, Qnil);
914
1018
  }
1019
+ } else {
1020
+ error = ruby_errinfo;
1021
+ }
915
1022
 
1023
+ if (rua_obj_is_executable(R->error_handler)) {
1024
+ errargs = rb_ary_new();
1025
+ rb_ary_push(errargs, error);
916
1026
  rb_ary_push(errargs, R->error_handler);
917
1027
  retval = rb_protect(_rua_proc_call, errargs, &status);
918
1028
 
@@ -924,7 +1034,8 @@ static int rua_proc_call(lua_State *L) {
924
1034
  }
925
1035
 
926
1036
  if (R->abort_by_error) {
927
- return luaL_error(L, "%s", StringValuePtr(errmsg));
1037
+ lua_pushlightuserdata(L, (void *) error);
1038
+ return lua_error(L);
928
1039
  }
929
1040
  }
930
1041
 
@@ -967,9 +1078,22 @@ static VALUE rua_toruaobj(VALUE klass, lua_State *L, int idx, struct rua_state *
967
1078
  p->R = R;
968
1079
  lua_pushvalue(L, idx);
969
1080
  p->ref = luaL_ref(L, LUA_REGISTRYINDEX);
1081
+ rb_hash_aset(p->R->refs, ruaobj, Qtrue);
970
1082
  return ruaobj;
971
1083
  }
972
1084
 
1085
+ static VALUE rua_toruaerror(VALUE cause, VALUE ruadebug) {
1086
+ struct rua_error *p;
1087
+ VALUE errmsg, ruaerr;
1088
+
1089
+ errmsg = rb_check_convert_type(cause, T_STRING, "String", "to_s");
1090
+ ruaerr = rb_funcall(RuaError, rb_intern("new"), 1, errmsg);
1091
+ Data_Get_Struct(ruaerr, struct rua_error, p);
1092
+ p->cause = cause;
1093
+ p->info = ruadebug;
1094
+ return ruaerr;
1095
+ }
1096
+
973
1097
  static VALUE rua_toruadebug(lua_Debug *ar) {
974
1098
  struct rua_debug *p;
975
1099
  VALUE ruadebug;
Binary file
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: rua
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-11-25 00:00:00 +09:00
6
+ version: 0.3.3
7
+ date: 2007-11-29 00:00:00 +09:00
8
8
  summary: Rua is a library for using Lua under Ruby.
9
9
  require_paths:
10
10
  - lib/i386-mswin32