Skip to content

Commit

Permalink
feat: removed lua_reg library
Browse files Browse the repository at this point in the history
feat: removed some lua 5.3 functions
  • Loading branch information
mdwagner committed Dec 13, 2023
1 parent 20cdc2a commit f3eb1b6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 90 deletions.
18 changes: 8 additions & 10 deletions src/luajit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ module Luajit

# Creates a Lua object for *type*
#
# NOTE: Will _mutate_ *type* if no global name or metatable name is set
# NOTE: Will mutate *type* global name or metatable name unless set
def self.create_lua_object(state : LuaState, type : T.class) : Nil forall T
{% unless T < Luajit::LuaObject %}
{% raise "'type' argument must be a Luajit::LuaObject" %}
{% end %}

state.new_table
class_table_index = state.size

unless type.global?
type.global_name(type.default_global)
end
state.push_value(class_table_index)
state.set_global(type.global)
state.register(type.global)
class_table_index = state.size

unless type.metatable?
type.metatable_name(type.global)
Expand All @@ -75,15 +72,16 @@ module Luajit
end
state.set_table(instance_table_index)

type.instance_methods.each do |name, proc|
next if name == "__gc"
type.instance_methods.reject("__gc").each do |name, proc|
state.push(name)
state.push(proc)
state.set_table(instance_table_index)
end

state.push_value(instance_table_index)
state.set_field(instance_table_index, "__index")
unless type.instance_methods["__index"]?
state.push_value(instance_table_index)
state.set_field(instance_table_index, "__index")
end
end

# Converts *value* into full userdata with metatable *type*
Expand Down
14 changes: 0 additions & 14 deletions src/luajit/lua_reg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,4 @@ module Luajit
raise "block cannot be a closure" if @function.closure?
end
end

class LuaReg::Library
getter name : String
getter regs = [] of LuaReg

def initialize(@name)
end

# Raises exception if passed block is a closure
def register(name : String, &block : LuaCFunction) : self
regs << LuaReg.new(name, block)
self
end
end
end
67 changes: 1 addition & 66 deletions src/luajit/lua_state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1010,73 +1010,8 @@ module Luajit
end
end

# Ensure that stack[idx][fname] has a table and push that table onto the stack
#
# Raises `LuaError` if operation fails
#
# NOTE: Adopted from Lua 5.3
def get_subtable(index : Int32, fname : String) : Bool
begin
get_field(index, fname)
if is_table?(-1)
true
else
pop(1)
index = abs_index(index)
new_table
push_value(-1)
set_field(index, fname)
false
end
rescue err : LuaError
raise LuaError.new("Failed to get subtable", err)
end
end

# Stripped-down 'require': After checking "loaded" table, calls 'openf'
# to open a module, registers the result in 'package.loaded' table and,
# if 'glb' is true, also registers the result in the global table.
# Leaves resulting module on the top.
#
# Raises `LuaError` if operation fails
#
# NOTE: Adopted from Lua 5.3
def requiref(modname : String, openf : LuaCFunction, glb : Bool = false) : Nil
begin
get_subtable("_LOADED")
get_field(-1, modname)
unless to_boolean(-1)
pop(1)
push(openf)
push(modname)
pcall(1, 1)
push_value(-1)
set_field(-3, modname)
end
remove(-2)
if glb
push_value(-1)
set_global(modname)
end
rescue err : LuaError
raise LuaError.new("Failed to require", err)
end
end

# Opens a *library*
#
# If *name?* == `false`, only registers library functions to table
# at the top of the stack
def register(library : LuaReg::Library, name? = true) : Nil
if name?
register(library.name, library.regs)
else
register(library.regs)
end
end

# Opens a library with *name* and registers all functions in *regs*
def register(name : String, regs : Array(LuaReg)) : Nil
def register(name : String, regs = [] of LuaReg) : Nil
libs = [] of LibLuaJIT::Reg
regs.each do |reg|
libs << LibLuaJIT::Reg.new(name: reg.name, func: reg.function.pointer)
Expand Down

0 comments on commit f3eb1b6

Please sign in to comment.