ruby-lua 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 90979ca5da80b6a4c4f271d82635e741270824fc
4
+ data.tar.gz: a8b68ebcfa8d8303ff4d31fa02d34be07dc27213
5
+ SHA512:
6
+ metadata.gz: ef5523c5f19f020f54cbada929185839c82a092d1c3e86aefe119350aeb0c857699aa894eb9502ac7691274560b7f3e9382984b7736e6ea3d241aaf8c0d1ef09
7
+ data.tar.gz: d475870087b3db055e8fea91037b4bef14981ef4310b765653df1767c4c5e3d56a970d26b3a8628a284f48a6452c611244513698bc1b4545df907ba541cff874
@@ -0,0 +1,29 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ # For MacOS:
18
+ .DS_Store
19
+ .rake_tasks
20
+
21
+ # For ext
22
+ Makefile
23
+ ext/config.h
24
+ *.bundle
25
+ *.so
26
+ *.dll
27
+ *.swp
28
+ *.log
29
+ *.o
data/Gemfile CHANGED
@@ -1,14 +1,2 @@
1
- source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
-
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
8
- group :development do
9
- gem "shoulda", ">= 0"
10
- gem "bundler", "~> 1.0.0"
11
- gem "jeweler", "~> 1.5.1"
12
- gem "rcov", ">= 0"
13
- gem "rdoc", ">=0"
14
- end
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -1,24 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-lua (0.4.0)
5
+
1
6
  GEM
2
- remote: http://rubygems.org/
7
+ remote: https://rubygems.org/
3
8
  specs:
4
- git (1.2.5)
5
- jeweler (1.5.2)
6
- bundler (~> 1.0.0)
7
- git (>= 1.2.5)
8
- rake
9
- json (1.6.3)
10
- rake (0.9.2.2)
11
- rcov (0.9.11)
12
- rdoc (3.11)
9
+ activesupport (3.2.13)
10
+ i18n (= 0.6.1)
11
+ multi_json (~> 1.0)
12
+ bourne (1.4.0)
13
+ mocha (~> 0.13.2)
14
+ i18n (0.6.1)
15
+ json (1.7.7)
16
+ metaclass (0.0.1)
17
+ mocha (0.13.3)
18
+ metaclass (~> 0.0.1)
19
+ multi_json (1.7.2)
20
+ rake (10.0.4)
21
+ rdoc (4.0.1)
13
22
  json (~> 1.4)
14
- shoulda (2.11.3)
23
+ shoulda (3.4.0)
24
+ shoulda-context (~> 1.0, >= 1.0.1)
25
+ shoulda-matchers (~> 1.0, >= 1.4.1)
26
+ shoulda-context (1.1.1)
27
+ shoulda-matchers (1.5.6)
28
+ activesupport (>= 3.0.0)
29
+ bourne (~> 1.3)
15
30
 
16
31
  PLATFORMS
17
32
  ruby
18
33
 
19
34
  DEPENDENCIES
20
- bundler (~> 1.0.0)
21
- jeweler (~> 1.5.1)
22
- rcov
35
+ bundler
36
+ rake
23
37
  rdoc
38
+ ruby-lua!
24
39
  shoulda
@@ -33,7 +33,7 @@ Load a Lua Script
33
33
  Call the Lua function named <lua_function>
34
34
  This is equivalent to
35
35
 
36
- Language::Lua.call( false, "<lua_function>", ... )
36
+ Language::Lua.call( false, "<lua_function>", ... )
37
37
 
38
38
  === Language::Lua.call( [true|false], "<lua_function>", ... )
39
39
 
@@ -69,6 +69,6 @@ In your Lua code, call <ruby_function>.
69
69
 
70
70
  == Copyright
71
71
 
72
- Copyright (c) 2010 Gregoire Lejeune. See LICENSE.txt for
72
+ Copyright (c) 2010-2013 Gregoire Lejeune. See LICENSE.txt for
73
73
  further details.
74
74
 
data/Rakefile CHANGED
@@ -1,49 +1,19 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
10
3
  require 'rake'
11
-
12
- require 'jeweler'
13
- Jeweler::Tasks.new do |gem|
14
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
- gem.name = "ruby-lua"
16
- gem.homepage = "http://github.com/glejeune/ruby-lua"
17
- gem.license = "MIT"
18
- gem.summary = %Q{Call Lua from Ruby (and Ruby from Lua in Ruby)}
19
- gem.description = %Q{Call Lua from Ruby (and Ruby from Lua in Ruby)}
20
- gem.email = "gregoire.lejeune@free.fr"
21
- gem.authors = ["Gregoire Lejeune"]
22
- gem.extensions = FileList["ext/**/extconf.rb"].to_a
23
- # Include your dependencies below. Runtime dependencies are required when using your gem,
24
- # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
25
- # gem.add_runtime_dependency 'jabber4r', '> 0.1'
26
- # gem.add_development_dependency 'rspec', '> 1.2.3'
27
- end
28
- Jeweler::RubygemsDotOrgTasks.new
29
-
30
4
  require 'rake/testtask'
