rufus-lua 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,7 +2,26 @@
2
2
  = rufus-lua CHANGELOG.txt
3
3
 
4
4
 
5
- == rufus-lua - 1.0.0 not yet released
5
+ == rufus-lua - 1.1.0 released 2009/09/30
6
+
7
+ - todo : Ruby symbols are passed to Lua as strings (Scott)
8
+ - todo : implemented State#function :to_ruby option
9
+ - todo : implemented Table#to_ruby
10
+ - todo : added 'pure' option to Table#to_a
11
+ - issue : added State@callbacks array to preserve callback from GC (Scott)
12
+ - issue : passing tables to ruby functions wrecked arg list. Fixed.
13
+ - todo : added way to load only a given set of libs when State.new (Scott)
14
+ - todo : honouring #to_lua when calling Lua functions from Ruby (Scott)
15
+ - todo : search for dylib in /usr/lib as well (Scott)
16
+ - issue #5 : Ruby function more tolerant with block arity.
17
+ - issue #4 : Ruby function inverses order of its parameters. Fixed. (Scott)
18
+ - todo : state['a'] = [ 1, 2, 3 ] now possible
19
+ - issue #2 : tables returned from Ruby functions were indexed at 0. (Scott)
20
+ - issue #3 : passing false from Ruby to Lua twists it to true. (Scott)
21
+ - issue #1 : Ruby function return array not OK. Fixed. (Thanks Scott Persinger)
22
+
23
+
24
+ == rufus-lua - 1.0.0 released 2009/03/27
6
25
 
7
26
  - automatically creates lib table when defined Ruby function (callback)
8
27
  - implemented #[]=, #objlen and #size for table
data/CREDITS.txt CHANGED
@@ -5,7 +5,8 @@
5
5
  == authors
6
6
 
7
7
  John Mettraux http://jmettraux.wordpress.com
8
- Alain Hoang http://blogs.law.harvard.edu/hoanga/
8
+ Alain Hoang http://blogs.law.harvard.edu/hoanga/
9
+ Scott Persinger http://github.com/scottpersinger
9
10
 
10
11
 
11
12
  == inspiration
@@ -15,7 +16,7 @@ Alain Hoang http://blogs.law.harvard.edu/hoanga/
15
16
 
16
17
  == finally
17
18
 
18
- many thanks to the authors of ruby-ffi
19
+ many thanks to the authors of ruby-ffi, especially Wayne Meissner,
19
20
 
20
- http://kenai.com/projects/ruby-ffi
21
+ http://wiki.github.com/ffi/ffi
21
22
 
@@ -3,10 +3,6 @@
3
3
 
4
4
  Lua embedded in Ruby, via Ruby FFI
5
5
 
6
- Tested with
7
- ruby 1.8.6, ruby 1.9.1p0, jruby 1.2.0
8
- jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)
9
-
10
6
 
11
7
  == Lua
12
8
 
@@ -24,8 +20,8 @@ http://www.lua.org/
24
20
  == other Ruby and Lua bridges / connectors
25
21
 
26
22
 
27
- http://rubyluabridge.rubyforge.org/
28
- http://raa.ruby-lang.org/project/ruby-lua
23
+ http://rubyluabridge.rubyforge.org/
24
+ http://raa.ruby-lang.org/project/ruby-lua
29
25
 
30
26
 
31
27
  == using rufus-lua
@@ -133,27 +129,27 @@ then
133
129
  modify the file src/Makefile as per http://lua-users.org/lists/lua-l/2006-09/msg00894.html
134
130
 
135
131
  make
136
- make masocx # or make linux ...
137
- make -C src src liblua.dylib
138
-
132
+ make macosx # or make linux ...
133
+ make -C src liblua.dylib
139
134
  sudo cp src/liblua.dylib /usr/local/lib/
140
135
 
136
+ sudo make macosx install
141
137
 
142
- == build dependencies
143
138
 
144
- You need to add the github gems to your gem sources
145
- gem sources -a http://gems.github.com
139
+ == build dependencies
146
140
 
