lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Paul Moore wrote:
> I am interfacing Lua to an application which uses Unicode exclusively
> for all of its internal processing (specifically, Win32 wide
> characters). The application APIs all work in terms of wide
> characters. So, at the boundary between Lua and the application, I
> need to convert data between char and wchar_t types.    
> 
> [...]
> 
> I've written helper functions
>     int luaU_pushunicode(lua_State *L, wchar_t *str)
>     wchar_t *luaU_checkunicode(lua_State *L, int index)
> 
> modelled on the lua equivalents for strings. However, I still find
> places where I need char* arguments rather than strings on the stack
> (one notable place is when I define a lua_Reader, another is
> luaL_loadbuffer).   

You can use the Lua function 'loadstring' [1] instead of those from the
C API. Example (I'm assuming your pushunicode pushes the unicode string
as a multibyte ascii-compatible Lua string):

int top = lua_gettop(L);
lua_getglobal(L, "loadstring");
luaU_pushunicode(L, some_unicode_string);
lua_call(L, 1, LUA_MULTRET);
if (lua_isnil(L, top+1))
    return luaL_error(L, lua_tostring(L, top+2));
lua_settop(L, top+1);
/* from here you have the compiled chunk on the top of the stack */


[1] http://www.lua.org/manual/5.1/manual.html#pdf-loadstring