Skip to content

Commit

Permalink
Allow non-id'd objects to be pushed. Uses less bytes, and is less buggy.
Browse files Browse the repository at this point in the history
  • Loading branch information
blue42u committed Jul 23, 2017
1 parent 1d88692 commit a2f4eb8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/ldata-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void pushread(lua_State* L, int idtab, const void** data, const void* st)

int ld_unpack(lua_State* L, const void* space) {
// Header: unsigned int numobj;
// Footer: unsigned int numpush, pushids[numpush];
// Footer: unsigned int numpush; ld_sml pushes[numpush];
int top = lua_gettop(L);
const void* st = space;
void* data = &space;
Expand All @@ -28,7 +28,7 @@ int ld_unpack(lua_State* L, const void* space) {
int tab = lua_absindex(L, -1);

n = read(unsigned int);
for(int i=0; i<n; i++) lua_geti(L, tab, read(unsigned int));
for(int i=0; i<n; i++) pushread(L, tab, data, st);
lua_remove(L, tab);

printf("ld_unpack top: %d from %d+%d\n", lua_gettop(L), top, n);
Expand Down
34 changes: 7 additions & 27 deletions src/ldata-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,15 @@ static void write_large(lua_State* L, int idtab, unsigned int* id,
default: break;
}
}
static inline void write_force(lua_State* L, int idtab, unsigned int* id,
void* ud, lua_Writer wf) {
idtab = lua_absindex(L, idtab);

int t = lua_type(L, -1);
if(t == LUA_TNIL || t == LUA_TNUMBER || t == LUA_TBOOLEAN) {
lua_pushvalue(L, -1);
lua_gettable(L, idtab);
int done = lua_toboolean(L, -1);
lua_pop(L, 1);
if(done) return;

lua_pushvalue(L, -1);
lua_pushinteger(L, (*id)++);
lua_settable(L, idtab);

write_small(L, idtab, ud, wf);
} else write_large(L, idtab, id, ud, wf);
}

static int addsz(lua_State* L, const void* p, size_t sz, void* ud) {
*(size_t*)ud += sz;
return 0;
}
size_t ld_size(lua_State* L, int n, unsigned int* numobj) {
// Header: unsigned int numobj;
// Footer: unsigned int numpush, pushids[numpush];
size_t sz = sizeof(unsigned int)*(2+n);
// Footer: unsigned int numpush; ld_sml pushes[numpush];
size_t sz = sizeof(unsigned int)*2;

int top = lua_gettop(L);
lua_newtable(L); // For storing ids
Expand All @@ -174,7 +155,8 @@ size_t ld_size(lua_State* L, int n, unsigned int* numobj) {
*numobj = 1; // Next available id
for(int i=1; i <= n; i++) {
lua_pushvalue(L, i);
write_force(L, -2, numobj, &sz, addsz);
write_large(L, -2, numobj, &sz, addsz);
write_small(L, -2, &sz, addsz);
lua_pop(L, 1);
}
lua_pop(L, 1);
Expand All @@ -192,7 +174,7 @@ static int writeout(lua_State* L, const void* p, size_t sz, void* ud) {
}
void ld_pack(lua_State* L, int n, unsigned int numobj, void* space) {
// Header: unsigned int numobj;
// Footer: unsigned int numpush, pushids[numpush];
// Footer: unsigned int numpush; ld_sml pushes[numpush];
writeout(L, &numobj, sizeof(unsigned int), &space);

int top = lua_gettop(L);
Expand All @@ -203,17 +185,15 @@ void ld_pack(lua_State* L, int n, unsigned int numobj, void* space) {
unsigned int id = 1; // Next available id
for(int i=1; i <= n; i++) {
lua_pushvalue(L, i);
write_force(L, -2, &id, &space, writeout);
write_large(L, -2, &id, &space, writeout);
lua_pop(L, 1);
}

unsigned int tmp = n;
writeout(L, &tmp, sizeof(unsigned int), &space);
for(int i=1; i <= n; i++) {
lua_pushvalue(L, i);
lua_gettable(L, -2);
tmp = lua_tointeger(L, -1);
writeout(L, &tmp, sizeof(unsigned int), &space);
write_small(L, -2, &space, writeout);
lua_pop(L, 1);
}

Expand Down

0 comments on commit a2f4eb8

Please sign in to comment.