elua: correctly wrap gettext funcs
authorDaniel Kolesa <d.kolesa@osg.samsung.com>
Fri, 24 Nov 2017 09:53:27 +0000 (10:53 +0100)
committerDaniel Kolesa <d.kolesa@osg.samsung.com>
Fri, 24 Nov 2017 09:55:46 +0000 (10:55 +0100)
Now, we cannot directly register funcs defined by a different
signature than the lua standard (int (*)(lua_State *)) so we
have to correctly wrap those with proper conversions etc.

src/lib/elua/elua.c
src/scripts/elua/core/gettext.lua

index 44b5c32..3f3654e 100644 (file)
@@ -360,14 +360,43 @@ _elua_get_localeconv(lua_State *L)
    return 1;
 };
 
+#ifdef ENABLE_NLS
+static int
+_elua_dgettext(lua_State *L)
+{
+   const char *domain = luaL_checkstring(L, 1);
+   const char *msgid  = luaL_checkstring(L, 2);
+   char *ret = dgettext(domain, msgid);
+   if (!ret)
+     lua_pushnil(L);
+   else
+     lua_pushstring(L, ret);
+   return 1;
+}
+
+static int
+_elua_dngettext(lua_State *L)
+{
+   const char *domain  = luaL_checkstring(L, 1);
+   const char *msgid   = luaL_checkstring(L, 2);
+   const char *plmsgid = luaL_checkstring(L, 3);
+   char *ret = dngettext(domain, msgid, plmsgid, luaL_checklong(L, 4));
+   if (!ret)
+     lua_pushnil(L);
+   else
+     lua_pushstring(L, ret);
+   return 1;
+}
+#endif
+
 const luaL_Reg gettextlib[] =
 {
    { "bind_textdomain", _elua_gettext_bind_textdomain },
    { "get_message_language", _elua_get_message_language },
    { "get_localeconv", _elua_get_localeconv },
 #ifdef ENABLE_NLS
-   { "dgettext", dgettext },
-   { "dgettext", dngettext },
+   { "dgettext", _elua_dgettext },
+   { "dngettext", _elua_dngettext },
 #endif
    { NULL, NULL }
 };
index 96af7d3..3659b4e 100644 (file)
@@ -1,7 +1,5 @@
 -- elua gettext module
 
-local ffi = require("ffi")
-
 local M = {}
 
 local gettext = ...
@@ -10,12 +8,6 @@ local bind_textdomain         = gettext.bind_textdomain
 local dgettext                = gettext.dgettext
 local dngettext               = gettext.dngettext
 
-if  dgettext then
-    dgettext  = ffi.cast("char *(*)(const char*, const char*)", dgettext)
-    dngettext = ffi.cast("char *(*)(const char*, const char*, const char*, "
-        .. "unsigned long)", dngettext)
-end
-
 local domains = {}
 
 local default_domain
@@ -39,7 +31,6 @@ M.set_default_domain = function(dom)
     return true
 end
 
-local cast, ffistr = ffi.cast, ffi.string
 local floor = math.floor
 local type = type
 
@@ -50,12 +41,11 @@ if dgettext then
             dom   = default_domain
         end
         if not domains[dom] or not msgid then return msgid end
-        local cmsgid = cast("const char*", msgid)
-        local lmsgid = dgettext(dom, cmsgid)
-        if cmsgid == lmsgid then
+        local lmsgid = dgettext(dom, msgid)
+        if msgid == lmsgid then
             return msgid
         end
-        return ffistr(lmsgid)
+        return lmsgid
     end
     M.dgettext = M.gettext
     M.ngettext = function(dom, msgid, plmsgid, n)
@@ -69,15 +59,13 @@ if dgettext then
             if not msgid or n == 1 then return msgid end
             return plmsgid
         end
-        local cmsgid   = cast("const char*", msgid)
-        local cplmsgid = cast("const char*", plmsgid)
-        local lmsgid   = dngettext(dom, cmsgid, cplmsgid, n)
+        local lmsgid = dngettext(dom, msgid, plmsgid, n)
         if n == 1 then
-            if cmsgid == lmsgid then return msgid end
+            if msgid == lmsgid then return msgid end
         else
-            if cplmsgid == lmsgid then return plmsgid end
+            if plmsgid == lmsgid then return plmsgid end
         end
-        return ffistr(lmsgid)
+        return lmsgid
     end
     M.dngettext = M.ngettext
 else