From 721eaf37603d3e66c274172428fb012950cb78c2 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 2 Feb 2026 03:40:54 -0600 Subject: buffer return prev on :set --- docs/thread/buffer.md | 1 + library/lullaby/thread.lua | 2 +- src/thread.c | 4 +++- tests/units/buffer-swap.lua | 8 ++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/units/buffer-swap.lua diff --git a/docs/thread/buffer.md b/docs/thread/buffer.md index 65af0c0..fbd6678 100644 --- a/docs/thread/buffer.md +++ b/docs/thread/buffer.md @@ -36,6 +36,7 @@ copies the value in the buffer to the current state buffer:set(V) sets the value in the buffer +returns the old value of the buffer ### buffer:mod diff --git a/library/lullaby/thread.lua b/library/lullaby/thread.lua index 43e897d..fc29e90 100644 --- a/library/lullaby/thread.lua +++ b/library/lullaby/thread.lua @@ -65,7 +65,7 @@ function buffer.get(T) end ---sets the value of the buffer ---@param T buffer-table ---@param value any ----@return nil +---@return any prev previous table value function buffer.set(T, value) end ---calls a function with a parameter that is the value of the buffer, return the new value of the buffer diff --git a/src/thread.c b/src/thread.c index 555a62b..1e58aef 100644 --- a/src/thread.c +++ b/src/thread.c @@ -392,12 +392,14 @@ int _buffer_get(lua_State* L){ return 1; } -#warning "how do you handle gc for the new object?" int _buffer_set(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); pthread_mutex_lock(&*buffer->lock); + luaI_deepcopy(buffer->L, L, SKIP_LOCALS | STRIP_GC); lua_settop(buffer->L, 0); + lua_pushvalue(L, -2); luaI_deepcopy(L, buffer->L, SKIP_LOCALS | STRIP_GC); + lua_pop(L, 1); pthread_mutex_unlock(&*buffer->lock); return 1; diff --git a/tests/units/buffer-swap.lua b/tests/units/buffer-swap.lua new file mode 100644 index 0000000..50b5b7c --- /dev/null +++ b/tests/units/buffer-swap.lua @@ -0,0 +1,8 @@ +local a = 539 +local b = "meow meow mrrp!" + +local buffer = llby.thread.buffer(a) + +local c = buffer:set(b) + +return c == a and b == buffer:get() -- cgit v1.2.3