Lua function calling.

develop
Warmist 2011-12-04 18:45:34 +02:00
parent c0a6036cde
commit e7a8c8c684
3 changed files with 56 additions and 0 deletions

@ -18,6 +18,7 @@
#include "lua_Misc.h"
#include "lua_VersionInfo.h"
#include "functioncall.h"
#include "lua_FunctionCall.h"
using std::vector;
using std::string;
@ -45,6 +46,8 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
lua::RegisterHexsearch(st);
lua::RegisterMisc(st);
lua::RegisterVersionInfo(st);
lua::RegisterFunctionCall(st);
#ifdef LINUX_BUILD
st.push(1);
st.setglobal("LINUX");

@ -0,0 +1,14 @@
#ifndef LUA_FUNCTIONCALL__H
#define LUA_FUNCTIONCALL__H
#include "luamain.h"
#include "functioncall.h"
namespace lua
{
void RegisterFunctionCall(lua::state &st);
}
#endif

@ -0,0 +1,39 @@
#include "lua_FunctionCall.h"
using namespace lua;
int lua_FunctionCall(lua_State *L)
{
lua::state st(L);
FunctionCaller cl(0);
size_t ptr=st.as<size_t>(1);
int callconv=st.as<size_t>(2);
vector<int> arguments;
for(int i=3;i<st.gettop();i++)
arguments.push_back(st.as<int>(i));
int ret=cl.CallFunction(ptr,(FunctionCaller::callconv)callconv,arguments);
st.push(ret);
return 1;// dunno if len is needed...
}
const luaL_Reg lua_functioncall_func[]=
{
{"call",lua_FunctionCall},
{NULL,NULL}
};
#define __ADDCONST(name) st.push(::FunctionCaller:: name); st.setglobal(#name)
void lua::RegisterFunctionCall(lua::state &st)
{
st.getglobal("FunctionCall");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
__ADDCONST(STD_CALL);
__ADDCONST(FAST_CALL);
__ADDCONST(THIS_CALL);
__ADDCONST(CDECL_CALL);
lua::RegFunctionsLocal(st, lua_functioncall_func);
st.setglobal("FunctionCall");
}
#undef __ADDCONST