Class: Lua::State

Inherits:
Object
  • Object
show all
Defined in:
ext/rlua.c

Instance Method Summary collapse

Constructor Details

#Lua::State.newObject

Creates a new Lua state.



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'ext/rlua.c', line 636

static VALUE rbLua_initialize(VALUE self)
{
  lua_State* state = luaL_newstate();
  
  VALUE rbState = Data_Wrap_Struct(rb_cObject, 0, lua_close, state);
  rb_iv_set(self, "@state", rbState);
  
  lua_pushlightuserdata(state, (void*) self);
  lua_setfield(state, LUA_REGISTRYINDEX, "rlua_state");
  
  lua_newtable(state);
  lua_setfield(state, LUA_REGISTRYINDEX, "rlua");
  
  rb_iv_set(rbState, "@refs", rb_hash_new());
  rb_iv_set(rbState, "@procs", rb_ary_new());
  
  return self;
}

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

See Lua::Table#method_missing. Equivalent to __env.method_missing(method, *args).



861
862
863
864
# File 'ext/rlua.c', line 861

static VALUE rbLua_method_missing(int argc, VALUE* argv, VALUE self)
{
  return rbLuaTable_method_missing(argc, argv, rbLua_get_env(self));
}

Instance Method Details

#[](key) ⇒ Object

Returns value of a global variable. Equivalent to __env.



802
803
804
805
806
# File 'ext/rlua.c', line 802

static VALUE rbLua_get_global(VALUE self, VALUE index)
{
  VALUE globals = rbLua_get_env(self);
  return rbLuaTable_get(globals, index);
}

#[]=(key) ⇒ Object

Sets value for a global variable. Equivalent to __env = value.



813
814
815
816
# File 'ext/rlua.c', line 813

static VALUE rbLua_set_global(VALUE self, VALUE index, VALUE value)
{
  return rbLuaTable_set(rbLua_get_env(self), index, value);
}

#__bootstraptrue

Deploys an absolute minimum of functions required to write minimally useful Lua programs. This is really a subset of Lua base library (copied from Lua 5.1 sources) that may be handy if you don’t like standard function layout. All of these functions can be implemented in pure Ruby, but that will slow down Lua code incredibly.

If you want to get familiar layout described in Lua documentation, check #__load_stdlib function.

Exact list of functions defined: type, next, tonumber, tostring, unpack, select, error, assert, pcall, xpcall.

Returns:

  • (true)


1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'ext/rlua.c', line 1085

static VALUE rbLua_bootstrap(VALUE self)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  int nf;
  for(nf = 0; nf < sizeof(stdlib) / sizeof(stdlib[0]); nf++) {
    lua_pushcclosure(state, stdlib[nf].func, 0);
    lua_setglobal(state, stdlib[nf].name);
  }
  
  lua_pushcfunction(state, bootstrap_next);
  lua_pushcclosure(state, bootstrap_pairs, 1);
  lua_setglobal(state, "pairs");
  
  lua_pushcfunction(state, bootstrap_inext);
  lua_pushcclosure(state, bootstrap_ipairs, 1);
  lua_setglobal(state, "ipairs");
  
  return Qtrue;
}

#__envLua::Table

Returns environment table of Lua::State or Lua::Function.

Returns:



688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'ext/rlua.c', line 688

static VALUE rbLua_get_env(VALUE self)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  VALUE ref;
  rlua_push_var(state, self);                   // stack: |this|...
  lua_getfenv(state, -1);                       //        |envi|this|...
  ref = rlua_makeref(state);                    //        |envi|this|...
  lua_pop(state, 2);                            //        ...

  return rb_funcall(cLuaTable, rb_intern("new"), 2, self, ref);
}

#__env=(table) ⇒ Object

Sets environment table for Lua::State or Lua::Function. table may be a Lua::Table or a Hash.



708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'ext/rlua.c', line 708

static VALUE rbLua_set_env(VALUE self, VALUE env)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  if(rb_obj_class(env) != cLuaTable && TYPE(env) != T_HASH)
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Lua::Table or Hash)", rb_obj_classname(env));

  rlua_push_var(state, self);                      // stack: |this|...
  rlua_push_var(state, env);                       //        |envi|this|...
  lua_setfenv(state, -2);                          //        |this|...
  lua_pop(state, 1);                               //        ...
  
  return env;
}

#__eval(code[, chunkname = '=&lt;eval&gt;']) ⇒ Object

