Lua 源代码表 |
|
一些描述实现的文章
以下是如何在 Lua 之外仅使用 Lua 的表实现。
/* tabletest.c - tested on Lua 5.1.3 */ #include "ltable.h" #include <stdio.h> #include <stdlib.h> #include <assert.h> /* Error handling */ void luaG_runerror (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); exit(1); } void *luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); return NULL; } /* Memory allocation */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { lua_assert((osize == 0) == (block == NULL)); if (nsize == 0) { free(block); block = NULL; } else block = realloc(block, nsize); if (block == NULL && nsize > 0) luaG_runerror(L, "not enough memory"); lua_assert((nsize == 0) == (block == NULL)); return block; } /* There is no global state (L) to link allocated tables to, so do nothing. */ void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { } void luaC_barrierback (lua_State *L, Table *t) { } int main() { Table * h; lua_State * L = NULL; int i; h = luaH_new (NULL, 0, 0); /* h[5] = 10 */ setnvalue(luaH_setnum(L, h, 5), 10); assert(luaH_getn(h) == 0); /* h[1] = 2 */ setnvalue(luaH_setnum(L, h, 1), 2); assert(luaH_getn(h) == 1); /* iterate over table keys 1..10 */ for(i=1; i<=10; i++) { const TValue * v = luaH_getnum(h, i); /* = h[i] */ double f = nvalue(v); assert(f == ((i == 1) ? 2 : (i == 5) ? 10 : 0)); assert(v != NULL || f == 0); printf("%d = %f\n", i, f); } luaH_free(L, h); printf("done\n"); return 0; }
此外,在 lobject.c 中,在 pushstr
、luaO_pushvfstring
、luaO_pushfstring
和 luaO_chunkid
函数周围添加 "#ifndef SKIP ..... #endif" 以避免由于额外依赖项导致的链接错误。
使用以下命令编译
gcc -DSKIP tabletest.c ltable.c lobject.c