diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 00000000..58d797a0 --- /dev/null +++ b/.style.yapf @@ -0,0 +1,15 @@ +[style] +based_on_style = pep8 +use_tabs = true +indent_width = 5 + +spaces_before_comment = 1 +spaces_around_power_operator = true +space_between_ending_comma_and_closing_bracket = true + +continuation_align_style = SPACE +split_before_first_argument = false +split_complex_comprehension = true +dedent_closing_brackets = false +coalesce_brackets = true +align_closing_bracket_with_visual_indent = false diff --git a/docs/source/media/sol.png b/docs/source/media/sol.png index eedee2ed..a4123ff2 100644 Binary files a/docs/source/media/sol.png and b/docs/source/media/sol.png differ diff --git a/docs/source/media/sol.psd b/docs/source/media/sol.psd index 84865d51..493b8a53 100644 Binary files a/docs/source/media/sol.psd and b/docs/source/media/sol.psd differ diff --git a/single.py b/single.py index 2d76b9f9..09e65a6a 100644 --- a/single.py +++ b/single.py @@ -7,15 +7,23 @@ import datetime as dt # python 3 compatibility try: - import cStringIO as sstream + import cStringIO as sstream except ImportError: - from io import StringIO + from io import StringIO description = "Converts sol to a single file for convenience." # command line parser -parser = argparse.ArgumentParser(usage='%(prog)s [options...]', description=description) -parser.add_argument('--output', '-o', nargs='+', help='name and location of where to place file (and forward declaration file)', metavar='file', default='sol.hpp') +parser = argparse.ArgumentParser( + usage='%(prog)s [options...]', description=description) +parser.add_argument( + '--output', + '-o', + nargs='+', + help= + 'name and location of where to place file (and forward declaration file)', + metavar='file', + default='sol.hpp') parser.add_argument('--quiet', help='suppress all output', action='store_true') args = parser.parse_args() @@ -23,9 +31,9 @@ single_file = '' forward_single_file = '' single_file = args.output[0] if len(args.output) > 1: - forward_single_file = args.output[1] + forward_single_file = args.output[1] else: - a,b = os.path.splitext(single_file) + a, b = os.path.splitext(single_file) forward_single_file = a + '_forward' + b script_path = os.path.normpath(os.path.dirname(os.path.realpath(__file__))) working_dir = os.getcwd() @@ -67,84 +75,100 @@ module_path = os.path.join(script_path) includes = set([]) standard_include = re.compile(r'#include <(.*?)>') local_include = re.compile(r'#(\s*?)include "(.*?)"') +project_include = re.compile(r'#(\s*?)include ') +pragma_once_cpp = re.compile(r'(\s*)#(\s*)pragma(\s+)once') ifndef_cpp = re.compile(r'#ifndef SOL_.*?_HPP') define_cpp = re.compile(r'#define SOL_.*?_HPP') endif_cpp = re.compile(r'#endif // SOL_.*?_HPP') -def get_include(line, base_path): - local_match = local_include.match(line) - if local_match: - # local include found - full_path = os.path.normpath(os.path.join(base_path, local_match.group(2))).replace('\\', '/') - return full_path - return None +def get_include(line, base_path): + local_match = local_include.match(line) + if local_match: + # local include found + full_path = os.path.normpath( + os.path.join(base_path, local_match.group(2))).replace( + '\\', '/') + return full_path + project_match = project_include.match(line) + if project_match: + # local include found + full_path = os.path.normpath( + os.path.join(base_path, project_match.group(2))).replace( + '\\', '/') + return full_path + return None def is_include_guard(line): - return ifndef_cpp.match(line) or define_cpp.match(line) or endif_cpp.match(line) + return ifndef_cpp.match(line) or define_cpp.match( + line) or endif_cpp.match(line) or pragma_once_cpp.match(line) + def get_revision(): - return os.popen('git rev-parse --short HEAD').read().strip() + return os.popen('git rev-parse --short HEAD').read().strip() + def get_version(): - return os.popen('git describe --tags --abbrev=0').read().strip() + return os.popen('git describe --tags --abbrev=0').read().strip() + def process_file(filename, out): - global includes - filename = os.path.normpath(filename) - relativefilename = filename.replace(script_path + os.sep, "").replace("\\", "/") + global includes + filename = os.path.normpath(filename) + relativefilename = filename.replace(script_path + os.sep, "").replace( + "\\", "/") - if filename in includes: - return + if filename in includes: + return - includes.add(filename) + includes.add(filename) - if not args.quiet: - print('processing {}'.format(filename)) - - out.write('// beginning of {}\n\n'.format(relativefilename)) - empty_line_state = True + if not args.quiet: + print('processing {}'.format(filename)) - with open(filename, 'r', encoding='utf-8') as f: - for line in f: - # skip comments - if line.startswith('//'): - continue + out.write('// beginning of {}\n\n'.format(relativefilename)) + empty_line_state = True - # skip include guard non-sense - if is_include_guard(line): - continue + with open(filename, 'r', encoding='utf-8') as f: + for line in f: + # skip comments + if line.startswith('//'): + continue - # get relative directory - base_path = os.path.dirname(filename) + # skip include guard non-sense + if is_include_guard(line): + continue - # check if it's a standard file - std = standard_include.search(line) - if std: - std_file = os.path.join('std', std.group(0)) - if std_file in includes: - continue - includes.add(std_file) + # get relative directory + base_path = os.path.dirname(filename) - # see if it's an include file - name = get_include(line, base_path) + # check if it's a standard file + std = standard_include.search(line) + if std: + std_file = os.path.join('std', std.group(0)) + if std_file in includes: + continue + includes.add(std_file) - if name: - process_file(name, out) - continue + # see if it's an include file + name = get_include(line, base_path) - empty_line = len(line.strip()) == 0 + if name: + process_file(name, out) + continue - if empty_line and empty_line_state: - continue + empty_line = len(line.strip()) == 0 - empty_line_state = empty_line + if empty_line and empty_line_state: + continue - # line is fine - out.write(line) + empty_line_state = empty_line - out.write('// end of {}\n\n'.format(relativefilename)) + # line is fine + out.write(line) + + out.write('// end of {}\n\n'.format(relativefilename)) version = get_version() @@ -153,49 +177,61 @@ include_guard = 'SOL_SINGLE_INCLUDE_HPP' forward_include_guard = 'SOL_SINGLE_INCLUDE_FORWARD_HPP' processed_files = [os.path.join(script_path, x) for x in ['sol.hpp']] -forward_processed_files = [os.path.join(script_path, x) for x in ['sol/forward.hpp']] +forward_processed_files = [ + os.path.join(script_path, x) for x in ['sol/forward.hpp'] +] result = '' forward_result = '' - if not args.quiet: - print('Current version: {version} (revision {revision})\n'.format(version = version, revision = revision)) - print('Creating single header for sol') + print('Current version: {version} (revision {revision})\n'.format( + version=version, revision=revision)) + print('Creating single header for sol') ss = StringIO() -ss.write(intro.format(time=dt.datetime.utcnow(), revision=revision, version=version, guard=include_guard)) +ss.write( + intro.format( + time=dt.datetime.utcnow(), + revision=revision, + version=version, + guard=include_guard)) for processed_file in processed_files: - process_file(processed_file, ss) + process_file(processed_file, ss) ss.write('#endif // {}\n'.format(include_guard)) result = ss.getvalue() ss.close() if not args.quiet: - print('finished creating single header for sol\n') + print('finished creating single header for sol\n') if not args.quiet: - print('Creating single forward declaration header for sol') + print('Creating single forward declaration header for sol') includes = set([]) forward_ss = StringIO() -forward_ss.write(intro.format(time=dt.datetime.utcnow(), revision=revision, version=version, guard=forward_include_guard)) +forward_ss.write( + intro.format( + time=dt.datetime.utcnow(), + revision=revision, + version=version, + guard=forward_include_guard)) for forward_processed_file in forward_processed_files: - process_file(forward_processed_file, forward_ss) + process_file(forward_processed_file, forward_ss) forward_ss.write('#endif // {}\n'.format(forward_include_guard)) forward_result = forward_ss.getvalue() forward_ss.close() if not args.quiet: - print('finished creating single forward declaration header for sol\n') + print('finished creating single forward declaration header for sol\n') with open(single_file, 'w', encoding='utf-8') as f: - if not args.quiet: - print('writing {}...'.format(single_file)) - f.write(result) + if not args.quiet: + print('writing {}...'.format(single_file)) + f.write(result) with open(forward_single_file, 'w', encoding='utf-8') as f: - if not args.quiet: - print('writing {}...'.format(forward_single_file)) - f.write(forward_result) + if not args.quiet: + print('writing {}...'.format(forward_single_file)) + f.write(forward_result) diff --git a/sol.hpp b/sol.hpp index da464f26..fcc2db1c 100644 --- a/sol.hpp +++ b/sol.hpp @@ -23,24 +23,24 @@ #define SOL_HPP #if defined(UE_BUILD_DEBUG) || defined(UE_BUILD_DEVELOPMENT) || defined(UE_BUILD_TEST) || defined(UE_BUILD_SHIPPING) || defined(UE_SERVER) -#define SOL_INSIDE_UNREAL +#define SOL_INSIDE_UNREAL 1 #endif // Unreal Engine 4 bullshit -#ifdef SOL_INSIDE_UNREAL +#if defined(SOL_INSIDE_UNREAL) && SOL_INSIDE_UNREAL #ifdef check #define SOL_INSIDE_UNREAL_REMOVED_CHECK #undef check #endif #endif // Unreal Engine 4 Bullshit -#ifdef __GNUC__ +#if defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" #pragma GCC diagnostic ignored "-Wconversion" #if __GNUC__ > 6 #pragma GCC diagnostic ignored "-Wnoexcept-type" #endif -#elif defined __clang__ +#elif defined(__clang__) // we'll just let this alone for now #elif defined _MSC_VER #pragma warning( push ) @@ -61,14 +61,14 @@ #include "sol/variadic_args.hpp" #include "sol/variadic_results.hpp" -#ifdef __GNUC__ +#if defined(__GNUC__) #pragma GCC diagnostic pop #elif defined _MSC_VER #pragma warning( push ) #endif // g++ -#ifdef SOL_INSIDE_UNREAL -#ifdef SOL_INSIDE_UNREAL_REMOVED_CHECK +#if defined(SOL_INSIDE_UNREAL) && SOL_INSIDE_UNREAL +#if defined(SOL_INSIDE_UNREAL_REMOVED_CHECK) && SOL_INSIDE_UNREAL_REMOVED_CHECK #if DO_CHECK #define check(expr) { if(UNLIKELY(!(expr))) { FDebug::LogAssertFailedMessage( #expr, __FILE__, __LINE__ ); _DebugBreakAndPromptForRemote(); FDebug::AssertFailed( #expr, __FILE__, __LINE__ ); CA_ASSUME(false); } } #else diff --git a/sol/bind_traits.hpp b/sol/bind_traits.hpp index 77072455..27356d66 100644 --- a/sol/bind_traits.hpp +++ b/sol/bind_traits.hpp @@ -200,7 +200,7 @@ namespace meta { typedef R (T::*function_pointer_type)(Args..., ...) const volatile&&; }; -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE template struct fx_traits : basic_traits { @@ -373,7 +373,7 @@ namespace meta { typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&&; }; -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE template struct fx_traits : basic_traits { diff --git a/sol/call.hpp b/sol/call.hpp index 4573cb63..5abdb2db 100644 --- a/sol/call.hpp +++ b/sol/call.hpp @@ -314,7 +314,7 @@ namespace sol { } }; -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE template struct agnostic_lua_call_wrapper { static int call(lua_State* L, detail::lua_CFunction_noexcept f) { @@ -371,7 +371,7 @@ namespace sol { template static int call(lua_State* L, Fx&& f) { typedef std::conditional_t::value, object_type, T> Ta; -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto maybeo = stack::check_get(L, 1); if (!maybeo || maybeo.value() == nullptr) { return luaL_error(L, "sol: received nil for 'self' argument (use ':' for accessing member functions, make sure member variables are preceeded by the actual object with '.' syntax)"); @@ -401,7 +401,7 @@ namespace sol { template static int call_assign(std::true_type, lua_State* L, V&& f) { typedef std::conditional_t::value, object_type, T> Ta; -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto maybeo = stack::check_get(L, 1); if (!maybeo || maybeo.value() == nullptr) { if (is_variable) { @@ -461,7 +461,7 @@ namespace sol { template static int call(lua_State* L, V&& f) { typedef std::conditional_t::value, object_type, T> Ta; -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto maybeo = stack::check_get(L, 1); if (!maybeo || maybeo.value() == nullptr) { if (is_variable) { @@ -642,7 +642,7 @@ namespace sol { typedef meta::pop_front_type_t args_list; typedef T Ta; typedef std::remove_pointer_t Oa; -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto maybeo = stack::check_get(L, 1); if (!maybeo || maybeo.value() == nullptr) { if (is_variable) { diff --git a/sol/compatibility.hpp b/sol/compatibility.hpp index 6e9c7d1e..392e3c2f 100644 --- a/sol/compatibility.hpp +++ b/sol/compatibility.hpp @@ -32,15 +32,17 @@ #include "feature_test.hpp" #include "compatibility/version.hpp" -#ifndef SOL_NO_COMPAT +#if !defined(SOL_NO_COMPAT) || !(SOL_NO_COMPAT) -#if defined(SOL_USING_CXX_LUA) +#if defined(SOL_USING_CXX_LUA) && SOL_USING_CXX_LUA #ifndef COMPAT53_LUA_CPP #define COMPAT53_LUA_CPP 1 +#endif // Build Lua Compat layer as C++ #endif -#endif - -#include "compatibility//compat-5.3.h" +#ifndef COMPAT53_INCLUDE_SOURCE +#define COMPAT53_INCLUDE_SOURCE 1 +#endif // Build Compat Layer Inline +#include "compatibility/compat-5.3.h" #endif // SOL_NO_COMPAT diff --git a/sol/compatibility/compat-5.3.c b/sol/compatibility/compat-5.3.c index b82c654f..94c0e968 100644 --- a/sol/compatibility/compat-5.3.c +++ b/sol/compatibility/compat-5.3.c @@ -1,881 +1,890 @@ -#include -#include -#include -#include -#include -#include -#include "compat-5.3.h" - -/* don't compile it again if it already is included via compat53.h */ -#ifndef COMPAT53_C_ -#define COMPAT53_C_ - - - -/* definitions for Lua 5.1 only */ -#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 - -#ifndef COMPAT53_FOPEN_NO_LOCK -# if defined(_MSC_VER) -# define COMPAT53_FOPEN_NO_LOCK 1 -# else /* otherwise */ -# define COMPAT53_FOPEN_NO_LOCK 0 -# endif /* VC++ only so far */ -#endif /* No-lock fopen_s usage if possible */ - -#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK -# include -#endif /* VC++ _fsopen for share-allowed file read */ - -#ifndef COMPAT53_HAVE_STRERROR_R -# if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || \ - (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) -# define COMPAT53_HAVE_STRERROR_R 1 -# else /* none of the defines matched: define to 0 */ -# define COMPAT53_HAVE_STRERROR_R 0 -# endif /* have strerror_r of some form */ -#endif /* strerror_r */ - -#ifndef COMPAT53_HAVE_STRERROR_S -# if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ - (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__) -# define COMPAT53_HAVE_STRERROR_S 1 -# else /* not VC++ or C11 */ -# define COMPAT53_HAVE_STRERROR_S 0 -# endif /* strerror_s from VC++ or C11 */ -#endif /* strerror_s */ - -#ifndef COMPAT53_LUA_FILE_BUFFER_SIZE -# define COMPAT53_LUA_FILE_BUFFER_SIZE 4096 -#endif /* Lua File Buffer Size */ - - -static char* compat53_strerror (int en, char* buff, size_t sz) { -#if COMPAT53_HAVE_STRERROR_R - /* use strerror_r here, because it's available on these specific platforms */ - if (sz > 0) { - buff[0] = '\0'; - /* we don't care whether the GNU version or the XSI version is used: */ - if (strerror_r(en, buff, sz)) { - /* Yes, we really DO want to ignore the return value! - * GCC makes that extra hard, not even a (void) cast will do. */ - } - if (buff[0] == '\0') { - /* Buffer is unchanged, so we probably have called GNU strerror_r which - * returned a static constant string. Chances are that strerror will - * return the same static constant string and therefore be thread-safe. */ - return strerror(en); - } - } - return buff; /* sz is 0 *or* strerror_r wrote into the buffer */ -#elif COMPAT53_HAVE_STRERROR_S - /* for MSVC and other C11 implementations, use strerror_s since it's - * provided by default by the libraries */ - strerror_s(buff, sz, en); - return buff; -#else - /* fallback, but strerror is not guaranteed to be threadsafe due to modifying - * errno itself and some impls not locking a static buffer for it ... but most - * known systems have threadsafe errno: this might only change if the locale - * is changed out from under someone while this function is being called */ - (void)buff; - (void)sz; - return strerror(en); -#endif -} - - -COMPAT53_API int lua_absindex (lua_State *L, int i) { - if (i < 0 && i > LUA_REGISTRYINDEX) - i += lua_gettop(L) + 1; - return i; -} - - -static void compat53_call_lua (lua_State *L, char const code[], size_t len, - int nargs, int nret) { - lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code); - if (lua_type(L, -1) != LUA_TFUNCTION) { - lua_pop(L, 1); - if (luaL_loadbuffer(L, code, len, "=none")) - lua_error(L); - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code); - } - lua_insert(L, -nargs-1); - lua_call(L, nargs, nret); -} - - -static const char compat53_arith_code[] = - "local op,a,b=...\n" - "if op==0 then return a+b\n" - "elseif op==1 then return a-b\n" - "elseif op==2 then return a*b\n" - "elseif op==3 then return a/b\n" - "elseif op==4 then return a%b\n" - "elseif op==5 then return a^b\n" - "elseif op==6 then return -a\n" - "end\n"; - -COMPAT53_API void lua_arith (lua_State *L, int op) { - if (op < LUA_OPADD || op > LUA_OPUNM) - luaL_error(L, "invalid 'op' argument for lua_arith"); - luaL_checkstack(L, 5, "not enough stack slots"); - if (op == LUA_OPUNM) - lua_pushvalue(L, -1); - lua_pushnumber(L, op); - lua_insert(L, -3); - compat53_call_lua(L, compat53_arith_code, - sizeof(compat53_arith_code)-1, 3, 1); -} - - -static const char compat53_compare_code[] = - "local a,b=...\n" - "return a<=b\n"; - -COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) { - int result = 0; - switch (op) { - case LUA_OPEQ: - return lua_equal(L, idx1, idx2); - case LUA_OPLT: - return lua_lessthan(L, idx1, idx2); - case LUA_OPLE: - luaL_checkstack(L, 5, "not enough stack slots"); - idx1 = lua_absindex(L, idx1); - idx2 = lua_absindex(L, idx2); - lua_pushvalue(L, idx1); - lua_pushvalue(L, idx2); - compat53_call_lua(L, compat53_compare_code, - sizeof(compat53_compare_code)-1, 2, 1); - result = lua_toboolean(L, -1); - lua_pop(L, 1); - return result; - default: - luaL_error(L, "invalid 'op' argument for lua_compare"); - } - return 0; -} - - -COMPAT53_API void lua_copy (lua_State *L, int from, int to) { - int abs_to = lua_absindex(L, to); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushvalue(L, from); - lua_replace(L, abs_to); -} - - -COMPAT53_API void lua_len (lua_State *L, int i) { - switch (lua_type(L, i)) { - case LUA_TSTRING: - lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); - break; - case LUA_TTABLE: - if (!luaL_callmeta(L, i, "__len")) - lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); - break; - case LUA_TUSERDATA: - if (luaL_callmeta(L, i, "__len")) - break; - /* FALLTHROUGH */ - default: - luaL_error(L, "attempt to get length of a %s value", - lua_typename(L, lua_type(L, i))); - } -} - - -COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p) { - int abs_i = lua_absindex(L, i); - lua_pushlightuserdata(L, (void*)p); - lua_rawget(L, abs_i); - return lua_type(L, -1); -} - -COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) { - int abs_i = lua_absindex(L, i); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushlightuserdata(L, (void*)p); - lua_insert(L, -2); - lua_rawset(L, abs_i); -} - - -COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) { - lua_Integer n = lua_tointeger(L, i); - if (isnum != NULL) { - *isnum = (n != 0 || lua_isnumber(L, i)); - } - return n; -} - - -COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) { - lua_Number n = lua_tonumber(L, i); - if (isnum != NULL) { - *isnum = (n != 0 || lua_isnumber(L, i)); - } - return n; -} - - -COMPAT53_API void luaL_checkversion (lua_State *L) { - (void)L; -} - - -COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg) { - if (!lua_checkstack(L, sp+LUA_MINSTACK)) { - if (msg != NULL) - luaL_error(L, "stack overflow (%s)", msg); - else { - lua_pushliteral(L, "stack overflow"); - lua_error(L); - } - } -} - - -COMPAT53_API int luaL_getsubtable (lua_State *L, int i, const char *name) { - int abs_i = lua_absindex(L, i); - luaL_checkstack(L, 3, "not enough stack slots"); - lua_pushstring(L, name); - lua_gettable(L, abs_i); - if (lua_istable(L, -1)) - return 1; - lua_pop(L, 1); - lua_newtable(L); - lua_pushstring(L, name); - lua_pushvalue(L, -2); - lua_settable(L, abs_i); - return 0; -} - - -COMPAT53_API lua_Integer luaL_len (lua_State *L, int i) { - lua_Integer res = 0; - int isnum = 0; - luaL_checkstack(L, 1, "not enough stack slots"); - lua_len(L, i); - res = lua_tointegerx(L, -1, &isnum); - lua_pop(L, 1); - if (!isnum) - luaL_error(L, "object length is not an integer"); - return res; -} - - -COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkstack(L, nup+1, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - lua_pushstring(L, l->name); - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -(nup + 1)); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ - } - lua_pop(L, nup); /* remove upvalues */ -} - - -COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname) { - luaL_checkstack(L, 1, "not enough stack slots"); - luaL_getmetatable(L, tname); - lua_setmetatable(L, -2); -} - - -COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) { - void *p = lua_touserdata(L, i); - luaL_checkstack(L, 2, "not enough stack slots"); - if (p == NULL || !lua_getmetatable(L, i)) - return NULL; - else { - int res = 0; - luaL_getmetatable(L, tname); - res = lua_rawequal(L, -1, -2); - lua_pop(L, 2); - if (!res) - p = NULL; - } - return p; -} - - -static int compat53_countlevels (lua_State *L) { - lua_Debug ar; - int li = 1, le = 1; - /* find an upper bound */ - while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } - /* do a binary search */ - while (li < le) { - int m = (li + le)/2; - if (lua_getstack(L, m, &ar)) li = m + 1; - else le = m; - } - return le - 1; -} - -static int compat53_findfield (lua_State *L, int objidx, int level) { - if (level == 0 || !lua_istable(L, -1)) - return 0; /* not found */ - lua_pushnil(L); /* start 'next' loop */ - while (lua_next(L, -2)) { /* for each pair in table */ - if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ - if (lua_rawequal(L, objidx, -1)) { /* found object? */ - lua_pop(L, 1); /* remove value (but keep name) */ - return 1; - } - else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); - return 1; - } - } - lua_pop(L, 1); /* remove value */ - } - return 0; /* not found */ -} - -static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) { - int top = lua_gettop(L); - lua_getinfo(L, "f", ar); /* push function */ - lua_pushvalue(L, LUA_GLOBALSINDEX); - if (compat53_findfield(L, top + 1, 2)) { - lua_copy(L, -1, top + 1); /* move name to proper place */ - lua_pop(L, 2); /* remove pushed values */ - return 1; - } - else { - lua_settop(L, top); /* remove function and global table */ - return 0; - } -} - -static void compat53_pushfuncname (lua_State *L, lua_Debug *ar) { - if (*ar->namewhat != '\0') /* is there a name? */ - lua_pushfstring(L, "function " LUA_QS, ar->name); - else if (*ar->what == 'm') /* main? */ - lua_pushliteral(L, "main chunk"); - else if (*ar->what == 'C') { - if (compat53_pushglobalfuncname(L, ar)) { - lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else - lua_pushliteral(L, "?"); - } - else - lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); -} - -#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */ -#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */ - -COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, - const char *msg, int level) { - lua_Debug ar; - int top = lua_gettop(L); - int numlevels = compat53_countlevels(L1); - int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0; - if (msg) lua_pushfstring(L, "%s\n", msg); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - if (level == mark) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */ - } - else { - lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); - compat53_pushfuncname(L, &ar); - lua_concat(L, lua_gettop(L) - top); - } - } - lua_concat(L, lua_gettop(L) - top); -} - - -COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { - const char *serr = NULL; - int en = errno; /* calls to Lua API may change this value */ - char buf[512] = { 0 }; - if (stat) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushnil(L); - serr = compat53_strerror(en, buf, sizeof(buf)); - if (fname) - lua_pushfstring(L, "%s: %s", fname, serr); - else - lua_pushstring(L, serr); - lua_pushnumber(L, (lua_Number)en); - return 3; - } -} - - -static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) { - if (mode && strchr(mode, modename[0]) == NULL) { - lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode); - return err; - } - return LUA_OK; -} - - -typedef struct { - lua_Reader reader; - void *ud; - int has_peeked_data; - const char *peeked_data; - size_t peeked_data_size; -} compat53_reader_data; - - -static const char *compat53_reader (lua_State *L, void *ud, size_t *size) { - compat53_reader_data *data = (compat53_reader_data *)ud; - if (data->has_peeked_data) { - data->has_peeked_data = 0; - *size = data->peeked_data_size; - return data->peeked_data; - } else - return data->reader(L, data->ud, size); -} - - -COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { - int status = LUA_OK; - compat53_reader_data compat53_data = { reader, data, 1, 0, 0 }; - compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size)); - if (compat53_data.peeked_data && compat53_data.peeked_data_size && - compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */ - status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); - else - status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX); - if (status != LUA_OK) - return status; - /* we need to call the original 5.1 version of lua_load! */ -#undef lua_load - return lua_load(L, compat53_reader, &compat53_data, source); -#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) -} - - -typedef struct { - int n; /* number of pre-read characters */ - FILE *f; /* file being read */ - char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */ -} compat53_LoadF; - - -static const char *compat53_getF (lua_State *L, void *ud, size_t *size) { - compat53_LoadF *lf = (compat53_LoadF *)ud; - (void)L; /* not used */ - if (lf->n > 0) { /* are there pre-read characters to be read? */ - *size = lf->n; /* return them (chars already in buffer) */ - lf->n = 0; /* no more pre-read characters */ - } - else { /* read a block from file */ - /* 'fread' can return > 0 *and* set the EOF flag. If next call to - 'compat53_getF' called 'fread', it might still wait for user input. - The next check avoids this problem. */ - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ - } - return lf->buff; -} - - -static int compat53_errfile (lua_State *L, const char *what, int fnameindex) { - char buf[512] = {0}; - const char *serr = compat53_strerror(errno, buf, sizeof(buf)); - const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); - lua_remove(L, fnameindex); - return LUA_ERRFILE; -} - - -static int compat53_skipBOM (compat53_LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = (char)c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ -} - - -/* -** reads the first character of file 'f' and skips an optional BOM mark -** in its beginning plus its first line if it starts with '#'. Returns -** true if it skipped the first line. In any case, '*cp' has the -** first "valid" character of the file (after the optional BOM and -** a first-line comment). -*/ -static int compat53_skipcomment (compat53_LoadF *lf, int *cp) { - int c = *cp = compat53_skipBOM(lf); - if (c == '#') { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(lf->f); - } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ - return 1; /* there was a comment */ - } - else return 0; /* no comment */ -} - - -COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { - compat53_LoadF lf; - int status, readstatus; - int c; - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ - if (filename == NULL) { - lua_pushliteral(L, "=stdin"); - lf.f = stdin; - } - else { - lua_pushfstring(L, "@%s", filename); -#if defined(_MSC_VER) - /* This code is here to stop a deprecation error that stops builds - * if a certain macro is defined. While normally not caring would - * be best, some header-only libraries and builds can't afford to - * dictate this to the user. A quick check shows that fopen_s this - * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET, - * possibly even before that so we don't need to do any version - * number checks, since this has been there since forever. */ - - /* TO USER: if you want the behavior of typical fopen_s/fopen, - * which does lock the file on VC++, define the macro used below to 0 */ -#if COMPAT53_FOPEN_NO_LOCK - lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */ - if (lf.f == NULL) - return compat53_errfile(L, "open", fnameindex); -#else /* use default locking version */ - if (fopen_s(&lf.f, filename, "r") != 0) - return compat53_errfile(L, "open", fnameindex); -#endif /* Locking vs. No-locking fopen variants */ -#else - lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */ - if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex); -#endif - } - if (compat53_skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ -#if defined(_MSC_VER) - if (freopen_s(&lf.f, filename, "rb", lf.f) != 0) - return compat53_errfile(L, "reopen", fnameindex); -#else - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex); -#endif - compat53_skipcomment(&lf, &c); /* re-read initial portion */ - } - if (c != EOF) - lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */ - status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ - if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ - return compat53_errfile(L, "read", fnameindex); - } - lua_remove(L, fnameindex); - return status; -} - - -COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) { - int status = LUA_OK; - if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) { - status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); - } - else { - status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX); - } - if (status != LUA_OK) - return status; - return luaL_loadbuffer(L, buff, sz, name); -} - - -#if !defined(l_inspectstat) && \ - (defined(unix) || defined(__unix) || defined(__unix__) || \ - defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \ - (defined(__APPLE__) && defined(__MACH__))) -/* some form of unix; check feature macros in unistd.h for details */ -# include -/* check posix version; the relevant include files and macros probably - * were available before 2001, but I'm not sure */ -# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L -# include -# define l_inspectstat(stat,what) \ - if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ - else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } -# endif -#endif - -/* provide default (no-op) version */ -#if !defined(l_inspectstat) -# define l_inspectstat(stat,what) ((void)0) -#endif - - -COMPAT53_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; - if (stat == -1) - return luaL_fileresult(L, 0, NULL); - else { - l_inspectstat(stat, what); - if (*what == 'e' && stat == 0) - lua_pushboolean(L, 1); - else - lua_pushnil(L); - lua_pushstring(L, what); - lua_pushinteger(L, stat); - return 3; - } -} - - -COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) { - /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ - B->b.p = NULL; - B->b.L = NULL; - B->b.lvl = 0; - /* reuse the buffer from the 5.1-style luaL_Buffer though! */ - B->ptr = B->b.buffer; - B->capacity = LUAL_BUFFERSIZE; - B->nelems = 0; - B->L2 = L; -} - - -COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) { - if (B->capacity - B->nelems < s) { /* needs to grow */ - char* newptr = NULL; - size_t newcap = B->capacity * 2; - if (newcap - B->nelems < s) - newcap = B->nelems + s; - if (newcap < B->capacity) /* overflow */ - luaL_error(B->L2, "buffer too large"); - newptr = (char*)lua_newuserdata(B->L2, newcap); - memcpy(newptr, B->ptr, B->nelems); - if (B->ptr != B->b.buffer) - lua_replace(B->L2, -2); /* remove old buffer */ - B->ptr = newptr; - B->capacity = newcap; - } - return B->ptr+B->nelems; -} - - -COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) { - memcpy(luaL_prepbuffsize(B, l), s, l); - luaL_addsize(B, l); -} - - -COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B) { - size_t len = 0; - const char *s = lua_tolstring(B->L2, -1, &len); - if (!s) - luaL_error(B->L2, "cannot convert value to string"); - if (B->ptr != B->b.buffer) - lua_insert(B->L2, -2); /* userdata buffer must be at stack top */ - luaL_addlstring(B, s, len); - lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1); -} - - -void luaL_pushresult (luaL_Buffer_53 *B) { - lua_pushlstring(B->L2, B->ptr, B->nelems); - if (B->ptr != B->b.buffer) - lua_replace(B->L2, -2); /* remove userdata buffer */ -} - - -#endif /* Lua 5.1 */ - - - -/* definitions for Lua 5.1 and Lua 5.2 */ -#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502 - - -COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) { - index = lua_absindex(L, index); - lua_pushinteger(L, i); - lua_gettable(L, index); - return lua_type(L, -1); -} - - -COMPAT53_API int lua_isinteger (lua_State *L, int index) { - if (lua_type(L, index) == LUA_TNUMBER) { - lua_Number n = lua_tonumber(L, index); - lua_Integer i = lua_tointeger(L, index); - if (i == n) - return 1; - } - return 0; -} - - -static void compat53_reverse (lua_State *L, int a, int b) { - for (; a < b; ++a, --b) { - lua_pushvalue(L, a); - lua_pushvalue(L, b); - lua_replace(L, a); - lua_replace(L, b); - } -} - - -COMPAT53_API void lua_rotate (lua_State *L, int idx, int n) { - int n_elems = 0; - idx = lua_absindex(L, idx); - n_elems = lua_gettop(L)-idx+1; - if (n < 0) - n += n_elems; - if ( n > 0 && n < n_elems) { - luaL_checkstack(L, 2, "not enough stack slots available"); - n = n_elems - n; - compat53_reverse(L, idx, idx+n-1); - compat53_reverse(L, idx+n, idx+n_elems-1); - compat53_reverse(L, idx, idx+n_elems-1); - } -} - - -COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i) { - luaL_checkstack(L, 1, "not enough stack slots available"); - index = lua_absindex(L, index); - lua_pushinteger(L, i); - lua_insert(L, -2); - lua_settable(L, index); -} - - -#if !defined(lua_str2number) -# define lua_str2number(s, p) strtod((s), (p)) -#endif - -COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) { - char* endptr; - lua_Number n = lua_str2number(s, &endptr); - if (endptr != s) { - while (*endptr != '\0' && isspace((unsigned char)*endptr)) - ++endptr; - if (*endptr == '\0') { - lua_pushnumber(L, n); - return endptr - s + 1; - } - } - return 0; -} - - -COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - if (!luaL_callmeta(L, idx, "__tostring")) { - int t = lua_type(L, idx), tt = 0; - char const* name = NULL; - switch (t) { - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - case LUA_TSTRING: - case LUA_TNUMBER: - lua_pushvalue(L, idx); - break; - case LUA_TBOOLEAN: - if (lua_toboolean(L, idx)) - lua_pushliteral(L, "true"); - else - lua_pushliteral(L, "false"); - break; - default: - tt = luaL_getmetafield(L, idx, "__name"); - name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t); - lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx)); - if (tt != LUA_TNIL) - lua_replace(L, -2); - break; - } - } else { - if (!lua_isstring(L, -1)) - luaL_error(L, "'__tostring' must return a string"); - } - return lua_tolstring(L, -1, len); -} - - -COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, - lua_CFunction openf, int glb) { - luaL_checkstack(L, 3, "not enough stack slots available"); - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - if (lua_getfield(L, -1, modname) == LUA_TNIL) { - lua_pop(L, 1); - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); - lua_call(L, 1, 1); - lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); - } - if (glb) { - lua_pushvalue(L, -1); - lua_setglobal(L, modname); - } - lua_replace(L, -2); -} - - -#endif /* Lua 5.1 and 5.2 */ - - -#endif /* COMPAT53_C_ */ - - -/********************************************************************* -* This file contains parts of Lua 5.2's and Lua 5.3's source code: -* -* Copyright (C) 1994-2014 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ - +#include +#include +#include +#include +#include +#include +#include "compat-5.3.h" + +/* don't compile it again if it already is included via compat53.h */ +#ifndef KEPLER_PROJECT_COMPAT53_C_ +#define KEPLER_PROJECT_COMPAT53_C_ + + + +/* definitions for Lua 5.1 only */ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 + +#ifndef COMPAT53_FOPEN_NO_LOCK +# if defined(_MSC_VER) +# define COMPAT53_FOPEN_NO_LOCK 1 +# else /* otherwise */ +# define COMPAT53_FOPEN_NO_LOCK 0 +# endif /* VC++ only so far */ +#endif /* No-lock fopen_s usage if possible */ + +#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK +# include +#endif /* VC++ _fsopen for share-allowed file read */ + +#ifndef COMPAT53_HAVE_STRERROR_R +# if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || \ + (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) +# define COMPAT53_HAVE_STRERROR_R 1 +# else /* none of the defines matched: define to 0 */ +# define COMPAT53_HAVE_STRERROR_R 0 +# endif /* have strerror_r of some form */ +#endif /* strerror_r */ + +#ifndef COMPAT53_HAVE_STRERROR_S +# if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ + (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__) +# define COMPAT53_HAVE_STRERROR_S 1 +# else /* not VC++ or C11 */ +# define COMPAT53_HAVE_STRERROR_S 0 +# endif /* strerror_s from VC++ or C11 */ +#endif /* strerror_s */ + +#ifndef COMPAT53_LUA_FILE_BUFFER_SIZE +# define COMPAT53_LUA_FILE_BUFFER_SIZE 4096 +#endif /* Lua File Buffer Size */ + + +static char* compat53_strerror(int en, char* buff, size_t sz) { +#if COMPAT53_HAVE_STRERROR_R + /* use strerror_r here, because it's available on these specific platforms */ + if (sz > 0) { + buff[0] = '\0'; + /* we don't care whether the GNU version or the XSI version is used: */ + if (strerror_r(en, buff, sz)) { + /* Yes, we really DO want to ignore the return value! + * GCC makes that extra hard, not even a (void) cast will do. */ + } + if (buff[0] == '\0') { + /* Buffer is unchanged, so we probably have called GNU strerror_r which + * returned a static constant string. Chances are that strerror will + * return the same static constant string and therefore be thread-safe. */ + return strerror(en); + } + } + return buff; /* sz is 0 *or* strerror_r wrote into the buffer */ +#elif COMPAT53_HAVE_STRERROR_S + /* for MSVC and other C11 implementations, use strerror_s since it's + * provided by default by the libraries */ + strerror_s(buff, sz, en); + return buff; +#else + /* fallback, but strerror is not guaranteed to be threadsafe due to modifying + * errno itself and some impls not locking a static buffer for it ... but most + * known systems have threadsafe errno: this might only change if the locale + * is changed out from under someone while this function is being called */ + (void)buff; + (void)sz; + return strerror(en); +#endif +} + + +COMPAT53_API int lua_absindex(lua_State *L, int i) { + if (i < 0 && i > LUA_REGISTRYINDEX) + i += lua_gettop(L) + 1; + return i; +} + + +static void compat53_call_lua(lua_State *L, char const code[], size_t len, + int nargs, int nret) { + lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code); + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 1); + if (luaL_loadbuffer(L, code, len, "=none")) + lua_error(L); + lua_pushvalue(L, -1); + lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code); + } + lua_insert(L, -nargs - 1); + lua_call(L, nargs, nret); +} + + +static const char compat53_arith_code[] = +"local op,a,b=...\n" +"if op==0 then return a+b\n" +"elseif op==1 then return a-b\n" +"elseif op==2 then return a*b\n" +"elseif op==3 then return a/b\n" +"elseif op==4 then return a%b\n" +"elseif op==5 then return a^b\n" +"elseif op==6 then return -a\n" +"end\n"; + +COMPAT53_API void lua_arith(lua_State *L, int op) { + if (op < LUA_OPADD || op > LUA_OPUNM) + luaL_error(L, "invalid 'op' argument for lua_arith"); + luaL_checkstack(L, 5, "not enough stack slots"); + if (op == LUA_OPUNM) + lua_pushvalue(L, -1); + lua_pushnumber(L, op); + lua_insert(L, -3); + compat53_call_lua(L, compat53_arith_code, + sizeof(compat53_arith_code) - 1, 3, 1); +} + + +static const char compat53_compare_code[] = +"local a,b=...\n" +"return a<=b\n"; + +COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op) { + int result = 0; + switch (op) { + case LUA_OPEQ: + return lua_equal(L, idx1, idx2); + case LUA_OPLT: + return lua_lessthan(L, idx1, idx2); + case LUA_OPLE: + luaL_checkstack(L, 5, "not enough stack slots"); + idx1 = lua_absindex(L, idx1); + idx2 = lua_absindex(L, idx2); + lua_pushvalue(L, idx1); + lua_pushvalue(L, idx2); + compat53_call_lua(L, compat53_compare_code, + sizeof(compat53_compare_code) - 1, 2, 1); + result = lua_toboolean(L, -1); + lua_pop(L, 1); + return result; + default: + luaL_error(L, "invalid 'op' argument for lua_compare"); + } + return 0; +} + + +COMPAT53_API void lua_copy(lua_State *L, int from, int to) { + int abs_to = lua_absindex(L, to); + luaL_checkstack(L, 1, "not enough stack slots"); + lua_pushvalue(L, from); + lua_replace(L, abs_to); +} + + +COMPAT53_API void lua_len(lua_State *L, int i) { + switch (lua_type(L, i)) { + case LUA_TSTRING: + lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); + break; + case LUA_TTABLE: + if (!luaL_callmeta(L, i, "__len")) + lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); + break; + case LUA_TUSERDATA: + if (luaL_callmeta(L, i, "__len")) + break; + /* FALLTHROUGH */ + default: + luaL_error(L, "attempt to get length of a %s value", + lua_typename(L, lua_type(L, i))); + } +} + + +COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p) { + int abs_i = lua_absindex(L, i); + lua_pushlightuserdata(L, (void*)p); + lua_rawget(L, abs_i); + return lua_type(L, -1); +} + +COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p) { + int abs_i = lua_absindex(L, i); + luaL_checkstack(L, 1, "not enough stack slots"); + lua_pushlightuserdata(L, (void*)p); + lua_insert(L, -2); + lua_rawset(L, abs_i); +} + + +COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum) { + lua_Number n = lua_tonumber(L, i); + if (isnum != NULL) { + *isnum = (n != 0 || lua_isnumber(L, i)); + } + return n; +} + + +COMPAT53_API void luaL_checkversion(lua_State *L) { + (void)L; +} + + +COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg) { + if (!lua_checkstack(L, sp + LUA_MINSTACK)) { + if (msg != NULL) + luaL_error(L, "stack overflow (%s)", msg); + else { + lua_pushliteral(L, "stack overflow"); + lua_error(L); + } + } +} + + +COMPAT53_API int luaL_getsubtable(lua_State *L, int i, const char *name) { + int abs_i = lua_absindex(L, i); + luaL_checkstack(L, 3, "not enough stack slots"); + lua_pushstring(L, name); + lua_gettable(L, abs_i); + if (lua_istable(L, -1)) + return 1; + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + lua_settable(L, abs_i); + return 0; +} + + +COMPAT53_API lua_Integer luaL_len(lua_State *L, int i) { + lua_Integer res = 0; + int isnum = 0; + luaL_checkstack(L, 1, "not enough stack slots"); + lua_len(L, i); + res = lua_tointegerx(L, -1, &isnum); + lua_pop(L, 1); + if (!isnum) + luaL_error(L, "object length is not an integer"); + return res; +} + + +COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { + luaL_checkstack(L, nup + 1, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup + 1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ + } + lua_pop(L, nup); /* remove upvalues */ +} + + +COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname) { + luaL_checkstack(L, 1, "not enough stack slots"); + luaL_getmetatable(L, tname); + lua_setmetatable(L, -2); +} + + +COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname) { + void *p = lua_touserdata(L, i); + luaL_checkstack(L, 2, "not enough stack slots"); + if (p == NULL || !lua_getmetatable(L, i)) + return NULL; + else { + int res = 0; + luaL_getmetatable(L, tname); + res = lua_rawequal(L, -1, -2); + lua_pop(L, 2); + if (!res) + p = NULL; + } + return p; +} + + +static int compat53_countlevels(lua_State *L) { + lua_Debug ar; + int li = 1, le = 1; + /* find an upper bound */ + while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } + /* do a binary search */ + while (li < le) { + int m = (li + le) / 2; + if (lua_getstack(L, m, &ar)) li = m + 1; + else le = m; + } + return le - 1; +} + +static int compat53_findfield(lua_State *L, int objidx, int level) { + if (level == 0 || !lua_istable(L, -1)) + return 0; /* not found */ + lua_pushnil(L); /* start 'next' loop */ + while (lua_next(L, -2)) { /* for each pair in table */ + if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ + if (lua_rawequal(L, objidx, -1)) { /* found object? */ + lua_pop(L, 1); /* remove value (but keep name) */ + return 1; + } + else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */ + lua_remove(L, -2); /* remove table (but keep name) */ + lua_pushliteral(L, "."); + lua_insert(L, -2); /* place '.' between the two names */ + lua_concat(L, 3); + return 1; + } + } + lua_pop(L, 1); /* remove value */ + } + return 0; /* not found */ +} + +static int compat53_pushglobalfuncname(lua_State *L, lua_Debug *ar) { + int top = lua_gettop(L); + lua_getinfo(L, "f", ar); /* push function */ + lua_pushvalue(L, LUA_GLOBALSINDEX); + if (compat53_findfield(L, top + 1, 2)) { + lua_copy(L, -1, top + 1); /* move name to proper place */ + lua_pop(L, 2); /* remove pushed values */ + return 1; + } + else { + lua_settop(L, top); /* remove function and global table */ + return 0; + } +} + +static void compat53_pushfuncname(lua_State *L, lua_Debug *ar) { + if (*ar->namewhat != '\0') /* is there a name? */ + lua_pushfstring(L, "function " LUA_QS, ar->name); + else if (*ar->what == 'm') /* main? */ + lua_pushliteral(L, "main chunk"); + else if (*ar->what == 'C') { + if (compat53_pushglobalfuncname(L, ar)) { + lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } + else + lua_pushliteral(L, "?"); + } + else + lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); +} + +#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */ +#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */ + +COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1, + const char *msg, int level) { + lua_Debug ar; + int top = lua_gettop(L); + int numlevels = compat53_countlevels(L1); + int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0; + if (msg) lua_pushfstring(L, "%s\n", msg); + lua_pushliteral(L, "stack traceback:"); + while (lua_getstack(L1, level++, &ar)) { + if (level == mark) { /* too many levels? */ + lua_pushliteral(L, "\n\t..."); /* add a '...' */ + level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */ + } + else { + lua_getinfo(L1, "Slnt", &ar); + lua_pushfstring(L, "\n\t%s:", ar.short_src); + if (ar.currentline > 0) + lua_pushfstring(L, "%d:", ar.currentline); + lua_pushliteral(L, " in "); + compat53_pushfuncname(L, &ar); + lua_concat(L, lua_gettop(L) - top); + } + } + lua_concat(L, lua_gettop(L) - top); +} + + +COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname) { + const char *serr = NULL; + int en = errno; /* calls to Lua API may change this value */ + char buf[512] = { 0 }; + if (stat) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushnil(L); + serr = compat53_strerror(en, buf, sizeof(buf)); + if (fname) + lua_pushfstring(L, "%s: %s", fname, serr); + else + lua_pushstring(L, serr); + lua_pushnumber(L, (lua_Number)en); + return 3; + } +} + + +static int compat53_checkmode(lua_State *L, const char *mode, const char *modename, int err) { + if (mode && strchr(mode, modename[0]) == NULL) { + lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode); + return err; + } + return LUA_OK; +} + + +typedef struct { + lua_Reader reader; + void *ud; + int has_peeked_data; + const char *peeked_data; + size_t peeked_data_size; +} compat53_reader_data; + + +static const char *compat53_reader(lua_State *L, void *ud, size_t *size) { + compat53_reader_data *data = (compat53_reader_data *)ud; + if (data->has_peeked_data) { + data->has_peeked_data = 0; + *size = data->peeked_data_size; + return data->peeked_data; + } + else + return data->reader(L, data->ud, size); +} + + +COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { + int status = LUA_OK; + compat53_reader_data compat53_data = { reader, data, 1, 0, 0 }; + compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size)); + if (compat53_data.peeked_data && compat53_data.peeked_data_size && + compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */ + status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); + else + status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX); + if (status != LUA_OK) + return status; + /* we need to call the original 5.1 version of lua_load! */ +#undef lua_load + return lua_load(L, compat53_reader, &compat53_data, source); +#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) +} + + +typedef struct { + int n; /* number of pre-read characters */ + FILE *f; /* file being read */ + char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */ +} compat53_LoadF; + + +static const char *compat53_getF(lua_State *L, void *ud, size_t *size) { + compat53_LoadF *lf = (compat53_LoadF *)ud; + (void)L; /* not used */ + if (lf->n > 0) { /* are there pre-read characters to be read? */ + *size = lf->n; /* return them (chars already in buffer) */ + lf->n = 0; /* no more pre-read characters */ + } + else { /* read a block from file */ + /* 'fread' can return > 0 *and* set the EOF flag. If next call to + 'compat53_getF' called 'fread', it might still wait for user input. + The next check avoids this problem. */ + if (feof(lf->f)) return NULL; + *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ + } + return lf->buff; +} + + +static int compat53_errfile(lua_State *L, const char *what, int fnameindex) { + char buf[512] = { 0 }; + const char *serr = compat53_strerror(errno, buf, sizeof(buf)); + const char *filename = lua_tostring(L, fnameindex) + 1; + lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + lua_remove(L, fnameindex); + return LUA_ERRFILE; +} + + +static int compat53_skipBOM(compat53_LoadF *lf) { + const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ + int c; + lf->n = 0; + do { + c = getc(lf->f); + if (c == EOF || c != *(const unsigned char *)p++) return c; + lf->buff[lf->n++] = (char)c; /* to be read by the parser */ + } while (*p != '\0'); + lf->n = 0; /* prefix matched; discard it */ + return getc(lf->f); /* return next character */ +} + + +/* +** reads the first character of file 'f' and skips an optional BOM mark +** in its beginning plus its first line if it starts with '#'. Returns +** true if it skipped the first line. In any case, '*cp' has the +** first "valid" character of the file (after the optional BOM and +** a first-line comment). +*/ +static int compat53_skipcomment(compat53_LoadF *lf, int *cp) { + int c = *cp = compat53_skipBOM(lf); + if (c == '#') { /* first line is a comment (Unix exec. file)? */ + do { /* skip first line */ + c = getc(lf->f); + } while (c != EOF && c != '\n'); + *cp = getc(lf->f); /* skip end-of-line, if present */ + return 1; /* there was a comment */ + } + else return 0; /* no comment */ +} + + +COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode) { + compat53_LoadF lf; + int status, readstatus; + int c; + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ + if (filename == NULL) { + lua_pushliteral(L, "=stdin"); + lf.f = stdin; + } + else { + lua_pushfstring(L, "@%s", filename); +#if defined(_MSC_VER) + /* This code is here to stop a deprecation error that stops builds + * if a certain macro is defined. While normally not caring would + * be best, some header-only libraries and builds can't afford to + * dictate this to the user. A quick check shows that fopen_s this + * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET, + * possibly even before that so we don't need to do any version + * number checks, since this has been there since forever. */ + + /* TO USER: if you want the behavior of typical fopen_s/fopen, + * which does lock the file on VC++, define the macro used below to 0 */ +#if COMPAT53_FOPEN_NO_LOCK + lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */ + if (lf.f == NULL) + return compat53_errfile(L, "open", fnameindex); +#else /* use default locking version */ + if (fopen_s(&lf.f, filename, "r") != 0) + return compat53_errfile(L, "open", fnameindex); +#endif /* Locking vs. No-locking fopen variants */ +#else + lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */ + if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex); +#endif + } + if (compat53_skipcomment(&lf, &c)) /* read initial portion */ + lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ + if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ +#if defined(_MSC_VER) + if (freopen_s(&lf.f, filename, "rb", lf.f) != 0) + return compat53_errfile(L, "reopen", fnameindex); +#else + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex); +#endif + compat53_skipcomment(&lf, &c); /* re-read initial portion */ + } + if (c != EOF) + lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */ + status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode); + readstatus = ferror(lf.f); + if (filename) fclose(lf.f); /* close file (even in case of errors) */ + if (readstatus) { + lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ + return compat53_errfile(L, "read", fnameindex); + } + lua_remove(L, fnameindex); + return status; +} + + +COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) { + int status = LUA_OK; + if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) { + status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); + } + else { + status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX); + } + if (status != LUA_OK) + return status; + return luaL_loadbuffer(L, buff, sz, name); +} + + +#if !defined(l_inspectstat) && \ + (defined(unix) || defined(__unix) || defined(__unix__) || \ + defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \ + (defined(__APPLE__) && defined(__MACH__))) +/* some form of unix; check feature macros in unistd.h for details */ +# include +/* check posix version; the relevant include files and macros probably +* were available before 2001, but I'm not sure */ +# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L +# include +# define l_inspectstat(stat,what) \ + if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ + else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } +# endif +#endif + +/* provide default (no-op) version */ +#if !defined(l_inspectstat) +# define l_inspectstat(stat,what) ((void)0) +#endif + + +COMPAT53_API int luaL_execresult(lua_State *L, int stat) { + const char *what = "exit"; + if (stat == -1) + return luaL_fileresult(L, 0, NULL); + else { + l_inspectstat(stat, what); + if (*what == 'e' && stat == 0) + lua_pushboolean(L, 1); + else + lua_pushnil(L); + lua_pushstring(L, what); + lua_pushinteger(L, stat); + return 3; + } +} + + +COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B) { + /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ + B->b.p = NULL; + B->b.L = NULL; + B->b.lvl = 0; + /* reuse the buffer from the 5.1-style luaL_Buffer though! */ + B->ptr = B->b.buffer; + B->capacity = LUAL_BUFFERSIZE; + B->nelems = 0; + B->L2 = L; +} + + +COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s) { + if (B->capacity - B->nelems < s) { /* needs to grow */ + char* newptr = NULL; + size_t newcap = B->capacity * 2; + if (newcap - B->nelems < s) + newcap = B->nelems + s; + if (newcap < B->capacity) /* overflow */ + luaL_error(B->L2, "buffer too large"); + newptr = (char*)lua_newuserdata(B->L2, newcap); + memcpy(newptr, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove old buffer */ + B->ptr = newptr; + B->capacity = newcap; + } + return B->ptr + B->nelems; +} + + +COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l) { + memcpy(luaL_prepbuffsize(B, l), s, l); + luaL_addsize(B, l); +} + + +COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B) { + size_t len = 0; + const char *s = lua_tolstring(B->L2, -1, &len); + if (!s) + luaL_error(B->L2, "cannot convert value to string"); + if (B->ptr != B->b.buffer) + lua_insert(B->L2, -2); /* userdata buffer must be at stack top */ + luaL_addlstring(B, s, len); + lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1); +} + + +void luaL_pushresult(luaL_Buffer_53 *B) { + lua_pushlstring(B->L2, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove userdata buffer */ +} + + +#endif /* Lua 5.1 */ + + + +/* definitions for Lua 5.1 and Lua 5.2 */ +#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502 + + +COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i) { + index = lua_absindex(L, index); + lua_pushinteger(L, i); + lua_gettable(L, index); + return lua_type(L, -1); +} + + +COMPAT53_API int lua_isinteger(lua_State *L, int index) { + if (lua_type(L, index) == LUA_TNUMBER) { + lua_Number n = lua_tonumber(L, index); + lua_Integer i = lua_tointeger(L, index); + if (i == n) + return 1; + } + return 0; +} + + +COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum) { + int ok = 0; + lua_Number n = lua_tonumberx(L, i, &ok); + if (ok) { + if (n == (lua_Integer)n) { + if (isnum) + *isnum = 1; + return (lua_Integer)n; + } + } + if (isnum) + *isnum = 0; + return 0; +} + + +static void compat53_reverse(lua_State *L, int a, int b) { + for (; a < b; ++a, --b) { + lua_pushvalue(L, a); + lua_pushvalue(L, b); + lua_replace(L, a); + lua_replace(L, b); + } +} + + +COMPAT53_API void lua_rotate(lua_State *L, int idx, int n) { + int n_elems = 0; + idx = lua_absindex(L, idx); + n_elems = lua_gettop(L) - idx + 1; + if (n < 0) + n += n_elems; + if (n > 0 && n < n_elems) { + luaL_checkstack(L, 2, "not enough stack slots available"); + n = n_elems - n; + compat53_reverse(L, idx, idx + n - 1); + compat53_reverse(L, idx + n, idx + n_elems - 1); + compat53_reverse(L, idx, idx + n_elems - 1); + } +} + + +COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i) { + luaL_checkstack(L, 1, "not enough stack slots available"); + index = lua_absindex(L, index); + lua_pushinteger(L, i); + lua_insert(L, -2); + lua_settable(L, index); +} + + +#if !defined(lua_str2number) +# define lua_str2number(s, p) strtod((s), (p)) +#endif + +COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s) { + char* endptr; + lua_Number n = lua_str2number(s, &endptr); + if (endptr != s) { + while (*endptr != '\0' && isspace((unsigned char)*endptr)) + ++endptr; + if (*endptr == '\0') { + lua_pushnumber(L, n); + return endptr - s + 1; + } + } + return 0; +} + + +COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len) { + if (!luaL_callmeta(L, idx, "__tostring")) { + int t = lua_type(L, idx), tt = 0; + char const* name = NULL; + switch (t) { + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + case LUA_TSTRING: + case LUA_TNUMBER: + lua_pushvalue(L, idx); + break; + case LUA_TBOOLEAN: + if (lua_toboolean(L, idx)) + lua_pushliteral(L, "true"); + else + lua_pushliteral(L, "false"); + break; + default: + tt = luaL_getmetafield(L, idx, "__name"); + name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t); + lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx)); + if (tt != LUA_TNIL) + lua_replace(L, -2); + break; + } + } + else { + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); + } + return lua_tolstring(L, -1, len); +} + + +COMPAT53_API void luaL_requiref(lua_State *L, const char *modname, + lua_CFunction openf, int glb) { + luaL_checkstack(L, 3, "not enough stack slots available"); + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + if (lua_getfield(L, -1, modname) == LUA_TNIL) { + lua_pop(L, 1); + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); + lua_call(L, 1, 1); + lua_pushvalue(L, -1); + lua_setfield(L, -3, modname); + } + if (glb) { + lua_pushvalue(L, -1); + lua_setglobal(L, modname); + } + lua_replace(L, -2); +} + + +#endif /* Lua 5.1 and 5.2 */ + + +#endif /* KEPLER_PROJECT_COMPAT53_C_ */ + + +/********************************************************************* +* This file contains parts of Lua 5.2's and Lua 5.3's source code: +* +* Copyright (C) 1994-2014 Lua.org, PUC-Rio. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*********************************************************************/ + diff --git a/sol/compatibility/compat-5.3.h b/sol/compatibility/compat-5.3.h index 755c23ed..665f81e5 100644 --- a/sol/compatibility/compat-5.3.h +++ b/sol/compatibility/compat-5.3.h @@ -1,420 +1,421 @@ -#ifndef COMPAT53_H_ -#define COMPAT53_H_ - -#include -#include -#include -#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) -extern "C" { -#endif -#include -#include -#include -#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) -} -#endif - - -#undef COMPAT53_INCLUDE_SOURCE -#if defined(COMPAT53_PREFIX) -/* - change the symbol names of functions to avoid linker conflicts - * - compat-5.3.c needs to be compiled (and linked) separately - */ -# if !defined(COMPAT53_API) -# define COMPAT53_API extern -# endif -#else /* COMPAT53_PREFIX */ -/* - make all functions static and include the source. - * - compat-5.3.c doesn't need to be compiled (and linked) separately - */ -# define COMPAT53_PREFIX compat53 -# undef COMPAT53_API -# if defined(__GNUC__) || defined(__clang__) -# define COMPAT53_API __attribute__((__unused__)) static -# else -# define COMPAT53_API static -# endif -# define COMPAT53_INCLUDE_SOURCE -#endif /* COMPAT53_PREFIX */ - -#define COMPAT53_CONCAT_HELPER(a, b) a##b -#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) - - - -/* declarations for Lua 5.1 */ -#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 - -/* XXX not implemented: - * lua_arith (new operators) - * lua_upvalueid - * lua_upvaluejoin - * lua_version - * lua_yieldk - */ - -#ifndef LUA_OK -# define LUA_OK 0 -#endif -#ifndef LUA_OPADD -# define LUA_OPADD 0 -#endif -#ifndef LUA_OPSUB -# define LUA_OPSUB 1 -#endif -#ifndef LUA_OPMUL -# define LUA_OPMUL 2 -#endif -#ifndef LUA_OPDIV -# define LUA_OPDIV 3 -#endif -#ifndef LUA_OPMOD -# define LUA_OPMOD 4 -#endif -#ifndef LUA_OPPOW -# define LUA_OPPOW 5 -#endif -#ifndef LUA_OPUNM -# define LUA_OPUNM 6 -#endif -#ifndef LUA_OPEQ -# define LUA_OPEQ 0 -#endif -#ifndef LUA_OPLT -# define LUA_OPLT 1 -#endif -#ifndef LUA_OPLE -# define LUA_OPLE 2 -#endif - -/* LuaJIT/Lua 5.1 does not have the updated - * error codes for thread status/function returns (but some patched versions do) - * define it only if it's not found - */ -#if !defined(LUA_ERRGCMM) -/* Use + 2 because in some versions of Lua (Lua 5.1) - * LUA_ERRFILE is defined as (LUA_ERRERR+1) - * so we need to avoid it (LuaJIT might have something at this - * integer value too) - */ -# define LUA_ERRGCMM (LUA_ERRERR + 2) -#endif /* LUA_ERRGCMM define */ - -typedef size_t lua_Unsigned; - -typedef struct luaL_Buffer_53 { - luaL_Buffer b; /* make incorrect code crash! */ - char *ptr; - size_t nelems; - size_t capacity; - lua_State *L2; -} luaL_Buffer_53; -#define luaL_Buffer luaL_Buffer_53 - -/* In PUC-Rio 5.1, userdata is a simple FILE* - * In LuaJIT, it's a struct where the first member is a FILE* - * We can't support the `closef` member - */ -typedef struct luaL_Stream { - FILE *f; -} luaL_Stream; - -#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) -COMPAT53_API int lua_absindex (lua_State *L, int i); - -#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith) -COMPAT53_API void lua_arith (lua_State *L, int op); - -#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare) -COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op); - -#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) -COMPAT53_API void lua_copy (lua_State *L, int from, int to); - -#define lua_getuservalue(L, i) \ - (lua_getfenv((L), (i)), lua_type((L), -1)) -#define lua_setuservalue(L, i) \ - (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i))) - -#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len) -COMPAT53_API void lua_len (lua_State *L, int i); - -#define lua_pushstring(L, s) \ - (lua_pushstring((L), (s)), lua_tostring((L), -1)) - -#define lua_pushlstring(L, s, len) \ - ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1)) - -#ifndef luaL_newlibtable -# define luaL_newlibtable(L, l) \ - (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1)) -#endif -#ifndef luaL_newlib -# define luaL_newlib(L, l) \ - (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l))) -#endif - -#define lua_pushglobaltable(L) \ - lua_pushvalue((L), LUA_GLOBALSINDEX) - -#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp) -COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p); - -#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp) -COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p); - -#define lua_rawlen(L, i) lua_objlen((L), (i)) - -#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx) -COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum); - -#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx) -COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum); - -#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) -COMPAT53_API void luaL_checkversion (lua_State *L); - -#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) -COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode); - -#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex) -COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode); - -#define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx) -COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); - -#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53) -COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg); - -#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable) -COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name); - -#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len) -COMPAT53_API lua_Integer luaL_len (lua_State *L, int i); - -#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs) -COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); - -#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable) -COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname); - -#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata) -COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname); - -#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback) -COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level); - -#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) -COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname); - -#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult) -COMPAT53_API int luaL_execresult (lua_State *L, int stat); - -#define lua_callk(L, na, nr, ctx, cont) \ - ((void)(ctx), (void)(cont), lua_call((L), (na), (nr))) -#define lua_pcallk(L, na, nr, err, ctx, cont) \ - ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err))) - -#define lua_resume(L, from, nargs) \ - ((void)(from), lua_resume((L), (nargs))) - -#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53) -COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B); - -#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53) -COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s); - -#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53) -COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l); - -#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53) -COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B); - -#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53) -COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B); - -#undef luaL_buffinitsize -#define luaL_buffinitsize(L, B, s) \ - (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s))) - -#undef luaL_prepbuffer -#define luaL_prepbuffer(B) \ - luaL_prepbuffsize((B), LUAL_BUFFERSIZE) - -#undef luaL_addchar -#define luaL_addchar(B, c) \ - ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \ - ((B)->ptr[(B)->nelems++] = (c))) - -#undef luaL_addsize -#define luaL_addsize(B, s) \ - ((B)->nelems += (s)) - -#undef luaL_addstring -#define luaL_addstring(B, s) \ - luaL_addlstring((B), (s), strlen((s))) - -#undef luaL_pushresultsize -#define luaL_pushresultsize(B, s) \ - (luaL_addsize((B), (s)), luaL_pushresult((B))) - -#if defined(LUA_COMPAT_APIINTCASTS) -#define lua_pushunsigned(L, n) \ - lua_pushinteger((L), (lua_Integer)(n)) -#define lua_tounsignedx(L, i, is) \ - ((lua_Unsigned)lua_tointegerx((L), (i), (is))) -#define lua_tounsigned(L, i) \ - lua_tounsignedx((L), (i), NULL) -#define luaL_checkunsigned(L, a) \ - ((lua_Unsigned)luaL_checkinteger((L), (a))) -#define luaL_optunsigned(L, a, d) \ - ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d))) -#endif - -#endif /* Lua 5.1 only */ - - - -/* declarations for Lua 5.1 and 5.2 */ -#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 - -typedef int lua_KContext; - -typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); - -#define lua_dump(L, w, d, s) \ - ((void)(s), lua_dump((L), (w), (d))) - -#define lua_getfield(L, i, k) \ - (lua_getfield((L), (i), (k)), lua_type((L), -1)) - -#define lua_gettable(L, i) \ - (lua_gettable((L), (i)), lua_type((L), -1)) - -#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) -COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); - -#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) -COMPAT53_API int lua_isinteger (lua_State *L, int index); - -#define lua_numbertointeger(n, p) \ - ((*(p) = (lua_Integer)(n)), 1) - -#define lua_rawget(L, i) \ - (lua_rawget((L), (i)), lua_type((L), -1)) - -#define lua_rawgeti(L, i, n) \ - (lua_rawgeti((L), (i), (n)), lua_type((L), -1)) - -#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate) -COMPAT53_API void lua_rotate (lua_State *L, int idx, int n); - -#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti) -COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i); - -#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber) -COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s); - -#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring) -COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len); - -#define luaL_getmetafield(L, o, e) \ - (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL) - -#define luaL_newmetatable(L, tn) \ - (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0) - -#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53) -COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, - lua_CFunction openf, int glb ); - -#endif /* Lua 5.1 and Lua 5.2 */ - - - -/* declarations for Lua 5.2 */ -#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502 - -/* XXX not implemented: - * lua_isyieldable - * lua_getextraspace - * lua_arith (new operators) - * lua_pushfstring (new formats) - */ - -#define lua_getglobal(L, n) \ - (lua_getglobal((L), (n)), lua_type((L), -1)) - -#define lua_getuservalue(L, i) \ - (lua_getuservalue((L), (i)), lua_type((L), -1)) - -#define lua_pushlstring(L, s, len) \ - (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))) - -#define lua_rawgetp(L, i, p) \ - (lua_rawgetp((L), (i), (p)), lua_type((L), -1)) - -#define LUA_KFUNCTION(_name) \ - static int (_name)(lua_State *L, int status, lua_KContext ctx); \ - static int (_name ## _52)(lua_State *L) { \ - lua_KContext ctx; \ - int status = lua_getctx(L, &ctx); \ - return (_name)(L, status, ctx); \ - } \ - static int (_name)(lua_State *L, int status, lua_KContext ctx) - -#define lua_pcallk(L, na, nr, err, ctx, cont) \ - lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52) - -#define lua_callk(L, na, nr, ctx, cont) \ - lua_callk((L), (na), (nr), (ctx), cont ## _52) - -#define lua_yieldk(L, nr, ctx, cont) \ - lua_yieldk((L), (nr), (ctx), cont ## _52) - -#ifdef lua_call -# undef lua_call -# define lua_call(L, na, nr) \ - (lua_callk)((L), (na), (nr), 0, NULL) -#endif - -#ifdef lua_pcall -# undef lua_pcall -# define lua_pcall(L, na, nr, err) \ - (lua_pcallk)((L), (na), (nr), (err), 0, NULL) -#endif - -#ifdef lua_yield -# undef lua_yield -# define lua_yield(L, nr) \ - (lua_yieldk)((L), (nr), 0, NULL) -#endif - -#endif /* Lua 5.2 only */ - - - -/* other Lua versions */ -#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503 - -# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)" - -#endif /* other Lua versions except 5.1, 5.2, and 5.3 */ - - - -/* helper macro for defining continuation functions (for every version - * *except* Lua 5.2) */ -#ifndef LUA_KFUNCTION -#define LUA_KFUNCTION(_name) \ - static int (_name)(lua_State *L, int status, lua_KContext ctx) -#endif - - -#if defined(COMPAT53_INCLUDE_SOURCE) -# include "compat-5.3.c" -#endif - - -#endif /* COMPAT53_H_ */ - +#ifndef KEPLER_PROJECT_COMPAT53_H_ +#define KEPLER_PROJECT_COMPAT53_H_ + +#include +#include +#include +#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) +extern "C" { +#endif +#include +#include +#include +#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) +} +#endif + +#ifndef COMPAT53_PREFIX +/* we chose this name because many other lua bindings / libs have +* their own compatibility layer, and that use the compat53 declaration +* frequently, causing all kinds of linker / compiler issues +*/ +# define COMPAT53_PREFIX kp_compat53 +#endif // COMPAT53_PREFIX + +#ifndef COMPAT53_API +# if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE +# if defined(__GNUC__) || defined(__clang__) +# define COMPAT53_API __attribute__((__unused__)) static +# else +# define COMPAT53_API static +# endif /* Clang/GCC */ +# else /* COMPAT53_INCLUDE_SOURCE */ +/* we are not including source, so everything is extern */ +# define COMPAT53_API extern +# endif /* COMPAT53_INCLUDE_SOURCE */ +#endif /* COMPAT53_PREFIX */ + + +#define COMPAT53_CONCAT_HELPER(a, b) a##b +#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) + + + +/* declarations for Lua 5.1 */ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 + +/* XXX not implemented: +* lua_arith (new operators) +* lua_upvalueid +* lua_upvaluejoin +* lua_version +* lua_yieldk +*/ + +#ifndef LUA_OK +# define LUA_OK 0 +#endif +#ifndef LUA_OPADD +# define LUA_OPADD 0 +#endif +#ifndef LUA_OPSUB +# define LUA_OPSUB 1 +#endif +#ifndef LUA_OPMUL +# define LUA_OPMUL 2 +#endif +#ifndef LUA_OPDIV +# define LUA_OPDIV 3 +#endif +#ifndef LUA_OPMOD +# define LUA_OPMOD 4 +#endif +#ifndef LUA_OPPOW +# define LUA_OPPOW 5 +#endif +#ifndef LUA_OPUNM +# define LUA_OPUNM 6 +#endif +#ifndef LUA_OPEQ +# define LUA_OPEQ 0 +#endif +#ifndef LUA_OPLT +# define LUA_OPLT 1 +#endif +#ifndef LUA_OPLE +# define LUA_OPLE 2 +#endif + +/* LuaJIT/Lua 5.1 does not have the updated +* error codes for thread status/function returns (but some patched versions do) +* define it only if it's not found +*/ +#if !defined(LUA_ERRGCMM) +/* Use + 2 because in some versions of Lua (Lua 5.1) +* LUA_ERRFILE is defined as (LUA_ERRERR+1) +* so we need to avoid it (LuaJIT might have something at this +* integer value too) +*/ +# define LUA_ERRGCMM (LUA_ERRERR + 2) +#endif /* LUA_ERRGCMM define */ + +typedef size_t lua_Unsigned; + +typedef struct luaL_Buffer_53 { + luaL_Buffer b; /* make incorrect code crash! */ + char *ptr; + size_t nelems; + size_t capacity; + lua_State *L2; +} luaL_Buffer_53; +#define luaL_Buffer luaL_Buffer_53 + +/* In PUC-Rio 5.1, userdata is a simple FILE* +* In LuaJIT, it's a struct where the first member is a FILE* +* We can't support the `closef` member +*/ +typedef struct luaL_Stream { + FILE *f; +} luaL_Stream; + +#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) +COMPAT53_API int lua_absindex(lua_State *L, int i); + +#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith) +COMPAT53_API void lua_arith(lua_State *L, int op); + +#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare) +COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op); + +#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) +COMPAT53_API void lua_copy(lua_State *L, int from, int to); + +#define lua_getuservalue(L, i) \ + (lua_getfenv((L), (i)), lua_type((L), -1)) +#define lua_setuservalue(L, i) \ + (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i))) + +#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len) +COMPAT53_API void lua_len(lua_State *L, int i); + +#define lua_pushstring(L, s) \ + (lua_pushstring((L), (s)), lua_tostring((L), -1)) + +#define lua_pushlstring(L, s, len) \ + ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1)) + +#ifndef luaL_newlibtable +# define luaL_newlibtable(L, l) \ + (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1)) +#endif +#ifndef luaL_newlib +# define luaL_newlib(L, l) \ + (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l))) +#endif + +#define lua_pushglobaltable(L) \ + lua_pushvalue((L), LUA_GLOBALSINDEX) + +#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp) +COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p); + +#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp) +COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p); + +#define lua_rawlen(L, i) lua_objlen((L), (i)) + +#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL) + +#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx) +COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum); + +#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) +COMPAT53_API void luaL_checkversion(lua_State *L); + +#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) +COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode); + +#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex) +COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode); + +#define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx) +COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); + +#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53) +COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg); + +#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable) +COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char *name); + +#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len) +COMPAT53_API lua_Integer luaL_len(lua_State *L, int i); + +#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs) +COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup); + +#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable) +COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname); + +#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata) +COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname); + +#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback) +COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level); + +#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) +COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname); + +#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult) +COMPAT53_API int luaL_execresult(lua_State *L, int stat); + +#define lua_callk(L, na, nr, ctx, cont) \ + ((void)(ctx), (void)(cont), lua_call((L), (na), (nr))) +#define lua_pcallk(L, na, nr, err, ctx, cont) \ + ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err))) + +#define lua_resume(L, from, nargs) \ + ((void)(from), lua_resume((L), (nargs))) + +#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53) +COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B); + +#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53) +COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s); + +#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53) +COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l); + +#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53) +COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B); + +#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53) +COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B); + +#undef luaL_buffinitsize +#define luaL_buffinitsize(L, B, s) \ + (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s))) + +#undef luaL_prepbuffer +#define luaL_prepbuffer(B) \ + luaL_prepbuffsize((B), LUAL_BUFFERSIZE) + +#undef luaL_addchar +#define luaL_addchar(B, c) \ + ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \ + ((B)->ptr[(B)->nelems++] = (c))) + +#undef luaL_addsize +#define luaL_addsize(B, s) \ + ((B)->nelems += (s)) + +#undef luaL_addstring +#define luaL_addstring(B, s) \ + luaL_addlstring((B), (s), strlen((s))) + +#undef luaL_pushresultsize +#define luaL_pushresultsize(B, s) \ + (luaL_addsize((B), (s)), luaL_pushresult((B))) + +#if defined(LUA_COMPAT_APIINTCASTS) +#define lua_pushunsigned(L, n) \ + lua_pushinteger((L), (lua_Integer)(n)) +#define lua_tounsignedx(L, i, is) \ + ((lua_Unsigned)lua_tointegerx((L), (i), (is))) +#define lua_tounsigned(L, i) \ + lua_tounsignedx((L), (i), NULL) +#define luaL_checkunsigned(L, a) \ + ((lua_Unsigned)luaL_checkinteger((L), (a))) +#define luaL_optunsigned(L, a, d) \ + ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d))) +#endif + +#endif /* Lua 5.1 only */ + + + +/* declarations for Lua 5.1 and 5.2 */ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 + +typedef int lua_KContext; + +typedef int(*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); + +#define lua_dump(L, w, d, s) \ + ((void)(s), lua_dump((L), (w), (d))) + +#define lua_getfield(L, i, k) \ + (lua_getfield((L), (i), (k)), lua_type((L), -1)) + +#define lua_gettable(L, i) \ + (lua_gettable((L), (i)), lua_type((L), -1)) + +#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) +COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i); + +#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) +COMPAT53_API int lua_isinteger(lua_State *L, int index); + +#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53) +COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum); + +#define lua_numbertointeger(n, p) \ + ((*(p) = (lua_Integer)(n)), 1) + +#define lua_rawget(L, i) \ + (lua_rawget((L), (i)), lua_type((L), -1)) + +#define lua_rawgeti(L, i, n) \ + (lua_rawgeti((L), (i), (n)), lua_type((L), -1)) + +#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate) +COMPAT53_API void lua_rotate(lua_State *L, int idx, int n); + +#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti) +COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i); + +#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber) +COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s); + +#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring) +COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len); + +#define luaL_getmetafield(L, o, e) \ + (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL) + +#define luaL_newmetatable(L, tn) \ + (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0) + +#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53) +COMPAT53_API void luaL_requiref(lua_State *L, const char *modname, + lua_CFunction openf, int glb); + +#endif /* Lua 5.1 and Lua 5.2 */ + + + +/* declarations for Lua 5.2 */ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502 + +/* XXX not implemented: +* lua_isyieldable +* lua_getextraspace +* lua_arith (new operators) +* lua_pushfstring (new formats) +*/ + +#define lua_getglobal(L, n) \ + (lua_getglobal((L), (n)), lua_type((L), -1)) + +#define lua_getuservalue(L, i) \ + (lua_getuservalue((L), (i)), lua_type((L), -1)) + +#define lua_pushlstring(L, s, len) \ + (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))) + +#define lua_rawgetp(L, i, p) \ + (lua_rawgetp((L), (i), (p)), lua_type((L), -1)) + +#define LUA_KFUNCTION(_name) \ + static int (_name)(lua_State *L, int status, lua_KContext ctx); \ + static int (_name ## _52)(lua_State *L) { \ + lua_KContext ctx; \ + int status = lua_getctx(L, &ctx); \ + return (_name)(L, status, ctx); \ + } \ + static int (_name)(lua_State *L, int status, lua_KContext ctx) + +#define lua_pcallk(L, na, nr, err, ctx, cont) \ + lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52) + +#define lua_callk(L, na, nr, ctx, cont) \ + lua_callk((L), (na), (nr), (ctx), cont ## _52) + +#define lua_yieldk(L, nr, ctx, cont) \ + lua_yieldk((L), (nr), (ctx), cont ## _52) + +#ifdef lua_call +# undef lua_call +# define lua_call(L, na, nr) \ + (lua_callk)((L), (na), (nr), 0, NULL) +#endif + +#ifdef lua_pcall +# undef lua_pcall +# define lua_pcall(L, na, nr, err) \ + (lua_pcallk)((L), (na), (nr), (err), 0, NULL) +#endif + +#ifdef lua_yield +# undef lua_yield +# define lua_yield(L, nr) \ + (lua_yieldk)((L), (nr), 0, NULL) +#endif + +#endif /* Lua 5.2 only */ + + + +/* other Lua versions */ +#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503 + +# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)" + +#endif /* other Lua versions except 5.1, 5.2, and 5.3 */ + + + +/* helper macro for defining continuation functions (for every version +* *except* Lua 5.2) */ +#ifndef LUA_KFUNCTION +#define LUA_KFUNCTION(_name) \ + static int (_name)(lua_State *L, int status, lua_KContext ctx) +#endif + + +#if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1 +# include "compat-5.3.c" +#endif + + +#endif /* KEPLER_PROJECT_COMPAT53_H_ */ + diff --git a/sol/compatibility/version.hpp b/sol/compatibility/version.hpp index 2e4e3ed4..c7cd1caa 100644 --- a/sol/compatibility/version.hpp +++ b/sol/compatibility/version.hpp @@ -26,15 +26,15 @@ #include "../feature_test.hpp" -#if defined(SOL_USING_CXX_LUA) +#if defined(SOL_USING_CXX_LUA) && SOL_USING_CXX_LUA #include #include #include -#ifdef SOL_USING_CXX_LUAJIT +#if defined(SOL_USING_CXX_LUAJIT) && SOL_USING_CXX_LUAJIT #include #endif // C++ LuaJIT ... whatever that means -#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) && !defined(SOL_EXCEPTIONS_ALWAYS_UNSAFE) -#define SOL_EXCEPTIONS_SAFE_PROPAGATION +#if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !(SOL_EXCEPTIONS_SAFE_PROPAGATION)) && (!defined(SOL_EXCEPTIONS_ALWAYS_UNSAFE) || !(SOL_EXCEPTIONS_ALWAYS_UNSAFE)) +#define SOL_EXCEPTIONS_SAFE_PROPAGATION 1 #endif // Exceptions can be propagated safely using C++-compiled Lua #else #include @@ -51,7 +51,7 @@ #define SOL_LUA_VERSION LUA_VERSION_NUM #elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 #define SOL_LUA_VERSION LUA_VERSION_NUM -#elif !defined(LUA_VERSION_NUM) +#elif !defined(LUA_VERSION_NUM) || !(LUA_VERSION_NUM) // Definitely 5.0 #define SOL_LUA_VERSION 500 #else diff --git a/sol/config.hpp b/sol/config.hpp new file mode 100644 index 00000000..4a30822c --- /dev/null +++ b/sol/config.hpp @@ -0,0 +1,220 @@ +// sol2 + +// The MIT License (MIT) + +// Copyright (c) 2013-2018 Rapptz, ThePhD and contributors + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SOL_CONFIG_HPP +#define SOL_CONFIG_HPP + +#ifdef _MSC_VER + #if defined(_DEBUG) && !defined(NDEBUG) + + #ifndef SOL_IN_DEBUG_DETECTED + #define SOL_IN_DEBUG_DETECTED 1 + #endif + + #endif // VC++ Debug macros + + #ifndef _CPPUNWIND + #ifndef SOL_NO_EXCEPTIONS + #define SOL_NO_EXCEPTIONS 1 + #endif + #endif // Automatic Exceptions + + #ifndef _CPPRTTI + #ifndef SOL_NO_RTTI + #define SOL_NO_RTTI 1 + #endif + #endif // Automatic RTTI +#elif defined(__GNUC__) || defined(__clang__) + + #if !defined(NDEBUG) && !defined(__OPTIMIZE__) + + #ifndef SOL_IN_DEBUG_DETECTED + #define SOL_IN_DEBUG_DETECTED 1 + #endif + + #endif // Not Debug && g++ optimizer flag + + #ifndef __EXCEPTIONS + #ifndef SOL_NO_EXCEPTIONS + #define SOL_NO_EXCEPTIONS 1 + #endif + #endif // No Exceptions + + #ifndef __GXX_RTTI + #ifndef SOL_NO_RTII + #define SOL_NO_RTTI 1 + #endif + #endif // No RTTI + +#endif // vc++ || clang++/g++ + +// If this is defined, turn on all the safety checks automatically +#if defined(SOL_CHECK_ARGUMENTS) && SOL_CHECK_ARGUMENTS + + // Checks low-level getter function + // (and thusly, affects nearly entire framework) + #if !defined(SOL_SAFE_GETTER) + #define SOL_SAFE_GETTER 1 + #endif + + // Checks access on usertype functions + // local my_obj = my_type.new() + // my_obj.my_member_function() + // -- bad syntax and crash + #if !defined(SOL_SAFE_USERTYPE) + #define SOL_SAFE_USERTYPE 1 + #endif + + // Checks sol::reference derived boundaries + // sol::function ref(L, 1); + // sol::userdata sref(L, 2); + #if !defined(SOL_SAFE_REFERENCES) + #define SOL_SAFE_REFERENCES 1 + #endif + + // Changes all typedefs of sol::function to point to the + // protected_function version, instead of unsafe_function + #if !defined(SOL_SAFE_FUNCTION) + #define SOL_SAFE_FUNCTION 1 + #endif + + // Checks function parameters and + // returns upon call into/from Lua + // local a = 1 + // local b = "woof" + // my_c_function(a, b) + #if !defined(SOL_SAFE_FUNCTION_CALLS) + #define SOL_SAFE_FUNCTION_CALLS 1 + #endif + + // Checks conversions + // int v = lua["bark"]; + // int v2 = my_sol_function(); + #if !defined(SOL_SAFE_PROXIES) + #define SOL_SAFE_PROXIES 1 + #endif + + // Check overflowing number conversions + // for things like 64 bit integers that don't fit in a typical lua_Number + // for Lua 5.1 and 5.2 + #if !defined(SOL_SAFE_NUMERICS) + #define SOL_SAFE_NUMERICS 1 + #endif + + // Turn off Number Precision Checks + // if this is defined, we do not do range + // checks on integers / unsigned integers that might + // be bigger than what Lua can represent + #if !defined(SOL_NO_CHECK_NUMBER_PRECISION) + // off by default + #define SOL_NO_CHECK_NUMBER_PRECISION 0 + #endif + +#endif // Turn on Safety for all if top-level macro is defined + +// If we are in debug, turn on certain safety checks +#if defined(SOL_IN_DEBUG_DETECTED) && SOL_IN_DEBUG_DETECTED + + #if !defined(SOL_SAFE_REFERENCES) + // Ensure that references are forcefully type-checked upon construction + #define SOL_SAFE_REFERENCES 1 + #endif + + // Safe usertypes checks for errors such as + // obj = my_type.new() + // obj.f() -- note the '.' instead of ':' + // usertypes should be safe no matter what + #if !defined(SOL_SAFE_USERTYPE) + #define SOL_SAFE_USERTYPE 1 + #endif + + #if !defined(SOL_SAFE_FUNCTION_CALLS) + // Function calls from Lua should be automatically safe in debug mode + #define SOL_SAFE_FUNCTION_CALLS 1 + #endif + + // Print any exceptions / errors that occur + // in debug mode to the default error stream / console + #if !defined(SOL_PRINT_ERRORS) + #define SOL_PRINT_ERRORS 1 + #endif + +#endif // DEBUG: Turn on all debug safety features for VC++ / g++ / clang++ and similar + +// Print any exceptions / errors that occur +// This is normally off due to relying on +// to get std::cerr / std::cout +// so it is only defined in debug modes +#if !defined(SOL_PRINT_ERRORS) +// off by default here +#define SOL_PRINT_ERRORS 0 +#endif + +// The default on_error handler should not throw/assert/print/abort, +// but simply pass the value through back to the user +// problematic due to not having a stable [[nodiscard]] attribute in C++11, +// off by default +#if !defined(SOL_DEFAULT_PASS_ON_ERROR) +// off by default here +#define SOL_DEFAULT_PASS_ON_ERROR 0 +#endif + +// Interop allows userdata from external systems +// with external memory layout and metatable names +// to be registered. It costs something to perform +// the checker / differentiation for sol2 usertypes versus +// external ones however, so this is off by default +#if !defined(SOL_ENABLE_INTEROP) +// off by default here +#define SOL_ENABLE_INTEROP 0 +#endif + +// Mac OSX and Objective C define a nil keyword +// we cannot use that for the sol::type::nil define +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) || defined(__OBJC__) || defined(nil) +#if !defined(SOL_NO_NIL) +#define SOL_NO_NIL 1 +#endif +#endif // avoiding nil defines / keywords + +// If we are using boost, there are a number of optimizations we can perform +// Boost has unordered_map with Compatible Key and CompatibleHash +// Boost also has a decent optional, and we check for this later +// and define our optional to use boost in that scenario +#if defined(SOL_USE_BOOST) && SOL_USE_BOOST +#ifndef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#define SOL_UNORDERED_MAP_COMPATIBLE_HASH 1 +#endif // SOL_UNORDERED_MAP_COMPATIBLE_HASH +#endif + +// For strings being serialized and +// deserialized from and to utf8 +// this controls the size of the buffer we create on the stack, +// in # of utf8 code units +// a buffer of 1KB covers almost all the strings +// we care about: anything larger and the user should know better +#ifndef SOL_STACK_STRING_OPTIMIZATION_SIZE +#define SOL_STACK_STRING_OPTIMIZATION_SIZE 1024 +#endif // Optimized conversion routines using a KB or so off the stack + +#endif // SOL_CONFIG_HPP diff --git a/sol/container_traits.hpp b/sol/container_traits.hpp index 80fefa8f..1d3fc4d3 100644 --- a/sol/container_traits.hpp +++ b/sol/container_traits.hpp @@ -596,7 +596,7 @@ namespace sol { }; static auto& get_src(lua_State* L) { -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto p = stack::check_get(L, 1); if (!p) { luaL_error(L, "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)", detail::demangle().c_str()); @@ -1313,7 +1313,7 @@ namespace sol { static auto& get_src(lua_State* L) { auto p = stack::check_get(L, 1); -#ifdef SOL_SAFE_USERTYPE +#if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE if (!p) { luaL_error(L, "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)", detail::demangle().c_str()); } @@ -1438,7 +1438,11 @@ namespace sol { } static std::ptrdiff_t index_adjustment(lua_State*, T&) { +#if defined(SOL_CONTAINERS_START_INDEX) + return (SOL_CONTAINERS_START) == 0 ? 0 : -(SOL_CONTAINERS_START); +#else return -1; +#endif } static iterator begin(lua_State*, T& self) { diff --git a/sol/container_usertype_metatable.hpp b/sol/container_usertype_metatable.hpp index b1131cd0..35c22927 100644 --- a/sol/container_usertype_metatable.hpp +++ b/sol/container_usertype_metatable.hpp @@ -63,7 +63,7 @@ namespace sol { auto maybenameview = stack::check_get(L, 2); if (maybenameview) { const string_view& nameview = *maybenameview; -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH auto it = calls.find(nameview, string_view_hash(), std::equal_to()); #else std::string name(nameview.data(), nameview.size()); diff --git a/sol/coroutine.hpp b/sol/coroutine.hpp index 512b62ed..2ecf1fac 100644 --- a/sol/coroutine.hpp +++ b/sol/coroutine.hpp @@ -85,7 +85,7 @@ namespace sol { template , basic_coroutine>>, meta::neg>>, meta::neg>, meta::neg>>, is_lua_reference>> = meta::enabler> basic_coroutine(T&& r) noexcept : base_t(std::forward(r)), error_handler(detail::get_default_handler::value>(r.lua_state())) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_function>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; @@ -142,7 +142,7 @@ namespace sol { template >> = meta::enabler> basic_coroutine(lua_State* L, T&& r, handler_t eh) : base_t(L, std::forward(r)), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -168,7 +168,7 @@ namespace sol { } basic_coroutine(lua_State* L, absolute_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety @@ -178,7 +178,7 @@ namespace sol { } basic_coroutine(lua_State* L, raw_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety @@ -188,7 +188,7 @@ namespace sol { } basic_coroutine(lua_State* L, ref_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); diff --git a/sol/environment.hpp b/sol/environment.hpp index 852f327b..dabb2114 100644 --- a/sol/environment.hpp +++ b/sol/environment.hpp @@ -62,7 +62,7 @@ namespace sol { basic_environment(env_t, const stack_reference& extraction_target) : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(this->lua_state(), -1, handler); #endif // Safety @@ -71,7 +71,7 @@ namespace sol { template basic_environment(env_t, const basic_reference& extraction_target) : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(this->lua_state(), -1, handler); #endif // Safety @@ -79,14 +79,14 @@ namespace sol { } basic_environment(lua_State* L, int index = -1) : base_t(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_environment(lua_State* L, ref_index index) : base_t(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(L, -1, handler); @@ -95,7 +95,7 @@ namespace sol { template , basic_environment>>, meta::neg>, meta::neg>>, is_lua_reference>> = meta::enabler> basic_environment(T&& r) noexcept : base_t(detail::no_safety, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_environment>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; @@ -110,7 +110,7 @@ namespace sol { template >> = meta::enabler> basic_environment(lua_State* L, T&& r) noexcept : base_t(detail::no_safety, L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_environment>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; diff --git a/sol/error.hpp b/sol/error.hpp index db571c94..ba1826f1 100644 --- a/sol/error.hpp +++ b/sol/error.hpp @@ -35,7 +35,7 @@ namespace sol { class error : public std::runtime_error { private: - // Because VC++ is a fuccboi + // Because VC++ is upsetting, most of the time! std::string w; public: diff --git a/sol/feature_test.hpp b/sol/feature_test.hpp index a5f19acc..99507c41 100644 --- a/sol/feature_test.hpp +++ b/sol/feature_test.hpp @@ -30,7 +30,7 @@ #endif // C++17 features macro #endif // C++17 features check -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES // TODO: there is a bug in the VC++ compiler?? // on /std:c++latest under x86 conditions (VS 15.5.2), // compiler errors are tossed for noexcept markings being on function types @@ -51,138 +51,6 @@ #endif // Clang screws up variant #endif // C++17 only -#ifdef _MSC_VER -#if defined(_DEBUG) && !defined(NDEBUG) - -#ifndef SOL_IN_DEBUG_DETECTED -#define SOL_IN_DEBUG_DETECTED 1 -#endif - -#endif // VC++ Debug macros - -#ifndef _CPPUNWIND -#ifndef SOL_NO_EXCEPTIONS -#define SOL_NO_EXCEPTIONS 1 -#endif -#endif // Automatic Exceptions - -#ifndef _CPPRTTI -#ifndef SOL_NO_RTTI -#define SOL_NO_RTTI 1 -#endif -#endif // Automatic RTTI -#elif defined(__GNUC__) || defined(__clang__) - -#if !defined(NDEBUG) && !defined(__OPTIMIZE__) - -#ifndef SOL_IN_DEBUG_DETECTED -#define SOL_IN_DEBUG_DETECTED 1 -#endif - -#endif // Not Debug && g++ optimizer flag - -#ifndef __EXCEPTIONS -#ifndef SOL_NO_EXCEPTIONS -#define SOL_NO_EXCEPTIONS 1 -#endif -#endif // No Exceptions - -#ifndef __GXX_RTTI -#ifndef SOL_NO_RTII -#define SOL_NO_RTTI 1 -#endif -#endif // No RTTI - -#endif // vc++ || clang++/g++ - -#if defined(SOL_CHECK_ARGUMENTS) -// turn things on automatically - -// Checks low-level getter function -// (and thusly, affects nearly entire framework) -#if !defined(SOL_SAFE_GETTER) -#define SOL_SAFE_GETTER 1 -#endif - -// Checks access on usertype functions -// local my_obj = my_type.new() -// my_obj.my_member_function() -// -- bad syntax and crash -#if !defined(SOL_SAFE_USERTYPE) -#define SOL_SAFE_USERTYPE 1 -#endif - -// Checks sol::reference derived boundaries -// sol::function ref(L, 1); -// sol::userdata sref(L, 2); -#if !defined(SOL_SAFE_REFERENCES) -#define SOL_SAFE_REFERENCES 1 -#endif - -// Changes all typedefs of sol::function to point to the -// protected_function version, instead of unsafe_function -#if !defined(SOL_SAFE_FUNCTION) -#define SOL_SAFE_FUNCTION 1 -#endif - -// Checks function parameters and -// returns upon call into/from Lua -// local a = 1 -// local b = "woof" -// my_c_function(a, b) -#if !defined(SOL_SAFE_FUNCTION_CALLS) -#define SOL_SAFE_FUNCTION_CALLS 1 -#endif - -// Checks conversions -// int v = lua["bark"]; -// int v2 = my_sol_function(); -#if !defined(SOL_SAFE_PROXIES) -#define SOL_SAFE_PROXIES 1 -#endif - -// Check overflowing number conversions -// for things like 64 bit integers that don't fit in a typical lua_Number -// for Lua 5.1 and 5.2 -#if !defined(SOL_SAFE_NUMERICS) -#define SOL_SAFE_NUMERICS 1 -#endif - -#endif // Turn on Safety for all if top-level macro is defined - -#ifdef SOL_IN_DEBUG_DETECTED - -#if !defined(SOL_SAFE_REFERENCES) -// Ensure that references are forcefully type-checked upon construction -#define SOL_SAFE_REFERENCES 1 -#endif - -#if !defined(SOL_SAFE_USERTYPE) -// Usertypes should be safe no matter what -#define SOL_SAFE_USERTYPE 1 -#endif - -#if !defined(SOL_SAFE_FUNCTION_CALLS) -// Function calls from Lua should be automatically safe in debug mode -#define SOL_SAFE_FUNCTION_CALLS 1 -#endif - -#endif // Turn on all debug safety features for VC++ / g++ / clang++ and similar - -#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) || defined(__OBJC__) || defined(nil) -#if !defined(SOL_NO_NIL) -#define SOL_NO_NIL 1 -#endif -#endif // avoiding nil defines / keywords - -#ifdef SOL_USE_BOOST -#ifndef SOL_UNORDERED_MAP_COMPATIBLE_HASH -#define SOL_UNORDERED_MAP_COMPATIBLE_HASH -#endif // SOL_UNORDERED_MAP_COMPATIBLE_HASH -#endif // Boost has unordered_map with Compatible Key and CompatibleHash - -#ifndef SOL_STACK_STRING_OPTIMIZATION_SIZE -#define SOL_STACK_STRING_OPTIMIZATION_SIZE 1024 -#endif // Optimized conversion routines using a KB or so off the stack +#include #endif // SOL_FEATURE_TEST_HPP diff --git a/sol/forward.hpp b/sol/forward.hpp index 9efba669..ed14352c 100644 --- a/sol/forward.hpp +++ b/sol/forward.hpp @@ -81,7 +81,7 @@ namespace sol { using main_protected_function = main_safe_function; using stack_protected_function = stack_safe_function; using stack_aligned_protected_function = stack_aligned_safe_function; -#ifdef SOL_SAFE_FUNCTION +#if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION using function = protected_function; using main_function = main_protected_function; using stack_function = stack_protected_function; @@ -96,7 +96,7 @@ namespace sol { struct unsafe_function_result; struct protected_function_result; using safe_function_result = protected_function_result; -#ifdef SOL_SAFE_FUNCTION +#if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION using function_result = safe_function_result; #else using function_result = unsafe_function_result; diff --git a/sol/forward_detail.hpp b/sol/forward_detail.hpp index 63598892..b3bd6eb5 100644 --- a/sol/forward_detail.hpp +++ b/sol/forward_detail.hpp @@ -31,7 +31,7 @@ namespace sol { namespace detail { const bool default_safe_function_calls = -#ifdef SOL_SAFE_FUNCTION_CALLS +#if defined(SOL_SAFE_FUNCTION_CALLS) && SOL_SAFE_FUNCTION_CALLS true; #else false; diff --git a/sol/function_types.hpp b/sol/function_types.hpp index 1ee752df..d0c49e16 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -199,7 +199,7 @@ namespace sol { stack::push(L, f); } -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE template static void select_function(std::true_type, lua_State* L, detail::lua_CFunction_noexcept f) { // TODO: support yielding @@ -299,8 +299,8 @@ namespace sol { template struct pusher>, meta::neg>, meta::neg>> -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE - , +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE + , meta::neg>, meta::neg>> #endif // noexcept function types >::value>> { diff --git a/sol/function_types_templated.hpp b/sol/function_types_templated.hpp index 13b8eabe..004a6a70 100644 --- a/sol/function_types_templated.hpp +++ b/sol/function_types_templated.hpp @@ -120,7 +120,7 @@ namespace sol { inline int c_call(lua_State* L) { typedef meta::unqualified_t Fu; typedef std::integral_constant::value -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE || std::is_same::value #endif > is_raw; diff --git a/sol/in_place.hpp b/sol/in_place.hpp index eb306c1d..2fc0873f 100644 --- a/sol/in_place.hpp +++ b/sol/in_place.hpp @@ -29,7 +29,7 @@ namespace sol { -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES using in_place_t = std::in_place_t; constexpr std::in_place_t in_place{}; constexpr std::in_place_t in_place_of{}; diff --git a/sol/load_result.hpp b/sol/load_result.hpp index 4646c9e2..930c2e5d 100644 --- a/sol/load_result.hpp +++ b/sol/load_result.hpp @@ -48,7 +48,7 @@ namespace sol { template decltype(auto) tagged_get(types) const { -#ifdef SOL_SAFE_PROXIES +#if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES != 0 if (!valid()) { type_panic_c_str(L, index, type_of(L, index), type::none); } @@ -64,7 +64,7 @@ namespace sol { } error tagged_get(types) const { -#ifdef SOL_SAFE_PROXIES +#if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES != 0 if (valid()) { type_panic_c_str(L, index, type_of(L, index), type::none, "expecting an error type (a string, from Lua)"); } diff --git a/sol/optional.hpp b/sol/optional.hpp index 8ddfec02..c828d811 100644 --- a/sol/optional.hpp +++ b/sol/optional.hpp @@ -26,7 +26,7 @@ #include "compatibility.hpp" #include "in_place.hpp" -#if defined(SOL_USE_BOOST) +#if defined(SOL_USE_BOOST) && SOL_USE_BOOST #include #else #include "optional_implementation.hpp" @@ -34,7 +34,7 @@ namespace sol { -#if defined(SOL_USE_BOOST) +#if defined(SOL_USE_BOOST) && SOL_USE_BOOST template using optional = boost::optional; using nullopt_t = boost::none_t; diff --git a/sol/optional_implementation.hpp b/sol/optional_implementation.hpp index 8a8fd937..7737e61c 100644 --- a/sol/optional_implementation.hpp +++ b/sol/optional_implementation.hpp @@ -41,7 +41,7 @@ #include #include #include -#ifdef SOL_NO_EXCEPTIONS +#if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS #include #endif // Exceptions diff --git a/sol/protected_function.hpp b/sol/protected_function.hpp index 8b721db9..64ad4278 100644 --- a/sol/protected_function.hpp +++ b/sol/protected_function.hpp @@ -77,7 +77,7 @@ namespace sol { int firstreturn = 1; int returncount = 0; call_status code = call_status::ok; -#ifndef SOL_NO_EXCEPTIONS +#if !defined(SOL_NO_EXCEPTIONS) || !SOL_NO_EXCEPTIONS auto onexcept = [&](optional maybe_ex, const char* error) { h.stackindex = 0; if (b) { @@ -90,7 +90,7 @@ namespace sol { } }; (void)onexcept; -#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || defined(SOL_LUAJIT) +#if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT) try { #endif // Safe Exception Propagation #endif // No Exceptions @@ -99,7 +99,7 @@ namespace sol { poststacksize = lua_gettop(lua_state()) - static_cast(h.valid()); returncount = poststacksize - (firstreturn - 1); #ifndef SOL_NO_EXCEPTIONS -#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || defined(SOL_LUAJIT) +#if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT) } // Handle C++ errors thrown from C++ functions bound inside of lua catch (const char* error) { @@ -117,7 +117,7 @@ namespace sol { firstreturn = lua_gettop(lua_state()); return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime); } -#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) +#if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) // LuaJIT cannot have the catchall when the safe propagation is on // but LuaJIT will swallow all C++ errors // if we don't at least catch std::exception ones @@ -143,7 +143,7 @@ namespace sol { template , basic_protected_function>>, meta::neg>>, meta::neg>, meta::neg>>, is_lua_reference>> = meta::enabler> basic_protected_function(T&& r) noexcept : base_t(std::forward(r)), error_handler(get_default_handler(r.lua_state())) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_function>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; @@ -200,7 +200,7 @@ namespace sol { template >> = meta::enabler> basic_protected_function(lua_State* L, T&& r, handler_t eh) : base_t(L, std::forward(r)), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -216,7 +216,7 @@ namespace sol { } basic_protected_function(lua_State* L, int index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety @@ -226,7 +226,7 @@ namespace sol { } basic_protected_function(lua_State* L, absolute_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety @@ -236,7 +236,7 @@ namespace sol { } basic_protected_function(lua_State* L, raw_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety @@ -246,7 +246,7 @@ namespace sol { } basic_protected_function(lua_State* L, ref_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); diff --git a/sol/protected_function_result.hpp b/sol/protected_function_result.hpp index d8128297..ff6fc051 100644 --- a/sol/protected_function_result.hpp +++ b/sol/protected_function_result.hpp @@ -54,7 +54,7 @@ namespace sol { template decltype(auto) tagged_get(types, int index_offset) const { int target = index + index_offset; -#ifdef SOL_SAFE_PROXIES +#if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES if (!valid()) { type t = type_of(L, target); type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is not an error)"); @@ -73,7 +73,7 @@ namespace sol { error tagged_get(types, int index_offset) const { int target = index + index_offset; -#ifdef SOL_SAFE_PROXIES +#if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES if (valid()) { type t = type_of(L, target); type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is an error)"); diff --git a/sol/simple_usertype_metatable.hpp b/sol/simple_usertype_metatable.hpp index 950ed022..377068ec 100644 --- a/sol/simple_usertype_metatable.hpp +++ b/sol/simple_usertype_metatable.hpp @@ -70,7 +70,7 @@ namespace sol { string_view accessor = stack::get(L, keyidx); variable_wrapper* varwrap = nullptr; { -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH string_view& accessorkey = accessor; auto vit = variables.find(accessorkey, string_view_hash(), std::equal_to()); #else @@ -86,7 +86,7 @@ namespace sol { } bool function_failed = false; { -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH string_view& accessorkey = accessor; auto fit = functions.find(accessorkey, string_view_hash(), std::equal_to()); #else @@ -453,19 +453,19 @@ namespace sol { switch (mf) { case meta_function::construct: if (prop) { -#ifndef SOL_NO_EXCEPTIONS - throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); -#else +#if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); +#else + throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); #endif } break; case meta_function::garbage_collect: if (prop) { -#ifndef SOL_NO_EXCEPTIONS - throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); -#else +#if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); +#else + throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); #endif } return; diff --git a/sol/stack_check.hpp b/sol/stack_check.hpp index 499318c4..a282c4d9 100644 --- a/sol/stack_check.hpp +++ b/sol/stack_check.hpp @@ -99,7 +99,7 @@ namespace stack { static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); #if SOL_LUA_VERSION >= 503 -#ifdef SOL_STRINGS_ARE_NUMBERS +#if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS int isnum = 0; lua_tointegerx(L, index, &isnum); const bool success = isnum != 0; @@ -107,20 +107,27 @@ namespace stack { // expected type, actual type handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string"); } -#else +#elif (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION) // this check is precise, does not convert if (lua_isinteger(L, index) == 1) { return true; } const bool success = false; + if (!success) { + // expected type, actual type + handler(L, index, type::number, type_of(L, index), "not a numeric (integral) type"); + } +#else + type t = type_of(L, index); + const bool success = t == type::number; +#endif // If numbers are enabled, use the imprecise check if (!success) { // expected type, actual type handler(L, index, type::number, type_of(L, index), "not a numeric type"); } -#endif // If numbers are enabled, use the imprecise check return success; #else -#ifndef SOL_STRINGS_ARE_NUMBERS +#if !defined(SOL_STRINGS_ARE_NUMBERS) || !SOL_STRINGS_ARE_NUMBERS // must pre-check, because it will convert type t = type_of(L, index); if (t != type::number) { @@ -131,17 +138,21 @@ namespace stack { #endif // Do not allow strings to be numbers int isnum = 0; const lua_Number v = lua_tonumberx(L, index, &isnum); - const bool success = isnum != 0 && static_cast(llround(v)) == v; + const bool success = isnum != 0 +#if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION) + && static_cast(llround(v)) == v +#endif // Safe numerics and number precision checking + ; if (!success) { // expected type, actual type -#ifndef SOL_STRINGS_ARE_NUMBERS +#if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS handler(L, index, type::number, t, "not a numeric type"); #else handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string"); #endif } return success; -#endif +#endif // Lua Version 5.3 versus others } }; @@ -150,7 +161,14 @@ namespace stack { template static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { tracking.use(1); -#ifndef SOL_STRINGS_ARE_NUMBERS +#if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS + bool success = lua_isnumber(L, index) == 1; + if (!success) { + // expected type, actual type + handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string"); + } + return success; +#else type t = type_of(L, index); bool success = t == type::number; if (!success) { @@ -158,14 +176,7 @@ namespace stack { handler(L, index, type::number, t, "not a numeric type"); } return success; -#else - bool success = lua_isnumber(L, index) == 1; - if (!success) { - // expected type, actual type - handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string"); - } - return success; -#endif +#endif // Strings are Numbers } }; @@ -424,7 +435,7 @@ namespace stack { template static bool check(types, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) { -#ifdef SOL_ENABLE_INTEROP +#if defined(SOL_ENABLE_INTEROP) && SOL_ENABLE_INTEROP userdata_checker> uc; (void)uc; if (uc.check(L, index, indextype, handler, tracking)) { @@ -577,8 +588,8 @@ namespace stack { } }; -#ifdef SOL_CXX17_FEATURES -#ifdef SOL_STD_VARIANT +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT template struct checker, type::poly, C> { typedef std::variant V; diff --git a/sol/stack_check_get.hpp b/sol/stack_check_get.hpp index 7f7bc81c..2d7503a1 100644 --- a/sol/stack_check_get.hpp +++ b/sol/stack_check_get.hpp @@ -85,7 +85,7 @@ namespace stack { int isnum = 0; const lua_Number value = lua_tonumberx(L, index, &isnum); if (isnum != 0) { -#if defined(SOL_SAFE_NUMERICS) && !defined(SOL_NO_CHECK_NUMBER_PRECISION) +#if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION) const auto integer_value = llround(value); if (static_cast(integer_value) == value) { tracking.use(1); @@ -144,8 +144,8 @@ namespace stack { } }; -#ifdef SOL_CXX17_FEATURES -#ifdef SOL_STD_VARIANT +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT template struct check_getter> { typedef std::variant V; diff --git a/sol/stack_core.hpp b/sol/stack_core.hpp index 7632f82c..a606bd24 100644 --- a/sol/stack_core.hpp +++ b/sol/stack_core.hpp @@ -88,7 +88,7 @@ namespace sol { inline void* align_usertype_pointer(void* ptr) { typedef std::integral_constant::value > 1) @@ -104,7 +104,7 @@ namespace sol { inline void* align_usertype_unique_destructor(void* ptr) { typedef std::integral_constant::value > 1) @@ -123,7 +123,7 @@ namespace sol { template inline void* align_usertype_unique(void* ptr) { typedef std::integral_constant::value > 1) @@ -144,7 +144,7 @@ namespace sol { template inline void* align_user(void* ptr) { typedef std::integral_constant::value > 1) @@ -161,7 +161,7 @@ namespace sol { template inline T** usertype_allocate_pointer(lua_State* L) { typedef std::integral_constant::value > 1) @@ -198,7 +198,7 @@ namespace sol { template inline T* usertype_allocate(lua_State* L) { typedef std::integral_constant::value > 1 || std::alignment_of::value > 1) @@ -278,7 +278,7 @@ namespace sol { template inline Real* usertype_unique_allocate(lua_State* L, T**& pref, unique_destructor*& dx) { typedef std::integral_constant::value > 1 || std::alignment_of::value > 1 || std::alignment_of::value > 1) @@ -352,7 +352,7 @@ namespace sol { template inline T* user_allocate(lua_State* L) { typedef std::integral_constant::value > 1) @@ -705,7 +705,7 @@ namespace sol { namespace stack_detail { -#ifdef SOL_SAFE_GETTER +#if defined(SOL_SAFE_GETTER) && SOL_SAFE_GETTER template inline auto tagged_get(types, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get(L, index, tracking)) { auto op = check_get(L, index, type_panic_c_str, tracking); @@ -782,7 +782,7 @@ namespace sol { template inline decltype(auto) get_usertype(lua_State* L, int index, record& tracking) { -#ifdef SOL_SAFE_GETTER +#if defined(SOL_SAFE_GETTER) && SOL_SAFE_GETTER return stack_detail::tagged_get(types::value, detail::as_pointer_tag>, detail::as_value_tag>>(), L, index, tracking); #else return stack_detail::unchecked_get::value, detail::as_pointer_tag>, detail::as_value_tag>>(L, index, tracking); diff --git a/sol/stack_get.hpp b/sol/stack_get.hpp index 3b9e5af6..5d2e6844 100644 --- a/sol/stack_get.hpp +++ b/sol/stack_get.hpp @@ -36,9 +36,9 @@ #include #include #include -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES #include -#ifdef SOL_STD_VARIANT +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT #include #endif // Apple clang screwed up #endif // C++17 @@ -712,7 +712,7 @@ namespace stack { struct getter> { static T* get_no_lua_nil(lua_State* L, int index, record& tracking) { void* memory = lua_touserdata(L, index); -#ifdef SOL_ENABLE_INTEROP +#if defined(SOL_ENABLE_INTEROP) && SOL_ENABLE_INTEROP userdata_getter> ug; (void)ug; auto ugr = ug.get(L, index, memory, tracking); @@ -842,8 +842,8 @@ namespace stack { } }; -#ifdef SOL_CXX17_FEATURES -#ifdef SOL_STD_VARIANT +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT template struct getter> { typedef std::variant V; diff --git a/sol/stack_guard.hpp b/sol/stack_guard.hpp index e61b9dfe..997de560 100644 --- a/sol/stack_guard.hpp +++ b/sol/stack_guard.hpp @@ -31,7 +31,7 @@ namespace sol { namespace detail { inline void stack_fail(int, int) { -#ifndef SOL_NO_EXCEPTIONS +#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS) throw error(detail::direct_error, "imbalanced stack after operation finish"); #else // Lol, what do you want, an error printout? :3c diff --git a/sol/stack_pop.hpp b/sol/stack_pop.hpp index ee6fe44a..1ba008fb 100644 --- a/sol/stack_pop.hpp +++ b/sol/stack_pop.hpp @@ -47,7 +47,7 @@ namespace stack { template struct popper>::value>> { - static_assert(meta::neg>>::value, "You cannot pop something that derives from stack_reference: it will not remain on the stack and thusly will go out of scope!"); + static_assert(meta::neg>>::value, "You cannot pop something that lives solely on the stack: it will not remain on the stack when popped and thusly will go out of scope!"); }; } } // namespace sol::stack diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index ed14dab0..e08ec99c 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -35,9 +35,9 @@ #include #include #include -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES #include -#ifdef SOL_STD_VARIANT +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT #include #endif // Can use variant #endif // C++17 @@ -214,19 +214,19 @@ namespace stack { lua_pushinteger(L, static_cast(value)); return 1; } -#endif -#if defined(SOL_SAFE_NUMERICS) && !defined(SOL_NO_CHECK_NUMBER_PRECISION) +#endif // Lua 5.3 and above +#if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION) if (static_cast(llround(static_cast(value))) != value) { -#ifdef SOL_NO_EXCEPTIONS +#if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS // Is this really worth it? assert(false && "integer value will be misrepresented in lua"); lua_pushnumber(L, static_cast(value)); return 1; #else throw error(detail::direct_error, "integer value will be misrepresented in lua"); -#endif +#endif // No Exceptions } -#endif +#endif // Safe Numerics and Number Precision Check lua_pushnumber(L, static_cast(value)); return 1; } @@ -288,7 +288,7 @@ namespace stack { } lua_pop(L, 1 + p); } -#endif +#endif // Lua Version 5.3 and others } // TODO: figure out a better way to do this...? //set_field(L, -1, cont.size()); @@ -388,7 +388,7 @@ namespace stack { } }; -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE template <> struct pusher> { static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) { @@ -988,8 +988,8 @@ namespace stack { } }; -#ifdef SOL_CXX17_FEATURES -#ifdef SOL_STD_VARIANT +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES +#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT namespace stack_detail { struct push_function { diff --git a/sol/state_handling.hpp b/sol/state_handling.hpp index deeae5bd..2c869a50 100644 --- a/sol/state_handling.hpp +++ b/sol/state_handling.hpp @@ -49,7 +49,7 @@ namespace sol { } inline int default_at_panic(lua_State* L) { -#ifdef SOL_NO_EXCEPTIONS +#if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS (void)L; return -1; #else @@ -58,7 +58,7 @@ namespace sol { if (message) { std::string err(message, messagesize); lua_settop(L, 0); -#ifdef SOL_PRINT_ERRORS +#if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS std::cerr << "[sol2] An error occurred and panic has been invoked: "; std::cerr << err; std::cerr << std::endl; @@ -66,8 +66,8 @@ namespace sol { throw error(err); } lua_settop(L, 0); - throw error(std::string("An unexpected error occurred and forced the lua state to call atpanic")); -#endif + throw error(std::string("An unexpected error occurred and panic has been invoked")); +#endif // Printing Errors } inline int default_traceback_error_handler(lua_State* L) { @@ -83,11 +83,11 @@ namespace sol { const string_view& traceback = maybetraceback.value(); msg.assign(traceback.data(), traceback.size()); } -#ifdef SOL_PRINT_ERRORS - std::cerr << "[sol2] An error occurred: "; - std::cerr << msg; - std::cerr << std::endl; -#endif +#if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS + //std::cerr << "[sol2] An error occurred and was caught in traceback: "; + //std::cerr << msg; + //std::cerr << std::endl; +#endif // Printing return stack::push(L, msg); } @@ -115,7 +115,7 @@ namespace sol { std::string err = "sol: "; err += to_string(result.status()); err += " error"; -#ifndef SOL_NO_EXCEPTIONS +#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS) std::exception_ptr eptr = std::current_exception(); if (eptr) { err += " with a "; @@ -144,12 +144,12 @@ namespace sol { string_view serr = stack::get(L, result.stack_index()); err.append(serr.data(), serr.size()); } -#ifdef SOL_PRINT_ERRORS +#if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS std::cerr << "[sol2] An error occurred and has been passed to an error handler: "; std::cerr << err; std::cerr << std::endl; #endif -#ifdef SOL_NO_EXCEPTIONS +#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS) // replacing information of stack error into pfr int target = result.stack_index(); if (result.pop_count() > 0) { @@ -169,7 +169,7 @@ namespace sol { } inline protected_function_result script_default_on_error(lua_State* L, protected_function_result pfr) { -#ifdef SOL_DEFAULT_PASS_ON_ERROR +#if defined(SOL_DEFAULT_PASS_ON_ERROR) && SOL_DEFAULT_PASS_ON_ERROR return script_pass_on_error(L, std::move(pfr)); #else return script_throw_on_error(L, std::move(pfr)); diff --git a/sol/state_view.hpp b/sol/state_view.hpp index 793de9f7..93a7d08a 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -391,7 +391,7 @@ namespace sol { return safe_script_file(filename, env, script_default_on_error, mode); } -#ifdef SOL_SAFE_FUNCTION +#if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION protected_function_result script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) { return safe_script(code, chunkname, mode); } diff --git a/sol/string_view.hpp b/sol/string_view.hpp index dff2888c..85f4daf2 100644 --- a/sol/string_view.hpp +++ b/sol/string_view.hpp @@ -27,16 +27,16 @@ #include "feature_test.hpp" #include #include -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES #include #endif // C++17 features #include -#ifdef SOL_USE_BOOST +#if defined(SOL_USE_BOOST) && SOL_USE_BOOST #include #endif namespace sol { -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES template > using basic_string_view = std::basic_string_view; typedef std::string_view string_view; @@ -139,7 +139,7 @@ namespace sol { } result_type operator()(const argument_type& r) const { -#ifdef SOL_USE_BOOST +#if defined(SOL_USE_BOOST) && SOL_USE_BOOST return boost::hash_range(r.begin(), r.end()); #else // Modified, from libstdc++ diff --git a/sol/table_core.hpp b/sol/table_core.hpp index d0cf38af..9222043f 100644 --- a/sol/table_core.hpp +++ b/sol/table_core.hpp @@ -218,7 +218,7 @@ namespace sol { template >> = meta::enabler> basic_table_core(lua_State* L, T&& r) : base_t(L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -232,14 +232,14 @@ namespace sol { } basic_table_core(lua_State* L, int index = -1) : basic_table_core(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_table_core(lua_State* L, ref_index index) : basic_table_core(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -248,7 +248,7 @@ namespace sol { template , basic_table_core>>, meta::neg>, meta::neg>>, is_lua_reference>> = meta::enabler> basic_table_core(T&& r) noexcept : basic_table_core(detail::no_safety, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_table>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; diff --git a/sol/thread.hpp b/sol/thread.hpp index 048edf81..4ff88dba 100644 --- a/sol/thread.hpp +++ b/sol/thread.hpp @@ -90,7 +90,7 @@ namespace sol { template , basic_thread>>, is_lua_reference>> = meta::enabler> basic_thread(T&& r) : base_t(std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -105,7 +105,7 @@ namespace sol { template >> = meta::enabler> basic_thread(lua_State* L, T&& r) : base_t(L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -113,14 +113,14 @@ namespace sol { } basic_thread(lua_State* L, int index = -1) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_thread(lua_State* L, ref_index index) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -134,7 +134,7 @@ namespace sol { } basic_thread(lua_State* L, lua_thread_state actualthread) : base_t(L, -stack::push(L, actualthread)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(lua_state(), -1, handler); #endif // Safety diff --git a/sol/traits.hpp b/sol/traits.hpp index dcc7ad9b..9a03b1a9 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -560,7 +560,7 @@ namespace sol { template using is_string_like = any< is_specialization_of, std::basic_string>, -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES is_specialization_of, std::basic_string_view>, #else is_specialization_of, basic_string_view>, @@ -573,7 +573,7 @@ namespace sol { meta::all>, std::is_same>>, char>>, std::is_same, const char*>, std::is_same, char>, std::is_same, std::string>, std::is_same, std::initializer_list> -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES , std::is_same, std::string_view> #endif >; diff --git a/sol/types.hpp b/sol/types.hpp index c2c94d94..b0f72c06 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -37,7 +37,7 @@ #include #include #include -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES #include #ifdef SOL_STD_VARIANT #include @@ -982,7 +982,7 @@ namespace sol { template <> struct lua_type_of : std::integral_constant {}; -#ifdef SOL_CXX17_FEATURES +#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES #ifdef SOL_STD_VARIANT template struct lua_type_of> : std::integral_constant {}; diff --git a/sol/unsafe_function.hpp b/sol/unsafe_function.hpp index eed55651..10baeb26 100644 --- a/sol/unsafe_function.hpp +++ b/sol/unsafe_function.hpp @@ -71,7 +71,7 @@ namespace sol { template , basic_function>>, meta::neg>, meta::neg>>, is_lua_reference>> = meta::enabler> basic_function(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_function>::value) { auto pp = stack::push_pop(*this); constructor_handler handler{}; @@ -95,7 +95,7 @@ namespace sol { template >> = meta::enabler> basic_function(lua_State* L, T&& r) : base_t(L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -103,14 +103,14 @@ namespace sol { } basic_function(lua_State* L, int index = -1) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_function(lua_State* L, ref_index index) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); diff --git a/sol/userdata.hpp b/sol/userdata.hpp index 7c6df40f..6b94f56a 100644 --- a/sol/userdata.hpp +++ b/sol/userdata.hpp @@ -39,7 +39,7 @@ namespace sol { template , basic_userdata>>, meta::neg>, is_lua_reference>> = meta::enabler> basic_userdata(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_userdata>::value) { auto pp = stack::push_pop(*this); type_assert(lua_state(), -1, type::userdata); @@ -59,7 +59,7 @@ namespace sol { template >> = meta::enabler> basic_userdata(lua_State* L, T&& r) : base_t(L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(L, -1, handler); @@ -67,14 +67,14 @@ namespace sol { } basic_userdata(lua_State* L, int index = -1) : base_t(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_userdata(lua_State* L, ref_index index) : base_t(detail::no_safety, L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(L, -1, handler); @@ -93,7 +93,7 @@ namespace sol { template , basic_lightuserdata>>, meta::neg>, is_lua_reference>> = meta::enabler> basic_lightuserdata(T&& r) noexcept : base_t(std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES if (!is_lightuserdata>::value) { auto pp = stack::push_pop(*this); type_assert(lua_state(), -1, type::lightuserdata); @@ -113,7 +113,7 @@ namespace sol { template >> = meta::enabler> basic_lightuserdata(lua_State* L, T&& r) : basic_lightuserdata(L, std::forward(r)) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), -1, handler); @@ -121,14 +121,14 @@ namespace sol { } basic_lightuserdata(lua_State* L, int index = -1) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES constructor_handler handler{}; stack::check(L, index, handler); #endif // Safety } basic_lightuserdata(lua_State* L, ref_index index) : base_t(L, index) { -#ifdef SOL_SAFE_REFERENCES +#if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES auto pp = stack::push_pop(*this); constructor_handler handler{}; stack::check(lua_state(), index, handler); diff --git a/sol/usertype_core.hpp b/sol/usertype_core.hpp index 821facc9..1679bdab 100644 --- a/sol/usertype_core.hpp +++ b/sol/usertype_core.hpp @@ -151,7 +151,7 @@ namespace sol { template inline void make_length_op_const(std::true_type, Regs& l, int& index) { const char* name = to_string(meta_function::length).c_str(); -#ifdef __clang__ +#if defined(__clang__) l[index] = luaL_Reg{ name, &c_call }; #else typedef decltype(std::declval().size()) R; @@ -164,7 +164,7 @@ namespace sol { template inline void make_length_op_const(std::false_type, Regs& l, int& index) { const char* name = to_string(meta_function::length).c_str(); -#ifdef __clang__ +#if defined(__clang__) l[index] = luaL_Reg{ name, &c_call }; #else typedef decltype(std::declval().size()) R; diff --git a/sol/usertype_metatable.hpp b/sol/usertype_metatable.hpp index 50f69491..5d475adf 100644 --- a/sol/usertype_metatable.hpp +++ b/sol/usertype_metatable.hpp @@ -216,7 +216,7 @@ namespace sol { template inline int indexing_fail(lua_State* L) { if (is_index) { -#if 0 //def SOL_SAFE_USERTYPE +#if 0 //defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE auto maybeaccessor = stack::get>(L, is_index ? -1 : -2); string_view accessor = maybeaccessor.value_or(string_detail::string_shim("(unknown)")); return luaL_error(L, "sol: attempt to index (get) nil value \"%s\" on userdata (bad (misspelled?) key name or does not exist)", accessor.data()); @@ -253,14 +253,14 @@ namespace sol { return; } string_view& accessor_view = maybeaccessor.value(); -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH auto preexistingit = functions.find(accessor_view, string_view_hash(), std::equal_to()); #else std::string accessor(accessor_view.data(), accessor_view.size()); auto preexistingit = functions.find(accessor); #endif if (preexistingit == functions.cend()) { -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH std::string accessor(accessor_view.data(), accessor_view.size()); #endif functions.emplace_hint(preexistingit, std::move(accessor), object(L, 3)); @@ -282,14 +282,14 @@ namespace sol { mapping_t& mapping = umc.mapping; std::vector& runtime = umc.runtime; int target = static_cast(runtime.size()); -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH auto preexistingit = mapping.find(accessor_view, string_view_hash(), std::equal_to()); #else std::string accessor(accessor_view.data(), accessor_view.size()); auto preexistingit = mapping.find(accessor); #endif if (preexistingit == mapping.cend()) { -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH std::string accessor(accessor_view.data(), accessor_view.size()); #endif runtime.emplace_back(L, 3); @@ -486,7 +486,7 @@ namespace sol { switch (mf) { case meta_function::construct: if (properties[i]) { -#ifndef SOL_NO_EXCEPTIONS +#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS) throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); #else assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); @@ -495,7 +495,7 @@ namespace sol { break; case meta_function::garbage_collect: if (destructfunc != nullptr) { -#ifndef SOL_NO_EXCEPTIONS +#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS) throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); #else assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems"); @@ -576,7 +576,7 @@ namespace sol { int runtime_target = 0; usertype_detail::member_search member = nullptr; { -#ifdef SOL_UNORDERED_MAP_COMPATIBLE_HASH +#if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH string_view name = stack::get(L, keyidx); auto memberit = f.mapping.find(name, string_view_hash(), std::equal_to()); #else diff --git a/sol/wrapper.hpp b/sol/wrapper.hpp index eabeb3c9..db14f82b 100644 --- a/sol/wrapper.hpp +++ b/sol/wrapper.hpp @@ -225,7 +225,8 @@ namespace sol { struct wrapper : public member_function_wrapper { }; -#ifdef SOL_NOEXCEPT_FUNCTION_TYPE //noexcept has become a part of a function's type +#if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE + //noexcept has become a part of a function's type template struct wrapper : public member_function_wrapper {