Runs code in Lua interpreter. Optional argument chunkname specifies a string that will be used in error messages and other debug information as a file name.

Start chunkname with a @ to make Lua think the following is filename (e.g. @test.lua); start it with a = to indicate a non-filename stream (e.g. =stdin). Anything other is interpreted as a plaintext Lua code and a few starting characters will be shown.



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'ext/rlua.c', line 667

static VALUE rbLua_eval(int argc, VALUE* argv, VALUE self)
{
  VALUE code, chunkname;
  rb_scan_args(argc, argv, "11", &code, &chunkname);

  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  if(chunkname == Qnil)
    chunkname = rb_str_new2("=<eval>");

  rlua_load_string(state, code, chunkname);
  
  return rlua_pcall(state, 0);
}

#__get_metatable(object) ⇒ Lua::Table?

Returns metatable of any valid Lua object or nil if it is not present. If you want to get metatables of non-table objects (e.g. numbers) just pass their Ruby equivalent.

Returns:



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'ext/rlua.c', line 731

static VALUE rbLua_get_metatable(VALUE self, VALUE object)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  rlua_push_var(state, object);                 // stack: |objt|...
  if(lua_getmetatable(state, -1)) {             //        |meta|objt|...
    VALUE ref = rlua_makeref(state);            //        |meta|objt|...
    lua_pop(state, 2);                          //        ...
    
    return rb_funcall(cLuaTable, rb_intern("new"), 2, self, ref);
  } else {                                      //        |objt|...
    lua_pop(state, 1);                          //        ...
    
    return Qnil;
  }
}

#__load_stdlib(*libs) ⇒ true

Loads Lua standard libraries. There are two ways of calling this function:

If you will call it as __load_stdlib(:all), it is equivalent to calling C luaL_openlibs function.

If you will pass it symbolized names of separate libraries (like :base), it is equivalent to calling corresponding luaopen_* functions.

Examples:

# Load all standard libraries
state = Lua::State.new
state.__load_stdlib :all

# Load only math, string and table libraries
state = Lua::State.new
state.__load_stdlib :math, :string, :table

Exact list of libraries recognized: :base, :table, :math, :string, :debug, :io, :os, :package. Anything not included in this list will be silently ignored.

Returns:

  • (true)


1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'ext/rlua.c', line 1139

static VALUE rbLua_load_stdlib(VALUE self, VALUE args)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  if(rb_ary_includes(args, ID2SYM(rb_intern("all")))) {
    luaL_openlibs(state);
  } else {
    if(rb_ary_includes(args, ID2SYM(rb_intern("base"))))
      rlua_openlib(state, luaopen_base);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_TABLIBNAME))))
      rlua_openlib(state, luaopen_table);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_MATHLIBNAME))))
      rlua_openlib(state, luaopen_math);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_STRLIBNAME))))
      rlua_openlib(state, luaopen_string);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_DBLIBNAME))))
      rlua_openlib(state, luaopen_debug);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_IOLIBNAME))))
      rlua_openlib(state, luaopen_io);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_OSLIBNAME))))
      rlua_openlib(state, luaopen_os);
    if(rb_ary_includes(args, ID2SYM(rb_intern(LUA_LOADLIBNAME))))
      rlua_openlib(state, luaopen_package);
  }
  
  return Qtrue;
}

#__set_metatable=(object, metatable) ⇒ Object

Sets metatable for any valid Lua object. metatable can be Lua::Table or Hash. If you want to set metatables for non-table objects (e.g. numbers) just pass their Ruby equivalent.

# Implement concatenation operator for Lua strnigs.
state = Lua::State.new
state.__set_metatable("", { '__add' => lambda{ |a, b| a + b } })
p state.__eval('return "hello," + " world"') # => "hello, world"


761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/rlua.c', line 761

static VALUE rbLua_set_metatable(VALUE self, VALUE object, VALUE metatable)
{
  lua_State* state;
  Data_Get_Struct(rb_iv_get(self, "@state"), lua_State, state);
  
  if(rb_obj_class(metatable) != cLuaTable && TYPE(metatable) != T_HASH)
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Lua::Table or Hash)", rb_obj_classname(metatable));

  rlua_push_var(state, object);                    // stack: |objt|...
  rlua_push_var(state, metatable);                 //        |meta|objt|...
  lua_setmetatable(state, -2);                     //        |objt|...
  lua_pop(state, 1);                               //        ...
  
  return metatable;
}