Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lua_kernel_base as a super class of game_lua_kernel
This class holds some "basic" initialization and methods that we would want in any lua kernel. It is not pure virtual, it could be instantiated.
- Loading branch information
Showing
6 changed files
with
175 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright (C) 2014 by Chris Beck <render787@gmail.com> | ||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#include "scripting/lua_kernel_base.hpp" | ||
|
||
#include "global.hpp" | ||
|
||
#include "log.hpp" | ||
#include "lua/lauxlib.h" | ||
#include "lua/lua.h" | ||
#include "lua/lualib.h" | ||
|
||
#ifdef DEBUG_LUA | ||
#include "scripting/debug_lua.hpp" | ||
#endif | ||
|
||
#include "scripting/lua_api.hpp" | ||
|
||
#include <cstring> | ||
#include <string> | ||
|
||
static lg::log_domain log_scripting_lua("scripting/lua"); | ||
#define LOG_LUA LOG_STREAM(info, log_scripting_lua) | ||
#define WRN_LUA LOG_STREAM(warn, log_scripting_lua) | ||
#define ERR_LUA LOG_STREAM(err, log_scripting_lua) | ||
|
||
lua_kernel_base::lua_kernel_base() | ||
: mState(luaL_newstate()) | ||
{ | ||
lua_State *L = mState; | ||
|
||
// Open safe libraries. | ||
// Debug and OS are not, but most of their functions will be disabled below. | ||
static const luaL_Reg safe_libs[] = { | ||
{ "", luaopen_base }, | ||
{ "table", luaopen_table }, | ||
{ "string", luaopen_string }, | ||
{ "math", luaopen_math }, | ||
{ "debug", luaopen_debug }, | ||
{ "os", luaopen_os }, | ||
{ NULL, NULL } | ||
}; | ||
for (luaL_Reg const *lib = safe_libs; lib->func; ++lib) | ||
{ | ||
luaL_requiref(L, lib->name, lib->func, 1); | ||
lua_pop(L, 1); /* remove lib */ | ||
} | ||
|
||
// Disable functions from os which we don't want. | ||
lua_getglobal(L, "os"); | ||
lua_pushnil(L); | ||
while(lua_next(L, -2) != 0) { | ||
lua_pop(L, 1); | ||
char const* function = lua_tostring(L, -1); | ||
if(strcmp(function, "clock") == 0 || strcmp(function, "date") == 0 | ||
|| strcmp(function, "time") == 0 || strcmp(function, "difftime") == 0) continue; | ||
lua_pushnil(L); | ||
lua_setfield(L, -3, function); | ||
} | ||
lua_pop(L, 1); | ||
|
||
// Disable functions from debug which we don't want. | ||
lua_getglobal(L, "debug"); | ||
lua_pushnil(L); | ||
while(lua_next(L, -2) != 0) { | ||
lua_pop(L, 1); | ||
char const* function = lua_tostring(L, -1); | ||
if(strcmp(function, "traceback") == 0) continue; | ||
lua_pushnil(L); | ||
lua_setfield(L, -3, function); | ||
} | ||
lua_pop(L, 1); | ||
|
||
lua_settop(L, 0); | ||
} | ||
|
||
lua_kernel_base::~lua_kernel_base() | ||
{ | ||
lua_close(mState); | ||
} | ||
|
||
/** | ||
* Runs a script on a stack containing @a nArgs arguments. | ||
* @return true if the script was successful and @a nRets return values are available. | ||
*/ | ||
bool lua_kernel_base::execute(char const *prog, int nArgs, int nRets) | ||
{ | ||
lua_State *L = mState; | ||
|
||
// Compile script into a variadic function. | ||
int res = luaL_loadstring(L, prog); | ||
if (res) | ||
{ | ||
char const *m = lua_tostring(L, -1); | ||
chat_message("Lua error", m); | ||
ERR_LUA << m << '\n'; | ||
lua_pop(L, 1); | ||
return false; | ||
} | ||
|
||
// Place the function before its arguments. | ||
if (nArgs) | ||
lua_insert(L, -1 - nArgs); | ||
|
||
return luaW_pcall(L, nArgs, nRets); | ||
} | ||
|
||
/** | ||
* Loads the "package" package into the Lua environment. | ||
* This action is inherently unsafe, as Lua scripts will now be able to | ||
* load C libraries on their own, hence granting them the same privileges | ||
* as the Wesnoth binary itsef. | ||
*/ | ||
void lua_kernel_base::load_package() | ||
{ | ||
lua_State *L = mState; | ||
lua_pushcfunction(L, luaopen_package); | ||
lua_pushstring(L, "package"); | ||
lua_call(L, 1, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright (C) 2014 by Chris Beck <render787@gmail.com> | ||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#ifndef SCRIPTING_LUA_KERNEL_BASE_HPP | ||
#define SCRIPTING_LUA_KERNEL_BASE_HPP | ||
|
||
#include <string> // for string | ||
|
||
struct lua_State; | ||
|
||
class lua_kernel_base { | ||
protected: | ||
lua_State *mState; | ||
bool execute(char const *, int, int); | ||
public: | ||
lua_kernel_base(); | ||
~lua_kernel_base(); | ||
|
||
/** Runs a plain script. */ | ||
void run(char const *prog) { execute(prog, 0, 0); } | ||
|
||
void load_package(); | ||
}; | ||
|
||
#endif |