static int CSFuncWrap(IntPtr L) { CppDll.LuaGetTop(L); int arg0 = (int)CppDll.LuaToNumber(L, 1); int arg1 = (int)CppDll.LuaToNumber(L, 2); int o = CSFunc(arg0, arg1); CppDll.LuaPushNumber(L, o); return(1); }
static void Main(string[] args) { // init lua state IntPtr L = CppDll.LuaNewState(); CppDll.LuaOpenLibs(L); // lua scripts to run string lua_1 = @" print('[lua] ha! it is lua here'); LuaFunc = function(x,y) print('[lua] this is LuaFunc with args x = ' .. x .. ' and y = ' .. y); local ret = x * 10 + y; print('[lua] ret will be ' .. ret); return ret; end "; string lua_2 = @" print('[lua] will call CSFunc(1,2) ...'); local ret = CSFunc(1,2); print('[lua] we got '.. ret); "; byte[] bytes = null; // load lua_1 bytes = System.Text.Encoding.Default.GetBytes(lua_1); CppDll.LuaDoString(L, bytes, bytes.Length); // register cs func IntPtr fn = Marshal.GetFunctionPointerForDelegate((LuaCSFunction)(CSFuncWrap)); CppDll.RegisterCSFunc(L, "CSFunc", fn); // load lua_2 bytes = System.Text.Encoding.Default.GetBytes(lua_2); CppDll.LuaDoString(L, bytes, bytes.Length); // call lua function Console.WriteLine("[CS] will call LuaFunc(3,5) ..."); CppDll.LuaGetGlobal(L, "LuaFunc"); CppDll.LuaPushNumber(L, 3); CppDll.LuaPushNumber(L, 5); CppDll.LuaPCall(L, 2, 1, 0); int ret = (int)CppDll.LuaToNumber(L, 1); Console.WriteLine("[CS] we got " + ret); CppDll.LuaClose(L); Console.ReadKey(); }