Skip to content

Repository files navigation

LuaNET

A modified fork of https://github.com/tilkinsc/Lua.NET but only with support for LuaJIT. This library uses a more C-Sharp styled API, with built-in functionality to load modules and call C-Sharp methods directly by function pointer.

Installation

The easiest way to install is with NuGet

dotnet add package JAJ.Packages.LuaNET --version 1.0.0

Basic Example

The basic example just prints 'Hello world' to the console.

usingSystem;usingSystem.IO;usingLuaNET;namespaceLuaNETExample{classProgram{staticvoidMain(string[]args){stringcode="print(\"Hello world\")";LuaStatestate=Lua.NewState();if(!state.IsNull){Lua.OpenLibs(state);Lua.DoString(state,code);Lua.Close(state);}}}}

Advanced Example

The advanced example creates a Lua module that can call C# methods and prints the output to the console. This is not the typical way of doing interop, but it illustrates the power and flexibility of LuaJIT. First create a file and call it example.lua and paste in the following code:

localtest=require('test')
localresult=test.addNumbers(10, 20)
test.writeLine('result: ' ..result)

Next create a new file and call it test.lua. Paste in the following code:

localffi=require ('ffi')
localluanet=require('luanet')
--You can name this variable however you likelocalcsharp= {}
csharp.WriteLine=luanet.findMethod('WriteLine', 'void (__cdecl*)(char*)')
csharp.AddNumbers=luanet.findMethod('AddNumbers', 'int (__cdecl*)(int,int)')
localtest= {}
functiontest.writeLine(message)
--If user passes in a number, converts it to a stringiftype(message) =='number' thenmessage=tostring(message)
end--Interop does not allow to pass Lua strings directly so they need to be converted to a C-type--Allocate enough memory for the string + null terminatorlocalc_message=ffi.new('char[?]', #message+1)
--Copy the Lua string to the allocated memoryffi.copy(c_message, message)
--Call the C# methodcsharp.WriteLine(c_message)
endfunctiontest.addNumbers(a, b)
localc_a=ffi.cast('int', a)
localc_b=ffi.cast('int', b)
localresult=csharp.AddNumbers(c_a, c_b)
returntonumber(result)
endreturntest

Finally create a file and call it Program.cs and add following code to it:

usingSystem;usingLuaNET;usingLuaNET.Interop;usingLuaNET.Modules;namespaceLuaNETExample{classProgram{staticvoidMain(string[]args){LuaNETModuleluanetModule=newLuaNETModule();TestModuletestModule=newTestModule();LuaStatestate=Lua.NewState();if(!state.IsNull){Lua.OpenLibs(state);//LuaNetModule is used to find C# methods and TestModule requires it//Loading order of modules is important in this caseluanetModule.Initialize(state);testModule.Initialize(state);//Note that this file path assumes that example.lua is in the same directory as the executableLua.DoFile(state,"example.lua");Lua.Close(state);}}}publicclassTestModule:LuaModule{publicoverridevoidInitialize(LuaStateL){//Register the marked methods before loading the moduleRegisterExternalMethods();//Note that this file path assumes that test.lua is in the same directory as the executableLuaModuleLoader.RegisterFromFile(L,"test","test.lua");}[LuaExternalMethod]privatestaticunsafevoidWriteLine(byte*text){strings=newstring((sbyte*)text);Console.WriteLine(s);}[LuaExternalMethod]privatestaticunsafeintAddNumbers(inta,intb){returna+b;}}}

More Examples

See Examples.

About

.NET wrapper for luajit

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages