Skip to content

Commit a70a4d7

Browse files
committed
Move version-dependent code from main C file
1 parent 0e74adc commit a70a4d7

File tree

8 files changed

+197
-145
lines changed

8 files changed

+197
-145
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ os:
1313
# environment variables for the build
1414
env:
1515
global:
16-
- LUAROCKS=2.3.0
16+
- LUAROCKS=3.1.3
1717
matrix:
1818
- LUA=5.1.3
1919
- LUA=5.1.4

compat.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ static GetArgFunction const getArgFuncs[] = {
2424

2525

2626

27-
extern int getType_50103(void const*);
28-
extern int getType_50200(void const*);
29-
extern int getType_50300(void const*);
30-
extern int getType_50400(void const*);
27+
extern size_t sizeNumber_50103(void const*);
28+
extern size_t sizeNumber_50200(void const*);
29+
extern size_t sizeNumber_50300(void const*);
30+
extern size_t sizeNumber_50400(void const*);
3131

32-
static GetTypeFunction const getTypeFuncs[] = {
32+
static NumberSizeFunction const numberSizeFuncs[] = {
3333
#if LUA_VERSION_NUM == 501
34-
0, 0, 0, getType_50103, getType_50103, getType_50103
34+
0, 0, 0, sizeNumber_50103, sizeNumber_50103, sizeNumber_50103
3535
#elif LUA_VERSION_NUM == 502
36-
getType_50200, getType_50200, getType_50200, getType_50200, getType_50200
36+
sizeNumber_50200, sizeNumber_50200, sizeNumber_50200, sizeNumber_50200, sizeNumber_50200
3737
#elif LUA_VERSION_NUM == 503
38-
getType_50300, getType_50300, getType_50300, getType_50300, getType_50300, getType_50300
38+
sizeNumber_50300, sizeNumber_50300, sizeNumber_50300, sizeNumber_50300, sizeNumber_50300, sizeNumber_50300
3939
#elif LUA_VERSION_NUM == 504
40-
getType_50400
40+
sizeNumber_50400
4141
#else
4242
#error unsupported Lua version
4343
#endif
@@ -67,6 +67,26 @@ static StringSizeFunction const stringSizeFuncs[] = {
6767

6868

6969

70+
extern size_t sizeFunction_50103(void const*, int, int);
71+
extern size_t sizeFunction_50200(void const*, int, int);
72+
extern size_t sizeFunction_50300(void const*, int, int);
73+
extern size_t sizeFunction_50400(void const*, int, int);
74+
static FunctionSizeFunction const functionSizeFuncs[] = {
75+
#if LUA_VERSION_NUM == 501
76+
0, 0, 0, sizeFunction_50103, sizeFunction_50103, sizeFunction_50103
77+
#elif LUA_VERSION_NUM == 502
78+
sizeFunction_50200, sizeFunction_50200, sizeFunction_50200, sizeFunction_50200, sizeFunction_50200
79+
#elif LUA_VERSION_NUM == 503
80+
sizeFunction_50300, sizeFunction_50300, sizeFunction_50300, sizeFunction_50300, sizeFunction_50300, sizeFunction_50300
81+
#elif LUA_VERSION_NUM == 504
82+
sizeFunction_50400
83+
#else
84+
#error unsupported Lua version
85+
#endif
86+
};
87+
88+
89+
7090
extern void* tableNode_50103(void const*);
7191
extern void* tableNode_50200(void const*);
7292
extern void* tableNode_50204(void const*);
@@ -173,8 +193,9 @@ GetSizeVTable* compat_init(lua_State* L) {
173193
} while (0)
174194

175195
DEFINE_FUNC(getArg, getArgFuncs);
176-
DEFINE_FUNC(getType, getTypeFuncs);
196+
DEFINE_FUNC(sizeNumber, numberSizeFuncs);
177197
DEFINE_FUNC(sizeString, stringSizeFuncs);
198+
DEFINE_FUNC(sizeFunction, functionSizeFuncs);
178199
DEFINE_FUNC(tableNode, tableNodeFuncs);
179200
DEFINE_FUNC(sizeTable, tableSizeFuncs);
180201
DEFINE_FUNC(sizeUserdata, userdataSizeFuncs);

compat.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66

77
typedef /* TValue */ void* (*GetArgFunction)(lua_State*, int);
8-
typedef int (*GetTypeFunction)(/* TValue */ void const*);
8+
typedef size_t (*NumberSizeFunction)(/* TValue */ void const*);
99
typedef size_t (*StringSizeFunction)(/* TValue */ void const*);
10+
typedef size_t (*FunctionSizeFunction)(/* TValue */ void const*, int, int);
1011
typedef /* Node */ void* (*TableNodeFunction)(/* TValue */ void const*);
1112
typedef size_t (*TableSizeFunction)(/* TValue */ void const*,
1213
/* Node */ void const*,
@@ -17,8 +18,9 @@ typedef size_t (*ThreadSizeFunction)(/* TValue */ void const*);
1718

1819
typedef struct {
1920
GetArgFunction getArg;
20-
GetTypeFunction getType;
21+
NumberSizeFunction sizeNumber;
2122
StringSizeFunction sizeString;
23+
FunctionSizeFunction sizeFunction;
2224
TableNodeFunction tableNode;
2325
TableSizeFunction sizeTable;
2426
UserdataSizeFunction sizeUserdata;

compat_50103.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ void* getArg_50103(lua_State* L, int n) {
1515
}
1616

1717

18-
int getType_50103(void const* v) {
19-
TValue const* o = v;
20-
return ttype(o);
18+
size_t sizeNumber_50103(void const* n) {
19+
(void)n;
20+
return sizeof(lua_Number);
2121
}
2222

2323

@@ -27,6 +27,28 @@ size_t sizeString_50103(void const* s) {
2727
}
2828

2929

30+
static size_t sizeProto(Proto const* p)
31+
{
32+
return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
33+
sizeof(Proto*) * p->sizep +
34+
sizeof(TValue) * p->sizek +
35+
sizeof(int) * p->sizelineinfo +
36+
sizeof(LocVar) * p->sizelocvars +
37+
sizeof(*(p->upvalues)) * p->sizeupvalues;
38+
}
39+
40+
size_t sizeFunction_50103(void const* v, int count_protos, int count_upvalues) {
41+
TValue const* o = v;
42+
Closure *cl = clvalue(o);
43+
if (cl->c.isC)
44+
return sizeCclosure(cl->c.nupvalues);
45+
else
46+
return sizeLclosure(cl->l.nupvalues) +
47+
(count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) +
48+
(count_protos ? sizeProto(cl->l.p) : 0);
49+
}
50+
51+
3052
void* tableNode_50103(void const* t) {
3153
TValue const* h = t;
3254
return hvalue(h)->node;

compat_50200.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ void* getArg_50200(lua_State* L, int n) {
1515
}
1616

1717

18-
int getType_50200(void const* v) {
19-
TValue const* o = v;
20-
return ttype(o);
18+
size_t sizeNumber_50200(void const* n) {
19+
(void)n;
20+
return sizeof(lua_Number);
2121
}
2222

2323

@@ -27,6 +27,34 @@ size_t sizeString_50200(void const* s) {
2727
}
2828

2929

30+
static size_t sizeProto(Proto const* p) {
31+
return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
32+
sizeof(Proto*) * p->sizep +
33+
sizeof(TValue) * p->sizek +
34+
sizeof(int) * p->sizelineinfo +
35+
sizeof(LocVar) * p->sizelocvars +
36+
sizeof(*(p->upvalues)) * p->sizeupvalues;
37+
}
38+
39+
size_t sizeFunction_50200(void const* v, int count_protos, int count_upvalues) {
40+
TValue const* o = v;
41+
switch (ttype(o)) {
42+
case LUA_TLCL: /* Lua closure */
43+
{
44+
Closure *cl = clvalue(o);
45+
return sizeLclosure(cl->l.nupvalues) +
46+
(count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) +
47+
(count_protos ? sizeProto(cl->l.p) : 0);
48+
}
49+
case LUA_TLCF: /* light C function */
50+
return sizeof(lua_CFunction);
51+
case LUA_TCCL: /* C closure */
52+
return sizeCclosure(clvalue(o)->c.nupvalues);
53+
}
54+
return 0;
55+
}
56+
57+
3058
void* tableNode_50200(void const* t) {
3159
TValue const* h = t;
3260
return hvalue(h)->node;

compat_50300.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ void* getArg_50300(lua_State* L, int n) {
1515
}
1616

1717

18-
int getType_50300(void const* v) {
18+
size_t sizeNumber_50300(void const* v) {
1919
TValue const* o = v;
20-
return ttype(o);
20+
if (ttype(o) == LUA_TNUMINT)
21+
return sizeof(lua_Integer);
22+
else
23+
return sizeof(lua_Number);
2124
}
2225

2326

@@ -27,6 +30,37 @@ size_t sizeString_50300(void const* s) {
2730
}
2831

2932

33+
34+
static size_t sizeProto(Proto const* p)
35+
{
36+
return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
37+
sizeof(Proto*) * p->sizep +
38+
sizeof(TValue) * p->sizek +
39+
sizeof(int) * p->sizelineinfo +
40+
sizeof(LocVar) * p->sizelocvars +
41+
sizeof(*(p->upvalues)) * p->sizeupvalues;
42+
}
43+
44+
45+
size_t sizeFunction_50300(void const* v, int count_protos, int count_upvalues) {
46+
TValue const* o = v;
47+
switch (ttype(o)) {
48+
case LUA_TLCL: /* Lua closure */
49+
{
50+
Closure *cl = clvalue(o);
51+
return sizeLclosure(cl->l.nupvalues) +
52+
(count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) +
53+
(count_protos ? sizeProto(cl->l.p) : 0);
54+
}
55+
case LUA_TLCF: /* light C function */
56+
return sizeof(lua_CFunction);
57+
case LUA_TCCL: /* C closure */
58+
return sizeCclosure(clvalue(o)->c.nupvalues);
59+
}
60+
return 0;
61+
}
62+
63+
3064
void* tableNode_50300(void const* t) {
3165
TValue const* h = t;
3266
return hvalue(h)->node;

compat_50400.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ void* getArg_50400(lua_State* L, int n) {
1515
}
1616

1717

18-
int getType_50400(void const* v) {
18+
size_t sizeNumber_50400(void const* v) {
1919
TValue const* o = v;
20-
return ttypetag(o);
20+
if (ttypetag(o) == LUA_TNUMINT)
21+
return sizeof(lua_Integer);
22+
else
23+
return sizeof(lua_Number);
2124
}
2225

2326

@@ -27,6 +30,35 @@ size_t sizeString_50400(void const* s) {
2730
}
2831

2932

33+
static size_t sizeProto(Proto const* p)
34+
{
35+
return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
36+
sizeof(Proto*) * p->sizep +
37+
sizeof(TValue) * p->sizek +
38+
sizeof(int) * p->sizelineinfo +
39+
sizeof(LocVar) * p->sizelocvars +
40+
sizeof(*(p->upvalues)) * p->sizeupvalues;
41+
}
42+
43+
size_t sizeFunction_50400(void const* v, int count_protos, int count_upvalues) {
44+
TValue const* o = v;
45+
switch (ttypetag(o)) {
46+
case LUA_TLCL: /* Lua closure */
47+
{
48+
Closure *cl = clvalue(o);
49+
return sizeLclosure(cl->l.nupvalues) +
50+
(count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) +
51+
(count_protos ? sizeProto(cl->l.p) : 0);
52+
}
53+
case LUA_TLCF: /* light C function */
54+
return sizeof(lua_CFunction);
55+
case LUA_TCCL: /* C closure */
56+
return sizeCclosure(clvalue(o)->c.nupvalues);
57+
}
58+
return 0;
59+
}
60+
61+
3062
void* tableNode_50400(void const* t) {
3163
TValue const* h = t;
3264
return hvalue(h)->node;

0 commit comments

Comments
 (0)