Skip to content

Commit

Permalink
Merge branch 'lua'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Dec 25, 2014
2 parents c20ef0f + 0f9bd0a commit 94006f7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 51 deletions.
15 changes: 1 addition & 14 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -3858,20 +3858,7 @@ game_lua_kernel::game_lua_kernel(const config &cfg, CVideo * video, game_state &
lua_rawset(L, LUA_REGISTRYINDEX);

// Create the vconfig metatable.
cmd_log_ << "Adding vconfig metatable...\n";

lua_pushlightuserdata(L
, vconfigKey);
lua_createtable(L, 0, 4);
lua_pushcfunction(L, lua_common::impl_vconfig_collect);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, lua_common::impl_vconfig_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua_common::impl_vconfig_size);
lua_setfield(L, -2, "__len");
lua_pushstring(L, "wml object");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);
cmd_log_ << lua_common::register_vconfig_metatable(L);

// Create the ai elements table.
cmd_log_ << "Adding ai elements table...\n";
Expand Down
73 changes: 66 additions & 7 deletions src/scripting/lua_common.cpp
Expand Up @@ -47,7 +47,7 @@ namespace lua_common {
* - Arg 2: string to translate.
* - Ret 1: string containing the translatable string.
*/
int impl_gettext(lua_State *L)
static int impl_gettext(lua_State *L)
{
char const *m = luaL_checkstring(L, 2);
char const *d = static_cast<char *>(lua_touserdata(L, 1));
Expand Down Expand Up @@ -104,7 +104,7 @@ static void tstring_concat_aux(lua_State *L, t_string &dst, int src)
/**
* Appends a scalar to a t_string object (__concat metamethod).
*/
int impl_tstring_concat(lua_State *L)
static int impl_tstring_concat(lua_State *L)
{
// Create a new t_string.
t_string *t = new(lua_newuserdata(L, sizeof(t_string))) t_string;
Expand All @@ -125,7 +125,7 @@ int impl_tstring_concat(lua_State *L)
/**
* Destroys a t_string object before it is collected (__gc metamethod).
*/
int impl_tstring_collect(lua_State *L)
static int impl_tstring_collect(lua_State *L)
{
t_string *t = static_cast<t_string *>(lua_touserdata(L, 1));
t->t_string::~t_string();
Expand All @@ -136,7 +136,7 @@ int impl_tstring_collect(lua_State *L)
* Converts a t_string object to a string (__tostring metamethod);
* that is, performs a translation.
*/
int impl_tstring_tostring(lua_State *L)
static int impl_tstring_tostring(lua_State *L)
{
t_string *t = static_cast<t_string *>(lua_touserdata(L, 1));
lua_pushstring(L, t->c_str());
Expand All @@ -148,7 +148,7 @@ int impl_tstring_tostring(lua_State *L)
* Special fields __literal, __shallow_literal, __parsed, and
* __shallow_parsed, return Lua tables.
*/
int impl_vconfig_get(lua_State *L)
static int impl_vconfig_get(lua_State *L)
{
vconfig *v = static_cast<vconfig *>(lua_touserdata(L, 1));

Expand Down Expand Up @@ -219,7 +219,7 @@ int impl_vconfig_get(lua_State *L)
/**
* Returns the number of a child of a vconfig object.
*/
int impl_vconfig_size(lua_State *L)
static int impl_vconfig_size(lua_State *L)
{
vconfig *v = static_cast<vconfig *>(lua_touserdata(L, 1));
lua_pushinteger(L, v->null() ? 0 :
Expand All @@ -230,7 +230,7 @@ int impl_vconfig_size(lua_State *L)
/**
* Destroys a vconfig object before it is collected (__gc metamethod).
*/
int impl_vconfig_collect(lua_State *L)
static int impl_vconfig_collect(lua_State *L)
{
vconfig *v = static_cast<vconfig *>(lua_touserdata(L, 1));
v->vconfig::~vconfig();
Expand All @@ -249,6 +249,65 @@ int intf_tovconfig(lua_State *L)
return 1;
}

/**
* Adds the gettext metatable
*/
std::string register_gettext_metatable(lua_State *L)
{
lua_pushlightuserdata(L
, gettextKey);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, lua_common::impl_gettext);
lua_setfield(L, -2, "__call");
lua_pushstring(L, "message domain");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);

return "Adding gettext metatable...\n";
}

/**
* Adds the tstring metatable
*/
std::string register_tstring_metatable(lua_State *L)
{
lua_pushlightuserdata(L
, tstringKey);
lua_createtable(L, 0, 4);
lua_pushcfunction(L, impl_tstring_concat);
lua_setfield(L, -2, "__concat");
lua_pushcfunction(L, impl_tstring_collect);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, impl_tstring_tostring);
lua_setfield(L, -2, "__tostring");
lua_pushstring(L, "translatable string");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);

return "Adding tstring metatable...\n";
}

/**
* Adds the vconfig metatable
*/
std::string register_vconfig_metatable(lua_State *L)
{
lua_pushlightuserdata(L
, vconfigKey);
lua_createtable(L, 0, 4);
lua_pushcfunction(L, impl_vconfig_collect);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, impl_vconfig_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, impl_vconfig_size);
lua_setfield(L, -2, "__len");
lua_pushstring(L, "wml object");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);

return "Adding vconfig metatable...\n";
}

} // end namespace lua_common

void luaW_pushvconfig(lua_State *L, vconfig const &cfg)
Expand Down
11 changes: 4 additions & 7 deletions src/scripting/lua_common.hpp
Expand Up @@ -28,15 +28,12 @@ class vconfig;
#include "scripting/lua_types.hpp"

namespace lua_common {
int impl_gettext(lua_State *L);
int intf_textdomain(lua_State *L);
int impl_tstring_concat(lua_State *L);
int impl_tstring_collect(lua_State *L);
int impl_tstring_tostring(lua_State *L);
int impl_vconfig_get(lua_State *L);
int impl_vconfig_size(lua_State *L);
int impl_vconfig_collect(lua_State *L);
int intf_tovconfig(lua_State* L);

std::string register_gettext_metatable(lua_State *L);
std::string register_tstring_metatable(lua_State *L);
std::string register_vconfig_metatable(lua_State *L);
}

/**
Expand Down
25 changes: 2 additions & 23 deletions src/scripting/lua_kernel_base.cpp
Expand Up @@ -203,32 +203,11 @@ lua_kernel_base::lua_kernel_base(CVideo * video)
lua_pop(L, 1);

// Create the gettext metatable.
cmd_log_ << "Adding gettext metatable...\n";

lua_pushlightuserdata(L
, gettextKey);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, lua_common::impl_gettext);
lua_setfield(L, -2, "__call");
lua_pushstring(L, "message domain");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);
cmd_log_ << lua_common::register_gettext_metatable(L);

// Create the tstring metatable.
cmd_log_ << "Adding tstring metatable...\n";
cmd_log_ << lua_common::register_tstring_metatable(L);

lua_pushlightuserdata(L
, tstringKey);
lua_createtable(L, 0, 4);
lua_pushcfunction(L, lua_common::impl_tstring_concat);
lua_setfield(L, -2, "__concat");
lua_pushcfunction(L, lua_common::impl_tstring_collect);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, lua_common::impl_tstring_tostring);
lua_setfield(L, -2, "__tostring");
lua_pushstring(L, "translatable string");
lua_setfield(L, -2, "__metatable");
lua_rawset(L, LUA_REGISTRYINDEX);

lua_settop(L, 0);

Expand Down

0 comments on commit 94006f7

Please sign in to comment.