rua 0.1.0 → 0.1.1

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.
Files changed (4) hide show
  1. data/README.txt +38 -0
  2. data/ext/rua.c +51 -49
  3. data/ext/rua.h +4 -0
  4. metadata +4 -3
data/README.txt ADDED
@@ -0,0 +1,38 @@
1
+ = rua
2
+
3
+ Copyright (c) 2007 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
4
+
5
+ == Description
6
+
7
+ Rua is a library for using Lua under Ruby.
8
+
9
+ == Example
10
+
11
+ require 'rua'
12
+
13
+ rua = Rua.new
14
+ rua.openlibs(:all)
15
+ #rua.openlibs(:base, :package, :string)
16
+ #rua.secure = false
17
+
18
+ rua[:str] = 'xxx'
19
+ rua[:num] = 100
20
+ rua[:proc] = lambda do
21
+ puts 'Proc called.'
22
+ end
23
+
24
+ puts rua.eval(<<EOS)
25
+ print('hello Rua!')
26
+ print(str)
27
+ print(num)
28
+ proc()
29
+
30
+ f = function()
31
+ print('f() called.')
32
+ end
33
+
34
+ return true
35
+ EOS
36
+
37
+ f = rua[:f]
38
+ f.call
data/ext/rua.c CHANGED
@@ -46,15 +46,16 @@ static const char *insecure_methods[] = {
46
46
  };
47
47
 
48
48
  static const int insecure_method_num = sizeof(insecure_methods) / sizeof(char*);
49
+ static VALUE Rua, RuaFunc, RuaError;
50
+ static VALUE s_all, s_base, s_package, s_string, s_table, s_math, s_io, s_debug;
49
51
 
50
52
  void Init_rua() {
51
- VALUE Rua, RuaFunc, RuaError;
52
-
53
53
  Rua = rb_define_class("Rua", rb_cObject);
54
54
  RuaFunc = rb_define_class("RuaFunc", rb_cObject);
55
55
  RuaError = rb_define_class("RuaError", rb_eStandardError);
56
56
 
57
57
  rb_define_alloc_func(Rua, rua_alloc);
58
+ rb_define_const(Rua, "VERSION", rb_str_new2("0.1.1"));
58
59
  rb_define_private_method(Rua, "initialize", rua_initialize, -1);
59
60
  rb_define_method(Rua, "openlibs", rua_openlibs, -1);
60
61
  rb_define_method(Rua, "eval", rua_eval, 1);
@@ -66,6 +67,15 @@ void Init_rua() {
66
67
  rb_define_alloc_func(RuaFunc, rua_func_alloc);
67
68
  rb_define_private_method(RuaFunc, "initialize", rua_func_initialize, 0);
68
69
  rb_define_method(RuaFunc, "call", rua_func_call, -1);
70
+
71
+ s_all = ID2SYM(rb_intern("all"));
72
+ s_base = ID2SYM(rb_intern("base"));
73
+ s_package = ID2SYM(rb_intern("package"));
74
+ s_string = ID2SYM(rb_intern("string"));
75
+ s_table = ID2SYM(rb_intern("table"));
76
+ s_math = ID2SYM(rb_intern("math"));
77
+ s_io = ID2SYM(rb_intern("io"));
78
+ s_debug = ID2SYM(rb_intern("debug"));
69
79
  }
70
80
 
71
81
  // ------------------------------------------------------------------
@@ -86,7 +96,7 @@ static void rua_free(struct rua *p) {
86
96
 
87
97
  static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
88
98
  struct rua *p;
89
- VALUE error_handler, klass, klassname;
99
+ VALUE error_handler;
90
100
 
91
101
  Data_Get_Struct(self, struct rua, p);
92
102
  p->L = NULL;
@@ -94,9 +104,7 @@ static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
94
104
  if (rb_scan_args(argc, argv, "01", &error_handler) < 1) {
95
105
  error_handler = Qnil;
96
106
  } 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));
107
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc or Method)", rua_classname_ptr(error_handler));
100
108
  }
101
109
 
102
110
  p->L = lua_open();
@@ -107,7 +115,7 @@ static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
107
115
 
108
116
  static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
109
117
  struct rua *p;
110
- VALUE arg, s_all, s_base, s_package, s_string, s_table, s_math, s_io, s_debug;
118
+ VALUE arg;
111
119
  int i;
112
120
 
