Skip to content

Commit d06aa9b

Browse files
committed
support lua 5.3 lua_Integer
1 parent da9e8fe commit d06aa9b

File tree

2 files changed

+132
-89
lines changed

2 files changed

+132
-89
lines changed

lualib-src/lua-seri.c

Lines changed: 108 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
#define TYPE_BOOLEAN 1
1616
// hibits 0 false 1 true
1717
#define TYPE_NUMBER 2
18-
// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 8 : double
18+
// hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double
19+
#define TYPE_NUMBER_ZERO 0
20+
#define TYPE_NUMBER_BYTE 1
21+
#define TYPE_NUMBER_WORD 2
22+
#define TYPE_NUMBER_DWORD 4
23+
#define TYPE_NUMBER_QWORD 6
24+
#define TYPE_NUMBER_REAL 8
25+
1926
#define TYPE_USERDATA 3
2027
#define TYPE_SHORT_STRING 4
2128
// hibits 0~31 : len
@@ -107,7 +114,7 @@ rball_init(struct read_block * rb, char * buffer, int size) {
107114
}
108115

109116
static void *
110-
rb_read(struct read_block *rb, void *buffer, int sz) {
117+
rb_read(struct read_block *rb, int sz) {
111118
if (rb->len < sz) {
112119
return NULL;
113120
}
@@ -131,36 +138,44 @@ wb_boolean(struct write_block *wb, int boolean) {
131138
}
132139

133140
static inline void
134-
wb_integer(struct write_block *wb, int v, int type) {
141+
wb_integer(struct write_block *wb, lua_Integer v) {
142+
int type = TYPE_NUMBER;
135143
if (v == 0) {
136-
uint8_t n = COMBINE_TYPE(type , 0);
144+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_ZERO);
145+
wb_push(wb, &n, 1);
146+
} else if (v != (int32_t)v) {
147+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_QWORD);
148+
int64_t v64 = v;
137149
wb_push(wb, &n, 1);
138-
} else if (v<0) {
139-
uint8_t n = COMBINE_TYPE(type , 4);
150+
wb_push(wb, &v64, sizeof(v64));
151+
} else if (v < 0) {
152+
int32_t v32 = (int32_t)v;
153+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD);
140154
wb_push(wb, &n, 1);
141-
wb_push(wb, &v, 4);
155+
wb_push(wb, &v32, sizeof(v32));
142156
} else if (v<0x100) {
143-
uint8_t n = COMBINE_TYPE(type , 1);
157+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_BYTE);
144158
wb_push(wb, &n, 1);
145159
uint8_t byte = (uint8_t)v;
146-
wb_push(wb, &byte, 1);
160+
wb_push(wb, &byte, sizeof(byte));
147161
} else if (v<0x10000) {
148-
uint8_t n = COMBINE_TYPE(type , 2);
162+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_WORD);
149163
wb_push(wb, &n, 1);
150164
uint16_t word = (uint16_t)v;
151-
wb_push(wb, &word, 2);
165+
wb_push(wb, &word, sizeof(word));
152166
} else {
153-
uint8_t n = COMBINE_TYPE(type , 4);
167+
uint8_t n = COMBINE_TYPE(type , TYPE_NUMBER_DWORD);
154168
wb_push(wb, &n, 1);
155-
wb_push(wb, &v, 4);
169+
uint32_t v32 = (uint32_t)v;
170+
wb_push(wb, &v32, sizeof(v32));
156171
}
157172
}
158173

