From 5ffc5a8784905b1b243656f5570b13907e3982c9 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 27 Oct 2016 11:22:35 +0200 Subject: [PATCH] Lua/LuaClass: Don't remove instance from the Lua stack The instance Lua entry now remains as the first index, shifting all the parameters indexes by 1 --- SDK/include/NDK/LuaBinding.hpp | 2 +- SDK/src/NDK/LuaBinding_Audio.cpp | 12 ++--- SDK/src/NDK/LuaBinding_Core.cpp | 32 ++++++------- SDK/src/NDK/LuaBinding_Graphics.cpp | 74 ++++++++++++++--------------- SDK/src/NDK/LuaBinding_Math.cpp | 72 ++++++++++++++-------------- SDK/src/NDK/LuaBinding_Network.cpp | 6 +-- SDK/src/NDK/LuaBinding_SDK.cpp | 40 ++++++++-------- SDK/src/NDK/LuaBinding_Utility.cpp | 46 +++++++++--------- include/Nazara/Lua/LuaClass.hpp | 2 +- include/Nazara/Lua/LuaClass.inl | 20 ++++---- include/Nazara/Lua/LuaInstance.inl | 20 +++++++- 11 files changed, 171 insertions(+), 155 deletions(-) diff --git a/SDK/include/NDK/LuaBinding.hpp b/SDK/include/NDK/LuaBinding.hpp index f8de961e2..da6d282d1 100644 --- a/SDK/include/NDK/LuaBinding.hpp +++ b/SDK/include/NDK/LuaBinding.hpp @@ -124,7 +124,7 @@ namespace Ndk Nz::String name; }; - ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 1); + ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 2); std::vector m_componentBinding; std::unordered_map m_componentBindingByName; diff --git a/SDK/src/NDK/LuaBinding_Audio.cpp b/SDK/src/NDK/LuaBinding_Audio.cpp index 7b81b0c8e..46791b6c7 100644 --- a/SDK/src/NDK/LuaBinding_Audio.cpp +++ b/SDK/src/NDK/LuaBinding_Audio.cpp @@ -41,7 +41,7 @@ namespace Ndk music.BindMethod("Stop", &Nz::Music::Stop); // Manual - music.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Music& music) -> int + music.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Music& music, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("Music("); stream << music.GetFilePath() << ')'; @@ -65,7 +65,7 @@ namespace Ndk sound.BindMethod("SetPlayingOffset", &Nz::Sound::SetPlayingOffset); // Manual - sound.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Sound& sound) -> int + sound.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Sound& sound, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("Sound("); if (const Nz::SoundBuffer* buffer = sound.GetBuffer()) @@ -101,9 +101,9 @@ namespace Ndk soundBuffer.BindStaticMethod("IsFormatSupported", &Nz::SoundBuffer::IsFormatSupported); // Manual - soundBuffer.BindMethod("Create", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance) -> int + soundBuffer.BindMethod("Create", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int { - int index = 1; + int index = 2; Nz::AudioFormat format = lua.Check(&index); unsigned int sampleCount = lua.Check(&index); unsigned int sampleRate = lua.Check(&index); @@ -116,13 +116,13 @@ namespace Ndk return 1; }); - soundBuffer.BindMethod("GetSamples", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance) -> int + soundBuffer.BindMethod("GetSamples", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int { lua.PushString(reinterpret_cast(instance->GetSamples()), instance->GetSampleCount() * sizeof(Nz::Int16)); return 1; }); - soundBuffer.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance) -> int + soundBuffer.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::SoundBufferRef& instance, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("SoundBuffer("); if (instance->IsValid()) diff --git a/SDK/src/NDK/LuaBinding_Core.cpp b/SDK/src/NDK/LuaBinding_Core.cpp index 12c559936..dc149e3d0 100644 --- a/SDK/src/NDK/LuaBinding_Core.cpp +++ b/SDK/src/NDK/LuaBinding_Core.cpp @@ -15,7 +15,7 @@ namespace Ndk /*********************************** Nz::Clock **********************************/ clock.SetConstructor([](Nz::LuaInstance& lua, Nz::Clock* clock, std::size_t /*argumentCount*/) { - int argIndex = 1; + int argIndex = 2; Nz::Int64 startingValue = lua.Check(&argIndex, 0); bool paused = lua.Check(&argIndex, false); @@ -32,7 +32,7 @@ namespace Ndk clock.BindMethod("Unpause", &Nz::Clock::Unpause); // Manual - clock.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Clock& clock) -> int { + clock.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Clock& clock, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("Clock(Elapsed: "); stream << clock.GetSeconds(); stream << "s, Paused: "; @@ -48,7 +48,7 @@ namespace Ndk { std::size_t argCount = std::min(argumentCount, 1U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 0: @@ -85,7 +85,7 @@ namespace Ndk directory.BindStaticMethod("SetCurrent", Nz::Directory::SetCurrent); // Manual - directory.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Directory& directory) -> int { + directory.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::Directory& directory, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("Directory("); stream << directory.GetPath(); stream << ')'; @@ -110,8 +110,8 @@ namespace Ndk stream.BindMethod("IsWritable", &Nz::Stream::IsWritable); stream.BindMethod("SetCursorPos", &Nz::Stream::SetCursorPos); - stream.BindMethod("Read", [] (Nz::LuaInstance& lua, Nz::Stream& stream) -> int { - int argIndex = 1; + stream.BindMethod("Read", [] (Nz::LuaInstance& lua, Nz::Stream& stream, std::size_t /*argumentCount*/) -> int { + int argIndex = 2; std::size_t length = lua.Check(&argIndex); @@ -122,8 +122,8 @@ namespace Ndk return 1; }); - stream.BindMethod("Write", [] (Nz::LuaInstance& lua, Nz::Stream& stream) -> int { - int argIndex = 1; + stream.BindMethod("Write", [] (Nz::LuaInstance& lua, Nz::Stream& stream, std::size_t /*argumentCount*/) -> int { + int argIndex = 2; std::size_t bufferSize = 0; const char* buffer = lua.CheckString(argIndex, &bufferSize); @@ -142,7 +142,7 @@ namespace Ndk { std::size_t argCount = std::min(argumentCount, 1U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 0: @@ -201,11 +201,11 @@ namespace Ndk file.BindStaticMethod("Rename", &Nz::File::Rename); // Manual - file.BindMethod("Open", [] (Nz::LuaInstance& lua, Nz::File& file) -> int + file.BindMethod("Open", [] (Nz::LuaInstance& lua, Nz::File& file, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 2U); + std::size_t argCount = std::min(argumentCount, 2U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 0: @@ -224,11 +224,11 @@ namespace Ndk return 0; }); - file.BindMethod("SetCursorPos", [] (Nz::LuaInstance& lua, Nz::File& file) -> int + file.BindMethod("SetCursorPos", [] (Nz::LuaInstance& lua, Nz::File& file, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 2U); + std::size_t argCount = std::min(argumentCount, 2U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 1: @@ -246,7 +246,7 @@ namespace Ndk return 0; }); - file.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::File& file) -> int { + file.BindMethod("__tostring", [] (Nz::LuaInstance& lua, Nz::File& file, std::size_t /*argumentCount*/) -> int { Nz::StringStream stream("File("); if (file.IsOpen()) stream << "Path: " << file.GetPath(); diff --git a/SDK/src/NDK/LuaBinding_Graphics.cpp b/SDK/src/NDK/LuaBinding_Graphics.cpp index c36098c7a..597c0dc68 100644 --- a/SDK/src/NDK/LuaBinding_Graphics.cpp +++ b/SDK/src/NDK/LuaBinding_Graphics.cpp @@ -25,19 +25,19 @@ namespace Ndk case 1: { - if (lua.IsOfType(1, "MaterialPipeline")) + int argIndex = 1; + if (lua.IsOfType(argIndex, "MaterialPipeline")) { - Nz::PlacementNew(instance, Nz::Material::New(*static_cast(lua.ToUserdata(1)))); + Nz::PlacementNew(instance, Nz::Material::New(*static_cast(lua.ToUserdata(argIndex)))); return true; } - else if (lua.IsOfType(1, "Material")) + else if (lua.IsOfType(argIndex, "Material")) { - Nz::PlacementNew(instance, Nz::Material::New(**static_cast(lua.ToUserdata(1)))); + Nz::PlacementNew(instance, Nz::Material::New(**static_cast(lua.ToUserdata(argIndex)))); return true; } else { - int argIndex = 1; Nz::PlacementNew(instance, Nz::Material::New(lua.Check(&argIndex))); return true; } @@ -48,16 +48,16 @@ namespace Ndk return false; }); - material.BindMethod("Configure", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("Configure", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "MaterialPipeline")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "MaterialPipeline")) { - instance->Configure(*static_cast(lua.ToUserdata(1))); + instance->Configure(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->Configure(lua.Check(&argIndex))); return 1; } @@ -146,106 +146,106 @@ namespace Ndk material.BindStaticMethod("GetDefault", &Nz::Material::GetDefault); - material.BindMethod("SetAlphaMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetAlphaMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetAlphaMap(*static_cast(lua.ToUserdata(1))); + instance->SetAlphaMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetAlphaMap(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetDiffuseMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetDiffuseMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetDiffuseMap(*static_cast(lua.ToUserdata(1))); + instance->SetDiffuseMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetDiffuseMap(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetEmissiveMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetEmissiveMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetEmissiveMap(*static_cast(lua.ToUserdata(1))); + instance->SetEmissiveMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetEmissiveMap(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetHeightMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetHeightMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetHeightMap(*static_cast(lua.ToUserdata(1))); + instance->SetHeightMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetHeightMap(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetNormalMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetNormalMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetNormalMap(*static_cast(lua.ToUserdata(1))); + instance->SetNormalMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetNormalMap(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetShader", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetShader", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "UberShader")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "UberShader")) { - instance->SetShader(*static_cast(lua.ToUserdata(1))); + instance->SetShader(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetShader(lua.Check(&argIndex))); return 1; } }); - material.BindMethod("SetSpecularMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance) -> int + material.BindMethod("SetSpecularMap", [] (Nz::LuaInstance& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int { - if (lua.IsOfType(1, "Texture")) + int argIndex = 2; + if (lua.IsOfType(argIndex, "Texture")) { - instance->SetSpecularMap(*static_cast(lua.ToUserdata(1))); + instance->SetSpecularMap(*static_cast(lua.ToUserdata(argIndex))); return 0; } else { - int argIndex = 1; lua.Push(instance->SetSpecularMap(lua.Check(&argIndex))); return 1; } diff --git a/SDK/src/NDK/LuaBinding_Math.cpp b/SDK/src/NDK/LuaBinding_Math.cpp index 989e2c28c..00707c9e3 100644 --- a/SDK/src/NDK/LuaBinding_Math.cpp +++ b/SDK/src/NDK/LuaBinding_Math.cpp @@ -42,7 +42,7 @@ namespace Ndk eulerAngles.SetGetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance) { std::size_t length; - const char* ypr = lua.CheckString(1, &length); + const char* ypr = lua.CheckString(2, &length); switch (length) { @@ -99,8 +99,8 @@ namespace Ndk eulerAngles.SetSetter([] (Nz::LuaInstance& lua, Nz::EulerAnglesd& instance) { std::size_t length; - const char* ypr = lua.CheckString(1, &length); - double value = lua.CheckNumber(2); + const char* ypr = lua.CheckString(2, &length); + double value = lua.CheckNumber(3); switch (length) { @@ -191,9 +191,9 @@ namespace Ndk matrix4d.SetGetter([] (Nz::LuaInstance& lua, Nz::Matrix4d& instance) { - int argIndex = 1; - std::size_t index = lua.Check(&argIndex); - if (index < 1 || index > 16) + bool succeeded = false; + std::size_t index = static_cast(lua.ToInteger(2, &succeeded)); + if (!succeeded || index < 1 || index > 16) return false; lua.Push(instance[index - 1]); @@ -202,12 +202,12 @@ namespace Ndk matrix4d.SetSetter([] (Nz::LuaInstance& lua, Nz::Matrix4d& instance) { - int argIndex = 1; - std::size_t index = lua.Check(&argIndex); - if (index < 1 || index > 16) + bool succeeded = false; + std::size_t index = static_cast(lua.ToInteger(2, &succeeded)); + if (!succeeded || index < 1 || index > 16) return false; - instance[index - 1] = lua.CheckNumber(argIndex); + instance[index - 1] = lua.CheckNumber(3); return true; }); @@ -265,11 +265,11 @@ namespace Ndk rect.SetGetter([] (Nz::LuaInstance& lua, Nz::Rectd& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - auto index = lua.CheckBoundInteger(1); + auto index = lua.CheckBoundInteger(2); if (index < 1 || index > 4) return false; @@ -280,7 +280,7 @@ namespace Ndk case Nz::LuaType_String: { std::size_t length; - const char* xywh = lua.CheckString(1, &length); + const char* xywh = lua.CheckString(2, &length); if (length != 1) break; @@ -318,11 +318,11 @@ namespace Ndk rect.SetSetter([] (Nz::LuaInstance& lua, Nz::Rectd& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - auto index = lua.CheckBoundInteger(1); + auto index = lua.CheckBoundInteger(2); if (index < 1 || index > 4) return false; @@ -333,12 +333,12 @@ namespace Ndk case Nz::LuaType_String: { std::size_t length; - const char* xywh = lua.CheckString(1, &length); + const char* xywh = lua.CheckString(2, &length); if (length != 1) break; - double value = lua.CheckNumber(2); + double value = lua.CheckNumber(3); switch (xywh[0]) { @@ -412,7 +412,7 @@ namespace Ndk quaternion.SetGetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance) { std::size_t length; - const char* wxyz = lua.CheckString(1, &length); + const char* wxyz = lua.CheckString(2, &length); if (length != 1) return false; @@ -442,12 +442,12 @@ namespace Ndk quaternion.SetSetter([] (Nz::LuaInstance& lua, Nz::Quaterniond& instance) { std::size_t length; - const char* wxyz = lua.CheckString(1, &length); + const char* wxyz = lua.CheckString(2, &length); if (length != 1) return false; - double value = lua.CheckNumber(2); + double value = lua.CheckNumber(3); switch (wxyz[0]) { @@ -507,11 +507,11 @@ namespace Ndk vector2d.SetGetter([](Nz::LuaInstance& lua, Nz::Vector2d& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - long long index = lua.CheckInteger(1); + long long index = lua.CheckInteger(2); if (index < 1 || index > 2) return false; @@ -522,7 +522,7 @@ namespace Ndk case Nz::LuaType_String: { std::size_t length; - const char* xy = lua.CheckString(1, &length); + const char* xy = lua.CheckString(2, &length); if (length != 1) break; @@ -552,27 +552,27 @@ namespace Ndk vector2d.SetSetter([](Nz::LuaInstance& lua, Nz::Vector2d& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - long long index = lua.CheckInteger(1); + long long index = lua.CheckInteger(2); if (index < 1 || index > 2) return false; - instance[index - 1] = lua.CheckNumber(2); + instance[index - 1] = lua.CheckNumber(3); return true; } case Nz::LuaType_String: { std::size_t length; - const char* xy = lua.CheckString(1, &length); + const char* xy = lua.CheckString(2, &length); if (length != 1) break; - double value = lua.CheckNumber(2); + double value = lua.CheckNumber(3); switch (xy[0]) { @@ -644,11 +644,11 @@ namespace Ndk vector3d.SetGetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - long long index = lua.CheckInteger(1); + long long index = lua.CheckInteger(2); if (index < 1 || index > 3) return false; @@ -659,7 +659,7 @@ namespace Ndk case Nz::LuaType_String: { std::size_t length; - const char* xyz = lua.CheckString(1, &length); + const char* xyz = lua.CheckString(2, &length); if (length != 1) break; @@ -693,27 +693,27 @@ namespace Ndk vector3d.SetSetter([] (Nz::LuaInstance& lua, Nz::Vector3d& instance) { - switch (lua.GetType(1)) + switch (lua.GetType(2)) { case Nz::LuaType_Number: { - long long index = lua.CheckInteger(1); + long long index = lua.CheckInteger(2); if (index < 1 || index > 3) return false; - instance[index - 1] = lua.CheckNumber(2); + instance[index - 1] = lua.CheckNumber(3); return true; } case Nz::LuaType_String: { std::size_t length; - const char* xyz = lua.CheckString(1, &length); + const char* xyz = lua.CheckString(2, &length); if (length != 1) break; - double value = lua.CheckNumber(2); + double value = lua.CheckNumber(3); switch (xyz[0]) { diff --git a/SDK/src/NDK/LuaBinding_Network.cpp b/SDK/src/NDK/LuaBinding_Network.cpp index 430cd1208..bd1105962 100644 --- a/SDK/src/NDK/LuaBinding_Network.cpp +++ b/SDK/src/NDK/LuaBinding_Network.cpp @@ -25,7 +25,7 @@ namespace Ndk { std::size_t argCount = std::min(argumentCount, 9U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 0: @@ -83,7 +83,7 @@ namespace Ndk Nz::String service; Nz::ResolveError error = Nz::ResolveError_Unknown; - int argIndex = 1; + int argIndex = 2; Nz::String hostName = Nz::IpAddress::ResolveAddress(instance.Check(&argIndex), &service, &error); if (error == Nz::ResolveError_NoError) @@ -104,7 +104,7 @@ namespace Ndk { Nz::ResolveError error = Nz::ResolveError_Unknown; - int argIndex = 1; + int argIndex = 2; Nz::NetProtocol protocol = instance.Check(&argIndex); Nz::String hostname = instance.Check(&argIndex); Nz::String service = instance.Check(&argIndex, "http"); diff --git a/SDK/src/NDK/LuaBinding_SDK.cpp b/SDK/src/NDK/LuaBinding_SDK.cpp index 3baab6f69..3046f2483 100644 --- a/SDK/src/NDK/LuaBinding_SDK.cpp +++ b/SDK/src/NDK/LuaBinding_SDK.cpp @@ -25,7 +25,7 @@ namespace Ndk application.BindMethod("IsFPSCounterEnabled", &Application::IsFPSCounterEnabled); #endif - application.BindMethod("AddWorld", [] (Nz::LuaInstance& instance, Application* application) -> int + application.BindMethod("AddWorld", [] (Nz::LuaInstance& instance, Application* application, std::size_t /*argumentCount*/) -> int { instance.Push(application->AddWorld().CreateHandle()); return 1; @@ -73,21 +73,21 @@ namespace Ndk entity.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents); entity.BindMethod("__tostring", &EntityHandle::ToString); - entity.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int + entity.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int { ComponentBinding* binding = QueryComponentIndex(instance); return binding->adder(instance, handle); }); - entity.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int + entity.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int { ComponentBinding* binding = QueryComponentIndex(instance); return binding->getter(instance, handle->GetComponent(binding->index)); }); - entity.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int + entity.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle, std::size_t /*argumentCount*/) -> int { ComponentBinding* binding = QueryComponentIndex(instance); @@ -105,7 +105,7 @@ namespace Ndk velocityComponent.SetGetter([] (Nz::LuaInstance& lua, VelocityComponentHandle& instance) { std::size_t length; - const char* member = lua.CheckString(1, &length); + const char* member = lua.CheckString(2, &length); if (std::strcmp(member, "Linear") == 0) { @@ -119,9 +119,9 @@ namespace Ndk velocityComponent.SetSetter([] (Nz::LuaInstance& lua, VelocityComponentHandle& instance) { std::size_t length; - const char* member = lua.CheckString(1, &length); + const char* member = lua.CheckString(2, &length); - int argIndex = 2; + int argIndex = 3; if (std::strcmp(member, "Linear") == 0) { instance->linearVelocity = lua.Check(&argIndex); @@ -139,38 +139,38 @@ namespace Ndk #ifndef NDK_SERVER /*********************************** Ndk::GraphicsComponent **********************************/ - graphicsComponent.BindMethod("Attach", [] (Nz::LuaInstance& lua, Ndk::GraphicsComponent *gfxComponent) -> int + graphicsComponent.BindMethod("Attach", [] (Nz::LuaInstance& lua, Ndk::GraphicsComponent *gfxComponent, std::size_t argumentCount) -> int { /* void Attach(Nz::InstancedRenderableRef renderable, int renderOrder = 0); void Attach(Nz::InstancedRenderableRef renderable, const Nz::Matrix4f& localMatrix, int renderOrder = 0); */ - unsigned int argCount = std::min(lua.GetStackTop(), 3U); + std::size_t argCount = std::min(argumentCount, 3U); switch (argCount) { case 1: { - int argIndex = 1; + int argIndex = 2; gfxComponent->Attach(lua.Check(&argIndex)); return 0; } case 2: { - int index = 1; - Nz::InstancedRenderableRef renderable = lua.Check(&index); + int argIndex = 2; + Nz::InstancedRenderableRef renderable = lua.Check(&argIndex); - if (lua.IsOfType(index, Nz::LuaType_Number)) + if (lua.IsOfType(argIndex, Nz::LuaType_Number)) { - int renderOrder = lua.Check(&index); + int renderOrder = lua.Check(&argIndex); gfxComponent->Attach(renderable, renderOrder); } - else if (lua.IsOfType(index, "Matrix4")) + else if (lua.IsOfType(argIndex, "Matrix4")) { - Nz::Matrix4f localMatrix = lua.Check(&index); + Nz::Matrix4f localMatrix = lua.Check(&argIndex); gfxComponent->Attach(renderable, localMatrix); } @@ -182,10 +182,10 @@ namespace Ndk case 3: { - int index = 1; - Nz::InstancedRenderableRef renderable = lua.Check(&index); - Nz::Matrix4f localMatrix = lua.Check(&index); - int renderOrder = lua.Check(&index); + int argIndex = 2; + Nz::InstancedRenderableRef renderable = lua.Check(&argIndex); + Nz::Matrix4f localMatrix = lua.Check(&argIndex); + int renderOrder = lua.Check(&argIndex); gfxComponent->Attach(renderable, localMatrix, renderOrder); return 0; diff --git a/SDK/src/NDK/LuaBinding_Utility.cpp b/SDK/src/NDK/LuaBinding_Utility.cpp index 5ee613e1e..5ae37f669 100644 --- a/SDK/src/NDK/LuaBinding_Utility.cpp +++ b/SDK/src/NDK/LuaBinding_Utility.cpp @@ -25,9 +25,9 @@ namespace Ndk abstractImage.BindMethod("IsCompressed", &Nz::AbstractImage::IsCompressed); abstractImage.BindMethod("IsCubemap", &Nz::AbstractImage::IsCubemap); - abstractImage.BindMethod("GetMemoryUsage", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int + abstractImage.BindMethod("GetMemoryUsage", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 1U); + std::size_t argCount = std::min(argumentCount, 1U); switch (argCount) { case 0: @@ -35,8 +35,8 @@ namespace Ndk case 1: { - int index = 1; - Nz::UInt8 level(lua.Check(&index)); + int argIndex = 2; + Nz::UInt8 level(lua.Check(&argIndex)); return lua.Push(abstractImage->GetMemoryUsage(level)); } @@ -46,10 +46,10 @@ namespace Ndk return 0; }); - abstractImage.BindMethod("Update", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage) -> int + abstractImage.BindMethod("Update", [] (Nz::LuaInstance& lua, Nz::AbstractImage* abstractImage, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 6U); - int argIndex = 1; + std::size_t argCount = std::min(argumentCount, 6U); + int argIndex = 2; std::size_t bufferSize = 0; const Nz::UInt8* pixels = reinterpret_cast(lua.CheckString(argIndex++, &bufferSize)); @@ -105,11 +105,11 @@ namespace Ndk font.BindMethod("Destroy", &Nz::Font::Destroy); - font.BindMethod("GetCachedGlyphCount", [] (Nz::LuaInstance& lua, Nz::FontRef& instance) -> int + font.BindMethod("GetCachedGlyphCount", [] (Nz::LuaInstance& lua, Nz::FontRef& instance, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 2U); + std::size_t argCount = std::min(argumentCount, 2U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 0: @@ -199,9 +199,9 @@ 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& node) -> int + node.BindMethod("Move", [] (Nz::LuaInstance& lua, Nz::Node& node, std::size_t /*argumentCount*/) -> int { - int argIndex = 1; + int argIndex = 2; Nz::Vector3f offset = lua.Check(&argIndex); Nz::CoordSys coordSys = lua.Check(&argIndex, Nz::CoordSys_Local); @@ -210,9 +210,9 @@ namespace Ndk return 0; }); - node.BindMethod("Rotate", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int + node.BindMethod("Rotate", [] (Nz::LuaInstance& lua, Nz::Node& node, std::size_t /*argumentCount*/) -> int { - int argIndex = 1; + int argIndex = 2; Nz::Quaternionf rotation = lua.Check(&argIndex); Nz::CoordSys coordSys = lua.Check(&argIndex, Nz::CoordSys_Local); @@ -221,11 +221,11 @@ namespace Ndk return 0; }); - node.BindMethod("Scale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int + node.BindMethod("Scale", [] (Nz::LuaInstance& lua, Nz::Node& node, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 4U); + std::size_t argCount = std::min(argumentCount, 4U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 1: @@ -247,11 +247,11 @@ namespace Ndk return 0; }); - node.BindMethod("SetScale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int + node.BindMethod("SetScale", [] (Nz::LuaInstance& lua, Nz::Node& node, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 4U); + std::size_t argCount = std::min(argumentCount, 4U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 1: @@ -284,11 +284,11 @@ namespace Ndk return 0; }); - node.BindMethod("SetInitialScale", [] (Nz::LuaInstance& lua, Nz::Node& node) -> int + node.BindMethod("SetInitialScale", [] (Nz::LuaInstance& lua, Nz::Node& node, std::size_t argumentCount) -> int { - unsigned int argCount = std::min(lua.GetStackTop(), 4U); + std::size_t argCount = std::min(argumentCount, 4U); - int argIndex = 1; + int argIndex = 2; switch (argCount) { case 1: diff --git a/include/Nazara/Lua/LuaClass.hpp b/include/Nazara/Lua/LuaClass.hpp index 795cfe6ff..3085aed0f 100644 --- a/include/Nazara/Lua/LuaClass.hpp +++ b/include/Nazara/Lua/LuaClass.hpp @@ -26,7 +26,7 @@ namespace Nz friend class LuaClass; public: - using ClassFunc = std::function; + using ClassFunc = std::function; using ClassIndexFunc = std::function; using ConstructorFunc = std::function; template using ConvertToParent = std::function; diff --git a/include/Nazara/Lua/LuaClass.inl b/include/Nazara/Lua/LuaClass.inl index 4f0144fc8..daf7a632a 100644 --- a/include/Nazara/Lua/LuaClass.inl +++ b/include/Nazara/Lua/LuaClass.inl @@ -85,7 +85,7 @@ namespace Nz // Let's create the metatable which will be associated with every instance. SetupMetatable(lua); - if (m_info->constructor || m_info->staticGetter || m_info->staticSetter || !m_info->staticMethods.empty()) + if (m_info->constructor || m_info->staticGetter || m_info->staticSetter || !m_staticMethods.empty()) SetupGlobalTable(lua); lua.Pop(); // Pop our ClassInfo, which is now referenced by all our functions @@ -127,7 +127,7 @@ namespace Nz { typename LuaImplMethodProxy::template Impl handler(std::forward(defArgs)...); - BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int + BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int { handler.ProcessArgs(lua); @@ -141,7 +141,7 @@ namespace Nz { typename LuaImplMethodProxy::template Impl handler(std::forward(defArgs)...); - BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int + BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int { handler.ProcessArgs(lua); @@ -155,7 +155,7 @@ namespace Nz { typename LuaImplMethodProxy::template Impl handler(std::forward(defArgs)...); - BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int + BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int { handler.ProcessArgs(lua); @@ -169,7 +169,7 @@ namespace Nz { typename LuaImplMethodProxy::template Impl handler(std::forward(defArgs)...); - BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int + BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int { handler.ProcessArgs(lua); @@ -449,7 +449,7 @@ namespace Nz { // Query from the metatable lua.GetMetatable(info->name); //< Metatable - lua.PushValue(1); //< Field + lua.PushValue(2); //< Field lua.GetTable(); // Metatable[Field] lua.Remove(-2); // Remove Metatable @@ -476,7 +476,6 @@ namespace Nz std::shared_ptr& info = *static_cast*>(lua.ToUserdata(lua.GetIndexOfUpValue(1))); T* instance = static_cast(lua.CheckUserdata(1, info->name)); - lua.Remove(1); //< Remove the instance from the Lua stack Get(info, lua, instance); return 1; @@ -501,8 +500,6 @@ namespace Nz instance = it->second(lua); } lua.Pop(2); - - lua.Remove(1); //< Remove the instance from the Lua stack } if (!instance) @@ -511,9 +508,11 @@ namespace Nz return 0; } + std::size_t argCount = lua.GetStackTop() - 1U; + unsigned int index = static_cast(lua.ToInteger(lua.GetIndexOfUpValue(2))); const ClassFunc& method = info->methods[index]; - return method(lua, *instance); + return method(lua, *instance, argCount); } template @@ -525,7 +524,6 @@ namespace Nz const ClassIndexFunc& setter = info->setter; T& instance = *static_cast(lua.CheckUserdata(1, info->name)); - lua.Remove(1); //< Remove the instance from the Lua stack if (!setter(lua, instance)) { diff --git a/include/Nazara/Lua/LuaInstance.inl b/include/Nazara/Lua/LuaInstance.inl index ab563d301..18c526e24 100644 --- a/include/Nazara/Lua/LuaInstance.inl +++ b/include/Nazara/Lua/LuaInstance.inl @@ -209,6 +209,24 @@ namespace Nz return LuaImplReplyVal(instance, val, TypeTag()); } + template + std::enable_if_t::value && !std::is_enum::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) + { + return LuaImplReplyVal(instance, std::move(val), TypeTag()); + } + + template + std::enable_if_t::value && !std::is_enum::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) + { + return LuaImplReplyVal(instance, std::move(val), TypeTag()); + } + + template + int LuaImplReplyVal(const LuaInstance& instance, T&& val, TypeTag) + { + return LuaImplReplyVal(instance, std::forward(val), TypeTag()); + } + inline int LuaImplReplyVal(const LuaInstance& instance, std::string&& val, TypeTag) { instance.PushString(val.c_str(), val.size()); @@ -375,7 +393,7 @@ namespace Nz void ProcessArgs(const LuaInstance& instance) const { - m_index = 1; + m_index = 2; //< 1 being the instance ProcessArgs<0, Args...>(instance); }