|
| 1 | +/** |
| 2 | +Internal module for pushing and getting pointers. |
| 3 | +Pointers are stored in metadata with metatables to enfirce type-safety. |
| 4 | +A 'deref' property is created to access the pointer's value. |
| 5 | +*/ |
| 6 | +module luad.conversions.pointers; |
| 7 | + |
| 8 | +import luad.conversions.helpers; |
| 9 | +import luad.conversions.functions; |
| 10 | + |
| 11 | +import luad.c.all; |
| 12 | +import luad.stack; |
| 13 | + |
| 14 | +import core.memory; |
| 15 | + |
| 16 | +import std.traits; |
| 17 | +import std.conv; |
| 18 | + |
| 19 | +void pushGetter(T)(lua_State* L) |
| 20 | +{ |
| 21 | + static if(isUserStruct!(PointerTarget!T)) |
| 22 | + { |
| 23 | + static ref PointerTarget!T deref(T ptr) |
| 24 | + { |
| 25 | + return *ptr; |
| 26 | + } |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + static PointerTarget!T deref(T ptr) |
| 31 | + { |
| 32 | + return *ptr; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + lua_pushlightuserdata(L, &deref); |
| 37 | + lua_pushcclosure(L, &functionWrapper!(typeof(&deref)), 1); |
| 38 | +} |
| 39 | + |
| 40 | +private void pushGetters(T)(lua_State* L) |
| 41 | +{ |
| 42 | + lua_newtable(L); // -2 is getters |
| 43 | + lua_newtable(L); // -1 is methods |
| 44 | + |
| 45 | + pushGetter!T(L); |
| 46 | + lua_setfield(L, -3, "deref"); |
| 47 | + |
| 48 | + lua_pushcclosure(L, &index, 2); |
| 49 | +} |
| 50 | + |
| 51 | +void pushSetter(T)(lua_State* L) |
| 52 | +{ |
| 53 | + static if(isUserStruct!(PointerTarget!T)) |
| 54 | + { |
| 55 | + static void deref(T ptr, ref PointerTarget!T val) |
| 56 | + { |
| 57 | + *ptr = val; |
| 58 | + } |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + static void deref(T ptr, PointerTarget!T val) |
| 63 | + { |
| 64 | + *ptr = val; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + lua_pushlightuserdata(L, &deref); |
| 69 | + lua_pushcclosure(L, &functionWrapper!(typeof(&deref)), 1); |
| 70 | +} |
| 71 | + |
| 72 | +private void pushSetters(T)(lua_State* L) |
| 73 | +{ |
| 74 | + lua_newtable(L); |
| 75 | + |
| 76 | + pushSetter!T(L); |
| 77 | + lua_setfield(L, -2, "deref"); |
| 78 | + |
| 79 | + lua_pushcclosure(L, &newIndex, 1); |
| 80 | +} |
| 81 | + |
| 82 | +private void pushMeta(T)(lua_State* L) |
| 83 | +{ |
| 84 | + if(luaL_newmetatable(L, T.mangleof.ptr) == 0) |
| 85 | + return; |
| 86 | + |
| 87 | + pushValue(L, T.stringof); |
| 88 | + lua_setfield(L, -2, "__dtype"); |
| 89 | + |
| 90 | + // TODO: mangled names can get REALLY long in D, it might be nicer to store a hash instead? |
| 91 | + pushValue(L, T.mangleof); |
| 92 | + lua_setfield(L, -2, "__dmangle"); |
| 93 | + |
| 94 | + lua_pushcfunction(L, &userdataCleaner); |
| 95 | + lua_setfield(L, -2, "__gc"); |
| 96 | + |
| 97 | + static if(!is(Unqual!(PointerTarget!T) == void)) |
| 98 | + { |
| 99 | + pushGetters!T(L); |
| 100 | + lua_setfield(L, -2, "__index"); |
| 101 | + static if(isMutable!(PointerTarget!T)) |
| 102 | + { |
| 103 | + pushSetters!T(L); |
| 104 | + lua_setfield(L, -2, "__newindex"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + lua_pushvalue(L, -1); |
| 109 | + lua_setfield(L, -2, "__metatable"); |
| 110 | +} |
| 111 | + |
| 112 | +void pushPointer(T)(lua_State* L, T value) if (isPointer!T) |
| 113 | +{ |
| 114 | + T* udata = cast(T*)lua_newuserdata(L, T.sizeof); |
| 115 | + *udata = value; |
| 116 | + |
| 117 | + GC.addRoot(udata); |
| 118 | + |
| 119 | + pushMeta!T(L); |
| 120 | + lua_setmetatable(L, -2); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +T getPointer(T)(lua_State* L, int idx) if(isPointer!T) |
| 125 | +{ |
| 126 | + verifyType!T(L, idx); |
| 127 | + |
| 128 | + T* udata = cast(T*)lua_touserdata(L, idx); |
| 129 | + return *udata; |
| 130 | +} |
| 131 | + |
| 132 | +version(unittest) |
| 133 | +{ |
| 134 | + import luad.base; |
| 135 | +} |
| 136 | + |
| 137 | +unittest |
| 138 | +{ |
| 139 | + import luad.testing; |
| 140 | + |
| 141 | + lua_State* L = luaL_newstate(); |
| 142 | + scope(success) lua_close(L); |
| 143 | + luaL_openlibs(L); |
| 144 | + |
| 145 | +} |
0 commit comments