147
141
  The following gems are needed to run the specs
148
- mislav-hanna
149
- install bacon
142
+
143
+ sudo gem install bacon
150
144
 
151
145
 
152
146
  == tested with
153
147
 
154
- ruby 1.8.6, ruby 1.9.1p0, jruby 1.2.0
148
+ ruby 1.8.7p72, ruby 1.9.1p0, jruby 1.2.0
155
149
  jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)
156
150
 
151
+ ruby-ffi 0.4.0 and 0.5.0
152
+
157
153
 
158
154
  == dependencies
159
155
 
@@ -164,12 +160,12 @@ the ruby gem 'ffi'
164
160
 
165
161
  On the rufus-ruby list :
166
162
 
167
- http://groups.google.com/group/rufus-ruby
163
+ http://groups.google.com/group/rufus-ruby
168
164
 
169
165
 
170
166
  == issue tracker
171
167
 
172
- http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
168
+ http://github.com/jmettraux/rufus-lua/issues
173
169
 
174
170
 
175
171
  == irc
@@ -188,8 +184,7 @@ http://github.com/jmettraux/rufus-lua
188
184
 
189
185
  many thanks to the authors of Ruby FFI, and of Lua
190
186
 
191
- http://kenai.com/projects/ruby-ffi/
192
-
187
+ http://wiki.github.com/ffi/ffi
193
188
  http://lua.org/
194
189
 
195
190
 
@@ -197,6 +192,7 @@ http://lua.org/
197
192
 
198
193
  John Mettraux, jmettraux@gmail.com, http://jmettraux.wordpress.com
199
194
  Alain Hoang, http://blogs.law.harvard.edu/hoanga/
195
+ Scott Persinger, http://github.com/scottpersinger/
200
196
 
201
197
 
202
198
  == the rest of Rufus
data/TODO.txt CHANGED
@@ -11,7 +11,7 @@
11
11
  [o] fix coroutine & co / memoization (make it better later)
12
12
  [o] fib.lua, use local, bench ! (2 times faster almost)
13
13
 
14
- [ ] add GC control methods (Alain)
14
+ [o] add GC control methods (Alain)
15
15
  [X] Add method to disable GC
16
16
  [X] Add method to enable GC
17
17
  [ ] Look at parameters in Lua GC that can be tweaked
@@ -21,3 +21,8 @@
21
21
  [x] ruby callbacks (functions)
22
22
  [ ] ability to bind ruby functions (callbacks) inside of tables (modules)
23
23
 
24
+ [ ] state['var'] = value
25
+ [ ] state['var'] = hash
26
+
27
+ [ ] state.load_and_eval(filepath)
28
+
data/lib/rufus/lua/lib.rb CHANGED
@@ -34,14 +34,16 @@ module Lua
34
34
  extend FFI::Library
35
35
 
36
36
  #
37
- # locate dylib
37
+ # locate the dynamic library
38
38
 
39
- paths = Array(ENV['LUA_LIB'] || %w{
40
- /opt/local/lib/liblua.dylib
41
- /usr/local/lib/liblua.dylib
42
- /opt/local/lib/liblua.so
43
- /usr/local/lib/liblua.so
44
- })
39
+ paths = Array(
40
+ ENV['LUA_LIB'] ||
41
+ Dir.glob('/usr/lib/liblua*.so') +
42
+ Dir.glob('/usr/local/lib/liblua*.so') +
43
+ Dir.glob('/opt/local/lib/liblua*.so') +
44
+ Dir.glob('/usr/lib/liblua*.dylib') +
45
+ Dir.glob('/usr/local/lib/liblua*.dylib') +
46
+ Dir.glob('/opt/local/lib/liblua*.dylib'))
45
47
 
46
48
  begin
47
49
 
@@ -63,6 +65,10 @@ module Lua
63
65
 
64
66
  attach_function :luaL_openlibs, [ :pointer ], :void
65
67
 
68
+ %w[ base package string table math io os debug ].each do |libname|
69
+ attach_function "luaopen_#{libname}", [ :pointer ], :void
70
+ end
71
+
66
72
  attach_function :lua_pcall, [ :pointer, :int, :int, :int ], :int
