diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-20 01:02:20 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-20 01:02:20 +0100 |
commit | 489cd678823e0981ff2e4d2544b84094ed23c587 (patch) | |
tree | 2855dbabb9205957434129aa1119cb526d2ae01b /c-api/compat-5.3.c | |
parent | f7f85b0826f7006b8dcc040111cd0ef8d42c2352 (diff) | |
download | lua-compat-5.3-489cd678823e0981ff2e4d2544b84094ed23c587.tar.gz lua-compat-5.3-489cd678823e0981ff2e4d2544b84094ed23c587.tar.bz2 lua-compat-5.3-489cd678823e0981ff2e4d2544b84094ed23c587.zip |
add lua_arith and lua_compare for Lua 5.1
Diffstat (limited to 'c-api/compat-5.3.c')
-rw-r--r-- | c-api/compat-5.3.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index e45c728..a539aed 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c | |||
@@ -24,6 +24,96 @@ COMPAT53_API int lua_absindex (lua_State *L, int i) { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | 26 | ||
27 | static const char compat53_arith_code[] = { | ||
28 | 'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b', | ||
29 | '=', '.', '.', '.', '\n', | ||
30 | 'i', 'f', ' ', 'o', 'p', '=', '=', '0', ' ', | ||
31 | 't', 'h', 'e', 'n', '\n', | ||
32 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '+', 'b', '\n', | ||
33 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '1', ' ', | ||
34 | 't', 'h', 'e', 'n', '\n', | ||
35 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '-', 'b', '\n', | ||
36 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '2', ' ', | ||
37 | 't', 'h', 'e', 'n', '\n', | ||
38 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '*', 'b', '\n', | ||
39 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '3', ' ', | ||
40 | 't', 'h', 'e', 'n', '\n', | ||
41 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '/', 'b', '\n', | ||
42 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '4', ' ', | ||
43 | 't', 'h', 'e', 'n', '\n', | ||
44 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '%', 'b', '\n', | ||
45 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '5', ' ', | ||
46 | 't', 'h', 'e', 'n', '\n', | ||
47 | 'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '^', 'b', '\n', | ||
48 | 'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '6', ' ', | ||
49 | 't', 'h', 'e', 'n', '\n', | ||
50 | 'r', 'e', 't', 'u', 'r', 'n', ' ', '-', 'a', '\n', | ||
51 | 'e', 'n', 'd', '\n', '\0' | ||
52 | }; | ||
53 | |||
54 | COMPAT53_API void lua_arith (lua_State *L, int op) { | ||
55 | luaL_checkstack(L, 5, "not enough stack slots"); | ||
56 | if (op == LUA_OPUNM) | ||
57 | 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); | ||
68 | lua_pushvalue(L, -4); | ||
69 | lua_pushvalue(L, -4); | ||
70 | lua_call(L, 3, 1); | ||
71 | lua_replace(L, -3); /* replace first operand */ | ||
72 | lua_pop(L, 1); /* pop second */ | ||
73 | } | ||
74 | |||
75 | |||
76 | static const char compat53_compare_code[] = { | ||
77 | 'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b', | ||
78 | '=', '.', '.', '.', '\n', | ||
79 | 'i', 'f', ' ', 'o', 'p', '=', '=', '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 | }; | ||
90 | |||
91 | COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) { | ||
92 | int result = 0; | ||
93 | luaL_checkstack(L, 4, "not enough stack slots"); | ||
94 | idx1 = lua_absindex(L, idx1); | ||
95 | idx2 = lua_absindex(L, idx2); | ||
96 | lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)compat53_compare_code); | ||
97 | if (lua_type(L, -1) != LUA_TFUNCTION) { | ||
98 | lua_pop(L, 1); | ||
99 | if (luaL_loadbuffer(L, compat53_compare_code, | ||
100 | sizeof(compat53_compare_code)-1, "=none")) | ||
101 | lua_error(L); | ||
102 | lua_pushvalue(L, -1); | ||
103 | lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)compat53_compare_code); | ||
104 | } | ||
105 | lua_pushnumber(L, op); | ||
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 | } | ||
115 | |||
116 | |||
27 | COMPAT53_API void lua_copy (lua_State *L, int from, int to) { | 117 | COMPAT53_API void lua_copy (lua_State *L, int from, int to) { |
28 | int abs_to = lua_absindex(L, to); | 118 | int abs_to = lua_absindex(L, to); |
29 | luaL_checkstack(L, 1, "not enough stack slots"); | 119 | luaL_checkstack(L, 1, "not enough stack slots"); |