| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #include "wrap_Data.h" |
| 22 | |
| 23 | // Put the Lua code directly into a raw string literal. |
| 24 | static const char data_lua[] = |
| 25 | #include "wrap_Data.lua" |
| 26 | ; |
| 27 | |
| 28 | namespace love |
| 29 | { |
| 30 | namespace data |
| 31 | { |
| 32 | |
| 33 | Data *luax_checkdata(lua_State *L, int idx) |
| 34 | { |
| 35 | return luax_checktype<Data>(L, idx); |
| 36 | } |
| 37 | |
| 38 | int w_Data_getString(lua_State *L) |
| 39 | { |
| 40 | Data *t = luax_checkdata(L, 1); |
| 41 | lua_pushlstring(L, (const char *) t->getData(), t->getSize()); |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | int w_Data_getPointer(lua_State *L) |
| 46 | { |
| 47 | Data *t = luax_checkdata(L, 1); |
| 48 | lua_pushlightuserdata(L, t->getData()); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | // Placeholder, overridden by the FFI code when the FFI is available. |
| 53 | int w_Data_getFFIPointer(lua_State *L) |
| 54 | { |
| 55 | lua_pushnil(L); |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | int w_Data_getSize(lua_State *L) |
| 60 | { |
| 61 | Data *t = luax_checkdata(L, 1); |
| 62 | lua_pushnumber(L, (lua_Number) t->getSize()); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | // C functions in a struct, necessary for the FFI versions of Data methods. |
| 67 | struct FFI_Data |
| 68 | { |
| 69 | void *(*getFFIPointer)(Proxy *p); |
| 70 | }; |
| 71 | |
| 72 | static FFI_Data ffifuncs = |
| 73 | { |
| 74 | [](Proxy *p) -> void * // getFFIPointer |
| 75 | { |
| 76 | auto data = luax_ffi_checktype<Data>(p); |
| 77 | return data != nullptr ? data->getData() : nullptr; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | const luaL_Reg w_Data_functions[] = |
| 82 | { |
| 83 | { "getString" , w_Data_getString }, |
| 84 | { "getPointer" , w_Data_getPointer }, |
| 85 | { "getFFIPointer" , w_Data_getFFIPointer }, |
| 86 | { "getSize" , w_Data_getSize }, |
| 87 | { 0, 0 } |
| 88 | }; |
| 89 | |
| 90 | void luax_rundatawrapper(lua_State *L, const love::Type &type) |
| 91 | { |
| 92 | luax_runwrapper(L, data_lua, sizeof(data_lua), "Data.lua" , type, &ffifuncs); |
| 93 | } |
| 94 | |
| 95 | int luaopen_data(lua_State *L) |
| 96 | { |
| 97 | int n = luax_register_type(L, &Data::type, w_Data_functions, nullptr); |
| 98 | luax_rundatawrapper(L, Data::type); |
| 99 | return n; |
| 100 | } |
| 101 | |
| 102 | } // data |
| 103 | } // love |
| 104 | |