67
73
  #attach_function :lua_resume, [ :pointer, :int ], :int
68
74
 
@@ -32,7 +32,6 @@ module Rufus::Lua
32
32
  class Ref
33
33
  include StateMixin
34
34
 
35
- #
36
35
  # The reference in the Lua registry.
37
36
  # (You shouldn't care about this value)
38
37
  #
@@ -45,18 +44,17 @@ module Rufus::Lua
45
44
  # this pops the object out of the stack !
46
45
  end
47
46
 
48
- #
49
47
  # Frees the reference to this object
50
48
  # (Problably a good idea if you want Lua's GC to get rid of it later).
51
49
  #
52
50
  def free
51
+
53
52
  Lib.luaL_unref(@pointer, LUA_REGISTRYINDEX, @ref)
54
53
  @ref = nil
55
54
  end
56
55
 
57
56
  protected
58
57
 
59
- #
60
58
  # Brings the referenced object on top of the stack (will probably
61
59
  # then take part in a method call).
62
60
  #
@@ -88,7 +86,6 @@ module Rufus::Lua
88
86
  #
89
87
  class Function < Ref
90
88
 
91
- #
92
89
  # Calls the Lua function.
93
90
  #
94
91
  def call (*args)
@@ -110,7 +107,6 @@ module Rufus::Lua
110
107
  #
111
108
  class Coroutine < Ref
112
109
 
113
- #
114
110
  # Resumes the coroutine
115
111
  #
116
112
  def resume (*args)
@@ -125,7 +121,6 @@ module Rufus::Lua
125
121
  pcall(bottom, args.length + 1)
126
122
  end
127
123
 
128
- #
129
124
  # Returns the string status of the coroutine :
130
125
  # suspended/running/dead/normal
131
126
  #
@@ -152,7 +147,6 @@ module Rufus::Lua
152
147
  class Table < Ref
153
148
  include Enumerable
154
149
 
155
- #
156
150
  # The classical 'each'.
157
151
  #
158
152
  # Note it cheats by first turning the table into a Ruby Hash and calling
@@ -165,7 +159,6 @@ module Rufus::Lua
165
159
  self.to_h.each { |k, v| yield(k, v) }
166
160
  end
167
161
 
168
- #
169
162
  # Returns the array of keys of this Table.
170
163
  #
171
164
  def keys
@@ -173,7 +166,6 @@ module Rufus::Lua
173
166
  self.to_h.keys
174
167
  end
175
168
 
176
- #
177
169
  # Returns the array of values in this Table.
178
170
  #
179
171
  def values
@@ -181,7 +173,6 @@ module Rufus::Lua
181
173
  self.to_h.values
182
174
  end
183
175
 
184
- #
185
176
  # Returns the value behind the key, or else nil.
186
177
  #
187
178
  def [] (k)
@@ -192,7 +183,6 @@ module Rufus::Lua
192
183
  stack_pop
193
184
  end
194
185
 
195
- #
196
186
  # Sets a value in the table
197
187
  #
198
188
  # TODO : have something for adding in the array part...
@@ -208,7 +198,6 @@ module Rufus::Lua
208
198
  v
209
199
  end
210
200
 
211
- #
212
201
  # Returns the size of the table, corresponds to the Lua '#' operator.
213
202
  #
214
203
  # Will thus return 0 if the table doesn't hold any value in its
@@ -220,7 +209,6 @@ module Rufus::Lua
220
209
  Lib.lua_objlen(@pointer, -1)
221
210
  end
222
211
 
223
- #
224
212
  # Returns the real size of the table (number of entries + number of elements
225
213
  # in array side)
226
214
  #
@@ -230,7 +218,6 @@ module Rufus::Lua
230
218
  end
231
219
  alias :length :size
232
220
 
233
- #
234
221
  # Returns a Ruby Hash instance representing this Lua table.
235
222
  #
236
223
  def to_h
@@ -259,23 +246,64 @@ module Rufus::Lua
259
246
  h
