From 14e416355f83cf0a1b871eedec2c92b86dbe76d6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 5 Sep 2023 15:30:45 -0300 Subject: Added suport for Fixed Buffers A fixed buffer keeps a binary chunk "forever", so that the program does not need to copy some of its parts when loading it. --- lbaselib.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index 1d60c9de..5a23c937 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -337,9 +337,20 @@ static int load_aux (lua_State *L, int status, int envidx) { } +static const char *getmode (lua_State *L, int idx) { + const char *mode = luaL_optstring(L, idx, "bt"); + int i = 0; + if (mode[i] == 'b') i++; + if (mode[i] == 't') i++; + if (mode[i] != '\0') + luaL_argerror(L, idx, "invalid mode"); + return mode; +} + + static int luaB_loadfile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); + const char *mode = getmode(L, 2); int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ int status = luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); @@ -388,7 +399,7 @@ static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); + const char *mode = getmode(L, 3); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); -- cgit v1.2.3-55-g6feb