Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
LineLength:
MaxLength: 150
Style/MultilineStringLiteral:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: true
MaxComplexity: 15
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
run: crystal spec

- name: Run linter
run: ./bin/ameba
run: crystal run lib/ameba/bin/ameba.cr
8 changes: 4 additions & 4 deletions examples/callable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -113,16 +113,16 @@ 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")
rose.milk()
brunhilde = europe_farm.new_cow("Brunhilde")
brunhilde.milk()
assert(rose.hungry == brunhilde.hungry, "Do Cows Moo in Different Accents?")
}
)
4 changes: 2 additions & 2 deletions examples/coroutine.cr
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions examples/crystal_callback.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions examples/error_handling.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/eval_function.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions examples/eval_string.cr
Original file line number Diff line number Diff line change
@@ -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!'
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ license: MIT
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: '~> 1.6'
version: '>= 1.7.0-dev'
20 changes: 10 additions & 10 deletions spec/lua/object/coroutine_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions spec/lua/object/function_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions spec/lua/object/table_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"}]
Expand All @@ -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"
Expand Down
26 changes: 13 additions & 13 deletions spec/lua/stack/callable_support_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -64,15 +64,15 @@ 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())
assert(csi2.simple_function() == "Hello from CallableSubClass", "call overriden method wrong res: " .. csi2.simple_function())
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
Expand All @@ -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

Expand Down
Loading
Loading