1 // The MIT License (MIT)
2
3 // Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
4
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #ifndef SOL_5_1_0_H
23 #define SOL_5_1_0_H
24
25 #include "version.hpp"
26
27 #if SOL_LUA_VERSION == 501
28 /* Lua 5.1 */
29
30 #include <lua.hpp>
31 #include <stddef.h>
32 #include <string.h>
33 #include <stdio.h>
34
35 /* LuaJIT doesn't define these unofficial macros ... */
36 #if !defined(LUAI_INT32)
37 #include <limits.h>
38 #if INT_MAX-20 < 32760
39 #define LUAI_INT32 long
40 #define LUAI_UINT32 unsigned long
41 #elif INT_MAX > 2147483640L
42 #define LUAI_INT32 int
43 #define LUAI_UINT32 unsigned int
44 #else
45 #error "could not detect suitable lua_Unsigned datatype"
46 #endif
47 #endif
48
49 /* LuaJIT does not have the updated error codes for thread status/function returns */
50 #ifndef LUA_ERRGCMM
51 #define LUA_ERRGCMM (LUA_ERRERR + 1)
52 #endif // LUA_ERRGCMM
53
54 /* LuaJIT does not support continuation contexts / return error codes? */
55 #ifndef LUA_KCONTEXT
56 #define LUA_KCONTEXT std::ptrdiff_t
57 typedef LUA_KCONTEXT lua_KContext;
58 typedef int(*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
59 #endif // LUA_KCONTEXT
60
61 #define LUA_OPADD 0
62 #define LUA_OPSUB 1
63 #define LUA_OPMUL 2
64 #define LUA_OPDIV 3
65 #define LUA_OPMOD 4
66 #define LUA_OPPOW 5
67 #define LUA_OPUNM 6
68 #define LUA_OPEQ 0
69 #define LUA_OPLT 1
70 #define LUA_OPLE 2
71
72 typedef LUAI_UINT32 lua_Unsigned;
73
74 typedef struct luaL_Buffer_52 {
75 luaL_Buffer b; /* make incorrect code crash! */
76 char *ptr;
77 size_t nelems;
78 size_t capacity;
79 lua_State *L2;
80 } luaL_Buffer_52;
81 #define luaL_Buffer luaL_Buffer_52
82
83 #define lua_tounsigned(L, i) lua_tounsignedx(L, i, NULL)
84
85 #define lua_rawlen(L, i) lua_objlen(L, i)
86
lua_callk(lua_State * L,int nargs,int nresults,lua_KContext,lua_KFunction)87 inline void lua_callk(lua_State *L, int nargs, int nresults, lua_KContext, lua_KFunction) {
88 // should probably warn the user of Lua 5.1 that continuation isn't supported...
89 lua_call(L, nargs, nresults);
90 }
lua_pcallk(lua_State * L,int nargs,int nresults,int errfunc,lua_KContext,lua_KFunction)91 inline int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc, lua_KContext, lua_KFunction) {
92 // should probably warn the user of Lua 5.1 that continuation isn't supported...
93 return lua_pcall(L, nargs, nresults, errfunc);
94 }
95 void lua_arith(lua_State *L, int op);
96 int lua_compare(lua_State *L, int idx1, int idx2, int op);
97 void lua_pushunsigned(lua_State *L, lua_Unsigned n);
98 lua_Unsigned luaL_checkunsigned(lua_State *L, int i);
99 lua_Unsigned lua_tounsignedx(lua_State *L, int i, int *isnum);
100 lua_Unsigned luaL_optunsigned(lua_State *L, int i, lua_Unsigned def);
101 lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum);
102 void lua_len(lua_State *L, int i);
103 int luaL_len(lua_State *L, int i);
104 const char *luaL_tolstring(lua_State *L, int idx, size_t *len);
105 void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb);
106
107 #define luaL_buffinit luaL_buffinit_52
108 void luaL_buffinit(lua_State *L, luaL_Buffer_52 *B);
109
110 #define luaL_prepbuffsize luaL_prepbuffsize_52
111 char *luaL_prepbuffsize(luaL_Buffer_52 *B, size_t s);
112
113 #define luaL_addlstring luaL_addlstring_52
114 void luaL_addlstring(luaL_Buffer_52 *B, const char *s, size_t l);
115
116 #define luaL_addvalue luaL_addvalue_52
117 void luaL_addvalue(luaL_Buffer_52 *B);
118
119 #define luaL_pushresult luaL_pushresult_52
120 void luaL_pushresult(luaL_Buffer_52 *B);
121
122 #undef luaL_buffinitsize
123 #define luaL_buffinitsize(L, B, s) \
124 (luaL_buffinit(L, B), luaL_prepbuffsize(B, s))
125
126 #undef luaL_prepbuffer
127 #define luaL_prepbuffer(B) \
128 luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
129
130 #undef luaL_addchar
131 #define luaL_addchar(B, c) \
132 ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \
133 ((B)->ptr[(B)->nelems++] = (c)))
134
135 #undef luaL_addsize
136 #define luaL_addsize(B, s) \
137 ((B)->nelems += (s))
138
139 #undef luaL_addstring
140 #define luaL_addstring(B, s) \
141 luaL_addlstring(B, s, strlen(s))
142
143 #undef luaL_pushresultsize
144 #define luaL_pushresultsize(B, s) \
145 (luaL_addsize(B, s), luaL_pushresult(B))
146
147 typedef struct kepler_lua_compat_get_string_view {
148 const char *s;
149 size_t size;
150 } kepler_lua_compat_get_string_view;
151
kepler_lua_compat_get_string(lua_State * L,void * ud,size_t * size)152 inline const char* kepler_lua_compat_get_string(lua_State* L, void* ud, size_t* size) {
153 kepler_lua_compat_get_string_view* ls = (kepler_lua_compat_get_string_view*) ud;
154 (void)L;
155 if (ls->size == 0) return NULL;
156 *size = ls->size;
157 ls->size = 0;
158 return ls->s;
159 }
160
161 #if !defined(SOL_LUAJIT) || (SOL_LUAJIT_VERSION < 20100)
162 // Luajit 2.1.0 has this function already
163
luaL_loadbufferx(lua_State * L,const char * buff,size_t size,const char * name,const char *)164 inline int luaL_loadbufferx(lua_State* L, const char* buff, size_t size, const char* name, const char*) {
165 kepler_lua_compat_get_string_view ls;
166 ls.s = buff;
167 ls.size = size;
168 return lua_load(L, kepler_lua_compat_get_string, &ls, name/*, mode*/);
169 }
170
171 #endif // LuaJIT 2.1.x beta and beyond
172
173 #endif /* Lua 5.1 */
174
175 #endif // SOL_5_1_0_H
176