113
121
  if (argc < 1) {
@@ -120,15 +128,6 @@ static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
120
128
 
121
129
  Data_Get_Struct(self, struct rua, p);
122
130
 
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
131
  for (i = 0; i < argc; i++) {
133
132
  arg = argv[i];
134
133
 
@@ -156,8 +155,7 @@ static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
156
155
  lua_pushcfunction(p->L, luaopen_debug);
157
156
  lua_call(p->L, 0, 0);
158
157
  } 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));
158
+ rb_raise(rb_eArgError, "unknown library '%s' (available: base, package, table, math, io, debug, all)", rua_to_sptr(arg));
161
159
  }
162
160
  }
163
161
 
@@ -166,7 +164,7 @@ static VALUE rua_openlibs(int argc, VALUE *argv, VALUE self) {
166
164
 
167
165
  static VALUE rua_eval(VALUE self, VALUE str) {
168
166
  struct rua *p;
169
- VALUE retval, RuaError;
167
+ VALUE retval;
170
168
  const char *errmsg;
171
169
 
172
170
  Data_Get_Struct(self, struct rua, p);
@@ -190,8 +188,7 @@ static VALUE rua_get(VALUE self, VALUE key) {
190
188
  VALUE retval;
191
189
 
192
190
  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));
191
+ lua_getglobal(p->L, rua_to_sptr(key));
195
192
  retval = rua_torbval(p->L, -1, p->error_handler, p->secure);
196
193
  lua_pop(p->L, 1);
197
194
  return retval;
@@ -199,21 +196,15 @@ static VALUE rua_get(VALUE self, VALUE key) {
199
196
 
200
197
  static VALUE rua_set(VALUE self, VALUE key, VALUE val) {
201
198
  struct rua *p;
202
- VALUE RuaError, inspect, klass, klassname;
203
199
 
204
200
  Data_Get_Struct(self, struct rua, p);
205
201
 
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));
202
+ if (p->secure && (rb_equal(rb_cModule, val) || rb_equal(rb_cClass, val))) {
203
+ rb_raise(RuaError, "set insecure value %s", rua_to_sptr(val));
212
204
  }
213
205
 
214
- key = rb_check_convert_type(key, T_STRING, "String", "to_s");
215
206
  rua_pushrbval(p->L, val, p->error_handler, p->secure);
216
- lua_setglobal(p->L, StringValuePtr(key));
207
+ lua_setglobal(p->L, rua_to_sptr(key));
217
208
  return Qnil;
218
209
  }
219
210
 
@@ -226,7 +217,6 @@ static VALUE rua_get_secure(VALUE self) {
226
217
 
227
218
  static VALUE rua_set_secure(VALUE self, VALUE secure) {
228
219
  struct rua *p;
229
- VALUE klass, klassname;
230
220
 
231
221
  Data_Get_Struct(self, struct rua, p);
232
222
 
@@ -234,9 +224,7 @@ static VALUE rua_set_secure(VALUE self, VALUE secure) {
234
224
  case T_TRUE: p->secure = 1; break;
235
225
  case T_FALSE: p->secure = 0; break;
236
226
  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));
227
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", rua_classname_ptr(secure));
240
228
  break;
241
229
  }
242
230
 
