-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleUserdataLowLevel.cs
More file actions
130 lines (104 loc) · 4.09 KB
/
ExampleUserdataLowLevel.cs
File metadata and controls
130 lines (104 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Runtime.InteropServices;
using System;
#if LUAU_UNITY
using AOT;
#endif
namespace LuauSharp
{
public unsafe class ExampleUserdataLowLevel
{
public float number;
private static readonly LuauNative.LuaCFunction NewDelegateKA = New;
private static readonly void* NewPtr = (void*)Marshal.GetFunctionPointerForDelegate(NewDelegateKA);
private static readonly LuauNative.LuaCFunction IndexDelegateKA = Index;
private static readonly void* IndexPtr = (void*)Marshal.GetFunctionPointerForDelegate(IndexDelegateKA);
private static readonly LuauNative.LuaCFunction NewIndexDelegateKA = NewIndex;
private static readonly void* NewIndexPtr = (void*)Marshal.GetFunctionPointerForDelegate(NewIndexDelegateKA);
private static readonly LuauNative.LuaCFunction PrintDelegateKA = Print;
private static readonly void* PrintPtr = (void*)Marshal.GetFunctionPointerForDelegate(PrintDelegateKA);
public ExampleUserdataLowLevel(float num)
{
number = num;
}
public static void Register(LuauNative.lua_State* luaState)
{
Luau.NewTable(luaState);
Luau.PushCFunction(luaState, NewPtr);
Luau.SetField(luaState, -2, "new");
Luau.SetGlobal(luaState, "example");
}
#if LUAU_UNITY
[MonoPInvokeCallback(typeof(LuauNative.LuaCFunction))]
#endif
public static int New(LuauNative.lua_State* luaState)
{
double? number = Luau.GetNumberSafe(luaState, 1);
if (number.HasValue)
{
ExampleUserdataLowLevel exampleUserdataLowLevel = new ExampleUserdataLowLevel((float)number.Value);
Luau.PushUserdata(luaState, exampleUserdataLowLevel);
//metatable
Luau.NewTable(luaState);
Luau.PushCFunction(luaState, IndexPtr);
Luau.SetField(luaState, -2, "__index");
Luau.PushCFunction(luaState, NewIndexPtr);
Luau.SetField(luaState, -2, "__newindex");
Luau.SetMetatable(luaState, -2);
return 1;
}
return 0;
}
#if LUAU_UNITY
[MonoPInvokeCallback(typeof(LuauNative.LuaCFunction))]
#endif
public static int Index(LuauNative.lua_State* luaState)
{
ExampleUserdataLowLevel userdataLowLevel = Luau.GetUserdata<ExampleUserdataLowLevel>(luaState, 1);
if (userdataLowLevel == null)
return 0;
byte* name = Luau.GetBytePtr(luaState, 2);
if (Luau.StrCmp(name, "print") == 0)
{
Luau.PushCFunction(luaState, PrintPtr);
return 1;
}
if (Luau.StrCmp(name, "number") == 0)
{
Luau.PushNumber(luaState, userdataLowLevel.number);
return 1;
}
return 0;
}
#if LUAU_UNITY
[MonoPInvokeCallback(typeof(LuauNative.LuaCFunction))]
#endif
public static int NewIndex(LuauNative.lua_State* luaState)
{
ExampleUserdataLowLevel userdataLowLevel = Luau.GetUserdata<ExampleUserdataLowLevel>(luaState, 1);
if (userdataLowLevel == null)
return 0;
byte* name = Luau.GetBytePtr(luaState, 2);
if (Luau.StrCmp(name, "number") == 0)
{
double? number = Luau.GetNumberSafe(luaState, 3);
if (number.HasValue)
{
userdataLowLevel.number = (float)number.Value;
}
}
return 0;
}
#if LUAU_UNITY
[MonoPInvokeCallback(typeof(LuauNative.LuaCFunction))]
#endif
public static int Print(LuauNative.lua_State* luaState)
{
ExampleUserdataLowLevel number = Luau.GetUserdata<ExampleUserdataLowLevel>(luaState, 1);
if (number != null)
Console.WriteLine("C# : " + number.number + " number!");
else
Console.WriteLine("C# : NULL!");
return 0;
}
}
}