| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef GEOGRAM_LUA_LUA_WRAP | ||
| 41 | |||
| 42 | #include <geogram/basic/common.h> | ||
| 43 | #include <geogram/lua/lua.h> | ||
| 44 | #include <geogram/lua/lua_vec_mat.h> | ||
| 45 | #include <geogram/basic/assert.h> | ||
| 46 | #include <geogram/basic/numeric.h> | ||
| 47 | #include <geogram/basic/string.h> | ||
| 48 | #include <geogram/basic/memory.h> | ||
| 49 | |||
| 50 | |||
| 51 | #ifdef GEO_COMPILER_MSVC | ||
| 52 | #pragma warning( push ) | ||
| 53 | #pragma warning( disable: 4702 ) | ||
| 54 | #endif | ||
| 55 | |||
| 56 | /** | ||
| 57 | * \file geogram/lua/lua_wrap.h | ||
| 58 | * \brief Utilities to write lua bindings. | ||
| 59 | */ | ||
| 60 | |||
| 61 | namespace GEO { | ||
| 62 | |||
| 63 | /** | ||
| 64 | * \brief A pointer to a LUA function to test an argument | ||
| 65 | * in the LUA stack. | ||
| 66 | */ | ||
| 67 | typedef int (*lua_test_func)(lua_State* L, int idx); | ||
| 68 | |||
| 69 | |||
| 70 | /** | ||
| 71 | * \brief Tests whether a LUA variable is a boolean. | ||
| 72 | * \details lua_isboolean() is a macro, and we needed | ||
| 73 | * a true function here (to be passed to lua_check_type()). | ||
| 74 | * \param[in] L a pointer to the LUA state | ||
| 75 | * \param[in] idx an index in the LUA stack | ||
| 76 | * \retval a non-zero integer if the variable | ||
| 77 | * at index \p idx in the LUA | ||
| 78 | * state \p L is a boolean. | ||
| 79 | * \retval 0 otherwise. | ||
| 80 | */ | ||
| 81 | ✗ | inline int my_lua_isboolean(lua_State* L, int idx) { | |
| 82 | ✗ | return lua_isboolean(L,idx); | |
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * \brief Tests whether a LUA variable is a light user data. | ||
| 87 | * \details lua_isuserdata() is a macro, and we needed | ||
| 88 | * a true function here (to be passed to lua_check_type()). | ||
| 89 | * \param[in] L a pointer to the LUA state | ||
| 90 | * \param[in] idx an index in the LUA stack | ||
| 91 | * \retval a non-zero integer if the variable | ||
| 92 | * at index \p idx in the LUA | ||
| 93 | * state \p L is a light user data. | ||
| 94 | * \retval 0 otherwise. | ||
| 95 | */ | ||
| 96 | inline int my_lua_islightuserdata(lua_State* L, int idx) { | ||
| 97 | return lua_islightuserdata(L,idx); | ||
| 98 | } | ||
| 99 | |||
| 100 | /** | ||
| 101 | * \brief Tests whether a LUA variable is a positive integer. | ||
| 102 | * \param[in] L a pointer to the LUA state | ||
| 103 | * \param[in] idx an index in the LUA stack | ||
| 104 | * \retval a non-zero integer if the variable | ||
| 105 | * at index \p idx in the LUA | ||
| 106 | * state \p L is a positive integer. | ||
| 107 | * \retval 0 otherwise. | ||
| 108 | */ | ||
| 109 | ✗ | inline int my_lua_ispositiveinteger(lua_State* L, int idx) { | |
| 110 | ✗ | if(!lua_isinteger(L,idx)) { | |
| 111 | return 0; | ||
| 112 | } | ||
| 113 | ✗ | lua_Integer x = lua_tointeger(L,idx); | |
| 114 | ✗ | return (x > 0) ? 1 : 0; | |
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * \brief Memorizes an error message in LUA registry. | ||
| 119 | * \details This is used by C++/LUA interoperability | ||
| 120 | * functions to memorize an error. The error message | ||
| 121 | * can be passed back to LUA when exiting a wrapper. | ||
| 122 | * \param[in] L a pointer to the LUA state. | ||
| 123 | * \param[in] error the error message. It will be copied. | ||
| 124 | */ | ||
| 125 | inline void lua_set_error(lua_State* L, const char* error) { | ||
| 126 | ✗ | lua_pushstring(L, error); | |
| 127 | ✗ | lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error"); | |
| 128 | ✗ | } | |
| 129 | |||
| 130 | /** | ||
| 131 | * \brief Memorizes an error message in LUA registry. | ||
| 132 | * \details This is used by C++/LUA interoperability | ||
| 133 | * functions to memorize an error. The error message | ||
| 134 | * can be passed back to LUA when exiting a wrapper. | ||
| 135 | * \param[in] L a pointer to the LUA state. | ||
| 136 | * \param[in] error the error message. It will be copied. | ||
| 137 | */ | ||
| 138 | inline void lua_set_error(lua_State* L, const std::string& error) { | ||
| 139 | lua_set_error(L,error.c_str()); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * \brief Clears the last error message in LUA registry. | ||
| 144 | * \param[in] L a pointer to the LUA state. | ||
| 145 | */ | ||
| 146 | inline void lua_clear_error(lua_State* L) { | ||
| 147 | ✗ | lua_pushnil(L); | |
| 148 | ✗ | lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error"); | |
| 149 | ✗ | } | |
| 150 | |||
| 151 | /** | ||
| 152 | * \brief Tests whether an error message was memorized in the | ||
| 153 | * registry. | ||
| 154 | * \param[in] L a pointer to the LUA state. | ||
| 155 | */ | ||
| 156 | inline bool lua_has_error(lua_State* L) { | ||
| 157 | lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error"); | ||
| 158 | bool result = !lua_isnil(L,-1); | ||
| 159 | lua_pop(L,1); | ||
| 160 | return result; | ||
| 161 | } | ||
| 162 | |||
| 163 | /** | ||
| 164 | * \brief Tests whether a LUA variable has the correct type. | ||
| 165 | * \details If the LUA variable does not have the correct type, | ||
| 166 | * then an error message is memorized in the registry. | ||
| 167 | * \param[in] L a pointer to the LUA state | ||
| 168 | * \param[in] idx the index of the variable | ||
| 169 | * \param[in] test a function to test the type, taking as argument | ||
| 170 | * \p L and \p idx, and returning 1 if the type of the argument | ||
| 171 | * is correct and 0 otherwise. | ||
| 172 | * \retval true if the type is corret | ||
| 173 | * \retval false otherwise | ||
| 174 | */ | ||
| 175 | ✗ | inline bool lua_check_type(lua_State* L, int idx, lua_test_func test) { | |
| 176 | ✗ | if(!test(L,idx)) { | |
| 177 | ✗ | std::string error = std::string("Argument ") + | |
| 178 | ✗ | String::to_string(idx) + ": wrong argument type (" + | |
| 179 | ✗ | "got " + lua_typename(L,lua_type(L,idx)) + ")"; | |
| 180 | lua_set_error(L, error.c_str()); | ||
| 181 | return false; | ||
| 182 | } | ||
| 183 | return true; | ||
| 184 | } | ||
| 185 | |||
| 186 | /** | ||
| 187 | * \brief Tests whether the expected number of arguments was pushed | ||
| 188 | * onto the stack. | ||
| 189 | * \details If the number of elements on the stack does not match | ||
| 190 | * the expected number of arguments, then an error message is | ||
| 191 | * memorized in the registry. | ||
| 192 | * \param[in] L a pointer to the LUA state | ||
| 193 | * \param[in] expected_nb_args the expected number of arguments | ||
| 194 | * \retval true if the expected number of arguments were pushed onto | ||
| 195 | * the stack | ||
| 196 | * \retval false otherwise | ||
| 197 | */ | ||
| 198 | ✗ | inline bool lua_check_nb_args(lua_State* L, int expected_nb_args) { | |
| 199 | ✗ | if(lua_gettop(L) != expected_nb_args) { | |
| 200 | std::string error = | ||
| 201 | ✗ | "Expected " + String::to_string(expected_nb_args) | |
| 202 | ✗ | + " arg(s), got " + String::to_string(lua_gettop(L)); | |
| 203 | lua_set_error(L,error.c_str()); | ||
| 204 | return false; | ||
| 205 | } | ||
| 206 | return true; | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * \brief Takes the last error message memorized in the registry | ||
| 211 | * and sends it back to LUA. | ||
| 212 | * \details This function is called by wrappers right before returning | ||
| 213 | * each time an error is detected. | ||
| 214 | * \retval the return value supposed to be returned by the wrapper, | ||
| 215 | * as follows: | ||
| 216 | * \code | ||
| 217 | * if(error occured) { | ||
| 218 | * return lua_notify_last_error(L); | ||
| 219 | * } | ||
| 220 | * \endcode | ||
| 221 | */ | ||
| 222 | ✗ | inline int lua_notify_last_error(lua_State* L) { | |
| 223 | std::string error; | ||
| 224 | ✗ | lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error"); | |
| 225 | ✗ | if(lua_isstring(L,-1)) { | |
| 226 | ✗ | error += lua_tostring(L,-1); | |
| 227 | } | ||
| 228 | ✗ | lua_pop(L,1); | |
| 229 | lua_clear_error(L); | ||
| 230 | ✗ | return luaL_error(L,error.c_str()); | |
| 231 | } | ||
| 232 | |||
| 233 | /**********************************************************************/ | ||
| 234 | |||
| 235 | /** | ||
| 236 | * \brief Converts LUA variables to C++ variables. | ||
| 237 | * \details This version is a placeholder, the actual implementation | ||
| 238 | * is done in the template specializations. | ||
| 239 | */ | ||
| 240 | template <class T> class lua_to { | ||
| 241 | public: | ||
| 242 | /** | ||
| 243 | * \brief lua_to constructor. | ||
| 244 | * \details Does the actual conversion. The converted variable | ||
| 245 | * is memorized in a member. It can be accesed by a conversion | ||
| 246 | * operator. Supposing that my_function() takes a std::string and | ||
| 247 | * an int as arguments, it can be called as follows: | ||
| 248 | * \code | ||
| 249 | * my_function(lua_to<std::string>(L,1), lua_to<int>(L,2)); | ||
| 250 | * \endcode | ||
| 251 | * \param[in] L a pointer to the LUA state. | ||
| 252 | * \param[in] idx the index of the variable to be converted. | ||
| 253 | */ | ||
| 254 | lua_to(lua_State* L, int idx) { | ||
| 255 | geo_argused(L); | ||
| 256 | geo_argused(idx); | ||
| 257 | geo_assert_not_reached; | ||
| 258 | } | ||
| 259 | |||
| 260 | /** | ||
| 261 | * \brief Tests whether a LUA variable can be converted to | ||
| 262 | * a C++ variable. | ||
| 263 | * \note It would have been possible to make the constructor throw | ||
| 264 | * an exception on conversion error, but exceptions are not well | ||
| 265 | * supported by Emscripten so we have separated validity check from | ||
| 266 | * conversion. | ||
| 267 | * \param[in] L a pointer to the LUA state. | ||
| 268 | * \param[in] idx the index of the variable to be converted. | ||
| 269 | * \retval true if the LUA variable can be converted to a C++ variable. | ||
| 270 | * \retval false otherwise. | ||
| 271 | */ | ||
| 272 | static bool can_convert(lua_State* L, int idx) { | ||
| 273 | geo_argused(L); | ||
| 274 | geo_argused(idx); | ||
| 275 | geo_assert_not_reached; | ||
| 276 | } | ||
| 277 | |||
| 278 | /** | ||
| 279 | * \brief Gets the converted value. | ||
| 280 | * \return the converted value. | ||
| 281 | */ | ||
| 282 | operator T() const { | ||
| 283 | geo_assert_not_reached; | ||
| 284 | } | ||
| 285 | protected: | ||
| 286 | T x_; | ||
| 287 | }; | ||
| 288 | |||
| 289 | /** | ||
| 290 | * \brief lua_to specialization for int. | ||
| 291 | */ | ||
| 292 | template<> class lua_to<int> { | ||
| 293 | public: | ||
| 294 | lua_to(lua_State* L, int idx) { | ||
| 295 | ✗ | x_ = int(lua_tointeger(L,idx)); | |
| 296 | } | ||
| 297 | static bool can_convert(lua_State* L, int idx) { | ||
| 298 | ✗ | return lua_check_type(L, idx, lua_isinteger); | |
| 299 | } | ||
| 300 | operator int() const { | ||
| 301 | return x_; | ||
| 302 | } | ||
| 303 | private: | ||
| 304 | int x_; | ||
| 305 | }; | ||
| 306 | |||
| 307 | /** | ||
| 308 | * \brief lua_to specialization for Numeric::uint32. | ||
| 309 | */ | ||
| 310 | template<> class lua_to<Numeric::uint32> { | ||
| 311 | public: | ||
| 312 | lua_to(lua_State* L, int idx) { | ||
| 313 | x_ = Numeric::uint32(lua_tointeger(L,idx)); | ||
| 314 | } | ||
| 315 | static bool can_convert(lua_State* L, int idx) { | ||
| 316 | return lua_check_type(L, idx, my_lua_ispositiveinteger); | ||
| 317 | } | ||
| 318 | operator Numeric::uint32() const { | ||
| 319 | return x_; | ||
| 320 | } | ||
| 321 | private: | ||
| 322 | Numeric::uint32 x_; | ||
| 323 | }; | ||
| 324 | |||
| 325 | /** | ||
| 326 | * \brief lua_to specialization for Numeric::uint64. | ||
| 327 | */ | ||
| 328 | template<> class lua_to<Numeric::uint64> { | ||
| 329 | public: | ||
| 330 | lua_to(lua_State* L, int idx) { | ||
| 331 | ✗ | x_ = Numeric::uint64(lua_tointeger(L,idx)); | |
| 332 | } | ||
| 333 | static bool can_convert(lua_State* L, int idx) { | ||
| 334 | ✗ | return lua_check_type(L, idx, my_lua_ispositiveinteger); | |
| 335 | } | ||
| 336 | operator Numeric::uint64() const { | ||
| 337 | return x_; | ||
| 338 | } | ||
| 339 | private: | ||
| 340 | Numeric::uint64 x_; | ||
| 341 | }; | ||
| 342 | |||
| 343 | |||
| 344 | /** | ||
| 345 | * \brief lua_to specialization for Numeric::int64. | ||
| 346 | */ | ||
| 347 | template<> class lua_to<Numeric::int64> { | ||
| 348 | public: | ||
| 349 | lua_to(lua_State* L, int idx) { | ||
| 350 | x_ = Numeric::int64(lua_tointeger(L,idx)); | ||
| 351 | } | ||
| 352 | static bool can_convert(lua_State* L, int idx) { | ||
| 353 | return lua_check_type(L, idx, lua_isinteger); | ||
| 354 | } | ||
| 355 | operator Numeric::int64() const { | ||
| 356 | return x_; | ||
| 357 | } | ||
| 358 | private: | ||
| 359 | Numeric::int64 x_; | ||
| 360 | }; | ||
| 361 | |||
| 362 | /** | ||
| 363 | * \brief lua_to specialization for float. | ||
| 364 | */ | ||
| 365 | template<> class lua_to<float> { | ||
| 366 | public: | ||
| 367 | lua_to(lua_State* L, int idx) { | ||
| 368 | ✗ | x_ = float(lua_tonumber(L,idx)); | |
| 369 | } | ||
| 370 | static bool can_convert(lua_State* L, int idx) { | ||
| 371 | ✗ | return lua_check_type(L, idx, lua_isnumber); | |
| 372 | } | ||
| 373 | operator float() const { | ||
| 374 | return x_; | ||
| 375 | } | ||
| 376 | private: | ||
| 377 | float x_; | ||
| 378 | }; | ||
| 379 | |||
| 380 | /** | ||
| 381 | * \brief lua_to specialization for double. | ||
| 382 | */ | ||
| 383 | template<> class lua_to<double> { | ||
| 384 | public: | ||
| 385 | lua_to(lua_State* L, int idx) { | ||
| 386 | ✗ | x_ = double(lua_tonumber(L,idx)); | |
| 387 | } | ||
| 388 | static bool can_convert(lua_State* L, int idx) { | ||
| 389 | ✗ | return lua_check_type(L, idx, lua_isnumber); | |
| 390 | } | ||
| 391 | operator double() const { | ||
| 392 | return x_; | ||
| 393 | } | ||
| 394 | private: | ||
| 395 | double x_; | ||
| 396 | }; | ||
| 397 | |||
| 398 | /** | ||
| 399 | * \brief lua_to specialization for bool. | ||
| 400 | */ | ||
| 401 | template<> class lua_to<bool> { | ||
| 402 | public: | ||
| 403 | lua_to(lua_State* L, int idx) { | ||
| 404 | ✗ | x_ = (lua_toboolean(L,idx) != 0); | |
| 405 | } | ||
| 406 | static bool can_convert(lua_State* L, int idx) { | ||
| 407 | ✗ | return lua_check_type(L, idx, my_lua_isboolean); | |
| 408 | } | ||
| 409 | operator bool() const { | ||
| 410 | return x_; | ||
| 411 | } | ||
| 412 | private: | ||
| 413 | bool x_; | ||
| 414 | }; | ||
| 415 | |||
| 416 | /** | ||
| 417 | * \brief lua_to specialization for raw string (const char*). | ||
| 418 | */ | ||
| 419 | template<> class lua_to<const char*> { | ||
| 420 | public: | ||
| 421 | lua_to(lua_State* L, int idx) { | ||
| 422 | x_ = lua_tostring(L,idx); | ||
| 423 | } | ||
| 424 | static bool can_convert(lua_State* L, int idx) { | ||
| 425 | return lua_check_type(L, idx, lua_isstring); | ||
| 426 | } | ||
| 427 | operator const char*() const { | ||
| 428 | return x_; | ||
| 429 | } | ||
| 430 | private: | ||
| 431 | const char* x_; | ||
| 432 | }; | ||
| 433 | |||
| 434 | /** | ||
| 435 | * \brief lua_to specialization for reference to std::string. | ||
| 436 | */ | ||
| 437 | ✗ | template<> class lua_to<const std::string&> { | |
| 438 | public: | ||
| 439 | ✗ | lua_to(lua_State* L, int idx) { | |
| 440 | ✗ | x_ = lua_tostring(L,idx); | |
| 441 | ✗ | } | |
| 442 | static bool can_convert(lua_State* L, int idx) { | ||
| 443 | ✗ | return lua_check_type(L, idx, lua_isstring); | |
| 444 | } | ||
| 445 | operator const std::string&() const { | ||
| 446 | return x_; | ||
| 447 | } | ||
| 448 | private: | ||
| 449 | std::string x_; | ||
| 450 | }; | ||
| 451 | |||
| 452 | /** | ||
| 453 | * \brief lua_to specialization for std::string. | ||
| 454 | */ | ||
| 455 | template<> class lua_to<std::string> { | ||
| 456 | public: | ||
| 457 | lua_to(lua_State* L, int idx) { | ||
| 458 | x_ = lua_tostring(L,idx); | ||
| 459 | } | ||
| 460 | static bool can_convert(lua_State* L, int idx) { | ||
| 461 | return lua_check_type(L, idx, lua_isstring); | ||
| 462 | } | ||
| 463 | operator std::string() const { | ||
| 464 | return x_; | ||
| 465 | } | ||
| 466 | private: | ||
| 467 | std::string x_; | ||
| 468 | }; | ||
| 469 | |||
| 470 | |||
| 471 | /** | ||
| 472 | * \brief lua_to specialization for vec. | ||
| 473 | */ | ||
| 474 | template<class T, unsigned int N> class lua_to< const vecng<N,T>& > { | ||
| 475 | public: | ||
| 476 | lua_to(lua_State* L, int idx) { | ||
| 477 | lua_tovec(L, idx, x_); | ||
| 478 | } | ||
| 479 | static bool can_convert(lua_State* L, int idx) { | ||
| 480 | vecng<N,T> x; | ||
| 481 | return lua_tovec(L, idx, x); | ||
| 482 | } | ||
| 483 | operator vecng<N,T>() const { | ||
| 484 | return x_; | ||
| 485 | } | ||
| 486 | private: | ||
| 487 | vecng<N,T> x_; | ||
| 488 | }; | ||
| 489 | |||
| 490 | /** | ||
| 491 | * \brief lua_to specialization for mat. | ||
| 492 | */ | ||
| 493 | template<class T, unsigned int N> class lua_to< const Matrix<N,T>& > { | ||
| 494 | public: | ||
| 495 | lua_to(lua_State* L, int idx) { | ||
| 496 | lua_tomat(L, idx, x_); | ||
| 497 | } | ||
| 498 | static bool can_convert(lua_State* L, int idx) { | ||
| 499 | Matrix<N,T> x; | ||
| 500 | return lua_tomat(L, idx, x); | ||
| 501 | } | ||
| 502 | operator Matrix<N,T>() const { | ||
| 503 | return x_; | ||
| 504 | } | ||
| 505 | private: | ||
| 506 | Matrix<N,T> x_; | ||
| 507 | }; | ||
| 508 | |||
| 509 | /**********************************************************************/ | ||
| 510 | |||
| 511 | /** | ||
| 512 | * \brief Converts and pushes a C++ variable onto the LUA stack. | ||
| 513 | * \details This version is a placeholder. The actual implementation | ||
| 514 | * is done in the specializations. | ||
| 515 | * \note Just using function overloading | ||
| 516 | * (instead of template partializations) works with gcc, | ||
| 517 | * but does not work with clang and MSVC. | ||
| 518 | * \param[in] L a pointer to the LUA state. | ||
| 519 | * \param[in] x the variable to be pushed. | ||
| 520 | */ | ||
| 521 | ✗ | template<class T> inline void lua_push(lua_State* L, T x) { | |
| 522 | geo_argused(L); | ||
| 523 | geo_argused(x); | ||
| 524 | ✗ | geo_assert_not_reached; | |
| 525 | } | ||
| 526 | |||
| 527 | /** | ||
| 528 | * \brief Specialization of lua_push() for int. | ||
| 529 | */ | ||
| 530 | template<> inline void lua_push(lua_State* L, int x) { | ||
| 531 | ✗ | lua_pushinteger(L,lua_Integer(x)); | |
| 532 | } | ||
| 533 | |||
| 534 | /** | ||
| 535 | * \brief Specialization of lua_push() for Numeric::uint32. | ||
| 536 | */ | ||
| 537 | template<> inline void lua_push(lua_State* L, Numeric::uint32 x) { | ||
| 538 | lua_pushinteger(L,lua_Integer(x)); | ||
| 539 | } | ||
| 540 | |||
| 541 | /** | ||
| 542 | * \brief Specialization of lua_push() for Numeric::uint64. | ||
| 543 | */ | ||
| 544 | template<> inline void lua_push(lua_State* L, Numeric::uint64 x) { | ||
| 545 | ✗ | lua_pushinteger(L,lua_Integer(x)); | |
| 546 | } | ||
| 547 | |||
| 548 | /** | ||
| 549 | * \brief Specialization of lua_push() for Numeric::int64. | ||
| 550 | */ | ||
| 551 | template<> inline void lua_push(lua_State* L, Numeric::int64 x) { | ||
| 552 | lua_pushinteger(L,lua_Integer(x)); | ||
| 553 | } | ||
| 554 | |||
| 555 | /** | ||
| 556 | * \brief Specialization of lua_push() for float. | ||
| 557 | */ | ||
| 558 | template<> inline void lua_push(lua_State* L, float x) { | ||
| 559 | ✗ | lua_pushnumber(L,lua_Number(x)); | |
| 560 | } | ||
| 561 | |||
| 562 | /** | ||
| 563 | * \brief Specialization of lua_push() for double. | ||
| 564 | */ | ||
| 565 | template<> inline void lua_push(lua_State* L, double x) { | ||
| 566 | lua_pushnumber(L,lua_Number(x)); | ||
| 567 | } | ||
| 568 | |||
| 569 | /** | ||
| 570 | * \brief Specialization of lua_push() for bool. | ||
| 571 | */ | ||
| 572 | template<> inline void lua_push(lua_State* L, bool x) { | ||
| 573 | ✗ | lua_pushboolean(L,x?1:0); | |
| 574 | } | ||
| 575 | |||
| 576 | /** | ||
| 577 | * \brief Specialization of lua_push() for raw string (const char*). | ||
| 578 | */ | ||
| 579 | template<> inline void lua_push(lua_State* L, const char* x) { | ||
| 580 | ✗ | lua_pushstring(L,x); | |
| 581 | } | ||
| 582 | |||
| 583 | /** | ||
| 584 | * \brief Specialization of lua_push() for reference to std::string. | ||
| 585 | */ | ||
| 586 | template<> inline void lua_push(lua_State* L, const std::string& x) { | ||
| 587 | lua_pushstring(L,x.c_str()); | ||
| 588 | } | ||
| 589 | |||
| 590 | /** | ||
| 591 | * \brief Specialization of lua_push() for std::string. | ||
| 592 | */ | ||
| 593 | template<> inline void lua_push(lua_State* L, std::string x) { | ||
| 594 | ✗ | lua_pushstring(L,x.c_str()); | |
| 595 | ✗ | } | |
| 596 | |||
| 597 | /** | ||
| 598 | * \brief Specialization of lua_push() for vectors. | ||
| 599 | * \details It pushes a table, indexed by integers, from 1 to n (rather than | ||
| 600 | * 0 to n-1, following LUA indexing convention). | ||
| 601 | */ | ||
| 602 | ✗ | template<class T> inline void lua_push( | |
| 603 | lua_State* L, const std::vector<T>& x | ||
| 604 | ) { | ||
| 605 | ✗ | lua_newtable(L); | |
| 606 | ✗ | for(size_t i=0; i<x.size(); ++i) { | |
| 607 | ✗ | lua_push(L,x[i]); | |
| 608 | ✗ | lua_seti(L,-2,lua_Integer(i+1)); | |
| 609 | } | ||
| 610 | ✗ | } | |
| 611 | |||
| 612 | /** | ||
| 613 | * \brief Declares a new enum type that can be used by LUA wrappers. | ||
| 614 | * \details enum types that can be used in wrapped functions need to be | ||
| 615 | * explicitely declared before using lua_bindwrapper() and | ||
| 616 | * lua_bindwrapperglobal(). This will be no longer the case when we will | ||
| 617 | * switch to C++11 (but for now, geogram needs to remain compatible with | ||
| 618 | * C++98). | ||
| 619 | * \note LUA_DECLAREENUMTYPE cannot be done from within a function. | ||
| 620 | * \param[in] T the C++ type name of the enum. | ||
| 621 | */ | ||
| 622 | #define LUA_DECLAREENUMTYPE(T) \ | ||
| 623 | template<> inline void lua_push(lua_State* L, T x) { \ | ||
| 624 | lua_push(L, int(x)); \ | ||
| 625 | } \ | ||
| 626 | \ | ||
| 627 | template<> class lua_to<T> : public GEO::lua_to<int> { \ | ||
| 628 | public: \ | ||
| 629 | lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \ | ||
| 630 | } \ | ||
| 631 | operator T() const { \ | ||
| 632 | return T(lua_to<int>::operator int()); \ | ||
| 633 | } \ | ||
| 634 | } | ||
| 635 | |||
| 636 | /**********************************************************************/ | ||
| 637 | |||
| 638 | /** | ||
| 639 | * \brief Calls a C++ function from LUA. | ||
| 640 | * \details The arguments are converted from the LUA stack, and the | ||
| 641 | * return value pushed onto the LUA stack. Whenever an error occurs, | ||
| 642 | * (invalid number of arguments or type error), it is captured and | ||
| 643 | * an error message is returned to the caller. | ||
| 644 | */ | ||
| 645 | ✗ | template <class R> inline int lua_wrap(lua_State* L, R (*fptr)(void)) { | |
| 646 | ✗ | if(!lua_check_nb_args(L,0)) { | |
| 647 | ✗ | return lua_notify_last_error(L); | |
| 648 | } | ||
| 649 | ✗ | R retval = fptr(); | |
| 650 | ✗ | lua_push(L,retval); | |
| 651 | ✗ | return 1; | |
| 652 | } | ||
| 653 | |||
| 654 | /** | ||
| 655 | * \brief Calls a C++ function from LUA. | ||
| 656 | * \details The arguments are converted from the LUA stack, and the | ||
| 657 | * return value pushed onto the LUA stack. Whenever an error occurs, | ||
| 658 | * (invalid number of arguments or type error), it is captured and | ||
| 659 | * an error message is returned to the caller. | ||
| 660 | */ | ||
| 661 | ✗ | template <class R, class T1> inline int lua_wrap( | |
| 662 | lua_State* L, R (*fptr)(T1) | ||
| 663 | ) { | ||
| 664 | if( | ||
| 665 | ✗ | !lua_check_nb_args(L,1) || | |
| 666 | !lua_to<T1>::can_convert(L,1) | ||
| 667 | ) { | ||
| 668 | ✗ | return lua_notify_last_error(L); | |
| 669 | } | ||
| 670 | ✗ | R retval = fptr( | |
| 671 | ✗ | lua_to<T1>(L,1) | |
| 672 | ); | ||
| 673 | ✗ | lua_push(L,retval); | |
| 674 | ✗ | return 1; | |
| 675 | ✗ | } | |
| 676 | |||
| 677 | /** | ||
| 678 | * \brief Calls a C++ function from LUA. | ||
| 679 | * \details The arguments are converted from the LUA stack, and the | ||
| 680 | * return value pushed onto the LUA stack. Whenever an error occurs, | ||
| 681 | * (invalid number of arguments or type error), it is captured and | ||
| 682 | * an error message is returned to the caller. | ||
| 683 | */ | ||
| 684 | ✗ | template <class R, class T1, class T2> inline int lua_wrap( | |
| 685 | lua_State* L, R (*fptr)(T1,T2) | ||
| 686 | ) { | ||
| 687 | if( | ||
| 688 | ✗ | !lua_check_nb_args(L,2) || | |
| 689 | ✗ | !lua_to<T1>::can_convert(L,1) || | |
| 690 | !lua_to<T2>::can_convert(L,2) | ||
| 691 | ) { | ||
| 692 | ✗ | return lua_notify_last_error(L); | |
| 693 | } | ||
| 694 | ✗ | R retval = fptr( | |
| 695 | ✗ | lua_to<T1>(L,1), | |
| 696 | ✗ | lua_to<T2>(L,2) | |
| 697 | ); | ||
| 698 | ✗ | lua_push(L,retval); | |
| 699 | ✗ | return 1; | |
| 700 | } | ||
| 701 | |||
| 702 | /** | ||
| 703 | * \brief Calls a C++ function from LUA. | ||
| 704 | * \details The arguments are converted from the LUA stack, and the | ||
| 705 | * return value pushed onto the LUA stack. Whenever an error occurs, | ||
| 706 | * (invalid number of arguments or type error), it is captured and | ||
| 707 | * an error message is returned to the caller. | ||
| 708 | */ | ||
| 709 | template <class R, class T1, class T2, class T3> inline int lua_wrap( | ||
| 710 | lua_State* L, R (*fptr)(T1,T2,T3) | ||
| 711 | ) { | ||
| 712 | if( | ||
| 713 | !lua_check_nb_args(L,3) || | ||
| 714 | !lua_to<T1>::can_convert(L,1) || | ||
| 715 | !lua_to<T2>::can_convert(L,2) || | ||
| 716 | !lua_to<T3>::can_convert(L,3) | ||
| 717 | ) { | ||
| 718 | return lua_notify_last_error(L); | ||
| 719 | } | ||
| 720 | R retval = fptr( | ||
| 721 | lua_to<T1>(L,1), | ||
| 722 | lua_to<T2>(L,2), | ||
| 723 | lua_to<T3>(L,3) | ||
| 724 | ); | ||
| 725 | lua_push(L,retval); | ||
| 726 | return 1; | ||
| 727 | } | ||
| 728 | |||
| 729 | /** | ||
| 730 | * \brief Calls a C++ function from LUA. | ||
| 731 | * \details The arguments are converted from the LUA stack, and the | ||
| 732 | * return value pushed onto the LUA stack. Whenever an error occurs, | ||
| 733 | * (invalid number of arguments or type error), it is captured and | ||
| 734 | * an error message is returned to the caller. | ||
| 735 | */ | ||
| 736 | template <class R, class T1, class T2, class T3, class T4> | ||
| 737 | inline int lua_wrap(lua_State* L, R (*fptr)(T1,T2,T3,T4)) { | ||
| 738 | if( | ||
| 739 | !lua_check_nb_args(L,4) || | ||
| 740 | !lua_to<T1>::can_convert(L,1) || | ||
| 741 | !lua_to<T2>::can_convert(L,2) || | ||
| 742 | !lua_to<T3>::can_convert(L,3) || | ||
| 743 | !lua_to<T4>::can_convert(L,4) | ||
| 744 | ) { | ||
| 745 | return lua_notify_last_error(L); | ||
| 746 | } | ||
| 747 | R retval = fptr( | ||
| 748 | lua_to<T1>(L,1), | ||
| 749 | lua_to<T2>(L,2), | ||
| 750 | lua_to<T3>(L,3), | ||
| 751 | lua_to<T4>(L,4) | ||
| 752 | ); | ||
| 753 | lua_push(L,retval); | ||
| 754 | return 1; | ||
| 755 | } | ||
| 756 | |||
| 757 | /*************************************************************************/ | ||
| 758 | |||
| 759 | /** | ||
| 760 | * \brief Calls a C++ function from LUA. | ||
| 761 | * \details The arguments are converted from the LUA stack. | ||
| 762 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 763 | * it is captured and an error message is returned to the caller. | ||
| 764 | */ | ||
| 765 | ✗ | template <> inline int lua_wrap(lua_State* L, void (*fptr)(void)) { | |
| 766 | ✗ | if(!lua_check_nb_args(L,0)) { | |
| 767 | ✗ | return lua_notify_last_error(L); | |
| 768 | } | ||
| 769 | ✗ | fptr(); | |
| 770 | ✗ | return 0; | |
| 771 | } | ||
| 772 | |||
| 773 | /** | ||
| 774 | * \brief Calls a C++ function from LUA. | ||
| 775 | * \details The arguments are converted from the LUA stack. | ||
| 776 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 777 | * it is captured and an error message is returned to the caller. | ||
| 778 | */ | ||
| 779 | ✗ | template <class T1> inline int lua_wrap(lua_State* L, void (*fptr)(T1)) { | |
| 780 | if( | ||
| 781 | ✗ | !lua_check_nb_args(L,1) || | |
| 782 | !lua_to<T1>::can_convert(L,1) | ||
| 783 | ) { | ||
| 784 | ✗ | return lua_notify_last_error(L); | |
| 785 | } | ||
| 786 | ✗ | fptr( | |
| 787 | lua_to<T1>(L,1) | ||
| 788 | ); | ||
| 789 | return 0; | ||
| 790 | } | ||
| 791 | |||
| 792 | /** | ||
| 793 | * \brief Calls a C++ function from LUA. | ||
| 794 | * \details The arguments are converted from the LUA stack. | ||
| 795 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 796 | * it is captured and an error message is returned to the caller. | ||
| 797 | */ | ||
| 798 | template <class T1, class T2> inline int lua_wrap( | ||
| 799 | lua_State* L, void (*fptr)(T1,T2) | ||
| 800 | ) { | ||
| 801 | if( | ||
| 802 | !lua_check_nb_args(L,2) || | ||
| 803 | !lua_to<T1>::can_convert(L,1) || | ||
| 804 | !lua_to<T2>::can_convert(L,2) | ||
| 805 | ) { | ||
| 806 | return lua_notify_last_error(L); | ||
| 807 | } | ||
| 808 | fptr( | ||
| 809 | lua_to<T1>(L,1), | ||
| 810 | lua_to<T2>(L,2) | ||
| 811 | ); | ||
| 812 | return 0; | ||
| 813 | } | ||
| 814 | |||
| 815 | /** | ||
| 816 | * \brief Calls a C++ function from LUA. | ||
| 817 | * \details The arguments are converted from the LUA stack. | ||
| 818 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 819 | * it is captured and an error message is returned to the caller. | ||
| 820 | */ | ||
| 821 | template <class T1, class T2, class T3> | ||
| 822 | ✗ | inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3)) { | |
| 823 | if( | ||
| 824 | ✗ | !lua_check_nb_args(L,3) || | |
| 825 | ✗ | !lua_to<T1>::can_convert(L,1) || | |
| 826 | ✗ | !lua_to<T2>::can_convert(L,2) || | |
| 827 | !lua_to<T3>::can_convert(L,3) | ||
| 828 | ) { | ||
| 829 | ✗ | return lua_notify_last_error(L); | |
| 830 | } | ||
| 831 | ✗ | fptr( | |
| 832 | lua_to<T1>(L,1), | ||
| 833 | lua_to<T2>(L,2), | ||
| 834 | lua_to<T3>(L,3) | ||
| 835 | ); | ||
| 836 | return 0; | ||
| 837 | } | ||
| 838 | |||
| 839 | /** | ||
| 840 | * \brief Calls a C++ function from LUA. | ||
| 841 | * \details The arguments are converted from the LUA stack. | ||
| 842 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 843 | * it is captured and an error message is returned to the caller. | ||
| 844 | */ | ||
| 845 | template <class T1, class T2, class T3, class T4> | ||
| 846 | ✗ | inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3,T4)) { | |
| 847 | if( | ||
| 848 | ✗ | !lua_check_nb_args(L,4) || | |
| 849 | ✗ | !lua_to<T1>::can_convert(L,1) || | |
| 850 | ✗ | !lua_to<T2>::can_convert(L,2) || | |
| 851 | ✗ | !lua_to<T3>::can_convert(L,3) || | |
| 852 | !lua_to<T4>::can_convert(L,4) | ||
| 853 | ) { | ||
| 854 | ✗ | return lua_notify_last_error(L); | |
| 855 | } | ||
| 856 | ✗ | fptr( | |
| 857 | lua_to<T1>(L,1), | ||
| 858 | lua_to<T2>(L,2), | ||
| 859 | lua_to<T3>(L,3), | ||
| 860 | lua_to<T4>(L,4) | ||
| 861 | ); | ||
| 862 | return 0; | ||
| 863 | } | ||
| 864 | |||
| 865 | /** | ||
| 866 | * \brief Calls a C++ function from LUA. | ||
| 867 | * \details The arguments are converted from the LUA stack. | ||
| 868 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 869 | * it is captured and an error message is returned to the caller. | ||
| 870 | */ | ||
| 871 | template < | ||
| 872 | class T1, class T2, class T3, class T4, class T5 | ||
| 873 | > | ||
| 874 | inline int lua_wrap( | ||
| 875 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5) | ||
| 876 | ) { | ||
| 877 | if( | ||
| 878 | !lua_check_nb_args(L,5) || | ||
| 879 | !lua_to<T1>::can_convert(L,1) || | ||
| 880 | !lua_to<T2>::can_convert(L,2) || | ||
| 881 | !lua_to<T3>::can_convert(L,3) || | ||
| 882 | !lua_to<T4>::can_convert(L,4) || | ||
| 883 | !lua_to<T5>::can_convert(L,5) | ||
| 884 | ) { | ||
| 885 | return lua_notify_last_error(L); | ||
| 886 | } | ||
| 887 | fptr( | ||
| 888 | lua_to<T1>(L,1), | ||
| 889 | lua_to<T2>(L,2), | ||
| 890 | lua_to<T3>(L,3), | ||
| 891 | lua_to<T4>(L,4), | ||
| 892 | lua_to<T5>(L,5) | ||
| 893 | ); | ||
| 894 | return 0; | ||
| 895 | } | ||
| 896 | |||
| 897 | /** | ||
| 898 | * \brief Calls a C++ function from LUA. | ||
| 899 | * \details The arguments are converted from the LUA stack. | ||
| 900 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 901 | * it is captured and an error message is returned to the caller. | ||
| 902 | */ | ||
| 903 | template < | ||
| 904 | class T1, class T2, class T3, class T4, class T5, class T6 | ||
| 905 | > | ||
| 906 | inline int lua_wrap( | ||
| 907 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6) | ||
| 908 | ) { | ||
| 909 | if( | ||
| 910 | !lua_check_nb_args(L,6) || | ||
| 911 | !lua_to<T1>::can_convert(L,1) || | ||
| 912 | !lua_to<T2>::can_convert(L,2) || | ||
| 913 | !lua_to<T3>::can_convert(L,3) || | ||
| 914 | !lua_to<T4>::can_convert(L,4) || | ||
| 915 | !lua_to<T5>::can_convert(L,5) || | ||
| 916 | !lua_to<T6>::can_convert(L,6) | ||
| 917 | ) { | ||
| 918 | return lua_notify_last_error(L); | ||
| 919 | } | ||
| 920 | fptr( | ||
| 921 | lua_to<T1>(L,1), | ||
| 922 | lua_to<T2>(L,2), | ||
| 923 | lua_to<T3>(L,3), | ||
| 924 | lua_to<T4>(L,4), | ||
| 925 | lua_to<T5>(L,5), | ||
| 926 | lua_to<T6>(L,6) | ||
| 927 | ); | ||
| 928 | return 0; | ||
| 929 | } | ||
| 930 | |||
| 931 | /** | ||
| 932 | * \brief Calls a C++ function from LUA. | ||
| 933 | * \details The arguments are converted from the LUA stack. | ||
| 934 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 935 | * it is captured and an error message is returned to the caller. | ||
| 936 | */ | ||
| 937 | template < | ||
| 938 | class T1, class T2, class T3, class T4, class T5, | ||
| 939 | class T6, class T7 | ||
| 940 | > | ||
| 941 | inline int lua_wrap( | ||
| 942 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7) | ||
| 943 | ) { | ||
| 944 | if( | ||
| 945 | !lua_check_nb_args(L,7) || | ||
| 946 | !lua_to<T1>::can_convert(L,1) || | ||
| 947 | !lua_to<T2>::can_convert(L,2) || | ||
| 948 | !lua_to<T3>::can_convert(L,3) || | ||
| 949 | !lua_to<T4>::can_convert(L,4) || | ||
| 950 | !lua_to<T5>::can_convert(L,5) || | ||
| 951 | !lua_to<T6>::can_convert(L,6) || | ||
| 952 | !lua_to<T7>::can_convert(L,7) | ||
| 953 | ) { | ||
| 954 | return lua_notify_last_error(L); | ||
| 955 | } | ||
| 956 | fptr( | ||
| 957 | lua_to<T1>(L,1), | ||
| 958 | lua_to<T2>(L,2), | ||
| 959 | lua_to<T3>(L,3), | ||
| 960 | lua_to<T4>(L,4), | ||
| 961 | lua_to<T5>(L,5), | ||
| 962 | lua_to<T6>(L,6), | ||
| 963 | lua_to<T7>(L,7) | ||
| 964 | ); | ||
| 965 | return 0; | ||
| 966 | } | ||
| 967 | |||
| 968 | |||
| 969 | /** | ||
| 970 | * \brief Calls a C++ function from LUA. | ||
| 971 | * \details The arguments are converted from the LUA stack. | ||
| 972 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 973 | * it is captured and an error message is returned to the caller. | ||
| 974 | */ | ||
| 975 | template < | ||
| 976 | class T1, class T2, class T3, class T4, class T5, | ||
| 977 | class T6, class T7, class T8 | ||
| 978 | > | ||
| 979 | inline int lua_wrap( | ||
| 980 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8) | ||
| 981 | ) { | ||
| 982 | if( | ||
| 983 | !lua_check_nb_args(L,8) || | ||
| 984 | !lua_to<T1>::can_convert(L,1) || | ||
| 985 | !lua_to<T2>::can_convert(L,2) || | ||
| 986 | !lua_to<T3>::can_convert(L,3) || | ||
| 987 | !lua_to<T4>::can_convert(L,4) || | ||
| 988 | !lua_to<T5>::can_convert(L,5) || | ||
| 989 | !lua_to<T6>::can_convert(L,6) || | ||
| 990 | !lua_to<T7>::can_convert(L,7) || | ||
| 991 | !lua_to<T8>::can_convert(L,8) | ||
| 992 | ) { | ||
| 993 | return lua_notify_last_error(L); | ||
| 994 | } | ||
| 995 | fptr( | ||
| 996 | lua_to<T1>(L,1), | ||
| 997 | lua_to<T2>(L,2), | ||
| 998 | lua_to<T3>(L,3), | ||
| 999 | lua_to<T4>(L,4), | ||
| 1000 | lua_to<T5>(L,5), | ||
| 1001 | lua_to<T6>(L,6), | ||
| 1002 | lua_to<T7>(L,7), | ||
| 1003 | lua_to<T8>(L,8) | ||
| 1004 | ); | ||
| 1005 | return 0; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | |||
| 1009 | /** | ||
| 1010 | * \brief Calls a C++ function from LUA. | ||
| 1011 | * \details The arguments are converted from the LUA stack. | ||
| 1012 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 1013 | * it is captured and an error message is returned to the caller. | ||
| 1014 | */ | ||
| 1015 | template < | ||
| 1016 | class T1, class T2, class T3, class T4, class T5, | ||
| 1017 | class T6, class T7, class T8, class T9 | ||
| 1018 | > | ||
| 1019 | inline int lua_wrap( | ||
| 1020 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9) | ||
| 1021 | ) { | ||
| 1022 | if( | ||
| 1023 | !lua_check_nb_args(L,9) || | ||
| 1024 | !lua_to<T1>::can_convert(L,1) || | ||
| 1025 | !lua_to<T2>::can_convert(L,2) || | ||
| 1026 | !lua_to<T3>::can_convert(L,3) || | ||
| 1027 | !lua_to<T4>::can_convert(L,4) || | ||
| 1028 | !lua_to<T5>::can_convert(L,5) || | ||
| 1029 | !lua_to<T6>::can_convert(L,6) || | ||
| 1030 | !lua_to<T7>::can_convert(L,7) || | ||
| 1031 | !lua_to<T8>::can_convert(L,8) || | ||
| 1032 | !lua_to<T9>::can_convert(L,9) | ||
| 1033 | ) { | ||
| 1034 | return lua_notify_last_error(L); | ||
| 1035 | } | ||
| 1036 | fptr( | ||
| 1037 | lua_to<T1>(L,1), | ||
| 1038 | lua_to<T2>(L,2), | ||
| 1039 | lua_to<T3>(L,3), | ||
| 1040 | lua_to<T4>(L,4), | ||
| 1041 | lua_to<T5>(L,5), | ||
| 1042 | lua_to<T6>(L,6), | ||
| 1043 | lua_to<T7>(L,7), | ||
| 1044 | lua_to<T8>(L,8), | ||
| 1045 | lua_to<T9>(L,9) | ||
| 1046 | ); | ||
| 1047 | return 0; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | |||
| 1051 | /** | ||
| 1052 | * \brief Calls a C++ function from LUA. | ||
| 1053 | * \details The arguments are converted from the LUA stack. | ||
| 1054 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 1055 | * it is captured and an error message is returned to the caller. | ||
| 1056 | */ | ||
| 1057 | template < | ||
| 1058 | class T1, class T2, class T3, class T4, class T5, | ||
| 1059 | class T6, class T7, class T8, class T9, class T10 | ||
| 1060 | > | ||
| 1061 | inline int lua_wrap( | ||
| 1062 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10) | ||
| 1063 | ) { | ||
| 1064 | if( | ||
| 1065 | !lua_check_nb_args(L,10) || | ||
| 1066 | !lua_to<T1>::can_convert(L,1) || | ||
| 1067 | !lua_to<T2>::can_convert(L,2) || | ||
| 1068 | !lua_to<T3>::can_convert(L,3) || | ||
| 1069 | !lua_to<T4>::can_convert(L,4) || | ||
| 1070 | !lua_to<T5>::can_convert(L,5) || | ||
| 1071 | !lua_to<T6>::can_convert(L,6) || | ||
| 1072 | !lua_to<T7>::can_convert(L,7) || | ||
| 1073 | !lua_to<T8>::can_convert(L,8) || | ||
| 1074 | !lua_to<T9>::can_convert(L,9) || | ||
| 1075 | !lua_to<T10>::can_convert(L,10) | ||
| 1076 | ) { | ||
| 1077 | return lua_notify_last_error(L); | ||
| 1078 | } | ||
| 1079 | fptr( | ||
| 1080 | lua_to<T1>(L,1), | ||
| 1081 | lua_to<T2>(L,2), | ||
| 1082 | lua_to<T3>(L,3), | ||
| 1083 | lua_to<T4>(L,4), | ||
| 1084 | lua_to<T5>(L,5), | ||
| 1085 | lua_to<T6>(L,6), | ||
| 1086 | lua_to<T7>(L,7), | ||
| 1087 | lua_to<T8>(L,8), | ||
| 1088 | lua_to<T9>(L,9), | ||
| 1089 | lua_to<T10>(L,10) | ||
| 1090 | ); | ||
| 1091 | return 0; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | |||
| 1095 | /** | ||
| 1096 | * \brief Calls a C++ function from LUA. | ||
| 1097 | * \details The arguments are converted from the LUA stack. | ||
| 1098 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 1099 | * it is captured and an error message is returned to the caller. | ||
| 1100 | */ | ||
| 1101 | template < | ||
| 1102 | class T1, class T2, class T3, class T4, class T5, | ||
| 1103 | class T6, class T7, class T8, class T9, class T10, | ||
| 1104 | class T11 | ||
| 1105 | > | ||
| 1106 | inline int lua_wrap( | ||
| 1107 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11) | ||
| 1108 | ) { | ||
| 1109 | if( | ||
| 1110 | !lua_check_nb_args(L,11) || | ||
| 1111 | !lua_to<T1>::can_convert(L,1) || | ||
| 1112 | !lua_to<T2>::can_convert(L,2) || | ||
| 1113 | !lua_to<T3>::can_convert(L,3) || | ||
| 1114 | !lua_to<T4>::can_convert(L,4) || | ||
| 1115 | !lua_to<T5>::can_convert(L,5) || | ||
| 1116 | !lua_to<T6>::can_convert(L,6) || | ||
| 1117 | !lua_to<T7>::can_convert(L,7) || | ||
| 1118 | !lua_to<T8>::can_convert(L,8) || | ||
| 1119 | !lua_to<T9>::can_convert(L,9) || | ||
| 1120 | !lua_to<T10>::can_convert(L,10) || | ||
| 1121 | !lua_to<T11>::can_convert(L,11) | ||
| 1122 | ) { | ||
| 1123 | return lua_notify_last_error(L); | ||
| 1124 | } | ||
| 1125 | fptr( | ||
| 1126 | lua_to<T1>(L,1), | ||
| 1127 | lua_to<T2>(L,2), | ||
| 1128 | lua_to<T3>(L,3), | ||
| 1129 | lua_to<T4>(L,4), | ||
| 1130 | lua_to<T5>(L,5), | ||
| 1131 | lua_to<T6>(L,6), | ||
| 1132 | lua_to<T7>(L,7), | ||
| 1133 | lua_to<T8>(L,8), | ||
| 1134 | lua_to<T9>(L,9), | ||
| 1135 | lua_to<T10>(L,10), | ||
| 1136 | lua_to<T11>(L,11) | ||
| 1137 | ); | ||
| 1138 | return 0; | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | /** | ||
| 1142 | * \brief Calls a C++ function from LUA. | ||
| 1143 | * \details The arguments are converted from the LUA stack. | ||
| 1144 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 1145 | * it is captured and an error message is returned to the caller. | ||
| 1146 | */ | ||
| 1147 | template < | ||
| 1148 | class T1, class T2, class T3, class T4, class T5, | ||
| 1149 | class T6, class T7, class T8, class T9, class T10, | ||
| 1150 | class T11, class T12 | ||
| 1151 | > | ||
| 1152 | inline int lua_wrap( | ||
| 1153 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12) | ||
| 1154 | ) { | ||
| 1155 | if( | ||
| 1156 | !lua_check_nb_args(L,12) || | ||
| 1157 | !lua_to<T1>::can_convert(L,1) || | ||
| 1158 | !lua_to<T2>::can_convert(L,2) || | ||
| 1159 | !lua_to<T3>::can_convert(L,3) || | ||
| 1160 | !lua_to<T4>::can_convert(L,4) || | ||
| 1161 | !lua_to<T5>::can_convert(L,5) || | ||
| 1162 | !lua_to<T6>::can_convert(L,6) || | ||
| 1163 | !lua_to<T7>::can_convert(L,7) || | ||
| 1164 | !lua_to<T8>::can_convert(L,8) || | ||
| 1165 | !lua_to<T9>::can_convert(L,9) || | ||
| 1166 | !lua_to<T10>::can_convert(L,10) || | ||
| 1167 | !lua_to<T11>::can_convert(L,11) || | ||
| 1168 | !lua_to<T12>::can_convert(L,12) | ||
| 1169 | ) { | ||
| 1170 | return lua_notify_last_error(L); | ||
| 1171 | } | ||
| 1172 | fptr( | ||
| 1173 | lua_to<T1>(L,1), | ||
| 1174 | lua_to<T2>(L,2), | ||
| 1175 | lua_to<T3>(L,3), | ||
| 1176 | lua_to<T4>(L,4), | ||
| 1177 | lua_to<T5>(L,5), | ||
| 1178 | lua_to<T6>(L,6), | ||
| 1179 | lua_to<T7>(L,7), | ||
| 1180 | lua_to<T8>(L,8), | ||
| 1181 | lua_to<T9>(L,9), | ||
| 1182 | lua_to<T10>(L,10), | ||
| 1183 | lua_to<T11>(L,11), | ||
| 1184 | lua_to<T12>(L,12) | ||
| 1185 | ); | ||
| 1186 | return 0; | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | |||
| 1190 | /** | ||
| 1191 | * \brief Calls a C++ function from LUA. | ||
| 1192 | * \details The arguments are converted from the LUA stack. | ||
| 1193 | * Whenever an error occurs, (invalid number of arguments or type error), | ||
| 1194 | * it is captured and an error message is returned to the caller. | ||
| 1195 | */ | ||
| 1196 | template < | ||
| 1197 | class T1, class T2, class T3, class T4, class T5, | ||
| 1198 | class T6, class T7, class T8, class T9, class T10, | ||
| 1199 | class T11, class T12, class T13 | ||
| 1200 | > | ||
| 1201 | inline int lua_wrap( | ||
| 1202 | lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13) | ||
| 1203 | ) { | ||
| 1204 | if( | ||
| 1205 | !lua_check_nb_args(L,13) || | ||
| 1206 | !lua_to<T1>::can_convert(L,1) || | ||
| 1207 | !lua_to<T2>::can_convert(L,2) || | ||
| 1208 | !lua_to<T3>::can_convert(L,3) || | ||
| 1209 | !lua_to<T4>::can_convert(L,4) || | ||
| 1210 | !lua_to<T5>::can_convert(L,5) || | ||
| 1211 | !lua_to<T6>::can_convert(L,6) || | ||
| 1212 | !lua_to<T7>::can_convert(L,7) || | ||
| 1213 | !lua_to<T8>::can_convert(L,8) || | ||
| 1214 | !lua_to<T9>::can_convert(L,9) || | ||
| 1215 | !lua_to<T10>::can_convert(L,10) || | ||
| 1216 | !lua_to<T11>::can_convert(L,11) || | ||
| 1217 | !lua_to<T12>::can_convert(L,12) || | ||
| 1218 | !lua_to<T12>::can_convert(L,13) | ||
| 1219 | ) { | ||
| 1220 | return lua_notify_last_error(L); | ||
| 1221 | } | ||
| 1222 | fptr( | ||
| 1223 | lua_to<T1>(L,1), | ||
| 1224 | lua_to<T2>(L,2), | ||
| 1225 | lua_to<T3>(L,3), | ||
| 1226 | lua_to<T4>(L,4), | ||
| 1227 | lua_to<T5>(L,5), | ||
| 1228 | lua_to<T6>(L,6), | ||
| 1229 | lua_to<T7>(L,7), | ||
| 1230 | lua_to<T8>(L,8), | ||
| 1231 | lua_to<T9>(L,9), | ||
| 1232 | lua_to<T10>(L,10), | ||
| 1233 | lua_to<T11>(L,11), | ||
| 1234 | lua_to<T12>(L,12), | ||
| 1235 | lua_to<T13>(L,13) | ||
| 1236 | ); | ||
| 1237 | return 0; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | /** | ||
| 1241 | * \brief Specialization of the wrapper for functions that | ||
| 1242 | * are "already wrapped". | ||
| 1243 | * \note Normally not used, since there is a specialization of | ||
| 1244 | * lua_pushwrapper() for lua_CFunction. | ||
| 1245 | */ | ||
| 1246 | template<> inline int lua_wrap(lua_State* L, lua_CFunction fptr) { | ||
| 1247 | return fptr(L); | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | |||
| 1251 | /*************************************************************************/ | ||
| 1252 | |||
| 1253 | /** | ||
| 1254 | * \brief Manages wrappers around C++ functions to be called from LUA. | ||
| 1255 | * \details This class should not be used directly by client code. | ||
| 1256 | * Client code will rather use the high-level macro lua_bindwrapper() | ||
| 1257 | * or the lower-level functions lua_bindwrapperwithname() and | ||
| 1258 | * lua_pushwrapper(). | ||
| 1259 | */ | ||
| 1260 | template <class FPTR> class lua_wrapper { | ||
| 1261 | public: | ||
| 1262 | |||
| 1263 | /** | ||
| 1264 | * \brief Implementation of the wrapper. | ||
| 1265 | * \details This is the C functions to be declared to LUA. | ||
| 1266 | * It is typed by the signature of the wrapped C++ function, | ||
| 1267 | * and the pointer to the actual wrapped C++ function is stored | ||
| 1268 | * in an upvalue. | ||
| 1269 | * \param[in] L a pointer to the LUA state. | ||
| 1270 | */ | ||
| 1271 | ✗ | static int call(lua_State* L) { | |
| 1272 | ✗ | FPTR f = Memory::generic_pointer_to_function_pointer<FPTR>( | |
| 1273 | lua_touserdata(L, lua_upvalueindex(1)) | ||
| 1274 | ); | ||
| 1275 | ✗ | return lua_wrap(L, f); | |
| 1276 | } | ||
| 1277 | |||
| 1278 | /** | ||
| 1279 | * \brief Pushes a wrapper for a given C++ function onto the LUA stack. | ||
| 1280 | * \param[in] L a pointer to the LUA state. | ||
| 1281 | * \param[in] f a pointer to the C++ function to be pushed. It cannot be | ||
| 1282 | * a non-static object's member function. | ||
| 1283 | */ | ||
| 1284 | static void push(lua_State* L, FPTR f) { | ||
| 1285 | ✗ | lua_pushlightuserdata( | |
| 1286 | L, Memory::function_pointer_to_generic_pointer<FPTR>(f) | ||
| 1287 | ); | ||
| 1288 | ✗ | lua_pushcclosure(L, lua_wrapper<FPTR>::call, 1); | |
| 1289 | } | ||
| 1290 | }; | ||
| 1291 | |||
| 1292 | /**************************************************************************/ | ||
| 1293 | |||
| 1294 | /** | ||
| 1295 | * \brief Pushes a wrapper for a given C++ function onto the LUA stack. | ||
| 1296 | * \param[in] L a pointer to the LUA state. | ||
| 1297 | * \param[in] f a pointer to the C++ function to be pushed. It cannot be | ||
| 1298 | * a non-static object member function. | ||
| 1299 | */ | ||
| 1300 | template <class FPTR> inline void lua_pushwrapper(lua_State* L, FPTR f) { | ||
| 1301 | lua_wrapper<FPTR>::push(L,f); | ||
| 1302 | } | ||
| 1303 | |||
| 1304 | /** | ||
| 1305 | * \brief Specialization for lua_CFunction. | ||
| 1306 | * \details No need for a wrapper if it is already a LUA function. | ||
| 1307 | */ | ||
| 1308 | template<> inline void lua_pushwrapper(lua_State* L, lua_CFunction f) { | ||
| 1309 | ✗ | lua_pushcfunction(L,f); | |
| 1310 | } | ||
| 1311 | |||
| 1312 | /**************************************************************************/ | ||
| 1313 | |||
| 1314 | /** | ||
| 1315 | * \brief Binds a wrapper to a name in the table at the top | ||
| 1316 | * of the LUA stack. | ||
| 1317 | * \pre the object on the top of the stack is a table. | ||
| 1318 | * \param[in] L a pointer to the LUA state. | ||
| 1319 | * \param[in] f a pointer to the C++ function to be wrapped. It cannot be | ||
| 1320 | * a non-static object member function. | ||
| 1321 | */ | ||
| 1322 | ✗ | template <class FPTR> inline void lua_bindwrapperwithname( | |
| 1323 | lua_State* L, FPTR f, const std::string& name | ||
| 1324 | ) { | ||
| 1325 | ✗ | geo_assert(lua_gettop(L) > 0); | |
| 1326 | ✗ | geo_assert(lua_istable(L,-1)); | |
| 1327 | ✗ | lua_pushstring(L,name.c_str()); | |
| 1328 | lua_pushwrapper(L,f); | ||
| 1329 | ✗ | lua_settable(L,-3); | |
| 1330 | ✗ | } | |
| 1331 | |||
| 1332 | |||
| 1333 | /** | ||
| 1334 | * \brief Binds a wrapper to a name in the global scole. | ||
| 1335 | * \param[in] L a pointer to the LUA state. | ||
| 1336 | * \param[in] f a pointer to the C++ function to be wrapped. It cannot be | ||
| 1337 | * a non-static object member function. | ||
| 1338 | */ | ||
| 1339 | template <class FPTR> inline void lua_bindwrapperwithnameglobal( | ||
| 1340 | lua_State* L, FPTR f, const std::string& name | ||
| 1341 | ) { | ||
| 1342 | lua_pushwrapper(L,f); | ||
| 1343 | ✗ | lua_setglobal(L,name.c_str()); | |
| 1344 | ✗ | } | |
| 1345 | |||
| 1346 | /**************************************************************************/ | ||
| 1347 | |||
| 1348 | /** | ||
| 1349 | * \brief Converts a C++ function name into a LUA function name. | ||
| 1350 | * \details Removes all namespaces from the name. | ||
| 1351 | * \param[in] L a pointer to the LUA state (unused). | ||
| 1352 | * \param[in] functionname the C++ function name. | ||
| 1353 | * \return a std::string with the function name with all namespaces removed. | ||
| 1354 | */ | ||
| 1355 | ✗ | inline std::string lua_wrappername(lua_State* L, const char* functionname) { | |
| 1356 | geo_argused(L); | ||
| 1357 | ✗ | std::string result(functionname); | |
| 1358 | size_t pos = result.find_last_of(":"); | ||
| 1359 | ✗ | if(pos != std::string::npos) { | |
| 1360 | ✗ | result = result.substr(pos+1, result.length()-pos); | |
| 1361 | } | ||
| 1362 | ✗ | return result; | |
| 1363 | } | ||
| 1364 | |||
| 1365 | /**************************************************************************/ | ||
| 1366 | |||
| 1367 | /** | ||
| 1368 | * \brief Binds a LUA wrapper around a C++ function to the table on the top | ||
| 1369 | * of the LUA stack. | ||
| 1370 | * \details The arguments are automatically converted from the LUA stack | ||
| 1371 | * to the function arguments. If the function is non-void, the return | ||
| 1372 | * value is automatically converted to LUA and pushed onto the LUA stack. | ||
| 1373 | * If the function is already a LUA interface (takes a lua_State* as an | ||
| 1374 | * argument and returns an integer), then it will be directly called, | ||
| 1375 | * without any conversion. | ||
| 1376 | * The name of the function is obtained by removing all namespaces | ||
| 1377 | * from \p f. | ||
| 1378 | * \param[in] L a pointer to the LUA state. | ||
| 1379 | * \param[in] f a pointer to the C++ function to be wrapped. It cannot be | ||
| 1380 | * a non-static object member function. | ||
| 1381 | */ | ||
| 1382 | #define lua_bindwrapper(L, f) lua_bindwrapperwithname( \ | ||
| 1383 | (L),(f),GEO::lua_wrappername(L,#f) \ | ||
| 1384 | ) | ||
| 1385 | |||
| 1386 | /** | ||
| 1387 | * \brief Binds a LUA wrapper around a C++ function to the global scope. | ||
| 1388 | * \details The arguments are automatically converted from the LUA stack | ||
| 1389 | * to the function arguments. If the function is non-void, the return | ||
| 1390 | * value is automatically converted to LUA and pushed onto the LUA stack. | ||
| 1391 | * If the function is already a LUA interface (takes a lua_State* as an | ||
| 1392 | * argument and returns an integer), then it will be directly called, | ||
| 1393 | * without any conversion. | ||
| 1394 | * The name of the function is obtained by removing all namespaces | ||
| 1395 | * from \p f. | ||
| 1396 | * \param[in] L a pointer to the LUA state. | ||
| 1397 | * \param[in] f a pointer to the C++ function to be wrapped. It cannot be | ||
| 1398 | * a non-static object member function. | ||
| 1399 | */ | ||
| 1400 | #define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \ | ||
| 1401 | (L),(f),GEO::lua_wrappername(L,#f) \ | ||
| 1402 | ) | ||
| 1403 | |||
| 1404 | |||
| 1405 | /*************************************************************************/ | ||
| 1406 | |||
| 1407 | } | ||
| 1408 | |||
| 1409 | #ifdef GEO_COMPILER_MSVC | ||
| 1410 | #pragma warning( pop ) | ||
| 1411 | #endif | ||
| 1412 | |||
| 1413 | #endif | ||
| 1414 |