@@ -257,7 +245,7 @@ static VALUE rua_func_initialize(VALUE self) {
257
245
 
258
246
  static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
259
247
  struct rua_func *p;
260
- VALUE retval, RuaError;
248
+ VALUE retval;
261
249
  int i;
262
250
  const char *errmsg;
263
251
 
@@ -269,7 +257,6 @@ static VALUE rua_func_call(int argc, VALUE *argv, VALUE self) {
269
257
  }
270
258
 
271
259
  if (lua_pcall(p->L, argc, 1, 0) != 0) {
272
- RuaError = rb_const_get(rb_cObject, rb_intern("RuaError"));
273
260
  errmsg = lua_tostring(p->L, -1);
274
261
  lua_pop(p->L, 1);
275
262
  rb_raise(RuaError, "%s", errmsg);
@@ -336,7 +323,6 @@ static VALUE rua_tohash(lua_State *L, int idx, VALUE error_handler, int secure)
336
323
 
337
324
  static void rua_pushrbval(lua_State *L, VALUE rbval, VALUE error_handler, int secure) {
338
325
  struct rua_func *p;
339
- VALUE RuaFunc, inspect, klass, klassname;
340
326
 
341
327
  switch (TYPE(rbval)) {
342
328
  case T_NIL:
@@ -370,13 +356,9 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, VALUE error_handler, int se
370
356
  break;
371
357
 
372
358
  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));
359
+ if (secure && (rb_equal(rb_cModule, rbval) || rb_equal(rb_cClass, rbval))) {
360
+ fprintf(stderr, "warning: convert insecure value %s", rua_to_sptr(rbval));
361
+ lua_pushnil(L);
380
362
  } else if (rb_obj_is_kind_of(rbval, RuaFunc)) {
381
363
  Data_Get_Struct(rbval, struct rua_func, p);
382
364
  lua_rawgeti(L, LUA_REGISTRYINDEX, p->ref);
@@ -448,7 +430,7 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, VALUE error_handler,
448
430
  }
449
431
 
450
432
  static int rua_proc_call(lua_State *L) {
451
- VALUE proc, args, retval, error_handler, errargs, errmsg;
433
+ VALUE proc, args, retval, error_handler, errargs;
452
434
  int i, n, status, secure;
453
435
 
454
436
  proc = (VALUE) lua_touserdata(L, lua_upvalueindex(1));
@@ -472,8 +454,7 @@ static int rua_proc_call(lua_State *L) {
472
454
  retval = rb_protect(_rua_proc_call, errargs, &status);
473
455
 
474
456
  if (status != 0) {
475
- errmsg = rb_check_convert_type(ruby_errinfo, T_STRING, "String", "to_s");
476
- fprintf(stderr, "warning: %s\n", StringValuePtr(errmsg));
457
+ fprintf(stderr, "warning: %s\n", rua_to_sptr(ruby_errinfo));
477
458
  }
478
459
  } else {
479
460
  retval = Qnil;
@@ -492,9 +473,8 @@ static VALUE _rua_proc_call(VALUE args) {
492
473
 
493
474
  static VALUE rua_toruafunc(lua_State *L, int idx, VALUE error_handler, int secure) {
494
475
  struct rua_func *p;
495
- VALUE RuaFunc, f;
476
+ VALUE f;
496
477
 
497
- RuaFunc = rb_const_get(rb_cObject, rb_intern("RuaFunc"));
498
478
  f = rb_funcall(RuaFunc, rb_intern("new"), 0);
499
479
  Data_Get_Struct(f, struct rua_func, p);
500
480
  p->L = L;
@@ -521,3 +501,25 @@ static int rua_name_is_insecure_method(const char *name) {
521
501
 
522
502
  return 0;
523
503
  }
504
+
505
+ static VALUE rua_to_s(VALUE v) {
506
+ return rb_check_convert_type(v, T_STRING, "String", "to_s");
507
+ }
508
+
509
+ static const char *rua_to_sptr(VALUE v) {
510
+ VALUE str = rua_to_s(v);
511
+
512
+ return StringValuePtr(str);
513
+ }
514
+
515
+ static VALUE rua_classname(VALUE v) {
516
+ VALUE klass = rb_obj_class(v);
517
+
518
+ return rb_class_name(klass);
519
+ }
520
+
521
+ static const char *rua_classname_ptr(VALUE v) {
522
+ VALUE klassname = rua_classname(v);
523
+
524
+ return StringValuePtr(klassname);
525
+ }
data/ext/rua.h CHANGED
@@ -40,5 +40,9 @@ static VALUE _rua_proc_call(VALUE args);
40
40
  static VALUE rua_toruafunc(lua_State *L, int idx, VALUE error_handler, int secure);
41
41
  static VALUE rua_obj_is_executable(VALUE obj);
42
42
  static int rua_name_is_insecure_method(const char *name);
43
+ static VALUE rua_to_s(VALUE v);
44
+ static const char *rua_to_sptr(VALUE v);
45
+ static VALUE rua_classname(VALUE v);
46
+ static const char *rua_classname_ptr(VALUE v);
43
47
 
44
48
  #endif
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
2
+ 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.0
7
- date: 2007-11-13 00:00:00 +09:00
6
+ version: 0.1.1
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:
10
10
  - lib
@@ -29,6 +29,7 @@ post_install_message:
29
29
  authors:
30
30
  - winebarrel
31
31
  files:
32
+ - README.txt
32
33
  - ext/rua.c
33
34
  - ext/rua.h
34
35
  - ext/extconf.rb