Skip to content

Commit

Permalink
feat: wrappers + api changes .new
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwagner committed Sep 13, 2024
1 parent 2039e14 commit a16fb24
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 117 deletions.
34 changes: 0 additions & 34 deletions spec/luajit_spec.cr

This file was deleted.

29 changes: 29 additions & 0 deletions spec/luajit_wrappers_nil_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "./spec_helper"
require "../src/wrappers/nil"

describe Luajit::Wrappers::NIL do
it ".setup" do
Luajit.run do |state|
Luajit::Wrappers::NIL.setup(state)

state.execute(<<-LUA).ok?.should be_true
local x = NIL
assert(type(x) == 'userdata')
assert(x ~= nil)
assert(tostring(x) == 'NIL')
LUA
end
end

it ".is_nil?" do
Luajit.run do |state|
Luajit::Wrappers::NIL.setup(state)

state.execute("return NIL").ok?.should be_true
Luajit::Wrappers::NIL.is_nil?(state, -1).should be_true

state.execute("return nil").ok?.should be_true
Luajit::Wrappers::NIL.is_nil?(state, -1).should be_false
end
end
end
2 changes: 0 additions & 2 deletions src/io.cr

This file was deleted.

43 changes: 8 additions & 35 deletions src/luajit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ require "./luajit/*"

module Luajit
# :nodoc:
# Define Wrappers namespace
module Wrappers
end

# Same as `LuaState.create`
def self.new : LuaState
LuaState.create
def self.new(defaults : Bool = false) : LuaState
LuaState.create.tap do |state|
state.open_library(:all) if defaults
end
end

# Same as `.new`, but also opens all Lua libraries
@[Deprecated("Use `.new(defaults: true)` instead")]
def self.new_with_defaults : LuaState
new.tap do |state|
state.open_library(:all)
end
new(defaults: true)
end

# Same as `LuaState.destroy`
Expand All @@ -29,8 +29,8 @@ module Luajit
end

# Yields a new `LuaState` and closes it at end of block
def self.run(& : LuaState ->) : Nil
state = new_with_defaults
def self.run(defaults : Bool = true, & : LuaState ->) : Nil
state = new(defaults: defaults)
begin
yield state
ensure
Expand Down Expand Up @@ -107,31 +107,4 @@ module Luajit
ud_ptr = state.get_userdata(index, type.metatable)
Box(T).unbox(ud_ptr)
end

# Sets `NIL` as a global value with metatable name *mt_name*
#
# See `.is_global_nil?`
def self.setup_global_nil(state : LuaState, mt_name = "luajit_cr::__NIL__") : Nil
state.new_userdata(0_u64)
state.push({
"__tostring" => Luajit::LuaState::Function.new { |__state|
__state.push("NIL")
1
}
})
state.push_value(-1)
state.set_registry(mt_name)
state.set_metatable(-2)
state.set_global("NIL")
end

# Checks if value at *index* is the global `NIL` value with metatable name *mt_name*
def self.is_global_nil?(state : LuaState, index : Int32, mt_name = "luajit_cr::__NIL__") : Bool
begin
state.check_userdata!(index, mt_name)
true
rescue LuaError
false
end
end
end
10 changes: 0 additions & 10 deletions src/luajit/wrappers/io.cr

This file was deleted.

4 changes: 0 additions & 4 deletions src/luajit/wrappers/io.lua

This file was deleted.

2 changes: 0 additions & 2 deletions src/path.cr

This file was deleted.

2 changes: 0 additions & 2 deletions src/string.cr

This file was deleted.

10 changes: 10 additions & 0 deletions src/wrappers/io.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "../luajit"

class Luajit::Wrappers::IO < Luajit::LuaObject
global_name "IO"
metatable_name "__IO__"

def self.setup(state : Luajit::LuaState) : Nil
Luajit.create_lua_object(state, self)
end
end
4 changes: 4 additions & 0 deletions src/wrappers/io.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---@meta _

---@class (exact) IO
IO = {}
26 changes: 26 additions & 0 deletions src/wrappers/nil.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "../luajit"

class Luajit::Wrappers::NIL
def self.setup(state : Luajit::LuaState) : Nil
state.new_userdata(0_u64)
state.push({
"__tostring" => Luajit::LuaState::Function.new { |__state|
__state.push("NIL")
1
}
})
state.push_value(-1)
state.set_registry("__NIL__")
state.set_metatable(-2)
state.set_global("NIL")
end

