diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2018-07-27 07:35:24 +0200 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2018-07-27 07:35:24 +0200 |
commit | c325be435c64be57cf2210fa6936af0d425d0ce0 (patch) | |
tree | 0a6398c67346ff8df989994e7e29d4110c515889 /c-api/compat-5.3.c | |
parent | 3c76f8f5edf602b023672f8d8f317b0eea2f1195 (diff) | |
download | lua-compat-5.3-c325be435c64be57cf2210fa6936af0d425d0ce0.tar.gz lua-compat-5.3-c325be435c64be57cf2210fa6936af0d425d0ce0.tar.bz2 lua-compat-5.3-c325be435c64be57cf2210fa6936af0d425d0ce0.zip |
Add an implementation of `lua_getextraspace()`.
Diffstat (limited to 'c-api/compat-5.3.c')
-rw-r--r-- | c-api/compat-5.3.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index bf81673..590e3c8 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c | |||
@@ -720,6 +720,63 @@ COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) { | |||
720 | } | 720 | } |
721 | 721 | ||
722 | 722 | ||
723 | #ifndef LUA_EXTRASPACE | ||
724 | #define LUA_EXTRASPACE (sizeof(void*)) | ||
725 | #endif | ||
726 | |||
727 | COMPAT53_API void *lua_getextraspace (lua_State *L) { | ||
728 | int is_main = 0; | ||
729 | void *ptr = NULL; | ||
730 | luaL_checkstack(L, 4, "not enough stack slots available"); | ||
731 | lua_pushliteral(L, "__compat53_extraspace"); | ||
732 | lua_pushvalue(L, -1); | ||
733 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
734 | if (!lua_istable(L, -1)) { | ||
735 | lua_pop(L, 1); | ||
736 | lua_createtable(L, 0, 2); | ||
737 | lua_createtable(L, 0, 1); | ||
738 | lua_pushliteral(L, "k"); | ||
739 | lua_setfield(L, -2, "__mode"); | ||
740 | lua_setmetatable(L, -2); | ||
741 | lua_pushvalue(L, -2); | ||
742 | lua_pushvalue(L, -2); | ||
743 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
744 | } | ||
745 | lua_replace(L, -2); | ||
746 | is_main = lua_pushthread(L); | ||
747 | lua_rawget(L, -2); | ||
748 | ptr = lua_touserdata(L, -1); | ||
749 | if (!ptr) { | ||
750 | lua_pop(L, 1); | ||
751 | ptr = lua_newuserdata(L, LUA_EXTRASPACE); | ||
752 | if (is_main) { | ||
753 | memset(ptr, '\0', LUA_EXTRASPACE); | ||
754 | lua_pushthread(L); | ||
755 | lua_pushvalue(L, -2); | ||
756 | lua_rawset(L, -4); | ||
757 | lua_pushboolean(L, 1); | ||
758 | lua_pushvalue(L, -2); | ||
759 | lua_rawset(L, -4); | ||
760 | } else { | ||
761 | void* mptr = NULL; | ||
762 | lua_pushboolean(L, 1); | ||
763 | lua_rawget(L, -3); | ||
764 | mptr = lua_touserdata(L, -1); | ||
765 | if (mptr) | ||
766 | memcpy(ptr, mptr, LUA_EXTRASPACE); | ||
767 | else | ||
768 | memset(ptr, '\0', LUA_EXTRASPACE); | ||
769 | lua_pop(L, 1); | ||
770 | lua_pushthread(L); | ||
771 | lua_pushvalue(L, -2); | ||
772 | lua_rawset(L, -4); | ||
773 | } | ||
774 | } | ||
775 | lua_pop(L, 2); | ||
776 | return ptr; | ||
777 | } | ||
778 | |||
779 | |||
723 | COMPAT53_API int lua_isinteger (lua_State *L, int index) { | 780 | COMPAT53_API int lua_isinteger (lua_State *L, int index) { |
724 | if (lua_type(L, index) == LUA_TNUMBER) { | 781 | if (lua_type(L, index) == LUA_TNUMBER) { |
725 | lua_Number n = lua_tonumber(L, index); | 782 | lua_Number n = lua_tonumber(L, index); |