Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt standard libraries to support memories by LuaMemory. #1

Draft
wants to merge 2 commits into
base: v5.3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "lauxlib.h"
#include "lualib.h"
#include "lmemlib.h"


static int luaB_print (lua_State *L) {
Expand All @@ -31,7 +32,7 @@ static int luaB_print (lua_State *L) {
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tolstring(L, -1, &l); /* get result */
s = luamem_tostring(L, -1, &l); /* get result */
if (s == NULL)
return luaL_error(L, "'tostring' must return a string to 'print'");
if (i>1) lua_writestring("\t", 1);
Expand Down Expand Up @@ -324,17 +325,17 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
*size = 0;
return NULL;
}
else if (!lua_isstring(L, -1))
luaL_error(L, "reader function must return a string");
else if (!luamem_isstring(L, -1))
luaL_error(L, "reader function must return a string or memory");
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
return lua_tolstring(L, RESERVEDSLOT, size);
return luamem_tostring(L, RESERVEDSLOT, size);
}


static int luaB_load (lua_State *L) {
int status;
size_t l;
const char *s = lua_tolstring(L, 1, &l);
const char *s = luamem_tostring(L, 1, &l);
const char *mode = luaL_optstring(L, 3, "bt");
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */
Expand Down
1 change: 1 addition & 0 deletions linit.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static const luaL_Reg loadedlibs[] = {
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_DBLIBNAME, luaopen_debug},
{LUA_MEMLIBNAME, luaopen_memory},
#if defined(LUA_COMPAT_BITLIB)
{LUA_BITLIBNAME, luaopen_bit32},
#endif
Expand Down
3 changes: 2 additions & 1 deletion liolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "lauxlib.h"
#include "lualib.h"
#include "lmemlib.h"



Expand Down Expand Up @@ -627,7 +628,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
}
else {
size_t l;
const char *s = luaL_checklstring(L, arg, &l);
const char *s = luamem_checkstring(L, arg, &l);
status = status && (fwrite(s, sizeof(char), l, f) == l);
}
}
Expand Down
66 changes: 42 additions & 24 deletions lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "lauxlib.h"
#include "lualib.h"
#include "lmemlib.h"


