aboutsummaryrefslogtreecommitdiff
path: root/ltests.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-11-09 17:05:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-11-09 17:05:42 -0300
commit024f9064f1b43758eb36aba52547edc0312bf4ba (patch)
tree9d8609112058e885196a581f0736fbdd94f7f94d /ltests.c
parent7f4906f565ab9f8b1125107a3abae3d759f3ecf2 (diff)
downloadlua-024f9064f1b43758eb36aba52547edc0312bf4ba.tar.gz
lua-024f9064f1b43758eb36aba52547edc0312bf4ba.tar.bz2
lua-024f9064f1b43758eb36aba52547edc0312bf4ba.zip
External strings
Strings can use external buffers to store their contents.
Diffstat (limited to 'ltests.c')
-rw-r--r--ltests.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/ltests.c b/ltests.c
index 6f556dc9..94bd4e33 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1277,6 +1277,37 @@ static int checkpanic (lua_State *L) {
1277} 1277}
1278 1278
1279 1279
1280static 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*/
1293static 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