Refactor LuaInstance to prepare coroutine handling

This commit is contained in:
Jérôme Leclercq 2017-06-07 21:18:07 +02:00
parent b7df3bd1c4
commit a8129b218b
34 changed files with 2635 additions and 2594 deletions

View File

@ -19,7 +19,7 @@
namespace Nz
{
class LuaInstance;
class LuaState;
}
namespace Ndk
@ -32,7 +32,7 @@ namespace Ndk
class NDK_API Console : public Nz::Node, public Nz::HandledObject<Console>
{
public:
Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance);
Console(World& world, const Nz::Vector2f& size, Nz::LuaState& state);
Console(const Console& console) = delete;
Console(Console&& console) = default;
~Console() = default;
@ -83,7 +83,7 @@ namespace Ndk
EntityOwner m_input;
EntityOwner m_inputBackground;
Nz::FontRef m_defaultFont;
Nz::LuaInstance& m_instance;
Nz::LuaState& m_state;
Nz::SpriteRef m_historyBackgroundSprite;
Nz::SpriteRef m_inputBackgroundSprite;
Nz::SimpleTextDrawer m_historyDrawer;

View File

@ -24,7 +24,7 @@ namespace Ndk
template<typename T> void BindComponent(const Nz::String& name);
void RegisterClasses(Nz::LuaInstance& instance);
void RegisterClasses(Nz::LuaState& state);
std::unique_ptr<LuaBinding_Base> core;
std::unique_ptr<LuaBinding_Base> math;
@ -40,13 +40,13 @@ namespace Ndk
private:
template<typename T>
static int AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle);
static int AddComponentOfType(Nz::LuaState& lua, EntityHandle& handle);
template<typename T>
static int PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component);
static int PushComponentOfType(Nz::LuaState& lua, BaseComponent& component);
using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&);
using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&);
using AddComponentFunc = int(*)(Nz::LuaState&, EntityHandle&);
using GetComponentFunc = int(*)(Nz::LuaState&, BaseComponent&);
struct ComponentBinding
{
@ -56,7 +56,7 @@ namespace Ndk
Nz::String name;
};
ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 2);
ComponentBinding* QueryComponentIndex(Nz::LuaState& lua, int argIndex = 2);
std::vector<ComponentBinding> m_componentBinding;
std::unordered_map<Nz::String, ComponentIndex> m_componentBindingByName;

View File

