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.8.1'

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



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

def initialize(sandbox, options = {})
  @sandbox                 = sandbox
  @state                   = options.fetch(:state) { Lua::State.new }
  @logger                  = options.fetch(:logger) { Logger.new('/dev/null') }
  leak_locals              = options.fetch(:leak_globals) { false }
  setup(leak_locals)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



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

def sandbox
  @sandbox
end

#stateObject (readonly)

Returns the value of attribute state.



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

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



37
38
39
40
# File 'lib/transparent_lua.rb', line 37

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