159174
static inline void
160-
wb_number(struct write_block *wb, double v) {
161-
uint8_t n = COMBINE_TYPE(TYPE_NUMBER , 8);
175+
wb_real(struct write_block *wb, double v) {
176+
uint8_t n = COMBINE_TYPE(TYPE_NUMBER , TYPE_NUMBER_REAL);
162177
wb_push(wb, &n, 1);
163-
wb_push(wb, &v, 8);
178+
wb_push(wb, &v, sizeof(v));
164179
}
165180

166181
static inline void
@@ -203,7 +218,7 @@ wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) {
203218
if (array_size >= MAX_COOKIE-1) {
204219
uint8_t n = COMBINE_TYPE(TYPE_TABLE, MAX_COOKIE-1);
205220
wb_push(wb, &n, 1);
206-
wb_integer(wb, array_size,TYPE_NUMBER);
221+
wb_integer(wb, array_size);
207222
} else {
208223
uint8_t n = COMBINE_TYPE(TYPE_TABLE, array_size);
209224
wb_push(wb, &n, 1);
@@ -224,11 +239,12 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a
224239
lua_pushnil(L);
225240
while (lua_next(L, index) != 0) {
226241
if (lua_type(L,-2) == LUA_TNUMBER) {
227-
lua_Number k = lua_tonumber(L,-2);
228-
int32_t x = (int32_t)lua_tointeger(L,-2);
229-
if (k == (lua_Number)x && x>0 && x<=array_size) {
230-
lua_pop(L,1);
231-
continue;
242+
if (lua_isinteger(L, -2)) {
243+
lua_Integer x = lua_tointeger(L,-2);
244+
if (x>0 && x<=array_size) {
245+
lua_pop(L,1);
246+
continue;
247+
}
232248
}
233249
}
234250
pack_one(L,wb,-2,depth);
@@ -259,12 +275,12 @@ pack_one(lua_State *L, struct write_block *b, int index, int depth) {
259275
wb_nil(b);
260276
break;
261277
case LUA_TNUMBER: {
262-
int32_t x = (int32_t)lua_tointeger(L,index);
263-
lua_Number n = lua_tonumber(L,index);
264-
if ((lua_Number)x==n) {
265-
wb_integer(b, x, TYPE_NUMBER);
278+
if (lua_isinteger(L, index)) {
279+
lua_Integer x = lua_tointeger(L,index);
280+
wb_integer(b, x);
266281
} else {
267-
wb_number(b,n);
282+
lua_Number n = lua_tonumber(L,index);
283+
wb_real(b,n);
268284
}
269285
break;
270286
}
@@ -310,31 +326,42 @@ invalid_stream_line(lua_State *L, struct read_block *rb, int line) {
310326

311327
#define invalid_stream(L,rb) invalid_stream_line(L,rb,__LINE__)
312328

313-
static int
329+
static lua_Integer
314330
get_integer(lua_State *L, struct read_block *rb, int cookie) {
315331
switch (cookie) {
316-
case 0:
332+
case TYPE_NUMBER_ZERO:
317333
return 0;
318-
case 1: {
319-
uint8_t n = 0;
320-
uint8_t * pn = rb_read(rb,&n,1);
334+
case TYPE_NUMBER_BYTE: {
335+
uint8_t n;
336+
uint8_t * pn = rb_read(rb,sizeof(n));
321337
if (pn == NULL)
322338
invalid_stream(L,rb);
323-
return *pn;
339+
n = *pn;
340+
return n;
324341
}
325-
case 2: {
326-
uint16_t n = 0;
327-
uint16_t * pn = rb_read(rb,&n,2);
342+
case TYPE_NUMBER_WORD: {
343+
uint16_t n;
344+
uint16_t * pn = rb_read(rb,sizeof(n));
328345
if (pn == NULL)
329346
invalid_stream(L,rb);
330-
return *pn;
347+
memcpy(&n, pn, sizeof(n));
348+
return n;
331349
}
332-
case 4: {
333-
int n = 0;
334-
int * pn = rb_read(rb,&n,4);
350+
case TYPE_NUMBER_DWORD: {
351+
int32_t n;
352+
int32_t * pn = rb_read(rb,sizeof(n));
335353
if (pn == NULL)
336354
invalid_stream(L,rb);
337-
return *pn;
355+
memcpy(&n, pn, sizeof(n));
356+
return n;
357+
}
358+
case TYPE_NUMBER_QWORD: {
359+
int64_t n;
360+
int64_t * pn = rb_read(rb,sizeof(n));
361+
if (pn == NULL)
362+
invalid_stream(L,rb);
363+
memcpy(&n, pn, sizeof(n));
364+
return n;
338365
}
339366
default:
340367
invalid_stream(L,rb);
@@ -343,32 +370,29 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) {
343370
}
344371

345372
static double
346-
get_number(lua_State *L, struct read_block *rb, int cookie) {
347-
if (cookie == 8) {
348-
double n = 0;
349-
double * pn = rb_read(rb,&n,8);
350-
if (pn == NULL)
351-
invalid_stream(L,rb);
352-
return *pn;
353-
} else {
354-
return (double)get_integer(L,rb,cookie);
355-
}
373+
get_real(lua_State *L, struct read_block *rb) {
374+
double n;
375+
double * pn = rb_read(rb,sizeof(n));
376+
if (pn == NULL)
377+
invalid_stream(L,rb);
378+
memcpy(&n, pn, sizeof(n));
379+
return n;
356380
}
357381

358382
static void *
359383
get_pointer(lua_State *L, struct read_block *rb) {
360384
void * userdata = 0;
361-
void ** v = (void **)rb_read(rb,&userdata,sizeof(userdata));
385+
void ** v = (void **)rb_read(rb,sizeof(userdata));
362386
if (v == NULL) {
363387
invalid_stream(L,rb);
364388
}
365-
return *v;
389+
memcpy(&userdata, v, sizeof(userdata));
390+
return userdata;
366391
}
367392

368393
static void
369394
get_buffer(lua_State *L, struct read_block *rb, int len) {
370-
char tmp[len];
371-
char * p = rb_read(rb,tmp,len);
395+
char * p = rb_read(rb,len);
372396
if (p == NULL) {
373397
invalid_stream(L,rb);
374398
}
@@ -380,12 +404,17 @@ static void unpack_one(lua_State *L, struct read_block *rb);
380404
static void
381405
unpack_table(lua_State *L, struct read_block *rb, int array_size) {
382406
if (array_size == MAX_COOKIE-1) {
383-
uint8_t type = 0;
384-
uint8_t *t = rb_read(rb, &type, 1);
385-
if (t==NULL || (*t & 7) != TYPE_NUMBER) {
407+
uint8_t type;
408+
uint8_t *t = rb_read(rb, sizeof(type));
409+
if (t==NULL) {
386410
invalid_stream(L,rb);
387411
}
388-
array_size = (int)get_number(L,rb,*t >> 3);
412+
type = *t;
413+
int cookie = type >> 3;
414+
if ((type & 7) != TYPE_NUMBER || cookie == TYPE_NUMBER_REAL) {
415+
invalid_stream(L,rb);
416+
}
417+
array_size = get_integer(L,rb,cookie);
389418
}
390419
lua_createtable(L,array_size,0);
391420
int i;
@@ -414,7 +443,11 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
414443
lua_pushboolean(L,cookie);
415444
break;
416445
case TYPE_NUMBER:
417-
lua_pushnumber(L,get_number(L,rb,cookie));
446+
if (cookie == TYPE_NUMBER_REAL) {
447+
lua_pushnumber(L,get_real(L,rb));
448+
} else {
449+
lua_pushinteger(L, get_integer(L, rb, cookie));
450+
}
418451
break;
419452
case TYPE_USERDATA:
420453
lua_pushlightuserdata(L,get_pointer(L,rb));
@@ -423,22 +456,25 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
423456
get_buffer(L,rb,cookie);
424457
break;
425458
case TYPE_LONG_STRING: {
426-
uint32_t len;
427459
if (cookie == 2) {
428-
uint16_t *plen = rb_read(rb, &len, 2);
460+
uint16_t *plen = rb_read(rb, 2);
429461
if (plen == NULL) {
430462
invalid_stream(L,rb);
431463
}
432-
get_buffer(L,rb,(int)*plen);
464+
uint16_t n;
465+
memcpy(&n, plen, sizeof(n));
466+
get_buffer(L,rb,n);
433467
} else {
434468
if (cookie != 4) {
435469
invalid_stream(L,rb);
436470
}
437-
uint32_t *plen = rb_read(rb, &len, 4);
471+
uint32_t *plen = rb_read(rb, 4);
438472
if (plen == NULL) {
439473
invalid_stream(L,rb);
440474
}
441-
get_buffer(L,rb,(int)*plen);
475+
uint32_t n;
476+
memcpy(&n, plen, sizeof(n));
477+
get_buffer(L,rb,n);
442478
}
443479
break;
444480
}
@@ -455,12 +491,13 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
455491

456492
static void
457493
unpack_one(lua_State *L, struct read_block *rb) {
458-
uint8_t type = 0;
459-
uint8_t *t = rb_read(rb, &type, 1);
494+
uint8_t type;
495+
uint8_t *t = rb_read(rb, sizeof(type));
460496
if (t==NULL) {
461497
invalid_stream(L, rb);
462498
}
463-
push_value(L, rb, *t & 0x7, *t>>3);
499+
type = *t;
500+
push_value(L, rb, type & 0x7, type>>3);
464501
}
465502

466503
static void
@@ -516,10 +553,11 @@ _luaseri_unpack(lua_State *L) {
516553
lua_checkstack(L,i);
517554
}
518555
uint8_t type = 0;
519-
uint8_t *t = rb_read(&rb, &type, 1);
556+
uint8_t *t = rb_read(&rb, sizeof(type));
520557
if (t==NULL)
521558
break;
522-
push_value(L, &rb, *t & 0x7, *t>>3);
559+
type = *t;
560+
push_value(L, &rb, type & 0x7, type>>3);
523561
}
524562

525563
// Need not free buffer

0 commit comments

Comments
 (0)