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 | |
| 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')
| -rw-r--r-- | c-api/compat-5.3.c | 90 | ||||
| -rw-r--r-- | c-api/compat-5.3.h | 18 |
2 files changed, 106 insertions, 2 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"); |
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index b288126..3905a10 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h | |||
| @@ -40,8 +40,6 @@ | |||
| 40 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 | 40 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 |
| 41 | 41 | ||
| 42 | /* XXX not implemented: | 42 | /* XXX not implemented: |
| 43 | * lua_arith | ||
| 44 | * lua_compare | ||
| 45 | * lua_upvalueid | 43 | * lua_upvalueid |
| 46 | * lua_upvaluejoin | 44 | * lua_upvaluejoin |
| 47 | * lua_version | 45 | * lua_version |
| @@ -63,6 +61,16 @@ | |||
| 63 | #endif | 61 | #endif |
| 64 | 62 | ||
| 65 | #define LUA_OK 0 | 63 | #define LUA_OK 0 |
| 64 | #define LUA_OPADD 0 | ||
| 65 | #define LUA_OPSUB 1 | ||
| 66 | #define LUA_OPMUL 2 | ||
| 67 | #define LUA_OPDIV 3 | ||
| 68 | #define LUA_OPMOD 4 | ||
| 69 | #define LUA_OPPOW 5 | ||
| 70 | #define LUA_OPUNM 6 | ||
| 71 | #define LUA_OPEQ 0 | ||
| 72 | #define LUA_OPLT 1 | ||
| 73 | #define LUA_OPLE 2 | ||
| 66 | 74 | ||
| 67 | typedef struct luaL_Stream { | 75 | typedef struct luaL_Stream { |
| 68 | FILE *f; | 76 | FILE *f; |
| @@ -74,6 +82,12 @@ typedef size_t lua_Unsigned; | |||
| 74 | #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) | 82 | #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) |
| 75 | COMPAT53_API int lua_absindex (lua_State *L, int i); | 83 | COMPAT53_API int lua_absindex (lua_State *L, int i); |
| 76 | 84 | ||
| 85 | #define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith) | ||
| 86 | COMPAT53_API void lua_arith (lua_State *L, int op); | ||
| 87 | |||
| 88 | #define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare) | ||
| 89 | COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op); | ||
| 90 | |||
| 77 | #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) | 91 | #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) |
| 78 | COMPAT53_API void lua_copy (lua_State *L, int from, int to); | 92 | COMPAT53_API void lua_copy (lua_State *L, int from, int to); |
| 79 | 93 | ||
