rua 0.3.3 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.txt +5 -0
  2. data/ext/extconf.rb +1 -1
  3. data/ext/rua.c +138 -1
  4. data/ext/rua.h +6 -0
  5. metadata +2 -2
data/README.txt CHANGED
@@ -27,6 +27,7 @@ http://storehouse.sakura.ne.jp/rua/
27
27
  class MyClass; end
28
28
 
29
29
  rua = Rua.new(:all)
30
+ #rua.external_charset = Rua::SJIS
30
31
  #rua.openlibs(:base, :package, :string)
31
32
  #rua.secure = false
32
33
  rua.abort_by_error = false
@@ -93,3 +94,7 @@ http://storehouse.sakura.ne.jp/rua/
93
94
  p co.resume('x', 'y')
94
95
 
95
96
  p rua.f.info.to_hash
97
+
98
+ == Notice
99
+ === License for Lua 5.0 and later versions
100
+ Copyright (c) 1994-2007 Lua.org, PUC-Rio.
data/ext/extconf.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'mkmf'
2
2
 
3
3
  dir_config('lua', '/usr/include/lua5.1', '/usr/lib')
4
- if have_header('lua.h') and have_header('lualib.h') and have_header('lauxlib.h') and have_library('lua5.1')
4
+ if have_header('lua.h') and have_header('lualib.h') and have_header('lauxlib.h') and have_library('lua5.1') and have_header('iconv.h') and have_header('errno.h')
5
5
  create_makefile('rua')
6
6
  end
data/ext/rua.c CHANGED
@@ -7,6 +7,8 @@ __declspec(dllexport) void Init_rua(void);
7
7
 
8
8
  #include <stddef.h>
9
9
  #include <string.h>
10
+ #include <malloc.h>
11
+ #include <errno.h>
10
12
 
11
13
  #include "ruby.h"
12
14
 
@@ -14,11 +16,18 @@ __declspec(dllexport) void Init_rua(void);
14
16
  #include "lualib.h"
15
17
  #include "lauxlib.h"
16
18
 
19
+ #include "iconv.h"
20
+
17
21
  #include "rua.h"
18
22
 
19
- #define VERSION "0.3.3"
23
+ #define VERSION "0.3.5"
20
24
  #define REF_RBOBJ "self"
21
25
 
