From a9bac39446b28b65191da76626f47e7700685b18 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sun, 9 Aug 2026 10:43:42 +0200 Subject: [PATCH 1/2] fix: bump ameba to 1.7.0-dev for latest Crystal compatibility Ameba 1.6.4 fails to compile with recent Crystal (next_string_array_token was removed from Crystal::Lexer). The 1.7 line drops the postinstall binary in favor of the lib/ameba/bin/ameba.cr entry script, so the CI linter step now compiles and runs it via crystal. --- .github/workflows/ci.yml | 2 +- shard.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae972e9..04cf342 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,4 +31,4 @@ jobs: run: crystal spec - name: Run linter - run: ./bin/ameba + run: crystal run lib/ameba/bin/ameba.cr diff --git a/shard.yml b/shard.yml index 8748d27..cc064c4 100644 --- a/shard.yml +++ b/shard.yml @@ -11,4 +11,4 @@ license: MIT development_dependencies: ameba: github: crystal-ameba/ameba - version: '~> 1.6' + version: '>= 1.7.0-dev' From e88a519c8a0eefc359fee1dd7a3e49d3d9c5b8aa Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sun, 9 Aug 2026 10:43:42 +0200 Subject: [PATCH 2/2] style: fix ameba 1.7 offenses Autocorrected new-rule offenses (percent literal delimiters, macro expression whitespace, be_true/be_false/be_nil spec expectations, redundant self) and disabled Style/MultilineStringLiteral, since %q(...) literals are used intentionally for embedded Lua snippets. --- .ameba.yml | 2 ++ examples/callable.cr | 8 +++---- examples/coroutine.cr | 4 ++-- examples/crystal_callback.cr | 4 ++-- examples/error_handling.cr | 4 ++-- examples/eval_function.cr | 4 ++-- examples/eval_string.cr | 4 ++-- spec/lua/object/coroutine_spec.cr | 20 ++++++++--------- spec/lua/object/function_spec.cr | 18 ++++++++-------- spec/lua/object/table_spec.cr | 20 ++++++++--------- spec/lua/stack/callable_support_spec.cr | 26 +++++++++++------------ spec/lua/stack/chunk_spec.cr | 26 +++++++++++------------ spec/lua/stack/error_handling_spec.cr | 26 +++++++++++------------ spec/lua/stack/standard_libraries_spec.cr | 6 +++--- spec/lua/stack/table_support_spec.cr | 2 +- spec/lua/stack_spec.cr | 24 ++++++++++----------- src/lua/callable.cr | 22 +++++++++---------- src/lua/constants.cr | 2 +- src/lua/stack.cr | 6 +++--- src/lua/stack/chunk.cr | 10 ++++----- src/lua/stack/function_support.cr | 2 +- src/lua/stack/stadard_libraries.cr | 12 +++++------ 22 files changed, 127 insertions(+), 125 deletions(-) diff --git a/.ameba.yml b/.ameba.yml index 43d090c..5d277ad 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -1,5 +1,7 @@ LineLength: MaxLength: 150 +Style/MultilineStringLiteral: + Enabled: false Metrics/CyclomaticComplexity: Enabled: true MaxComplexity: 15 diff --git a/examples/callable.cr b/examples/callable.cr index 8b3c718..67f6b9e 100644 --- a/examples/callable.cr +++ b/examples/callable.cr @@ -92,7 +92,7 @@ end # single farm company stack.set_global("farm", MyLuaModule.new) -stack.run %q{ +stack.run %q( print(farm.hello_farm()) rose = farm.new_cow("Rose") rose.milk() @@ -113,11 +113,11 @@ stack.run %q{ assert(tiger.hungry == 0) assert(rose.hungry == 0) assert(flower.hungry == 0) -} +) # multi farm company stack.set_global("Farm", MyLuaModule) -stack.run %q{ +stack.run %q( europe_farm = Farm.new() us_farm = Farm.new() rose = us_farm.new_cow("Rose") @@ -125,4 +125,4 @@ stack.run %q{ brunhilde = europe_farm.new_cow("Brunhilde") brunhilde.milk() assert(rose.hungry == brunhilde.hungry, "Do Cows Moo in Different Accents?") -} +) diff --git a/examples/coroutine.cr b/examples/coroutine.cr index 3f27ec9..a518f87 100644 --- a/examples/coroutine.cr +++ b/examples/coroutine.cr @@ -1,7 +1,7 @@ require "../src/lua" lua = Lua.load -co = lua.run(%q{ +co = lua.run(%q( function foo (a) print("foo", a) return coroutine.yield(2*a) @@ -15,7 +15,7 @@ co = lua.run(%q{ print("co-body", r, s) return b, "end" end) -}).as(Lua::Coroutine) +)).as(Lua::Coroutine) res = co.resume 1, 10 puts "main #{res}" diff --git a/examples/crystal_callback.cr b/examples/crystal_callback.cr index 3c51867..5a26550 100644 --- a/examples/crystal_callback.cr +++ b/examples/crystal_callback.cr @@ -26,9 +26,9 @@ puts lua.run "return greet('Lua')" # => Hi, Lua lua.function "join", ->(prefix : String, count : Int32, ok : Bool) do "#{prefix}:#{count}:#{ok}" end -puts lua.run %q{ +puts lua.run %q( local label = join("requests", 5, true) return label .. " (" .. tostring(add(10, 20)) .. ")" -} # => requests:5:true (30.0) +) # => requests:5:true (30.0) lua.close diff --git a/examples/error_handling.cr b/examples/error_handling.cr index cc9ce6e..44b4eca 100644 --- a/examples/error_handling.cr +++ b/examples/error_handling.cr @@ -1,13 +1,13 @@ require "../src/lua" begin - Lua.run %q{ + Lua.run %q( function runtime_error(x, y) return x + y end runtime_error("blah", 10) - } + ) rescue e : Lua::RuntimeError puts e.message puts e.traceback diff --git a/examples/eval_function.cr b/examples/eval_function.cr index 802ea0a..bdd2cb8 100644 --- a/examples/eval_function.cr +++ b/examples/eval_function.cr @@ -2,12 +2,12 @@ require "../src/lua" # Evaluates Lua function with Crystal objects as arguments lua = Lua.load -sum = lua.run %q{ +sum = lua.run %q( function sum(x, y) return x + y end return sum -} +) p sum.as(Lua::Function).call(3.2, 1) # => 4.2 lua.close diff --git a/examples/eval_string.cr b/examples/eval_string.cr index 3b5706b..3f49647 100644 --- a/examples/eval_string.cr +++ b/examples/eval_string.cr @@ -1,7 +1,7 @@ require "../src/lua" # Run a chunk of Lua code -Lua.run %q{ +Lua.run %q( local hello_message = table.concat({ 'Hello', 'from', 'Lua!' }, ' ') print(hello_message) -} # => prints 'Hello from Lua!' +) # => prints 'Hello from Lua!' diff --git a/spec/lua/object/coroutine_spec.cr b/spec/lua/object/coroutine_spec.cr index becfdad..a380227 100644 --- a/spec/lua/object/coroutine_spec.cr +++ b/spec/lua/object/coroutine_spec.cr @@ -5,11 +5,11 @@ module Lua describe "#resume" do it "starts and resumes coroutine" do lua = Lua.load - f = lua.run %q{ + f = lua.run %q( return function() coroutine.yield() end - } + ) co = lua.newthread(f.as Lua::Function) co.status.should eq CALL::OK co.resume @@ -21,28 +21,28 @@ module Lua it "can yield arguments" do lua = Lua.load - f = lua.run %q{ + f = lua.run %q( return function(x) return coroutine.yield() end - } + ) co = lua.newthread(f.as Lua::Function) - co.resume.should eq nil + co.resume.should be_nil co.resume(42).should eq 42.0 lua.close end it "resumes coroutine created with coroutine.create" do lua = Lua.load - t = lua.run %q{ + t = lua.run %q( function s(x) return coroutine.yield(x) end return coroutine.create(s) - } + ) co = t.as(Lua::Coroutine) - co.resume.should eq nil + co.resume.should be_nil co.status.should eq CALL::YIELD co.resume("test").should eq "test" co.status.should eq CALL::OK @@ -51,12 +51,12 @@ module Lua it "can return an error" do lua = Lua.load - t = lua.run %q{ + t = lua.run %q( function s(x) return x * 20 end return coroutine.create(s) - } + ) co = t.as(Lua::Coroutine) expect_raises RuntimeError do diff --git a/spec/lua/object/function_spec.cr b/spec/lua/object/function_spec.cr index f80a27c..8e9030c 100644 --- a/spec/lua/object/function_spec.cr +++ b/spec/lua/object/function_spec.cr @@ -4,47 +4,47 @@ module Lua describe Function do describe "#call" do it "make a call to the Lua function" do - sum = Lua::Stack.new.run(%q{ + sum = Lua::Stack.new.run(%q( function sum(x, y) return x + y end return sum - }).as(Lua::Function) + )).as(Lua::Function) sum.call(2, 3).should eq 5 end it "returns nil if function does not have returning" do - sum = Lua::Stack.new.run(%q{ + sum = Lua::Stack.new.run(%q( function sum(x, y) a = x + y end return sum - }).as(Lua::Function) - sum.call(2, 3).should eq nil + )).as(Lua::Function) + sum.call(2, 3).should be_nil end it "can call a function without arguments" do - f = Lua::Stack.new.run(%q{ + f = Lua::Stack.new.run(%q( function ff() return "ff" end return ff - }).as(Lua::Function) + )).as(Lua::Function) f.call.should eq "ff" end end describe "#to_s" do it "returns string representation of the function" do - f = Lua::Stack.new.run(%q{ + f = Lua::Stack.new.run(%q( return function(x, y) return x + y end - }) + )) f.to_s.should eq "argsize:2, function(x, y)..." end end diff --git a/spec/lua/object/table_spec.cr b/spec/lua/object/table_spec.cr index 7af405c..0b8fb76 100644 --- a/spec/lua/object/table_spec.cr +++ b/spec/lua/object/table_spec.cr @@ -19,16 +19,16 @@ module Lua it "converts bool" do t = table [true, false] - t[1].should eq true - t[2].should eq false + t[1].should be_true + t[2].should be_false end it "returns nil if there is no values" do - table([] of String)[1].should eq nil + table([] of String)[1].should be_nil end it "converts" do - table([nil])[1].should eq nil + table([nil])[1].should be_nil end it "converts char" do @@ -47,8 +47,8 @@ module Lua t[1].should eq "one" t[2].should eq "two" t[3].should eq "x" - t[4].should eq true - t[5].should eq false + t[4].should be_true + t[5].should be_false t[6].should eq 3.0 t[7].as(Table)[1].should eq "a" end @@ -57,7 +57,7 @@ module Lua t = table({one: 1, "two": 2, bool: true, char: 'x', tuple: {:one}}) t["one"].should eq 1 t["two"].should eq 2 - t["bool"].should eq true + t["bool"].should be_true t["char"].should eq "x" t["tuple"].as(Table)[1].should eq "one" end @@ -67,7 +67,7 @@ module Lua t[1].should eq 1 t[2].should eq "a" t[3].as(Table).tap do |nested| - nested[1].should eq true + nested[1].should be_true nested[2].as(Table)["key"].should eq "value" end t.select { |_, v| v.is_a?(String) }.should eq [{2, "a"}] @@ -88,14 +88,14 @@ module Lua end it "updates existed value" do - t = table %w(a b) + t = table %w[a b] t[2].should eq "b" t[2] = "c" t[2].should eq "c" end it "sets/updates value if table is removed from the stack" do - t = Stack.new.tap(&.<< %w(a b)).pop.as(Table) + t = Stack.new.tap(&.<< %w[a b]).pop.as(Table) t[1].should eq "a" t[1] = "aabb" t[1].should eq "aabb" diff --git a/spec/lua/stack/callable_support_spec.cr b/spec/lua/stack/callable_support_spec.cr index d2de3c8..a041c80 100644 --- a/spec/lua/stack/callable_support_spec.cr +++ b/spec/lua/stack/callable_support_spec.cr @@ -7,11 +7,11 @@ module Lua::StackMixin s = Stack.new obj = CallableClass.new s.set_global("o", obj) - res = s.run %q{ + res = s.run %q( c = o.w o.w = "Lua" return c - } + ) res.should eq "Crystal" obj.w.should eq "Lua" end @@ -20,13 +20,13 @@ module Lua::StackMixin s = Stack.new obj = CallableClass.new s.set_global("o", obj) - res = s.run %q{ + res = s.run %q( return o.simple_function() - } + ) res.should eq "Hello from CallableClass" - res = s.run %q{ + res = s.run %q( return o.arg_function("Dogs", 4) - } + ) res.should eq "4 Dogs" end @@ -38,20 +38,20 @@ module Lua::StackMixin obj2.w = "Second" s.set_global("o1", obj1) s.set_global("o2", obj2) - res = s.run %q{ + res = s.run %q( return o1.join_other(o2) - } + ) res.should eq "Second after First" end it "create new callable instance from Lua" do s = Stack.new s.set_global("m", CallableClass) - res = s.run %q{ + res = s.run %q( n = m.new() n.w = "Lua" return n - } + ) res.should be_a(Lua::Callable) cc = res.as(Lua::Callable) cc.crystal_type_name.should eq CallableClass.name @@ -64,7 +64,7 @@ module Lua::StackMixin s = Stack.new s.set_global("cs", CallableSubClass) s.set_global("csi", CallableSubClass.new) - s.run %q{ + s.run %q( csi2 = cs.new() assert(csi.subclass_function() == "Hi i'm CallableSubClass. This is my own function.", "call subclass method wrong res: " .. csi.subclass_function()) assert(csi.simple_function() == "Hello from CallableSubClass", "call overriden method wrong res: " .. csi.simple_function()) @@ -72,7 +72,7 @@ module Lua::StackMixin csi.w = "First" csi2.w = "Second" assert(csi.join_other(csi2) == "Second after First", "call inherited method wrong res: " .. csi.join_other(csi2)) - } + ) end end end @@ -90,7 +90,7 @@ module Lua::StackMixin end def join_other(other : CallableClass) : String - "#{other.w} after #{self.w}" + "#{other.w} after #{w}" end end diff --git a/spec/lua/stack/chunk_spec.cr b/spec/lua/stack/chunk_spec.cr index 19da9d6..763f119 100644 --- a/spec/lua/stack/chunk_spec.cr +++ b/spec/lua/stack/chunk_spec.cr @@ -4,43 +4,43 @@ module Lua::StackMixin describe Chunk do describe "#run" do it "evaluates a lua chunk" do - r = Stack.new.run %q{ + r = Stack.new.run %q( runuble = function (x) return x * 2 end return runuble(runuble(2)) - } + ) r.should eq 8 end it "removes a chunk and results from the stack" do s = Stack.new.tap(&.<< "first element") - s.run %q{ return 122 } + s.run %q( return 122 ) s.size.should eq 1 s[1].should eq "first element" end it "returns nil if there are no returnings" do s = Stack.new - s.run("a = 10").should eq nil + s.run("a = 10").should be_nil s.size.should eq 0 end it "can return any of Lua::Type" do s = Stack.new - s.run(%q{ return nil }).should eq nil - s.run(%q{ return "a" }).should eq "a" - s.run(%q{ return false }).should eq false - s.run(%q{ return true }).should eq true - s.run(%q{ return 100 }).should eq 100 - s.run(%q{ a = {}; a[1] = "a"; return a }).as(Lua::Table)[1].should eq "a" - s.run(%q{ + s.run(%q( return nil )).should be_nil + s.run(%q( return "a" )).should eq "a" + s.run(%q( return false )).should be_false + s.run(%q( return true )).should be_true + s.run(%q( return 100 )).should eq 100 + s.run(%q( a = {}; a[1] = "a"; return a )).as(Lua::Table)[1].should eq "a" + s.run(%q( sum = function(x, y) return x + y end return sum - }).should be_a Function + )).should be_a Function s.close end end @@ -54,7 +54,7 @@ module Lua::StackMixin s = Stack.new.tap(&.<< false) s.run File.new("spec/fixtures/sample.lua") s.size.should eq 1 - s[1].should eq false + s[1].should be_false end end end diff --git a/spec/lua/stack/error_handling_spec.cr b/spec/lua/stack/error_handling_spec.cr index 96e69ee..63ee45e 100644 --- a/spec/lua/stack/error_handling_spec.cr +++ b/spec/lua/stack/error_handling_spec.cr @@ -9,21 +9,21 @@ module Lua::StackMixin end it "can catch lua syntax error" do - expect_raises SyntaxError, %q[unexpected symbol near '"a"'] do - Lua.run %q{ + expect_raises SyntaxError, %q(unexpected symbol near '"a"') do + Lua.run %q( "a" * 3 - } + ) end end it "can catch lua stack overflow" do expect_raises RuntimeError, "stack overflow" do - Lua.run %q{ + Lua.run %q( function s() s() end s() - } + ) end end @@ -39,35 +39,35 @@ module Lua::StackMixin it "can give you a lua error message" do stack = Stack.new expect_raises RuntimeError, "attempt to add a 'string' with a 'number'" do - stack.run %q{ + stack.run %q( s = "a" + 1 - } + ) end end it "can give you a lua traceback" do stack = Stack.new expect_raises RuntimeError, "attempt to add a 'string' with a 'number'" do - stack.run %q{ + stack.run %q( s = function() return "a" + 1 end print(s()) - } + ) end.traceback.should match /stack traceback:/ end it "can give you a lua traceback with correct structure" do stack = Stack.new expect_raises RuntimeError, "attempt to add a 'string' with a 'number'" do - stack.run %q{ + stack.run %q( s = function() return "a" + 1 end print(s()) - } + ) end.traceback.tap do |traceback| traceback.should match /in metamethod 'add'/ traceback.should match /in main chunk/ @@ -76,11 +76,11 @@ module Lua::StackMixin it "throws RuntimeError on non-emtpy stack" do stack = Stack.new.tap &.<< 1 - sum = stack.run %q{ + sum = stack.run %q( return function (x, y) return x + y end - } + ) expect_raises RuntimeError, "attempt to add a 'string' with a 'number'" do sum.as(Lua::Function).call("a", 3) end diff --git a/spec/lua/stack/standard_libraries_spec.cr b/spec/lua/stack/standard_libraries_spec.cr index 89b285a..5ae9345 100644 --- a/spec/lua/stack/standard_libraries_spec.cr +++ b/spec/lua/stack/standard_libraries_spec.cr @@ -11,7 +11,7 @@ module Lua::StackMixin describe "#open_libs" do it "accepts an Array of symbols" do stack = Stack.new libs: nil - stack.open_libs %i(base debug) + stack.open_libs %i[base debug] stack.libs.to_a.should eq [:base, :debug] end @@ -34,7 +34,7 @@ module Lua::StackMixin end it "adds a library to the list of libs" do - stack = Stack.new %i(base package) + stack = Stack.new %i[base package] stack.open_libs :string stack.libs.to_a.should eq [:base, :package, :string] end @@ -54,7 +54,7 @@ module Lua::StackMixin end it "can open module twice" do - stack = Stack.new %i(debug debug) + stack = Stack.new %i[debug debug] stack.libs.to_a.should eq [:debug] end end diff --git a/spec/lua/stack/table_support_spec.cr b/spec/lua/stack/table_support_spec.cr index 706a214..9c20244 100644 --- a/spec/lua/stack/table_support_spec.cr +++ b/spec/lua/stack/table_support_spec.cr @@ -7,7 +7,7 @@ module Lua::StackMixin t = Stack.new.tap(&.pushtable([1, true, false, "a"])).pop.as(Lua::Table) t.size.should eq 4 t[1].should eq 1 - t[2].should eq true + t[2].should be_true t[4].should eq "a" end diff --git a/spec/lua/stack_spec.cr b/spec/lua/stack_spec.cr index d0cf358..a3e57cf 100644 --- a/spec/lua/stack_spec.cr +++ b/spec/lua/stack_spec.cr @@ -22,7 +22,7 @@ module Lua describe "#<<" do it "can push nil value" do - Stack.new.tap(&.<< nil)[1].should eq nil + Stack.new.tap(&.<< nil)[1].should be_nil end it "can push int value" do @@ -34,11 +34,11 @@ module Lua end it "can push bool true value" do - Stack.new.tap(&.<< true)[1].should eq true + Stack.new.tap(&.<< true)[1].should be_true end it "can push bool false value" do - Stack.new.tap(&.<< false)[1].should eq false + Stack.new.tap(&.<< false)[1].should be_false end it "can push char value" do @@ -54,8 +54,8 @@ module Lua end it "can push array" do - r = Stack.new.tap(&.<< %w(lua is cool))[1].as(Table).map { |_, v| v } - r.should eq %w(lua is cool) + r = Stack.new.tap(&.<< %w[lua is cool])[1].as(Table).map { |_, v| v } + r.should eq %w[lua is cool] end it "can push hash" do @@ -65,12 +65,12 @@ module Lua it "can push tuple" do r = Stack.new.tap(&.<<({:one, :two, :three}))[1].as(Table).map { |_, v| v } - r.should eq %w(one two three) + r.should eq %w[one two three] end it "can push named tuple" do r = Stack.new.tap(&.<<({one: '1', two: '2', three: '3'}))[1].as(Table).map { |_, v| v.as(String) } - r.sort.should eq %w(1 2 3) + r.sort.should eq %w[1 2 3] end it "can push inner array" do @@ -97,7 +97,7 @@ module Lua end it "returns nil for 0" do - Stack.new[0].should eq nil + Stack.new[0].should be_nil end end @@ -139,13 +139,13 @@ module Lua end it "returns nil when stack is empty" do - Stack.new.pop.should eq nil + Stack.new.pop.should be_nil end end describe "#remove" do it "removes element from the top of the stack and does not return it" do - Stack.new.tap(&.<< 100).remove.should eq nil + Stack.new.tap(&.<< 100).remove.should be_nil end it "can remove n elements" do @@ -153,7 +153,7 @@ module Lua stack << 1 stack << 3 stack << 5 - stack.remove(2).should eq nil + stack.remove(2).should be_nil stack.size.should eq 1 end @@ -162,7 +162,7 @@ module Lua stack << :"1" stack << :"3" stack << :"5" - stack.remove(stack.size).should eq nil + stack.remove(stack.size).should be_nil stack.size.should eq 0 end diff --git a/src/lua/callable.cr b/src/lua/callable.cr index 17f2df4..2113bee 100644 --- a/src/lua/callable.cr +++ b/src/lua/callable.cr @@ -47,7 +47,7 @@ module LuaCallable {% if !@type.abstract? %} def self.__new(state : LibLua::State) : Int32 stack = Lua::Stack.new(state, :all) - instance = {{@type.name}}.allocate + instance = {{ @type.name }}.allocate instance.initialize stack << instance return 1 @@ -83,7 +83,7 @@ module LuaCallable {% end %} {% for k, m in method_map %} {% if !m.name.ends_with?("=") && !m.name.starts_with?("_") %} - if key == "{{m.name}}" + if key == "{{ m.name }}" proc = ->(state : LibLua::State) { stack = Lua::Stack.new(state, :all) {% reverse_args = [] of Arg %} @@ -92,15 +92,15 @@ module LuaCallable {% end %} {% for a, index in reverse_args %} {% if a.restriction.is_a?(Nop) %} - {{a.name}} = stack[-{{index + 1}}] + {{ a.name }} = stack[-{{ index + 1 }}] {% else %} - {{a.name}} = LuaConvert({{a.restriction}}).convert(stack[-{{index + 1}}]) + {{ a.name }} = LuaConvert({{ a.restriction }}).convert(stack[-{{ index + 1 }}]) {% end %} {% end %} {% if m.args.empty? %} - res = self.{{m.name}}() + res = self.{{ m.name }}() {% else %} - res = self.{{m.name}}({{(m.args.map &.name).join(",").id}}) + res = self.{{ m.name }}({{ (m.args.map &.name).join(",").id }}) {% end %} stack << res return 1 @@ -126,19 +126,19 @@ module LuaCallable def _index(key : String) {% for m in @type.instance_vars %} {% if @type.has_method?(m.name) %} #only expose public fields - if key == "{{m.name}}" - return self.{{m.name}} + if key == "{{ m.name }}" + return self.{{ m.name }} end {% end %} {% end %} - self._call(key) + _call(key) end def _newindex(key, val) {% for m in @type.instance_vars %} {% if @type.has_method?(m.name + "=") %} #only expose public fields - if key == "{{m.name}}" - self.{{m.name}} = val.as({{m.type}}) + if key == "{{ m.name }}" + self.{{ m.name }} = val.as({{ m.type }}) end {% end %} {% end %} diff --git a/src/lua/constants.cr b/src/lua/constants.cr index be3ec9f..3ac5e24 100644 --- a/src/lua/constants.cr +++ b/src/lua/constants.cr @@ -1,5 +1,5 @@ module Lua - alias Type = Nil | Bool | Float64 | String | Lua::Object | Lua::Reference + alias Type = Bool | Float64 | String | Lua::Object | Lua::Reference? enum TYPE TNONE = -1 diff --git a/src/lua/stack.cr b/src/lua/stack.cr index 4058783..056151f 100644 --- a/src/lua/stack.cr +++ b/src/lua/stack.cr @@ -44,11 +44,11 @@ module Lua open_libs(libs) if @libs.includes?(:debug) - set_error_handler %q{ + set_error_handler %q( function(e) return { message = e, traceback = debug.traceback() } end - } + ) else set_error_handler "function(e) return { message = e } end" end @@ -113,7 +113,7 @@ module Lua # stack[2] # => "lua" # ``` def [](pos : Int) - return nil if pos == 0 + return if pos == 0 case type_at(pos) when TYPE::TNIL, TYPE::TNONE then nil diff --git a/src/lua/stack/chunk.cr b/src/lua/stack/chunk.cr index ae83ede..948256c 100644 --- a/src/lua/stack/chunk.cr +++ b/src/lua/stack/chunk.cr @@ -14,7 +14,7 @@ module Lua # ``` def run(buff : String, name : String? = nil) call = CALL.new LibLua.l_loadbufferx @state, buff, buff.size, name || buff.strip, nil - raise self.error(call, pop) if call != CALL::OK + raise error(call, pop) if call != CALL::OK call_and_return size end @@ -25,22 +25,22 @@ module Lua # ``` def run(lua_file : File) call = CALL.new LibLua.l_loadfilex @state, lua_file.path, nil - raise self.error(call, pop) if call != CALL::OK + raise error(call, pop) if call != CALL::OK call_and_return size end protected def call_and_return(chunk_pos, *args) # loads handler just below the chunk - error_handler_pos = self.load_error_handler chunk_pos + error_handler_pos = load_error_handler chunk_pos chunk_pos += 1 if error_handler_pos != 0 args.each { |arg| self.<< arg } call = CALL.new LibLua.pcallk(@state, args.size, Lua::MULTRET, error_handler_pos, 0, nil) - raise self.error(call, pop) if call != CALL::OK + raise error(call, pop) if call != CALL::OK pick_results chunk_pos ensure - self.remove if error_handler_pos != 0 # removes the handler + remove if error_handler_pos != 0 # removes the handler end end end diff --git a/src/lua/stack/function_support.cr b/src/lua/stack/function_support.cr index ba8815d..6f5ae2f 100644 --- a/src/lua/stack/function_support.cr +++ b/src/lua/stack/function_support.cr @@ -41,7 +41,7 @@ module Lua {% else %} result = proc.call( {% for type, i in Args.type_vars %} - ::LuaCallable::LuaConvert({{type}}).convert(stack[{{i + 1}}]), + ::LuaCallable::LuaConvert({{ type }}).convert(stack[{{ i + 1 }}]), {% end %} ) {% end %} diff --git a/src/lua/stack/stadard_libraries.cr b/src/lua/stack/stadard_libraries.cr index 323b31a..a4a83a4 100644 --- a/src/lua/stack/stadard_libraries.cr +++ b/src/lua/stack/stadard_libraries.cr @@ -1,6 +1,6 @@ module Lua::StackMixin module StandardLibraries - MODULES = %i( + MODULES = %i[ base package coroutine @@ -11,7 +11,7 @@ module Lua::StackMixin math utf8 debug - ) + ] # Opens Lua standard libraries. # @@ -27,14 +27,14 @@ module Lua::StackMixin end macro open_library(library) - case {{library}} + case {{ library }} {% for name in MODULES %} - when {{name}} - LibLua.l_requiref(@state, {{name}}.to_s, ->(l : LibLua::State) { LibLua.open_{{name.id}}(l) }, 1) + when {{ name }} + LibLua.l_requiref(@state, {{ name }}.to_s, ->(l : LibLua::State) { LibLua.open_{{ name.id }}(l) }, 1) remove # remove the copy of returned module {% end %} else - raise ArgumentError.new "unable to load '#{{{library}}}' lib. Try one of the following: #{MODULES.join(", ")}" + raise ArgumentError.new "unable to load '#{{{ library }}}' lib. Try one of the following: #{MODULES.join(", ")}" end end end