1 /** Manages a LuaState object.*/ 2 module bluejay.execution_state; 3 4 import luad.all; 5 6 struct Options { 7 bool luastd = false; 8 bool recurse = false; 9 string scriptPath; 10 } 11 12 class ExecutionState { 13 import bluejay.functions; 14 private: 15 16 TestFunctions _testFunctions; 17 UtilFunctions _utilFunctions; 18 ScriptFunctions _scriptFunctions; 19 20 public: 21 22 // It would be nice to make this `package` but alias this won't work. 23 LuaState _lua; 24 alias _lua this; 25 26 this(Options options) { 27 this._lua = new LuaState; 28 29 if (options.luastd) { 30 _lua.openLibs(); 31 } else { 32 import luad.c.all; 33 // TODO: Add utf8, table? 34 luaopen_base(_lua.state); 35 luaopen_string(_lua.state); 36 } 37 38 setVariables(_lua); 39 _testFunctions = new TestFunctions(_lua, options); 40 _utilFunctions = UtilFunctions(_lua); 41 _scriptFunctions = new ScriptFunctions(_lua); 42 _lua["Test"] = _testFunctions; 43 _lua["Util"] = _utilFunctions; 44 _lua["Script"] = _scriptFunctions; 45 _lua.doString("function cleanup() end"); 46 } 47 48 private void setVariables(ref LuaState lua) { 49 import std.typecons : Tuple, tuple; 50 51 Tuple!(string, string)[] system; 52 version(Windows) { 53 system ~= tuple("OS", "Windows"); 54 system ~= tuple("endl", "\r\n"); 55 } else version(linux) { 56 system ~= tuple("OS", "Linux"); 57 system ~= tuple("endl", "\n"); 58 } else version(OSX) { 59 system ~= tuple("OS", "macOS"); 60 system ~= tuple("endl", "\n"); 61 } 62 version(X86) { 63 system ~= tuple("Arch", "x86"); 64 } else version(X86_64) { 65 system ~= tuple("Arch", "x86_64"); 66 } else version(ARM) { 67 system ~= tuple("Arch", "ARM"); 68 } 69 lua["System"] = lua.newTable(system); 70 71 import std.process : environment; 72 Tuple!(string, string)[] environ; 73 foreach (env; environment.toAA.byKeyValue) { 74 environ ~= tuple(env.key, env.value); 75 } 76 lua["Env"] = lua.newTable(environ); 77 } 78 }