26
+ #define ICONV_JIS "ISO-2022-JP"
27
+ #define ICONV_SJIS "SJIS"
28
+ #define ICONV_EUC "eucJP"
29
+ #define ICONV_UTF8 "UTF-8"
30
+
22
31
  static const char *insecure_methods[] = {
23
32
  "__id__",
24
33
  "__send__",
@@ -87,6 +96,10 @@ void Init_rua() {
87
96
 
88
97
  rb_define_alloc_func(Rua, rua_alloc);
89
98
  rb_define_const(Rua, "VERSION", rb_str_new2(VERSION));
99
+ rb_define_const(Rua, "JIS", rb_str_new2(ICONV_JIS));
100
+ rb_define_const(Rua, "SJIS", rb_str_new2(ICONV_SJIS));
101
+ rb_define_const(Rua, "EUC", rb_str_new2(ICONV_EUC));
102
+ //rb_define_const(Rua, "UTF8", rb_str_new2(ICONV_UTF8));
90
103
  rb_define_private_method(Rua, "initialize", rua_initialize, -1);
91
104
  rb_define_method(Rua, "openlibs", rua_openlibs, -1);
92
105
  rb_define_method(Rua, "eval", rua_eval, 1);
@@ -98,6 +111,8 @@ void Init_rua() {
98
111
  rb_define_method(Rua, "abort_by_error=", rua_set_abort_by_error, 1);
99
112
  rb_define_method(Rua, "wrap_error", rua_get_wrap_error, 0);
100
113
  rb_define_method(Rua, "wrap_error=", rua_set_wrap_error, 1);
114
+ rb_define_method(Rua, "external_charset", rua_get_external_charset, 0);
115
+ rb_define_method(Rua, "external_charset=", rua_set_external_charset, 1);
101
116
  rb_define_method(Rua, "error_handler", rua_get_error_handler, 0);
102
117
  rb_define_method(Rua, "error_handler=", rua_set_error_handler, 1);
103
118
  rb_define_method(Rua, "method_missing", rua_method_missing, -1);
@@ -147,6 +162,7 @@ static VALUE rua_alloc(VALUE klass) {
147
162
  p->R->rua = Qnil;
148
163
  p->R->refs = Qnil;
149
164
  p->R->error_handler = Qnil;
165
+ p->R->external_charset = Qnil;
150
166
  p->R->secure = 1;
151
167
  p->R->abort_by_error = 1;
152
168
  p->R->wrap_error = 1;
@@ -157,6 +173,7 @@ static void rua_mark(struct rua *p) {
157
173
  rb_gc_mark(p->R->rua);
158
174
  rb_gc_mark(p->R->refs);
159
175
  rb_gc_mark(p->R->error_handler);
176
+ rb_gc_mark(p->R->external_charset);
160
177
  }
161
178
 
162
179
  static void rua_free(struct rua *p) {
@@ -182,6 +199,7 @@ static VALUE rua_initialize(int argc, VALUE *argv, VALUE self) {
182
199
  p->R->rua = self;
183
200
  p->R->refs = rb_hash_new();
184
201
  p->R->error_handler = Qnil;
202
+ p->R->external_charset = Qnil;
185
203
  p->R->secure = 1;
186
204
  p->R->abort_by_error = 1;
187
205
  p->R->wrap_error = 1;
@@ -257,6 +275,11 @@ static VALUE rua_eval(VALUE self, VALUE str) {
257
275
 
258
276
  Check_Type(str, T_STRING);
259
277
  Data_Get_Struct(self, struct rua, p);
278
+
279
+ if (!NIL_P(p->R->external_charset)) {
280
+ str = rua_iconv(ICONV_UTF8, StringValuePtr(p->R->external_charset), str);
281
+ }
282
+
260
283
  pretop = lua_gettop(p->L);
261
284
  luaL_loadstring(p->L, StringValuePtr(str));
262
285
 
@@ -396,6 +419,34 @@ static VALUE rua_set_wrap_error(VALUE self, VALUE wrap_error) {
396
419
  return Qnil;
397
420
  }
398
421
 
422
+ /**
423
+ * get external character set.
424
+ */
425
+ static VALUE rua_get_external_charset(VALUE self) {
426
+ struct rua *p;
427
+
428
+ Data_Get_Struct(self, struct rua, p);
429
+ return p->R->external_charset;
430
+ }
431
+
432
+ /**
433
+ * set external character set.
434
+ */
435
+ static VALUE rua_set_external_charset(VALUE self, VALUE external_charset) {
436
+ struct rua *p;
437
+
438
+ Data_Get_Struct(self, struct rua, p);
439
+
440
+ if (NIL_P(external_charset)) {
441
+ p->R->external_charset = Qnil;
442
+ } else {
443
+ Check_Type(external_charset, T_STRING);
444
+ p->R->external_charset = external_charset;
445
+ }
446
+
447
+ return Qnil;
448
+ }
449
+
399
450
  /**
400
451
  * get error handler.
401
452
  */
@@ -458,6 +509,7 @@ static void rua_ref_mark(struct rua_ref *p) {
458
509
  rb_gc_mark(p->R->rua);
459
510
  rb_gc_mark(p->R->refs);
460
511
  rb_gc_mark(p->R->error_handler);
512
+ rb_gc_mark(p->R->external_charset);
461
513
  }
462
514
 
463
515
  static void rua_ref_free(struct rua_ref *p) {
@@ -797,6 +849,11 @@ static VALUE rua_torbval(lua_State *L, int idx, struct rua_state *R) {
797
849
 
798
850
  case LUA_TSTRING:
799
851
  rbval = rb_str_new2(lua_tostring(L, idx));
852
+
853
+ if (!NIL_P(R->external_charset)) {
854
+ rbval = rua_iconv(StringValuePtr(R->external_charset), ICONV_UTF8, rbval);
855
+ }
856
+
800
857
  break;
801
858
 
802
859
  case LUA_TTABLE:
@@ -884,6 +941,10 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state *R) {
884
941
  break;
885
942
 
886
943
  case T_STRING:
944
+ if (!NIL_P(R->external_charset)) {
945
+ rbval = rua_iconv(ICONV_UTF8, StringValuePtr(R->external_charset), rbval);
946
+ }
947
+
887
948
  lua_pushstring(L, StringValuePtr(rbval));
888
949
  break;
889
950
 
@@ -917,6 +978,7 @@ static void rua_pushrbval(lua_State *L, VALUE rbval, struct rua_state *R) {
917
978
  lua_pushlightuserdata(L, (void *) rbval);
918
979
  lua_pushlightuserdata(L, R);
919
980
  lua_pushcclosure(L, rua_proc_call, 2);
981
+ rua_set_finalizer(L, -1, rbval, R);
920
982
  } else {
921
983
  rua_newtable_from_obj(L, rbval, R);
922
984
  }
@@ -967,6 +1029,7 @@ static void rua_newtable_from_obj(lua_State *L, VALUE obj, struct rua_state *R)
967
1029
 
968
1030
  lua_pushstring(L, REF_RBOBJ);
969
1031
  lua_pushlightuserdata(L, (void *) obj);
1032
+ rua_set_finalizer(L, -1, obj, R);
970
1033
  lua_rawset(L, tblidx);
971
1034
  rb_hash_aset(R->refs, obj, Qtrue);
972
1035
 
@@ -1149,3 +1212,77 @@ static const char *rua_classname_ptr(VALUE v) {
1149
1212
 
1150
1213
  return StringValuePtr(klassname);
1151
1214
  }
1215
+
1216
+ static void rua_set_finalizer(lua_State *L, int idx, VALUE rbobj, struct rua_state *R) {
1217
+ int pretop, objidx, tblidx;
1218
+
1219
+ pretop = lua_gettop(L);
1220
+ lua_pushvalue(L, idx);
1221
+ objidx = lua_gettop(L);
1222
+
1223
+ if (!lua_getmetatable(L, objidx)) {
1224
+ lua_newtable(L);
1225
+ lua_setmetatable(L, objidx);
1226
+ lua_getmetatable(L, objidx);
1227
+ }
1228
+
1229
+ tblidx = lua_gettop(L);
1230
+ lua_pushstring(L, "__gc");
1231
+
1232
+ lua_pushlightuserdata(L, (void *) rbobj);
1233
+ lua_pushlightuserdata(L, R);
1234
+ lua_pushcclosure(L, rua_finalize_rbobj, 2);
1235
+
1236
+ lua_rawset(L, tblidx);
1237
+ lua_pop(L, lua_gettop(L) - pretop);
1238
+ }
1239
+
1240
+ static int rua_finalize_rbobj(lua_State *L) {
1241
+ struct rua_state *R;
1242
+ VALUE rbobj;
1243
+
1244
+ if (lua_gettop(L) > 0) {
1245
+ rbobj = (VALUE) lua_touserdata(L, lua_upvalueindex(1));
1246
+ R = (struct rua_state *) lua_touserdata(L, lua_upvalueindex(2));
1247
+
1248
+ if (rbobj) {
1249
+ rb_hash_delete(R->refs, rbobj);
1250
+ }
1251
+ }
1252
+
1253
+ return 0;
1254
+ }
1255
+
1256
+ static VALUE rua_iconv(const char *to, const char *from, VALUE in) {
1257
+ VALUE out;
1258
+ iconv_t cd;
1259
+ char *cin, *cout, *pcout;
1260
+ size_t cin_len, cout_len;
1261
+
1262
+ cd = iconv_open(to, from);
1263
+
1264
+ if ((int) cd == -1) {
1265
+ rb_warn("%s (%s, %s)", to, from, strerror(errno));
1266
+ return Qnil;
1267
+ }
1268
+
1269
+ cin = StringValuePtr(in);
1270
+ cin_len = strlen(cin);
1271
+ cout_len = cin_len * 3 + 1;
1272
+ cout = pcout = alloca(sizeof(char) * cout_len);
1273
+
1274
+ if (iconv(cd, &cin, &cin_len, &pcout, &cout_len) == -1) {
1275
+ rb_warn("%s (%s, %s)", to, from, strerror(errno));
1276
+ iconv_close(cd);
1277
+ return Qnil;
1278
+ }
1279
+
1280
+ *pcout = '\0';
1281
+ out = rb_str_new2(cout);
1282
+
1283
+ if (iconv_close(cd) != 0) {
1284
+ rb_warn("%s (%s, %s)", to, from, strerror(errno));
1285
+ }
1286
+
1287
+ return out;
1288
+ }
data/ext/rua.h CHANGED
@@ -5,6 +5,7 @@ struct rua_state {
5
5
  VALUE rua;
6
6
  VALUE refs;
7
7
  VALUE error_handler;
8
+ VALUE external_charset;
8
9
  int secure;
9
10
  int abort_by_error;
10
11
  int wrap_error;
@@ -53,6 +54,8 @@ static VALUE rua_get_abort_by_error(VALUE self);
53
54
  static VALUE rua_set_abort_by_error(VALUE self, VALUE abort_by_error);
54
55
  static VALUE rua_get_wrap_error(VALUE self);
55
56
  static VALUE rua_set_wrap_error(VALUE self, VALUE wrap_error);
57
+ static VALUE rua_get_external_charset(VALUE self);
58
+ static VALUE rua_set_external_charset(VALUE self, VALUE external_charset);
56
59
  static VALUE rua_get_error_handler(VALUE self);
57
60
  static VALUE rua_set_error_handler(VALUE self, VALUE error_handler);
58
61
  static VALUE rua_method_missing(int argc, VALUE *argv, VALUE self);
@@ -109,5 +112,8 @@ static VALUE rua_to_s(VALUE v);
109
112
  static const char *rua_to_sptr(VALUE v);
110
113
  static VALUE rua_classname(VALUE v);
111
114
  static const char *rua_classname_ptr(VALUE v);
115
+ static int rua_finalize_rbobj(lua_State *L);
116
+ static void rua_set_finalizer(lua_State *L, int idx, VALUE rbobj, struct rua_state *R);
117
+ static VALUE rua_iconv(const char *to, const char *from, VALUE in);
112
118
 
113
119
  #endif
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.3
7
- date: 2007-11-29 00:00:00 +09:00
6
+ version: 0.3.5
7
+ date: 2007-12-10 00:00:00 +09:00
8
8
  summary: Rua is a library for using Lua under Ruby.
9
9
  require_paths:
10
10
  - lib