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
23 changes: 23 additions & 0 deletions spec/lua_state_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,27 @@ describe Luajit::LuaState do
end
end
end

describe "#push_thread" do
it "pushes the current thread" do
Luajit.run do |state|
state.push_thread.should eq(Luajit::LuaState::ThreadStatus::Main)
state.is_thread?(-1).should be_true

SpecHelper.assert_stack_size!(state, 1)
end
end

it "pushes a coroutine" do
Luajit.run do |state|
coroutine = state.new_thread
state.pop(1) # remove thread pushed by `new_thread`

coroutine.push_thread.should eq(Luajit::LuaState::ThreadStatus::Coroutine)
coroutine.is_thread?(-1).should be_true

SpecHelper.assert_stack_size!(coroutine, 1)
end
end
end
end
9 changes: 6 additions & 3 deletions src/luajit/lua_state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,12 @@ module Luajit
LibLuaJIT.lua_pushcclosure(self, proc, 1)
end

# Pushes thread represented by *x* onto the stack, returns `ThreadStatus`
def push_thread(x : LuaState) : ThreadStatus
if LibLuaJIT.lua_pushthread(x) == 1
# Pushes `self` onto its own stack and returns its thread status
#
# `ThreadStatus::Main` is returned when `self` represents the main
# thread, otherwise `ThreadStatus::Coroutine` is returned.
def push_thread : ThreadStatus
if LibLuaJIT.lua_pushthread(self) == 1
ThreadStatus::Main
else
ThreadStatus::Coroutine
Expand Down
Loading