[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re[2]: Unique object references
- From: Cloud Wu <cloudwu@...>
- Date: Sun, 10 Dec 2006 00:04:51 +0800
Hello Luiz,
Saturday, December 9, 2006, 7:24:33 PM, you wrote:
>> Is there any way to get a state-wide unique reference to any
>> particular object in the state?
LHdF> The value itself is a good reference. Or you can try lua_topointer.
LHdF> --lhf
Before a lua object collected, the result of lua_topointer is never
changed. Am I right?
Can you guarantee this will not be changed in future version of lua ?
;) I saw some other language may move the objects during the garbage
collection.
I have another question.
When I use lua_newuserdata(or lua_touserdata) to get a pointer to the
memory , can I save it in my C struct directly? or I should use
lua_tousedata to convert the userdata to a pointer every time I need ?
For example:
struct array {
int n;
int *data;
};
int array_create(lua_State *L)
{
int n = luaL_checkint(L, 1);
struct array* ret=(struct array*)lua_newuserdata(L,sizeof(*ret));
lua_createtable(L,1,0);
ret->data=(int*)lua_newuserdata(L,sizeof(int)*n);
lua_rawseti(L,-2,1);
lua_setfenv(L,-2);
return 1;
}
int array_set(lua_State *L)
{
struct array* a=(struct array*)lua_touserdata(L, 1);
int n = luaL_checkint(L, 2);
int d = luaL_checkint(L, 3);
a->data[n]=d;
return 0;
}
--
Best regards,
Cloud mailto:cloudwu@163.com
http://blog.codingnow.com
[不患莫己知,求为可知也]