def self.is_nil?(state : Luajit::LuaState, index : Int32) : Bool
begin
state.check_userdata!(index, "__NIL__")
true
rescue Luajit::LuaError
false
end
end
end
File renamed without changes.
27 changes: 15 additions & 12 deletions src/luajit/wrappers/path.cr → src/wrappers/path.cr
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require "../../luajit"
require "../luajit"

class Luajit::Wrappers::Path < Luajit::LuaObject
global_name "__PATH__"
global_name "Path"
metatable_name "__PATH__"

# ---@field new fun(path: string?): self
def_class_method "new" do |state|
_self = (
if state.is_none?(1)
new(::Path.new)
elsif state.is_string?(1)
new(::Path.new(state.to_string(1)))
else
state.raise_type_error!(1, "expected string or nil")
end
)
if state.is_none?(1)
_self = new(::Path.new)
elsif state.is_string?(1)
_self = new(::Path.new(state.to_string(1)))
else
state.raise_type_error!(1, "expected string or nil")
end
Luajit.setup_userdata(state, _self, self)
1
end
Expand Down Expand Up @@ -53,14 +52,18 @@ class Luajit::Wrappers::Path < Luajit::LuaObject
1
end

# ---@operator tostring(self): self
# ---@operator tostring(self): string
def_instance_method "__tostring" do |state|
state.assert_userdata!(1)
_self = Luajit.userdata_value(state, self, 1)
state.push _self.path.to_s
1
end

def self.setup(state : Luajit::LuaState) : Nil
Luajit.create_lua_object(state, self)
end

property path : ::Path

def initialize(@path)
Expand Down
18 changes: 9 additions & 9 deletions src/luajit/wrappers/path.lua → src/wrappers/path.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
---@meta _

---@class __PATH__DriveAndRoot
---@class PathDriveAndRoot
---@field drive string|NIL
---@field root string|NIL

---@class __PATH__ExpandOptions
---@field base __PATH__|string?
---@field home __PATH__|string|boolean?
---@class PathExpandOptions
---@field base Path|string?
---@field home Path|string|boolean?
---@field expand_base boolean?

---@class (exact) __PATH__
---@class (exact) Path
---@field new fun(path: string?): self
---@field home fun(): self
---@field is_absolute fun(self): boolean
---@field anchor fun(self): self?
---@field basename fun(self, suffix: string?): string
---@field dirname fun(self): string
---@field drive fun(self): self?
---@field drive_and_root fun(self): __PATH__DriveAndRoot
---@field each_parent fun(self, cb: fun(path: __PATH__))
---@field drive_and_root fun(self): PathDriveAndRoot
---@field each_parent fun(self, cb: fun(path: Path))
---@field each_part fun(self, cb: fun(str: string))
---@field ends_with_separator fun(self): boolean
---@field expand fun(self, options: __PATH__ExpandOptions?)
---@field expand fun(self, options: PathExpandOptions?)
---@field extension fun(self): string
---@field join fun(self, parts: string[]|string): self
---@field normalize fun(self, remove_final_separator: boolean?)
Expand All @@ -34,4 +34,4 @@
---@field sibling fun(self, name: self|string): self
---@field stem fun(self): string
---@operator concat(): string
__PATH__ = {}
Path = {}
10 changes: 5 additions & 5 deletions src/luajit/wrappers/string.cr → src/wrappers/string.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "../../luajit"
require "../luajit"

class Luajit::Wrappers::String < Luajit::LuaObject
global_name "__STRING__"
global_name "String"
metatable_name "__STRING__"

# ---@field split fun(str: string, sep: string): string[]
def_class_method "split" do |state|
Expand All @@ -15,8 +16,7 @@ class Luajit::Wrappers::String < Luajit::LuaObject
1
end

property str : ::String

def initialize(@str)
def self.setup(state : Luajit::LuaState) : Nil
Luajit.create_lua_object(state, self)
end
end
4 changes: 2 additions & 2 deletions src/luajit/wrappers/string.lua → src/wrappers/string.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---@meta _

---@class (exact) __STRING__
---@class (exact) String
---@field split fun(path: string, sep: string): string[]
__STRING__ = {}
String = {}

0 comments on commit a16fb24

Please sign in to comment.