/*
Expand Down Expand Up @@ -69,7 +70,7 @@ static lua_Integer posrelat (lua_Integer pos, size_t len) {

static int str_sub (lua_State *L) {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
if (start < 1) start = 1;
Expand All @@ -84,7 +85,7 @@ static int str_sub (lua_State *L) {
static int str_reverse (lua_State *L) {
size_t l, i;
luaL_Buffer b;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i = 0; i < l; i++)
p[i] = s[l - i - 1];
Expand All @@ -97,7 +98,7 @@ static int str_lower (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i=0; i<l; i++)
p[i] = tolower(uchar(s[i]));
Expand All @@ -110,7 +111,7 @@ static int str_upper (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i=0; i<l; i++)
p[i] = toupper(uchar(s[i]));
Expand All @@ -121,9 +122,9 @@ static int str_upper (lua_State *L) {

static int str_rep (lua_State *L) {
size_t l, lsep;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
lua_Integer n = luaL_checkinteger(L, 2);
const char *sep = luaL_optlstring(L, 3, "", &lsep);
const char *sep = luamem_optstring(L, 3, "", &lsep);
if (n <= 0) lua_pushliteral(L, "");
else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
return luaL_error(L, "resulting string too large");
Expand All @@ -147,7 +148,7 @@ static int str_rep (lua_State *L) {

static int str_byte (lua_State *L) {
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
const char *s = luamem_checkstring(L, 1, &l);
lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l);
lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l);
int n, i;
Expand Down Expand Up @@ -342,7 +343,7 @@ static const char *matchbalance (MatchState *ms, const char *s,
const char *p) {
if (p >= ms->p_end - 1)
luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
if (*s != *p) return NULL;
if (s >= ms->src_end || *s != *p) return NULL;
else {
int b = *p;
int e = *(p+1);
Expand Down Expand Up @@ -455,14 +456,15 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
break;
}
case 'f': { /* frontier? */
const char *ep; char previous;
const char *ep; char previous, current;
p += 2;
if (*p != '[')
luaL_error(ms->L, "missing '[' after '%%f' in pattern");
ep = classend(ms, p); /* points to what is next */
previous = (s == ms->src_init) ? '\0' : *(s - 1);
current = (s == ms->src_end) ? '\0' : *s;
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
matchbracketclass(uchar(*s), p, ep - 1)) {
matchbracketclass(uchar(current), p, ep - 1)) {
p = ep; goto init; /* return match(ms, s, ep); */
}
s = NULL; /* match failed */
Expand Down Expand Up @@ -606,9 +608,10 @@ static void reprepstate (MatchState *ms) {

static int str_find_aux (lua_State *L, int find) {
size_t ls, lp;
const char *s = luaL_checklstring(L, 1, &ls);
const char *s = luamem_checkstring(L, 1, &ls);
const char *p = luaL_checklstring(L, 2, &lp);
lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
if (!s && !ls) s = p; /* force a non-null pointer for empty memory */
if (init < 1) init = 1;
else if (init > (lua_Integer)ls + 1) { /* start after string's end? */
lua_pushnil(L); /* cannot find anything */
Expand Down Expand Up @@ -688,7 +691,7 @@ static int gmatch_aux (lua_State *L) {

static int gmatch (lua_State *L) {
size_t ls, lp;
const char *s = luaL_checklstring(L, 1, &ls);
const char *s = luamem_checkstring(L, 1, &ls);
const char *p = luaL_checklstring(L, 2, &lp);
GMatchState *gm;
lua_settop(L, 2); /* keep them on closure to avoid being collected */
Expand All @@ -704,7 +707,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
const char *e) {
size_t l, i;
lua_State *L = ms->L;
const char *news = lua_tolstring(L, 3, &l);
const char *news = luamem_tostring(L, 3, &l);
for (i = 0; i < l; i++) {
if (news[i] != L_ESC)
luaL_addchar(b, news[i]);
Expand Down Expand Up @@ -753,15 +756,15 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
lua_pop(L, 1);
lua_pushlstring(L, s, e - s); /* keep original text */
}
else if (!lua_isstring(L, -1))
else if (!luamem_isstring(L, -1))
luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
luaL_addvalue(b); /* add result to accumulator */
luamem_addvalue(b); /* add result to accumulator */
}


static int str_gsub (lua_State *L) {
size_t srcl, lp;
const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
const char *src = luamem_checkstring(L, 1, &srcl); /* subject */
const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
const char *lastmatch = NULL; /* end of last match */
int tr = lua_type(L, 3); /* replacement type */
Expand All @@ -770,9 +773,11 @@ static int str_gsub (lua_State *L) {
lua_Integer n = 0; /* replacement count */
MatchState ms;
luaL_Buffer b;
if (!src && !srcl) src = p; /* force a non-null pointer for empty memory */
luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
"string/function/table expected");
tr == LUA_TFUNCTION || tr == LUA_TTABLE ||
luamem_ismemory(L, 3), 3,
"string/function/table/memory expected");
luaL_buffinit(L, &b);
if (anchor) {
p++; lp--; /* skip anchor character */
Expand Down Expand Up @@ -971,6 +976,15 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
luaL_addvalue(b);
break;
}
case LUA_TUSERDATA: {
size_t len;
int type;
const char *s = luamem_tomemoryx(L, arg, &len, NULL, &type);
if (type != LUAMEM_TNONE) {
addquoted(b, s, len);
break;
}
}
default: {
luaL_argerror(L, arg, "value has no literal form");
}
Expand Down Expand Up @@ -1375,7 +1389,7 @@ static int str_pack (lua_State *L) {
}
case Kchar: { /* fixed-size string */
size_t len;
const char *s = luaL_checklstring(L, arg, &len);
const char *s = luamem_checkstring(L, arg, &len);
luaL_argcheck(L, len <= (size_t)size, arg,
"string longer than given size");
luaL_addlstring(&b, s, len); /* add string */
Expand All @@ -1385,7 +1399,7 @@ static int str_pack (lua_State *L) {
}
case Kstring: { /* strings with length count */
size_t len;
const char *s = luaL_checklstring(L, arg, &len);
const char *s = luamem_checkstring(L, arg, &len);
luaL_argcheck(L, size >= (int)sizeof(size_t) ||
len < ((size_t)1 << (size * NB)),
arg, "string length does not fit in given size");
Expand All @@ -1396,8 +1410,9 @@ static int str_pack (lua_State *L) {
}
case Kzstr: { /* zero-terminated string */
size_t len;
const char *s = luaL_checklstring(L, arg, &len);
luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
const char *s = luamem_checkstring(L, arg, &len);
luaL_argcheck(L, memchr(s, '\0', len) == NULL,
arg, "string contains zeros");
luaL_addlstring(&b, s, len);
luaL_addchar(&b, '\0'); /* add zero at the end */
totalsize += len + 1;
Expand Down Expand Up @@ -1477,7 +1492,7 @@ static int str_unpack (lua_State *L) {
Header h;
const char *fmt = luaL_checkstring(L, 1);
size_t ld;
const char *data = luaL_checklstring(L, 2, &ld);
const char *data = luamem_checkstring(L, 2, &ld);
size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
int n = 0; /* number of results */
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
Expand Down Expand Up @@ -1521,7 +1536,10 @@ static int str_unpack (lua_State *L) {
break;
}
case Kzstr: {
size_t len = (int)strlen(data + pos);
size_t len;
const char *z = (const char *)memchr(data + pos, '\0', ld - pos);
luaL_argcheck(L, z, 2, "data string too short");
len = (size_t)(z - data - pos);
lua_pushlstring(L, data + pos, len);
pos += len + 1; /* skip string plus final '\0' */
break;
Expand Down
7 changes: 4 additions & 3 deletions ltablib.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "lauxlib.h"
#include "lualib.h"
#include "lmemlib.h"


/*
Expand Down Expand Up @@ -159,18 +160,18 @@ static int tmove (lua_State *L) {

static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
lua_geti(L, 1, i);
if (!lua_isstring(L, -1))
if (!luamem_isstring(L, -1))
luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
luaL_typename(L, -1), i);
luaL_addvalue(b);
luamem_addvalue(b);
}


static int tconcat (lua_State *L) {
luaL_Buffer b;
lua_Integer last = aux_getn(L, 1, TAB_R);
size_t lsep;
const char *sep = luaL_optlstring(L, 2, "", &lsep);
const char *sep = luamem_optstring(L, 2, "", &lsep);
lua_Integer i = luaL_optinteger(L, 3, 1);
last = luaL_optinteger(L, 4, last);
luaL_buffinit(L, &b);
Expand Down
3 changes: 3 additions & 0 deletions lualib.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ LUAMOD_API int (luaopen_debug) (lua_State *L);
#define LUA_LOADLIBNAME "package"
LUAMOD_API int (luaopen_package) (lua_State *L);

#define LUA_MEMLIBNAME "memory"
LUAMOD_API int (luaopen_memory) (lua_State *L);


/* open all previous libraries */
LUALIB_API void (luaL_openlibs) (lua_State *L);
Expand Down
13 changes: 7 additions & 6 deletions lutf8lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "lauxlib.h"
#include "lualib.h"
#include "lmemlib.h"

#define MAXUNICODE 0x10FFFF

Expand Down Expand Up @@ -71,7 +72,7 @@ static const char *utf8_decode (const char *o, int *val) {
static int utflen (lua_State *L) {
int n = 0;
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
const char *s = luamem_checkstring(L, 1, &len);
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
Expand Down Expand Up @@ -99,7 +100,7 @@ static int utflen (lua_State *L) {
*/
static int codepoint (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
const char *s = luamem_checkstring(L, 1, &len);
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
int n;
Expand Down Expand Up @@ -159,7 +160,7 @@ static int utfchar (lua_State *L) {
*/
static int byteoffset (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
const char *s = luamem_checkstring(L, 1, &len);
lua_Integer n = luaL_checkinteger(L, 2);
lua_Integer posi = (n >= 0) ? 1 : len + 1;
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
Expand Down Expand Up @@ -200,13 +201,13 @@ static int byteoffset (lua_State *L) {

static int iter_aux (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
const char *s = luamem_checkstring(L, 1, &len);
lua_Integer n = lua_tointeger(L, 2) - 1;
if (n < 0) /* first iteration? */
n = 0; /* start from here */
else if (n < (lua_Integer)len) {
n++; /* skip current byte */
while (iscont(s + n)) n++; /* and its continuations */
while (n < (lua_Integer)len && iscont(s + n)) n++; /* and its continuations */
}
if (n >= (lua_Integer)len)
return 0; /* no more codepoints */
Expand All @@ -223,7 +224,7 @@ static int iter_aux (lua_State *L) {


static int iter_codes (lua_State *L) {
luaL_checkstring(L, 1);
luamem_checkstring(L, 1, NULL);
lua_pushcfunction(L, iter_aux);
lua_pushvalue(L, 1);
lua_pushinteger(L, 0);
Expand Down
Loading