5
+ require 'rubygems/package_task'
6
+ require 'rdoc/task'
7
+
31
8
  Rake::TestTask.new(:test) do |test|
32
9
  test.libs << 'lib' << 'test'
33
10
  test.pattern = 'test/**/test_*.rb'
34
11
  test.verbose = true
35
12
  end
36
-
37
- require 'rcov/rcovtask'
38
- Rcov::RcovTask.new do |test|
39
- test.libs << 'test'
40
- test.pattern = 'test/**/test_*.rb'
41
- test.verbose = true
42
- end
13
+ task :test => :compile
43
14
 
44
15
  task :default => :test
45
16
 
46
- require 'rdoc/task'
47
17
  RDoc::Task.new do |rdoc|
48
18
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
19
 
@@ -52,3 +22,18 @@ RDoc::Task.new do |rdoc|
52
22
  rdoc.rdoc_files.include('README*')
53
23
  rdoc.rdoc_files.include('lib/**/*.rb')
54
24
  end
25
+
26
+ task :compile do
27
+ chdir 'ext' do
28
+ ruby 'extconf.rb'
29
+ sh 'make'
30
+ end
31
+ end
32
+
33
+ task :clean do
34
+ chdir 'ext' do
35
+ sh 'rm -f *.bundle *.dll *.so *.o *.obj Makefile config.h mkmf.log'
36
+ end
37
+ end
38
+
39
+ Bundler::GemHelper.install_tasks
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -1,5 +1,5 @@
1
1
  -- defines a factorial function
2
- function fact (n)
2
+ function fact(n)
3
3
  if n == 0 then
4
4
  return 1
5
5
  else
@@ -8,7 +8,7 @@ function fact (n)
8
8
  end
9
9
 
10
10
  -- define a stupid function
11
- function hello( )
11
+ function hello()
12
12
  return "world"
13
13
  end
14
14
 
@@ -18,4 +18,4 @@ str = "blah blah blah"
18
18
  -- define a number variable
19
19
  num = 123
20
20
 
21
- fact( 3 )
21
+ fact(3)
@@ -2,48 +2,65 @@
2
2
  # See the LICENSE file for copyright and distribution information
3
3
 
4
4
  require "mkmf"
5
+ init_mkmf
5
6
 
6
- $LIBPATH.push(Config::CONFIG['libdir'])
7
+ $LIBPATH.push(RbConfig::CONFIG['libdir'])
7
8
 
8
9
  def crash(str)
9
10
  printf(" extconf failure: %s\n", str)
10
11
  exit 1
11
12
  end
12
13
 
13
- lib = ""
14
-
15
- #unless have_library( 'm', 'atan' )
16
- # crash( 'need libm' )
17
- #end
18
14
  if /^1.8/.match RUBY_VERSION
19
- $CFLAGS << " -DRUBY_1_8"
15
+ $CFLAGS << " -DRUBY_1_8"
20
16
  end
21
17
 
22
18
  if dir_config( "lua50" ) != [nil, nil]
23
- inc, lib = dir_config( 'lua50' )
24
- $LDFLAGS << " -L#{lib} -llualib50 -llua50"
25
- $CFLAGS << " -I#{inc}"
19
+ inc, lib = dir_config( 'lua50' )
20
+ $LDFLAGS << " -L#{lib} -llualib50 -llua50"
21
+ $CFLAGS << " -I#{inc}"
26
22
  elsif dir_config( "lua" ) != [nil, nil]
27
- inc, lib = dir_config( 'lua' )
28
- $LDFLAGS << " -L#{lib} -llualib -llua"
29
- $CFLAGS << " -I#{inc}"
23
+ inc, lib = dir_config( 'lua' )
24
+ $LDFLAGS << " -L#{lib} -llualib -llua"
25
+ $CFLAGS << " -I#{inc}"
26
+ elsif dir_config( "lua51" ) != [nil, nil]
27
+ inc, lib = dir_config( 'lua51' )
28
+ $LDFLAGS << " -L#{lib} -llua5.1"
29
+ $CFLAGS << " -I#{inc}"
30
+ elsif dir_config( "lua52" ) != [nil, nil]
31
+ inc, lib = dir_config( 'lua52' )
32
+ $LDFLAGS << " -L#{lib} -llua5.2"
33
+ $CFLAGS << " -I#{inc}"
30
34
  elsif ex = find_executable( "lua-config" )
