lua-users home
lua-l archive

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


Thanks mike,

after I write this, I fall.
-------------------------------
#include "lua.h"
#include "lauxlib.h"

LUA_API int luaopen_misc(lua_State *L)
{
lua_CFunction tosf;
lua_getglobal( L, "tostring" );
printf("get tostring from global, type(%s)\n", lua_typename(L,-1));
printf("get tostring from global, tostring(%s)\n", lua_tostring(L,-1));
tosf = lua_tocfunction( L, -1 );
printf("get tostring lua_CFunction pointer(%p)\n", tosf);
lua_pop(L,1);

lua_getglobal(L,"_G");
printf("_G tostring(%s)\n",lua_tostring(L,-1));
lua_getfield( L, -1, "tostring" );
printf("get tostring from global, type(%s)\n", lua_typename(L,-1));
printf("get tostring from global, tostring(%s)\n", lua_tostring(L,-1));
tosf = lua_tocfunction( L, -1 );
printf("get tostring lua_CFunction pointer(%p)\n", tosf);
lua_pop(L,1);

return 0;
}
---------------------------------
After run I got:
LuaJIT 2.0.0-beta5 -- Copyright (C) 2005-2010 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE4.1 fold cse dce fwd dse narrow loop abc fuse
> require "misc"
get tostring from global, type(no value)
get tostring from global, tostring((null))
get tostring lua_CFunction pointer(00000000)
_G tostring((null))
get tostring from global, type(no value)
get tostring from global, tostring((null))
get tostring lua_CFunction pointer(00000000)
>
-----------------------------------

2010/11/30 Mike Pall <mikelu-1011@mike.de>
zhiguo zhao wrote:
> Why I first require lanes failed, but it works after call tostring?

Nope, it doesn't really work. It probably just doesn't print the
error the 2nd time.

> > print(tostring)
> function: fast#14

Lanes tries to do some strange things with the tostring()
function. It expects this to be a plain C function which can be
called from a different state. But tostring() is not a plain C
function in LuaJIT (as you can see above).

>From looking at the Lanes source code, it seems to use the string
returned from tostring() as a key to a cache for tables. I won't
go into why that is an exceptionally bad idea -- the fact that
tostring() will call any __tostring metamethods should be enough
to discredit that approach. IOW: Lanes needs to be fixed.

--Mike