ffi_accel.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From 1ea38575248fdf0467a8a57a9a43301e33592024 Mon Sep 17 00:00:00 2001
  2. From: Pedro Gimeno <pgimeno@users.noreply.notabug.org>
  3. Date: Tue, 25 Jun 2019 13:36:22 +0200
  4. Subject: [PATCH] Add FFI pointer and size retrieval and sanity check functions
  5. ---
  6. src/mapnode.cpp | 9 +++++++++
  7. src/porting.h | 2 ++
  8. src/script/lua_api/l_noise.cpp | 44 +++++++++++++++++++++++++++++++++++++++++
  9. src/script/lua_api/l_noise.h | 2 ++
  10. src/script/lua_api/l_vmanip.cpp | 20 +++++++++++++++++++
  11. 5 files changed, 77 insertions(+)
  12. diff --git a/src/mapnode.cpp b/src/mapnode.cpp
  13. index 9761a661..b6840eb1 100644
  14. --- a/src/mapnode.cpp
  15. +++ b/src/mapnode.cpp
  16. @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  17. #include "util/numeric.h"
  18. #include <string>
  19. #include <sstream>
  20. +#include <cstddef>
  21. static const Rotation wallmounted_to_rot[] = {
  22. ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
  23. @@ -44,6 +45,14 @@ static const u8 rot_to_wallmounted[] = {
  24. MapNode
  25. */
  26. +extern "C" EXPORT int get_mapnode_version(void)
  27. +{
  28. + if (sizeof(MapNode) == 4 && offsetof(MapNode, param0) == 0
  29. + && offsetof(MapNode, param1) == 2 && offsetof(MapNode, param2) == 3)
  30. + return 1;
  31. + return 0;
  32. +}
  33. +
  34. void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
  35. {
  36. if (f.palette) {
  37. diff --git a/src/porting.h b/src/porting.h
  38. index 4d30a597..56419ff9 100644
  39. --- a/src/porting.h
  40. +++ b/src/porting.h
  41. @@ -54,6 +54,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  42. #include <windows.h>
  43. #define sleep_ms(x) Sleep(x)
  44. + #define EXPORT __declspec(dllexport)
  45. #else
  46. #include <unistd.h>
  47. #include <cstdint> //for uintptr_t
  48. @@ -67,6 +68,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  49. #endif
  50. #define sleep_ms(x) usleep(x*1000)
  51. + #define EXPORT
  52. #endif
  53. #ifdef _MSC_VER
  54. diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp
  55. index e38d319f..47955977 100644
  56. --- a/src/script/lua_api/l_noise.cpp
  57. +++ b/src/script/lua_api/l_noise.cpp
  58. @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  59. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  60. */
  61. +#include "config.h"
  62. #include "lua_api/l_noise.h"
  63. #include "lua_api/l_internal.h"
  64. #include "common/c_converter.h"
  65. @@ -401,6 +402,49 @@ luaL_Reg LuaPerlinNoiseMap::methods[] = {
  66. {0,0}
  67. };
  68. +extern "C" {
  69. +
  70. +EXPORT size_t PerlinNoiseMap_get_area(void **pnmp)
  71. +{
  72. + NO_MAP_LOCK_REQUIRED;
  73. +
  74. + if (pnmp == nullptr)
  75. + throw ModError("Nil pointer in C call");
  76. +
  77. + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp;
  78. +
  79. + Noise *n = o->getNoise();
  80. +
  81. + return n->sx * n->sy;
  82. +}
  83. +
  84. +EXPORT size_t PerlinNoiseMap_get_volume(void **pnmp)
  85. +{
  86. + NO_MAP_LOCK_REQUIRED;
  87. +
  88. + if (pnmp == nullptr)
  89. + throw ModError("Nil pointer in C call");
  90. +
  91. + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp;
  92. +
  93. + Noise *n = o->getNoise();
  94. +
  95. + return n->sx * n->sy * n->sz;
  96. +}
  97. +
  98. +EXPORT void PerlinNoiseMap_get_pointer(void **pnmp, void **ptr)
  99. +{
  100. + NO_MAP_LOCK_REQUIRED;
  101. +
  102. + if (pnmp == nullptr || ptr == nullptr)
  103. + throw ModError("Nil pointer in C call");
  104. +
  105. + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp;
  106. + *ptr = o->getNoise();
  107. +}
  108. +
  109. +} // extern "C"
  110. +
  111. ///////////////////////////////////////
  112. /*
  113. LuaPseudoRandom
  114. diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h
  115. index 9f50dfd3..49a7ec6e 100644
  116. --- a/src/script/lua_api/l_noise.h
  117. +++ b/src/script/lua_api/l_noise.h
  118. @@ -91,6 +91,8 @@ class LuaPerlinNoiseMap : public ModApiBase
  119. static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg);
  120. static void Register(lua_State *L);
  121. +
  122. + Noise *getNoise() const { return noise; }
  123. };
  124. /*
  125. diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp
  126. index c92983bd..8401657d 100644
  127. --- a/src/script/lua_api/l_vmanip.cpp
  128. +++ b/src/script/lua_api/l_vmanip.cpp
  129. @@ -30,6 +30,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
  130. #include "mapgen/mapgen.h"
  131. #include "voxelalgorithms.h"
  132. +extern "C" {
  133. +
  134. +EXPORT void VoxelManip_get_pointer(void **lvmp, void **ptr)
  135. +{
  136. + if (lvmp == nullptr || ptr == nullptr)
  137. + throw ModError("Nil pointer in C call");
  138. +
  139. + *ptr = (*(LuaVoxelManip **)lvmp)->vm->m_data;
  140. +}
  141. +
  142. +EXPORT s32 VoxelManip_get_volume(void **lvmp)
  143. +{
  144. + if (lvmp == nullptr)
  145. + throw ModError("Nil pointer in C call");
  146. +
  147. + return (*(LuaVoxelManip **)lvmp)->vm->m_area.getVolume();
  148. +}
  149. +
  150. +} // extern "C"
  151. +
  152. // garbage collector
  153. int LuaVoxelManip::gc_object(lua_State *L)
  154. {
  155. --
  156. 2.11.0