Lua::State represents Lua interpreter state which is one thread of execution.
Methods
Public Class
Public Instance
Public Instance Aliases
to_a | -> | to_ary |
Public Class methods
Iterates over a Lua table referenced by
table
. This function is analogue for Lua next function, but can be used even if Lua one is not defined.
You can use this method yourself, through it is easier to invoke #to_a or to_hash.
static VALUE rbLuaTable_next(VALUE self, VALUE table, VALUE index) { lua_State* state; Data_Get_Struct(rb_iv_get(table, "@state"), lua_State, state); VALUE retval; rlua_push_var(state, table); // stack: |this|... rlua_push_var(state, index); // |indx|this|... if(lua_next(state, -2) != 0) { // |valu|key |this|... VALUE value, key; value = rlua_get_var(state); // |valu|key |this|... lua_pop(state, 1); // |key |this|... key = rlua_get_var(state); // |key |this|... lua_pop(state, 2); // ... retval = rb_ary_new(); rb_ary_push(retval, key); rb_ary_push(retval, value); } else { // |this|... retval = Qnil; lua_pop(state, 1); // ... } return retval; }
Public Instance methods
Compares this
with other
. May call
__eq
metamethod.
static VALUE rbLua_equal(VALUE self, VALUE other) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); int equal; rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, other); // |othr|this|... equal = lua_equal(state, -1, -2); // |othr|this|... lua_pop(state, 2); // ... return equal ? Qtrue : Qfalse; }
static VALUE rbLuaTable_get(VALUE self, VALUE index) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); VALUE value; rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, index); // |indx|this|... lua_gettable(state, -2); // |valu|this|... value = rlua_get_var(state); // |valu|this|... lua_pop(state, 2); // ... return value; }
static VALUE rbLuaTable_set(VALUE self, VALUE index, VALUE value) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, index); // |indx|this|... rlua_push_var(state, value); // |valu|indx|this|... lua_settable(state, -3); // |this|... lua_pop(state, 1); // ... return value; }
Compares this
with other
without calling any
metamethods.
static VALUE rbLua_rawequal(VALUE self, VALUE other) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); int equal; rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, other); // |othr|this|... equal = lua_rawequal(state, -1, -2); // |othr|this|... lua_pop(state, 2); // ... return equal ? Qtrue : Qfalse; }
Returns value associated with key
in Lua table without invoking any Lua metamethod (similar to rawget
Lua function).
static VALUE rbLuaTable_rawget(VALUE self, VALUE index) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); VALUE value; rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, index); // |indx|this|... lua_rawget(state, -2); // |valu|this|... value = rlua_get_var(state); // |valu|this|... lua_pop(state, 2); // ... return value; }
Returns table length as with Lua length # operator. Will not call any metamethod because __len metamethod has no effect when defined on table (and string).
static VALUE rbLuaTable_length(VALUE self) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); VALUE length; rlua_push_var(state, self); // stack: |this|... length = INT2FIX(lua_objlen(state, -1)); lua_pop(state, 1); // ... return length; }
Returns metatable of this table or nil if it is not present.
static VALUE rbLuaTable_get_metatable(VALUE self) { return rbLua_get_metatable(self, self); }
Sets metatable for this table. metatable
can be Lua::Table or Hash.
static VALUE rbLuaTable_set_metatable(VALUE self, VALUE metatable) { return rbLua_set_metatable(self, self, metatable); }
Sets value associated with key
to value
in Lua table without invoking any Lua metamethod (similar to rawset
Lua function).
static VALUE rbLuaTable_rawset(VALUE self, VALUE index, VALUE value) { lua_State* state; Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state); rlua_push_var(state, self); // stack: |this|... rlua_push_var(state, index); // |indx|this|... rlua_push_var(state, value); // |valu|indx|this|... lua_rawset(state, -3); // |this|... lua_pop(state, 1); // ... return value; }
Traverses the table using ::next function.
# File lib/rlua.rb, line 21 def each(&block) key = nil loop { key, value = *self.class.next(self, key) break if key.nil? block.call(key, value) } self end
Recursively pretty-prints the table properly handling reference loops.
trace
argument is internal, do not use it.
# File lib/rlua.rb, line 55 def inspect(trace=[]) if to_hash.empty? "L{}" elsif trace.include? self "L{...}" else trace << self v = [] each { |key, value| s = "" if key.class == self.class s += key.inspect(trace) else s += key.inspect end s += " => " if value.class == self.class s += value.inspect(trace) else s += value.inspect end v << s } "L{#{v.join ', '}}" end end
This method allows accessing Lua tables much as if they were Ruby objects.
Setting values can be done with a standard setter
method: table.key = value
is equivalent to
table["key"] = value
.
Getting values is as easy as setting: if the value is not
a function, table.key
is equivalent to
table["key"]
.
If some table has a function as value, you can invoke
methods on it. table.method(arg1, ..., argN)
is
equivalent to table["method"].call(arg1, ..., argN)
, and
table.method!(arg1, ..., argN)
is equivalent to
table["method"].call(table, arg1, ..., argN)
. To get a
reference to function you should use the table["method"]
notation.
If the value is not present in table (which is equivalent to
nil
value in Lua) MethodNotFound
exception will be raised; if you will attempt to call something which is
not a function as a method, TypeError exception will be raised.
static VALUE rbLuaTable_method_missing(int argc, VALUE* argv, VALUE self) { VALUE id, args; rb_scan_args(argc, argv, "1*", &id, &args); VALUE name = rb_str_new2(rb_id2name(rb_to_id(id))); int is_method = 0; int is_assign = 0; if(RSTRING_PTR(name)[RSTRING_LEN(name) - 1] == '!') is_method = 1; if(RSTRING_PTR(name)[RSTRING_LEN(name) - 1] == '=') is_assign = 1; if(is_method || is_assign) rb_str_resize(name, RSTRING_LEN(name) - 1); if(is_assign) { VALUE value; rb_scan_args(argc, argv, "11", &id, &value); return rbLuaTable_set(self, name, value); } else { VALUE value = rbLuaTable_get(self, name); if(value == Qnil) { return rb_call_super(argc, argv); } else if(rb_obj_class(value) != cLuaFunction) { if(is_method) rb_raise(rb_eTypeError, "%s is not a Lua::Function", RSTRING_PTR(name)); return value; } else { if(is_method) rb_ary_unshift(args, self); return rbLuaFunction_call(value, args); } } }
Converts table to array. Only integer indexes are used.
# File lib/rlua.rb, line 39 def to_ary ary = [] 1.upto(__length) { |i| if self[i] ary << self[i] else break end } ary end
Non-recursively converts table to hash.
# File lib/rlua.rb, line 32 def to_hash hash = {} each { |k, v| hash[k] = v } hash end