Class: TransparentLua

Inherits:
Object
  • Object
show all
Defined in:
lib/transparent_lua.rb,
lib/transparent_lua/version.rb

Constant Summary collapse

SUPPORTED_SIMPLE_DATATYPES =
[
    NilClass,
    TrueClass,
    FalseClass,
    Fixnum,
    Bignum,
    Float,
    Proc,
    String,
    Hash,
    Array,
]
VERSION =
0.6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sandbox, options = {}) ⇒ TransparentLua

Returns a new instance of TransparentLua.

Parameters:

  • sandbox (Object)

    The object which will be made visible to the lua script

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :state (Lua::State) — default: Lua::State.new

    a lua state to use

  • :leak_globals (Boolean)

    When true, all locals from the lua scope are set in the sandbox. The sandbox must store the values itself or an error will be raised. When false the locals are not reflected in the sandbox



25
26
27
28
29
30
# File 'lib/transparent_lua.rb', line 25

def initialize(sandbox, options = {})
  @sandbox    = sandbox
  @state      = options.fetch(:state) { Lua::State.new }
  leak_locals = options.fetch(:leak_globals) { false }
  setup(leak_locals)
end

Instance Attribute Details

#sandboxObject (readonly)

Returns the value of attribute sandbox.



17
18
19
# File 'lib/transparent_lua.rb', line 17

def sandbox
  @sandbox
end

#stateObject (readonly)

Returns the value of attribute state.



17
18
19
# File 'lib/transparent_lua.rb', line 17

def state
  @state
end

Instance Method Details

#call(script, script_name = nil) ⇒ Object

Returns the return value of the lua script.

Parameters:

  • script (String)

    a lua script

  • script_name (String) (defaults to: nil)

    the name of the lua script (#see Lua::State.__eval)

Returns:

  • (Object)

    the return value of the lua script



35
36
37
38
# File 'lib/transparent_lua.rb', line 35

def call(script, script_name = nil)
  v = state.__eval(script, script_name)
  lua2rb(v)
end