31
- $LDFLAGS << ' ' + `#{ex} --libs`.chomp
32
- $CFLAGS << ' ' + `#{ex} --include`.chomp
35
+ $LDFLAGS << ' ' + `#{ex} --libs`.chomp
36
+ $CFLAGS << ' ' + `#{ex} --include`.chomp
33
37
  elsif ex = find_executable( "pkg-config" )
34
- $LDFLAGS << ' ' + `#{ex} --libs lua`.chomp
35
- $CFLAGS << ' ' + `#{ex} --cflags lua`.chomp
38
+ ["lua", "lua5.1", "lua5.2"].any? do |l|
39
+ ldf = `#{ex} --libs #{l}`.chomp
40
+ cf = `#{ex} --cflags #{l}`.chomp
41
+ next false if ldf.empty? or cf.empty?
42
+
43
+ $LDFLAGS << " #{ldf}"
44
+ $CFLAGS << " #{cf}"
45
+ true
46
+ end
36
47
  else
37
48
  crash(<<EOL)
38
- need liblua50.
49
+ need lua
39
50
 
40
51
  Install the library or try one of the following options to extconf.rb:
41
52
 
42
53
  --with-lua-lib=/path/to/liblua/lib
43
54
  --with-lua-include=/path/to/liblua/include
44
- or
55
+ or
45
56
  --with-lua50-lib=/path/to/liblua/lib
46
57
  --with-lua50-include=/path/to/liblua/include
58
+ or
59
+ --with-lua51-lib=/path/to/liblua/lib
60
+ --with-lua51-include=/path/to/liblua/include
61
+ or
62
+ --with-lua52-lib=/path/to/liblua/lib
63
+ --with-lua52-include=/path/to/liblua/include
47
64
  EOL
48
65
  end
49
66
 
@@ -55,7 +72,7 @@ need liblua50 (headers not found)
55
72
 
56
73
  --with-lua-lib=/path/to/liblua/lib
57
74
  --with-lua-include=/path/to/liblua/include
58
- or
75
+ or
59
76
  --with-lua50-lib=/path/to/liblua/lib
60
77
  --with-lua50-include=/path/to/liblua/include
61
78
  EOL
@@ -66,7 +83,16 @@ puts " CFLAGS = #{$CFLAGS}"
66
83
  puts " LDFLAGS = #{$LDFLAGS}"
67
84
  puts
68
85
 
69
- $CFLAGS = '-g -Wall ' + $CFLAGS
86
+ #$CFLAGS = '-g -Wall ' + $CFLAGS
87
+ HAVE_LUA_GLOBALSINDEX = have_macro("LUA_GLOBALSINDEX", "lua.h")
88
+ RUBY_LUA_VERSION = open(File.join(File.dirname(__FILE__), "..", "VERSION")).read.chomp
89
+ open("config.h", 'w') { |f|
90
+ f.puts "#ifndef RUBY_LUA_CONFIG"
91
+ f.puts "#define RUBY_LUA_CONFIG"
92
+ f.puts "#define RUBY_LUA_VERSION \"#{RUBY_LUA_VERSION}\""
93
+ f.puts "#define HAVE_LUA_GLOBALSINDEX 1" if HAVE_LUA_GLOBALSINDEX
94
+ f.puts "#endif"
95
+ }
70
96
 
71
- create_header()
97
+ #create_header()
72
98
  create_makefile("language/lua")
data/ext/rlua.c CHANGED
@@ -18,6 +18,12 @@
18
18
 
19
19
  #include "rlua.h"
20
20
 
21
+ VALUE mLanguage;
22
+ VALUE cLua;
23
+
24
+ VALUE ruby_lua_stack_size(VALUE self);
25
+ VALUE ruby_lua_stack_dump(VALUE self);
26
+
21
27
  #define _W_STACK(L) {\
22
28
  int i;\
23
29
  int iStackSize = lua_gettop( L );\
