lua runnables update
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ inline const std::string CORE_AIR = "core:air";
|
|||||||
|
|
||||||
inline const std::string TEXTURE_NOTFOUND = "notfound";
|
inline const std::string TEXTURE_NOTFOUND = "notfound";
|
||||||
|
|
||||||
/* bindings used in engine code */
|
// bindings used in engine code
|
||||||
inline const std::string BIND_MOVE_FORWARD = "movement.forward";
|
inline const std::string BIND_MOVE_FORWARD = "movement.forward";
|
||||||
inline const std::string BIND_MOVE_BACK = "movement.back";
|
inline const std::string BIND_MOVE_BACK = "movement.back";
|
||||||
inline const std::string BIND_MOVE_LEFT = "movement.left";
|
inline const std::string BIND_MOVE_LEFT = "movement.left";
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#include "../../../debug/Logger.hpp"
|
#include "../../../debug/Logger.hpp"
|
||||||
#include "../../../util/stringutil.h"
|
#include "../../../util/stringutil.h"
|
||||||
|
|
||||||
|
inline std::string LAMBDAS_TABLE = "$L";
|
||||||
|
|
||||||
static debug::Logger logger("lua-state");
|
static debug::Logger logger("lua-state");
|
||||||
|
|
||||||
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
|
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
|
||||||
@@ -54,6 +56,9 @@ lua::LuaState::LuaState() {
|
|||||||
|
|
||||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||||
setglobal(envName(0));
|
setglobal(envName(0));
|
||||||
|
|
||||||
|
lua_createtable(L, 0, 0);
|
||||||
|
setglobal(LAMBDAS_TABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string lua::LuaState::envName(int env) {
|
const std::string lua::LuaState::envName(int env) {
|
||||||
@@ -65,7 +70,7 @@ lua::LuaState::~LuaState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lua::LuaState::logError(const std::string& text) {
|
void lua::LuaState::logError(const std::string& text) {
|
||||||
std::cerr << text << std::endl;
|
logger.error() << text;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::LuaState::addfunc(const std::string& name, lua_CFunction func) {
|
void lua::LuaState::addfunc(const std::string& name, lua_CFunction func) {
|
||||||
@@ -366,6 +371,28 @@ const std::string lua::LuaState::storeAnonymous() {
|
|||||||
return funcName;
|
return funcName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runnable lua::LuaState::createLambda() {
|
||||||
|
auto ptr = reinterpret_cast<ptrdiff_t>(lua_topointer(L, -1));
|
||||||
|
auto name = util::mangleid(ptr);
|
||||||
|
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||||
|
lua_pushvalue(L, -2);
|
||||||
|
lua_setfield(L, -2, name.c_str());
|
||||||
|
lua_pop(L, 2);
|
||||||
|
|
||||||
|
std::shared_ptr<std::string> funcptr(new std::string(name), [=](auto* name) {
|
||||||
|
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_setfield(L, -2, name->c_str());
|
||||||
|
lua_pop(L, 1);
|
||||||
|
delete name;
|
||||||
|
});
|
||||||
|
return [=]() {
|
||||||
|
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||||
|
lua_getfield(L, -1, funcptr->c_str());
|
||||||
|
lua_call(L, 0, LUA_MULTRET);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
int lua::LuaState::createEnvironment(int parent) {
|
int lua::LuaState::createEnvironment(int parent) {
|
||||||
int id = nextEnvironment++;
|
int id = nextEnvironment++;
|
||||||
|
|
||||||
@@ -396,7 +423,7 @@ void lua::LuaState::removeEnvironment(int id) {
|
|||||||
}
|
}
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
setglobal(envName(id));
|
setglobal(envName(id));
|
||||||
logger.info() << "removed environment " << envName(id);
|
logger.debug() << "removed environment " << envName(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::LuaState::dumpStack() {
|
void lua::LuaState::dumpStack() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "../../../data/dynamic.h"
|
#include "../../../data/dynamic.h"
|
||||||
|
#include "../../../delegates.h"
|
||||||
|
|
||||||
#ifndef LUAJIT_VERSION
|
#ifndef LUAJIT_VERSION
|
||||||
#error LuaJIT required
|
#error LuaJIT required
|
||||||
@@ -62,6 +63,7 @@ namespace lua {
|
|||||||
bool hasglobal(const std::string& name);
|
bool hasglobal(const std::string& name);
|
||||||
bool rename(const std::string& from, const std::string& to);
|
bool rename(const std::string& from, const std::string& to);
|
||||||
void remove(const std::string& name);;
|
void remove(const std::string& name);;
|
||||||
|
runnable createLambda();
|
||||||
int createEnvironment(int parent);
|
int createEnvironment(int parent);
|
||||||
void removeEnvironment(int id);
|
void removeEnvironment(int id);
|
||||||
const std::string storeAnonymous();
|
const std::string storeAnonymous();
|
||||||
|
|||||||
@@ -3,12 +3,15 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "lua/LuaState.h"
|
#include "lua/LuaState.h"
|
||||||
|
#include "../../debug/Logger.hpp"
|
||||||
#include "../../util/stringutil.h"
|
#include "../../util/stringutil.h"
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
extern lua::LuaState* state;
|
extern lua::LuaState* state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static debug::Logger logger("scripting_func");
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
runnable scripting::create_runnable(
|
runnable scripting::create_runnable(
|
||||||
@@ -16,13 +19,13 @@ runnable scripting::create_runnable(
|
|||||||
const std::string& src,
|
const std::string& src,
|
||||||
const std::string& file
|
const std::string& file
|
||||||
) {
|
) {
|
||||||
return [=](){
|
try {
|
||||||
try {
|
state->loadbuffer(*env, src, file);
|
||||||
state->execute(*env, src, file);
|
return state->createLambda();
|
||||||
} catch (const lua::luaerror& err) {
|
} catch (const lua::luaerror& err) {
|
||||||
std::cerr << err.what() << std::endl;
|
logger.error() << err.what();
|
||||||
}
|
return [](){};
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool processCallback(
|
static bool processCallback(
|
||||||
|
|||||||
Reference in New Issue
Block a user