public Test001() { const string test001 = "n = 99 + (1 * 10) / 2 - 0.5;\n" + "if n > 10 then return 'Oh, 真的比10还大哦:'..n end\n" + "return n\n"; const string test002 = "return _VERSION"; const string test003 = "return nil"; const bool isLoadLib = true; try { Console.WriteLine("Start test..."); Lua L = new Lua(); if (isLoadLib) { BaseLib.open(L); PackageLib.open(L); MathLib.open(L); OSLib.open(L); StringLib.open(L); TableLib.open(L); } int status = L.doString(test001); if (status != 0) { object errObj = L.value(1); object tostring = L.getGlobal("tostring"); L.push(tostring); L.push(errObj); L.call(1, 1); string errObjStr = L.toString(L.value(-1)); throw new Exception("Error compiling : " + L.value(1)); } else { object result = L.value(1); object tostring_ = L.getGlobal("tostring"); L.push(tostring_); L.push(result); L.call(1, 1); string resultStr = L.toString(L.value(-1)); Console.WriteLine("Result >>> " + resultStr); } } catch (Exception e) { Console.WriteLine(e); } }
/// <summary> /// Creates a global environment with all the base modules registered and /// some default values set. /// </summary> /// <returns></returns> public static LuaTable CreateGlobalEnviroment(bool createBaseLib = true, bool createStringLib = true, bool createTableLib = true, bool createOSLib = true, bool createIOLib = true, bool createFileLib = true, bool createMathLib = true, bool createScriptLib = true, bool createWinFormsLib = true, bool createConsoleLib = true, bool createCoroutineLib = true, bool createPackageLib = true, bool createClassLib = true, bool createFileSystemLib = true) { LuaTable global = new LuaTable(); // Register Lua Modules if (createBaseLib) { BaseLib.RegisterFunctions(global); } if (createStringLib) { StringLib.RegisterModule(global); } if (createTableLib) { TableLib.RegisterModule(global); } if (createIOLib) { IOLib.RegisterModule(global); } if (createFileLib) { FileLib.RegisterModule(global); } if (createMathLib) { MathLib.RegisterModule(global); } if (createOSLib) { OSLib.RegisterModule(global); } if (createScriptLib) { ScriptLib.RegisterModule(global); } //if (createWinFormsLib) // WinFormLib.RegisterModule(global); if (createConsoleLib) { ConsoleLib.RegisterModule(global); } if (createCoroutineLib) { CoroutineLib.RegisterModule(global); } if (createPackageLib) { PackageLib.RegisterModule(global); } if (createClassLib) { ClassLib.RegisterModule(global); } if (createFileSystemLib) { FileSystemLib.RegisterModule(global); } //global.SetNameValue("_WORKDIR", new LuaString(Application.StartupPath + "\\")); global.SetNameValue("_VERSION", new LuaString("Sharp Lua 1.1")); global.SetNameValue("_G", global); if (createPackageLib) { // set package.preload table LuaTable preload = (LuaTable)(global.GetValue("package") as LuaTable).GetValue("preload"); if (createStringLib) { preload.SetNameValue("string", (LuaTable)global.GetValue("string")); } if (createTableLib) { preload.SetNameValue("table", (LuaTable)global.GetValue("table")); } if (createIOLib) { preload.SetNameValue("io", (LuaTable)global.GetValue("io")); } if (createFileLib) { preload.SetNameValue("file", (LuaTable)global.GetValue("file")); } if (createMathLib) { preload.SetNameValue("math", (LuaTable)global.GetValue("math")); } if (createOSLib) { preload.SetNameValue("os", (LuaTable)global.GetValue("os")); } if (createScriptLib) { preload.SetNameValue("script", (LuaTable)global.GetValue("script")); } //if (createWinFormsLib) // preload.SetNameValue("WinForms", (LuaTable) global.GetValue("WinForms")); if (createConsoleLib) { preload.SetNameValue("console", (LuaTable)global.GetValue("console")); } if (createCoroutineLib) { preload.SetNameValue("coroutine", (LuaTable)global.GetValue("coroutine")); } if (createPackageLib) // wait a second... { preload.SetNameValue("package", (LuaTable)global.GetValue("package")); } if (createClassLib) { preload.SetNameValue("class", (LuaTable)global.GetValue("class")); } if (createFileSystemLib) { preload.SetNameValue("filesystem", (LuaTable)global.GetValue("filesystem")); } } if (createFileSystemLib) { FileSystemLib.currentDir = global.GetValue("_WORKDIR").ToString(); } GlobalEnvironment = global; return(global); }
public Runner(string[] args, string filename) { if (args.Length > 0) { string path = args[0]; StreamReader sr = new StreamReader(path, Encoding.Default); String line; string content = ""; while ((line = sr.ReadLine()) != null) { content += line.ToString() + "\n"; } const bool isLoadLib = true; const bool useArg = true; // try { Lua L = new Lua(); if (isLoadLib) { BaseLib.open(L); PackageLib.open(L); MathLib.open(L); OSLib.open(L); StringLib.open(L); TableLib.open(L); } if (useArg) { //FIXME: index may be minus (for example, arg[-1], before script file name) //@see http://www.ttlsa.com/lua/lua-install-and-lua-variable-ttlsa/ int narg = args.Length; LuaTable tbl = L.createTable(narg, narg); for (int i = 0; i < narg; i++) { L.rawSetI(tbl, i, args[i]); } L.setGlobal("arg", tbl); } int status = L.doString(content); if (status != 0) { object errObj = L.value(1); object tostring = L.getGlobal("tostring"); L.push(tostring); L.push(errObj); L.call(1, 1); string errObjStr = L.toString(L.value(-1)); throw new Exception("Error compiling : " + L.value(1)); } else { object result = L.value(1); object tostring_ = L.getGlobal("tostring"); L.push(tostring_); L.push(result); L.call(1, 1); string resultStr = L.toString(L.value(-1)); Console.WriteLine("Result >>> " + resultStr); } } // catch (Exception e) // { // Console.WriteLine(e); // } } else { Console.WriteLine("usage: {0} <filename>", filename); } }