标签:
1、概述userdata内存存储形式类似于字符串,以一个头开始然后紧随头后面保存相关的数据,但是每个userdata都拥有自己独立的元表。下面是userdata对应的头数据结构Udata代码(lobject.h):
431 /*
432 ** Header for userdata; memory area follows the end of this structure
433 */
434 typedef union Udata {
435 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
436 struct {
437 CommonHeader;
438 struct Table *metatable;
439 struct Table *env;
440 size_t len; /* number of bytes */
441 } uv;
442 } Udata;175 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
176 Udata *u;
177 if (s > MAX_SIZET - sizeof(Udata))
178 luaM_toobig(L);
179 u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
180 u->uv.len = s;
181 u->uv.metatable = NULL;
182 u->uv.env = e;
183 return u;
184 } 3、总结参考资料
Lua 5.2.1源码
《Lua程序设计》(第二版)
标签:
原文地址:http://blog.csdn.net/maximuszhou/article/details/45063619