2014-12-08 03:38:04 -08:00
|
|
|
#include "Elua.h"
|
2014-12-11 07:48:18 -08:00
|
|
|
#include "elua_private.h"
|
|
|
|
|
|
|
|
static Eina_Prefix *_elua_pfx = NULL;
|
|
|
|
|
|
|
|
static int _elua_init_counter = 0;
|
|
|
|
int _elua_log_dom = -1;
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
elua_init(void)
|
|
|
|
{
|
|
|
|
const char *dom = "elua";
|
|
|
|
if (_elua_init_counter > 0) return ++_elua_init_counter;
|
|
|
|
|
|
|
|
eina_init();
|
|
|
|
_elua_log_dom = eina_log_domain_register(dom, EINA_COLOR_LIGHTBLUE);
|
|
|
|
if (_elua_log_dom < 0)
|
|
|
|
{
|
|
|
|
EINA_LOG_ERR("Could not register log domain: %s", dom);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
eina_log_timing(_elua_log_dom, EINA_LOG_STATE_STOP, EINA_LOG_STATE_INIT);
|
|
|
|
INF("elua init");
|
|
|
|
|
2014-12-11 08:41:24 -08:00
|
|
|
_elua_pfx = eina_prefix_new(NULL, elua_init, "ELUA", "elua", "checkme",
|
2014-12-11 07:48:18 -08:00
|
|
|
PACKAGE_BIN_DIR, "", PACKAGE_DATA_DIR,
|
|
|
|
LOCALE_DIR);
|
|
|
|
|
|
|
|
if (!_elua_pfx)
|
|
|
|
{
|
|
|
|
ERR("coul not find elua prefix");
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ++_elua_init_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
elua_shutdown(void)
|
|
|
|
{
|
|
|
|
if (_elua_init_counter <= 0)
|
|
|
|
{
|
|
|
|
EINA_LOG_ERR("Init count not greater than 0 in shutdown.");
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
--_elua_init_counter;
|
|
|
|
|
|
|
|
if (_elua_init_counter > 0)
|
|
|
|
return _elua_init_counter;
|
|
|
|
|
|
|
|
INF("shutdown");
|
|
|
|
eina_log_timing(_elua_log_dom, EINA_LOG_STATE_START, EINA_LOG_STATE_SHUTDOWN);
|
|
|
|
|
|
|
|
eina_prefix_free(_elua_pfx);
|
|
|
|
_elua_pfx = NULL;
|
|
|
|
|
|
|
|
eina_log_domain_unregister(_elua_log_dom);
|
|
|
|
_elua_log_dom = -1;
|
|
|
|
|
|
|
|
eina_shutdown();
|
|
|
|
return _elua_init_counter;
|
|
|
|
}
|
2014-12-12 02:44:58 -08:00
|
|
|
|
2015-04-08 06:00:57 -07:00
|
|
|
EAPI Elua_State *
|
|
|
|
elua_state_new(void)
|
|
|
|
{
|
|
|
|
Elua_State *ret = NULL;
|
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
if (!L)
|
|
|
|
return NULL;
|
|
|
|
ret = malloc(sizeof(Elua_State));
|
|
|
|
ret->luastate = L;
|
|
|
|
luaL_openlibs(L);
|
2015-04-08 06:29:58 -07:00
|
|
|
lua_pushlightuserdata(L, ret);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "elua_ptr");
|
2015-04-08 06:00:57 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
2015-04-08 06:52:06 -07:00
|
|
|
elua_state_free(Elua_State *es)
|
2015-04-08 06:00:57 -07:00
|
|
|
{
|
2015-04-08 06:52:06 -07:00
|
|
|
if (!es) return;
|
|
|
|
if (es->luastate) lua_close(es->luastate);
|
|
|
|
free(es);
|
2015-04-08 06:00:57 -07:00
|
|
|
}
|
|
|
|
|
2015-04-08 06:29:58 -07:00
|
|
|
EAPI Elua_State *
|
|
|
|
elua_state_from_lua_get(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "elua_ptr");
|
|
|
|
if (!lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
void *st = lua_touserdata(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return (Elua_State *)st;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:43:50 -08:00
|
|
|
static void
|
|
|
|
_elua_errmsg(const char *pname, const char *msg)
|
|
|
|
{
|
|
|
|
ERR("%s%s%s", pname ? pname : "", pname ? ": " : "", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
2015-04-08 06:43:52 -07:00
|
|
|
elua_report_error(Elua_State *es, const char *pname, int status)
|
2014-12-17 06:43:50 -08:00
|
|
|
{
|
2015-04-08 06:43:52 -07:00
|
|
|
if (!es || !es->luastate) return status;
|
|
|
|
if (status && !lua_isnil(es->luastate, -1))
|
2014-12-17 06:43:50 -08:00
|
|
|
{
|
2015-04-08 06:43:52 -07:00
|
|
|
const char *msg = lua_tostring(es->luastate, -1);
|
2014-12-17 06:43:50 -08:00
|
|
|
_elua_errmsg(pname, msg ? msg : "(non-string error)");
|
2015-04-08 06:43:52 -07:00
|
|
|
lua_pop(es->luastate, 1);
|
2014-12-17 06:43:50 -08:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-12-12 02:44:58 -08:00
|
|
|
static int
|
|
|
|
_elua_gettext_bind_textdomain(lua_State *L)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
const char *textdomain = luaL_checkstring(L, 1);
|
|
|
|
const char *dirname = luaL_checkstring(L, 2);
|
|
|
|
const char *ret;
|
|
|
|
if (!textdomain[0] || !strcmp(textdomain, PACKAGE))
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushliteral(L, "invalid textdomain");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (!(ret = bindtextdomain(textdomain, dirname)))
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, strerror(errno));
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
bind_textdomain_codeset(textdomain, "UTF-8");
|
|
|
|
lua_pushstring(L, ret);
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
lua_pushliteral(L, "");
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const luaL_reg gettextlib[] =
|
|
|
|
{
|
|
|
|
{ "bind_textdomain", _elua_gettext_bind_textdomain },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
EAPI void
|
2015-04-08 06:52:06 -07:00
|
|
|
elua_state_setup_i18n(Elua_State *es)
|
2014-12-12 02:44:58 -08:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
char *(*dgettextp)(const char*, const char*) = dgettext;
|
|
|
|
char *(*dngettextp)(const char*, const char*, const char*, unsigned long)
|
|
|
|
= dngettext;
|
|
|
|
#endif
|
2015-04-08 06:52:06 -07:00
|
|
|
lua_createtable(es->luastate, 0, 0);
|
|
|
|
luaL_register(es->luastate, NULL, gettextlib);
|
2014-12-12 02:44:58 -08:00
|
|
|
#ifdef ENABLE_NLS
|
2015-04-08 06:52:06 -07:00
|
|
|
lua_pushlightuserdata(es->luastate, *((void**)&dgettextp));
|
|
|
|
lua_setfield(es->luastate, -2, "dgettext");
|
|
|
|
lua_pushlightuserdata(es->luastate, *((void**)&dngettextp));
|
|
|
|
lua_setfield(es->luastate, -2, "dngettext");
|
2014-12-12 02:44:58 -08:00
|
|
|
#endif
|
|
|
|
}
|