|
1 | 1 | # Lua.NET |
2 | 2 |  |
3 | 3 |
|
4 | | -C# .NET Core 6.0 Lua bindings and helper functions. |
| 4 | +C# .NET Core 7.0 Lua bindings and helper functions. |
5 | 5 |
|
6 | 6 | https://github.com/tilkinsc/Lua.NET |
7 | 7 | Copyright © Cody Tilkins 2022 MIT License |
@@ -116,3 +116,109 @@ class Project |
116 | 116 |
|
117 | 117 | } |
118 | 118 | ``` |
| 119 | + |
| 120 | +Example Usage NativeAOT DLL Library: |
| 121 | +```C# |
| 122 | +// test2.csproj |
| 123 | +// <PropertyGroup> |
| 124 | +// ... |
| 125 | +// <AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
| 126 | +// <PublishAot>true</PublishAot> |
| 127 | +// </PropertyGroup> |
| 128 | +
|
| 129 | +using System.Runtime.InteropServices; |
| 130 | +using LuaJIT; |
| 131 | +using static LuaJIT.Lua; |
| 132 | + |
| 133 | +namespace test2; |
| 134 | + |
| 135 | +public unsafe class Test2 |
| 136 | +{ |
| 137 | + |
| 138 | + [UnmanagedCallersOnly] |
| 139 | + public static int l_GetHello(lua_State L) |
| 140 | + { |
| 141 | + lua_pushstring(L, "Hello World!"); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + public static luaL_Reg[] test2Lib = new luaL_Reg[] |
| 146 | + { |
| 147 | + AsLuaLReg("GetHello", &l_GetHello), |
| 148 | + AsLuaLReg(null, null) |
| 149 | + }; |
| 150 | + |
| 151 | + [UnmanagedCallersOnly(EntryPoint = "luaopen_test2")] |
| 152 | + public static int luaopen_test2(lua_State L) |
| 153 | + { |
| 154 | + luaL_newlib(L, test2Lib); |
| 155 | + lua_setglobal(L, "test2"); |
| 156 | + return 1; |
| 157 | + } |
| 158 | + |
| 159 | +} |
| 160 | + |
| 161 | +// test1.csproj |
| 162 | +
|
| 163 | +using LuaJIT; |
| 164 | +using static LuaJIT.Lua; |
| 165 | + |
| 166 | +namespace test1; |
| 167 | + |
| 168 | +public class Test1 |
| 169 | +{ |
| 170 | + |
| 171 | + public static void Main(string[] args) |
| 172 | + { |
| 173 | + lua_State L = luaL_newstate(); |
| 174 | + if (L.Handle == UIntPtr.Zero) |
| 175 | + { |
| 176 | + Console.WriteLine("Unable to create context!"); |
| 177 | + } |
| 178 | + luaL_openlibs(L); |
| 179 | + |
| 180 | + // require("test2.dll"); |
| 181 | + lua_getglobal(L, "require"); |
| 182 | + lua_pushstring(L, "test2"); |
| 183 | + int result = lua_pcall(L, 1, 0, 0); |
| 184 | + if (result != LUA_OK) |
| 185 | + { |
| 186 | + string? err = luaL_checkstring(L, 1); |
| 187 | + if (err != null) |
| 188 | + { |
| 189 | + Console.WriteLine($"1 Error Result: {err}"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // print(_G.test2.GetHello()) |
| 194 | + lua_getglobal(L, "test2"); |
| 195 | + lua_getglobal(L, "print"); |
| 196 | + lua_getfield(L, 1, "GetHello"); |
| 197 | + result = lua_pcall(L, 0, 1, 0); |
| 198 | + if (result != LUA_OK) |
| 199 | + { |
| 200 | + string? err = luaL_checkstring(L, 1); |
| 201 | + if (err != null) |
| 202 | + { |
| 203 | + Console.WriteLine($"2 Error Result: {err}"); |
| 204 | + } |
| 205 | + } |
| 206 | + result = lua_pcall(L, 1, 0, 0); |
| 207 | + if (result != LUA_OK) |
| 208 | + { |
| 209 | + string? err = luaL_checkstring(L, 1); |
| 210 | + if (err != null) |
| 211 | + { |
| 212 | + Console.WriteLine($"3 Error Result: {err}"); |
| 213 | + } |
| 214 | + } |
| 215 | + // pop test2 |
| 216 | + lua_pop(L, 1); |
| 217 | + |
| 218 | + lua_close(L); |
| 219 | + |
| 220 | + Console.WriteLine("Success!"); |
| 221 | + } |
| 222 | + |
| 223 | +} |
| 224 | +``` |
0 commit comments