Lua/LuaState: Add Load methods

This commit is contained in:
Lynix 2018-05-13 14:42:27 +02:00
parent 70e0787b93
commit 8b6311de63
3 changed files with 71 additions and 36 deletions

View File

@ -192,7 +192,7 @@ Nazara Engine:
- Added VertexMapper::GetVertexCount()
- Added VertexMapper::HasComponentOfType()
- Fixed SimpleTextDrawer bounds computation
- Added LuaState::Load methods which allows to load (compile) lua code to function without executing it.
Nazara Development Kit:
- ⚠️ Components no longer need to be copyable by assignation

View File

@ -112,6 +112,11 @@ namespace Nz
bool IsOfType(int index, const String& tname) const;
bool IsValid(int index) const;
bool Load(const String& code);
bool LoadFromFile(const String& filePath);
bool LoadFromMemory(const void* data, std::size_t size);
bool LoadFromStream(Stream& stream);
long long Length(int index) const;
std::size_t LengthRaw(int index) const;

View File

@ -355,39 +355,18 @@ namespace Nz
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);
if (!Load(code))
return false;
}
return Run(0, LUA_MULTRET);
return Call(0);
}
bool LuaState::ExecuteFromFile(const String& filePath)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
if (!LoadFromFile(filePath))
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);
return Call(0);
}
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size)
@ -398,18 +377,10 @@ namespace Nz
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);
if (!LoadFromStream(stream))
return false;
}
return Run(0, LUA_MULTRET);
return Call(0);
}
int LuaState::GetAbsIndex(int index) const
@ -545,6 +516,65 @@ namespace Nz
return lua_isnoneornil(m_state, index) == 0;
}
bool LuaState::Load(const String& code)
{
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
bool LuaState::LoadFromFile(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 Load(source);
}
bool LuaState::LoadFromMemory(const void* data, std::size_t size)
{
MemoryView stream(data, size);
return LoadFromStream(stream);
}
bool LuaState::LoadFromStream(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 true;
}
long long LuaState::Length(int index) const
{
return luaL_len(m_state, index);