260
247
  end
261
248
 
262
- #
263
249
  # Returns a Ruby Array instance representing this Lua table.
264
250
  #
265
251
  # Will raise an error if the 'rendering' is not possible.
266
252
  #
267
- def to_a
253
+ # s = Rufus::Lua::State.new
254
+ #
255
+ # @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a
256
+ # # => error !
257
+ #
258
+ # @s.eval("return { 1, 2 }").to_a
259
+ # # => [ 1.0, 2.0 ]
260
+ #
261
+ # @s.eval("return {}").to_a
262
+ # # => []
263
+ #
264
+ # @s.eval("return { 1, 2, car = 'benz' }").to_a
265
+ # # => error !
266
+ #
267
+ # == to_a(false)
268
+ #
269
+ # Setting the optional argument 'pure' to false will manage any table :
270
+ #
271
+ # s = Rufus::Lua::State.new
272
+ #
273
+ # @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a(false)
274
+ # # => [["a", "A"], ["b", "B"], ["c", 3.0]]
275
+ #
276
+ # @s.eval("return { 1, 2 }").to_a(false)
277
+ # # => [1.0, 2.0]
278
+ #
279
+ # @s.eval("return {}").to_a(false)
280
+ # # => []
281
+ #
282
+ # @s.eval("return { 1, 2, car = 'benz' }").to_a(false)
283
+ # # => [1.0, 2.0, ["car", "benz"]]
284
+ #
285
+ def to_a (pure=true)
268
286
 
269
287
  h = self.to_h
270
288
 
271
- keys = h.keys.sort
272
-
273
- keys.find { |k| not [ Float ].include?(k.class) } &&
289
+ pure && h.keys.find { |k| not [ Float ].include?(k.class) } &&
274
290
  raise("cannot turn hash into array, some keys are not numbers")
275
291
 
276
- keys.inject([]) { |a, k| a << h[k]; a }
292
+ a_keys = (1..objlen).to_a.collect { |k| k.to_f }
293
+ keys = a_keys + (h.keys - a_keys)
294
+
295
+ keys.inject([]) { |a, k|
296
+ a << (a_keys.include?(k) ? h[k] : [ k, h[k] ])
297
+ a
298
+ }
277
299
  end
278
300
 
301
+ # Turns the Lua table into a Ruby array, or else into a Ruby Hash instance.
302
+ #
303
+ def to_ruby
304
+
305
+ to_a rescue to_h
306
+ end
279
307
  end
280
308
  end
281
309
 
@@ -25,7 +25,7 @@
25
25
 
26
26
  module Rufus::Lua
27
27
 
28
- VERSION = '1.0.0'
28
+ VERSION = '1.1.0'
29
29
 
30
30
  #
31
31
  # An error class for this gem/library.
@@ -75,7 +75,6 @@ module Rufus::Lua
75
75
 
76
76
  protected
77
77
 
78
- #
79
78
  # This method is used to fetch/cache references to library methods like
80
79
  # 'math.sin' or 'coroutine.resume'.
81
80
  # The caching is done at the Lua state level (ie, all Lua objects available
@@ -92,7 +91,6 @@ module Rufus::Lua
92
91
  end
93
92
  end
94
93
 
95
- #
96
94
  # This method holds the 'eval' mechanism.
97
95
  #
98
96
  def loadstring_and_call (s)
@@ -105,7 +103,6 @@ module Rufus::Lua
105
103
  pcall(bottom, 0) # arg_count is set to 0
106
104
  end
107
105
 
108
- #
109
106
  # Returns a string representation of the state's stack.
110
107
  #
111
108
  def stack_to_s
@@ -135,7 +132,6 @@ module Rufus::Lua
135
132
  s
136
133
  end
137
134
 
138
- #
139
135
  # Outputs the stack to the stdout
140
136
  #
141
137
  def print_stack (msg=nil)
@@ -146,7 +142,6 @@ module Rufus::Lua
146
142
  puts "= ="
147
143
  end
148
144
 
