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 | |
| 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')
| -rw-r--r-- | c-api/compat-5.3.c | 57 | ||||
| -rw-r--r-- | c-api/compat-5.3.h | 4 |
2 files changed, 60 insertions, 1 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); |
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index 8e10893..082a6a0 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h | |||
| @@ -293,6 +293,9 @@ typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); | |||
| 293 | #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) | 293 | #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) |
| 294 | COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); | 294 | COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); |
| 295 | 295 | ||
| 296 | #define lua_getextraspace COMPAT53_CONCAT(COMPAT53_PREFIX, _getextraspace) | ||
| 297 | COMPAT53_API void *lua_getextraspace (lua_State *L); | ||
| 298 | |||
| 296 | #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) | 299 | #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) |
| 297 | COMPAT53_API int lua_isinteger (lua_State *L, int index); | 300 | COMPAT53_API int lua_isinteger (lua_State *L, int index); |
| 298 | 301 | ||
| @@ -339,7 +342,6 @@ COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, | |||
| 339 | 342 | ||
| 340 | /* XXX not implemented: | 343 | /* XXX not implemented: |
| 341 | * lua_isyieldable | 344 | * lua_isyieldable |
| 342 | * lua_getextraspace | ||
| 343 | * lua_arith (new operators) | 345 | * lua_arith (new operators) |
| 344 | * lua_pushfstring (new formats) | 346 | * lua_pushfstring (new formats) |
| 345 | */ | 347 | */ |
