Skip to content

Commit 2039e14

Browse files
committed
feat: wrappers + improvements
1 parent 43338f9 commit 2039e14

File tree

14 files changed

+204
-17
lines changed

14 files changed

+204
-17
lines changed

src/io.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "./luajit/wrappers/io"
2+
alias LuajitIO = Luajit::Wrappers::IO

src/luajit.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ require "./luajit/lua_any"
66
require "./luajit/*"
77

88
module Luajit
9+
# :nodoc:
10+
# Define Wrappers namespace
11+
module Wrappers
12+
end
13+
914
# Same as `LuaState.create`
1015
def self.new : LuaState
1116
LuaState.create

src/luajit/iterators.cr

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ module Luajit
2020
result : TablePair? = nil
2121

2222
while @state.next(@index)
23-
key = case @state.get_type(KEY)
24-
when .string?
25-
@state.to_string(KEY)
26-
when .number?
27-
@state.to_f(KEY)
28-
else
29-
@state.pop(1)
30-
next
31-
end
32-
33-
value = if any_value = @state.to_any?(VALUE)
34-
any_value
35-
else
36-
@state.pop(1)
37-
next
38-
end
23+
key =
24+
case @state.get_type(KEY)
25+
when .string?
26+
@state.to_string(KEY)
27+
when .number?
28+
@state.to_f(KEY)
29+
else
30+
@state.pop(1)
31+
next
32+
end
33+
34+
value =
35+
if any_value = @state.to_any?(VALUE)
36+
any_value
37+
else
38+
@state.pop(1)
39+
next
40+
end
3941

4042
result = {key, value}
4143

src/luajit/lua_any.cr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module Luajit
5252
@raw.pretty_print(pp)
5353
end
5454

55-
def ==(other : JSON::Any)
55+
def ==(other : LuaAny)
5656
raw == other.raw
5757
end
5858

@@ -61,5 +61,22 @@ module Luajit
6161
end
6262

6363
def_hash raw
64+
65+
# Returns a new LuaAny instance with the `raw` value `dup`ed.
66+
def dup
67+
LuaAny.new(raw.dup)
68+
end
69+
70+
# Returns a new LuaAny instance with the `raw` value `clone`ed.
71+
# NOTE: If the `raw` value is a `LuaRef`, it will create a new one.
72+
def clone(state : LuaState)
73+
case value = raw
74+
when LuaRef
75+
state.get_ref_value(value)
76+
LuaAny.new(state.create_ref)
77+
else
78+
LuaAny.new(raw.clone)
79+
end
80+
end
6481
end
6582
end

src/luajit/lua_ref.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
module Luajit
22
record LuaRef, ref : Int32, type : LuaType do
3+
def pcall(state : LuaState, nargs : Int32, nresults : Int32) : LuaStatus
4+
raise LuaError.new("Ref is not a function") unless type.function?
5+
state.get_ref_value(ref)
6+
state.insert(-(nargs + 1)) if nargs > 0
7+
state.pcall(nargs, nresults)
8+
end
9+
310
def inspect(io : IO) : Nil
411
io << "LuaRef(@type=#{type.to_s}, @ref=#{ref})"
512
end

src/luajit/wrappers/io.cr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "../../luajit"
2+
3+
class Luajit::Wrappers::IO < Luajit::LuaObject
4+
global_name "__IO__"
5+
6+
property io : ::IO
7+
8+
def initialize(@io)
9+
end
10+
end

src/luajit/wrappers/io.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---@meta _
2+
3+
---@class (exact) __IO__
4+
__IO__ = {}

src/luajit/wrappers/nil.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---@meta _
2+
3+
---@class (exact) NIL
4+
NIL = {}

src/luajit/wrappers/path.cr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require "../../luajit"
2+
3+
class Luajit::Wrappers::Path < Luajit::LuaObject
4+
global_name "__PATH__"
5+
6+
# ---@field new fun(path: string?): self
7+
def_class_method "new" do |state|
8+
_self = (
9+
if state.is_none?(1)
10+
new(::Path.new)
11+
elsif state.is_string?(1)
12+
new(::Path.new(state.to_string(1)))
13+
else
14+
state.raise_type_error!(1, "expected string or nil")
15+
end
16+
)
17+
Luajit.setup_userdata(state, _self, self)
18+
1
19+
end
20+
21+
# ---@field home fun(): self
22+
def_class_method "home" do |state|
23+
_self = new(::Path.home)
24+
Luajit.setup_userdata(state, _self, self)
25+
1
26+
end
27+
28+
# ---@field is_absolute fun(self): boolean
29+
def_instance_method "is_absolute" do |state|
30+
state.assert_userdata!(1)
31+
_self = Luajit.userdata_value(state, self, 1)
32+
state.push _self.path.absolute?
33+
1
34+
end
35+
36+
# ---@field anchor fun(self): self?
37+
def_instance_method "anchor" do |state|
38+
state.assert_userdata!(1)
39+
_self = Luajit.userdata_value(state, self, 1)
40+
41+
new_path = _self.path.anchor
42+
43+
unless new_path
44+
state.push(nil)
45+
next 1
46+
end
47+
48+
state.get_global("__PATH__")
49+
state.get_field(-1, "new")
50+
state.pcall(0, 1)
51+
__self = Luajit.userdata_value(state, self, -1)
52+
__self.path = new_path
53+
1
54+
end
55+
56+
# ---@operator tostring(self): self
57+
def_instance_method "__tostring" do |state|
58+
state.assert_userdata!(1)
59+
_self = Luajit.userdata_value(state, self, 1)
60+
state.push _self.path.to_s
61+
1
62+
end
63+
64+
property path : ::Path
65+
66+
def initialize(@path)
67+
end
68+
end

src/luajit/wrappers/path.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---@meta _
2+
3+
---@class __PATH__DriveAndRoot
4+
---@field drive string|NIL
5+
---@field root string|NIL
6+
7+
---@class __PATH__ExpandOptions
8+
---@field base __PATH__|string?
9+
---@field home __PATH__|string|boolean?
10+
---@field expand_base boolean?
11+
12+
---@class (exact) __PATH__
13+
---@field new fun(path: string?): self
14+
---@field home fun(): self
15+
---@field is_absolute fun(self): boolean
16+
---@field anchor fun(self): self?
17+
---@field basename fun(self, suffix: string?): string
18+
---@field dirname fun(self): string
19+
---@field drive fun(self): self?
20+
---@field drive_and_root fun(self): __PATH__DriveAndRoot
21+
---@field each_parent fun(self, cb: fun(path: __PATH__))
22+
---@field each_part fun(self, cb: fun(str: string))
23+
---@field ends_with_separator fun(self): boolean
24+
---@field expand fun(self, options: __PATH__ExpandOptions?)
25+
---@field extension fun(self): string
26+
---@field join fun(self, parts: string[]|string): self
27+
---@field normalize fun(self, remove_final_separator: boolean?)
28+
---@field parent fun(self): self
29+
---@field parents fun(self): self[]
30+
---@field parts fun(self): string[]
31+
---@field relative_to fun(self, base: self|string): self
32+
---@field is_relative_to fun(self, base: self|string): self?
33+
---@field root fun(self): self?
34+
---@field sibling fun(self, name: self|string): self
35+
---@field stem fun(self): string
36+
---@operator concat(): string
37+
__PATH__ = {}

0 commit comments

Comments
 (0)