149
- #
150
145
  # Returns the offset (int) of the top element of the stack.
151
146
  #
152
147
  def stack_top
@@ -154,7 +149,6 @@ module Rufus::Lua
154
149
  Lib.lua_gettop(@pointer)
155
150
  end
156
151
 
157
- #
158
152
  # Returns a pair type (int) and type name (string) of the element on top
159
153
  # of the Lua state's stack. There is an optional pos paramter to peek
160
154
  # at other elements of the stack.
@@ -167,7 +161,6 @@ module Rufus::Lua
167
161
  [ type, tname ]
168
162
  end
169
163
 
170
- #
171
164
  # Fetches the top value on the stack (or the one specified by the optional
172
165
  # pos parameter), but does not 'pop' it.
173
166
  #
@@ -184,6 +177,8 @@ module Rufus::Lua
184
177
  when TNUMBER then Lib.lua_tonumber(@pointer, pos)
185
178
 
186
179
  when TTABLE then Table.new(@pointer)
180
+ # warning : this pops up the item from the stack !
181
+
187
182
  when TFUNCTION then Function.new(@pointer)
188
183
  when TTHREAD then Coroutine.new(@pointer)
189
184
 
@@ -191,7 +186,6 @@ module Rufus::Lua
191
186
  end
192
187
  end
193
188
 
194
- #
195
189
  # Pops the top value of lua state's stack and returns it.
196
190
  #
197
191
  def stack_pop
@@ -201,7 +195,6 @@ module Rufus::Lua
201
195
  r
202
196
  end
203
197
 
204
- #
205
198
  # Makes sure the stack loses its top element (but doesn't return it).
206
199
  #
207
200
  def stack_unstack
@@ -216,22 +209,24 @@ module Rufus::Lua
216
209
  Lib.lua_settop(@pointer, new_top)
217
210
  end
218
211
 
219
- #
220
212
  # Given a Ruby instance, will attempt to push it on the Lua stack.
221
213
  #
222
214
  def stack_push (o)
223
215
 
216
+ return stack_push(o.to_lua) if o.respond_to?(:to_lua)
217
+
224
218
  case o
225
219
 
226
220
  when NilClass then Lib.lua_pushnil(@pointer)
227
221
 
228
222
  when TrueClass then Lib.lua_pushboolean(@pointer, 1)
229
- when FalseClass then Lib.lua_pushboolean(@pointer, 1)
223
+ when FalseClass then Lib.lua_pushboolean(@pointer, 0)
230
224
 
231
225
  when Fixnum then Lib.lua_pushinteger(@pointer, o)
232
226
  when Float then Lib.lua_pushnumber(@pointer, o)
233
227
 
234
228
  when String then Lib.lua_pushstring(@pointer, o)
229
+ when Symbol then Lib.lua_pushstring(@pointer, o.to_s)
235
230
 
236
231
  when Hash then stack_push_hash(o)
237
232
  when Array then stack_push_array(o)
@@ -242,7 +237,6 @@ module Rufus::Lua
242
237
  end
243
238
  end
244
239
 
245
- #
246
240
  # Pushes a hash on top of the Lua stack.
247
241
  #
248
242
  def stack_push_hash (h)
@@ -257,7 +251,6 @@ module Rufus::Lua
257
251
  end
258
252
  end
259
253
 
260
- #
261
254
  # Pushes an array on top of the Lua stack.
262
255
  #
263
256
  def stack_push_array (a)
@@ -266,13 +259,12 @@ module Rufus::Lua
266
259
  # since we already know the size of the table...
267
260
 
268
261
  a.each_with_index do |e, i|
269
- stack_push(i)
262
+ stack_push(i + 1)
270
263
  stack_push(e)
271
264
  Lib.lua_settable(@pointer, -3)
272
265
  end
273
266
  end
274
267
 
275
- #
276
268
  # Loads a Lua global value on top of the stack
277
269
  #
278
270
  def stack_load_global (name)
@@ -280,7 +272,6 @@ module Rufus::Lua
280
272
  Lib.lua_getfield(@pointer, LUA_GLOBALSINDEX, name)
