diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-24 00:54:17 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-24 00:54:17 +0100 |
commit | ddf17edc80948d47af84f5977028df9611c75fff (patch) | |
tree | 00fc1e2e6845f088a8ee991f81ce1e09d5d55c38 /c-api | |
parent | 315b2b7a39d979df2a993e671694b2b9b4cec624 (diff) | |
download | lua-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
Diffstat (limited to 'c-api')
-rw-r--r-- | c-api/compat-5.3.c | 86 |
1 files changed, 41 insertions, 45 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 | ||
27 | static 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 | |||
27 | static const char compat53_arith_code[] = { | 42 | static 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 | ||
54 | COMPAT53_API void lua_arith (lua_State *L, int op) { | 69 | COMPAT53_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 | ||
76 | static const char compat53_compare_code[] = { | 82 | static 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 | ||
91 | COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) { | 88 | COMPAT53_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 | ||