Class: Rvim::Lua::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/rvim/lua/runtime.rb

Overview

One Lua VM per Editor. Wraps Rufus::Lua::State and registers the ‘vim` global. If the rufus-lua gem or a Lua 5.1-compatible dynamic library isn’t available, every public method becomes a no-op that surfaces a status_message — the editor stays usable, only Lua features are off.

Constant Summary collapse

LIBRARY_CANDIDATES =
[
  ENV['LUA_LIB'],
  '/opt/homebrew/opt/luajit/lib/libluajit-5.1.dylib',
  '/usr/local/opt/luajit/lib/libluajit-5.1.dylib',
  '/opt/homebrew/lib/libluajit-5.1.dylib',
  '/usr/local/lib/libluajit-5.1.dylib',
  '/opt/homebrew/lib/liblua5.1.dylib',
  '/usr/local/lib/liblua5.1.dylib',
  '/usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2',
  '/usr/lib/liblua5.1.so',
].compact.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ Runtime

Returns a new instance of Runtime.



62
63
64
65
66
67
68
# File 'lib/rvim/lua/runtime.rb', line 62

def initialize(editor)
  @editor = editor
  @state = nil
  @callbacks = {} # id => Lua function ref
  @next_callback_id = 0
  @initialized = false
end

Instance Attribute Details

#editorObject (readonly)

Returns the value of attribute editor.



60
61
62
# File 'lib/rvim/lua/runtime.rb', line 60

def editor
  @editor
end

#stateObject (readonly)

Returns the value of attribute state.



60
61
62
# File 'lib/rvim/lua/runtime.rb', line 60

def state
  @state
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/rvim/lua/runtime.rb', line 23

def available?
  ensure_loaded
  @available == true
end

.unavailable_reasonObject



28
29
30
31
# File 'lib/rvim/lua/runtime.rb', line 28

def unavailable_reason
  ensure_loaded
  @unavailable_reason
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rvim/lua/runtime.rb', line 70

def available?
  self.class.available?
end

#call_callback(id, *args) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/rvim/lua/runtime.rb', line 116

def call_callback(id, *args)
  fn = @callbacks[id]
  return nil unless fn

  fn.call(*args)
rescue => e
  @editor.status_message = "E5108: Lua callback error: #{e.message}"
  nil
end

#eval(code) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rvim/lua/runtime.rb', line 84

def eval(code)
  unless available?
    @editor.status_message = "Lua disabled: #{self.class.unavailable_reason}"
    return nil
  end

  state.eval(code)
rescue Rufus::Lua::LuaError => e
  @editor.status_message = "E5108: Lua: #{e.message}"
  nil
rescue => e
  @editor.status_message = "E5108: Lua error: #{e.message}"
  nil
end

#load_file(path) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rvim/lua/runtime.rb', line 99

def load_file(path)
  unless File.file?(path)
    @editor.status_message = "E484: Can't open file #{path}"
    return nil
  end

  eval(File.read(path))
end

#register_callback(lua_fn) ⇒ Object

Callback registry: keep Ruby-side refs to Lua functions so they can be invoked later (e.g. autocmd handlers).



110
111
112
113
114
# File 'lib/rvim/lua/runtime.rb', line 110

def register_callback(lua_fn)
  id = (@next_callback_id += 1)
  @callbacks[id] = lua_fn
  id
end

#remove_callback(id) ⇒ Object



126
127
128
# File 'lib/rvim/lua/runtime.rb', line 126

def remove_callback(id)
  @callbacks.delete(id)
end