281
273
  end
282
274
 
283
- #
284
275
  # Loads the Lua object registered with the given ref on top of the stack
285
276
  #
286
277
  def stack_load_ref (ref)
@@ -288,7 +279,6 @@ module Rufus::Lua
288
279
  Lib.lua_rawgeti(@pointer, LUA_REGISTRYINDEX, @ref)
289
280
  end
290
281
 
291
- #
292
282
  # Returns the result of a function call or a coroutine.resume().
293
283
  #
294
284
  def return_result (stack_bottom)
@@ -301,7 +291,6 @@ module Rufus::Lua
301
291
  (1..count).collect { |pos| stack_pop }.reverse
302
292
  end
303
293
 
304
- #
305
294
  # Assumes the Lua stack is loaded with a ref to a method and arg_count
306
295
  # arguments (on top of the method), will then call that Lua method and
307
296
  # return a result.
@@ -331,7 +320,6 @@ module Rufus::Lua
331
320
  #end
332
321
  #++
333
322
 
334
- #
335
323
  # This method will raise an error with err > 0, else it will immediately
336
324
  # return.
337
325
  #
@@ -352,7 +340,6 @@ module Rufus::Lua
352
340
  raise LuaError.new("#{where} : '#{s}' (#{err})")
353
341
  end
354
342
 
355
- #
356
343
  # Given the name of a Lua global variable, will return its value (or nil
357
344
  # if there is nothing bound under that name).
358
345
  #
@@ -375,17 +362,32 @@ module Rufus::Lua
375
362
  class State
376
363
  include StateMixin
377
364
 
378
- #
379
365
  # Instantiates a Lua state (runtime).
380
366
  #
381
367
  # Accepts an 'include_libs' optional arg. When set to true (the default,
382
368
  # all the base Lua libs are loaded in the runtime.
383
369
  #
370
+ # This optional arg can be set to false, when no libs should be present, or
371
+ # to an array of libs to load in order to prepare the state.
372
+ #
373
+ # The list may include 'base', 'package', 'table', 'string', 'math', 'io',
374
+ # 'os' and 'debug'.
375
+ #
384
376
  def initialize (include_libs=true)
385
377
 
386
378
  @pointer = Lib.luaL_newstate
387
379
 
388
- Lib.luaL_openlibs(@pointer) if include_libs
380
+ Array(include_libs).each do |libname|
381
+
382
+ if libname == false
383
+ break
384
+ elsif libname == true
385
+ Lib.luaL_openlibs(@pointer)
386
+ break
387
+ else
388
+ Lib.send("luaopen_#{libname}", @pointer)
389
+ end
390
+ end
389
391
 
390
392
  #
391
393
  # preparing library methods cache
@@ -394,9 +396,14 @@ module Rufus::Lua
394
396
  attr_reader :__lib_method_cache
395
397
  end
396
398
  @pointer.instance_variable_set(:@__lib_method_cache, {})
399
+
400
+ #
401
+ # an array for preserving callback (Ruby functions) from Ruby
402
+ # garbage collection (Scott).
403
+
404
+ @callbacks = []
397
405
  end
398
406
 
399
- #
400
407
  # Evaluates a piece (string) of Lua code within the state.
401
408
  #
402
409
  def eval (s)
@@ -404,7 +411,6 @@ module Rufus::Lua
404
411
  loadstring_and_call(s)
405
412
  end
406
413
 
407
- #
408
414
  # Returns a value set at the 'global' level in the state.
409
415
  #
410
416
  # state.eval('a = 1 + 2')
@@ -415,7 +421,17 @@ module Rufus::Lua
415
421
  k.index('.') ? self.eval("return #{k}") : get_global(k)
416
422
  end
417
423
 
424
+ # Allows for setting a Lua varible immediately.
425
+ #
426
+ # state['var'] = [ 1, 2, 3 ]
427
+ # puts state['var'].to_a[0] # => 1
418
428
  #
