[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Using the C API
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Fri, 1 Feb 2008 11:01:03 -0500
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