diff options
Diffstat (limited to 'ltests.c')
-rw-r--r-- | ltests.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -1277,6 +1277,37 @@ static int checkpanic (lua_State *L) { | |||
1277 | } | 1277 | } |
1278 | 1278 | ||
1279 | 1279 | ||
1280 | static int externKstr (lua_State *L) { | ||
1281 | size_t len; | ||
1282 | const char *s = luaL_checklstring(L, 1, &len); | ||
1283 | lua_pushextlstring(L, s, len, NULL, NULL); | ||
1284 | return 1; | ||
1285 | } | ||
1286 | |||
1287 | |||
1288 | /* | ||
1289 | ** Create a buffer with the content of a given string and then | ||
1290 | ** create an external string using that buffer. Use the allocation | ||
1291 | ** function from Lua to create and free the buffer. | ||
1292 | */ | ||
1293 | static int externstr (lua_State *L) { | ||
1294 | size_t len; | ||
1295 | const char *s = luaL_checklstring(L, 1, &len); | ||
1296 | void *ud; | ||
1297 | lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */ | ||
1298 | /* create the buffer */ | ||
1299 | char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1)); | ||
1300 | if (buff == NULL) { /* memory error? */ | ||
1301 | lua_pushliteral(L, "not enough memory"); | ||
1302 | lua_error(L); /* raise a memory error */ | ||
1303 | } | ||
1304 | /* copy string content to buffer, including ending 0 */ | ||
1305 | memcpy(buff, s, (len + 1) * sizeof(char)); | ||
1306 | /* create external string */ | ||
1307 | lua_pushextlstring(L, buff, len, allocf, ud); | ||
1308 | return 1; | ||
1309 | } | ||
1310 | |||
1280 | 1311 | ||
1281 | /* | 1312 | /* |
1282 | ** {==================================================================== | 1313 | ** {==================================================================== |
@@ -1949,6 +1980,8 @@ static const struct luaL_Reg tests_funcs[] = { | |||
1949 | {"udataval", udataval}, | 1980 | {"udataval", udataval}, |
1950 | {"unref", unref}, | 1981 | {"unref", unref}, |
1951 | {"upvalue", upvalue}, | 1982 | {"upvalue", upvalue}, |
1983 | {"externKstr", externKstr}, | ||
1984 | {"externstr", externstr}, | ||
1952 | {NULL, NULL} | 1985 | {NULL, NULL} |
1953 | }; | 1986 | }; |
1954 | 1987 | ||