Skip to content

Commit a16fb24

Browse files
committed
feat: wrappers + api changes .new
1 parent 2039e14 commit a16fb24

File tree

16 files changed

+108
-117
lines changed

16 files changed

+108
-117
lines changed

spec/luajit_spec.cr

Lines changed: 0 additions & 34 deletions
This file was deleted.

spec/luajit_wrappers_nil_spec.cr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "./spec_helper"
2+
require "../src/wrappers/nil"
3+
4+
describe Luajit::Wrappers::NIL do
5+
it ".setup" do
6+
Luajit.run do |state|
7+
Luajit::Wrappers::NIL.setup(state)
8+
9+
state.execute(<<-LUA).ok?.should be_true
10+
local x = NIL
11+
assert(type(x) == 'userdata')
12+
assert(x ~= nil)
13+
assert(tostring(x) == 'NIL')
14+
LUA
15+
end
16+
end
17+
18+
it ".is_nil?" do
19+
Luajit.run do |state|
20+
Luajit::Wrappers::NIL.setup(state)
21+
22+
state.execute("return NIL").ok?.should be_true
23+
Luajit::Wrappers::NIL.is_nil?(state, -1).should be_true
24+
25+
state.execute("return nil").ok?.should be_true
26+
Luajit::Wrappers::NIL.is_nil?(state, -1).should be_false
27+
end
28+
end
29+
end

src/io.cr

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/luajit.cr

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ require "./luajit/*"
77

88
module Luajit
99
# :nodoc:
10-
# Define Wrappers namespace
1110
module Wrappers
1211
end
1312

1413
# Same as `LuaState.create`
15-
def self.new : LuaState
16-
LuaState.create
14+
def self.new(defaults : Bool = false) : LuaState
15+
LuaState.create.tap do |state|
16+
state.open_library(:all) if defaults
17+
end
1718
end
1819

1920
# Same as `.new`, but also opens all Lua libraries
21+
@[Deprecated("Use `.new(defaults: true)` instead")]
2022
def self.new_with_defaults : LuaState
21-
new.tap do |state|
22-
state.open_library(:all)
23-
end
23+
new(defaults: true)
2424
end
2525

2626
# Same as `LuaState.destroy`
@@ -29,8 +29,8 @@ module Luajit
2929
end
3030

3131
# Yields a new `LuaState` and closes it at end of block
32-
def self.run(& : LuaState ->) : Nil
33-
state = new_with_defaults
32+
def self.run(defaults : Bool = true, & : LuaState ->) : Nil
33+
state = new(defaults: defaults)
3434
begin
3535
yield state
3636
ensure
@@ -107,31 +107,4 @@ module Luajit
107107
ud_ptr = state.get_userdata(index, type.metatable)
108108
Box(T).unbox(ud_ptr)
109109
end
110-
111-
# Sets `NIL` as a global value with metatable name *mt_name*
112-
#
113-
# See `.is_global_nil?`
114-
def self.setup_global_nil(state : LuaState, mt_name = "luajit_cr::__NIL__") : Nil
115-
state.new_userdata(0_u64)
116-
state.push({
117-
"__tostring" => Luajit::LuaState::Function.new { |__state|
118-
__state.push("NIL")
119-
1
120-
}
121-
})
122-
state.push_value(-1)
123-
state.set_registry(mt_name)
124-
state.set_metatable(-2)
125-
state.set_global("NIL")
126-
end
127-
128-
# Checks if value at *index* is the global `NIL` value with metatable name *mt_name*
129-
def self.is_global_nil?(state : LuaState, index : Int32, mt_name = "luajit_cr::__NIL__") : Bool
130-
begin
131-
state.check_userdata!(index, mt_name)
132-
true
133-
rescue LuaError
134-
false
135-
end
136-
end
137110
end

src/luajit/wrappers/io.cr

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/luajit/wrappers/io.lua

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/path.cr

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/string.cr

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/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+
metatable_name "__IO__"
6+
7+
def self.setup(state : Luajit::LuaState) : Nil
8+
Luajit.create_lua_object(state, self)
9+
end
10+
end

src/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 = {}

0 commit comments

Comments
 (0)