429
+ def []= (k, v)
430
+
431
+ #puts; puts("#{k} = #{Rufus::Lua.to_lua_s(v)}")
432
+ self.eval("#{k} = #{Rufus::Lua.to_lua_s(v)}")
433
+ end
434
+
419
435
  # Binds a Ruby function (callback) in the top environment of Lua
420
436
  #
421
437
  # require 'rubygems'
@@ -440,28 +456,63 @@ module Rufus::Lua
440
456
  #
441
457
  # s.close
442
458
  #
443
- def function (name, &block)
459
+ # == :to_ruby => true
460
+ #
461
+ # Without this option set to true, Lua tables passed to the wrapped
462
+ # Ruby code are instances of Rufus::Lua::Table. With this option set,
463
+ # rufus-lua will call #to_ruby on any parameter that responds to it
464
+ # (And Rufus::Lua::Table does).
465
+ #
466
+ # s = Rufus::Lua::State.new
467
+ #
468
+ # s.function 'is_array', :to_ruby => true do |table|
469
+ # table.is_a?(Array)
470
+ # end
471
+ #
472
+ # s.eval(return is_array({ 1, 2 }))
473
+ # # => true
474
+ # s.eval(return is_array({ 'a' = 'b' }))
475
+ # # => false
476
+ #
477
+ def function (name, opts={}, &block)
444
478
 
445
479
  raise 'please pass a block for the body of the function' unless block
446
480
 
481
+ to_ruby = opts[:to_ruby]
482
+
447
483
  callback = Proc.new do |state|
448
484
 
449
- args = block.arity > 0 ?
450
- (1..block.arity).collect { |i| stack_pop } :
451
- []
485
+ args = []
452
486
 
453
- result = block.call(*args)
487
+ loop do
454
488
 
455
- if result.is_a?(Hash)
456
- stack_push(result)
457
- 1
458
- else
459
- result = Array(result)
460
- result.each { |e| stack_push(e) }
461
- result.size
489
+ break if stack_top == 0 # never touch stack[0] !!
490
+
491
+ arg = stack_fetch
492
+ break if arg.class == Rufus::Lua::Function
493
+
494
+ args.unshift(arg)
495
+
496
+ stack_unstack unless args.first.is_a?(Rufus::Lua::Table)
497
+ end
498
+
499
+ while args.size < block.arity
500
+ args << nil
462
501
  end
502
+
503
+ args = args.collect { |a| a.respond_to?(:to_ruby) ? a.to_ruby : a } \
504
+ if to_ruby
505
+
506
+ result = block.call(*args)
507
+
508
+ stack_push(result)
509
+
510
+ 1
463
511
  end
464
512
 
513
+ @callbacks << callback
514
+ # preserving the callback from garbage collection
515
+
465
516
  name = name.to_s
466
517
 
467
518
  name, index = if ri = name.rindex('.')
@@ -494,7 +545,6 @@ module Rufus::Lua
494
545
  Lib.lua_setfield(@pointer, index, name)
495
546
  end
496
547
 
497
- #
498
548
  # Closes the state.
499
549
  #
500
550
  # It's probably a good idea (mem leaks) to close a Lua state once you're
@@ -507,7 +557,6 @@ module Rufus::Lua
507
557
  @pointer = nil
508
558
  end
509
559
 
510
- #
511
560
  # Returns current amount of memory in KB in use by Lua
512
561
  #
513
562
  def gc_count
@@ -516,7 +565,6 @@ module Rufus::Lua
516
565
  Lib.lua_gc(@pointer, LUA_GCCOUNT, 0)
517
566
  end
518
567
 
519
- #
520
568
  # Runs garbage collection
521
569
  #
522
570
  def gc_collect!
@@ -525,7 +573,6 @@ module Rufus::Lua
525
573
  Lib.lua_gc(@pointer, LUA_GCCOLLECT, 0)
526
574
  end
527
575
 
528
- #
529
576
  # Stop garbage collection for this state
530
577
  #
531
578
  def gc_stop
@@ -534,7 +581,6 @@ module Rufus::Lua
534
581
  Lib.lua_gc(@pointer, LUA_GCSTOP, 0)
