aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-24 00:54:17 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-24 00:54:17 +0100
commitddf17edc80948d47af84f5977028df9611c75fff (patch)
tree00fc1e2e6845f088a8ee991f81ce1e09d5d55c38
parent315b2b7a39d979df2a993e671694b2b9b4cec624 (diff)
downloadlua-compat-5.3-ddf17edc80948d47af84f5977028df9611c75fff.tar.gz
lua-compat-5.3-ddf17edc80948d47af84f5977028df9611c75fff.tar.bz2
lua-compat-5.3-ddf17edc80948d47af84f5977028df9611c75fff.zip
better performance for lua_compare; some code refactoring
-rw-r--r--c-api/compat-5.3.c86
-rw-r--r--lprefix.h7
2 files changed, 41 insertions, 52 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c
index 8db5189..1f00180 100644
--- a/c-api/compat-5.3.c
+++ b/c-api/compat-5.3.c
@@ -24,6 +24,21 @@ COMPAT53_API int lua_absindex (lua_State *L, int i) {
24} 24}
25 25
26 26
27static void compat53_call_lua (lua_State *L, char const code[], size_t len,
28 int nargs, int nret) {
29 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
30 if (lua_type(L, -1) != LUA_TFUNCTION) {
31 lua_pop(L, 1);
32 if (luaL_loadbuffer(L, code, len, "=none"))
33 lua_error(L);
34 lua_pushvalue(L, -1);
35 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
36 }
37 lua_insert(L, -nargs-1);
38 lua_call(L, nargs, nret);
39}
40
41
27static const char compat53_arith_code[] = { 42static const char compat53_arith_code[] = {
28 'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b', 43 'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b',
29 '=', '.', '.', '.', '\n', 44 '=', '.', '.', '.', '\n',
@@ -52,65 +67,46 @@ static const char compat53_arith_code[] = {
52}; 67};
53 68
54COMPAT53_API void lua_arith (lua_State *L, int op) { 69COMPAT53_API void lua_arith (lua_State *L, int op) {
70 if( op < LUA_OPADD && op > LUA_OPUNM )
71 luaL_error(L, "invalid 'op' argument for lua_arith");
55 luaL_checkstack(L, 5, "not enough stack slots"); 72 luaL_checkstack(L, 5, "not enough stack slots");
56 if (op == LUA_OPUNM) 73 if (op == LUA_OPUNM)
57 lua_pushvalue(L, -1); 74 lua_pushvalue(L, -1);
58 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)compat53_arith_code);
59 if (lua_type(L, -1) != LUA_TFUNCTION) {
60 lua_pop(L, 1);
61 if (luaL_loadbuffer(L, compat53_arith_code,
62 sizeof(compat53_arith_code)-1, "=none"))
63 lua_error(L);
64 lua_pushvalue(L, -1);
65 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)compat53_arith_code);
66 }
67 lua_pushnumber(L, op); 75 lua_pushnumber(L, op);
68 lua_pushvalue(L, -4); 76 lua_insert(L, -3);
69 lua_pushvalue(L, -4); 77 compat53_call_lua(L, compat53_arith_code,
70 lua_call(L, 3, 1); 78 sizeof(compat53_arith_code)-1, 3, 1);
71 lua_replace(L, -3); /* replace first operand */
72 lua_pop(L, 1); /* pop second */
73} 79}
74 80
75 81
76static const char compat53_compare_code[] = { 82static const char compat53_compare_code[] = {
77 'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b', 83 'l', 'o', 'c', 'a', 'l', ' ', 'a', ',', 'b',
78 '=', '.', '.', '.', '\n', 84 '=', '.', '.', '.', '\n',
79 'i', 'f', ' ', 'o', 'p', '=', '=', '0', ' ', 85 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '<', '=', 'b', '\n', '\0'
80 't', 'h', 'e', 'n', '\n',
81 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '=', '=', 'b', '\n',
82 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '1', ' ',
83 't', 'h', 'e', 'n', '\n',
84 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '<', 'b', '\n',
85 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '2', ' ',
86 't', 'h', 'e', 'n', '\n',
87 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '<', '=', 'b', '\n',
88 'e', 'n', 'd', '\n', '\0'
89}; 86};
90 87
91COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) { 88COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) {
92 int result = 0; 89 int result = 0;
93 luaL_checkstack(L, 4, "not enough stack slots"); 90 switch (op) {
94 idx1 = lua_absindex(L, idx1); 91 case LUA_OPEQ:
95 idx2 = lua_absindex(L, idx2); 92 return lua_equal(L, idx1, idx2);
96 lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)compat53_compare_code); 93 case LUA_OPLT:
97 if (lua_type(L, -1) != LUA_TFUNCTION) { 94 return lua_lessthan(L, idx1, idx2);
98 lua_pop(L, 1); 95 case LUA_OPLE:
99 if (luaL_loadbuffer(L, compat53_compare_code, 96 luaL_checkstack(L, 4, "not enough stack slots");
100 sizeof(compat53_compare_code)-1, "=none")) 97 idx1 = lua_absindex(L, idx1);
101 lua_error(L); 98 idx2 = lua_absindex(L, idx2);
102 lua_pushvalue(L, -1); 99 lua_pushvalue(L, idx1);
103 lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)compat53_compare_code); 100 lua_pushvalue(L, idx2);
101 compat53_call_lua(L, (void*)compat53_compare_code,
102 sizeof(compat53_compare_code)-1, 2, 1);
103 result = lua_toboolean(L, -1);
104 lua_pop(L, 1);
105 return result;
106 default:
107 luaL_error(L, "invalid 'op' argument for lua_compare");
104 } 108 }
105 lua_pushnumber(L, op); 109 return 0;
106 lua_pushvalue(L, idx1);
107 lua_pushvalue(L, idx2);
108 lua_call(L, 3, 1);
109 if(lua_type(L, -1) != LUA_TBOOLEAN)
110 luaL_error(L, "invalid 'op' argument for lua_compare");
111 result = lua_toboolean(L, -1);
112 lua_pop(L, 1);
113 return result;
114} 110}
115 111
116 112
diff --git a/lprefix.h b/lprefix.h
index 530ea60..5864251 100644
--- a/lprefix.h
+++ b/lprefix.h
@@ -96,13 +96,6 @@ static void compat53_rawseti (lua_State *L, int i, lua_Integer n) {
96} 96}
97# undef lua_rawseti 97# undef lua_rawseti
98# define lua_rawseti compat53_rawseti 98# define lua_rawseti compat53_rawseti
99/* we have lua_compare emulation for Lua 5.1, but it involves calling
100 * Lua code, and the only use in the table library is for '<', so ...
101 */
102# if LUA_VERSION_NUM == 501
103# undef lua_compare
104# define lua_compare(L, a, b, op) lua_lessthan(L, a, b)
105# endif
106#endif /* ltablib_c */ 99#endif /* ltablib_c */
107 100
108#ifdef lstrlib_c 101#ifdef lstrlib_c