@ -33,7 +33,7 @@ namespace Ndk
}
template<typename T>
int LuaBinding::AddComponentOfType(Nz::LuaInstance& lua, EntityHandle& handle)
int LuaBinding::AddComponentOfType(Nz::LuaState& lua, EntityHandle& handle)
{
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");
@ -43,7 +43,7 @@ namespace Ndk
}
template<typename T>
int LuaBinding::PushComponentOfType(Nz::LuaInstance& lua, BaseComponent& component)
int LuaBinding::PushComponentOfType(Nz::LuaState& lua, BaseComponent& component)
{
static_assert(std::is_base_of<BaseComponent, T>::value, "ComponentType must inherit BaseComponent");

View File

@ -21,7 +21,7 @@ namespace Ndk
LuaBinding_Audio(LuaBinding& binding);
~LuaBinding_Audio() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::Music> music;
Nz::LuaClass<Nz::Sound> sound;

View File

@ -30,7 +30,7 @@ namespace Ndk
LuaBinding_Base(LuaBinding& binding);
virtual ~LuaBinding_Base();
virtual void Register(Nz::LuaInstance& instance) = 0;
virtual void Register(Nz::LuaState& state) = 0;
// Implementation lies in the respective .cpp files (still searching for a cleaner way..)
static std::unique_ptr<LuaBinding_Base> BindCore(LuaBinding& binding);

View File

@ -21,7 +21,7 @@ namespace Ndk
LuaBinding_Core(LuaBinding& binding);
~LuaBinding_Core() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::Clock> clock;
Nz::LuaClass<Nz::Directory> directory;

View File

@ -22,7 +22,7 @@ namespace Ndk
LuaBinding_Graphics(LuaBinding& binding);
~LuaBinding_Graphics() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::AbstractViewer> abstractViewer;
Nz::LuaClass<Nz::InstancedRenderableRef> instancedRenderable;

View File

@ -23,7 +23,7 @@ namespace Ndk
LuaBinding_Math(LuaBinding& binding);
~LuaBinding_Math() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::EulerAnglesd> eulerAngles;
Nz::LuaClass<Nz::Matrix4d> matrix4d;

View File

@ -20,7 +20,7 @@ namespace Ndk
LuaBinding_Network(LuaBinding& binding);
~LuaBinding_Network() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::AbstractSocket> abstractSocket;
Nz::LuaClass<Nz::IpAddress> ipAddress;

View File

@ -18,7 +18,7 @@ namespace Ndk
LuaBinding_Renderer(LuaBinding& binding);
~LuaBinding_Renderer() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Nz::TextureRef> texture;
Nz::LuaClass<Nz::TextureLibrary> textureLibrary;

View File

@ -22,7 +22,7 @@ namespace Ndk
LuaBinding_SDK(LuaBinding& binding);
~LuaBinding_SDK() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
Nz::LuaClass<Application*> application;
Nz::LuaClass<EntityHandle> entity;

View File

@ -21,7 +21,7 @@ namespace Ndk
LuaBinding_Utility(LuaBinding& binding);
~LuaBinding_Utility() = default;
void Register(Nz::LuaInstance& instance) override;
void Register(Nz::LuaState& state) override;
// Utility
Nz::LuaClass<Nz::AbstractImageRef> abstractImage;

View File

@ -11,7 +11,7 @@
namespace Nz
{
class LuaInstance;
class LuaState;
}
namespace Ndk
@ -28,7 +28,7 @@ namespace Ndk
static bool Initialize();
static void RegisterClasses(Nz::LuaInstance& instance);
static void RegisterClasses(Nz::LuaState& state);
static void Uninitialize();

View File

@ -4,7 +4,7 @@
#include <NDK/LuaAPI.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <Nazara/Lua/LuaState.hpp>
#include <Nazara/Math/EulerAngles.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
@ -28,103 +28,103 @@
namespace Nz
{
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Color* color, TypeTag<Color>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Color* color, TypeTag<Color>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
color->r = instance.CheckField<UInt8>("r", index);
color->g = instance.CheckField<UInt8>("g", index);
color->b = instance.CheckField<UInt8>("b", index);
color->a = instance.CheckField<UInt8>("a", 255, index);
color->r = state.CheckField<UInt8>("r", index);
color->g = state.CheckField<UInt8>("g", index);
color->b = state.CheckField<UInt8>("b", index);
color->a = state.CheckField<UInt8>("a", 255, index);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, EulerAnglesd* angles, TypeTag<EulerAnglesd>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, EulerAnglesd* angles, TypeTag<EulerAnglesd>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_Table:
angles->Set(instance.CheckField<double>("pitch", index), instance.CheckField<double>("yaw", index), instance.CheckField<double>("roll", index));
angles->Set(state.CheckField<double>("pitch", index), state.CheckField<double>("yaw", index), state.CheckField<double>("roll", index));
return 1;
default:
{
if (instance.IsOfType(index, "EulerAngles"))
angles->Set(*static_cast<EulerAnglesd*>(instance.ToUserdata(index)));
if (state.IsOfType(index, "EulerAngles"))
angles->Set(*static_cast<EulerAnglesd*>(state.ToUserdata(index)));
else
angles->Set(*static_cast<Quaterniond*>(instance.CheckUserdata(index, "Quaternion")));
angles->Set(*static_cast<Quaterniond*>(state.CheckUserdata(index, "Quaternion")));
return 1;
}
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, EulerAnglesf* angles, TypeTag<EulerAnglesf>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, EulerAnglesf* angles, TypeTag<EulerAnglesf>)
{
EulerAnglesd anglesDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &anglesDouble, TypeTag<EulerAnglesd>());
unsigned int ret = LuaImplQueryArg(state, index, &anglesDouble, TypeTag<EulerAnglesd>());
angles->Set(anglesDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, FontRef* fontRef, TypeTag<FontRef>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, FontRef* fontRef, TypeTag<FontRef>)
{
*fontRef = *static_cast<FontRef*>(instance.CheckUserdata(index, "Font"));
*fontRef = *static_cast<FontRef*>(state.CheckUserdata(index, "Font"));
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, FontParams* params, TypeTag<FontParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, FontParams* params, TypeTag<FontParams>)
{
NazaraUnused(params);
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
// Structure is empty for now
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, ImageParams* params, TypeTag<ImageParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, ImageParams* params, TypeTag<ImageParams>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->levelCount = instance.CheckField<Nz::UInt8>("LevelCount");
params->loadFormat = instance.CheckField<Nz::PixelFormatType>("LoadFormat");
params->levelCount = state.CheckField<Nz::UInt8>("LevelCount");
params->loadFormat = state.CheckField<Nz::PixelFormatType>("LoadFormat");
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, IpAddress* address, TypeTag<IpAddress>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, IpAddress* address, TypeTag<IpAddress>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_String:
address->BuildFromAddress(instance.CheckString(index));
address->BuildFromAddress(state.CheckString(index));
return 1;
default:
*address = *static_cast<IpAddress*>(instance.CheckUserdata(index, "IpAddress"));
*address = *static_cast<IpAddress*>(state.CheckUserdata(index, "IpAddress"));
return 1;
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Matrix4d* mat, TypeTag<Matrix4d>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Matrix4d* mat, TypeTag<Matrix4d>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_Table:
{
double values[16];
for (std::size_t i = 0; i < 16; ++i)
{
instance.PushInteger(i + 1);
instance.GetTable();
state.PushInteger(i + 1);
state.GetTable();
values[i] = instance.CheckNumber(-1);
instance.Pop();
values[i] = state.CheckNumber(-1);
state.Pop();
}
mat->Set(values);
@ -133,500 +133,500 @@ namespace Nz
default:
{
if (instance.IsOfType(index, "Matrix4"))
mat->Set(*static_cast<Matrix4d*>(instance.ToUserdata(index)));
if (state.IsOfType(index, "Matrix4"))
mat->Set(*static_cast<Matrix4d*>(state.ToUserdata(index)));
return 1;
}
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Matrix4f* mat, TypeTag<Matrix4f>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Matrix4f* mat, TypeTag<Matrix4f>)
{
Matrix4d matDouble = Matrix4d::Identity();
unsigned int ret = LuaImplQueryArg(instance, index, &matDouble, TypeTag<Matrix4d>());
unsigned int ret = LuaImplQueryArg(state, index, &matDouble, TypeTag<Matrix4d>());
mat->Set(matDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, MeshParams* params, TypeTag<MeshParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, MeshParams* params, TypeTag<MeshParams>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->animated = instance.CheckField<bool>("Animated", params->animated);
params->center = instance.CheckField<bool>("Center", params->center);
params->matrix = instance.CheckField<Matrix4f>("Matrix", params->matrix);
params->optimizeIndexBuffers = instance.CheckField<bool>("OptimizeIndexBuffers", params->optimizeIndexBuffers);
params->texCoordOffset = instance.CheckField<Vector2f>("TexCoordOffset", params->texCoordOffset);
params->texCoordScale = instance.CheckField<Vector2f>("TexCoordScale", params->texCoordScale);
params->animated = state.CheckField<bool>("Animated", params->animated);
params->center = state.CheckField<bool>("Center", params->center);
params->matrix = state.CheckField<Matrix4f>("Matrix", params->matrix);
params->optimizeIndexBuffers = state.CheckField<bool>("OptimizeIndexBuffers", params->optimizeIndexBuffers);
params->texCoordOffset = state.CheckField<Vector2f>("TexCoordOffset", params->texCoordOffset);
params->texCoordScale = state.CheckField<Vector2f>("TexCoordScale", params->texCoordScale);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Quaterniond* quat, TypeTag<Quaterniond>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Quaterniond* quat, TypeTag<Quaterniond>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_Table:
quat->Set(instance.CheckField<double>("w", index), instance.CheckField<double>("x", index), instance.CheckField<double>("y", index), instance.CheckField<double>("z", index));
quat->Set(state.CheckField<double>("w", index), state.CheckField<double>("x", index), state.CheckField<double>("y", index), state.CheckField<double>("z", index));
return 1;
default:
{
if (instance.IsOfType(index, "EulerAngles"))
quat->Set(*static_cast<EulerAnglesd*>(instance.ToUserdata(index)));
if (state.IsOfType(index, "EulerAngles"))
quat->Set(*static_cast<EulerAnglesd*>(state.ToUserdata(index)));
else
quat->Set(*static_cast<Quaterniond*>(instance.CheckUserdata(index, "Quaternion")));
quat->Set(*static_cast<Quaterniond*>(state.CheckUserdata(index, "Quaternion")));
return 1;
}
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Quaternionf* quat, TypeTag<Quaternionf>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Quaternionf* quat, TypeTag<Quaternionf>)
{
Quaterniond quatDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &quatDouble, TypeTag<Quaterniond>());
unsigned int ret = LuaImplQueryArg(state, index, &quatDouble, TypeTag<Quaterniond>());
quat->Set(quatDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Rectd* rect, TypeTag<Rectd>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Rectd* rect, TypeTag<Rectd>)
{
instance.CheckType(index, LuaType_Table);
state.CheckType(index, LuaType_Table);
rect->x = instance.CheckField<double>("x", index);
rect->y = instance.CheckField<double>("y", index);
rect->width = instance.CheckField<double>("width", index);
rect->height = instance.CheckField<double>("height", index);
rect->x = state.CheckField<double>("x", index);
rect->y = state.CheckField<double>("y", index);
rect->width = state.CheckField<double>("width", index);
rect->height = state.CheckField<double>("height", index);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Rectf* rect, TypeTag<Rectf>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Rectf* rect, TypeTag<Rectf>)
{
Rectd rectDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &rectDouble, TypeTag<Rectd>());
unsigned int ret = LuaImplQueryArg(state, index, &rectDouble, TypeTag<Rectd>());
rect->Set(rectDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Recti* rect, TypeTag<Recti>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Recti* rect, TypeTag<Recti>)
{
Rectd rectDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &rectDouble, TypeTag<Rectd>());
unsigned int ret = LuaImplQueryArg(state, index, &rectDouble, TypeTag<Rectd>());
rect->Set(rectDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Rectui* rect, TypeTag<Rectui>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Rectui* rect, TypeTag<Rectui>)
{
Rectd rectDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &rectDouble, TypeTag<Rectd>());
unsigned int ret = LuaImplQueryArg(state, index, &rectDouble, TypeTag<Rectd>());
rect->Set(rectDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector2d* vec, TypeTag<Vector2d>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector2d* vec, TypeTag<Vector2d>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_Number:
if (index < 0 && index > -2)
instance.Error("Vector2 expected, two numbers are required to convert it");
state.Error("Vector2 expected, two numbers are required to convert it");
vec->Set(instance.CheckNumber(index), instance.CheckNumber(index + 1));
vec->Set(state.CheckNumber(index), state.CheckNumber(index + 1));
return 2;
case Nz::LuaType_Table:
vec->Set(instance.CheckField<double>("x", index), instance.CheckField<double>("y", index));
vec->Set(state.CheckField<double>("x", index), state.CheckField<double>("y", index));
return 1;
default:
vec->Set(*static_cast<Vector2d*>(instance.CheckUserdata(index, "Vector2")));
vec->Set(*static_cast<Vector2d*>(state.CheckUserdata(index, "Vector2")));
return 1;
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector2f* vec, TypeTag<Vector2f>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector2f* vec, TypeTag<Vector2f>)
{
Vector2d vecDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &vecDouble, TypeTag<Vector2d>());
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector2d>());
vec->Set(vecDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector2ui* vec, TypeTag<Vector2ui>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector2ui* vec, TypeTag<Vector2ui>)
{
Vector2d vecDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &vecDouble, TypeTag<Vector2d>());
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector2d>());
vec->Set(vecDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector3d* vec, TypeTag<Vector3d>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector3d* vec, TypeTag<Vector3d>)
{
switch (instance.GetType(index))
switch (state.GetType(index))
{
case Nz::LuaType_Number:
if (index < 0 && index > -3)
instance.Error("Vector3 expected, three numbers are required to convert it");
state.Error("Vector3 expected, three numbers are required to convert it");
vec->Set(instance.CheckNumber(index), instance.CheckNumber(index + 1), instance.CheckNumber(index + 2, 0.0));
vec->Set(state.CheckNumber(index), state.CheckNumber(index + 1), state.CheckNumber(index + 2, 0.0));
return 3;
case Nz::LuaType_Table:
vec->Set(instance.CheckField<double>("x", index), instance.CheckField<double>("y", index), instance.CheckField<double>("z", 0.0, index));
vec->Set(state.CheckField<double>("x", index), state.CheckField<double>("y", index), state.CheckField<double>("z", 0.0, index));
return 1;
default:
vec->Set(*static_cast<Vector3d*>(instance.CheckUserdata(index, "Vector3")));
vec->Set(*static_cast<Vector3d*>(state.CheckUserdata(index, "Vector3")));
return 1;
}
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector3f* vec, TypeTag<Vector3f>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector3f* vec, TypeTag<Vector3f>)
{
Vector3d vecDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &vecDouble, TypeTag<Vector3d>());
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector3d>());
vec->Set(vecDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Vector3ui* vec, TypeTag<Vector3ui>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector3ui* vec, TypeTag<Vector3ui>)
{
Vector3d vecDouble;
unsigned int ret = LuaImplQueryArg(instance, index, &vecDouble, TypeTag<Vector3d>());
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector3d>());
vec->Set(vecDouble);
return ret;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Ndk::EntityHandle* handle, TypeTag<Ndk::EntityHandle>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Ndk::EntityHandle* handle, TypeTag<Ndk::EntityHandle>)
{
*handle = *static_cast<Ndk::EntityHandle*>(instance.CheckUserdata(index, "Entity"));
*handle = *static_cast<Ndk::EntityHandle*>(state.CheckUserdata(index, "Entity"));
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Ndk::WorldHandle* handle, TypeTag<Ndk::WorldHandle>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Ndk::WorldHandle* handle, TypeTag<Ndk::WorldHandle>)
{
*handle = *static_cast<Ndk::WorldHandle*>(instance.CheckUserdata(index, "World"));
*handle = *static_cast<Ndk::WorldHandle*>(state.CheckUserdata(index, "World"));
return 1;
}
#ifndef NDK_SERVER
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, InstancedRenderableRef* renderable, TypeTag<InstancedRenderableRef>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, InstancedRenderableRef* renderable, TypeTag<InstancedRenderableRef>)
{
if (instance.IsOfType(index, "InstancedRenderable") ||
instance.IsOfType(index, "Model") ||
instance.IsOfType(index, "Sprite"))
if (state.IsOfType(index, "InstancedRenderable") ||
state.IsOfType(index, "Model") ||
state.IsOfType(index, "Sprite"))
{
*renderable = *static_cast<InstancedRenderableRef*>(instance.ToUserdata(index));
*renderable = *static_cast<InstancedRenderableRef*>(state.ToUserdata(index));
}
else
instance.ArgError(index, "is not a InstancedRenderable instance");
state.ArgError(index, "is not a InstancedRenderable instance");
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, MaterialRef* materialRef, TypeTag<MaterialRef>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, MaterialRef* materialRef, TypeTag<MaterialRef>)
{
*materialRef = *static_cast<MaterialRef*>(instance.CheckUserdata(index, "Material"));
*materialRef = *static_cast<MaterialRef*>(state.CheckUserdata(index, "Material"));
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, MaterialParams* params, TypeTag<MaterialParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, MaterialParams* params, TypeTag<MaterialParams>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->loadAlphaMap = instance.CheckField<bool>("LoadAlphaMap", params->loadAlphaMap);
params->loadDiffuseMap = instance.CheckField<bool>("LoadDiffuseMap", params->loadDiffuseMap);
params->loadEmissiveMap = instance.CheckField<bool>("LoadEmissiveMap", params->loadEmissiveMap);
params->loadHeightMap = instance.CheckField<bool>("LoadHeightMap", params->loadHeightMap);
params->loadNormalMap = instance.CheckField<bool>("LoadNormalMap", params->loadNormalMap);
params->loadSpecularMap = instance.CheckField<bool>("LoadSpecularMap", params->loadSpecularMap);
params->shaderName = instance.CheckField<String>("ShaderName", params->shaderName);
params->loadAlphaMap = state.CheckField<bool>("LoadAlphaMap", params->loadAlphaMap);
params->loadDiffuseMap = state.CheckField<bool>("LoadDiffuseMap", params->loadDiffuseMap);
params->loadEmissiveMap = state.CheckField<bool>("LoadEmissiveMap", params->loadEmissiveMap);
params->loadHeightMap = state.CheckField<bool>("LoadHeightMap", params->loadHeightMap);
params->loadNormalMap = state.CheckField<bool>("LoadNormalMap", params->loadNormalMap);
params->loadSpecularMap = state.CheckField<bool>("LoadSpecularMap", params->loadSpecularMap);
params->shaderName = state.CheckField<String>("ShaderName", params->shaderName);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, ModelParameters* params, TypeTag<ModelParameters>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, ModelParameters* params, TypeTag<ModelParameters>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->loadMaterials = instance.CheckField<bool>("LoadMaterials", params->loadMaterials);
params->loadMaterials = state.CheckField<bool>("LoadMaterials", params->loadMaterials);
LuaImplQueryArg(instance, -1, &params->material, TypeTag<MaterialParams>());
LuaImplQueryArg(instance, -1, &params->mesh, TypeTag<MeshParams>());
LuaImplQueryArg(state, -1, &params->material, TypeTag<MaterialParams>());
LuaImplQueryArg(state, -1, &params->mesh, TypeTag<MeshParams>());
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, MusicParams* params, TypeTag<MusicParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, MusicParams* params, TypeTag<MusicParams>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->forceMono = instance.CheckField<bool>("ForceMono", params->forceMono);
params->forceMono = state.CheckField<bool>("ForceMono", params->forceMono);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, SoundBufferParams* params, TypeTag<SoundBufferParams>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, SoundBufferParams* params, TypeTag<SoundBufferParams>)
{
instance.CheckType(index, Nz::LuaType_Table);
state.CheckType(index, Nz::LuaType_Table);
params->forceMono = instance.CheckField<bool>("ForceMono", params->forceMono);
params->forceMono = state.CheckField<bool>("ForceMono", params->forceMono);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, SpriteRef* spriteRef, TypeTag<SpriteRef>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, SpriteRef* spriteRef, TypeTag<SpriteRef>)
{
*spriteRef = *static_cast<SpriteRef*>(instance.CheckUserdata(index, "Sprite"));
*spriteRef = *static_cast<SpriteRef*>(state.CheckUserdata(index, "Sprite"));
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, TextureRef* textureRef, TypeTag<TextureRef>)
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, TextureRef* textureRef, TypeTag<TextureRef>)
{
*textureRef = *static_cast<TextureRef*>(instance.CheckUserdata(index, "Texture"));
*textureRef = *static_cast<TextureRef*>(state.CheckUserdata(index, "Texture"));
return 1;
}
#endif
inline int LuaImplReplyVal(const LuaInstance& instance, Color&& val, TypeTag<Color>)
inline int LuaImplReplyVal(const LuaState& state, Color&& val, TypeTag<Color>)
{
instance.PushTable();
instance.PushField("r", val.r);
instance.PushField("g", val.g);
instance.PushField("b", val.b);
instance.PushField("a", val.a);
state.PushTable();
state.PushField("r", val.r);
state.PushField("g", val.g);
state.PushField("b", val.b);
state.PushField("a", val.a);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesd&& val, TypeTag<EulerAnglesd>)
inline int LuaImplReplyVal(const LuaState& state, EulerAnglesd&& val, TypeTag<EulerAnglesd>)
{
instance.PushInstance<EulerAnglesd>("EulerAngles", val);
state.PushInstance<EulerAnglesd>("EulerAngles", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, EulerAnglesf&& val, TypeTag<EulerAnglesf>)
inline int LuaImplReplyVal(const LuaState& state, EulerAnglesf&& val, TypeTag<EulerAnglesf>)
{
instance.PushInstance<EulerAnglesd>("EulerAngles", val);
state.PushInstance<EulerAnglesd>("EulerAngles", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, FontRef&& val, TypeTag<FontRef>)
inline int LuaImplReplyVal(const LuaState& state, FontRef&& val, TypeTag<FontRef>)
{
instance.PushInstance<FontRef>("Font", val);
state.PushInstance<FontRef>("Font", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Font::SizeInfo&& val, TypeTag<Font::SizeInfo>)
inline int LuaImplReplyVal(const LuaState& state, Font::SizeInfo&& val, TypeTag<Font::SizeInfo>)
{
instance.PushTable();
instance.PushField("LineHeight", val.lineHeight);
instance.PushField("SpaceAdvance", val.spaceAdvance);
instance.PushField("UnderlinePosition", val.underlinePosition);
instance.PushField("UnderlineThickness", val.underlineThickness);
state.PushTable();
state.PushField("LineHeight", val.lineHeight);
state.PushField("SpaceAdvance", val.spaceAdvance);
state.PushField("UnderlinePosition", val.underlinePosition);
state.PushField("UnderlineThickness", val.underlineThickness);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, ImageParams&& val, TypeTag<ImageParams>)
inline int LuaImplReplyVal(const LuaState& state, ImageParams&& val, TypeTag<ImageParams>)
{
instance.PushTable(0, 2);
instance.PushField("LevelCount", val.levelCount);
instance.PushField("LoadFormat", val.loadFormat);
state.PushTable(0, 2);
state.PushField("LevelCount", val.levelCount);
state.PushField("LoadFormat", val.loadFormat);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, IpAddress&& val, TypeTag<IpAddress>)
inline int LuaImplReplyVal(const LuaState& state, IpAddress&& val, TypeTag<IpAddress>)
{
instance.PushInstance<IpAddress>("IpAddress", val);
state.PushInstance<IpAddress>("IpAddress", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Matrix4d&& val, TypeTag<Matrix4d>)
inline int LuaImplReplyVal(const LuaState& state, Matrix4d&& val, TypeTag<Matrix4d>)
{
instance.PushInstance<Matrix4d>("Matrix4", val);
state.PushInstance<Matrix4d>("Matrix4", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Matrix4f&& val, TypeTag<Matrix4f>)
inline int LuaImplReplyVal(const LuaState& state, Matrix4f&& val, TypeTag<Matrix4f>)
{
instance.PushInstance<Matrix4d>("Matrix4", val);
state.PushInstance<Matrix4d>("Matrix4", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Quaterniond&& val, TypeTag<Quaterniond>)
inline int LuaImplReplyVal(const LuaState& state, Quaterniond&& val, TypeTag<Quaterniond>)
{
instance.PushInstance<Quaterniond>("Quaternion", val);
state.PushInstance<Quaterniond>("Quaternion", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Quaternionf&& val, TypeTag<Quaternionf>)
inline int LuaImplReplyVal(const LuaState& state, Quaternionf&& val, TypeTag<Quaternionf>)
{
instance.PushInstance<Quaterniond>("Quaternion", val);
state.PushInstance<Quaterniond>("Quaternion", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Rectd&& val, TypeTag<Rectd>)
inline int LuaImplReplyVal(const LuaState& state, Rectd&& val, TypeTag<Rectd>)
{
instance.PushInstance<Rectd>("Rect", val);
state.PushInstance<Rectd>("Rect", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Rectf&& val, TypeTag<Rectf>)
inline int LuaImplReplyVal(const LuaState& state, Rectf&& val, TypeTag<Rectf>)
{
instance.PushInstance<Rectd>("Rect", val);
state.PushInstance<Rectd>("Rect", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Recti&& val, TypeTag<Recti>)
inline int LuaImplReplyVal(const LuaState& state, Recti&& val, TypeTag<Recti>)
{
instance.PushInstance<Rectd>("Rect", val);
state.PushInstance<Rectd>("Rect", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Rectui&& val, TypeTag<Rectui>)
inline int LuaImplReplyVal(const LuaState& state, Rectui&& val, TypeTag<Rectui>)
{
instance.PushInstance<Rectd>("Rect", val);
state.PushInstance<Rectd>("Rect", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2d&& val, TypeTag<Vector2d>)
inline int LuaImplReplyVal(const LuaState& state, Vector2d&& val, TypeTag<Vector2d>)
{
instance.PushInstance<Vector2d>("Vector2", val);
state.PushInstance<Vector2d>("Vector2", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2f&& val, TypeTag<Vector2f>)
inline int LuaImplReplyVal(const LuaState& state, Vector2f&& val, TypeTag<Vector2f>)
{
instance.PushInstance<Vector2d>("Vector2", val);
state.PushInstance<Vector2d>("Vector2", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector2ui&& val, TypeTag<Vector2ui>)
inline int LuaImplReplyVal(const LuaState& state, Vector2ui&& val, TypeTag<Vector2ui>)
{
instance.PushInstance<Vector2d>("Vector2", val);
state.PushInstance<Vector2d>("Vector2", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3d&& val, TypeTag<Vector3d>)
inline int LuaImplReplyVal(const LuaState& state, Vector3d&& val, TypeTag<Vector3d>)
{
instance.PushInstance<Vector3d>("Vector3", val);
state.PushInstance<Vector3d>("Vector3", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3f&& val, TypeTag<Vector3f>)
inline int LuaImplReplyVal(const LuaState& state, Vector3f&& val, TypeTag<Vector3f>)
{
instance.PushInstance<Vector3d>("Vector3", val);
state.PushInstance<Vector3d>("Vector3", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Vector3ui&& val, TypeTag<Vector3ui>)
inline int LuaImplReplyVal(const LuaState& state, Vector3ui&& val, TypeTag<Vector3ui>)
{
instance.PushInstance<Vector3d>("Vector3", val);
state.PushInstance<Vector3d>("Vector3", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::Entity* ptr, TypeTag<Ndk::Entity*>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::Entity* ptr, TypeTag<Ndk::Entity*>)
{
instance.PushInstance<Ndk::EntityHandle>("Entity", ptr);
state.PushInstance<Ndk::EntityHandle>("Entity", ptr);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::Application* ptr, TypeTag<Ndk::Application*>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::Application* ptr, TypeTag<Ndk::Application*>)
{
instance.PushInstance<Ndk::Application*>("Application", ptr);
state.PushInstance<Ndk::Application*>("Application", ptr);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::EntityHandle&& handle, TypeTag<Ndk::EntityHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::EntityHandle&& handle, TypeTag<Ndk::EntityHandle>)
{
instance.PushInstance<Ndk::EntityHandle>("Entity", handle);
state.PushInstance<Ndk::EntityHandle>("Entity", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::NodeComponentHandle&& handle, TypeTag<Ndk::NodeComponentHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::NodeComponentHandle&& handle, TypeTag<Ndk::NodeComponentHandle>)
{
instance.PushInstance<Ndk::NodeComponentHandle>("NodeComponent", handle);
state.PushInstance<Ndk::NodeComponentHandle>("NodeComponent", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::VelocityComponentHandle&& handle, TypeTag<Ndk::VelocityComponentHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::VelocityComponentHandle&& handle, TypeTag<Ndk::VelocityComponentHandle>)
{
instance.PushInstance<Ndk::VelocityComponentHandle>("VelocityComponent", handle);
state.PushInstance<Ndk::VelocityComponentHandle>("VelocityComponent", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::World* ptr, TypeTag<Ndk::World*>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::World* ptr, TypeTag<Ndk::World*>)
{
instance.PushInstance<Ndk::WorldHandle>("World", ptr);
state.PushInstance<Ndk::WorldHandle>("World", ptr);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::WorldHandle&& handle, TypeTag<Ndk::WorldHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::WorldHandle&& handle, TypeTag<Ndk::WorldHandle>)
{
instance.PushInstance<Ndk::WorldHandle>("World", handle);
state.PushInstance<Ndk::WorldHandle>("World", handle);
return 1;
}
#ifndef NDK_SERVER
inline int LuaImplReplyVal(const LuaInstance& instance, MaterialRef&& handle, TypeTag<MaterialRef>)
inline int LuaImplReplyVal(const LuaState& state, MaterialRef&& handle, TypeTag<MaterialRef>)
{
instance.PushInstance<MaterialRef>("Material", handle);
state.PushInstance<MaterialRef>("Material", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, const SoundBuffer* val, TypeTag<const SoundBuffer*>)
inline int LuaImplReplyVal(const LuaState& state, const SoundBuffer* val, TypeTag<const SoundBuffer*>)
{
instance.PushInstance<SoundBufferConstRef>("SoundBuffer", val);
state.PushInstance<SoundBufferConstRef>("SoundBuffer", val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, SpriteRef&& handle, TypeTag<SpriteRef>)
inline int LuaImplReplyVal(const LuaState& state, SpriteRef&& handle, TypeTag<SpriteRef>)
{
instance.PushInstance<SpriteRef>("Sprite", handle);
state.PushInstance<SpriteRef>("Sprite", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, TextureRef&& handle, TypeTag<TextureRef>)
inline int LuaImplReplyVal(const LuaState& state, TextureRef&& handle, TypeTag<TextureRef>)
{
instance.PushInstance<TextureRef>("Texture", handle);
state.PushInstance<TextureRef>("Texture", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::CameraComponentHandle&& handle, TypeTag<Ndk::CameraComponentHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::CameraComponentHandle&& handle, TypeTag<Ndk::CameraComponentHandle>)
{
instance.PushInstance<Ndk::CameraComponentHandle>("CameraComponent", handle);
state.PushInstance<Ndk::CameraComponentHandle>("CameraComponent", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::ConsoleHandle&& handle, TypeTag<Ndk::ConsoleHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::ConsoleHandle&& handle, TypeTag<Ndk::ConsoleHandle>)
{
instance.PushInstance<Ndk::ConsoleHandle>("Console", handle);
state.PushInstance<Ndk::ConsoleHandle>("Console", handle);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, Ndk::GraphicsComponentHandle&& handle, TypeTag<Ndk::GraphicsComponentHandle>)
inline int LuaImplReplyVal(const LuaState& state, Ndk::GraphicsComponentHandle&& handle, TypeTag<Ndk::GraphicsComponentHandle>)
{
instance.PushInstance<Ndk::GraphicsComponentHandle>("GraphicsComponent", handle);
state.PushInstance<Ndk::GraphicsComponentHandle>("GraphicsComponent", handle);
return 1;
}
#endif

View File

@ -164,25 +164,25 @@ namespace Ndk
LuaAPI::RegisterClasses(overlay->lua);
// Override "print" function to add a line in the console
overlay->lua.PushFunction([&consoleRef] (Nz::LuaInstance& instance)
overlay->lua.PushFunction([&consoleRef] (Nz::LuaState& state)
{
Nz::StringStream stream;
unsigned int argCount = instance.GetStackTop();
instance.GetGlobal("tostring");
unsigned int argCount = state.GetStackTop();
state.GetGlobal("tostring");
for (unsigned int i = 1; i <= argCount; ++i)
{
instance.PushValue(-1); // tostring function
instance.PushValue(i); // argument
instance.Call(1, 1);
state.PushValue(-1); // tostring function
state.PushValue(i); // argument
state.Call(1, 1);
std::size_t length;
const char* str = instance.CheckString(-1, &length);
const char* str = state.CheckString(-1, &length);
if (i > 1)
stream << '\t';
stream << Nz::String(str, length);
instance.Pop(1);
state.Pop(1);
}
consoleRef.AddLine(stream);

View File

@ -33,10 +33,10 @@ namespace Ndk
* \param instance Lua instance that will interact with the world
*/
Console::Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance) :
Console::Console(World& world, const Nz::Vector2f& size, Nz::LuaState& state) :
m_historyPosition(0),
m_defaultFont(Nz::Font::GetDefault()),
m_instance(instance),
m_state(state),
m_size(size),
m_opened(false),
m_characterSize(24)
@ -310,8 +310,8 @@ namespace Ndk
AddLineInternal(input); //< With the input prefix
if (!m_instance.Execute(inputCmd))
AddLineInternal(m_instance.GetLastError(), Nz::Color::Red);
if (!m_state.Execute(inputCmd))
AddLineInternal(m_state.GetLastError(), Nz::Color::Red);
RefreshHistory();
}

View File

@ -36,31 +36,31 @@ namespace Ndk
* \param instance Lua instance that will interact with the engine & SDK
*/
void LuaBinding::RegisterClasses(Nz::LuaInstance& instance)
void LuaBinding::RegisterClasses(Nz::LuaState& state)
{
core->Register(instance);
math->Register(instance);
network->Register(instance);
sdk->Register(instance);
utility->Register(instance);
core->Register(state);
math->Register(state);
network->Register(state);
sdk->Register(state);
utility->Register(state);
#ifndef NDK_SERVER
audio->Register(instance);
graphics->Register(instance);
renderer->Register(instance);
audio->Register(state);
graphics->Register(state);
renderer->Register(state);
#endif
// ComponentType (fake enumeration to expose component indexes)
instance.PushTable(0, m_componentBinding.size());
state.PushTable(0, m_componentBinding.size());
{
for (const ComponentBinding& entry : m_componentBinding)
{
if (entry.name.IsEmpty())
continue;
instance.PushField(entry.name, entry.index);
state.PushField(entry.name, entry.index);
}
}
instance.SetGlobal("ComponentType");
state.SetGlobal("ComponentType");
}
}

View File

@ -77,7 +77,7 @@ namespace Ndk
music.BindMethod("Stop", &Nz::Music::Stop);
// Manual
music.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Music& instance, std::size_t /*argumentCount*/) -> int
music.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::Music& instance, std::size_t /*argumentCount*/) -> int
{
Nz::StringStream ss("Music(");
ss << instance.GetFilePath() << ')';
@ -104,7 +104,7 @@ namespace Ndk
sound.BindMethod("SetPlayingOffset", &Nz::Sound::SetPlayingOffset);
// Manual
sound.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Sound& instance, std::size_t /*argumentCount*/) -> int
sound.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::Sound& instance, std::size_t /*argumentCount*/) -> int
{
Nz::StringStream ss("Sound(");
if (const Nz::SoundBuffer* buffer = instance.GetBuffer())
@ -120,7 +120,7 @@ namespace Ndk
/*********************************** Nz::SoundBuffer **********************************/
soundBuffer.Reset("SoundBuffer");
{
soundBuffer.SetConstructor([] (Nz::LuaInstance& lua, Nz::SoundBufferRef* instance, std::size_t argumentCount)
soundBuffer.SetConstructor([] (Nz::LuaState& lua, Nz::SoundBufferRef* instance, std::size_t argumentCount)
{
NazaraUnused(lua);
NazaraUnused(argumentCount);
@ -143,7 +143,7 @@ namespace Ndk
soundBuffer.BindStaticMethod("IsFormatSupported", &Nz::SoundBuffer::IsFormatSupported);
// Manual
soundBuffer.BindMethod("Create", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
soundBuffer.BindMethod("Create", [] (Nz::LuaState& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
{
int index = 2;
Nz::AudioFormat format = lua.Check<Nz::AudioFormat>(&index);
@ -158,13 +158,13 @@ namespace Ndk
return 1;
});
soundBuffer.BindMethod("GetSamples", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
soundBuffer.BindMethod("GetSamples", [] (Nz::LuaState& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
{
lua.PushString(reinterpret_cast<const char*>(instance->GetSamples()), instance->GetSampleCount() * sizeof(Nz::Int16));
return 1;
});
soundBuffer.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
soundBuffer.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int
{
Nz::StringStream ss("SoundBuffer(");
if (instance->IsValid())
@ -188,11 +188,11 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Audio classes
*/
void LuaBinding_Audio::Register(Nz::LuaInstance& instance)
void LuaBinding_Audio::Register(Nz::LuaState& state)
{
music.Register(instance);
sound.Register(instance);
soundBuffer.Register(instance);
soundEmitter.Register(instance);
music.Register(state);
sound.Register(state);
soundBuffer.Register(state);
soundEmitter.Register(state);
}
}

View File

@ -32,28 +32,28 @@ namespace Ndk
stream.BindMethod("IsWritable", &Nz::Stream::IsWritable);
stream.BindMethod("SetCursorPos", &Nz::Stream::SetCursorPos);
stream.BindMethod("Read", [] (Nz::LuaInstance& lua, Nz::Stream& instance, std::size_t /*argumentCount*/) -> int {
stream.BindMethod("Read", [] (Nz::LuaState& lua, Nz::Stream& stream, std::size_t /*argumentCount*/) -> int {
int argIndex = 2;
std::size_t length = lua.Check<std::size_t>(&argIndex);
std::unique_ptr<char[]> buffer(new char[length]);
std::size_t readLength = instance.Read(buffer.get(), length);
std::size_t readLength = stream.Read(buffer.get(), length);
lua.PushString(Nz::String(buffer.get(), readLength));
return 1;
});
stream.BindMethod("Write", [] (Nz::LuaInstance& lua, Nz::Stream& instance, std::size_t /*argumentCount*/) -> int {
stream.BindMethod("Write", [] (Nz::LuaState& lua, Nz::Stream& stream, std::size_t /*argumentCount*/) -> int {
int argIndex = 2;
std::size_t bufferSize = 0;
const char* buffer = lua.CheckString(argIndex, &bufferSize);
if (instance.IsTextModeEnabled())
lua.Push(instance.Write(Nz::String(buffer, bufferSize)));
if (stream.IsTextModeEnabled())
lua.Push(stream.Write(Nz::String(buffer, bufferSize)));
else
lua.Push(instance.Write(buffer, bufferSize));
lua.Push(stream.Write(buffer, bufferSize));
return 1;
});
}
@ -61,7 +61,7 @@ namespace Ndk
/*********************************** Nz::Clock **********************************/
clock.Reset("Clock");
{
clock.SetConstructor([] (Nz::LuaInstance& lua, Nz::Clock* instance, std::size_t argumentCount)
clock.SetConstructor([] (Nz::LuaState& lua, Nz::Clock* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 2U);
@ -103,11 +103,11 @@ namespace Ndk
clock.BindMethod("Unpause", &Nz::Clock::Unpause);
// Manual
clock.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Clock& instance, std::size_t /*argumentCount*/) -> int {
clock.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::Clock& clock, std::size_t /*argumentCount*/) -> int {
Nz::StringStream ss("Clock(Elapsed: ");
ss << instance.GetSeconds();
ss << clock.GetSeconds();
ss << "s, Paused: ";
ss << instance.IsPaused();
ss << clock.IsPaused();
ss << ')';
lua.PushString(ss);
@ -118,7 +118,7 @@ namespace Ndk
/********************************* Nz::Directory ********************************/
directory.Reset("Directory");
{
directory.SetConstructor([] (Nz::LuaInstance& lua, Nz::Directory* instance, std::size_t argumentCount)
directory.SetConstructor([] (Nz::LuaState& lua, Nz::Directory* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 1U);
@ -159,9 +159,9 @@ namespace Ndk
directory.BindStaticMethod("SetCurrent", Nz::Directory::SetCurrent);
// Manual
directory.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Directory& instance, std::size_t /*argumentCount*/) -> int {
directory.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::Directory& dir, std::size_t /*argumentCount*/) -> int {
Nz::StringStream ss("Directory(");
ss << instance.GetPath();
ss << dir.GetPath();
ss << ')';
lua.PushString(ss);
@ -174,7 +174,7 @@ namespace Ndk
{
file.Inherit(stream);
file.SetConstructor([] (Nz::LuaInstance& lua, Nz::File* instance, std::size_t argumentCount)
file.SetConstructor([] (Nz::LuaState& lua, Nz::File* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 1U);
@ -237,7 +237,7 @@ namespace Ndk
file.BindStaticMethod("Rename", &Nz::File::Rename);
// Manual
file.BindMethod("Open", [] (Nz::LuaInstance& lua, Nz::File& instance, std::size_t argumentCount) -> int
file.BindMethod("Open", [] (Nz::LuaState& lua, Nz::File& file, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 2U);
@ -246,13 +246,13 @@ namespace Ndk
{
case 0:
case 1:
return lua.Push(instance.Open(lua.Check<Nz::UInt32>(&argIndex, Nz::OpenMode_NotOpen)));
return lua.Push(file.Open(lua.Check<Nz::UInt32>(&argIndex, Nz::OpenMode_NotOpen)));
case 2:
{
Nz::String filePath = lua.Check<Nz::String>(&argIndex);
Nz::UInt32 openMode = lua.Check<Nz::UInt32>(&argIndex, Nz::OpenMode_NotOpen);
return lua.Push(instance.Open(filePath, openMode));
return lua.Push(file.Open(filePath, openMode));
}
}
@ -260,7 +260,7 @@ namespace Ndk
return 0;
});
file.BindMethod("SetCursorPos", [] (Nz::LuaInstance& lua, Nz::File& instance, std::size_t argumentCount) -> int
file.BindMethod("SetCursorPos", [] (Nz::LuaState& lua, Nz::File& file, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 2U);
@ -268,13 +268,13 @@ namespace Ndk
switch (argCount)
{
case 1:
return lua.Push(instance.SetCursorPos(lua.Check<Nz::UInt64>(&argIndex)));
return lua.Push(file.SetCursorPos(lua.Check<Nz::UInt64>(&argIndex)));
case 2:
{
Nz::CursorPosition curPos = lua.Check<Nz::CursorPosition>(&argIndex);
Nz::Int64 offset = lua.Check<Nz::Int64>(&argIndex);
return lua.Push(instance.SetCursorPos(curPos, offset));
return lua.Push(file.SetCursorPos(curPos, offset));
}
}
@ -282,10 +282,10 @@ namespace Ndk
return 0;
});
file.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::File& instance, std::size_t /*argumentCount*/) -> int {
file.BindMethod("__tostring", [] (Nz::LuaState& lua, Nz::File& file, std::size_t /*argumentCount*/) -> int {
Nz::StringStream ss("File(");
if (instance.IsOpen())
ss << "Path: " << instance.GetPath();
if (file.IsOpen())
ss << "Path: " << file.GetPath();
ss << ')';
@ -300,55 +300,55 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Core classes
*/
void LuaBinding_Core::Register(Nz::LuaInstance& instance)
void LuaBinding_Core::Register(Nz::LuaState& state)
{
// Classes
clock.Register(instance);
directory.Register(instance);
file.Register(instance);
stream.Register(instance);
clock.Register(state);
directory.Register(state);
file.Register(state);
stream.Register(state);
// Enums
// Nz::CursorPosition
static_assert(Nz::CursorPosition_Max + 1 == 3, "Nz::CursorPosition has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 3);
state.PushTable(0, 3);
{
instance.PushField("AtBegin", Nz::CursorPosition_AtBegin);
instance.PushField("AtCurrent", Nz::CursorPosition_AtCurrent);
instance.PushField("AtEnd", Nz::CursorPosition_AtEnd);
state.PushField("AtBegin", Nz::CursorPosition_AtBegin);
state.PushField("AtCurrent", Nz::CursorPosition_AtCurrent);
state.PushField("AtEnd", Nz::CursorPosition_AtEnd);
}
instance.SetGlobal("CursorPosition");
state.SetGlobal("CursorPosition");
// Nz::HashType
static_assert(Nz::HashType_Max + 1 == 9, "Nz::HashType has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 9);
state.PushTable(0, 9);
{
instance.PushField("CRC32", Nz::HashType_CRC32);
instance.PushField("Fletcher16", Nz::HashType_Fletcher16);
instance.PushField("MD5", Nz::HashType_MD5);
instance.PushField("SHA1", Nz::HashType_SHA1);
instance.PushField("SHA224", Nz::HashType_SHA224);
instance.PushField("SHA256", Nz::HashType_SHA256);
instance.PushField("SHA384", Nz::HashType_SHA384);
instance.PushField("SHA512", Nz::HashType_SHA512);
instance.PushField("Whirlpool", Nz::HashType_Whirlpool);
state.PushField("CRC32", Nz::HashType_CRC32);
state.PushField("Fletcher16", Nz::HashType_Fletcher16);
state.PushField("MD5", Nz::HashType_MD5);
state.PushField("SHA1", Nz::HashType_SHA1);
state.PushField("SHA224", Nz::HashType_SHA224);
state.PushField("SHA256", Nz::HashType_SHA256);
state.PushField("SHA384", Nz::HashType_SHA384);
state.PushField("SHA512", Nz::HashType_SHA512);
state.PushField("Whirlpool", Nz::HashType_Whirlpool);
}
instance.SetGlobal("HashType");
state.SetGlobal("HashType");
// Nz::OpenMode
static_assert(Nz::OpenMode_Max + 1 == 8, "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding");
instance.PushTable(0, Nz::OpenMode_Max + 1);
state.PushTable(0, Nz::OpenMode_Max + 1);
{
instance.PushField("Append", Nz::OpenMode_Append);
instance.PushField("NotOpen", Nz::OpenMode_NotOpen);
instance.PushField("Lock", Nz::OpenMode_Lock);
instance.PushField("ReadOnly", Nz::OpenMode_ReadOnly);
instance.PushField("ReadWrite", Nz::OpenMode_ReadWrite);
instance.PushField("Text", Nz::OpenMode_Text);
instance.PushField("Truncate", Nz::OpenMode_Truncate);
instance.PushField("WriteOnly", Nz::OpenMode_WriteOnly);
state.PushField("Append", Nz::OpenMode_Append);
state.PushField("NotOpen", Nz::OpenMode_NotOpen);
state.PushField("Lock", Nz::OpenMode_Lock);
state.PushField("ReadOnly", Nz::OpenMode_ReadOnly);
state.PushField("ReadWrite", Nz::OpenMode_ReadWrite);
state.PushField("Text", Nz::OpenMode_Text);
state.PushField("Truncate", Nz::OpenMode_Truncate);
state.PushField("WriteOnly", Nz::OpenMode_WriteOnly);
}
instance.SetGlobal("OpenMode");
state.SetGlobal("OpenMode");
}
}

View File

@ -37,7 +37,7 @@ namespace Ndk
/*********************************** Nz::Material ***********************************/
material.Reset("Material");
{
material.SetConstructor([] (Nz::LuaInstance& lua, Nz::MaterialRef* instance, std::size_t argumentCount)
material.SetConstructor([] (Nz::LuaState& lua, Nz::MaterialRef* instance, std::size_t argumentCount)
{
switch (argumentCount)
{
@ -70,7 +70,7 @@ namespace Ndk
return false;
});
material.BindMethod("Configure", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("Configure", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "MaterialPipeline"))
@ -168,7 +168,7 @@ namespace Ndk
material.BindStaticMethod("GetDefault", &Nz::Material::GetDefault);
material.BindMethod("SetAlphaMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetAlphaMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -180,7 +180,7 @@ namespace Ndk
return lua.Push(instance->SetAlphaMap(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetDiffuseMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetDiffuseMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -192,7 +192,7 @@ namespace Ndk
return lua.Push(instance->SetDiffuseMap(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetEmissiveMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetEmissiveMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -204,7 +204,7 @@ namespace Ndk
return lua.Push(instance->SetEmissiveMap(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetHeightMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetHeightMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -216,7 +216,7 @@ namespace Ndk
return lua.Push(instance->SetHeightMap(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetNormalMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetNormalMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -228,7 +228,7 @@ namespace Ndk
return lua.Push(instance->SetNormalMap(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetShader", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetShader", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "UberShader"))
@ -240,7 +240,7 @@ namespace Ndk
return lua.Push(instance->SetShader(lua.Check<Nz::String>(&argIndex)));
});
material.BindMethod("SetSpecularMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
material.BindMethod("SetSpecularMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Texture"))
@ -261,7 +261,7 @@ namespace Ndk
return reinterpret_cast<Nz::InstancedRenderableRef*>(modelRef); //TODO: Make a ObjectRefCast
});
model.SetConstructor([] (Nz::LuaInstance& /*lua*/, Nz::ModelRef* instance, std::size_t /*argumentCount*/)
model.SetConstructor([] (Nz::LuaState& /*lua*/, Nz::ModelRef* instance, std::size_t /*argumentCount*/)
{
Nz::PlacementNew(instance, Nz::Model::New());
return true;
@ -293,7 +293,7 @@ namespace Ndk
return reinterpret_cast<Nz::InstancedRenderableRef*>(spriteRef); //TODO: Make a ObjectRefCast
});
sprite.SetConstructor([] (Nz::LuaInstance& /*lua*/, Nz::SpriteRef* instance, std::size_t /*argumentCount*/)
sprite.SetConstructor([] (Nz::LuaState& /*lua*/, Nz::SpriteRef* instance, std::size_t /*argumentCount*/)
{
Nz::PlacementNew(instance, Nz::Sprite::New());
return true;
@ -314,7 +314,7 @@ namespace Ndk
sprite.BindMethod("SetTextureCoords", &Nz::Sprite::SetTextureCoords);
sprite.BindMethod("SetTextureRect", &Nz::Sprite::SetTextureRect);
sprite.BindMethod("SetMaterial", [] (Nz::LuaInstance& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
sprite.BindMethod("SetMaterial", [] (Nz::LuaState& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
bool resizeSprite = lua.CheckBoolean(argIndex + 1, true);
@ -327,7 +327,7 @@ namespace Ndk
return 0;
});
sprite.BindMethod("SetTexture", [] (Nz::LuaInstance& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
sprite.BindMethod("SetTexture", [] (Nz::LuaState& lua, Nz::SpriteRef& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
bool resizeSprite = lua.CheckBoolean(argIndex + 1, true);
@ -358,13 +358,13 @@ namespace Ndk
* \param instance Lua instance that will interact with the Graphics classes
*/
void LuaBinding_Graphics::Register(Nz::LuaInstance& instance)
void LuaBinding_Graphics::Register(Nz::LuaState& state)
{
abstractViewer.Register(instance);
instancedRenderable.Register(instance);
material.Register(instance);
model.Register(instance);
sprite.Register(instance);
spriteLibrary.Register(instance);
abstractViewer.Register(state);
instancedRenderable.Register(state);
material.Register(state);
model.Register(state);
sprite.Register(state);
spriteLibrary.Register(state);
}
}

View File

@ -18,7 +18,7 @@ namespace Ndk
/*********************************** Nz::EulerAngles **********************************/
eulerAngles.Reset("EulerAngles");
{
eulerAngles.SetConstructor([] (Nz::LuaInstance& lua, Nz::EulerAnglesd* instance, std::size_t argumentCount)
eulerAngles.SetConstructor([] (Nz::LuaState& lua, Nz::EulerAnglesd* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 3U);
@ -46,7 +46,7 @@ namespace Ndk
eulerAngles.BindMethod("__tostring", &Nz::EulerAnglesd::ToString);
eulerAngles.SetGetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance)
eulerAngles.SetGetter([] (Nz::LuaState& lua, Nz::EulerAnglesd& instance)
{
std::size_t length;
const char* ypr = lua.CheckString(2, &length);
@ -103,7 +103,7 @@ namespace Ndk
return false;
});
eulerAngles.SetSetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance)
eulerAngles.SetSetter([] (Nz::LuaState& lua, Nz::EulerAnglesd& instance)
{
std::size_t length;
const char* ypr = lua.CheckString(2, &length);
@ -165,7 +165,7 @@ namespace Ndk
/*********************************** Nz::Matrix4 **********************************/
matrix4d.Reset("Matrix4");
{
matrix4d.SetConstructor([] (Nz::LuaInstance& lua, Nz::Matrix4d* matrix, std::size_t argumentCount)
matrix4d.SetConstructor([] (Nz::LuaState& lua, Nz::Matrix4d* matrix, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 3U);
@ -207,7 +207,7 @@ namespace Ndk
matrix4d.BindMethod("GetDeterminant", &Nz::Matrix4d::GetDeterminant);
matrix4d.BindMethod("GetDeterminantAffine", &Nz::Matrix4d::GetDeterminantAffine);
matrix4d.BindMethod("GetInverse", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("GetInverse", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
Nz::Matrix4d result;
if (instance.GetInverse(&result))
@ -216,7 +216,7 @@ namespace Ndk
return lua.Push(false);
});
matrix4d.BindMethod("GetInverseAffine", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("GetInverseAffine", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
Nz::Matrix4d result;
if (instance.GetInverseAffine(&result))
@ -232,7 +232,7 @@ namespace Ndk
matrix4d.BindMethod("GetSquaredScale", &Nz::Matrix4d::GetSquaredScale);
matrix4d.BindMethod("GetTranslation", &Nz::Matrix4d::GetTranslation);
matrix4d.BindMethod("GetTransposed", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("GetTransposed", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
Nz::Matrix4d result;
instance.GetTransposed(&result);
@ -243,7 +243,7 @@ namespace Ndk
matrix4d.BindMethod("HasNegativeScale", &Nz::Matrix4d::HasNegativeScale);
matrix4d.BindMethod("HasScale", &Nz::Matrix4d::HasScale);
matrix4d.BindMethod("Inverse", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("Inverse", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
bool succeeded;
instance.Inverse(&succeeded);
@ -251,7 +251,7 @@ namespace Ndk
return lua.Push(succeeded);
});
matrix4d.BindMethod("InverseAffine", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("InverseAffine", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
bool succeeded;
instance.InverseAffine(&succeeded);
@ -273,7 +273,7 @@ namespace Ndk
matrix4d.BindMethod("MakeViewMatrix", &Nz::Matrix4d::MakeViewMatrix);
matrix4d.BindMethod("MakeZero", &Nz::Matrix4d::MakeZero);
matrix4d.BindMethod("Set", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t argumentCount) -> int
matrix4d.BindMethod("Set", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 3U);
@ -305,7 +305,7 @@ namespace Ndk
matrix4d.BindMethod("SetScale", &Nz::Matrix4d::SetScale);
matrix4d.BindMethod("SetTranslation", &Nz::Matrix4d::SetTranslation);
matrix4d.BindMethod("Transform", [] (Nz::LuaInstance& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
matrix4d.BindMethod("Transform", [] (Nz::LuaState& lua, Nz::Matrix4d& instance, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "Vector2"))
@ -345,7 +345,7 @@ namespace Ndk
matrix4d.BindStaticMethod("ViewMatrix", &Nz::Matrix4d::ViewMatrix);
matrix4d.BindStaticMethod("Zero", &Nz::Matrix4d::Zero);
matrix4d.SetGetter([] (Nz::LuaInstance& lua, Nz::Matrix4d& instance)
matrix4d.SetGetter([] (Nz::LuaState& lua, Nz::Matrix4d& instance)
{
bool succeeded = false;
std::size_t index = static_cast<std::size_t>(lua.ToInteger(2, &succeeded));
@ -356,7 +356,7 @@ namespace Ndk
return true;
});
matrix4d.SetSetter([] (Nz::LuaInstance& lua, Nz::Matrix4d& instance)
matrix4d.SetSetter([] (Nz::LuaState& lua, Nz::Matrix4d& instance)
{
bool succeeded = false;
std::size_t index = static_cast<std::size_t>(lua.ToInteger(2, &succeeded));
@ -372,7 +372,7 @@ namespace Ndk
/*********************************** Nz::Rect **********************************/
rect.Reset("Rect");
{
rect.SetConstructor([] (Nz::LuaInstance& lua, Nz::Rectd* instance, std::size_t argumentCount)
rect.SetConstructor([] (Nz::LuaState& lua, Nz::Rectd* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 4U);
@ -422,7 +422,7 @@ namespace Ndk
rect.BindMethod("__tostring", &Nz::Rectd::ToString);
rect.SetGetter([] (Nz::LuaInstance& lua, Nz::Rectd& instance)
rect.SetGetter([] (Nz::LuaState& lua, Nz::Rectd& instance)
{
switch (lua.GetType(2))
{
@ -475,7 +475,7 @@ namespace Ndk
return false;
});
rect.SetSetter([] (Nz::LuaInstance& lua, Nz::Rectd& instance)
rect.SetSetter([] (Nz::LuaState& lua, Nz::Rectd& instance)
{
switch (lua.GetType(2))
{
@ -531,7 +531,7 @@ namespace Ndk
/*********************************** Nz::Quaternion **********************************/
quaternion.Reset("Quaternion");
{
quaternion.SetConstructor([] (Nz::LuaInstance& lua, Nz::Quaterniond* instance, std::size_t argumentCount)
quaternion.SetConstructor([] (Nz::LuaState& lua, Nz::Quaterniond* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 4U);
@ -587,7 +587,7 @@ namespace Ndk
quaternion.BindStaticMethod("RotationBetween", &Nz::Quaterniond::RotationBetween);
quaternion.BindStaticMethod("Slerp", &Nz::Quaterniond::Slerp);
quaternion.BindMethod("GetNormal", [] (Nz::LuaInstance& lua, Nz::Quaterniond& instance, std::size_t /*argumentCount*/) -> int
quaternion.BindMethod("GetNormal", [] (Nz::LuaState& lua, Nz::Quaterniond& instance, std::size_t /*argumentCount*/) -> int
{
double length;
@ -597,7 +597,7 @@ namespace Ndk
return 2;
});
quaternion.BindMethod("Normalize", [] (Nz::LuaInstance& lua, Nz::Quaterniond& instance, std::size_t /*argumentCount*/) -> int
quaternion.BindMethod("Normalize", [] (Nz::LuaState& lua, Nz::Quaterniond& instance, std::size_t /*argumentCount*/) -> int
{
double length;
@ -608,20 +608,20 @@ namespace Ndk
return 2;
});
quaternion.BindStaticMethod("Normalize", [] (Nz::LuaInstance& instance) -> int
quaternion.BindStaticMethod("Normalize", [] (Nz::LuaState& state) -> int
{
int argIndex = 1;
Nz::Quaterniond quat = instance.Check<Nz::Quaterniond>(&argIndex);
Nz::Quaterniond quat = state.Check<Nz::Quaterniond>(&argIndex);
double length;
instance.Push(Nz::Quaterniond::Normalize(quat, &length));
instance.Push(length);
state.Push(Nz::Quaterniond::Normalize(quat, &length));
state.Push(length);
return 2;
});
quaternion.SetGetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance)
quaternion.SetGetter([] (Nz::LuaState& lua, Nz::Quaterniond& instance)
{
std::size_t length;
const char* wxyz = lua.CheckString(2, &length);
@ -651,7 +651,7 @@ namespace Ndk
return false;
});
quaternion.SetSetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance)
quaternion.SetSetter([] (Nz::LuaState& lua, Nz::Quaterniond& instance)
{
std::size_t length;
const char* wxyz = lua.CheckString(2, &length);
@ -690,7 +690,7 @@ namespace Ndk
/*********************************** Nz::Vector2 **********************************/
vector2d.Reset("Vector2");
{
vector2d.SetConstructor([] (Nz::LuaInstance& lua, Nz::Vector2d* vector, std::size_t argumentCount)
vector2d.SetConstructor([] (Nz::LuaState& lua, Nz::Vector2d* vector, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 2U);
@ -720,7 +720,7 @@ namespace Ndk
vector2d.BindMethod("__tostring", &Nz::Vector2d::ToString);
vector2d.SetGetter([] (Nz::LuaInstance& lua, Nz::Vector2d& instance)
vector2d.SetGetter([] (Nz::LuaState& lua, Nz::Vector2d& instance)
{
switch (lua.GetType(2))
{
@ -765,7 +765,7 @@ namespace Ndk
return false;
});
vector2d.SetSetter([] (Nz::LuaInstance& lua, Nz::Vector2d& instance)
vector2d.SetSetter([] (Nz::LuaState& lua, Nz::Vector2d& instance)
{
switch (lua.GetType(2))
{
@ -816,7 +816,7 @@ namespace Ndk
/*********************************** Nz::Vector3 **********************************/
vector3d.Reset("Vector3");
{
vector3d.SetConstructor([] (Nz::LuaInstance& lua, Nz::Vector3d* vector, std::size_t argumentCount)
vector3d.SetConstructor([] (Nz::LuaState& lua, Nz::Vector3d* vector, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 3U);
@ -860,7 +860,7 @@ namespace Ndk
vector3d.BindMethod("__tostring", &Nz::Vector3d::ToString);
vector3d.SetGetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
vector3d.SetGetter([] (Nz::LuaState& lua, Nz::Vector3d& instance)
{
switch (lua.GetType(2))
{
@ -909,7 +909,7 @@ namespace Ndk
return false;
});
vector3d.SetSetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance)
vector3d.SetSetter([] (Nz::LuaState& lua, Nz::Vector3d& instance)
{
switch (lua.GetType(2))
{
@ -967,20 +967,20 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Math classes
*/
void LuaBinding_Math::Register(Nz::LuaInstance& instance)
void LuaBinding_Math::Register(Nz::LuaState& state)
{
eulerAngles.Register(instance);
matrix4d.Register(instance);
quaternion.Register(instance);
rect.Register(instance);
vector2d.Register(instance);
vector3d.Register(instance);
eulerAngles.Register(state);
matrix4d.Register(state);
quaternion.Register(state);
rect.Register(state);
vector2d.Register(state);
vector3d.Register(state);
quaternion.PushGlobalTable(instance);
quaternion.PushGlobalTable(state);
{
instance.PushField("Identity", Nz::Quaterniond::Identity());
instance.PushField("Zero", Nz::Quaterniond::Zero());
state.PushField("Identity", Nz::Quaterniond::Identity());
state.PushField("Zero", Nz::Quaterniond::Zero());
}
instance.Pop();
state.Pop();
}
}

View File

@ -28,7 +28,7 @@ namespace Ndk
/*********************************** Nz::IpAddress **********************************/
ipAddress.Reset("IpAddress");
{
ipAddress.SetConstructor([] (Nz::LuaInstance& lua, Nz::IpAddress* instance, std::size_t argumentCount)
ipAddress.SetConstructor([] (Nz::LuaState& lua, Nz::IpAddress* instance, std::size_t argumentCount)
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 9U);
@ -85,59 +85,59 @@ namespace Ndk
ipAddress.BindMethod("ToUInt32", &Nz::IpAddress::ToUInt32);
ipAddress.BindMethod("__tostring", &Nz::IpAddress::ToString);
ipAddress.BindStaticMethod("ResolveAddress", [] (Nz::LuaInstance& instance) -> int
ipAddress.BindStaticMethod("ResolveAddress", [] (Nz::LuaState& state) -> int
{
Nz::String service;
Nz::ResolveError error = Nz::ResolveError_Unknown;
int argIndex = 2;
Nz::String hostName = Nz::IpAddress::ResolveAddress(instance.Check<Nz::IpAddress>(&argIndex), &service, &error);
Nz::String hostName = Nz::IpAddress::ResolveAddress(state.Check<Nz::IpAddress>(&argIndex), &service, &error);
if (error == Nz::ResolveError_NoError)
{
instance.Push(hostName);
instance.Push(service);
state.Push(hostName);
state.Push(service);
return 2;
}
else
{
instance.PushBoolean(false);
instance.Push(error);
state.PushBoolean(false);
state.Push(error);
return 2;
}
});
ipAddress.BindStaticMethod("ResolveHostname", [] (Nz::LuaInstance& instance) -> int
ipAddress.BindStaticMethod("ResolveHostname", [] (Nz::LuaState& state) -> int
{
Nz::ResolveError error = Nz::ResolveError_Unknown;
int argIndex = 2;
Nz::NetProtocol protocol = instance.Check<Nz::NetProtocol>(&argIndex);
Nz::String hostname = instance.Check<Nz::String>(&argIndex);
Nz::String service = instance.Check<Nz::String>(&argIndex, "http");
Nz::NetProtocol protocol = state.Check<Nz::NetProtocol>(&argIndex);
Nz::String hostname = state.Check<Nz::String>(&argIndex);
Nz::String service = state.Check<Nz::String>(&argIndex, "http");
std::vector<Nz::HostnameInfo> addresses = Nz::IpAddress::ResolveHostname(protocol, hostname, service, &error);
if (error == Nz::ResolveError_NoError)
{
int index = 1;
instance.PushTable(addresses.size());
state.PushTable(addresses.size());
for (Nz::HostnameInfo& info : addresses)
{
instance.PushInteger(index++);
instance.PushTable(0, 4);
instance.PushField("Address", std::move(info.address));
instance.PushField("CanonicalName", std::move(info.canonicalName));
instance.PushField("Protocol", std::move(info.protocol));
instance.PushField("SocketType", std::move(info.socketType));
instance.SetTable();
state.PushInteger(index++);
state.PushTable(0, 4);
state.PushField("Address", std::move(info.address));
state.PushField("CanonicalName", std::move(info.canonicalName));
state.PushField("Protocol", std::move(info.protocol));
state.PushField("SocketType", std::move(info.socketType));
state.SetTable();
}
return 1;
}
else
{
instance.PushBoolean(false);
instance.Push(error);
state.PushBoolean(false);
state.Push(error);
return 2;
}
});
@ -156,22 +156,22 @@ namespace Ndk
udpSocket.BindMethod("IsBroadcastingEnabled", &Nz::UdpSocket::IsBroadcastingEnabled);
udpSocket.BindMethod("QueryMaxDatagramSize", &Nz::UdpSocket::QueryMaxDatagramSize);
udpSocket.BindMethod("Bind", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
udpSocket.BindMethod("Bind", [](Nz::LuaState& lua, Nz::UdpSocket& socket, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
if (lua.IsOfType(argIndex, "IpAddress"))
return lua.Push(instance.Bind(*static_cast<Nz::IpAddress*>(lua.ToUserdata(argIndex))));
return lua.Push(socket.Bind(*static_cast<Nz::IpAddress*>(lua.ToUserdata(argIndex))));
else
return lua.Push(instance.Bind(lua.Check<Nz::UInt16>(&argIndex)));
return lua.Push(socket.Bind(lua.Check<Nz::UInt16>(&argIndex)));
});
udpSocket.BindMethod("Receive", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
udpSocket.BindMethod("Receive", [](Nz::LuaState& lua, Nz::UdpSocket& socket, std::size_t /*argumentCount*/) -> int
{
Nz::IpAddress from;
std::array<char, 0xFFFF> buffer;
std::size_t received;
if (instance.Receive(buffer.data(), buffer.size(), &from, &received))
if (socket.Receive(buffer.data(), buffer.size(), &from, &received))
{
lua.PushBoolean(true);
lua.PushString(from.ToString());
@ -183,7 +183,7 @@ namespace Ndk
return 1;
});
udpSocket.BindMethod("Send", [](Nz::LuaInstance& lua, Nz::UdpSocket& instance, std::size_t /*argumentCount*/) -> int
udpSocket.BindMethod("Send", [](Nz::LuaState& lua, Nz::UdpSocket& socket, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
Nz::String to = lua.Check<Nz::String>(&argIndex);
@ -193,7 +193,7 @@ namespace Ndk
std::size_t sent;
bool ret;
if ((ret = instance.Send(Nz::IpAddress(to), buffer, bufferLength, &sent)) != true)
if ((ret = socket.Send(Nz::IpAddress(to), buffer, bufferLength, &sent)) != true)
sent = 0;
return lua.Push(std::make_pair(ret, sent));
@ -206,108 +206,108 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Network classes
*/
void LuaBinding_Network::Register(Nz::LuaInstance& instance)
void LuaBinding_Network::Register(Nz::LuaState& state)
{
// Classes
abstractSocket.Register(instance);
ipAddress.Register(instance);
udpSocket.Register(instance);
abstractSocket.Register(state);
ipAddress.Register(state);
udpSocket.Register(state);
// Enums
// Nz::NetProtocol
static_assert(Nz::NetProtocol_Max + 1 == 4, "Nz::NetProtocol has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 4);
state.PushTable(0, 4);
{
instance.PushField("Any", Nz::NetProtocol_Any);
instance.PushField("IPv4", Nz::NetProtocol_IPv4);
instance.PushField("IPv6", Nz::NetProtocol_IPv6);
instance.PushField("Unknown", Nz::NetProtocol_Unknown);
state.PushField("Any", Nz::NetProtocol_Any);
state.PushField("IPv4", Nz::NetProtocol_IPv4);
state.PushField("IPv6", Nz::NetProtocol_IPv6);
state.PushField("Unknown", Nz::NetProtocol_Unknown);
}
instance.SetGlobal("NetProtocol");
state.SetGlobal("NetProtocol");
// Nz::PacketPriority
static_assert(Nz::PacketPriority_Max + 1 == 4, "Nz::PacketPriority has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 6);
state.PushTable(0, 6);
{
instance.PushField("High", Nz::PacketPriority_High);
instance.PushField("Highest", Nz::PacketPriority_Highest);
instance.PushField("Immediate", Nz::PacketPriority_Immediate);
instance.PushField("Medium", Nz::PacketPriority_Medium);
instance.PushField("Low", Nz::PacketPriority_Low);
instance.PushField("Lowest", Nz::PacketPriority_Lowest);
state.PushField("High", Nz::PacketPriority_High);
state.PushField("Highest", Nz::PacketPriority_Highest);
state.PushField("Immediate", Nz::PacketPriority_Immediate);
state.PushField("Medium", Nz::PacketPriority_Medium);
state.PushField("Low", Nz::PacketPriority_Low);
state.PushField("Lowest", Nz::PacketPriority_Lowest);
}
instance.SetGlobal("PacketPriority");
state.SetGlobal("PacketPriority");
// Nz::PacketReliability
static_assert(Nz::PacketReliability_Max + 1 == 3, "Nz::PacketReliability has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 3);
state.PushTable(0, 3);
{
instance.PushField("Reliable", Nz::PacketReliability_Reliable);
instance.PushField("ReliableOrdered", Nz::PacketReliability_ReliableOrdered);
instance.PushField("Unreliable", Nz::PacketReliability_Unreliable);
state.PushField("Reliable", Nz::PacketReliability_Reliable);
state.PushField("ReliableOrdered", Nz::PacketReliability_ReliableOrdered);
state.PushField("Unreliable", Nz::PacketReliability_Unreliable);
}
instance.SetGlobal("PacketReliability");
state.SetGlobal("PacketReliability");
// Nz::ResolveError
static_assert(Nz::ResolveError_Max + 1 == 9, "Nz::ResolveError has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 9);
state.PushTable(0, 9);
{
instance.PushField("Internal", Nz::ResolveError_Internal);
instance.PushField("ResourceError", Nz::ResolveError_ResourceError);
instance.PushField("NoError", Nz::ResolveError_NoError);
instance.PushField("NonRecoverable", Nz::ResolveError_NonRecoverable);
instance.PushField("NotFound", Nz::ResolveError_NotFound);
instance.PushField("NotInitialized", Nz::ResolveError_NotInitialized);
instance.PushField("ProtocolNotSupported", Nz::ResolveError_ProtocolNotSupported);
instance.PushField("TemporaryFailure", Nz::ResolveError_TemporaryFailure);
instance.PushField("Unknown", Nz::ResolveError_Unknown);
state.PushField("Internal", Nz::ResolveError_Internal);
state.PushField("ResourceError", Nz::ResolveError_ResourceError);
state.PushField("NoError", Nz::ResolveError_NoError);
state.PushField("NonRecoverable", Nz::ResolveError_NonRecoverable);
state.PushField("NotFound", Nz::ResolveError_NotFound);
state.PushField("NotInitialized", Nz::ResolveError_NotInitialized);
state.PushField("ProtocolNotSupported", Nz::ResolveError_ProtocolNotSupported);
state.PushField("TemporaryFailure", Nz::ResolveError_TemporaryFailure);
state.PushField("Unknown", Nz::ResolveError_Unknown);
}
instance.SetGlobal("ResolveError");
state.SetGlobal("ResolveError");
// Nz::SocketError
static_assert(Nz::SocketError_Max + 1 == 15, "Nz::ResolveError has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 15);
state.PushTable(0, 15);
{
instance.PushField("AddressNotAvailable", Nz::SocketError_AddressNotAvailable);
instance.PushField("ConnectionClosed", Nz::SocketError_ConnectionClosed);
instance.PushField("ConnectionRefused", Nz::SocketError_ConnectionRefused);
instance.PushField("DatagramSize", Nz::SocketError_DatagramSize);
instance.PushField("Internal", Nz::SocketError_Internal);
instance.PushField("Packet", Nz::SocketError_Packet);
instance.PushField("NetworkError", Nz::SocketError_NetworkError);
instance.PushField("NoError", Nz::SocketError_NoError);
instance.PushField("NotInitialized", Nz::SocketError_NotInitialized);
instance.PushField("NotSupported", Nz::SocketError_NotSupported);
instance.PushField("ResolveError", Nz::SocketError_ResolveError);
instance.PushField("ResourceError", Nz::SocketError_ResourceError);
instance.PushField("TimedOut", Nz::SocketError_TimedOut);
instance.PushField("Unknown", Nz::SocketError_Unknown);
instance.PushField("UnreachableHost", Nz::SocketError_UnreachableHost);
state.PushField("AddressNotAvailable", Nz::SocketError_AddressNotAvailable);
state.PushField("ConnectionClosed", Nz::SocketError_ConnectionClosed);
state.PushField("ConnectionRefused", Nz::SocketError_ConnectionRefused);
state.PushField("DatagramSize", Nz::SocketError_DatagramSize);
state.PushField("Internal", Nz::SocketError_Internal);
state.PushField("Packet", Nz::SocketError_Packet);
state.PushField("NetworkError", Nz::SocketError_NetworkError);
state.PushField("NoError", Nz::SocketError_NoError);
state.PushField("NotInitialized", Nz::SocketError_NotInitialized);
state.PushField("NotSupported", Nz::SocketError_NotSupported);
state.PushField("ResolveError", Nz::SocketError_ResolveError);
state.PushField("ResourceError", Nz::SocketError_ResourceError);
state.PushField("TimedOut", Nz::SocketError_TimedOut);
state.PushField("Unknown", Nz::SocketError_Unknown);
state.PushField("UnreachableHost", Nz::SocketError_UnreachableHost);
}
instance.SetGlobal("SocketError");
state.SetGlobal("SocketError");
// Nz::SocketState
static_assert(Nz::SocketState_Max + 1 == 5, "Nz::SocketState has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 5);
state.PushTable(0, 5);
{
instance.PushField("Bound", Nz::SocketState_Bound);
instance.PushField("Connecting", Nz::SocketState_Connecting);
instance.PushField("Connected", Nz::SocketState_Connected);
instance.PushField("NotConnected", Nz::SocketState_NotConnected);
instance.PushField("Resolving", Nz::SocketState_Resolving);
state.PushField("Bound", Nz::SocketState_Bound);
state.PushField("Connecting", Nz::SocketState_Connecting);
state.PushField("Connected", Nz::SocketState_Connected);
state.PushField("NotConnected", Nz::SocketState_NotConnected);
state.PushField("Resolving", Nz::SocketState_Resolving);
}
instance.SetGlobal("SocketState");
state.SetGlobal("SocketState");
// Nz::SocketType
static_assert(Nz::SocketType_Max + 1 == 4, "Nz::SocketState has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 4);
state.PushTable(0, 4);
{
instance.PushField("Raw", Nz::SocketType_Raw);
instance.PushField("TCP", Nz::SocketType_TCP);
instance.PushField("UDP", Nz::SocketType_UDP);
instance.PushField("Unknown", Nz::SocketType_Unknown);
state.PushField("Raw", Nz::SocketType_Raw);
state.PushField("TCP", Nz::SocketType_TCP);
state.PushField("UDP", Nz::SocketType_UDP);
state.PushField("Unknown", Nz::SocketType_Unknown);
}
instance.SetGlobal("SocketType");
state.SetGlobal("SocketType");
}
}

View File

@ -27,7 +27,7 @@ namespace Ndk
return reinterpret_cast<Nz::AbstractImageRef*>(textureRef); //TODO: Make a ObjectRefCast
});
texture.SetConstructor([] (Nz::LuaInstance& /*lua*/, Nz::TextureRef* instance, std::size_t /*argumentCount*/)
texture.SetConstructor([] (Nz::LuaState& /*lua*/, Nz::TextureRef* instance, std::size_t /*argumentCount*/)
{
Nz::PlacementNew(instance, Nz::Texture::New());
return true;
@ -101,10 +101,10 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Renderer classes
*/
void LuaBinding_Renderer::Register(Nz::LuaInstance& instance)
void LuaBinding_Renderer::Register(Nz::LuaState& state)
{
texture.Register(instance);
textureLibrary.Register(instance);
textureManager.Register(instance);
texture.Register(state);
textureLibrary.Register(state);
textureManager.Register(state);
}
}

View File

@ -40,7 +40,7 @@ namespace Ndk
application.BindMethod("IsFPSCounterEnabled", &Application::IsFPSCounterEnabled);
#endif
application.BindMethod("AddWorld", [] (Nz::LuaInstance& lua, Application* instance, std::size_t /*argumentCount*/) -> int
application.BindMethod("AddWorld", [] (Nz::LuaState& lua, Application* instance, std::size_t /*argumentCount*/) -> int
{
lua.Push(instance->AddWorld().CreateHandle());
return 1;
@ -94,23 +94,23 @@ namespace Ndk
entity.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents);
entity.BindMethod("__tostring", &EntityHandle::ToString);
entity.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
entity.BindMethod("AddComponent", [this] (Nz::LuaState& state, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
{
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(instance);
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(state);
return bindingComponent->adder(instance, handle);
return bindingComponent->adder(state, handle);
});
entity.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
entity.BindMethod("GetComponent", [this] (Nz::LuaState& state, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
{
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(instance);
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(state);
return bindingComponent->getter(instance, handle->GetComponent(bindingComponent->index));
return bindingComponent->getter(state, handle->GetComponent(bindingComponent->index));
});
entity.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
entity.BindMethod("RemoveComponent", [this] (Nz::LuaState& state, EntityHandle& handle, std::size_t /*argumentCount*/) -> int
{
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(instance);
LuaBinding::ComponentBinding* bindingComponent = m_binding.QueryComponentIndex(state);
handle->RemoveComponent(bindingComponent->index);
return 0;
@ -129,7 +129,7 @@ namespace Ndk
/*********************************** Ndk::VelocityComponent **********************************/
velocityComponent.Reset("VelocityComponent");
{
velocityComponent.SetGetter([] (Nz::LuaInstance& lua, VelocityComponentHandle& instance)
velocityComponent.SetGetter([] (Nz::LuaState& lua, VelocityComponentHandle& instance)
{
std::size_t length;
const char* member = lua.CheckString(2, &length);
@ -143,7 +143,7 @@ namespace Ndk
return false;
});
velocityComponent.SetSetter([] (Nz::LuaInstance& lua, VelocityComponentHandle& instance)
velocityComponent.SetSetter([] (Nz::LuaState& lua, VelocityComponentHandle& instance)
{
std::size_t length;
const char* member = lua.CheckString(2, &length);
@ -193,7 +193,7 @@ namespace Ndk
/*********************************** Ndk::GraphicsComponent **********************************/
graphicsComponent.Reset("GraphicsComponent");
{
graphicsComponent.BindMethod("Attach", [] (Nz::LuaInstance& lua, Ndk::GraphicsComponent* instance, std::size_t argumentCount) -> int
graphicsComponent.BindMethod("Attach", [] (Nz::LuaState& lua, Ndk::GraphicsComponent* instance, std::size_t argumentCount) -> int
{
/*
void Attach(Nz::InstancedRenderableRef renderable, int renderOrder = 0);
@ -267,19 +267,19 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the SDK classes
*/
void LuaBinding_SDK::Register(Nz::LuaInstance& instance)
void LuaBinding_SDK::Register(Nz::LuaState& state)
{
// Classes
application.Register(instance);
entity.Register(instance);
nodeComponent.Register(instance);
velocityComponent.Register(instance);
world.Register(instance);
application.Register(state);
entity.Register(state);
nodeComponent.Register(state);
velocityComponent.Register(state);
world.Register(state);
#ifndef NDK_SERVER
cameraComponent.Register(instance);
console.Register(instance);
graphicsComponent.Register(instance);
cameraComponent.Register(state);
console.Register(state);
graphicsComponent.Register(state);
#endif
// Enums
@ -292,23 +292,23 @@ namespace Ndk
* \param instance Lua instance that will interact with the component
* \param argIndex Index of the component
*/
LuaBinding::ComponentBinding* LuaBinding::QueryComponentIndex(Nz::LuaInstance& instance, int argIndex)
LuaBinding::ComponentBinding* LuaBinding::QueryComponentIndex(Nz::LuaState& state, int argIndex)
{
switch (instance.GetType(argIndex))
switch (state.GetType(argIndex))
{
case Nz::LuaType_Number:
{
ComponentIndex componentIndex = instance.Check<ComponentIndex>(&argIndex);
ComponentIndex componentIndex = state.Check<ComponentIndex>(&argIndex);
if (componentIndex > m_componentBinding.size())
{
instance.Error("Invalid component index");
state.Error("Invalid component index");
return nullptr;
}
ComponentBinding& binding = m_componentBinding[componentIndex];
if (binding.name.IsEmpty())
{
instance.Error("Invalid component index");
state.Error("Invalid component index");
return nullptr;
}
@ -317,11 +317,11 @@ namespace Ndk
case Nz::LuaType_String:
{
const char* key = instance.CheckString(argIndex);
const char* key = state.CheckString(argIndex);
auto it = m_componentBindingByName.find(key);
if (it == m_componentBindingByName.end())
{
instance.Error("Invalid component name");
state.Error("Invalid component name");
return nullptr;
}
@ -332,7 +332,7 @@ namespace Ndk
break;
}
instance.Error("Invalid component index at #" + Nz::String::Number(argIndex));
state.Error("Invalid component index at #" + Nz::String::Number(argIndex));
return nullptr;
}
}

View File

@ -29,7 +29,7 @@ namespace Ndk
abstractImage.BindMethod("IsCompressed", &Nz::AbstractImage::IsCompressed);
abstractImage.BindMethod("IsCubemap", &Nz::AbstractImage::IsCubemap);
abstractImage.BindMethod("GetMemoryUsage", [] (Nz::LuaInstance& lua, Nz::AbstractImage* instance, std::size_t argumentCount) -> int
abstractImage.BindMethod("GetMemoryUsage", [] (Nz::LuaState& lua, Nz::AbstractImage* instance, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 1U);
switch (argCount)
@ -50,7 +50,7 @@ namespace Ndk
return 0;
});
abstractImage.BindMethod("Update", [] (Nz::LuaInstance& lua, Nz::AbstractImage* instance, std::size_t argumentCount) -> int
abstractImage.BindMethod("Update", [] (Nz::LuaState& lua, Nz::AbstractImage* instance, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 6U);
int argIndex = 2;
@ -100,7 +100,7 @@ namespace Ndk
/*********************************** Nz::Font **********************************/
font.Reset("Font");
{
font.SetConstructor([] (Nz::LuaInstance& /*lua*/, Nz::FontRef* instance, std::size_t /*argumentCount*/)
font.SetConstructor([] (Nz::LuaState& /*lua*/, Nz::FontRef* instance, std::size_t /*argumentCount*/)
{
Nz::PlacementNew(instance, Nz::Font::New());
return true;
@ -112,7 +112,7 @@ namespace Ndk
font.BindMethod("Destroy", &Nz::Font::Destroy);
font.BindMethod("GetCachedGlyphCount", [] (Nz::LuaInstance& lua, Nz::FontRef& instance, std::size_t argumentCount) -> int
font.BindMethod("GetCachedGlyphCount", [] (Nz::LuaState& lua, Nz::FontRef& instance, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 2U);
@ -216,29 +216,29 @@ namespace Ndk
node.BindMethod("SetPosition", (void(Nz::Node::*)(const Nz::Vector3f&, Nz::CoordSys)) &Nz::Node::SetPosition, Nz::CoordSys_Local);
node.BindMethod("SetRotation", (void(Nz::Node::*)(const Nz::Quaternionf&, Nz::CoordSys)) &Nz::Node::SetRotation, Nz::CoordSys_Local);
node.BindMethod("Move", [] (Nz::LuaInstance& lua, Nz::Node& instance, std::size_t /*argumentCount*/) -> int
node.BindMethod("Move", [] (Nz::LuaState& lua, Nz::Node& node, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
Nz::Vector3f offset = lua.Check<Nz::Vector3f>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
instance.Move(offset, coordSys);
node.Move(offset, coordSys);
return 0;
});
node.BindMethod("Rotate", [] (Nz::LuaInstance& lua, Nz::Node& instance, std::size_t /*argumentCount*/) -> int
node.BindMethod("Rotate", [] (Nz::LuaState& lua, Nz::Node& node, std::size_t /*argumentCount*/) -> int
{
int argIndex = 2;
Nz::Quaternionf rotation = lua.Check<Nz::Quaternionf>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
instance.Rotate(rotation, coordSys);
node.Rotate(rotation, coordSys);
return 0;
});
node.BindMethod("Scale", [] (Nz::LuaInstance& lua, Nz::Node& instance, std::size_t argumentCount) -> int
node.BindMethod("Scale", [] (Nz::LuaState& lua, Nz::Node& node, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 4U);
@ -248,15 +248,15 @@ namespace Ndk
case 1:
{
if (lua.IsOfType(argIndex, Nz::LuaType_Number))
instance.Scale(lua.Check<float>(&argIndex));
node.Scale(lua.Check<float>(&argIndex));
else
instance.Scale(lua.Check<Nz::Vector3f>(&argIndex));
node.Scale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
case 3:
instance.Scale(lua.Check<Nz::Vector3f>(&argIndex));
node.Scale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
@ -264,7 +264,7 @@ namespace Ndk
return 0;
});
node.BindMethod("SetScale", [] (Nz::LuaInstance& lua, Nz::Node& instance, std::size_t argumentCount) -> int
node.BindMethod("SetScale", [] (Nz::LuaState& lua, Nz::Node& node, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 4U);
@ -278,10 +278,10 @@ namespace Ndk
{
float scale = lua.Check<float>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
instance.SetScale(scale, coordSys);
node.SetScale(scale, coordSys);
}
else
instance.SetScale(lua.Check<Nz::Vector3f>(&argIndex));
node.SetScale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
@ -292,7 +292,7 @@ namespace Ndk
Nz::Vector3f scale = lua.Check<Nz::Vector3f>(&argIndex);
Nz::CoordSys coordSys = lua.Check<Nz::CoordSys>(&argIndex, Nz::CoordSys_Local);
instance.SetScale(scale, coordSys);
node.SetScale(scale, coordSys);
return 0;
}
}
@ -301,7 +301,7 @@ namespace Ndk
return 0;
});
node.BindMethod("SetInitialScale", [] (Nz::LuaInstance& lua, Nz::Node& instance, std::size_t argumentCount) -> int
node.BindMethod("SetInitialScale", [] (Nz::LuaState& lua, Nz::Node& node, std::size_t argumentCount) -> int
{
std::size_t argCount = std::min<std::size_t>(argumentCount, 4U);
@ -311,16 +311,16 @@ namespace Ndk
case 1:
{
if (lua.IsOfType(argIndex, Nz::LuaType_Number))
instance.SetInitialScale(lua.Check<float>(&argIndex));
node.SetInitialScale(lua.Check<float>(&argIndex));
else
instance.SetInitialScale(lua.Check<Nz::Vector2f>(&argIndex));
node.SetInitialScale(lua.Check<Nz::Vector2f>(&argIndex));
return 0;
}
case 2:
case 3:
instance.SetInitialScale(lua.Check<Nz::Vector3f>(&argIndex));
node.SetInitialScale(lua.Check<Nz::Vector3f>(&argIndex));
return 0;
}
@ -335,112 +335,110 @@ namespace Ndk
*
* \param instance Lua instance that will interact with the Utility classes
*/
void LuaBinding_Utility::Register(Nz::LuaInstance& instance)
void LuaBinding_Utility::Register(Nz::LuaState& state)
{
abstractImage.Register(instance);
font.Register(instance);
keyboard.Register(instance);
node.Register(instance);
abstractImage.Register(state);
font.Register(state);
keyboard.Register(state);
node.Register(state);
keyboard.PushGlobalTable(instance);
keyboard.PushGlobalTable(state);
{
static_assert(Nz::Keyboard::Count == 121, "Nz::Keyboard::Key has been updated but change was not reflected to Lua binding");
instance.PushField("Undefined", Nz::Keyboard::Undefined);
state.PushField("Undefined", Nz::Keyboard::Undefined);
// A-Z
for (std::size_t i = 0; i < 26; ++i)
instance.PushField(Nz::String('A' + char(i)), Nz::Keyboard::A + i);
state.PushField(Nz::String('A' + char(i)), Nz::Keyboard::A + i);
// Numerical
for (std::size_t i = 0; i < 10; ++i)
{
instance.PushField("Num" + Nz::String::Number(i), Nz::Keyboard::Num0 + i);
instance.PushField("Numpad" + Nz::String::Number(i), Nz::Keyboard::Numpad0 + i);
state.PushField("Num" + Nz::String::Number(i), Nz::Keyboard::Num0 + i);
state.PushField("Numpad" + Nz::String::Number(i), Nz::Keyboard::Numpad0 + i);
}
// F1-F15
for (std::size_t i = 0; i < 15; ++i)
instance.PushField('F' + Nz::String::Number(i+1), Nz::Keyboard::F1 + i);
state.PushField('F' + Nz::String::Number(i+1), Nz::Keyboard::F1 + i);
// And all the others...
instance.PushField("Down", Nz::Keyboard::Down);
instance.PushField("Left", Nz::Keyboard::Left);
instance.PushField("Right", Nz::Keyboard::Right);
instance.PushField("Up", Nz::Keyboard::Up);
state.PushField("Down", Nz::Keyboard::Down);
state.PushField("Left", Nz::Keyboard::Left);
state.PushField("Right", Nz::Keyboard::Right);
state.PushField("Up", Nz::Keyboard::Up);
instance.PushField("Add", Nz::Keyboard::Add);
instance.PushField("Decimal", Nz::Keyboard::Decimal);
instance.PushField("Divide", Nz::Keyboard::Divide);
instance.PushField("Multiply", Nz::Keyboard::Multiply);
instance.PushField("Subtract", Nz::Keyboard::Subtract);
state.PushField("Add", Nz::Keyboard::Add);
state.PushField("Decimal", Nz::Keyboard::Decimal);
state.PushField("Divide", Nz::Keyboard::Divide);
state.PushField("Multiply", Nz::Keyboard::Multiply);
state.PushField("Subtract", Nz::Keyboard::Subtract);
instance.PushField("Backslash", Nz::Keyboard::Backslash);
instance.PushField("Backspace", Nz::Keyboard::Backspace);
instance.PushField("Clear", Nz::Keyboard::Clear);
instance.PushField("Comma", Nz::Keyboard::Comma);
instance.PushField("Dash", Nz::Keyboard::Dash);
instance.PushField("Delete", Nz::Keyboard::Delete);
instance.PushField("End", Nz::Keyboard::End);
instance.PushField("Equal", Nz::Keyboard::Equal);
instance.PushField("Escape", Nz::Keyboard::Escape);
instance.PushField("Home", Nz::Keyboard::Home);
instance.PushField("Insert", Nz::Keyboard::Insert);
instance.PushField("LAlt", Nz::Keyboard::LAlt);
instance.PushField("LBracket", Nz::Keyboard::LBracket);
instance.PushField("LControl", Nz::Keyboard::LControl);
instance.PushField("LShift", Nz::Keyboard::LShift);
instance.PushField("LSystem", Nz::Keyboard::LSystem);
instance.PushField("PageDown", Nz::Keyboard::PageDown);
instance.PushField("PageUp", Nz::Keyboard::PageUp);
instance.PushField("Pause", Nz::Keyboard::Pause);
instance.PushField("Period", Nz::Keyboard::Period);
instance.PushField("Print", Nz::Keyboard::Print);
instance.PushField("PrintScreen", Nz::Keyboard::PrintScreen);
instance.PushField("Quote", Nz::Keyboard::Quote);
instance.PushField("RAlt", Nz::Keyboard::RAlt);
instance.PushField("RBracket", Nz::Keyboard::RBracket);
instance.PushField("RControl", Nz::Keyboard::RControl);
instance.PushField("Return", Nz::Keyboard::Return);
instance.PushField("RShift", Nz::Keyboard::RShift);
instance.PushField("RSystem", Nz::Keyboard::RSystem);
instance.PushField("Semicolon", Nz::Keyboard::Semicolon);
instance.PushField("Slash", Nz::Keyboard::Slash);
instance.PushField("Space", Nz::Keyboard::Space);
instance.PushField("Tab", Nz::Keyboard::Tab);
instance.PushField("Tilde", Nz::Keyboard::Tilde);
instance.PushField("Browser_Back", Nz::Keyboard::Browser_Back);
instance.PushField("Browser_Favorites", Nz::Keyboard::Browser_Favorites);
instance.PushField("Browser_Forward", Nz::Keyboard::Browser_Forward);
instance.PushField("Browser_Home", Nz::Keyboard::Browser_Home);
instance.PushField("Browser_Refresh", Nz::Keyboard::Browser_Refresh);
instance.PushField("Browser_Search", Nz::Keyboard::Browser_Search);
instance.PushField("Browser_Stop", Nz::Keyboard::Browser_Stop);
instance.PushField("Media_Next", Nz::Keyboard::Media_Next);
instance.PushField("Media_Play", Nz::Keyboard::Media_Play);
instance.PushField("Media_Previous", Nz::Keyboard::Media_Previous);
instance.PushField("Media_Stop", Nz::Keyboard::Media_Stop);
instance.PushField("Volume_Down", Nz::Keyboard::Volume_Down);
instance.PushField("Volume_Mute", Nz::Keyboard::Volume_Mute);
instance.PushField("Volume_Up", Nz::Keyboard::Volume_Up);
instance.PushField("CapsLock", Nz::Keyboard::CapsLock);
instance.PushField("NumLock", Nz::Keyboard::NumLock);
instance.PushField("ScrollLock", Nz::Keyboard::ScrollLock);
state.PushField("Backslash", Nz::Keyboard::Backslash);
state.PushField("Backspace", Nz::Keyboard::Backspace);
state.PushField("Clear", Nz::Keyboard::Clear);
state.PushField("Comma", Nz::Keyboard::Comma);
state.PushField("Dash", Nz::Keyboard::Dash);
state.PushField("Delete", Nz::Keyboard::Delete);
state.PushField("End", Nz::Keyboard::End);
state.PushField("Equal", Nz::Keyboard::Equal);
state.PushField("Escape", Nz::Keyboard::Escape);
state.PushField("Home", Nz::Keyboard::Home);
state.PushField("Insert", Nz::Keyboard::Insert);
state.PushField("LAlt", Nz::Keyboard::LAlt);
state.PushField("LBracket", Nz::Keyboard::LBracket);
state.PushField("LControl", Nz::Keyboard::LControl);
state.PushField("LShift", Nz::Keyboard::LShift);
state.PushField("LSystem", Nz::Keyboard::LSystem);
state.PushField("PageDown", Nz::Keyboard::PageDown);
state.PushField("PageUp", Nz::Keyboard::PageUp);
state.PushField("Pause", Nz::Keyboard::Pause);
state.PushField("Period", Nz::Keyboard::Period);
state.PushField("Print", Nz::Keyboard::Print);
state.PushField("PrintScreen", Nz::Keyboard::PrintScreen);
state.PushField("Quote", Nz::Keyboard::Quote);
state.PushField("RAlt", Nz::Keyboard::RAlt);
state.PushField("RBracket", Nz::Keyboard::RBracket);
state.PushField("RControl", Nz::Keyboard::RControl);
state.PushField("Return", Nz::Keyboard::Return);
state.PushField("RShift", Nz::Keyboard::RShift);
state.PushField("RSystem", Nz::Keyboard::RSystem);
state.PushField("Semicolon", Nz::Keyboard::Semicolon);
state.PushField("Slash", Nz::Keyboard::Slash);
state.PushField("Space", Nz::Keyboard::Space);
state.PushField("Tab", Nz::Keyboard::Tab);
state.PushField("Tilde", Nz::Keyboard::Tilde);
state.PushField("Browser_Back", Nz::Keyboard::Browser_Back);
state.PushField("Browser_Favorites", Nz::Keyboard::Browser_Favorites);
state.PushField("Browser_Forward", Nz::Keyboard::Browser_Forward);
state.PushField("Browser_Home", Nz::Keyboard::Browser_Home);
state.PushField("Browser_Refresh", Nz::Keyboard::Browser_Refresh);
state.PushField("Browser_Search", Nz::Keyboard::Browser_Search);
state.PushField("Browser_Stop", Nz::Keyboard::Browser_Stop);
state.PushField("Media_Next", Nz::Keyboard::Media_Next);
state.PushField("Media_Play", Nz::Keyboard::Media_Play);
state.PushField("Media_Previous", Nz::Keyboard::Media_Previous);
state.PushField("Media_Stop", Nz::Keyboard::Media_Stop);
state.PushField("Volume_Down", Nz::Keyboard::Volume_Down);
state.PushField("Volume_Mute", Nz::Keyboard::Volume_Mute);
state.PushField("Volume_Up", Nz::Keyboard::Volume_Up);
state.PushField("CapsLock", Nz::Keyboard::CapsLock);
state.PushField("NumLock", Nz::Keyboard::NumLock);
state.PushField("ScrollLock", Nz::Keyboard::ScrollLock);
}
instance.Pop();
state.Pop();
static_assert(Nz::WindowStyle_Max + 1 == 6, "Nz::WindowStyle has been updated but change was not reflected to Lua binding");
instance.PushTable(0, Nz::WindowStyle_Max + 1);
state.PushTable(0, Nz::WindowStyle_Max + 1);
{
instance.PushField("None", Nz::WindowStyle_None);
instance.PushField("Fullscreen", Nz::WindowStyle_Fullscreen);
instance.PushField("Closable", Nz::WindowStyle_Closable);
instance.PushField("Resizable", Nz::WindowStyle_Resizable);
instance.PushField("Titlebar", Nz::WindowStyle_Titlebar);
instance.PushField("Threaded", Nz::WindowStyle_Threaded);
state.PushField("None", Nz::WindowStyle_None);
state.PushField("Fullscreen", Nz::WindowStyle_Fullscreen);
state.PushField("Closable", Nz::WindowStyle_Closable);
state.PushField("Resizable", Nz::WindowStyle_Resizable);
state.PushField("Titlebar", Nz::WindowStyle_Titlebar);
state.PushField("Threaded", Nz::WindowStyle_Threaded);
}
instance.SetGlobal("WindowStyle");
state.SetGlobal("WindowStyle");
}
}

View File

@ -44,10 +44,10 @@ namespace Ndk
* \param instance Lua instance that will interact with the engine & SDK
*/
void LuaAPI::RegisterClasses(Nz::LuaInstance& instance)
void LuaAPI::RegisterClasses(Nz::LuaState& state)
{
Nz::ErrorFlags errFlags(Nz::ErrorFlag_ThrowException, true);
GetBinding()->RegisterClasses(instance);
GetBinding()->RegisterClasses(state);
}
/*!

View File

@ -26,13 +26,13 @@ namespace Nz
friend class LuaClass;
public:
using ClassFunc = std::function<int(LuaInstance& lua, T& instance, std::size_t argumentCount)>;
using ClassIndexFunc = std::function<bool(LuaInstance& lua, T& instance)>;
using ConstructorFunc = std::function<bool(LuaInstance& lua, T* instance, std::size_t argumentCount)>;
using ClassFunc = std::function<int(LuaState& state, T& instance, std::size_t argumentCount)>;
using ClassIndexFunc = std::function<bool(LuaState& state, T& instance)>;
using ConstructorFunc = std::function<bool(LuaState& state, T* instance, std::size_t argumentCount)>;
template<typename P> using ConvertToParent = std::function<P*(T*)>;
using FinalizerFunc = std::function<bool(LuaInstance& lua, T& instance)>;
using StaticIndexFunc = std::function<bool(LuaInstance& lua)>;
using StaticFunc = std::function<int(LuaInstance& lua)>;
using FinalizerFunc = std::function<bool(LuaState& state, T& instance)>;
using StaticIndexFunc = std::function<bool(LuaState& state)>;
using StaticFunc = std::function<int(LuaState& state)>;
LuaClass() = default;
LuaClass(const String& name);
@ -54,9 +54,9 @@ namespace Nz
void Reset();
void Reset(const String& name);
void Register(LuaInstance& lua);
void Register(LuaState& state);
void PushGlobalTable(LuaInstance& lua);
void PushGlobalTable(LuaState& state);
void SetConstructor(ConstructorFunc constructor);
void SetFinalizer(FinalizerFunc finalizer);
@ -69,18 +69,18 @@ namespace Nz
template<typename U, bool HasDestructor>
friend struct LuaClassImplFinalizerSetupProxy;
void PushClassInfo(LuaInstance& lua);
void SetupConstructor(LuaInstance& lua);
void SetupDefaultToString(LuaInstance& lua);
void SetupFinalizer(LuaInstance& lua);
void SetupGetter(LuaInstance& lua, LuaCFunction proxy);
void SetupGlobalTable(LuaInstance& lua);
void SetupMetatable(LuaInstance& lua);
void SetupMethod(LuaInstance& lua, LuaCFunction proxy, const String& name, std::size_t methodIndex);
void SetupSetter(LuaInstance& lua, LuaCFunction proxy);
void PushClassInfo(LuaState& state);
void SetupConstructor(LuaState& state);
void SetupDefaultToString(LuaState& state);
void SetupFinalizer(LuaState& state);
void SetupGetter(LuaState& state, LuaCFunction proxy);
void SetupGlobalTable(LuaState& state);
void SetupMetatable(LuaState& state);
void SetupMethod(LuaState& state, LuaCFunction proxy, const String& name, std::size_t methodIndex);
void SetupSetter(LuaState& state, LuaCFunction proxy);
using ParentFunc = std::function<void(LuaInstance& lua, T* instance)>;
using InstanceGetter = std::function<T*(LuaInstance& lua)>;
using ParentFunc = std::function<void(LuaState& state, T* instance)>;
using InstanceGetter = std::function<T*(LuaState& state)>;
struct ClassInfo
{
@ -98,17 +98,17 @@ namespace Nz
int globalTableRef = -1;
};
static int ConstructorProxy(lua_State* state);
static int FinalizerProxy(lua_State* state);
static int InfoDestructor(lua_State* state);
static void Get(const std::shared_ptr<ClassInfo>& info, LuaInstance& lua, T* instance);
static int GetterProxy(lua_State* state);
static int MethodProxy(lua_State* state);
static int SetterProxy(lua_State* state);
static int StaticGetterProxy(lua_State* state);
static int StaticMethodProxy(lua_State* state);
static int StaticSetterProxy(lua_State* state);
static int ToStringProxy(lua_State* state);
static int ConstructorProxy(lua_State* internalState);
static int FinalizerProxy(lua_State* internalState);
static int InfoDestructor(lua_State* internalState);
static void Get(const std::shared_ptr<ClassInfo>& info, LuaState& state, T* instance);
static int GetterProxy(lua_State* internalState);
static int MethodProxy(lua_State* internalState);
static int SetterProxy(lua_State* internalState);
static int StaticGetterProxy(lua_State* internalState);
static int StaticMethodProxy(lua_State* internalState);
static int StaticSetterProxy(lua_State* internalState);
static int ToStringProxy(lua_State* internalState);
std::map<String, ClassFunc> m_methods;
std::map<String, StaticFunc> m_staticMethods;

View File

@ -19,9 +19,9 @@ namespace Nz
template<class T>
inline void LuaClass<T>::BindDefaultConstructor()
{
SetConstructor([] (Nz::LuaInstance& lua, T* instance, std::size_t argumentCount)
SetConstructor([] (Nz::LuaState& state, T* instance, std::size_t argumentCount)
{
NazaraUnused(lua);
NazaraUnused(state);
NazaraUnused(argumentCount);
PlacementNew(instance);
@ -47,14 +47,14 @@ namespace Nz
std::shared_ptr<typename LuaClass<P>::ClassInfo>& parentInfo = parent.m_info;
parentInfo->instanceGetters[m_info->name] = [info = m_info, convertFunc] (LuaInstance& lua) -> P*
parentInfo->instanceGetters[m_info->name] = [info = m_info, convertFunc] (LuaState& state) -> P*
{
return convertFunc(static_cast<T*>(lua.CheckUserdata(1, info->name)));
return convertFunc(static_cast<T*>(state.CheckUserdata(1, info->name)));
};
m_info->parentGetters.emplace_back([parentInfo, convertFunc] (LuaInstance& lua, T* instance)
m_info->parentGetters.emplace_back([parentInfo, convertFunc] (LuaState& state, T* instance)
{
LuaClass<P>::Get(parentInfo, lua, convertFunc(instance));
LuaClass<P>::Get(parentInfo, state, convertFunc(instance));
});
}
@ -71,30 +71,30 @@ namespace Nz
m_info = std::make_shared<ClassInfo>();
m_info->name = name;
m_info->instanceGetters[m_info->name] = [info = m_info] (LuaInstance& instance)
m_info->instanceGetters[m_info->name] = [info = m_info] (LuaState& state)
{
return static_cast<T*>(instance.CheckUserdata(1, info->name));
return static_cast<T*>(state.CheckUserdata(1, info->name));
};
}
template<class T>
void LuaClass<T>::Register(LuaInstance& lua)
void LuaClass<T>::Register(LuaState& state)
{
PushClassInfo(lua);
PushClassInfo(state);
// Let's create the metatable which will be associated with every instance.
SetupMetatable(lua);
// Let's create the metatable which will be associated with every state.
SetupMetatable(state);
if (m_info->constructor || m_info->staticGetter || m_info->staticSetter || !m_staticMethods.empty())
SetupGlobalTable(lua);
SetupGlobalTable(state);
lua.Pop(); // Pop our ClassInfo, which is now referenced by all our functions
state.Pop(); // Pop our ClassInfo, which is now referenced by all our functions
}
template<class T>
void LuaClass<T>::PushGlobalTable(LuaInstance& lua)
void LuaClass<T>::PushGlobalTable(LuaState& state)
{
lua.PushReference(m_info->globalTableRef);
state.PushReference(m_info->globalTableRef);
}
template<class T>
@ -127,11 +127,11 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
BindMethod(name, [func, handler] (LuaState& state, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArguments(lua);
handler.ProcessArguments(state);
return handler.Invoke(lua, object, func);
return handler.Invoke(state, object, func);
});
}
@ -141,11 +141,11 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
BindMethod(name, [func, handler] (LuaState& state, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArguments(lua);
handler.ProcessArguments(state);
return handler.Invoke(lua, object, func);
return handler.Invoke(state, object, func);
});
}
@ -155,11 +155,11 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
BindMethod(name, [func, handler] (LuaState& state, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArguments(lua);
handler.ProcessArguments(state);
return handler.Invoke(lua, object, func);
return handler.Invoke(state, object, func);
});
}
@ -169,11 +169,11 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
BindMethod(name, [func, handler] (LuaState& state, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArguments(lua);
handler.ProcessArguments(state);
return handler.Invoke(lua, object, func);
return handler.Invoke(state, object, func);
});
}
@ -201,11 +201,11 @@ namespace Nz
{
typename LuaImplFunctionProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindStaticMethod(name, [func, handler] (LuaInstance& lua) -> int
BindStaticMethod(name, [func, handler] (LuaState& state) -> int
{
handler.ProcessArguments(lua);
handler.ProcessArguments(state);
return handler.Invoke(lua, func);
return handler.Invoke(state, func);
});
}
@ -216,37 +216,37 @@ namespace Nz
}
template<class T>
void LuaClass<T>::PushClassInfo(LuaInstance& lua)
void LuaClass<T>::PushClassInfo(LuaState& state)
{
// Our ClassInfo has to outlive the LuaClass, because we don't want to force the user to keep the LuaClass alive
// To do that, each Registration creates a tiny shared_ptr wrapper whose life is directly managed by Lua.
// This shared_ptr object gets pushed as a up-value for every proxy function set in the metatable.
// This way, there is no way our ClassInfo gets freed before any instance and the global class gets destroyed.
std::shared_ptr<ClassInfo>* info = static_cast<std::shared_ptr<ClassInfo>*>(lua.PushUserdata(sizeof(std::shared_ptr<ClassInfo>)));
std::shared_ptr<ClassInfo>* info = static_cast<std::shared_ptr<ClassInfo>*>(state.PushUserdata(sizeof(std::shared_ptr<ClassInfo>)));
PlacementNew(info, m_info);
// Setup a tiny metatable to let Lua know how to destroy our ClassInfo
lua.PushTable(0, 1);
lua.PushLightUserdata(info);
lua.PushCFunction(InfoDestructor, 1);
lua.SetField("__gc");
lua.SetMetatable(-2);
state.PushTable(0, 1);
state.PushLightUserdata(info);
state.PushCFunction(InfoDestructor, 1);
state.SetField("__gc");
state.SetMetatable(-2);
}
template<class T>
void LuaClass<T>::SetupConstructor(LuaInstance& lua)
void LuaClass<T>::SetupConstructor(LuaState& state)
{
lua.PushValue(1); // ClassInfo
lua.PushCFunction(ConstructorProxy, 1);
lua.SetField("__call"); // ClassMeta.__call = ConstructorProxy
state.PushValue(1); // ClassInfo
state.PushCFunction(ConstructorProxy, 1);
state.SetField("__call"); // ClassMeta.__call = ConstructorProxy
}
template<class T>
void LuaClass<T>::SetupDefaultToString(LuaInstance& lua)
void LuaClass<T>::SetupDefaultToString(LuaState& state)
{
lua.PushValue(1); // shared_ptr on UserData
lua.PushCFunction(ToStringProxy, 1);
lua.SetField("__tostring");
state.PushValue(1); // shared_ptr on UserData
state.PushCFunction(ToStringProxy, 1);
state.SetField("__tostring");
}
template<typename T, bool HasDestructor>
@ -255,61 +255,61 @@ namespace Nz
template<typename T>
struct LuaClassImplFinalizerSetupProxy<T, true>
{
static void Setup(LuaInstance& lua)
static void Setup(LuaState& state)
{
lua.PushValue(1); // ClassInfo
lua.PushCFunction(LuaClass<T>::FinalizerProxy, 1);
lua.SetField("__gc");
state.PushValue(1); // ClassInfo
state.PushCFunction(LuaClass<T>::FinalizerProxy, 1);
state.SetField("__gc");
}
};
template<typename T>
struct LuaClassImplFinalizerSetupProxy<T, false>
{
static void Setup(LuaInstance&)
static void Setup(LuaState&)
{
}
};
template<class T>
void LuaClass<T>::SetupFinalizer(LuaInstance& lua)
void LuaClass<T>::SetupFinalizer(LuaState& state)
{
LuaClassImplFinalizerSetupProxy<T, std::is_destructible<T>::value>::Setup(lua);
LuaClassImplFinalizerSetupProxy<T, std::is_destructible<T>::value>::Setup(state);
}
template<class T>
void LuaClass<T>::SetupGetter(LuaInstance& lua, LuaCFunction proxy)
void LuaClass<T>::SetupGetter(LuaState& state, LuaCFunction proxy)
{
lua.PushValue(1); // ClassInfo
lua.PushValue(-2); // Metatable
lua.PushCFunction(proxy, 2);
state.PushValue(1); // ClassInfo
state.PushValue(-2); // Metatable
state.PushCFunction(proxy, 2);
lua.SetField("__index"); // Getter
state.SetField("__index"); // Getter
}
template<class T>
void LuaClass<T>::SetupGlobalTable(LuaInstance& lua)
void LuaClass<T>::SetupGlobalTable(LuaState& state)
{
// Create the global table
lua.PushTable(); // Class = {}
state.PushTable(); // Class = {}
// Create a metatable which will be used for our global table
lua.PushTable(); // ClassMeta = {}
state.PushTable(); // ClassMeta = {}
if (m_info->constructor)
SetupConstructor(lua);
SetupConstructor(state);
if (m_info->staticGetter)
SetupGetter(lua, StaticGetterProxy);
SetupGetter(state, StaticGetterProxy);
else
{
// Optimize by assigning the metatable instead of a search function
lua.PushValue(-1); // Metatable
lua.SetField("__index");
state.PushValue(-1); // Metatable
state.SetField("__index");
}
if (m_info->staticSetter)
SetupSetter(lua, StaticSetterProxy);
SetupSetter(state, StaticSetterProxy);
m_info->staticMethods.reserve(m_staticMethods.size());
for (auto& pair : m_staticMethods)
@ -317,41 +317,41 @@ namespace Nz
std::size_t methodIndex = m_info->staticMethods.size();
m_info->staticMethods.push_back(pair.second);
SetupMethod(lua, StaticMethodProxy, pair.first, methodIndex);
SetupMethod(state, StaticMethodProxy, pair.first, methodIndex);
}
lua.SetMetatable(-2); // setmetatable(Class, ClassMeta), pops ClassMeta
state.SetMetatable(-2); // setmetatable(Class, ClassMeta), pops ClassMeta
lua.PushValue(-1); // As CreateReference() pops the table, push a copy
m_info->globalTableRef = lua.CreateReference();
state.PushValue(-1); // As CreateReference() pops the table, push a copy
m_info->globalTableRef = state.CreateReference();
lua.SetGlobal(m_info->name); // _G["Class"] = Class
state.SetGlobal(m_info->name); // _G["Class"] = Class
}
template<class T>
void LuaClass<T>::SetupMetatable(LuaInstance& lua)
void LuaClass<T>::SetupMetatable(LuaState& state)
{
if (!lua.NewMetatable(m_info->name))
if (!state.NewMetatable(m_info->name))
NazaraWarning("Class \"" + m_info->name + "\" already registred in this instance");
{
SetupFinalizer(lua);
SetupFinalizer(state);
if (m_info->getter || !m_info->parentGetters.empty())
SetupGetter(lua, GetterProxy);
SetupGetter(state, GetterProxy);
else
{
// Optimize by assigning the metatable instead of a search function
// This is only possible if we have no custom getter nor parent
lua.PushValue(-1); // Metatable
lua.SetField("__index");
state.PushValue(-1); // Metatable
state.SetField("__index");
}
if (m_info->setter)
SetupSetter(lua, SetterProxy);
SetupSetter(state, SetterProxy);
// In case a __tostring method is missing, add a default implementation returning the class name
if (m_methods.find("__tostring") == m_methods.end())
SetupDefaultToString(lua);
SetupDefaultToString(state);
m_info->methods.reserve(m_methods.size());
for (auto& pair : m_methods)
@ -359,80 +359,80 @@ namespace Nz
std::size_t methodIndex = m_info->methods.size();
m_info->methods.push_back(pair.second);
SetupMethod(lua, MethodProxy, pair.first, methodIndex);
SetupMethod(state, MethodProxy, pair.first, methodIndex);
}
}
lua.Pop(); //< Pops the metatable, it won't be collected before it's referenced by the Lua registry.
state.Pop(); //< Pops the metatable, it won't be collected before it's referenced by the Lua registry.
}
template<class T>
void LuaClass<T>::SetupMethod(LuaInstance& lua, LuaCFunction proxy, const String& name, std::size_t methodIndex)
void LuaClass<T>::SetupMethod(LuaState& state, LuaCFunction proxy, const String& name, std::size_t methodIndex)
{
lua.PushValue(1); // ClassInfo
lua.PushInteger(methodIndex);
lua.PushCFunction(proxy, 2);
state.PushValue(1); // ClassInfo
state.PushInteger(methodIndex);
state.PushCFunction(proxy, 2);
lua.SetField(name); // Method name
state.SetField(name); // Method name
}
template<class T>
void LuaClass<T>::SetupSetter(LuaInstance& lua, LuaCFunction proxy)
void LuaClass<T>::SetupSetter(LuaState& state, LuaCFunction proxy)
{
lua.PushValue(1); // ClassInfo
lua.PushCFunction(proxy, 1);
state.PushValue(1); // ClassInfo
state.PushCFunction(proxy, 1);
lua.SetField("__newindex"); // Setter
state.SetField("__newindex"); // Setter
}
template<class T>
int LuaClass<T>::ConstructorProxy(lua_State* state)
int LuaClass<T>::ConstructorProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
const ConstructorFunc& constructor = info->constructor;
lua.Remove(1); // On enlève l'argument "table" du stack
state.Remove(1); // On enlève l'argument "table" du stack
std::size_t argCount = lua.GetStackTop();
std::size_t argCount = state.GetStackTop();
T* instance = static_cast<T*>(lua.PushUserdata(sizeof(T)));
T* instance = static_cast<T*>(state.PushUserdata(sizeof(T)));
if (!constructor(lua, instance, argCount))
if (!constructor(state, instance, argCount))
{
lua.Error("Constructor failed");
state.Error("Constructor failed");
return 0; // Normalement jamais exécuté (l'erreur provoquant une exception)
}
lua.SetMetatable(info->name);
state.SetMetatable(info->name);
return 1;
}
template<class T>
int LuaClass<T>::FinalizerProxy(lua_State* state)
int LuaClass<T>::FinalizerProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
const FinalizerFunc& finalizer = info->finalizer;
T* instance = static_cast<T*>(lua.CheckUserdata(1, info->name));
lua.Remove(1); //< Remove the instance from the Lua stack
T* instance = static_cast<T*>(state.CheckUserdata(1, info->name));
state.Remove(1); //< Remove the instance from the Lua stack
if (!finalizer || finalizer(lua, *instance))
if (!finalizer || finalizer(state, *instance))
instance->~T();
return 0;
}
template<class T>
int LuaClass<T>::InfoDestructor(lua_State* state)
int LuaClass<T>::InfoDestructor(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
lua.DestroyReference(info->globalTableRef);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
state.DestroyReference(info->globalTableRef);
using namespace std; // Obligatoire pour le destructeur
info.~shared_ptr(); // Si vous voyez une autre façon de faire, je suis preneur
@ -441,27 +441,27 @@ namespace Nz
}
template<class T>
void LuaClass<T>::Get(const std::shared_ptr<ClassInfo>& info, LuaInstance& lua, T* instance)
void LuaClass<T>::Get(const std::shared_ptr<ClassInfo>& info, LuaState& state, T* instance)
{
const ClassIndexFunc& getter = info->getter;
if (!getter || !getter(lua, *instance))
if (!getter || !getter(state, *instance))
{
// Query from the metatable
lua.GetMetatable(info->name); //< Metatable
lua.PushValue(2); //< Field
lua.GetTable(); // Metatable[Field]
state.GetMetatable(info->name); //< Metatable
state.PushValue(2); //< Field
state.GetTable(); // Metatable[Field]
lua.Remove(-2); // Remove Metatable
state.Remove(-2); // Remove Metatable
if (!lua.IsValid(-1))
if (!state.IsValid(-1))
{
for (const ParentFunc& parentGetter : info->parentGetters)
{
lua.Pop(); //< Pop the last nil value
state.Pop(); //< Pop the last nil value
parentGetter(lua, instance);
if (lua.IsValid(-1))
parentGetter(state, instance);
if (state.IsValid(-1))
return;
}
}
@ -469,131 +469,131 @@ namespace Nz
}
template<class T>
int LuaClass<T>::GetterProxy(lua_State* state)
int LuaClass<T>::GetterProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
T* instance = static_cast<T*>(lua.CheckUserdata(1, info->name));
T* instance = static_cast<T*>(state.CheckUserdata(1, info->name));
Get(info, lua, instance);
Get(info, state, instance);
return 1;
}
template<class T>
int LuaClass<T>::MethodProxy(lua_State* state)
int LuaClass<T>::MethodProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
T* instance = nullptr;
if (lua.GetMetatable(1))
if (state.GetMetatable(1))
{
LuaType type = lua.GetField("__name");
LuaType type = state.GetField("__name");
if (type == LuaType_String)
{
String name = lua.ToString(-1);
String name = state.ToString(-1);
auto it = info->instanceGetters.find(name);
if (it != info->instanceGetters.end())
instance = it->second(lua);
instance = it->second(state);
}
lua.Pop(2);
state.Pop(2);
}
if (!instance)
{
lua.Error("Method cannot be called without an object");
state.Error("Method cannot be called without an object");
return 0;
}
std::size_t argCount = lua.GetStackTop() - 1U;
std::size_t argCount = state.GetStackTop() - 1U;
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
unsigned int index = static_cast<unsigned int>(state.ToInteger(state.GetIndexOfUpValue(2)));
const ClassFunc& method = info->methods[index];
return method(lua, *instance, argCount);
return method(state, *instance, argCount);
}
template<class T>
int LuaClass<T>::SetterProxy(lua_State* state)
int LuaClass<T>::SetterProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
const ClassIndexFunc& setter = info->setter;
T& instance = *static_cast<T*>(lua.CheckUserdata(1, info->name));
T& instance = *static_cast<T*>(state.CheckUserdata(1, info->name));
if (!setter(lua, instance))
if (!setter(state, instance))
{
std::size_t length;
const char* str = lua.ToString(2, &length);
const char* str = state.ToString(2, &length);
lua.Error("Class \"" + info->name + "\" has no field \"" + String(str, length) + "\")");
state.Error("Class \"" + info->name + "\" has no field \"" + String(str, length) + "\")");
}
return 1;
}
template<class T>
int LuaClass<T>::StaticGetterProxy(lua_State* state)
int LuaClass<T>::StaticGetterProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
const StaticIndexFunc& getter = info->staticGetter;
if (!getter(lua))
if (!getter(state))
{
// On accède alors à la table
lua.PushValue(lua.GetIndexOfUpValue(2));
lua.PushValue(-2);
lua.GetTable();
state.PushValue(state.GetIndexOfUpValue(2));
state.PushValue(-2);
state.GetTable();
}
return 1;
}
template<class T>
int LuaClass<T>::StaticMethodProxy(lua_State* state)
int LuaClass<T>::StaticMethodProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
unsigned int index = static_cast<unsigned int>(state.ToInteger(state.GetIndexOfUpValue(2)));
const StaticFunc& method = info->staticMethods[index];
return method(lua);
return method(state);
}
template<class T>
int LuaClass<T>::StaticSetterProxy(lua_State* state)
int LuaClass<T>::StaticSetterProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
const StaticIndexFunc& setter = info->staticSetter;
if (!setter(lua))
if (!setter(state))
{
std::size_t length;
const char* str = lua.ToString(2, &length);
const char* str = state.ToString(2, &length);
lua.Error("Class \"" + info->name + "\" has no static field \"" + String(str, length) + ')');
state.Error("Class \"" + info->name + "\" has no static field \"" + String(str, length) + ')');
}
return 1;
}
template<class T>
int LuaClass<T>::ToStringProxy(lua_State* state)
int LuaClass<T>::ToStringProxy(lua_State* internalState)
{
LuaInstance& lua = *LuaInstance::GetInstance(state);
LuaState state = LuaInstance::GetState(internalState);
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(state.ToUserdata(state.GetIndexOfUpValue(1)));
lua.PushString(info->name);
state.PushString(info->name);
return 1;
}
}

View File

@ -4,201 +4,41 @@
#pragma once
#ifndef NAZARA_LUASTATE_HPP
#define NAZARA_LUASTATE_HPP
#ifndef NAZARA_LUAINSTANCE_HPP
#define NAZARA_LUAINSTANCE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Stream.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Lua/Config.hpp>
#include <Nazara/Lua/Enums.hpp>
#include <Nazara/Lua/LuaState.hpp>
#include <cstddef>
#include <functional>
struct lua_Debug;
struct lua_State;
namespace Nz
{
class LuaInstance;
using LuaCFunction = int (*)(lua_State* state);
using LuaFunction = std::function<int(LuaInstance& instance)>;
class NAZARA_LUA_API LuaInstance
class NAZARA_LUA_API LuaInstance : public LuaState
{
friend class LuaState;
public:
LuaInstance();
LuaInstance(const LuaInstance&) = delete;
LuaInstance(LuaInstance&& instance) noexcept;
LuaInstance(LuaInstance&& instance) = default;
~LuaInstance();
void ArgCheck(bool condition, unsigned int argNum, const char* error) const;
void ArgCheck(bool condition, unsigned int argNum, const String& error) const;
int ArgError(unsigned int argNum, const char* error) const;
int ArgError(unsigned int argNum, const String& error) const;
bool Call(unsigned int argCount);
bool Call(unsigned int argCount, unsigned int resultCount);
template<typename T> T Check(int* index) const;
template<typename T> T Check(int* index, T defValue) const;
void CheckAny(int index) const;
bool CheckBoolean(int index) const;
bool CheckBoolean(int index, bool defValue) const;
template<typename T> T CheckBoundInteger(int index) const;
template<typename T> T CheckBoundInteger(int index, T defValue) const;
template<typename T> T CheckField(const char* fieldName, int tableIndex = -1) const;
template<typename T> T CheckField(const String& fieldName, int tableIndex = -1) const;
template<typename T> T CheckField(const char* fieldName, T defValue, int tableIndex = -1) const;
template<typename T> T CheckField(const String& fieldName, T defValue, int tableIndex = -1) const;
long long CheckInteger(int index) const;
long long CheckInteger(int index, long long defValue) const;
template<typename T> T CheckGlobal(const char* fieldName) const;
template<typename T> T CheckGlobal(const String& fieldName) const;
template<typename T> T CheckGlobal(const char* fieldName, T defValue) const;
template<typename T> T CheckGlobal(const String& fieldName, T defValue) const;
double CheckNumber(int index) const;
double CheckNumber(int index, double defValue) const;
void CheckStack(int space, const char* error = nullptr) const;
void CheckStack(int space, const String& error) const;
const char* CheckString(int index, std::size_t* length = nullptr) const;
const char* CheckString(int index, const char* defValue, std::size_t* length = nullptr) const;
void CheckType(int index, LuaType type) const;
void* CheckUserdata(int index, const char* tname) const;
void* CheckUserdata(int index, const String& tname) const;
bool Compare(int index1, int index2, LuaComparison comparison) const;
void Compute(LuaOperation operation) const;
void Concatenate(int count) const;
int CreateReference();
void DestroyReference(int ref);
String DumpStack() const;
void Error(const char* message) const;
void Error(const String& message) const;
bool Execute(const String& code);
bool ExecuteFromFile(const String& filePath);
bool ExecuteFromMemory(const void* data, std::size_t size);
bool ExecuteFromStream(Stream& stream);
int GetAbsIndex(int index) const;
LuaType GetField(const char* fieldName, int tableIndex = -1) const;
LuaType GetField(const String& fieldName, int tableIndex = -1) const;
LuaType GetGlobal(const char* name) const;
LuaType GetGlobal(const String& name) const;
inline lua_State* GetInternalState() const;
inline String GetLastError() const;
inline std::size_t GetMemoryLimit() const;
inline std::size_t GetMemoryUsage() const;
LuaType GetMetatable(const char* tname) const;
LuaType GetMetatable(const String& tname) const;
bool GetMetatable(int index) const;
unsigned int GetStackTop() const;
LuaType GetTable(int index = -2) const;
LuaType GetTableRaw(int index = -2) const;
inline UInt32 GetTimeLimit() const;
LuaType GetType(int index) const;
const char* GetTypeName(LuaType type) const;
void Insert(int index) const;
bool IsOfType(int index, LuaType type) const;
bool IsOfType(int index, const char* tname) const;
bool IsOfType(int index, const String& tname) const;
bool IsValid(int index) const;
long long Length(int index) const;
std::size_t LengthRaw(int index) const;
void MoveTo(LuaInstance* instance, int n) const;
bool NewMetatable(const char* str);
bool NewMetatable(const String& str);
bool Next(int index = -2) const;
void Pop(unsigned int n = 1U) const;
template<typename T> int Push(T arg) const;
template<typename T, typename T2, typename... Args> int Push(T firstArg, T2 secondArg, Args... args) const;
void PushBoolean(bool value) const;
void PushCFunction(LuaCFunction func, unsigned int upvalueCount = 0) const;
template<typename T> void PushField(const char* name, T&& arg, int tableIndex = -2) const;
template<typename T> void PushField(const String& name, T&& arg, int tableIndex = -2) const;
void PushFunction(LuaFunction func) const;
template<typename R, typename... Args, typename... DefArgs> void PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const;
template<typename T> void PushGlobal(const char* name, T&& arg);
template<typename T> void PushGlobal(const String& name, T&& arg);
template<typename T> void PushInstance(const char* tname, const T& instance) const;
template<typename T> void PushInstance(const char* tname, T&& instance) const;
template<typename T, typename... Args> void PushInstance(const char* tname, Args&&... args) const;
void PushInteger(long long value) const;
void PushLightUserdata(void* value) const;
void PushMetatable(const char* str) const;
void PushMetatable(const String& str) const;
void PushNil() const;
void PushNumber(double value) const;
void PushReference(int ref) const;
void PushString(const char* str) const;
void PushString(const char* str, std::size_t size) const;
void PushString(const String& str) const;
void PushTable(std::size_t sequenceElementCount = 0, std::size_t arrayElementCount = 0) const;
void* PushUserdata(std::size_t size) const;
void PushValue(int index) const;
void Remove(int index) const;
void Replace(int index) const;
void SetField(const char* name, int tableIndex = -2) const;
void SetField(const String& name, int tableIndex = -2) const;
void SetGlobal(const char* name);
void SetGlobal(const String& name);
void SetMetatable(const char* tname) const;
void SetMetatable(const String& tname) const;
void SetMetatable(int index) const;
void SetMemoryLimit(std::size_t memoryLimit);
void SetTable(int index = -3) const;
void SetTableRaw(int index = -3) const;
void SetTimeLimit(UInt32 timeLimit);
bool ToBoolean(int index) const;
long long ToInteger(int index, bool* succeeded = nullptr) const;
double ToNumber(int index, bool* succeeded = nullptr) const;
const void* ToPointer(int index) const;
const char* ToString(int index, std::size_t* length = nullptr) const;
void* ToUserdata(int index) const;
void* ToUserdata(int index, const char* tname) const;
void* ToUserdata(int index, const String& tname) const;
LuaInstance& operator=(const LuaInstance&) = delete;
LuaInstance& operator=(LuaInstance&& instance) noexcept;
static int GetIndexOfUpValue(int upValue);
static LuaInstance* GetInstance(lua_State* state);
LuaInstance& operator=(LuaInstance&& instance) = default;
private:
template<typename T> T CheckBounds(int index, long long value) const;
bool Run(int argCount, int resultCount);
static void* MemoryAllocator(void *ud, void *ptr, std::size_t osize, std::size_t nsize);
static int ProxyFunc(lua_State* state);
static void TimeLimiter(lua_State* state, lua_Debug* debug);
static void TimeLimiter(lua_State* internalState, lua_Debug* debug);
std::size_t m_memoryLimit;
std::size_t m_memoryUsage;
UInt32 m_timeLimit;
Clock m_clock;
String m_lastError;
lua_State* m_state;
unsigned int m_level;
};
}
#include <Nazara/Lua/LuaInstance.inl>
#endif // NAZARA_LUASTATE_HPP
#endif // NAZARA_LUAINSTANCE_HPP

View File

@ -15,783 +15,4 @@
namespace Nz
{
inline lua_State* LuaInstance::GetInternalState() const
{
return m_state;
}
inline String LuaInstance::GetLastError() const
{
return m_lastError;
}
inline std::size_t LuaInstance::GetMemoryLimit() const
{
return m_memoryLimit;
}
inline std::size_t LuaInstance::GetMemoryUsage() const
{
return m_memoryUsage;
}
inline UInt32 LuaInstance::GetTimeLimit() const
{
return m_timeLimit;
}
// Functions args
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, bool* arg, TypeTag<bool>)
{
*arg = instance.CheckBoolean(index);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, bool* arg, bool defValue, TypeTag<bool>)
{
*arg = instance.CheckBoolean(index, defValue);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, std::string* arg, TypeTag<std::string>)
{
std::size_t strLength = 0;
const char* str = instance.CheckString(index, &strLength);
arg->assign(str, strLength);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, String* arg, TypeTag<String>)
{
std::size_t strLength = 0;
const char* str = instance.CheckString(index, &strLength);
arg->Set(str, strLength);
return 1;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
return LuaImplQueryArg(instance, index, reinterpret_cast<UnderlyingT*>(arg), TypeTag<UnderlyingT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
return LuaImplQueryArg(instance, index, reinterpret_cast<UnderlyingT*>(arg), static_cast<UnderlyingT>(defValue), TypeTag<UnderlyingT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
UnderlyingT pot2Val;
unsigned int ret = LuaImplQueryArg(instance, index, &pot2Val, TypeTag<UnderlyingT>());
*arg = static_cast<T>(IntegralLog2Pot(pot2Val));
return ret;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
UnderlyingT pot2Val;
unsigned int ret = LuaImplQueryArg(instance, index, &pot2Val, 1U << static_cast<UnderlyingT>(defValue), TypeTag<UnderlyingT>());
*arg = static_cast<T>(IntegralLog2Pot(pot2Val));
return ret;
}
template<typename E>
unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, Flags<E>* arg, TypeTag<Flags<E>>)
{
*arg = Flags<E>(instance.CheckBoundInteger<UInt32>(index));
return 1;
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag<T>)
{
*arg = static_cast<T>(instance.CheckNumber(index));
return 1;
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
{
*arg = static_cast<T>(instance.CheckNumber(index, static_cast<double>(defValue)));
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag<T>)
{
*arg = instance.CheckBoundInteger<T>(index);
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
{
*arg = instance.CheckBoundInteger<T>(index, defValue);
return 1;
}
template<typename T>
std::enable_if_t<!std::is_integral<T>::value && !std::is_enum<T>::value && !std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, const T& defValue, TypeTag<T> tag)
{
if (instance.IsValid(index))
return LuaImplQueryArg(instance, index, arg, tag);
else
{
*arg = defValue;
return 1;
}
}
template<typename T>
unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag<const T&>)
{
return LuaImplQueryArg(instance, index, arg, TypeTag<T>());
}
template<typename T>
unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, const T& defValue, TypeTag<const T&>)
{
return LuaImplQueryArg(instance, index, arg, defValue, TypeTag<T>());
}
// Function returns
inline int LuaImplReplyVal(const LuaInstance& instance, bool val, TypeTag<bool>)
{
instance.PushBoolean(val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, double val, TypeTag<double>)
{
instance.PushNumber(val);
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, float val, TypeTag<float>)
{
instance.PushNumber(val);
return 1;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T>)
{
using EnumT = typename std::underlying_type<T>::type;
return LuaImplReplyVal(instance, static_cast<EnumT>(val), TypeTag<EnumT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T>)
{
Flags<T> flags(val);
return LuaImplReplyVal(instance, flags, TypeTag<decltype(flags)>());
}
template<typename E>
int LuaImplReplyVal(const LuaInstance& instance, Flags<E> val, TypeTag<Flags<E>>)
{
instance.PushInteger(UInt32(val));
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T>)
{
instance.PushInteger(val);
return 1;
}
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value || std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T&>)
{
return LuaImplReplyVal(instance, val, TypeTag<T>());
}
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value || std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<const T&>)
{
return LuaImplReplyVal(instance, val, TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<const T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
int LuaImplReplyVal(const LuaInstance& instance, T&& val, TypeTag<T&&>)
{
return LuaImplReplyVal(instance, std::forward<T>(val), TypeTag<T>());
}
inline int LuaImplReplyVal(const LuaInstance& instance, std::string&& val, TypeTag<std::string>)
{
instance.PushString(val.c_str(), val.size());
return 1;
}
template<typename T>
inline int LuaImplReplyVal(const LuaInstance& instance, std::vector<T>&& valContainer, TypeTag<std::vector<T>>)
{
std::size_t index = 1;
instance.PushTable(valContainer.size());
for (T& val : valContainer)
{
instance.PushInteger(index++);
if (LuaImplReplyVal(instance, std::move(val), TypeTag<T>()) != 1)
{
instance.Error("Couldn't create table: type need more than one place to store");
return 0;
}
instance.SetTable();
}
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, ByteArray&& val, TypeTag<ByteArray>)
{
instance.PushString(reinterpret_cast<const char*>(val.GetConstBuffer()), val.GetSize());
return 1;
}
inline int LuaImplReplyVal(const LuaInstance& instance, String&& val, TypeTag<String>)
{
instance.PushString(std::move(val));
return 1;
}
template<typename T1, typename T2>
int LuaImplReplyVal(const LuaInstance& instance, std::pair<T1, T2>&& val, TypeTag<std::pair<T1, T2>>)
{
int retVal = 0;
retVal += LuaImplReplyVal(instance, std::move(val.first), TypeTag<T1>());
retVal += LuaImplReplyVal(instance, std::move(val.second), TypeTag<T2>());
return retVal;
}
template<bool HasDefault>
struct LuaImplArgProcesser;
template<>
struct LuaImplArgProcesser<true>
{
template<std::size_t N, std::size_t FirstDefArg, typename ArgType, typename ArgContainer, typename DefArgContainer>
static unsigned int Process(const LuaInstance& instance, unsigned int argIndex, ArgContainer& args, DefArgContainer& defArgs)
{
return LuaImplQueryArg(instance, argIndex, &std::get<N>(args), std::get<FirstDefArg + std::tuple_size<DefArgContainer>() - N - 1>(defArgs), TypeTag<ArgType>());
}
};
template<>
struct LuaImplArgProcesser<false>
{
template<std::size_t N, std::size_t FirstDefArg, typename ArgType, typename ArgContainer, typename DefArgContainer>
static unsigned int Process(const LuaInstance& instance, unsigned int argIndex, ArgContainer& args, DefArgContainer& defArgs)
{
NazaraUnused(defArgs);
return LuaImplQueryArg(instance, argIndex, &std::get<N>(args), TypeTag<ArgType>());
}
};
template<typename... Args>
class LuaImplFunctionProxy
{
public:
template<typename... DefArgs>
class Impl
{
static constexpr std::size_t ArgCount = sizeof...(Args);
static constexpr std::size_t DefArgCount = sizeof...(DefArgs);
static_assert(ArgCount >= DefArgCount, "There cannot be more default arguments than argument");
static constexpr std::size_t FirstDefArg = ArgCount - DefArgCount;
public:
Impl(DefArgs... defArgs) :
m_defaultArgs(std::forward<DefArgs>(defArgs)...)
{
}
void ProcessArguments(const LuaInstance& instance) const
{
m_index = 1;
ProcessArgs<0, Args...>(instance);
}
int Invoke(const LuaInstance& instance, void(*func)(Args...)) const
{
NazaraUnused(instance);
Apply(func, m_args);
return 0;
}
template<typename Ret>
int Invoke(const LuaInstance& instance, Ret(*func)(Args...)) const
{
return LuaImplReplyVal(instance, std::move(Apply(func, m_args)), TypeTag<decltype(Apply(func, m_args))>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;
template<std::size_t N>
void ProcessArgs(const LuaInstance& instance) const
{
NazaraUnused(instance);
// No argument to process
}
template<std::size_t N, typename ArgType>
void ProcessArgs(const LuaInstance& instance) const
{
LuaImplArgProcesser<(N >= FirstDefArg)>::template Process<N, FirstDefArg, ArgType>(instance, m_index, m_args, m_defaultArgs);
}
template<std::size_t N, typename ArgType1, typename ArgType2, typename... Rest>
void ProcessArgs(const LuaInstance& instance) const
{
ProcessArgs<N, ArgType1>(instance);
ProcessArgs<N + 1, ArgType2, Rest...>(instance);
}
mutable ArgContainer m_args;
DefArgContainer m_defaultArgs;
mutable unsigned int m_index;
};
};
template<typename... Args>
class LuaImplMethodProxy
{
public:
template<typename... DefArgs>
class Impl
{
static constexpr std::size_t ArgCount = sizeof...(Args);
static constexpr std::size_t DefArgCount = sizeof...(DefArgs);
static_assert(ArgCount >= DefArgCount, "There cannot be more default arguments than argument");
static constexpr std::size_t FirstDefArg = ArgCount - DefArgCount;
public:
Impl(DefArgs... defArgs) :
m_defaultArgs(std::forward<DefArgs>(defArgs)...)
{
}
void ProcessArguments(const LuaInstance& instance) const
{
m_index = 2; //< 1 being the instance
ProcessArgs<0, Args...>(instance);
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, T& object, void(P::*func)(Args...)) const
{
NazaraUnused(instance);
Apply(object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, T& object, Ret(P::*func)(Args...)) const
{
return LuaImplReplyVal(instance, std::move(Apply(object, func, m_args)), TypeTag<decltype(Apply(object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, T& object, T&(P::*func)(Args...)) const
{
T& r = Apply(object, func, m_args);
if (&r == &object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, const T& object, void(P::*func)(Args...) const) const
{
NazaraUnused(instance);
Apply(object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, const T& object, Ret(P::*func)(Args...) const) const
{
return LuaImplReplyVal(instance, std::move(Apply(object, func, m_args)), TypeTag<decltype(Apply(object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaInstance& instance, const T& object, const T&(P::*func)(Args...) const) const
{
const T& r = Apply(object, func, m_args);
if (&r == &object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, T& object, void(P::*func)(Args...)) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
Apply(*object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, T& object, Ret(P::*func)(Args...)) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
return LuaImplReplyVal(instance, std::move(Apply(*object, func, m_args)), TypeTag<decltype(Apply(*object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, T& object, typename PointedType<T>::type&(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
const typename PointedType<T>::type& r = Apply(*object, func, m_args);
if (&r == &*object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, const T& object, void(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
Apply(*object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, const T& object, Ret(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
return LuaImplReplyVal(instance, std::move(Apply(*object, func, m_args)), TypeTag<decltype(Apply(*object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaInstance& instance, const T& object, const typename PointedType<T>::type&(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
const typename PointedType<T>::type& r = Apply(*object, func, m_args);
if (&r == &*object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;
template<std::size_t N>
void ProcessArgs(const LuaInstance& instance) const
{
NazaraUnused(instance);
// No argument to process
}
template<std::size_t N, typename ArgType>
void ProcessArgs(const LuaInstance& instance) const
{
m_index += LuaImplArgProcesser<(N >= FirstDefArg)>::template Process<N, FirstDefArg, ArgType>(instance, m_index, m_args, m_defaultArgs);
}
template<std::size_t N, typename ArgType1, typename ArgType2, typename... Rest>
void ProcessArgs(const LuaInstance& instance) const
{
ProcessArgs<N, ArgType1>(instance);
ProcessArgs<N + 1, ArgType2, Rest...>(instance);
}
mutable ArgContainer m_args;
DefArgContainer m_defaultArgs;
mutable unsigned int m_index;
};
};
template<typename T>
T LuaInstance::Check(int* index) const
{
NazaraAssert(index, "Invalid index pointer");
T object;
*index += LuaImplQueryArg(*this, *index, &object, TypeTag<T>());
return object;
}
template<typename T>
T LuaInstance::Check(int* index, T defValue) const
{
NazaraAssert(index, "Invalid index pointer");
T object;
*index += LuaImplQueryArg(*this, *index, &object, defValue, TypeTag<T>());
return object;
}
template<typename T>
inline T LuaInstance::CheckBoundInteger(int index) const
{
return CheckBounds<T>(index, CheckInteger(index));
}
template<typename T>
inline T LuaInstance::CheckBoundInteger(int index, T defValue) const
{
return CheckBounds<T>(index, CheckInteger(index, defValue));
}
template<typename T>
T LuaInstance::CheckField(const char* fieldName, int tableIndex) const
{
T object;
GetField(fieldName, tableIndex);
tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaInstance::CheckField(const String& fieldName, int tableIndex) const
{
return CheckField<T>(fieldName.GetConstBuffer(), tableIndex);
}
template<typename T>
T LuaInstance::CheckField(const char* fieldName, T defValue, int tableIndex) const
{
T object;
GetField(fieldName, tableIndex);
tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaInstance::CheckField(const String& fieldName, T defValue, int tableIndex) const
{
return CheckField<T>(fieldName.GetConstBuffer(), defValue, tableIndex);
}
template<typename T>
T LuaInstance::CheckGlobal(const char* fieldName) const
{
T object;
GetGlobal(fieldName);
LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaInstance::CheckGlobal(const String& fieldName) const
{
return CheckGlobal<T>(fieldName.GetConstBuffer());
}
template<typename T>
T LuaInstance::CheckGlobal(const char* fieldName, T defValue) const
{
T object;
GetGlobal(fieldName);
LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaInstance::CheckGlobal(const String& fieldName, T defValue) const
{
return CheckGlobal<T>(fieldName.GetConstBuffer(), defValue);
}
template<typename T>
int LuaInstance::Push(T arg) const
{
return LuaImplReplyVal(*this, std::move(arg), TypeTag<T>());
}
template<typename T, typename T2, typename... Args>
int LuaInstance::Push(T firstArg, T2 secondArg, Args... args) const
{
int valCount = 0;
valCount += Push(std::move(firstArg));
valCount += Push(secondArg, std::forward<Args>(args)...);
return valCount;
}
template<typename T>
void LuaInstance::PushField(const char* name, T&& arg, int tableIndex) const
{
Push<T>(std::forward<T>(arg));
SetField(name, tableIndex);
}
template<typename T>
void LuaInstance::PushField(const String& name, T&& arg, int tableIndex) const
{
PushField(name.GetConstBuffer(), std::forward<T>(arg), tableIndex);
}
template<typename R, typename... Args, typename... DefArgs>
void LuaInstance::PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const
{
typename LuaImplFunctionProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
PushFunction([func, handler] (LuaInstance& lua) -> int
{
handler.ProcessArguments(lua);
return handler.Invoke(lua, func);
});
}
template<typename T>
void LuaInstance::PushGlobal(const char* name, T&& arg)
{
Push<T>(std::forward<T>(arg));
SetGlobal(name);
}
template<typename T>
void LuaInstance::PushGlobal(const String& name, T&& arg)
{
PushGlobal(name.GetConstBuffer(), std::forward<T>(arg));
}
template<typename T>
void LuaInstance::PushInstance(const char* tname, const T& instance) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, instance);
SetMetatable(tname);
}
template<typename T>
void LuaInstance::PushInstance(const char* tname, T&& instance) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, std::move(instance));
SetMetatable(tname);
}
template<typename T, typename... Args>
void LuaInstance::PushInstance(const char* tname, Args&&... args) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, std::forward<Args>(args)...);
SetMetatable(tname);
}
template<typename T>
T LuaInstance::CheckBounds(int index, long long value) const
{
constexpr long long minBounds = std::numeric_limits<T>::min();
constexpr long long maxBounds = std::numeric_limits<T>::max();
if (value < minBounds || value > maxBounds)
{
Nz::StringStream stream;
stream << "Argument #" << index << " is outside value range [" << minBounds << ", " << maxBounds << "] (" << value << ')';
Error(stream);
}
return static_cast<T>(value);
}
}

View File

@ -0,0 +1,197 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_LUASTATE_HPP
#define NAZARA_LUASTATE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Stream.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Lua/Config.hpp>
#include <Nazara/Lua/Enums.hpp>
#include <cstddef>
#include <functional>
struct lua_Debug;
struct lua_State;
namespace Nz
{
class LuaInstance;
class LuaState;
using LuaCFunction = int (*)(lua_State* internalState);
using LuaFunction = std::function<int(LuaState& state)>;
class NAZARA_LUA_API LuaState
{
public:
LuaState(const LuaState&) = default;
LuaState(LuaState&& instance) noexcept;
~LuaState() = default;
void ArgCheck(bool condition, unsigned int argNum, const char* error) const;
void ArgCheck(bool condition, unsigned int argNum, const String& error) const;
int ArgError(unsigned int argNum, const char* error) const;
int ArgError(unsigned int argNum, const String& error) const;
bool Call(unsigned int argCount);
bool Call(unsigned int argCount, unsigned int resultCount);
template<typename T> T Check(int* index) const;
template<typename T> T Check(int* index, T defValue) const;
void CheckAny(int index) const;
bool CheckBoolean(int index) const;
bool CheckBoolean(int index, bool defValue) const;
template<typename T> T CheckBoundInteger(int index) const;
template<typename T> T CheckBoundInteger(int index, T defValue) const;
template<typename T> T CheckField(const char* fieldName, int tableIndex = -1) const;
template<typename T> T CheckField(const String& fieldName, int tableIndex = -1) const;
template<typename T> T CheckField(const char* fieldName, T defValue, int tableIndex = -1) const;
template<typename T> T CheckField(const String& fieldName, T defValue, int tableIndex = -1) const;
long long CheckInteger(int index) const;
long long CheckInteger(int index, long long defValue) const;
template<typename T> T CheckGlobal(const char* fieldName) const;
template<typename T> T CheckGlobal(const String& fieldName) const;
template<typename T> T CheckGlobal(const char* fieldName, T defValue) const;
template<typename T> T CheckGlobal(const String& fieldName, T defValue) const;
double CheckNumber(int index) const;
double CheckNumber(int index, double defValue) const;
void CheckStack(int space, const char* error = nullptr) const;
void CheckStack(int space, const String& error) const;
const char* CheckString(int index, std::size_t* length = nullptr) const;
const char* CheckString(int index, const char* defValue, std::size_t* length = nullptr) const;
void CheckType(int index, LuaType type) const;
void* CheckUserdata(int index, const char* tname) const;
void* CheckUserdata(int index, const String& tname) const;
bool Compare(int index1, int index2, LuaComparison comparison) const;
void Compute(LuaOperation operation) const;
void Concatenate(int count) const;
int CreateReference();
void DestroyReference(int ref);
String DumpStack() const;
void Error(const char* message) const;
void Error(const String& message) const;
bool Execute(const String& code);
bool ExecuteFromFile(const String& filePath);
bool ExecuteFromMemory(const void* data, std::size_t size);
bool ExecuteFromStream(Stream& stream);
int GetAbsIndex(int index) const;
LuaType GetField(const char* fieldName, int tableIndex = -1) const;
LuaType GetField(const String& fieldName, int tableIndex = -1) const;
LuaType GetGlobal(const char* name) const;
LuaType GetGlobal(const String& name) const;
inline lua_State* GetInternalState() const;
inline String GetLastError() const;
LuaType GetMetatable(const char* tname) const;
LuaType GetMetatable(const String& tname) const;
bool GetMetatable(int index) const;
unsigned int GetStackTop() const;
LuaType GetTable(int index = -2) const;
LuaType GetTableRaw(int index = -2) const;
LuaType GetType(int index) const;
const char* GetTypeName(LuaType type) const;
void Insert(int index) const;
bool IsOfType(int index, LuaType type) const;
bool IsOfType(int index, const char* tname) const;
bool IsOfType(int index, const String& tname) const;
bool IsValid(int index) const;
long long Length(int index) const;
std::size_t LengthRaw(int index) const;
void MoveTo(LuaState* instance, int n) const;
bool NewMetatable(const char* str);
bool NewMetatable(const String& str);
bool Next(int index = -2) const;
void Pop(unsigned int n = 1U) const;
template<typename T> int Push(T arg) const;
template<typename T, typename T2, typename... Args> int Push(T firstArg, T2 secondArg, Args... args) const;
void PushBoolean(bool value) const;
void PushCFunction(LuaCFunction func, unsigned int upvalueCount = 0) const;
template<typename T> void PushField(const char* name, T&& arg, int tableIndex = -2) const;
template<typename T> void PushField(const String& name, T&& arg, int tableIndex = -2) const;
void PushFunction(LuaFunction func) const;
template<typename R, typename... Args, typename... DefArgs> void PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const;
template<typename T> void PushGlobal(const char* name, T&& arg);
template<typename T> void PushGlobal(const String& name, T&& arg);
template<typename T> void PushInstance(const char* tname, const T& instance) const;
template<typename T> void PushInstance(const char* tname, T&& instance) const;
template<typename T, typename... Args> void PushInstance(const char* tname, Args&&... args) const;
void PushInteger(long long value) const;
void PushLightUserdata(void* value) const;
void PushMetatable(const char* str) const;
void PushMetatable(const String& str) const;
void PushNil() const;
void PushNumber(double value) const;
void PushReference(int ref) const;
void PushString(const char* str) const;
void PushString(const char* str, std::size_t size) const;
void PushString(const String& str) const;
void PushTable(std::size_t sequenceElementCount = 0, std::size_t arrayElementCount = 0) const;
void* PushUserdata(std::size_t size) const;
void PushValue(int index) const;
void Remove(int index) const;
void Replace(int index) const;
void SetField(const char* name, int tableIndex = -2) const;
void SetField(const String& name, int tableIndex = -2) const;
void SetGlobal(const char* name);
void SetGlobal(const String& name);
void SetMetatable(const char* tname) const;
void SetMetatable(const String& tname) const;
void SetMetatable(int index) const;
void SetMemoryLimit(std::size_t memoryLimit);
void SetTable(int index = -3) const;
void SetTableRaw(int index = -3) const;
void SetTimeLimit(UInt32 timeLimit);
bool ToBoolean(int index) const;
long long ToInteger(int index, bool* succeeded = nullptr) const;
double ToNumber(int index, bool* succeeded = nullptr) const;
const void* ToPointer(int index) const;
const char* ToString(int index, std::size_t* length = nullptr) const;
void* ToUserdata(int index) const;
void* ToUserdata(int index, const char* tname) const;
void* ToUserdata(int index, const String& tname) const;
LuaState& operator=(const LuaState&) = default;
LuaState& operator=(LuaState&& instance) noexcept;
static int GetIndexOfUpValue(int upValue);
static LuaState GetState(lua_State* internalState);
protected:
LuaState(LuaInstance* instance, lua_State* internalState);
template<typename T> T CheckBounds(int index, long long value) const;
bool Run(int argCount, int resultCount);
static int ProxyFunc(lua_State* internalState);
String m_lastError;
LuaInstance* m_instance;
lua_State* m_state;
};
}
#include <Nazara/Lua/LuaState.inl>
#endif // NAZARA_LUASTATE_HPP

View File

@ -0,0 +1,788 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaState.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Flags.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Math/Algorithm.hpp>
#include <limits>
#include <string>
#include <vector>
#include <type_traits>
namespace Nz
{
inline LuaState::LuaState(LuaInstance* instance, lua_State* internalState) :
m_instance(instance),
m_state(internalState)
{
}
inline lua_State* LuaState::GetInternalState() const
{
return m_state;
}
inline String LuaState::GetLastError() const
{
return m_lastError;
}
// Functions args
inline unsigned int LuaImplQueryArg(const LuaState& instance, int index, bool* arg, TypeTag<bool>)
{
*arg = instance.CheckBoolean(index);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaState& instance, int index, bool* arg, bool defValue, TypeTag<bool>)
{
*arg = instance.CheckBoolean(index, defValue);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaState& instance, int index, std::string* arg, TypeTag<std::string>)
{
std::size_t strLength = 0;
const char* str = instance.CheckString(index, &strLength);
arg->assign(str, strLength);
return 1;
}
inline unsigned int LuaImplQueryArg(const LuaState& instance, int index, String* arg, TypeTag<String>)
{
std::size_t strLength = 0;
const char* str = instance.CheckString(index, &strLength);
arg->Set(str, strLength);
return 1;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
return LuaImplQueryArg(instance, index, reinterpret_cast<UnderlyingT*>(arg), TypeTag<UnderlyingT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, T defValue, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
return LuaImplQueryArg(instance, index, reinterpret_cast<UnderlyingT*>(arg), static_cast<UnderlyingT>(defValue), TypeTag<UnderlyingT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
UnderlyingT pot2Val;
unsigned int ret = LuaImplQueryArg(instance, index, &pot2Val, TypeTag<UnderlyingT>());
*arg = static_cast<T>(IntegralLog2Pot(pot2Val));
return ret;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, T defValue, TypeTag<T>)
{
using UnderlyingT = std::underlying_type_t<T>;
UnderlyingT pot2Val;
unsigned int ret = LuaImplQueryArg(instance, index, &pot2Val, 1U << static_cast<UnderlyingT>(defValue), TypeTag<UnderlyingT>());
*arg = static_cast<T>(IntegralLog2Pot(pot2Val));
return ret;
}
template<typename E>
unsigned int LuaImplQueryArg(const LuaState& instance, int index, Flags<E>* arg, TypeTag<Flags<E>>)
{
*arg = Flags<E>(instance.CheckBoundInteger<UInt32>(index));
return 1;
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, TypeTag<T>)
{
*arg = static_cast<T>(instance.CheckNumber(index));
return 1;
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, T defValue, TypeTag<T>)
{
*arg = static_cast<T>(instance.CheckNumber(index, static_cast<double>(defValue)));
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, TypeTag<T>)
{
*arg = instance.CheckBoundInteger<T>(index);
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, T defValue, TypeTag<T>)
{
*arg = instance.CheckBoundInteger<T>(index, defValue);
return 1;
}
template<typename T>
std::enable_if_t<!std::is_integral<T>::value && !std::is_enum<T>::value && !std::is_floating_point<T>::value, unsigned int> LuaImplQueryArg(const LuaState& instance, int index, T* arg, const T& defValue, TypeTag<T> tag)
{
if (instance.IsValid(index))
return LuaImplQueryArg(instance, index, arg, tag);
else
{
*arg = defValue;
return 1;
}
}
template<typename T>
unsigned int LuaImplQueryArg(const LuaState& instance, int index, T* arg, TypeTag<const T&>)
{
return LuaImplQueryArg(instance, index, arg, TypeTag<T>());
}
template<typename T>
unsigned int LuaImplQueryArg(const LuaState& instance, int index, T* arg, const T& defValue, TypeTag<const T&>)
{
return LuaImplQueryArg(instance, index, arg, defValue, TypeTag<T>());
}
// Function returns
inline int LuaImplReplyVal(const LuaState& instance, bool val, TypeTag<bool>)
{
instance.PushBoolean(val);
return 1;
}
inline int LuaImplReplyVal(const LuaState& instance, double val, TypeTag<double>)
{
instance.PushNumber(val);
return 1;
}
inline int LuaImplReplyVal(const LuaState& instance, float val, TypeTag<float>)
{
instance.PushNumber(val);
return 1;
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && !EnumAsFlags<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<T>)
{
using EnumT = typename std::underlying_type<T>::type;
return LuaImplReplyVal(instance, static_cast<EnumT>(val), TypeTag<EnumT>());
}
template<typename T>
std::enable_if_t<std::is_enum<T>::value && EnumAsFlags<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<T>)
{
Flags<T> flags(val);
return LuaImplReplyVal(instance, flags, TypeTag<decltype(flags)>());
}
template<typename E>
int LuaImplReplyVal(const LuaState& instance, Flags<E> val, TypeTag<Flags<E>>)
{
instance.PushInteger(UInt32(val));
return 1;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<T>)
{
instance.PushInteger(val);
return 1;
}
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value || std::is_enum<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<T&>)
{
return LuaImplReplyVal(instance, val, TypeTag<T>());
}
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value || std::is_enum<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<const T&>)
{
return LuaImplReplyVal(instance, val, TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaState& instance, T val, TypeTag<const T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
int LuaImplReplyVal(const LuaState& instance, T&& val, TypeTag<T&&>)
{
return LuaImplReplyVal(instance, std::forward<T>(val), TypeTag<T>());
}
inline int LuaImplReplyVal(const LuaState& instance, std::string&& val, TypeTag<std::string>)
{
instance.PushString(val.c_str(), val.size());
return 1;
}
template<typename T>
inline int LuaImplReplyVal(const LuaState& instance, std::vector<T>&& valContainer, TypeTag<std::vector<T>>)
{
std::size_t index = 1;
instance.PushTable(valContainer.size());
for (T& val : valContainer)
{
instance.PushInteger(index++);
if (LuaImplReplyVal(instance, std::move(val), TypeTag<T>()) != 1)
{
instance.Error("Couldn't create table: type need more than one place to store");
return 0;
}
instance.SetTable();
}
return 1;
}
inline int LuaImplReplyVal(const LuaState& instance, ByteArray&& val, TypeTag<ByteArray>)
{
instance.PushString(reinterpret_cast<const char*>(val.GetConstBuffer()), val.GetSize());
return 1;
}
inline int LuaImplReplyVal(const LuaState& instance, String&& val, TypeTag<String>)
{
instance.PushString(std::move(val));
return 1;
}
template<typename T1, typename T2>
int LuaImplReplyVal(const LuaState& instance, std::pair<T1, T2>&& val, TypeTag<std::pair<T1, T2>>)
{
int retVal = 0;
retVal += LuaImplReplyVal(instance, std::move(val.first), TypeTag<T1>());
retVal += LuaImplReplyVal(instance, std::move(val.second), TypeTag<T2>());
return retVal;
}
template<bool HasDefault>
struct LuaImplArgProcesser;
template<>
struct LuaImplArgProcesser<true>
{
template<std::size_t N, std::size_t FirstDefArg, typename ArgType, typename ArgContainer, typename DefArgContainer>
static unsigned int Process(const LuaState& instance, unsigned int argIndex, ArgContainer& args, DefArgContainer& defArgs)
{
return LuaImplQueryArg(instance, argIndex, &std::get<N>(args), std::get<FirstDefArg + std::tuple_size<DefArgContainer>() - N - 1>(defArgs), TypeTag<ArgType>());
}
};
template<>
struct LuaImplArgProcesser<false>
{
template<std::size_t N, std::size_t FirstDefArg, typename ArgType, typename ArgContainer, typename DefArgContainer>
static unsigned int Process(const LuaState& instance, unsigned int argIndex, ArgContainer& args, DefArgContainer& defArgs)
{
NazaraUnused(defArgs);
return LuaImplQueryArg(instance, argIndex, &std::get<N>(args), TypeTag<ArgType>());
}
};
template<typename... Args>
class LuaImplFunctionProxy
{
public:
template<typename... DefArgs>
class Impl
{
static constexpr std::size_t ArgCount = sizeof...(Args);
static constexpr std::size_t DefArgCount = sizeof...(DefArgs);
static_assert(ArgCount >= DefArgCount, "There cannot be more default arguments than argument");
static constexpr std::size_t FirstDefArg = ArgCount - DefArgCount;
public:
Impl(DefArgs... defArgs) :
m_defaultArgs(std::forward<DefArgs>(defArgs)...)
{
}
void ProcessArguments(const LuaState& instance) const
{
m_index = 1;
ProcessArgs<0, Args...>(instance);
}
int Invoke(const LuaState& instance, void(*func)(Args...)) const
{
NazaraUnused(instance);
Apply(func, m_args);
return 0;
}
template<typename Ret>
int Invoke(const LuaState& instance, Ret(*func)(Args...)) const
{
return LuaImplReplyVal(instance, std::move(Apply(func, m_args)), TypeTag<decltype(Apply(func, m_args))>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;
template<std::size_t N>
void ProcessArgs(const LuaState& instance) const
{
NazaraUnused(instance);
// No argument to process
}
template<std::size_t N, typename ArgType>
void ProcessArgs(const LuaState& instance) const
{
LuaImplArgProcesser<(N >= FirstDefArg)>::template Process<N, FirstDefArg, ArgType>(instance, m_index, m_args, m_defaultArgs);
}
template<std::size_t N, typename ArgType1, typename ArgType2, typename... Rest>
void ProcessArgs(const LuaState& instance) const
{
ProcessArgs<N, ArgType1>(instance);
ProcessArgs<N + 1, ArgType2, Rest...>(instance);
}
mutable ArgContainer m_args;
DefArgContainer m_defaultArgs;
mutable unsigned int m_index;
};
};
template<typename... Args>
class LuaImplMethodProxy
{
public:
template<typename... DefArgs>
class Impl
{
static constexpr std::size_t ArgCount = sizeof...(Args);
static constexpr std::size_t DefArgCount = sizeof...(DefArgs);
static_assert(ArgCount >= DefArgCount, "There cannot be more default arguments than argument");
static constexpr std::size_t FirstDefArg = ArgCount - DefArgCount;
public:
Impl(DefArgs... defArgs) :
m_defaultArgs(std::forward<DefArgs>(defArgs)...)
{
}
void ProcessArguments(const LuaState& instance) const
{
m_index = 2; //< 1 being the instance
ProcessArgs<0, Args...>(instance);
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, T& object, void(P::*func)(Args...)) const
{
NazaraUnused(instance);
Apply(object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, T& object, Ret(P::*func)(Args...)) const
{
return LuaImplReplyVal(instance, std::move(Apply(object, func, m_args)), TypeTag<decltype(Apply(object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, T& object, T&(P::*func)(Args...)) const
{
T& r = Apply(object, func, m_args);
if (&r == &object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, const T& object, void(P::*func)(Args...) const) const
{
NazaraUnused(instance);
Apply(object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, const T& object, Ret(P::*func)(Args...) const) const
{
return LuaImplReplyVal(instance, std::move(Apply(object, func, m_args)), TypeTag<decltype(Apply(object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, T>::value, int> Invoke(const LuaState& instance, const T& object, const T&(P::*func)(Args...) const) const
{
const T& r = Apply(object, func, m_args);
if (&r == &object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, T& object, void(P::*func)(Args...)) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
Apply(*object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, T& object, Ret(P::*func)(Args...)) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
return LuaImplReplyVal(instance, std::move(Apply(*object, func, m_args)), TypeTag<decltype(Apply(*object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, T& object, typename PointedType<T>::type&(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
const typename PointedType<T>::type& r = Apply(*object, func, m_args);
if (&r == &*object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, const T& object, void(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
Apply(*object, func, m_args);
return 0;
}
template<typename T, typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, const T& object, Ret(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
return LuaImplReplyVal(instance, std::move(Apply(*object, func, m_args)), TypeTag<decltype(Apply(*object, func, m_args))>());
}
template<typename T, typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(const LuaState& instance, const T& object, const typename PointedType<T>::type&(P::*func)(Args...) const) const
{
if (!object)
{
instance.Error("Invalid object");
return 0;
}
const typename PointedType<T>::type& r = Apply(*object, func, m_args);
if (&r == &*object)
{
instance.PushValue(1); //< Userdata
return 1;
}
else
return LuaImplReplyVal(instance, r, TypeTag<T&>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;
template<std::size_t N>
void ProcessArgs(const LuaState& instance) const
{
NazaraUnused(instance);
// No argument to process
}
template<std::size_t N, typename ArgType>
void ProcessArgs(const LuaState& instance) const
{
m_index += LuaImplArgProcesser<(N >= FirstDefArg)>::template Process<N, FirstDefArg, ArgType>(instance, m_index, m_args, m_defaultArgs);
}
template<std::size_t N, typename ArgType1, typename ArgType2, typename... Rest>
void ProcessArgs(const LuaState& instance) const
{
ProcessArgs<N, ArgType1>(instance);
ProcessArgs<N + 1, ArgType2, Rest...>(instance);
}
mutable ArgContainer m_args;
DefArgContainer m_defaultArgs;
mutable unsigned int m_index;
};
};
template<typename T>
T LuaState::Check(int* index) const
{
NazaraAssert(index, "Invalid index pointer");
T object;
*index += LuaImplQueryArg(*this, *index, &object, TypeTag<T>());
return object;
}
template<typename T>
T LuaState::Check(int* index, T defValue) const
{
NazaraAssert(index, "Invalid index pointer");
T object;
*index += LuaImplQueryArg(*this, *index, &object, defValue, TypeTag<T>());
return object;
}
template<typename T>
inline T LuaState::CheckBoundInteger(int index) const
{
return CheckBounds<T>(index, CheckInteger(index));
}
template<typename T>
inline T LuaState::CheckBoundInteger(int index, T defValue) const
{
return CheckBounds<T>(index, CheckInteger(index, defValue));
}
template<typename T>
T LuaState::CheckField(const char* fieldName, int tableIndex) const
{
T object;
GetField(fieldName, tableIndex);
tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaState::CheckField(const String& fieldName, int tableIndex) const
{
return CheckField<T>(fieldName.GetConstBuffer(), tableIndex);
}
template<typename T>
T LuaState::CheckField(const char* fieldName, T defValue, int tableIndex) const
{
T object;
GetField(fieldName, tableIndex);
tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaState::CheckField(const String& fieldName, T defValue, int tableIndex) const
{
return CheckField<T>(fieldName.GetConstBuffer(), defValue, tableIndex);
}
template<typename T>
T LuaState::CheckGlobal(const char* fieldName) const
{
T object;
GetGlobal(fieldName);
LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaState::CheckGlobal(const String& fieldName) const
{
return CheckGlobal<T>(fieldName.GetConstBuffer());
}
template<typename T>
T LuaState::CheckGlobal(const char* fieldName, T defValue) const
{
T object;
GetGlobal(fieldName);
LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
Pop();
return object;
}
template<typename T>
T LuaState::CheckGlobal(const String& fieldName, T defValue) const
{
return CheckGlobal<T>(fieldName.GetConstBuffer(), defValue);
}
template<typename T>
int LuaState::Push(T arg) const
{
return LuaImplReplyVal(*this, std::move(arg), TypeTag<T>());
}
template<typename T, typename T2, typename... Args>
int LuaState::Push(T firstArg, T2 secondArg, Args... args) const
{
int valCount = 0;
valCount += Push(std::move(firstArg));
valCount += Push(secondArg, std::forward<Args>(args)...);
return valCount;
}
template<typename T>
void LuaState::PushField(const char* name, T&& arg, int tableIndex) const
{
Push<T>(std::forward<T>(arg));
SetField(name, tableIndex);
}
template<typename T>
void LuaState::PushField(const String& name, T&& arg, int tableIndex) const
{
PushField(name.GetConstBuffer(), std::forward<T>(arg), tableIndex);
}
template<typename R, typename... Args, typename... DefArgs>
void LuaState::PushFunction(R(*func)(Args...), DefArgs&&... defArgs) const
{
typename LuaImplFunctionProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
PushFunction([func, handler] (LuaState& lua) -> int
{
handler.ProcessArguments(lua);
return handler.Invoke(lua, func);
});
}
template<typename T>
void LuaState::PushGlobal(const char* name, T&& arg)
{
Push<T>(std::forward<T>(arg));
SetGlobal(name);
}
template<typename T>
void LuaState::PushGlobal(const String& name, T&& arg)
{
PushGlobal(name.GetConstBuffer(), std::forward<T>(arg));
}
template<typename T>
void LuaState::PushInstance(const char* tname, const T& instance) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, instance);
SetMetatable(tname);
}
template<typename T>
void LuaState::PushInstance(const char* tname, T&& instance) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, std::move(instance));
SetMetatable(tname);
}
template<typename T, typename... Args>
void LuaState::PushInstance(const char* tname, Args&&... args) const
{
T* userdata = static_cast<T*>(PushUserdata(sizeof(T)));
PlacementNew(userdata, std::forward<Args>(args)...);
SetMetatable(tname);
}
template<typename T>
T LuaState::CheckBounds(int index, long long value) const
{
constexpr long long minBounds = std::numeric_limits<T>::min();
constexpr long long maxBounds = std::numeric_limits<T>::max();
if (value < minBounds || value > maxBounds)
{
Nz::StringStream stream;
stream << "Argument #" << index << " is outside value range [" << minBounds << ", " << maxBounds << "] (" << value << ')';
Error(stream);
}
return static_cast<T>(value);
}
}

View File

@ -21,117 +21,16 @@ namespace Nz
{
namespace
{
LuaType FromLuaType(int type)
int AtPanic(lua_State* internalState)
{
switch (type)
{
case LUA_TBOOLEAN:
return LuaType_Boolean;
case LUA_TFUNCTION:
return LuaType_Function;
case LUA_TLIGHTUSERDATA:
return LuaType_LightUserdata;
case LUA_TNIL:
return LuaType_Nil;
case LUA_TNONE:
return LuaType_None;
case LUA_TNUMBER:
return LuaType_Number;
case LUA_TSTRING:
return LuaType_String;
case LUA_TTABLE:
return LuaType_Table;
case LUA_TTHREAD:
return LuaType_Thread;
case LUA_TUSERDATA:
return LuaType_Userdata;
default:
return LuaType_None;
}
}
struct StreamData
{
Stream* stream;
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
};
int AtPanic(lua_State* state)
{
String lastError(lua_tostring(state, -1));
String lastError(lua_tostring(internalState, -1));
throw std::runtime_error("Lua panic: " + lastError);
}
const char* StreamReader(lua_State* state, void* data, std::size_t* size)
{
NazaraUnused(state);
StreamData* streamData = static_cast<StreamData*>(data);
if (streamData->stream->EndOfStream())
return nullptr;
else
{
*size = streamData->stream->Read(streamData->buffer, NAZARA_CORE_FILE_BUFFERSIZE);
return streamData->buffer;
}
}
int s_comparisons[] = {
LUA_OPEQ, // LuaComparison_Equality
LUA_OPLT, // LuaComparison_Less
LUA_OPLE // LuaComparison_LessOrEqual
};
static_assert(sizeof(s_comparisons)/sizeof(int) == LuaComparison_Max+1, "Lua comparison array is incomplete");
int s_operations[] = {
LUA_OPADD, // LuaOperation_Addition
LUA_OPBAND, // LuaOperation_BitwiseAnd
LUA_OPSHL, // LuaOperation_BitwiseLeftShift
LUA_OPBNOT, // LuaOperation_BitwiseNot
LUA_OPBOR, // LuaOperation_BitwiseOr
LUA_OPSHR, // LuaOperation_BitwiseRightShift
LUA_OPBXOR, // LuaOperation_BitwiseXOr
LUA_OPDIV, // LuaOperation_Division
LUA_OPPOW, // LuaOperation_Exponentiation
LUA_OPIDIV, // LuaOperation_FloorDivision
LUA_OPMUL, // LuaOperation_Multiplication
LUA_OPMOD, // LuaOperation_Modulo
LUA_OPUNM, // LuaOperation_Negation
LUA_OPSUB // LuaOperation_Substraction
};
static_assert(sizeof(s_operations)/sizeof(int) == LuaOperation_Max+1, "Lua operation array is incomplete");
int s_types[] = {
LUA_TBOOLEAN, // LuaType_Boolean
LUA_TFUNCTION, // LuaType_Function
LUA_TLIGHTUSERDATA, // LuaType_LightUserdata
LUA_TNIL, // LuaType_Nil
LUA_TNUMBER, // LuaType_Number
LUA_TNONE, // LuaType_None
LUA_TSTRING, // LuaType_String
LUA_TTABLE, // LuaType_Table
LUA_TTHREAD, // LuaType_Thread
LUA_TUSERDATA // LuaType_Userdata
};
static_assert(sizeof(s_types)/sizeof(int) == LuaType_Max+1, "Lua type array is incomplete");
}
LuaInstance::LuaInstance() :
LuaState(this, lua_newstate(MemoryAllocator, this)),
m_memoryLimit(0),
m_memoryUsage(0),
m_timeLimit(1000),
@ -143,744 +42,12 @@ namespace Nz
luaL_openlibs(m_state);
}
LuaInstance::LuaInstance(LuaInstance&& instance) noexcept :
m_memoryLimit(instance.m_memoryLimit),
m_memoryUsage(instance.m_memoryUsage),
m_timeLimit(instance.m_timeLimit),
m_clock(std::move(instance.m_clock)),
m_lastError(std::move(instance.m_lastError)),
m_state(instance.m_state),
m_level(instance.m_level)
{
instance.m_state = nullptr;
lua_setallocf(m_state, MemoryAllocator, this);
}
LuaInstance::~LuaInstance()
{
if (m_state)
lua_close(m_state);
}
void LuaInstance::ArgCheck(bool condition, unsigned int argNum, const char* error) const
{
luaL_argcheck(m_state, condition, argNum, error);
}
void LuaInstance::ArgCheck(bool condition, unsigned int argNum, const String& error) const
{
luaL_argcheck(m_state, condition, argNum, error.GetConstBuffer());
}
int LuaInstance::ArgError(unsigned int argNum, const char* error) const
{
return luaL_argerror(m_state, argNum, error);
}
int LuaInstance::ArgError(unsigned int argNum, const String& error) const
{
return luaL_argerror(m_state, argNum, error.GetConstBuffer());
}
bool LuaInstance::Call(unsigned int argCount)
{
return Run(argCount, LUA_MULTRET);
}
bool LuaInstance::Call(unsigned int argCount, unsigned int resultCount)
{
return Run(argCount, resultCount);
}
void LuaInstance::CheckAny(int index) const
{
luaL_checkany(m_state, index);
}
bool LuaInstance::CheckBoolean(int index) const
{
if (lua_isnoneornil(m_state, index))
{
const char* msg = lua_pushfstring(m_state, "%s expected, got %s", lua_typename(m_state, LUA_TBOOLEAN), luaL_typename(m_state, index));
luaL_argerror(m_state, index, msg); // Lance une exception
return false;
}
return lua_toboolean(m_state, index) != 0;
}
bool LuaInstance::CheckBoolean(int index, bool defValue) const
{
if (lua_isnoneornil(m_state, index))
return defValue;
return lua_toboolean(m_state, index) != 0;
}
long long LuaInstance::CheckInteger(int index) const
{
return luaL_checkinteger(m_state, index);
}
long long LuaInstance::CheckInteger(int index, long long defValue) const
{
return luaL_optinteger(m_state, index, defValue);
}
double LuaInstance::CheckNumber(int index) const
{
return luaL_checknumber(m_state, index);
}
double LuaInstance::CheckNumber(int index, double defValue) const
{
return luaL_optnumber(m_state, index, defValue);
}
void LuaInstance::CheckStack(int space, const char* error) const
{
luaL_checkstack(m_state, space, error);
}
void LuaInstance::CheckStack(int space, const String& error) const
{
CheckStack(space, error.GetConstBuffer());
}
const char* LuaInstance::CheckString(int index, std::size_t* length) const
{
return luaL_checklstring(m_state, index, length);
}
const char* LuaInstance::CheckString(int index, const char* defValue, std::size_t* length) const
{
return luaL_optlstring(m_state, index, defValue, length);
}
void LuaInstance::CheckType(int index, LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return;
}
#endif
luaL_checktype(m_state, index, s_types[type]);
}
void* LuaInstance::CheckUserdata(int index, const char* tname) const
{
return luaL_checkudata(m_state, index, tname);
}
void* LuaInstance::CheckUserdata(int index, const String& tname) const
{
return luaL_checkudata(m_state, index, tname.GetConstBuffer());
}
bool LuaInstance::Compare(int index1, int index2, LuaComparison comparison) const
{
#ifdef NAZARA_DEBUG
if (comparison > LuaComparison_Max)
{
NazaraError("Lua comparison out of enum");
return false;
}
#endif
return (lua_compare(m_state, index1, index2, s_comparisons[comparison]) != 0);
}
void LuaInstance::Compute(LuaOperation operation) const
{
#ifdef NAZARA_DEBUG
if (operation > LuaOperation_Max)
{
NazaraError("Lua operation out of enum");
return;
}
#endif
lua_arith(m_state, s_operations[operation]);
}
void LuaInstance::Concatenate(int count) const
{
lua_concat(m_state, count);
}
int LuaInstance::CreateReference()
{
return luaL_ref(m_state, LUA_REGISTRYINDEX);
}
void LuaInstance::DestroyReference(int ref)
{
luaL_unref(m_state, LUA_REGISTRYINDEX, ref);
}
String LuaInstance::DumpStack() const
{
StringStream stream;
unsigned int stackTop = GetStackTop();
stream << stackTop << " entries\n";
for (unsigned int i = 1; i <= stackTop; ++i)
{
stream << i << ": ";
switch (GetType(i))
{
case LuaType_Boolean:
stream << "Boolean(" << ToBoolean(i) << ')';
break;
case LuaType_Function:
stream << "Function(" << ToPointer(i) << ')';
break;
case LuaType_LightUserdata:
case LuaType_Userdata:
stream << "Userdata(" << ToUserdata(i) << ')';
break;
case LuaType_Nil:
stream << "Nil";
break;
case LuaType_None:
stream << "None";
break;
case LuaType_Number:
stream << "Number(" << ToNumber(i) << ')';
break;
case LuaType_String:
stream << "String(" << ToString(i) << ')';
break;
case LuaType_Table:
stream << "Table(" << ToPointer(i) << ')';
break;
case LuaType_Thread:
stream << "Thread(" << ToPointer(i) << ')';
break;
default:
stream << "Unknown(" << ToPointer(i) << ')';
break;
}
stream << '\n';
}
return stream.ToString();
}
void LuaInstance::Error(const char* message) const
{
luaL_error(m_state, message);
}
void LuaInstance::Error(const String& message) const
{
luaL_error(m_state, message.GetConstBuffer());
}
bool LuaInstance::Execute(const String& code)
{
if (code.IsEmpty())
return true;
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return Run(0, 0);
}
bool LuaInstance::ExecuteFromFile(const String& filePath)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
return false;
}
std::size_t length = static_cast<std::size_t>(file.GetSize());
String source(length, '\0');
if (file.Read(&source[0], length) != length)
{
NazaraError("Failed to read file");
return false;
}
file.Close();
return Execute(source);
}
bool LuaInstance::ExecuteFromMemory(const void* data, std::size_t size)
{
MemoryView stream(data, size);
return ExecuteFromStream(stream);
}
bool LuaInstance::ExecuteFromStream(Stream& stream)
{
StreamData data;
data.stream = &stream;
if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return Run(0, 0);
}
int LuaInstance::GetAbsIndex(int index) const
{
return lua_absindex(m_state, index);
}
LuaType LuaInstance::GetField(const char* fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName));
}
LuaType LuaInstance::GetField(const String& fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName.GetConstBuffer()));
}
LuaType LuaInstance::GetGlobal(const char* name) const
{
return FromLuaType(lua_getglobal(m_state, name));
}
LuaType LuaInstance::GetGlobal(const String& name) const
{
return FromLuaType(lua_getglobal(m_state, name.GetConstBuffer()));
}
LuaType LuaInstance::GetMetatable(const char* tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname));
}
LuaType LuaInstance::GetMetatable(const String& tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname.GetConstBuffer()));
}
bool LuaInstance::GetMetatable(int index) const
{
return lua_getmetatable(m_state, index) != 0;
}
unsigned int LuaInstance::GetStackTop() const
{
return static_cast<int>(lua_gettop(m_state));
}
LuaType LuaInstance::GetTable(int index) const
{
return FromLuaType(lua_gettable(m_state, index));
}
LuaType LuaInstance::GetTableRaw(int index) const
{
return FromLuaType(lua_rawget(m_state, index));
}
LuaType LuaInstance::GetType(int index) const
{
return FromLuaType(lua_type(m_state, index));
}
const char* LuaInstance::GetTypeName(LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return nullptr;
}
#endif
return lua_typename(m_state, s_types[type]);
}
void LuaInstance::Insert(int index) const
{
lua_insert(m_state, index);
}
bool LuaInstance::IsOfType(int index, LuaType type) const
{
switch (type)
{
case LuaType_Boolean:
return lua_isboolean(m_state, index) != 0;
case LuaType_Function:
return lua_isfunction(m_state, index) != 0;
case LuaType_LightUserdata:
return lua_islightuserdata(m_state, index) != 0;
case LuaType_Nil:
return lua_isnil(m_state, index) != 0;
case LuaType_None:
return lua_isnone(m_state, index) != 0;
case LuaType_Number:
return lua_isnumber(m_state, index) != 0;
case LuaType_String:
return lua_isstring(m_state, index) != 0;
case LuaType_Table:
return lua_istable(m_state, index) != 0;
case LuaType_Thread:
return lua_isthread(m_state, index) != 0;
case LuaType_Userdata:
return lua_isuserdata(m_state, index) != 0;
}
NazaraError("Lua type not handled (0x" + String::Number(type, 16) + ')');
return false;
}
bool LuaInstance::IsOfType(int index, const char* tname) const
{
void* ud = luaL_testudata(m_state, index, tname);
return ud != nullptr;
}
bool LuaInstance::IsOfType(int index, const String& tname) const
{
return IsOfType(index, tname.GetConstBuffer());
}
bool LuaInstance::IsValid(int index) const
{
return lua_isnoneornil(m_state, index) == 0;
}
long long LuaInstance::Length(int index) const
{
return luaL_len(m_state, index);
}
std::size_t LuaInstance::LengthRaw(int index) const
{
return lua_rawlen(m_state, index);
}
void LuaInstance::MoveTo(LuaInstance* instance, int n) const
{
lua_xmove(m_state, instance->m_state, n);
}
bool LuaInstance::NewMetatable(const char* str)
{
return luaL_newmetatable(m_state, str) != 0;
}
bool LuaInstance::NewMetatable(const String& str)
{
return luaL_newmetatable(m_state, str.GetConstBuffer()) != 0;
}
bool LuaInstance::Next(int index) const
{
return lua_next(m_state, index) != 0;
}
void LuaInstance::Pop(unsigned int n) const
{
lua_pop(m_state, static_cast<int>(n));
}
void LuaInstance::PushBoolean(bool value) const
{
lua_pushboolean(m_state, (value) ? 1 : 0);
}
void LuaInstance::PushCFunction(LuaCFunction func, unsigned int upvalueCount) const
{
lua_pushcclosure(m_state, func, upvalueCount);
}
void LuaInstance::PushFunction(LuaFunction func) const
{
LuaFunction* luaFunc = static_cast<LuaFunction*>(lua_newuserdata(m_state, sizeof(LuaFunction)));
PlacementNew(luaFunc, std::move(func));
lua_pushcclosure(m_state, ProxyFunc, 1);
}
void LuaInstance::PushInteger(long long value) const
{
lua_pushinteger(m_state, value);
}
void LuaInstance::PushLightUserdata(void* value) const
{
lua_pushlightuserdata(m_state, value);
}
void LuaInstance::PushMetatable(const char* str) const
{
luaL_getmetatable(m_state, str);
}
void LuaInstance::PushMetatable(const String& str) const
{
luaL_getmetatable(m_state, str.GetConstBuffer());
}
void LuaInstance::PushNil() const
{
lua_pushnil(m_state);
}
void LuaInstance::PushNumber(double value) const
{
lua_pushnumber(m_state, value);
}
void LuaInstance::PushReference(int ref) const
{
lua_rawgeti(m_state, LUA_REGISTRYINDEX, ref);
}
void LuaInstance::PushString(const char* str) const
{
lua_pushstring(m_state, str);
}
void LuaInstance::PushString(const char* str, std::size_t size) const
{
lua_pushlstring(m_state, str, size);
}
void LuaInstance::PushString(const String& str) const
{
lua_pushlstring(m_state, str.GetConstBuffer(), str.GetSize());
}
void LuaInstance::PushTable(std::size_t sequenceElementCount, std::size_t arrayElementCount) const
{
constexpr std::size_t maxInt = std::numeric_limits<int>::max();
lua_createtable(m_state, static_cast<int>(std::min(sequenceElementCount, maxInt)), static_cast<int>(std::min(arrayElementCount, maxInt)));
}
void* LuaInstance::PushUserdata(std::size_t size) const
{
return lua_newuserdata(m_state, size);
}
void LuaInstance::PushValue(int index) const
{
lua_pushvalue(m_state, index);
}
void LuaInstance::Remove(int index) const
{
lua_remove(m_state, index);
}
void LuaInstance::Replace(int index) const
{
lua_replace(m_state, index);
}
void LuaInstance::SetField(const char* name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name);
}
void LuaInstance::SetField(const String& name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name.GetConstBuffer());
}
void LuaInstance::SetGlobal(const char* name)
{
lua_setglobal(m_state, name);
}
void LuaInstance::SetGlobal(const String& name)
{
lua_setglobal(m_state, name.GetConstBuffer());
}
void LuaInstance::SetMetatable(const char* tname) const
{
luaL_setmetatable(m_state, tname);
}
void LuaInstance::SetMetatable(const String& tname) const
{
luaL_setmetatable(m_state, tname.GetConstBuffer());
}
void LuaInstance::SetMetatable(int index) const
{
lua_setmetatable(m_state, index);
}
void LuaInstance::SetMemoryLimit(std::size_t memoryLimit)
{
m_memoryLimit = memoryLimit;
}
void LuaInstance::SetTable(int index) const
{
lua_settable(m_state, index);
}
void LuaInstance::SetTableRaw(int index) const
{
lua_rawset(m_state, index);
}
void LuaInstance::SetTimeLimit(UInt32 timeLimit)
{
if (m_timeLimit != timeLimit)
{
if (m_timeLimit == 0)
lua_sethook(m_state, TimeLimiter, LUA_MASKCOUNT, 1000);
else if (timeLimit == 0)
lua_sethook(m_state, TimeLimiter, 0, 1000);
m_timeLimit = timeLimit;
}
}
bool LuaInstance::ToBoolean(int index) const
{
return lua_toboolean(m_state, index) != 0;
}
long long LuaInstance::ToInteger(int index, bool* succeeded) const
{
int success;
long long result = lua_tointegerx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
double LuaInstance::ToNumber(int index, bool* succeeded) const
{
int success;
double result = lua_tonumberx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
const void* LuaInstance::ToPointer(int index) const
{
return lua_topointer(m_state, index);
}
const char* LuaInstance::ToString(int index, std::size_t* length) const
{
return lua_tolstring(m_state, index, length);
}
void* LuaInstance::ToUserdata(int index) const
{
return lua_touserdata(m_state, index);
}
void* LuaInstance::ToUserdata(int index, const char* tname) const
{
return luaL_testudata(m_state, index, tname);
}
void* LuaInstance::ToUserdata(int index, const String& tname) const
{
return luaL_testudata(m_state, index, tname.GetConstBuffer());
}
LuaInstance& LuaInstance::operator=(LuaInstance&& instance) noexcept
{
m_clock = std::move(instance.m_clock);
m_lastError = std::move(instance.m_lastError);
m_level = instance.m_level;
m_memoryLimit = instance.m_memoryLimit;
m_memoryUsage = instance.m_memoryUsage;
m_state = instance.m_state;
m_timeLimit = instance.m_timeLimit;
instance.m_state = nullptr;
// Update allocator pointer
lua_setallocf(m_state, MemoryAllocator, this);
return *this;
}
int LuaInstance::GetIndexOfUpValue(int upValue)
{
return lua_upvalueindex(upValue);
}
LuaInstance* LuaInstance::GetInstance(lua_State* state)
{
LuaInstance* instance;
lua_getallocf(state, reinterpret_cast<void**>(&instance));
return instance;
}
bool LuaInstance::Run(int argCount, int resultCount)
{
if (m_level++ == 0)
m_clock.Restart();
int status = lua_pcall(m_state, argCount, resultCount, 0);
m_level--;
if (status != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
void* LuaInstance::MemoryAllocator(void* ud, void* ptr, std::size_t osize, std::size_t nsize)
{
LuaInstance* instance = static_cast<LuaInstance*>(ud);
@ -912,18 +79,14 @@ namespace Nz
}
}
int LuaInstance::ProxyFunc(lua_State* state)
{
LuaFunction& func = *static_cast<LuaFunction*>(lua_touserdata(state, lua_upvalueindex(1)));
return func(*GetInstance(state));
}
void LuaInstance::TimeLimiter(lua_State* state, lua_Debug* debug)
void LuaInstance::TimeLimiter(lua_State* internalState, lua_Debug* debug)
{
NazaraUnused(debug);
LuaInstance* instance = GetInstance(state);
LuaInstance* instance;
lua_getallocf(internalState, reinterpret_cast<void**>(&instance));
if (instance->m_clock.GetMilliseconds() > instance->m_timeLimit)
luaL_error(state, "maximum execution time exceeded");
luaL_error(internalState, "maximum execution time exceeded");
}
}

834
src/Nazara/Lua/LuaState.cpp Normal file
View File

@ -0,0 +1,834 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaState.hpp>
#include <Lua/lauxlib.h>
#include <Lua/lua.h>
#include <Lua/lualib.h>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Core/MemoryView.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <cstdlib>
#include <stdexcept>
#include <unordered_map>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
{
namespace
{
LuaType FromLuaType(int type)
{
switch (type)
{
case LUA_TBOOLEAN:
return LuaType_Boolean;
case LUA_TFUNCTION:
return LuaType_Function;
case LUA_TLIGHTUSERDATA:
return LuaType_LightUserdata;
case LUA_TNIL:
return LuaType_Nil;
case LUA_TNONE:
return LuaType_None;
case LUA_TNUMBER:
return LuaType_Number;
case LUA_TSTRING:
return LuaType_String;
case LUA_TTABLE:
return LuaType_Table;
case LUA_TTHREAD:
return LuaType_Thread;
case LUA_TUSERDATA:
return LuaType_Userdata;
default:
return LuaType_None;
}
}
struct StreamData
{
Stream* stream;
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
};
const char* StreamReader(lua_State* internalState, void* data, std::size_t* size)
{
NazaraUnused(internalState);
StreamData* streamData = static_cast<StreamData*>(data);
if (streamData->stream->EndOfStream())
return nullptr;
else
{
*size = streamData->stream->Read(streamData->buffer, NAZARA_CORE_FILE_BUFFERSIZE);
return streamData->buffer;
}
}
int s_comparisons[] = {
LUA_OPEQ, // LuaComparison_Equality
LUA_OPLT, // LuaComparison_Less
LUA_OPLE // LuaComparison_LessOrEqual
};
static_assert(sizeof(s_comparisons)/sizeof(int) == LuaComparison_Max+1, "Lua comparison array is incomplete");
int s_operations[] = {
LUA_OPADD, // LuaOperation_Addition
LUA_OPBAND, // LuaOperation_BitwiseAnd
LUA_OPSHL, // LuaOperation_BitwiseLeftShift
LUA_OPBNOT, // LuaOperation_BitwiseNot
LUA_OPBOR, // LuaOperation_BitwiseOr
LUA_OPSHR, // LuaOperation_BitwiseRightShift
LUA_OPBXOR, // LuaOperation_BitwiseXOr
LUA_OPDIV, // LuaOperation_Division
LUA_OPPOW, // LuaOperation_Exponentiation
LUA_OPIDIV, // LuaOperation_FloorDivision
LUA_OPMUL, // LuaOperation_Multiplication
LUA_OPMOD, // LuaOperation_Modulo
LUA_OPUNM, // LuaOperation_Negation
LUA_OPSUB // LuaOperation_Substraction
};
static_assert(sizeof(s_operations)/sizeof(int) == LuaOperation_Max+1, "Lua operation array is incomplete");
int s_types[] = {
LUA_TBOOLEAN, // LuaType_Boolean
LUA_TFUNCTION, // LuaType_Function
LUA_TLIGHTUSERDATA, // LuaType_LightUserdata
LUA_TNIL, // LuaType_Nil
LUA_TNUMBER, // LuaType_Number
LUA_TNONE, // LuaType_None
LUA_TSTRING, // LuaType_String
LUA_TTABLE, // LuaType_Table
LUA_TTHREAD, // LuaType_Thread
LUA_TUSERDATA // LuaType_Userdata
};
static_assert(sizeof(s_types)/sizeof(int) == LuaType_Max+1, "Lua type array is incomplete");
}
LuaState::LuaState(LuaState&& state) noexcept :
m_lastError(state.m_lastError),
m_instance(state.m_instance),
m_state(state.m_state)
{
}
void LuaState::ArgCheck(bool condition, unsigned int argNum, const char* error) const
{
luaL_argcheck(m_state, condition, argNum, error);
}
void LuaState::ArgCheck(bool condition, unsigned int argNum, const String& error) const
{
luaL_argcheck(m_state, condition, argNum, error.GetConstBuffer());
}
int LuaState::ArgError(unsigned int argNum, const char* error) const
{
return luaL_argerror(m_state, argNum, error);
}
int LuaState::ArgError(unsigned int argNum, const String& error) const
{
return luaL_argerror(m_state, argNum, error.GetConstBuffer());
}
bool LuaState::Call(unsigned int argCount)
{
return Run(argCount, LUA_MULTRET);
}
bool LuaState::Call(unsigned int argCount, unsigned int resultCount)
{
return Run(argCount, resultCount);
}
void LuaState::CheckAny(int index) const
{
luaL_checkany(m_state, index);
}
bool LuaState::CheckBoolean(int index) const
{
if (lua_isnoneornil(m_state, index))
{
const char* msg = lua_pushfstring(m_state, "%s expected, got %s", lua_typename(m_state, LUA_TBOOLEAN), luaL_typename(m_state, index));
luaL_argerror(m_state, index, msg); // Lance une exception
return false;
}
return lua_toboolean(m_state, index) != 0;
}
bool LuaState::CheckBoolean(int index, bool defValue) const
{
if (lua_isnoneornil(m_state, index))
return defValue;
return lua_toboolean(m_state, index) != 0;
}
long long LuaState::CheckInteger(int index) const
{
return luaL_checkinteger(m_state, index);
}
long long LuaState::CheckInteger(int index, long long defValue) const
{
return luaL_optinteger(m_state, index, defValue);
}
double LuaState::CheckNumber(int index) const
{
return luaL_checknumber(m_state, index);
}
double LuaState::CheckNumber(int index, double defValue) const
{
return luaL_optnumber(m_state, index, defValue);
}
void LuaState::CheckStack(int space, const char* error) const
{
luaL_checkstack(m_state, space, error);
}
void LuaState::CheckStack(int space, const String& error) const
{
CheckStack(space, error.GetConstBuffer());
}
const char* LuaState::CheckString(int index, std::size_t* length) const
{
return luaL_checklstring(m_state, index, length);
}
const char* LuaState::CheckString(int index, const char* defValue, std::size_t* length) const
{
return luaL_optlstring(m_state, index, defValue, length);
}
void LuaState::CheckType(int index, LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return;
}
#endif
luaL_checktype(m_state, index, s_types[type]);
}
void* LuaState::CheckUserdata(int index, const char* tname) const
{
return luaL_checkudata(m_state, index, tname);
}
void* LuaState::CheckUserdata(int index, const String& tname) const
{
return luaL_checkudata(m_state, index, tname.GetConstBuffer());
}
bool LuaState::Compare(int index1, int index2, LuaComparison comparison) const
{
#ifdef NAZARA_DEBUG
if (comparison > LuaComparison_Max)
{
NazaraError("Lua comparison out of enum");
return false;
}
#endif
return (lua_compare(m_state, index1, index2, s_comparisons[comparison]) != 0);
}
void LuaState::Compute(LuaOperation operation) const
{
#ifdef NAZARA_DEBUG
if (operation > LuaOperation_Max)
{
NazaraError("Lua operation out of enum");
return;
}
#endif
lua_arith(m_state, s_operations[operation]);
}
void LuaState::Concatenate(int count) const
{
lua_concat(m_state, count);
}
int LuaState::CreateReference()
{
return luaL_ref(m_state, LUA_REGISTRYINDEX);
}
void LuaState::DestroyReference(int ref)
{
luaL_unref(m_state, LUA_REGISTRYINDEX, ref);
}
String LuaState::DumpStack() const
{
StringStream stream;
unsigned int stackTop = GetStackTop();
stream << stackTop << " entries\n";
for (unsigned int i = 1; i <= stackTop; ++i)
{
stream << i << ": ";
switch (GetType(i))
{
case LuaType_Boolean:
stream << "Boolean(" << ToBoolean(i) << ')';
break;
case LuaType_Function:
stream << "Function(" << ToPointer(i) << ')';
break;
case LuaType_LightUserdata:
case LuaType_Userdata:
stream << "Userdata(" << ToUserdata(i) << ')';
break;
case LuaType_Nil:
stream << "Nil";
break;
case LuaType_None:
stream << "None";
break;
case LuaType_Number:
stream << "Number(" << ToNumber(i) << ')';
break;
case LuaType_String:
stream << "String(" << ToString(i) << ')';
break;
case LuaType_Table:
stream << "Table(" << ToPointer(i) << ')';
break;
case LuaType_Thread:
stream << "Thread(" << ToPointer(i) << ')';
break;
default:
stream << "Unknown(" << ToPointer(i) << ')';
break;
}
stream << '\n';
}
return stream.ToString();
}
void LuaState::Error(const char* message) const
{
luaL_error(m_state, message);
}
void LuaState::Error(const String& message) const
{
luaL_error(m_state, message.GetConstBuffer());
}
bool LuaState::Execute(const String& code)
{
if (code.IsEmpty())
return true;
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return Run(0, 0);
}
bool LuaState::ExecuteFromFile(const String& filePath)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
return false;
}
std::size_t length = static_cast<std::size_t>(file.GetSize());
String source(length, '\0');
if (file.Read(&source[0], length) != length)
{
NazaraError("Failed to read file");
return false;
}
file.Close();
return Execute(source);
}
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size)
{
MemoryView stream(data, size);
return ExecuteFromStream(stream);
}
bool LuaState::ExecuteFromStream(Stream& stream)
{
StreamData data;
data.stream = &stream;
if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return Run(0, 0);
}
int LuaState::GetAbsIndex(int index) const
{
return lua_absindex(m_state, index);
}
LuaType LuaState::GetField(const char* fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName));
}
LuaType LuaState::GetField(const String& fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName.GetConstBuffer()));
}
LuaType LuaState::GetGlobal(const char* name) const
{
return FromLuaType(lua_getglobal(m_state, name));
}
LuaType LuaState::GetGlobal(const String& name) const
{
return FromLuaType(lua_getglobal(m_state, name.GetConstBuffer()));
}
LuaType LuaState::GetMetatable(const char* tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname));
}
LuaType LuaState::GetMetatable(const String& tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname.GetConstBuffer()));
}
bool LuaState::GetMetatable(int index) const
{
return lua_getmetatable(m_state, index) != 0;
}
unsigned int LuaState::GetStackTop() const
{
return static_cast<int>(lua_gettop(m_state));
}
LuaType LuaState::GetTable(int index) const
{
return FromLuaType(lua_gettable(m_state, index));
}
LuaType LuaState::GetTableRaw(int index) const
{
return FromLuaType(lua_rawget(m_state, index));
}
LuaType LuaState::GetType(int index) const
{
return FromLuaType(lua_type(m_state, index));
}
const char* LuaState::GetTypeName(LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return nullptr;
}
#endif
return lua_typename(m_state, s_types[type]);
}
void LuaState::Insert(int index) const
{
lua_insert(m_state, index);
}
bool LuaState::IsOfType(int index, LuaType type) const
{
switch (type)
{
case LuaType_Boolean:
return lua_isboolean(m_state, index) != 0;
case LuaType_Function:
return lua_isfunction(m_state, index) != 0;
case LuaType_LightUserdata:
return lua_islightuserdata(m_state, index) != 0;
case LuaType_Nil:
return lua_isnil(m_state, index) != 0;
case LuaType_None:
return lua_isnone(m_state, index) != 0;
case LuaType_Number:
return lua_isnumber(m_state, index) != 0;
case LuaType_String:
return lua_isstring(m_state, index) != 0;
case LuaType_Table:
return lua_istable(m_state, index) != 0;
case LuaType_Thread:
return lua_isthread(m_state, index) != 0;
case LuaType_Userdata:
return lua_isuserdata(m_state, index) != 0;
}
NazaraError("Lua type not handled (0x" + String::Number(type, 16) + ')');
return false;
}
bool LuaState::IsOfType(int index, const char* tname) const
{
void* ud = luaL_testudata(m_state, index, tname);
return ud != nullptr;
}
bool LuaState::IsOfType(int index, const String& tname) const
{
return IsOfType(index, tname.GetConstBuffer());
}
bool LuaState::IsValid(int index) const
{
return lua_isnoneornil(m_state, index) == 0;
}
long long LuaState::Length(int index) const
{
return luaL_len(m_state, index);
}
std::size_t LuaState::LengthRaw(int index) const
{
return lua_rawlen(m_state, index);
}
void LuaState::MoveTo(LuaState* instance, int n) const
{
lua_xmove(m_state, instance->m_state, n);
}
bool LuaState::NewMetatable(const char* str)
{
return luaL_newmetatable(m_state, str) != 0;
}
bool LuaState::NewMetatable(const String& str)
{
return luaL_newmetatable(m_state, str.GetConstBuffer()) != 0;
}
bool LuaState::Next(int index) const
{
return lua_next(m_state, index) != 0;
}
void LuaState::Pop(unsigned int n) const
{
lua_pop(m_state, static_cast<int>(n));
}
void LuaState::PushBoolean(bool value) const
{
lua_pushboolean(m_state, (value) ? 1 : 0);
}
void LuaState::PushCFunction(LuaCFunction func, unsigned int upvalueCount) const
{
lua_pushcclosure(m_state, func, upvalueCount);
}
void LuaState::PushFunction(LuaFunction func) const
{
LuaFunction* luaFunc = static_cast<LuaFunction*>(lua_newuserdata(m_state, sizeof(LuaFunction)));
PlacementNew(luaFunc, std::move(func));
lua_pushcclosure(m_state, ProxyFunc, 1);
}
void LuaState::PushInteger(long long value) const
{
lua_pushinteger(m_state, value);
}
void LuaState::PushLightUserdata(void* value) const
{
lua_pushlightuserdata(m_state, value);
}
void LuaState::PushMetatable(const char* str) const
{
luaL_getmetatable(m_state, str);
}
void LuaState::PushMetatable(const String& str) const
{
luaL_getmetatable(m_state, str.GetConstBuffer());
}
void LuaState::PushNil() const
{
lua_pushnil(m_state);
}
void LuaState::PushNumber(double value) const
{
lua_pushnumber(m_state, value);
}
void LuaState::PushReference(int ref) const
{
lua_rawgeti(m_state, LUA_REGISTRYINDEX, ref);
}
void LuaState::PushString(const char* str) const
{
lua_pushstring(m_state, str);
}
void LuaState::PushString(const char* str, std::size_t size) const
{
lua_pushlstring(m_state, str, size);
}
void LuaState::PushString(const String& str) const
{
lua_pushlstring(m_state, str.GetConstBuffer(), str.GetSize());
}
void LuaState::PushTable(std::size_t sequenceElementCount, std::size_t arrayElementCount) const
{
constexpr std::size_t maxInt = std::numeric_limits<int>::max();
lua_createtable(m_state, static_cast<int>(std::min(sequenceElementCount, maxInt)), static_cast<int>(std::min(arrayElementCount, maxInt)));
}
void* LuaState::PushUserdata(std::size_t size) const
{
return lua_newuserdata(m_state, size);
}
void LuaState::PushValue(int index) const
{
lua_pushvalue(m_state, index);
}
void LuaState::Remove(int index) const
{
lua_remove(m_state, index);
}
void LuaState::Replace(int index) const
{
lua_replace(m_state, index);
}
void LuaState::SetField(const char* name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name);
}
void LuaState::SetField(const String& name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name.GetConstBuffer());
}
void LuaState::SetGlobal(const char* name)
{
lua_setglobal(m_state, name);
}
void LuaState::SetGlobal(const String& name)
{
lua_setglobal(m_state, name.GetConstBuffer());
}
void LuaState::SetMetatable(const char* tname) const
{
luaL_setmetatable(m_state, tname);
}
void LuaState::SetMetatable(const String& tname) const
{
luaL_setmetatable(m_state, tname.GetConstBuffer());
}
void LuaState::SetMetatable(int index) const
{
lua_setmetatable(m_state, index);
}
void LuaState::SetTable(int index) const
{
lua_settable(m_state, index);
}
void LuaState::SetTableRaw(int index) const
{
lua_rawset(m_state, index);
}
bool LuaState::ToBoolean(int index) const
{
return lua_toboolean(m_state, index) != 0;
}
long long LuaState::ToInteger(int index, bool* succeeded) const
{
int success;
long long result = lua_tointegerx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
double LuaState::ToNumber(int index, bool* succeeded) const
{
int success;
double result = lua_tonumberx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
const void* LuaState::ToPointer(int index) const
{
return lua_topointer(m_state, index);
}
const char* LuaState::ToString(int index, std::size_t* length) const
{
return lua_tolstring(m_state, index, length);
}
void* LuaState::ToUserdata(int index) const
{
return lua_touserdata(m_state, index);
}
void* LuaState::ToUserdata(int index, const char* tname) const
{
return luaL_testudata(m_state, index, tname);
}
void* LuaState::ToUserdata(int index, const String& tname) const
{
return luaL_testudata(m_state, index, tname.GetConstBuffer());
}
LuaState& LuaState::operator=(LuaState&& state) noexcept
{
m_instance = state.m_instance;
m_lastError = std::move(state.m_lastError);
m_state = state.m_state;
return *this;
}
bool LuaState::Run(int argCount, int resultCount)
{
if (m_instance->m_level++ == 0)
m_instance->m_clock.Restart();
int status = lua_pcall(m_state, argCount, resultCount, 0);
m_instance->m_level--;
if (status != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
int LuaState::GetIndexOfUpValue(int upValue)
{
return lua_upvalueindex(upValue);
}
LuaState LuaState::GetState(lua_State* internalState)
{
LuaInstance* instance;
lua_getallocf(internalState, reinterpret_cast<void**>(&instance));
return LuaState(instance, internalState);
}
int LuaState::ProxyFunc(lua_State* internalState)
{
LuaFunction& func = *static_cast<LuaFunction*>(lua_touserdata(internalState, lua_upvalueindex(1)));
LuaState state = GetState(internalState);
return func(state);
}
}