535
582
  end
536
583
 
537
- #
538
584
  # Restart garbage collection for this state
539
585
  #
540
586
  def gc_resume
@@ -30,7 +30,6 @@ module Rufus::Lua
30
30
  # to load liblua.dylib
31
31
  #++
32
32
 
33
- #
34
33
  # Turns a Ruby instance into a Lua parseable string representation.
35
34
  #
36
35
  # Will raise an ArgumentError as soon as something else than a simple
@@ -60,7 +59,6 @@ module Rufus::Lua
60
59
  end
61
60
  end
62
61
 
63
- #
64
62
  # Turns a Ruby Array or Hash instance into a Lua parseable string
65
63
  # representation.
66
64
  #
@@ -69,7 +67,7 @@ module Rufus::Lua
69
67
  s = if o.is_a?(Array)
70
68
  o.collect { |e| to_lua_s(e) }
71
69
  else
72
- o.collect { |k, v| "#{to_lua_s(k)}: #{to_lua_s(v)}" }
70
+ o.collect { |k, v| "[#{to_lua_s(k)}] = #{to_lua_s(v)}" }
73
71
  end
74
72
 
75
73
  "{ #{s.join(', ')} }"
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-lua
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  - Alain Hoang
9
+ - Scott Persinger
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2009-03-27 00:00:00 +09:00
14
+ date: 2009-09-30 00:00:00 +09:00
14
15
  default_executable:
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
@@ -30,7 +31,7 @@ executables: []
30
31
  extensions: []
31
32
 
32
33
  extra_rdoc_files:
33
- - README.txt
34
+ - README.rdoc
34
35
  - CHANGELOG.txt
35
36
  - CREDITS.txt
36
37
  - LICENSE.txt
@@ -44,11 +45,12 @@ files:
44
45
  - CHANGELOG.txt
45
46
  - CREDITS.txt
46
47
  - LICENSE.txt
47
- - README.txt
48
- - t.txt
49
48
  - TODO.txt
49
+ - README.rdoc
50
50
  has_rdoc: true
51
51
  homepage: http://rufus.rubyforge.org/
52
+ licenses: []
53
+
52
54
  post_install_message:
53
55
  rdoc_options: []
54
56
 
@@ -69,9 +71,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
71
  requirements:
70
72
  - ffi
71
73
  rubyforge_project: rufus
72
- rubygems_version: 1.3.1
74
+ rubygems_version: 1.3.5
73
75
  signing_key:
74
- specification_version: 2
76
+ specification_version: 3
75
77
  summary: ruby-ffi based bridge from Ruby to Lua
76
78
  test_files:
77
79
  - spec/spec.rb
data/t.txt DELETED
@@ -1,24 +0,0 @@
1
- $ ruby19 test/bm_fiber.rb
2
- user system total real
3
- ruby 0.050000 0.010000 0.060000 ( 0.054605)
4
- lua via ruby 0.180000 0.000000 0.180000 ( 0.189010)
5
- lua 0.010000 0.000000 0.010000 ( 0.005543)
6
-
7
- $ ruby19 test/bm_fiber.rb
8
- user system total real
9
- ruby 0.050000 0.000000 0.050000 ( 0.051531)
10
- lua via ruby 0.180000 0.010000 0.190000 ( 0.194944)
11
- lua 0.010000 0.000000 0.010000 ( 0.006325)
12
-
13
- $ ruby19 test/bm_fiber.rb
14
- user system total real
15
- ruby 0.050000 0.010000 0.060000 ( 0.052032)
16
- lua via ruby 0.180000 0.000000 0.180000 ( 0.195411)
17
- lua 0.010000 0.000000 0.010000 ( 0.006394)
18
-
19
- $ ruby19 test/bm_fiber.rb
20
- user system total real
21
- ruby 0.050000 0.010000 0.060000 ( 0.054892)
22
- lua via ruby 0.180000 0.000000 0.180000 ( 0.267880)
23
- lua 0.000000 0.000000 0.000000 ( 0.005865)
24
-