#pragma once

#include <EASTL/functional.h>

struct lua_State;

namespace Luax
{
	struct LuaInitFn;
	extern LuaInitFn* gLuaInitFnList;

	using InitCallback = eastl::function<void(lua_State*)>;

	struct LuaInitFn
	{
		LuaInitFn(InitCallback cb) 
			: mCallback(cb)
			, mNext(gLuaInitFnList)
		{
			if (gLuaInitFnList)
			{
				gLuaInitFnList->mPrev = this;
			}
			gLuaInitFnList = this;
		}

		~LuaInitFn()
		{
			if (mPrev && mPrev->mNext == this)
			{
				mPrev->mNext = mNext;
			}
		}

		void operator()(lua_State* L) const
		{
			mCallback(L);
		}

		InitCallback	mCallback;
		LuaInitFn*		mPrev = nullptr;
		LuaInitFn*		mNext = nullptr;
	};
}


#define LUA_INIT_FN(fn) Luax::LuaInitFn fn##_functor(fn)