diff options
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/attrib.lua | 8 | ||||
| -rw-r--r-- | testes/libs/lib22.c | 51 |
2 files changed, 58 insertions, 1 deletions
diff --git a/testes/attrib.lua b/testes/attrib.lua index d8b6e0f3..8a3462ea 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua | |||
| @@ -300,6 +300,12 @@ else | |||
| 300 | assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") | 300 | assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") |
| 301 | assert(lib2.id("x") == true) -- a different "id" implementation | 301 | assert(lib2.id("x") == true) -- a different "id" implementation |
| 302 | 302 | ||
| 303 | for _, len in ipairs{0, 10, 39, 40, 41, 1000} do | ||
| 304 | local str = string.rep("a", len) | ||
| 305 | local str1 = lib2.newstr(str) | ||
| 306 | assert(str == str1) | ||
| 307 | end | ||
| 308 | |||
| 303 | -- test C submodules | 309 | -- test C submodules |
| 304 | local fs, ext = require"lib1.sub" | 310 | local fs, ext = require"lib1.sub" |
| 305 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") | 311 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") |
| @@ -447,7 +453,7 @@ do | |||
| 447 | end | 453 | end |
| 448 | 454 | ||
| 449 | 455 | ||
| 450 | -- test of large float/integer indices | 456 | -- test of large float/integer indices |
| 451 | 457 | ||
| 452 | -- compute maximum integer where all bits fit in a float | 458 | -- compute maximum integer where all bits fit in a float |
| 453 | local maxint = math.maxinteger | 459 | local maxint = math.maxinteger |
diff --git a/testes/libs/lib22.c b/testes/libs/lib22.c index 8e656502..b377cce5 100644 --- a/testes/libs/lib22.c +++ b/testes/libs/lib22.c | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | /* implementation for lib2-v2 */ | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 1 | #include "lua.h" | 5 | #include "lua.h" |
| 2 | #include "lauxlib.h" | 6 | #include "lauxlib.h" |
| 3 | 7 | ||
| @@ -8,8 +12,54 @@ static int id (lua_State *L) { | |||
| 8 | } | 12 | } |
| 9 | 13 | ||
| 10 | 14 | ||
| 15 | struct STR { | ||
| 16 | void *ud; | ||
| 17 | lua_Alloc allocf; | ||
| 18 | }; | ||
| 19 | |||
| 20 | |||
| 21 | static void *t_freestr (void *ud, void *ptr, size_t osize, size_t nsize) { | ||
| 22 | struct STR *blk = (struct STR*)ptr - 1; | ||
| 23 | blk->allocf(blk->ud, blk, sizeof(struct STR) + osize, 0); | ||
| 24 | return NULL; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | static int newstr (lua_State *L) { | ||
| 29 | size_t len; | ||
| 30 | const char *str = luaL_checklstring(L, 1, &len); | ||
| 31 | void *ud; | ||
| 32 | lua_Alloc allocf = lua_getallocf(L, &ud); | ||
| 33 | struct STR *blk = (struct STR*)allocf(ud, NULL, 0, | ||
| 34 | len + 1 + sizeof(struct STR)); | ||
| 35 | if (blk == NULL) { /* allocation error? */ | ||
| 36 | lua_pushliteral(L, "not enough memory"); | ||
| 37 | lua_error(L); /* raise a memory error */ | ||
| 38 | } | ||
| 39 | blk->ud = ud; blk->allocf = allocf; | ||
| 40 | memcpy(blk + 1, str, len + 1); | ||
| 41 | lua_pushexternalstring(L, (char *)(blk + 1), len, t_freestr, L); | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | /* | ||
| 47 | ** Create an external string and keep it in the registry, so that it | ||
| 48 | ** will test that the library code is still available (to deallocate | ||
| 49 | ** this string) when closing the state. | ||
| 50 | */ | ||
| 51 | static void initstr (lua_State *L) { | ||
| 52 | lua_pushcfunction(L, newstr); | ||
| 53 | lua_pushstring(L, | ||
| 54 | "012345678901234567890123456789012345678901234567890123456789"); | ||
| 55 | lua_call(L, 1, 1); /* call newstr("0123...") */ | ||
| 56 | luaL_ref(L, LUA_REGISTRYINDEX); /* keep string in the registry */ | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 11 | static const struct luaL_Reg funcs[] = { | 60 | static const struct luaL_Reg funcs[] = { |
| 12 | {"id", id}, | 61 | {"id", id}, |
| 62 | {"newstr", newstr}, | ||
| 13 | {NULL, NULL} | 63 | {NULL, NULL} |
| 14 | }; | 64 | }; |
| 15 | 65 | ||
| @@ -18,6 +68,7 @@ LUAMOD_API int luaopen_lib2 (lua_State *L) { | |||
| 18 | lua_settop(L, 2); | 68 | lua_settop(L, 2); |
| 19 | lua_setglobal(L, "y"); /* y gets 2nd parameter */ | 69 | lua_setglobal(L, "y"); /* y gets 2nd parameter */ |
| 20 | lua_setglobal(L, "x"); /* x gets 1st parameter */ | 70 | lua_setglobal(L, "x"); /* x gets 1st parameter */ |
| 71 | initstr(L); | ||
| 21 | luaL_newlib(L, funcs); | 72 | luaL_newlib(L, funcs); |
| 22 | return 1; | 73 | return 1; |
| 23 | } | 74 | } |
