ruby-lua 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ 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)
13
+ json (~> 1.4)
14
+ shoulda (2.11.3)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ bundler (~> 1.0.0)
21
+ jeweler (~> 1.5.1)
22
+ rcov
23
+ rdoc
24
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gregoire Lejeune
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ = ruby-lua
2
+
3
+ Call Lua from Ruby (and Ruby from Lua in Ruby)
4
+
5
+ Language::Lua - Lua interpretor for Ruby
6
+
7
+ == SYNOPSIS
8
+
9
+ require 'language/lua'
10
+ lua = Language::Lua.new()
11
+ lua.eval( "script.lua" )
12
+ # same as
13
+ lua.dofile( "script.lua" )
14
+
15
+ out = lua.my_lua_function( ... )
16
+ print out;
17
+ # same as
18
+ out = lua.call( "my_lua_function", ... )
19
+ print out;
20
+
21
+ == Ruby API
22
+
23
+ === Language::Lua.new()
24
+
25
+ Create a new Lua interpretor
26
+
27
+ === Language::Lua.load( file ) or Language::Lua.dofile( file )
28
+
29
+ Load a Lua Script
30
+
31
+ === Language::Lua.<lua_function>( ... )
32
+
33
+ Call the Lua function named <lua_function>
34
+ This is equivalent to
35
+
36
+ Language::Lua.call( false, "<lua_function>", ... )
37
+
38
+ === Language::Lua.call( [true|false], "<lua_function>", ... )
39
+
40
+ Call the Lua function named <lua_function>.
41
+ If first argument is true, the function result is not poped from the Lua Stack (default: false).
42
+
43
+ === Language::Lua.var( [true|false], "<lua_varname>", [<value>] )
44
+
45
+ Return the <lua_varname> value or set the <lua_varname> if <value> is given.
46
+
47
+
48
+ === Language::Lua.eval( "lua_code" )
49
+
50
+ Evaluate the lua_code
51
+
52
+ == Lua
53
+
54
+ Ruby/Lua adds the following new method to Lua :
55
+
56
+ === ruby( "<ruby_function>", ... )
57
+
58
+ In your Lua code, call <ruby_function>.
59
+
60
+ == Contributing to ruby-lua
61
+
62
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
63
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
64
+ * Fork the project
65
+ * Start a feature/bugfix branch
66
+ * Commit and push until you are happy with your contribution
67
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
68
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
69
+
70
+ == Copyright
71
+
72
+ Copyright (c) 2010 Gregoire Lejeune. See LICENSE.txt for
73
+ further details.
74
+
@@ -0,0 +1,54 @@
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
10
+ 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
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ 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
43
+
44
+ task :default => :test
45
+
46
+ require 'rdoc/task'
47
+ RDoc::Task.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "ruby-lua #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "../ext/lua"
4
+
5
+ def my_function( str, num )
6
+ puts "In my_function !!!"
7
+ return "str = #{str} / num = #{num}"
8
+ end
9
+
10
+ lua = Language::Lua.new()
11
+
12
+ lua.eval <<LUA
13
+ function one( )
14
+ return 1
15
+ end
16
+
17
+ function two( )
18
+ return 2, 3
19
+ end
20
+
21
+ function data( x )
22
+ return x
23
+ end
24
+
25
+ function callRuby( )
26
+ r = ruby( "my_function", "string", 3 )
27
+ print( "r = " .. r )
28
+ end
29
+ LUA
30
+
31
+ p lua.call( true, "one" );
32
+
33
+ p lua.call( true, "two" );
34
+
35
+ p lua.call( true, "data", "hello" )
36
+
37
+ lua.callRuby
Binary file
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "../ext/lua"
4
+
5
+ print "Ruby Lua Interpreter !\n"
6
+ o = Language::Lua.new()
7
+
8
+ o.load( ARGV[0] )
@@ -0,0 +1,21 @@
1
+ -- defines a factorial function
2
+ function fact (n)
3
+ if n == 0 then
4
+ return 1
5
+ else
6
+ return n * fact(n-1)
7
+ end
8
+ end
9
+
10
+ -- define a stupid function
11
+ function hello( )
12
+ return "world"
13
+ end
14
+
15
+ -- define a string variable
16
+ str = "blah blah blah"
17
+
18
+ -- define a number variable
19
+ num = 123
20
+
21
+ fact( 3 )
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'pp'
4
+ require "../ext/lua"
5
+
6
+ lua = Language::Lua.new()
7
+ lua.load( "test.lua" )
8
+
9
+ i = 0;
10
+ print "BEGIN[#{i}]: -------------------------------------------------\n"
11
+ print "Lua Stack Size = #{lua.stack_size}\n"
12
+ lua.stack_dump
13
+ print "END[#{i}]: -------------------------------------------------\n"
14
+
15
+ r = lua.call( "fact", 10 )
16
+ print "call( \"fact\", 10 ) -> #{r}\n"
17
+
18
+ r = lua.call( false, "fact", 8 )
19
+ print "call( false, \"fact\", 8 ) -> #{r}\n"
20
+
21
+ r = lua.call( true, "fact", 6 )
22
+ print "call( true, \"fact\", 6 ) -> #{r}\n"
23
+
24
+ r = lua.hello( )
25
+ print "hello( ) -> #{r}\n"
26
+
27
+ r = lua.var( "str" )
28
+ print "var( \"str\" ) -> #{r}\n"
29
+
30
+ r = lua.var( false, "num" )
31
+ print "var( false, \"num\" ) -> #{r}\n"
32
+
33
+ lua.var( "rdef", "Definied" )
34
+ r = lua.var( true, "rdef" )
35
+ print "var( true, \"rdef\" ) -> #{r}\n"
36
+
37
+ lua.eval( 'print "Hello from Lua !"' )
38
+
39
+ lua.eval <<LUA
40
+ function norm (x, y)
41
+ local n2 = x^2 + y^2
42
+ return math.sqrt(n2)
43
+ end
44
+ LUA
45
+
46
+ r = lua.norm( 2, 3 )
47
+ print "norm( 2, 3 ) -> #{r}\n"
48
+
49
+
50
+ print "BEGIN: -------------------------------------------------\n"
51
+ print "Lua Stack Size = #{lua.stack_size}\n"
52
+ lua.stack_dump
53
+ print "END: -------------------------------------------------\n"
54
+
55
+ a = Array::new( )
56
+ a[0] = 30;
57
+ a[1] = "greg"
58
+ lua.var( "tbl", a )
59
+ r = lua.var( "tbl" )
60
+ print "BEGIN: -------------------------------------------------\n"
61
+ print "Lua Stack Size = #{lua.stack_size}\n"
62
+ lua.stack_dump
63
+ print "END: -------------------------------------------------\n"
64
+
65
+ print "var( \"tbl\" ) -> "; pp r
66
+ print "BEGIN: -------------------------------------------------\n"
67
+ print "Lua Stack Size = #{lua.stack_size}\n"
68
+ lua.stack_dump
69
+ print "END: -------------------------------------------------\n"
70
+
71
+ r.each { |k, v|
72
+ puts "#{k} => #{v}"
73
+ }
74
+
75
+ h = Hash::new( )
76
+ h['greg'] = 30
77
+ h['arthur'] = 6
78
+ lua.var( "fml", h )
79
+ r = lua.var( "fml" )
80
+ r.each { |k, v|
81
+ puts "#{k} => #{v}"
82
+ }
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "../ext/lua"
4
+
5
+ def rb_function( str, num )
6
+ puts "I'm a Ruby function. I was called by Lua!"
7
+ puts "Lua gave me the String : #{str}"
8
+ puts "and the Mumber : #{num}"
9
+ puts "So I return the string : 'Thank you Lua!'"
10
+ return 'Thank you Lua!'
11
+ end
12
+
13
+ puts "Hello... You are in Ruby..."
14
+
15
+ Language::Lua.new() { |lua|
16
+ lua.eval '
17
+ function lua_function( )
18
+ print( "Yes! I\'m Lua.")
19
+
20
+ print( "I give you a String and a Number")
21
+ r = ruby( "rb_function", "string", 3 )
22
+
23
+ tagada="glop"
24
+
25
+ print( "Ruby said : " .. r )
26
+ print( "- You welcome!" )
27
+ end
28
+ '
29
+
30
+ puts "Knok! Knok! Lua ?"
31
+ lua.lua_function()
32
+ puts lua.var('tagada')
33
+ }
34
+
35
+ puts "- Bye!"
@@ -0,0 +1,12 @@
1
+ -- defines a factorial function
2
+ function fact (n)
3
+ if n == 0 then
4
+ return 1
5
+ else
6
+ return n * fact(n-1)
7
+ end
8
+ end
9
+
10
+ print("enter a number:")
11
+ a = io.read("*number") -- read a number
12
+ print(fact(a))
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/ruby -w
2
+ # See the LICENSE file for copyright and distribution information
3
+
4
+ require "mkmf"
5
+
6
+ $LIBPATH.push(Config::CONFIG['libdir'])
7
+
8
+ def crash(str)
9
+ printf(" extconf failure: %s\n", str)
10
+ exit 1
11
+ end
12
+
13
+ lib = ""
14
+
15
+ #unless have_library( 'm', 'atan' )
16
+ # crash( 'need libm' )
17
+ #end
18
+ if /^1.8/.match RUBY_VERSION
19
+ $CFLAGS << " -DRUBY_1_8"
20
+ end
21
+
22
+ if dir_config( "lua50" ) != [nil, nil]
23
+ inc, lib = dir_config( 'lua50' )
24
+ $LDFLAGS << " -L#{lib} -llualib50 -llua50"
25
+ $CFLAGS << " -I#{inc}"
26
+ elsif dir_config( "lua" ) != [nil, nil]
27
+ inc, lib = dir_config( 'lua' )
28
+ $LDFLAGS << " -L#{lib} -llualib -llua"
29
+ $CFLAGS << " -I#{inc}"
30
+ elsif ex = find_executable( "lua-config" )
31
+ $LDFLAGS << ' ' + `#{ex} --libs`.chomp
32
+ $CFLAGS << ' ' + `#{ex} --include`.chomp
33
+ elsif ex = find_executable( "pkg-config" )
34
+ $LDFLAGS << ' ' + `#{ex} --libs lua`.chomp
35
+ $CFLAGS << ' ' + `#{ex} --cflags lua`.chomp
36
+ else
37
+ crash(<<EOL)
38
+ need liblua50.
39
+
40
+ Install the library or try one of the following options to extconf.rb:
41
+
42
+ --with-lua-lib=/path/to/liblua/lib
43
+ --with-lua-include=/path/to/liblua/include
44
+ or
45
+ --with-lua50-lib=/path/to/liblua/lib
46
+ --with-lua50-include=/path/to/liblua/include
47
+ EOL
48
+ end
49
+
50
+ if have_header( "lua.h" ) == false or have_header( "lauxlib.h" ) == false or have_header( "lualib.h" ) == false
51
+ crash(<<EOL)
52
+ need liblua50 (headers not found)
53
+
54
+ Install the library or try one of the following options to extconf.rb:
55
+
56
+ --with-lua-lib=/path/to/liblua/lib
57
+ --with-lua-include=/path/to/liblua/include
58
+ or
59
+ --with-lua50-lib=/path/to/liblua/lib
60
+ --with-lua50-include=/path/to/liblua/include
61
+ EOL
62
+ end
63
+
64
+ puts "compile with : "
65
+ puts " CFLAGS = #{$CFLAGS}"
66
+ puts " LDFLAGS = #{$LDFLAGS}"
67
+ puts
68
+
69
+ $CFLAGS = '-g -Wall ' + $CFLAGS
70
+
71
+ create_header()
72
+ create_makefile("language/lua")
@@ -0,0 +1,522 @@
1
+ /**
2
+ * Copyright (C) 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
3
+ *
4
+ * This program is free software; you can redistribute it and/or modify
5
+ * it under the terms of the GNU General Public License as published by
6
+ * the Free Software Foundation; either version 2 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ */
18
+
19
+ #include "rlua.h"
20
+
21
+ #define _W_STACK(L) {\
22
+ int i;\
23
+ int iStackSize = lua_gettop( L );\
24
+ rb_warn( "Lua stack size = %d", iStackSize );\
25
+ \
26
+ for (i = 1; i <= iStackSize; i++) {\
27
+ int t = lua_type(L, i);\
28
+ switch (t) {\
29
+ case LUA_TSTRING: /* strings */\
30
+ printf("`%s'", lua_tostring(L, i));\
31
+ break;\
32
+ \
33
+ case LUA_TBOOLEAN: /* booleans */\
34
+ printf(lua_toboolean(L, i) ? "true" : "false");\
35
+ break;\
36
+ \
37
+ case LUA_TNUMBER: /* numbers */\
38
+ printf("%g", lua_tonumber(L, i));\
39
+ break;\
40
+ \
41
+ default: /* other values */\
42
+ printf("%s", lua_typename(L, t));\
43
+ break;\
44
+ }\
45
+ printf(" "); /* put a separator */\
46
+ }\
47
+ printf("\n"); /* end the listing */ \
48
+ \
49
+ }
50
+
51
+ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
52
+ (void)ud; (void)osize; /* not used */
53
+ if (nsize == 0) {
54
+ free(ptr);
55
+ return NULL;
56
+ } else {
57
+ return realloc(ptr, nsize);
58
+ }
59
+ }
60
+
61
+ static const char *_xSelf = "_xSelf";
62
+
63
+ /**
64
+ * lua_SetVarToStack( lua_State *L, VALUE vVarValue )
65
+ *
66
+ * Desc: Positionne une valeur Ruby dans la pile Lua
67
+ */
68
+ static void lua_SetVarToStack( lua_State *L, VALUE vVarValue ) {
69
+ int iStackSize;
70
+ long iRbArrayLength;
71
+ int iCpt;
72
+ VALUE keys;
73
+
74
+ switch( TYPE(vVarValue) ) {
75
+ case T_NIL:
76
+ lua_pushnil( L );
77
+ break;
78
+ case T_TRUE:
79
+ lua_pushboolean( L, 1 );
80
+ break;
81
+ case T_FALSE:
82
+ lua_pushboolean( L, 0 );
83
+ break;
84
+ case T_STRING:
85
+ lua_pushlstring( L, RSTRING_PTR(vVarValue), RSTRING_LEN(vVarValue) );
86
+ break;
87
+ case T_FIXNUM:
88
+ lua_pushnumber( L, FIX2INT(vVarValue) );
89
+ break;
90
+ case T_BIGNUM:
91
+ lua_pushnumber( L, NUM2DBL(vVarValue) );
92
+ break;
93
+ case T_FLOAT:
94
+ lua_pushnumber( L, (lua_Number)RFLOAT_VALUE(vVarValue) );
95
+ break;
96
+ case T_ARRAY:
97
+ lua_newtable( L );
98
+ iStackSize = lua_gettop( L );
99
+ iRbArrayLength = RARRAY_LEN( vVarValue );
100
+ lua_pushstring( L, "n" );
101
+ lua_pushnumber( L, iRbArrayLength );
102
+ lua_settable( L, -3 );
103
+ for( iCpt = 0; iCpt < iRbArrayLength; iCpt++ ) {
104
+ lua_SetVarToStack( L, RARRAY_PTR(vVarValue)[iCpt] );
105
+ lua_rawseti( L, iStackSize, iCpt + 1 );
106
+ }
107
+ break;
108
+ case T_HASH:
109
+ lua_newtable( L );
110
+ keys = rb_funcall( vVarValue, rb_intern("keys"), 0 );
111
+ for( iCpt = 0; iCpt <= RARRAY_LEN(keys)-1; iCpt++ ){
112
+ VALUE vHashKey;
113
+ vHashKey = *(RARRAY_PTR(keys)+iCpt);
114
+ lua_pushlstring( L, RSTRING_PTR(vHashKey), RSTRING_LEN(vHashKey) );
115
+ lua_SetVarToStack( L, rb_hash_aref(vVarValue, vHashKey) );
116
+ lua_settable( L, -3 );
117
+ }
118
+ break;
119
+
120
+ default:
121
+ lua_pushlightuserdata( L,(void*)vVarValue ); /* saves ruby object */
122
+ break;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * lua_SetNamedVarFromRuby( lua_State *L, VALUE vVarName, VALUE vVarValue );
128
+ *
129
+ * Desc: Positionne une valeur nomm�e Ruby dans l'index global Lua
130
+ */
131
+ static void lua_SetNamedVarFromRuby( lua_State *L, VALUE vVarName, VALUE vVarValue ) {
132
+ lua_pushlstring( L, RSTRING_PTR(vVarName), RSTRING_LEN(vVarName) );
133
+ lua_SetVarToStack( L, vVarValue );
134
+ lua_settable( L, LUA_GLOBALSINDEX );
135
+ return;
136
+ }
137
+
138
+ /**
139
+ * VALUE v = lua_GetVarFromStack( lua_State *L );
140
+ *
141
+ * Desc: R�cup�re la valeur au sommet de la pile Lua
142
+ */
143
+ static VALUE lua_GetVarFromStack( lua_State *L ) {
144
+ int iStackSize = 0;
145
+ VALUE vTableKey = Qnil;
146
+ VALUE vTableValue = Qnil;
147
+ VALUE RCod = Qnil;
148
+
149
+ switch(lua_type(L,-1)){
150
+ case LUA_TNONE:
151
+ break;
152
+ case LUA_TBOOLEAN:
153
+ RCod = lua_toboolean(L,-1) ? Qtrue : Qfalse;
154
+ break;
155
+ case LUA_TUSERDATA:
156
+ RCod = (VALUE) lua_touserdata(L,-1);
157
+ break;
158
+ case LUA_TNIL:
159
+ break;
160
+ case LUA_TNUMBER:
161
+ RCod = rb_float_new(lua_tonumber(L,-1));
162
+ break;
163
+ case LUA_TSTRING:
164
+ RCod = rb_str_new2(lua_tostring(L,-1));
165
+ break;
166
+ case LUA_TTABLE:
167
+ RCod = rb_hash_new();
168
+ iStackSize = lua_gettop(L);
169
+ lua_pushnil(L);
170
+ while(lua_next(L,iStackSize) != 0){
171
+ vTableValue = lua_GetVarFromStack(L);
172
+ lua_pop(L,1);
173
+ vTableKey = lua_GetVarFromStack(L);
174
+ rb_hash_aset(RCod,vTableKey,vTableValue);
175
+ }
176
+ lua_settop(L,iStackSize);
177
+ break;
178
+ case LUA_TFUNCTION:
179
+ break;
180
+ }
181
+ return(RCod);
182
+ }
183
+
184
+ static int lua_CallRubyFunction( lua_State *L ) {
185
+ int iNbArgs = lua_gettop(L);
186
+ VALUE self;
187
+ char *xRubyFuncName;
188
+ ID idRubyFunc;
189
+ int iNbFuncArgs = 0;
190
+ VALUE *vFuncArgs;
191
+ VALUE vFuncRCod = Qnil;
192
+ int iCpt;
193
+
194
+ if( iNbArgs < 1 || !lua_isstring(L, 1) ) {
195
+ lua_pushstring( L, "incorrect argument to function `ruby'" );
196
+ lua_error(L);
197
+ }
198
+
199
+ xRubyFuncName = (char *)lua_tostring(L, 1);
200
+ idRubyFunc = rb_intern( xRubyFuncName );
201
+ iNbFuncArgs = iNbArgs - 1;
202
+ lua_remove( L, 1 );
203
+
204
+ /** Act: Recuperation de self depuis la registry Lua */
205
+ lua_pushlightuserdata(L, (void *)&_xSelf);
206
+ lua_gettable(L, LUA_REGISTRYINDEX);
207
+ self = (VALUE)lua_touserdata(L,-1);
208
+ lua_pop( L, 1 );
209
+
210
+ vFuncArgs = malloc(sizeof(VALUE) * iNbFuncArgs );
211
+ for( iCpt = 0; iCpt < iNbFuncArgs; iCpt++ ) {
212
+ vFuncArgs[iNbFuncArgs - iCpt - 1] = lua_GetVarFromStack( L );
213
+ lua_remove( L, -1 );
214
+ }
215
+
216
+ vFuncRCod = rb_funcall2( self, idRubyFunc, iNbFuncArgs, vFuncArgs );
217
+ lua_SetVarToStack( L, vFuncRCod );
218
+
219
+ return 1;
220
+ }
221
+
222
+ /** ------------------------------------------------------------------------ */
223
+
224
+ /**
225
+ * VALUE v = ruby_CallLuaFunction( char *xFunctionName, int iNbArgs, VALUE *vArgs, VALUE self )
226
+ *
227
+ * Desc: Appel la fonction Lua xFunctionName avec les param�tres contenu dans vArgs
228
+ * et retourne le r�sultat
229
+ */
230
+ static VALUE ruby_CallLuaFunction( char *xFunctionName, int iNbArgs, VALUE *vArgs, VALUE self ) {
231
+ RbTlua *pRbTlua;
232
+ int iCpt;
233
+ VALUE RCod = Qnil;
234
+
235
+ Data_Get_Struct( self, RbTlua, pRbTlua );
236
+
237
+ lua_getglobal( pRbTlua->L, xFunctionName ); /* get function */
238
+
239
+ for( iCpt = 0; iCpt < iNbArgs; iCpt++ ) {
240
+ lua_SetVarToStack( pRbTlua->L, vArgs[iCpt] );
241
+ }
242
+
243
+ if( lua_pcall( pRbTlua->L, iNbArgs, 1, 0 ) != 0 ) /* do the call */
244
+ rb_raise( rb_eSystemCallError, "error running lua function `%s': %s", xFunctionName, lua_tostring(pRbTlua->L, -1) );
245
+
246
+ RCod = lua_GetVarFromStack( pRbTlua->L );
247
+
248
+ return( RCod );
249
+ }
250
+
251
+ /** ------------------------------------------------------------------------ */
252
+
253
+ /**
254
+ * Desc: Destructeur de class
255
+ */
256
+ void ruby_lua_free( RbTlua *pRbTlua ) {
257
+ if (pRbTlua != NULL) {
258
+ lua_close(pRbTlua->L);
259
+ free(pRbTlua);
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Desc: mark
265
+ */
266
+ void ruby_lua_mark( RbTlua *pRbTlua ) {
267
+ if( pRbTlua == NULL ) return;
268
+ // if( !NIL_P(pRbTlua->xXmlData) ) rb_gc_mark( pRbTlua->xXmlData );
269
+
270
+ return;
271
+ }
272
+
273
+ /**
274
+ * o = Language::Lua.new()
275
+ *
276
+ * Desc: Constructeur de class
277
+ */
278
+ VALUE ruby_lua_new( VALUE class ) {
279
+ VALUE self;
280
+ RbTlua *pRbTlua;
281
+
282
+ pRbTlua = (RbTlua *)malloc(sizeof(RbTlua));
283
+ if( pRbTlua == NULL )
284
+ rb_raise( rb_eNoMemError, "No memory left for Lua struct" );
285
+
286
+ pRbTlua->L = lua_newstate(l_alloc, pRbTlua->ud);
287
+ luaL_openlibs(pRbTlua->L);
288
+
289
+ lua_register( pRbTlua->L, "ruby", lua_CallRubyFunction );
290
+
291
+ self = Data_Wrap_Struct( class, ruby_lua_mark, ruby_lua_free, pRbTlua );
292
+
293
+ /** Act: On place self dans la Registry Lua */
294
+ lua_pushlightuserdata( pRbTlua->L, (void *)&_xSelf );
295
+ lua_pushlightuserdata( pRbTlua->L, (void *)self );
296
+ lua_settable( pRbTlua->L, LUA_REGISTRYINDEX );
297
+
298
+ if(rb_block_given_p()) {
299
+ rb_yield(self);
300
+ }
301
+
302
+ return( self );
303
+ }
304
+
305
+ /**
306
+ * lua.load( "script.lua" )
307
+ * lua.dofile( "script.lua" )
308
+ *
309
+ * Desc: Charge un script Lua
310
+ */
311
+ VALUE ruby_lua_load( VALUE self, VALUE xOutFilename ) {
312
+ RbTlua *pRbTlua;
313
+ Data_Get_Struct( self, RbTlua, pRbTlua );
314
+
315
+ if( luaL_loadfile( pRbTlua->L, StringValuePtr( xOutFilename ) ) || lua_pcall( pRbTlua->L, 0, 0, 0 ) ) {
316
+ rb_raise( rb_eSystemCallError, "cannot run file %s !", lua_tostring( pRbTlua->L, -1 ) );
317
+ }
318
+
319
+ return( Qnil );
320
+ }
321
+
322
+ /**
323
+ * v = lua.<lua_method>( ... )
324
+ *
325
+ * Desc: Appel la fonction Lua <lua_method> et renvoi le r�sultat
326
+ * en le supprimant de la stack
327
+ */
328
+ VALUE ruby_lua_method_missing(int iNbArgs, VALUE *vArgs, VALUE self) {
329
+ VALUE RCod = Qnil;
330
+ RbTlua *pRbTlua;
331
+ Data_Get_Struct( self, RbTlua, pRbTlua );
332
+
333
+ RCod = ruby_CallLuaFunction( (char*)rb_id2name(SYM2ID(vArgs[0])), iNbArgs - 1, vArgs + 1, self );
334
+ lua_pop( pRbTlua->L, 1 );
335
+
336
+ return( RCod );
337
+ }
338
+
339
+ /**
340
+ * v = lua.call( [true|false], "<lua_method>", ... )
341
+ *
342
+ * Desc: Appel la fonction Lua <lua_method> et renvoi le r�sultat
343
+ * en le conservant dant la stack si le premier argument est true
344
+ */
345
+ VALUE ruby_lua_call(int iNbArgs, VALUE *vArgs, VALUE self) {
346
+ VALUE RCod = Qnil;
347
+ RbTlua *pRbTlua;
348
+ Data_Get_Struct( self, RbTlua, pRbTlua );
349
+
350
+ switch( TYPE( vArgs[0] ) ) {
351
+ case T_TRUE:
352
+ RCod = ruby_CallLuaFunction( StringValuePtr(vArgs[1]), iNbArgs - 2, vArgs + 2, self );
353
+ break;
354
+
355
+ case T_FALSE:
356
+ RCod = ruby_CallLuaFunction( StringValuePtr(vArgs[1]), iNbArgs - 2, vArgs + 2, self );
357
+ lua_pop( pRbTlua->L, 1 );
358
+ break;
359
+
360
+ case T_STRING:
361
+ RCod = ruby_CallLuaFunction( StringValuePtr(vArgs[0]), iNbArgs - 1, vArgs + 1, self );
362
+ lua_pop( pRbTlua->L, 1 );
363
+ break;
364
+
365
+ default:
366
+ rb_raise( rb_eArgError, "Language::Lua.call argument error !" );
367
+ }
368
+
369
+ return( RCod );
370
+ }
371
+
372
+ /**
373
+ * v = lua.var( [true|false], "<lua_varname>", [<value>] )
374
+ *
375
+ * Desc: Retourne la valeur de la valiable Lua <lua_varname>
376
+ * en le conservant dans la stack si le premier argument est true .
377
+ * Ou positionne la valeur de
378
+ */
379
+ VALUE ruby_lua_var( int iNbArgs, VALUE *vArgs, VALUE self ) {
380
+ VALUE RCod = Qnil;
381
+ RbTlua *pRbTlua;
382
+ Data_Get_Struct( self, RbTlua, pRbTlua );
383
+
384
+ switch( TYPE( vArgs[0] ) ) {
385
+ case T_TRUE:
386
+ if( iNbArgs == 2 ) {
387
+ /** Act: Recup�re la valeur */
388
+ lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[1]) );
389
+ RCod = lua_GetVarFromStack( pRbTlua->L );
390
+ } else if( iNbArgs == 3 ) {
391
+ /** Positionne la valeur */
392
+ lua_SetNamedVarFromRuby( pRbTlua->L, vArgs[1], vArgs[2] );
393
+ } else {
394
+ rb_raise( rb_eArgError, "Language::Lua.var argument error !" );
395
+ }
396
+ break;
397
+
398
+ case T_FALSE:
399
+ if( iNbArgs == 2 ) {
400
+ /** Act: Recup�re la valeur */
401
+ lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[1]) );
402
+ RCod = lua_GetVarFromStack( pRbTlua->L );
403
+ lua_pop( pRbTlua->L, 1 );
404
+ } else if( iNbArgs == 3 ) {
405
+ /** Positionne la valeur */
406
+ lua_SetNamedVarFromRuby( pRbTlua->L, vArgs[1], vArgs[2] );
407
+ } else {
408
+ rb_raise( rb_eArgError, "Language::Lua.var argument error !" );
409
+ }
410
+ break;
411
+
412
+ case T_STRING:
413
+ if( iNbArgs == 1 ) {
414
+ /** Act: Recup�re la valeur */
415
+ lua_getglobal( pRbTlua->L, StringValuePtr(vArgs[0]) );
416
+ RCod = lua_GetVarFromStack( pRbTlua->L );
417
+ lua_pop( pRbTlua->L, 1 );
418
+ } else if( iNbArgs == 2 ) {
419
+ /** Positionne la valeur */
420
+ lua_SetNamedVarFromRuby( pRbTlua->L, vArgs[0], vArgs[1] );
421
+ } else {
422
+ rb_raise( rb_eArgError, "Language::Lua.var argument error !" );
423
+ }
424
+ break;
425
+
426
+ default:
427
+ rb_raise( rb_eArgError, "Language::Lua.var argument error !" );
428
+ }
429
+
430
+ return( RCod );
431
+ }
432
+
433
+ /**
434
+ * lua.eval( "<lua_code>" )
435
+ *
436
+ * Desc: Evalue le code Lua <lua_code>
437
+ */
438
+ VALUE ruby_lua_eval( VALUE self, VALUE xLuaCode ) {
439
+ char *pszLuaCode;
440
+ RbTlua *pRbTlua;
441
+ Data_Get_Struct( self, RbTlua, pRbTlua );
442
+
443
+ pszLuaCode = StringValuePtr( xLuaCode );
444
+ if( luaL_loadbuffer( pRbTlua->L, pszLuaCode, strlen(pszLuaCode), "line" ) || lua_pcall(pRbTlua->L, 0, 0, 0) ) {
445
+ rb_raise( rb_eSystemCallError, "Lua error: %s", lua_tostring( pRbTlua->L, -1 ) );
446
+ }
447
+
448
+ return( Qnil );
449
+ }
450
+
451
+ /**
452
+ * v = lua.stackSize( )
453
+ *
454
+ * Desc: Retourne la taille de la stack Lua
455
+ */
456
+ VALUE ruby_lua_stack_size( VALUE self ) {
457
+ RbTlua *pRbTlua;
458
+ Data_Get_Struct( self, RbTlua, pRbTlua );
459
+
460
+ return( INT2NUM( lua_gettop( pRbTlua->L ) ) );
461
+ }
462
+
463
+ /**
464
+ * lua.stackDump( )
465
+ *
466
+ * Desc: Dump le contenu de la stack sur la sortie standard
467
+ */
468
+ VALUE ruby_lua_stack_dump( VALUE self ) {
469
+ RbTlua *pRbTlua;
470
+ int i;
471
+ int iStackSize;
472
+
473
+ Data_Get_Struct( self, RbTlua, pRbTlua );
474
+
475
+ iStackSize = lua_gettop(pRbTlua->L);
476
+ for (i = 1; i <= iStackSize; i++) { /* repeat for each level */
477
+ int t = lua_type(pRbTlua->L, i);
478
+ switch (t) {
479
+ case LUA_TSTRING: /* strings */
480
+ printf("%04d: `%s'\n", i, lua_tostring(pRbTlua->L, i));
481
+ break;
482
+
483
+ case LUA_TBOOLEAN: /* booleans */
484
+ printf("%04d: %s\n", i, (lua_toboolean(pRbTlua->L, i) ? "true" : "false"));
485
+ break;
486
+
487
+ case LUA_TNUMBER: /* numbers */
488
+ printf("%04d: %g\n", i, lua_tonumber(pRbTlua->L, i));
489
+ break;
490
+
491
+ default: /* other values */
492
+ printf("%04d: #%s\n", i, lua_typename(pRbTlua->L, t));
493
+ break;
494
+ }
495
+ }
496
+
497
+ return( Qnil );
498
+ }
499
+
500
+ /** ------------------------------------------------------------------------ */
501
+
502
+ VALUE mLanguage;
503
+ VALUE cLua;
504
+
505
+ void Init_lua( void ) {
506
+ mLanguage = rb_define_module( "Language" );
507
+ cLua = rb_define_class_under( mLanguage, "Lua", rb_cObject );
508
+
509
+ rb_define_const( cLua, "RUBY_LUA_VERSION", rb_str_new2( RUBY_LUA_VERSION ) );
510
+
511
+ rb_define_singleton_method( cLua, "new", ruby_lua_new, 0 );
512
+
513
+ rb_define_method( cLua, "load", ruby_lua_load, 1 );
514
+ rb_define_method( cLua, "dofile", ruby_lua_load, 1 );
515
+ rb_define_method( cLua, "method_missing", ruby_lua_method_missing, -1 );
516
+ rb_define_method( cLua, "call", ruby_lua_call, -1 );
517
+ rb_define_method( cLua, "var", ruby_lua_var, -1 );
518
+ rb_define_method( cLua, "eval", ruby_lua_eval, 1 );
519
+
520
+ rb_define_method( cLua, "stack_size", ruby_lua_stack_size, 0 );
521
+ rb_define_method( cLua, "stack_dump", ruby_lua_stack_dump, 0 );
522
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright (C) 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
3
+ *
4
+ * This program is free software; you can redistribute it and/or modify
5
+ * it under the terms of the GNU General Public License as published by
6
+ * the Free Software Foundation; either version 2 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ */
18
+
19
+ /* Please see the LICENSE file for copyright and distribution information */
20
+
21
+ #ifndef __RUBY_LUA_H__
22
+ #define __RUBY_LUA_H__
23
+
24
+ #include <ruby.h>
25
+ #ifdef RUBY_1_8
26
+ #include <rubyio.h>
27
+ #else
28
+ #include <ruby/io.h>
29
+ #endif
30
+
31
+ #include <stdlib.h>
32
+ #include <stdio.h>
33
+ #include <string.h>
34
+ #include <assert.h>
35
+
36
+ #include <lua.h>
37
+ #include <lauxlib.h>
38
+ #include <lualib.h>
39
+
40
+ #define RUBY_LUA_VERSION "0.2.0"
41
+ #define RUBY_LUA_VERNUM 020
42
+
43
+ typedef struct RbTlua {
44
+ lua_State *L;
45
+ void *ud;
46
+ } RbTlua;
47
+
48
+ #endif
@@ -0,0 +1,18 @@
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
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'ruby-lua'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
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
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
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
11
+ platform: ruby
12
+ authors:
13
+ - Gregoire Lejeune
14
+ autorequire:
15
+ bindir: bin
16
+ 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"
30
+ name: shoulda
31
+ prerelease: false
32
+ 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
+ prerelease: false
48
+ 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
+ prerelease: false
64
+ 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
+ prerelease: false
78
+ 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
+ prerelease: false
92
+ type: :development
93
+ requirement: *id005
94
+ description: Call Lua from Ruby (and Ruby from Lua in Ruby)
95
+ email: gregoire.lejeune@free.fr
96
+ executables: []
97
+
98
+ extensions:
99
+ - ext/extconf.rb
100
+ extra_rdoc_files:
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ files:
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.rdoc
108
+ - Rakefile
109
+ - VERSION
110
+ - examples/func.rb
111
+ - examples/luac.out
112
+ - examples/rli.rb
113
+ - examples/test.lua
114
+ - examples/test.rb
115
+ - examples/tt.rb
116
+ - examples/verif.lua
117
+ - ext/extconf.rb
118
+ - ext/rlua.c
119
+ - ext/rlua.h
120
+ - test/helper.rb
121
+ - test/test_ruby-lua.rb
122
+ homepage: http://github.com/glejeune/ruby-lua
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+
128
+ require_paths:
129
+ - 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"
148
+ requirements: []
149
+
150
+ rubyforge_project:
151
+ rubygems_version: 1.8.15
152
+ signing_key:
153
+ specification_version: 3
154
+ 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
160
+ - test/helper.rb
161
+ - test/test_ruby-lua.rb