rua 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/rua.c +83 -10
  2. data/ext/rua.h +8 -1
  3. metadata +2 -2
data/ext/rua.c CHANGED
@@ -8,6 +8,7 @@ __declspec(dllexport) void Init_rua(void);
8
8
  #include <malloc.h>
9
9
  #include <errno.h>
10
10
  #include <stdarg.h>
11
+ #include <math.h>
11
12
 
12
13
  #include "lua.h"
13
14
  #include "lualib.h"
@@ -36,7 +37,7 @@ __declspec(dllexport) void Init_rua(void);
36
37
  #define RARRAY_LEN(s) (RARRAY(s)->len)
37
38
  #endif
38
39
 
39
- #define VERSION "0.4.6"
40
+ #define VERSION "0.4.7"
40
41
  #define REF_RBOBJ "self"
41
42
 
42
43
  #define ICONV_JIS "ISO-2022-JP"
@@ -119,7 +120,7 @@ void Init_rua() {
119
120
  //rb_define_const(Rua, "UTF8", rb_str_new2(ICONV_UTF8));
120
121
  rb_define_private_method(Rua, "initialize", rua_initialize, -1);
121
122
  rb_define_method(Rua, "openlibs", rua_openlibs, -1);
122
- rb_define_method(Rua, "eval", rua_eval, 1);
123
+ rb_define_method(Rua, "eval", rua_eval, -1);
123
124
  rb_define_method(Rua, "[]", rua_get, 1);
124
125
  rb_define_method(Rua, "[]=", rua_set, 2);
125
126
  rb_define_method(Rua, "secure", rua_get_secure, 0);
@@ -133,6 +134,8 @@ void Init_rua() {
133
134
  rb_define_method(Rua, "error_handler", rua_get_error_handler, 0);
134
135
  rb_define_method(Rua, "error_handler=", rua_set_error_handler, 1);
135
136
  rb_define_method(Rua, "method_missing", rua_method_missing, -1);
137
+ rb_define_method(Rua, "accept_block", rua_get_accept_block, 0);
138
+ rb_define_method(Rua, "accept_block=", rua_set_accept_block, 1);
136
139
 
137
140
  m_method_unbound = rb_funcall(rb_cObject, rb_intern("instance_method"), 1, ID2SYM(rb_intern("method")));
138
141
  m_methods_unbound = rb_funcall(rb_cObject, rb_intern("instance_method"), 1, ID2SYM(rb_intern("methods")));
@@ -225,6 +228,7 @@ static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
225
228
  p->R->secure = 1;
226
229
  p->R->abort_by_error = 1;
227
230
  p->R->wrap_error = 1;
231
+ p->R->accept_block = 1;
228
232
 
229
233
  if (argc > 0) {
230
234
  rua_openlibs(argc, argv, self);
@@ -289,13 +293,20 @@ static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
289
293
  /**
290
294
  * evaluates string.
291
295
  */
292
- static VALUE rua_eval(VALUE self, VALUE str) {
296
+ static VALUE rua_eval(int argc, VALUE *argv, VALUE self) {
293
297
  struct rua *p;
294
- VALUE errinfo;
298
+ VALUE str, script_name, errinfo;
295
299
  const char *errmsg = NULL;
296
- int pretop, result = -1;
300
+ int pretop, loaded = -1, result = -1;
301
+
302
+ rb_scan_args(argc, argv, "11", &str, &script_name);
297
303
 
298
304
  Check_Type(str, T_STRING);
305
+
306
+ if (!NIL_P(script_name)) {
307
+ Check_Type(script_name, T_STRING);
308
+ }
309
+
299
310
  Data_Get_Struct(self, struct rua, p);
300
311
 
301
312
  if (!NIL_P(p->R->external_charset)) {
@@ -304,7 +315,13 @@ static VALUE rua_eval(VALUE self, VALUE str) {
304
315
 
305
316
  pretop = lua_gettop(p->L);
306
317
 
307
- if (luaL_loadstring(p->L, RSTRING_PTR(str)) != 0) {
318
+ if (NIL_P(script_name)) {
319
+ loaded = luaL_loadstring(p->L, RSTRING_PTR(str));
320
+ } else {
321
+ loaded = luaL_loadbuffer(p->L, RSTRING_PTR(str), RSTRING_LEN(str), RSTRING_PTR(script_name));
322
+ }
323
+
324
+ if (loaded != 0) {
308
325
  int curtop = lua_gettop(p->L);
309
326
 
310
327
  if (!lua_isnil(p->L, -1)) {
@@ -540,11 +557,40 @@ static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self) {
540
557
  }
541
558
  }
542
559
 
560
+ /**
561
+ * get accept block flag.
562
+ */
563
+ static VALUE rua_get_accept_block(VALUE self) {
564
+ struct rua *p;
565
+
566
+ Data_Get_Struct(self, struct rua, p);
567
+ return p->R->accept_block ? Qtrue : Qfalse;
568
+ }
569
+
570
+ /**
571
+ * set accept block flag.
572
+ */
573
+ static VALUE rua_set_accept_block(VALUE self, VALUE accept_block) {
574
+ struct rua *p;
575
+
576
+ Data_Get_Struct(self, struct rua, p);
577
+
578
+ switch (TYPE(accept_block)) {
579
+ case T_TRUE: p->R->accept_block = 1; break;
580
+ case T_FALSE: p->R->accept_block = 0; break;
581
+ default:
582
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", rua_classname_ptr(accept_block));
583
+ break;
584
+ }
585
+
586
+ return Qnil;
587
+ }
588
+
543
589
  // ------------------------------------------------------------------
544
590
 
545
591
  static VALUE rua_ref_alloc(VALUE klass) {
546
592
  struct rua_ref *p = ALLOC(struct rua_ref);
547
-
593
+ p->is_ruby = 0;
548
594
  return Data_Wrap_Struct(klass, rua_ref_mark, rua_ref_free, p);
549
595
  }
550
596
 
@@ -565,6 +611,18 @@ static void rua_ref_free(struct rua_ref *p) {
565
611
  xfree(p);
566
612
  }
567
613
 
614
+ static void rua_ref_set_ruby_flag(VALUE self, int is_ruby) {
615
+ struct rua_ref *p;
616
+ Data_Get_Struct(self, struct rua_ref, p);
617
+ p->is_ruby = is_ruby;
618
+ }
619
+
620
+ static int rua_ref_get_ruby_flag(VALUE self) {
621
+ struct rua_ref *p;
622
+ Data_Get_Struct(self, struct rua_ref, p);
623
+ return p->is_ruby;
624
+ }
625
+
568
626
  // ------------------------------------------------------------------
569
627
 
570
628
  /**
@@ -891,7 +949,7 @@ static VALUE rua_torbval(lua_State *L, int idx, struct rua_state *R) {
891
949
 
892
950
  switch (lua_type(L, idx)) {
893
951
  case LUA_TNUMBER:
894
- rbval = rb_float_new(lua_tonumber(L, idx));
952
+ rbval = rua_torbnum(L, idx);
895
953
  break;
896
954
 
897
955
  case LUA_TBOOLEAN:
@@ -917,7 +975,11 @@ static VALUE rua_torbval(lua_State *L, int idx, struct rua_state *R) {
917
975
  break;
918
976
 
919
977
  case LUA_TFUNCTION:
920
- rbval = rua_toruaobj(RuaFunc, L, idx, R);
978
+ {
979
+ int type_is_cfunction = lua_iscfunction(L, idx);
980
+ rbval = rua_toruaobj(RuaFunc, L, idx, R);
981
+ rua_ref_set_ruby_flag(rbval, type_is_cfunction);
982
+ }
921
983
  break;
922
984
 
923
985
  case LUA_TTHREAD:
@@ -1190,7 +1252,7 @@ static int rua_proc_call(lua_State *L) {
1190
1252
  last = rb_ary_entry(args, -1);
1191
1253
  rb_ary_push(args, proc);
1192
1254
 
1193
- if (rb_obj_is_kind_of(last, RuaFunc)) {
1255
+ if (R->accept_block && rb_obj_is_kind_of(last, RuaFunc) && !rua_ref_get_ruby_flag(last)) {
1194
1256
  retval = rb_protect(_rua_proc_call_with_block, args, &status);
1195
1257
  } else {
1196
1258
  retval = rb_protect(_rua_proc_call, args, &status);
@@ -1433,3 +1495,14 @@ static VALUE rua_iconv(const char *to, const char *from, VALUE in) {
1433
1495
 
1434
1496
  return out;
1435
1497
  }
1498
+
1499
+ static VALUE rua_torbnum(lua_State *L, int idx) {
1500
+ double luanum = lua_tonumber(L, idx);
1501
+ double truncated = floor(luanum);
1502
+
1503
+ if (luanum == truncated) {
1504
+ return INT2NUM(lua_tointeger(L, idx));
1505
+ } else {
1506
+ return rb_float_new(luanum);
1507
+ }
1508
+ }
data/ext/rua.h CHANGED
@@ -9,6 +9,7 @@ struct rua_state {
9
9
  int secure;
10
10
  int abort_by_error;
11
11
  int wrap_error;
12
+ int accept_block;
12
13
  };
13
14
 
14
15
  struct rua {
@@ -20,6 +21,7 @@ struct rua_ref {
20
21
  lua_State *L;
21
22
  struct rua_state *R;
22
23
  int ref;
24
+ int is_ruby;
23
25
  };
24
26
 
25
27
  struct rua_debug {
@@ -45,7 +47,7 @@ static void rua_mark(struct rua *p);
45
47
  static void rua_free(struct rua *p);
46
48
  static VALUE rua_initialize(int argc, VALUE *argv, VALUE self);
47
49
  static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self);
48
- static VALUE rua_eval(VALUE self, VALUE str);
50
+ static VALUE rua_eval(int argc, VALUE *argv, VALUE self);
49
51
  static VALUE rua_get(VALUE self, VALUE key);
50
52
  static VALUE rua_set(VALUE self, VALUE key, VALUE val);
51
53
  static VALUE rua_get_secure(VALUE self);
@@ -59,10 +61,14 @@ static VALUE rua_set_external_charset(VALUE self, VALUE external_charset);
59
61
  static VALUE rua_get_error_handler(VALUE self);
60
62
  static VALUE rua_set_error_handler(VALUE self, VALUE error_handler);
61
63
  static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self);
64
+ static VALUE rua_get_accept_block(VALUE self);
65
+ static VALUE rua_set_accept_block(VALUE self, VALUE accept_block);
62
66
 
63
67
  static VALUE rua_ref_alloc(VALUE klass);
64
68
  static void rua_ref_mark(struct rua_ref *p);
65
69
  static void rua_ref_free(struct rua_ref *p);
70
+ static void rua_ref_set_ruby_flag(VALUE self, int is_ruby);
71
+ static int rua_ref_get_ruby_flag(VALUE self);
66
72
 
67
73
  static VALUE rua_func_initialize(VALUE self);
68
74
  static VALUE rua_func_call(int argc, VALUE *argv, VALUE self);
@@ -118,5 +124,6 @@ static void rua_setmeta(lua_State *L, int idx, const char *key, lua_CFunction f,
118
124
  static void rua_setmeta2(lua_State *L, int idx, const char *key, lua_CFunction f, int n, ...);
119
125
  static int rua_finalize_rbobj(lua_State *L);
120
126
  static VALUE rua_iconv(const char *to, const char *from, VALUE in);
127
+ static VALUE rua_torbnum(lua_State *L, int idx);
121
128
 
122
129
  #endif
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rua
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-21 00:00:00 +09:00
12
+ date: 2008-09-30 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15