@@ -129,9 +135,21 @@ static void lua_SetVarToStack( lua_State *L, VALUE vVarValue ) {
129
135
  * Desc: Positionne une valeur nomm�e Ruby dans l'index global Lua
130
136
  */
131
137
  static void lua_SetNamedVarFromRuby( lua_State *L, VALUE vVarName, VALUE vVarValue ) {
138
+ #ifdef HAVE_LUA_GLOBALSINDEX
132
139
  lua_pushlstring( L, RSTRING_PTR(vVarName), RSTRING_LEN(vVarName) );
133
- lua_SetVarToStack( L, vVarValue );
134
- lua_settable( L, LUA_GLOBALSINDEX );
140
+ #else
141
+ char * var_name = (char*)malloc(sizeof(char)*(RSTRING_LEN(vVarName)+1));
142
+ memset(var_name, 0, RSTRING_LEN(vVarName)+1);
143
+ memcpy(var_name, RSTRING_PTR(vVarName), RSTRING_LEN(vVarName));
144
+ lua_pushstring(L, var_name);
145
+ #endif
146
+ lua_SetVarToStack(L, vVarValue);
147
+ #ifdef HAVE_LUA_GLOBALSINDEX
148
+ lua_settable(L, LUA_GLOBALSINDEX);
149
+ #else
150
+ lua_setglobal(L, var_name);
151
+ free(var_name);
152
+ #endif
135
153
  return;
136
154
  }
137
155
 
@@ -140,12 +158,14 @@ static void lua_SetNamedVarFromRuby( lua_State *L, VALUE vVarName, VALUE vVarVal
140
158
  *
141
159
  * Desc: R�cup�re la valeur au sommet de la pile Lua
142
160
  */
143
- static VALUE lua_GetVarFromStack( lua_State *L ) {
161
+ static VALUE lua_GetVarFromStack(lua_State *L) {
144
162
  int iStackSize = 0;
145
163
  VALUE vTableKey = Qnil;
146
164
  VALUE vTableValue = Qnil;
147
165
  VALUE RCod = Qnil;
148
166
 
167
+ rb_require("language/lua/function");
168
+
149
169
  switch(lua_type(L,-1)){
150
170
  case LUA_TNONE:
151
171
  break;
@@ -171,16 +191,60 @@ static VALUE lua_GetVarFromStack( lua_State *L ) {
171
191
  vTableValue = lua_GetVarFromStack(L);
172
192
  lua_pop(L,1);
173
193
  vTableKey = lua_GetVarFromStack(L);
194
+
174
195
  rb_hash_aset(RCod,vTableKey,vTableValue);
175
196
  }
176
197
  lua_settop(L,iStackSize);
177
198
  break;
178
199
  case LUA_TFUNCTION:
200
+ RCod = rb_class_new_instance(0, NULL, rb_const_get(cLua, rb_intern("Function")));
179
201
  break;
180
202
  }
181
203
  return(RCod);
182
204
  }
183
205
 
206
+ typedef struct populate {
207
+ VALUE lua;
208
+ char *parent;
209
+ } populate;
210
+ void lua_populate_fun(VALUE table, VALUE lua, char *parent);
211
+
212
+ int do_populate_lua(VALUE key, VALUE val, VALUE in) {
213
+ if(T_STRING == TYPE(key)) {
214
+ char *fn = concatenate(3, ((populate*)in)->parent, ".", StringValuePtr(key));
215
+
216
+ switch(TYPE(val)) {
217
+ case T_OBJECT:
218
+ if(0 == strcmp("Language::Lua::Function", rb_obj_classname(val))) {
219
+ rb_ivar_set(val, rb_intern("@lua"), ((populate*)in)->lua);
220
+ rb_ivar_set(val, rb_intern("@fname"), rb_str_new2(fn));
221
+ }
222
+ break;
223
+ case T_HASH:
224
+ lua_populate_fun(val, ((populate*)in)->lua, fn);
225
+ break;
226
+ default:
227
+ break;
228
+ }
229
+
230
+ free(fn);
231
+ }
232
+ return ST_CONTINUE;
233
+ }
234
+
235
+ void lua_populate_fun(VALUE table, VALUE lua, char *parent) {
236
+ populate *p = (populate*)malloc(sizeof(populate));
237
+ p->lua = lua;
238
+ p->parent = parent;
239
+
240
+ if(T_HASH != TYPE(table)) {
241
+ return;
242
+ }
243
+ rb_hash_foreach(table, do_populate_lua, (VALUE)p);
244
+
245
+ free(p);
246
+ }
247
+
184
248
  static int lua_CallRubyFunction( lua_State *L ) {
185
249
  int iNbArgs = lua_gettop(L);
186
250
  VALUE self;
@@ -209,7 +273,7 @@ static int lua_CallRubyFunction( lua_State *L ) {
209
273
 
210
274
  vFuncArgs = malloc(sizeof(VALUE) * iNbFuncArgs );
211
275
  for( iCpt = 0; iCpt < iNbFuncArgs; iCpt++ ) {
212
- vFuncArgs[iNbFuncArgs - iCpt - 1] = lua_GetVarFromStack( L );
276
+ vFuncArgs[iNbFuncArgs - iCpt - 1] = lua_GetVarFromStack(L);
213
277
  lua_remove( L, -1 );
214
278
  }
215
279
 
@@ -230,20 +294,47 @@ static int lua_CallRubyFunction( lua_State *L ) {
230
294
  static VALUE ruby_CallLuaFunction( char *xFunctionName, int iNbArgs, VALUE *vArgs, VALUE self ) {
231
295
  RbTlua *pRbTlua;
232
296
  int iCpt;
297
+ int stack_size, final_stack_size, final_pop;
298
+ char *point_in_fun;
233
299
  VALUE RCod = Qnil;
234
300
 
235
301
  Data_Get_Struct( self, RbTlua, pRbTlua );
236
302
 
303
+ stack_size = lua_gettop(pRbTlua->L);
304
+
237
305
  lua_getglobal( pRbTlua->L, xFunctionName ); /* get function */
306
+ if(!lua_isfunction(pRbTlua->L,lua_gettop(pRbTlua->L)) || NULL == strchr(xFunctionName, '.')){
307
+ lua_pop(pRbTlua->L, 1);
308
+
309
+ point_in_fun = strtok(xFunctionName, ".");
310
+ if(NULL == point_in_fun) {
311
+ rb_raise(rb_eSystemCallError, "error running lua function `%s': table does not exist", xFunctionName);
312
+ } else {
313
+ lua_getglobal(pRbTlua->L, point_in_fun);
314
+ }
315
+ point_in_fun = strtok(NULL, ".");
316
+ while(NULL != point_in_fun) {
317
+ lua_getfield(pRbTlua->L, -1, point_in_fun);
318
+ point_in_fun = strtok(NULL, ".");
319
+ }
320
+ } else {
321
+ rb_raise(rb_eSystemCallError, "error running lua function `%s': does not exist", xFunctionName);
322
+ }
238
323
 
239
324
  for( iCpt = 0; iCpt < iNbArgs; iCpt++ ) {
240
325
  lua_SetVarToStack( pRbTlua->L, vArgs[iCpt] );
241
326
  }
242
327
 
243
- if( lua_pcall( pRbTlua->L, iNbArgs, 1, 0 ) != 0 ) /* do the call */
328
+ if( lua_pcall( pRbTlua->L, iNbArgs, 1, 0 ) != 0 ) /* do the call */ {
244
329
  rb_raise( rb_eSystemCallError, "error running lua function `%s': %s", xFunctionName, lua_tostring(pRbTlua->L, -1) );
330
+ }
245
331
 
246
- RCod = lua_GetVarFromStack( pRbTlua->L );
332
+ RCod = lua_GetVarFromStack(pRbTlua->L);
333
+
334
+ final_stack_size = lua_gettop(pRbTlua->L);
335
+ final_pop = final_stack_size - stack_size;
336
+ lua_pop(pRbTlua->L, final_pop);
337
+ lua_SetVarToStack(pRbTlua->L, RCod);
247
338
 
248
339
  return( RCod );
249
340
  }
@@ -284,9 +375,8 @@ VALUE ruby_lua_new( VALUE class ) {
284
375
  rb_raise( rb_eNoMemError, "No memory left for Lua struct" );
285
376
 
286
377
  pRbTlua->L = lua_newstate(l_alloc, pRbTlua->ud);
287
- luaL_openlibs(pRbTlua->L);
288
-
289
378
  lua_register( pRbTlua->L, "ruby", lua_CallRubyFunction );
379
+ luaL_openlibs(pRbTlua->L);
290
380
 
291
381
  self = Data_Wrap_Struct( class, ruby_lua_mark, ruby_lua_free, pRbTlua );
292
382
 
@@ -386,7 +476,8 @@ VALUE ruby_lua_var( int iNbArgs, VALUE *vArgs, VALUE self ) {
386
476
  if( iNbArgs == 2 ) {
387
477
  /** Act: Recup�re la valeur */
388
478
  lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[1]) );
389
- RCod = lua_GetVarFromStack( pRbTlua->L );
479
+ RCod = lua_GetVarFromStack(pRbTlua->L);
480
+ lua_populate_fun(RCod, self, StringValuePtr(vArgs[1]));
390
481
  } else if( iNbArgs == 3 ) {
391
482
  /** Positionne la valeur */
392
483
  lua_SetNamedVarFromRuby( pRbTlua->L, vArgs[1], vArgs[2] );
@@ -399,7 +490,8 @@ VALUE ruby_lua_var( int iNbArgs, VALUE *vArgs, VALUE self ) {
399
490
  if( iNbArgs == 2 ) {
400
491
  /** Act: Recup�re la valeur */
401
492
  lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[1]) );
402
- RCod = lua_GetVarFromStack( pRbTlua->L );
493
+ RCod = lua_GetVarFromStack(pRbTlua->L);
494
+ lua_populate_fun(RCod, self, StringValuePtr(vArgs[1]));
403
495
  lua_pop( pRbTlua->L, 1 );
404
496
  } else if( iNbArgs == 3 ) {
405
497
  /** Positionne la valeur */
@@ -413,7 +505,8 @@ VALUE ruby_lua_var( int iNbArgs, VALUE *vArgs, VALUE self ) {
413
505
  if( iNbArgs == 1 ) {
414
506
  /** Act: Recup�re la valeur */
415
507
  lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[0]) );
416
- RCod = lua_GetVarFromStack( pRbTlua->L );
508
+ RCod = lua_GetVarFromStack(pRbTlua->L);
509
+ lua_populate_fun(RCod, self, StringValuePtr(vArgs[0]));
417
510
  lua_pop( pRbTlua->L, 1 );
418
511
  } else if( iNbArgs == 2 ) {
419
512
  /** Positionne la valeur */
@@ -499,9 +592,6 @@ VALUE ruby_lua_stack_dump( VALUE self ) {
499
592
 
500
593
  /** ------------------------------------------------------------------------ */
501
594
 
502
- VALUE mLanguage;
503
- VALUE cLua;
504
-
505
595
  void Init_lua( void ) {
506
596
  mLanguage = rb_define_module( "Language" );
507
597
  cLua = rb_define_class_under( mLanguage, "Lua", rb_cObject );
@@ -520,3 +610,4 @@ void Init_lua( void ) {
520
610
  rb_define_method( cLua, "stack_size", ruby_lua_stack_size, 0 );
521
611
  rb_define_method( cLua, "stack_dump", ruby_lua_stack_dump, 0 );
522
612
  }
613
+
data/ext/rlua.h CHANGED
@@ -26,6 +26,7 @@
26
26
  #include <rubyio.h>
27
27
  #else
28
28
  #include <ruby/io.h>
29
+ #include <ruby/st.h>
29
30
  #endif
30
31
 
31
32
  #include <stdlib.h>
@@ -37,8 +38,8 @@
37
38
  #include <lauxlib.h>
38
39
  #include <lualib.h>
39
40
 
40
- #define RUBY_LUA_VERSION "0.2.0"
41
- #define RUBY_LUA_VERNUM 020
41
+ #include "config.h"
42
+ #include "util.h"
42
43
 
43
44
  typedef struct RbTlua {
44
45
  lua_State *L;
@@ -0,0 +1,30 @@
1
+ #include <stdio.h>
2
+ #include <stdarg.h>
3
+ #include <string.h>
4
+ #include <stdlib.h>
5
+
6
+ char* concatenate(int n, ...) {
7
+ int i;
8
+ const char* v;
9
+ va_list vl;
10
+ va_start(vl, n);
11
+ char *result = NULL;
12
+ for(i=0; i<n; i++) {
13
+ v = va_arg(vl, const char*);
14
+
15
+ if(result) {
16
+ char *tmp = strdup(result);
17
+ free(result);
18
+ result = (char*)malloc(sizeof(char)*(strlen(tmp)+strlen(v)+1));
19
+ memset(result, 0, strlen(tmp)+strlen(v)+1);
20
+ sprintf(result, "%s%s", tmp, v);
21
+ free(tmp);
22
+ } else {
23
+ result = (char*)malloc(sizeof(char)*(strlen(v)+1));
24
+ memset(result, 0, strlen(v)+1);
25
+ memcpy(result, v, strlen(v));
26
+ }
27
+ }
28
+ return result;
29
+ }
30
+
@@ -0,0 +1,7 @@
1
+ #ifndef __UTIL_H
2
+ #define __UTIL_H
3
+
4
+ char* concatenate(int n, ...);
5
+
6
+ #endif // __UTIL_H
7
+
@@ -0,0 +1,11 @@
1
+ module Language
2
+ class Lua
3
+ class Function
4
+ attr_accessor :lua, :fname
5
+
6
+ def call(*args)
7
+ @lua.call(fname, *args)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require 'language/lua'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "ruby-lua"
5
+ s.description = "Call Lua from Ruby (and Ruby from Lua in Ruby)"
6
+ s.summary = "Call Lua from Ruby (and Ruby from Lua in Ruby)"
7
+
8
+ s.version = open(File.join(File.dirname(__FILE__), "VERSION")).read
9
+ s.platform = Gem::Platform::RUBY
10
+
11
+ s.authors = ["Gregoire Lejeune"]
12
+ s.email = "gregoire.lejeune@free.fr"
13
+ s.homepage = "http://github.com/glejeune/ruby-lua"
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- test/*`.split("\n")
18
+ s.extra_rdoc_files = ["LICENSE.txt","README.rdoc"]
19
+ s.extensions = ["ext/extconf.rb"]
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency("shoulda")
23
+ s.add_development_dependency("bundler")
24
+ s.add_development_dependency("rdoc")
25
+ s.add_development_dependency("rake")
26
+ end
27
+
@@ -1,18 +1,17 @@
1
1
  require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
2
+ require 'bundler/setup'
10
3
  require 'test/unit'
11
4
  require 'shoulda'
5
+ require 'pathname'
12
6
 
13
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
- $LOAD_PATH.unshift(File.dirname(__FILE__))
15
- require 'ruby-lua'
7
+ cur = Pathname.new(File.expand_path("..", __FILE__))
8
+ lib = cur.join('..', 'lib')
9
+ ext = cur.join('..', 'ext')
16
10
 
17
- class Test::Unit::TestCase
11
+ unless ext.join("lua.so").exist? or ext.join("lua.bundle").exist? or ext.join("lua.dll").exist?
12
+ abort " ! Unable to run tests. Please run `rake build` first"
18
13
  end
14
+
15
+ $LOAD_PATH.unshift(lib.to_s, ext.to_s, cur.to_s)
16
+ require 'lua'
17
+
@@ -0,0 +1,34 @@
1
+ require 'helper'
2
+
3
+ class TestCallLuaFromRuby < Test::Unit::TestCase
4
+ context "a lua jit" do
5
+ setup do
6
+ @lua = Language::Lua.new()
7
+ @lua.eval <<-LUA
8
+ function one()
9
+ return 1
10
+ end
11
+
12
+ function two()
13
+ return 2, 3
14
+ end
15
+
16
+ function data(x)
17
+ return x
18
+ end
19
+ LUA
20
+ end
21
+
22
+ should "return 1.0" do
23
+ assert_equal 1.0, @lua.call(true, "one")
24
+ end
25
+
26
+ should "return 2.0" do
27
+ assert_equal 2.0, @lua.call(true, "two")
28
+ end
29
+
30
+ should "return the given value" do
31
+ assert_equal "hello", @lua.call(true, "data", "hello")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestCallLuaFromRuby < Test::Unit::TestCase
4
+ context "a lua jit" do
5
+ setup do
6
+ @lua = Language::Lua.new()
7
+ end
8
+
9
+ should "return 5.0" do
10
+ assert_equal 5.0, @lua.call("string.len", "hello")
11
+ end
12
+
13
+ should "return 65.0" do
14
+ assert_equal 65.0, @lua.call("string.byte", "ABCDE")
15
+ end
16
+
17
+ should "return 12.0" do
18
+ assert_equal 12.0, @lua.call("string.find", "Hello from Lua", "Lua")
19
+ end
20
+
21
+ should "return 8.0" do
22
+ assert_equal 8.0, @lua.var('string')["len"].call("ruby-lua")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ def my_ruby_function(str, num)
4
+ "str = #{str}, num = #{num}"
5
+ end
6
+
7
+ class TestRubyFromLua < Test::Unit::TestCase
8
+ context "a lua jit" do
9
+ setup do
10
+ @lua = Language::Lua.new()
11
+ @lua.eval <<-LUA
12
+ function call_ruby()
13
+ return ruby("my_ruby_function", "string", 3)
14
+ end
15
+ LUA
16
+ end
17
+
18
+ should "return the result of my_ruby_function" do
19
+ assert_equal "str = string, num = 3.0", @lua.call(true, "call_ruby")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ class TestVars < Test::Unit::TestCase
4
+ context "a lua jit" do
5
+ setup do
6
+ @lua = Language::Lua.new()
7
+ end
8
+
9
+ should "create a string variable" do
10
+ @lua.var("str", "This is a string")
11
+ assert_equal "This is a string", @lua.var("str")
12
+ end
13
+
14
+ should "create a number variable" do
15
+ @lua.var("num", 123)
16
+ assert_equal 123.0, @lua.var("num")
17
+ end
18
+
19
+ should "create a boolean variable" do
20
+ @lua.var("yes", true)
21
+ @lua.var("no", false)
22
+
23
+ assert_equal true, @lua.var("yes")
24
+ assert_equal false, @lua.var("no")
25
+ end
26
+
27
+ should "create a nil variable" do
28
+ @lua.var("null", NIL)
29
+ assert_equal NIL, @lua.var("null")
30
+ end
31
+
32
+ should "create a table" do
33
+ @lua.var("table", ["string", 123, true, NIL])
34
+ table = @lua.var("table")
35
+ assert_equal 4, table.size()
36
+ assert_equal "string", table[1.0]
37
+ assert_equal 123.0, table[2.0]
38
+ assert_equal true, table[3.0]
39
+ assert_equal NIL, table[4.0]
40
+ end
41
+
42
+ should "create a hash" do
43
+ @lua.var("hash", {"string" => 123})
44
+ hash = @lua.var("hash")
45
+ assert_equal 123.0, hash["string"]
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,106 +1,81 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-lua
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 0
10
- version: 0.3.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Gregoire Lejeune
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-04-14 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- hash: 3
27
- segments:
28
- - 0
29
- version: "0"
11
+ date: 2013-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
30
14
  name: shoulda
31
- prerelease: false
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
32
20
  type: :development
33
- requirement: *id001
34
- - !ruby/object:Gem::Dependency
35
- version_requirements: &id002 !ruby/object:Gem::Requirement
36
- none: false
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- hash: 23
41
- segments:
42
- - 1
43
- - 0
44
- - 0
45
- version: 1.0.0
46
- name: bundler
47
21
  prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
48
34
  type: :development
49
- requirement: *id002
50
- - !ruby/object:Gem::Dependency
51
- version_requirements: &id003 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ~>
55
- - !ruby/object:Gem::Version
56
- hash: 1
57
- segments:
58
- - 1
59
- - 5
60
- - 1
61
- version: 1.5.1
62
- name: jeweler
63
35
  prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
64
48
  type: :development
65
- requirement: *id003
66
- - !ruby/object:Gem::Dependency
67
- version_requirements: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
76
- name: rcov
77
49
  prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
78
62
  type: :development
79
- requirement: *id004
80
- - !ruby/object:Gem::Dependency
81
- version_requirements: &id005 !ruby/object:Gem::Requirement
82
- none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 3
87
- segments:
88
- - 0
89
- version: "0"
90
- name: rdoc
91
63
  prerelease: false
92
- type: :development
93
- requirement: *id005
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
94
69
  description: Call Lua from Ruby (and Ruby from Lua in Ruby)
95
70
  email: gregoire.lejeune@free.fr
96
71
  executables: []
97
-
98
- extensions:
72
+ extensions:
99
73
  - ext/extconf.rb
100
- extra_rdoc_files:
74
+ extra_rdoc_files:
101
75
  - LICENSE.txt
102
76
  - README.rdoc
103
- files:
77
+ files:
78
+ - .gitignore
104
79
  - Gemfile
105
80
  - Gemfile.lock
106
81
  - LICENSE.txt
@@ -117,45 +92,43 @@ files:
117
92
  - ext/extconf.rb
118
93
  - ext/rlua.c
119
94
  - ext/rlua.h
95
+ - ext/util.c
96
+ - ext/util.h
97
+ - lib/language/lua/function.rb
98
+ - lib/ruby-lua.rb
99
+ - ruby-lua.gemspec
120
100
  - test/helper.rb
121
- - test/test_ruby-lua.rb
101
+ - test/test_lua_from_ruby.rb
102
+ - test/test_lua_function.rb
103
+ - test/test_ruby_from_lua.rb
104
+ - test/test_vars.rb
122
105
  homepage: http://github.com/glejeune/ruby-lua
123
- licenses:
106
+ licenses:
124
107
  - MIT
108
+ metadata: {}
125
109
  post_install_message:
126
110
  rdoc_options: []
127
-
128
- require_paths:
111
+ require_paths:
129
112
  - lib
130
- required_ruby_version: !ruby/object:Gem::Requirement
131
- none: false
132
- requirements:
133
- - - ">="
134
- - !ruby/object:Gem::Version
135
- hash: 3
136
- segments:
137
- - 0
138
- version: "0"
139
- required_rubygems_version: !ruby/object:Gem::Requirement
140
- none: false
141
- requirements:
142
- - - ">="
143
- - !ruby/object:Gem::Version
144
- hash: 3
145
- segments:
146
- - 0
147
- version: "0"
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
148
123
  requirements: []
149
-
150
124
  rubyforge_project:
151
- rubygems_version: 1.8.15
125
+ rubygems_version: 2.0.3
152
126
  signing_key:
153
- specification_version: 3
127
+ specification_version: 4
154
128
  summary: Call Lua from Ruby (and Ruby from Lua in Ruby)
155
- test_files:
156
- - examples/func.rb
157
- - examples/rli.rb
158
- - examples/test.rb
159
- - examples/tt.rb
129
+ test_files:
160
130
  - test/helper.rb
161
- - test/test_ruby-lua.rb
131
+ - test/test_lua_from_ruby.rb
132
+ - test/test_lua_function.rb
133
+ - test/test_ruby_from_lua.rb
134
+ - test/test_vars.rb
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestRubyLua < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end