Method: Immunio::LuaVM#initialize

Defined in:
lib/immunio/vm.rb

#initialize(key, secret, dev_mode = false, debug_mode = false) ⇒ LuaVM

Returns a new instance of LuaVM.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/immunio/vm.rb', line 165

def initialize(key, secret, dev_mode = false, debug_mode = false)
  @state = Rufus::Lua::State.new

  # Bootstrap the VM ...

  # Define load path for `require`.
  lua_hooks = File.expand_path("#{Immunio::DIR}/../lua-hooks")
  @state['package.path'] = lua_hooks + "/lib/?.lua;" + lua_hooks + "/lib/lexers/?.lua"
  # Set the config key
  @state['IMMUNIO_KEY'] = key
  # Set the secret key
  @state['IMMUNIO_SECRET'] = secret
  # Pass dev mode option
  @state['DEV_MODE'] = dev_mode
  # Pass debug mode option
  @state['DEBUG_MODE'] = debug_mode
  # Pass platform to the vm.
  # From: http://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on
  if (/cygwin|mswin|mingw|bccwin|wince|emx|windows/ =~ RUBY_PLATFORM) != nil then
    @state['LUA_PLATFORM'] = 'windows'
  elsif (/darwin/ =~ RUBY_PLATFORM) != nil
    @state['LUA_PLATFORM'] = 'darwin'
  else
    @state['LUA_PLATFORM'] = 'unix'
  end
  # Boot it. Yeaaaaah!
  @state.eval "require 'boot'"

  @error_handler = @state["debug.traceback"]
  @call_function = @state['sandboxed_call']

  # The pass function acts as a Ruby => Lua converter.
  # It simply passes back it's first argument.
  # Arguments are converted from Ruby to Lua by rufus-lua.
  @pass_function = @state.eval "return function(obj) return obj end"

  self.class.check_rufus_stack @state, "Stack not empty after bootstrap"
end