1 module zua.interop.userdata; 2 import zua.interop.table; 3 import zua.interop; 4 import zua.vm.engine; 5 import std.typecons; 6 import std.uuid; 7 8 /** Wrapper around Lua userdata */ 9 struct Userdata { 10 11 /** The internal data for this userdata; should seldom be accessed by user code */ 12 Value _internalUserdata; 13 14 package this(Value userdata) { 15 _internalUserdata = userdata; 16 } 17 18 @disable this(); 19 20 /** Create a new userdata */ 21 static Userdata create(void* ptr, Nullable!Table metatable = Nullable!Table()) { 22 auto res = Userdata(Value(new UserdataValue(ptr, null))); 23 if (!metatable.isNull) { 24 res.metatable = metatable; 25 } 26 return res; 27 } 28 29 /** Get the metatable */ 30 Nullable!Table metatable() { 31 if (_internalUserdata.metatable) { 32 return Nullable!Table(Table(Value(_internalUserdata.metatable))); 33 } 34 else { 35 return Nullable!Table(); 36 } 37 } 38 39 /** Set the metatable */ 40 void metatable(Nullable!Table newMetatable) { 41 if (newMetatable.isNull) { 42 _internalUserdata.userdata.metatable = null; 43 } 44 else { 45 _internalUserdata.userdata.metatable = newMetatable.get._internalTable.table; 46 } 47 } 48 49 /** Set the metatable */ 50 pragma(inline) void metatable(Table newMetatable) { 51 metatable = newMetatable.Nullable!Table; 52 } 53 54 /** Set the metatable */ 55 pragma(inline) void metatable(typeof(null)) { 56 metatable = Nullable!Table(); 57 } 58 59 /** Get the internal data pointer */ 60 void* data() { 61 return _internalUserdata.userdata.data; 62 } 63 64 /** Set the internal data pointer */ 65 void data(void* value) { 66 _internalUserdata.userdata.data = value; 67 } 68 69 /** Get the owner UUID of this userdata */ 70 UUID owner() { 71 return _internalUserdata.userdata.ownerId; 72 } 73 74 /** Set the owner UUID of this userdata */ 75 void owner(UUID value) { 76 _internalUserdata.userdata.ownerId = value; 77 } 78 79 }