This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
The-Powder-Toy/src/lua/LuaSmartRef.h
Tamás Bálint Misius fd032eff36
Fix LuaSmartRefs acting on dead Lua states
This fixes problems with degenerate cases such as:

	local button
	assert(coroutine.resume(coroutine.create(function()
		button = Button(10, 10, 20, 20)
	end)))
	button:action(function()
		print("hi")
	end)
2020-08-21 11:16:11 +02:00

27 lines
451 B
C++

#pragma once
#include "LuaCompat.h"
class LuaSmartRef
{
int ref;
lua_State *rootl;
public:
LuaSmartRef(lua_State *l);
~LuaSmartRef();
void Assign(lua_State *l, int index); // Copies the value before getting reference, stack unchanged.
void Clear();
int Push(lua_State *l); // Always pushes exactly one value, possibly nil.
inline operator int() const
{
return ref;
}
inline operator bool() const
{
return ref != LUA_REFNIL;
}
};