1 module api.execute; 2 3 import lumars, api; 4 5 string executeLuaString(string text, string[] args) 6 { 7 auto state = makeState(args); 8 9 try 10 { 11 state.doString(text); 12 return null; 13 } 14 catch(Exception ex) 15 { 16 version(release) 17 return ex.msg; 18 else 19 throw ex; 20 } 21 } 22 23 string executeLuaFile(string file, string[] args) 24 { 25 auto state = makeState(args); 26 27 try 28 { 29 state.doFile(file); 30 return null; 31 } 32 catch(Exception ex) 33 { 34 version(release) 35 return ex.msg; 36 else 37 throw ex; 38 } 39 } 40 41 private: 42 43 LuaState* makeState(string[] args) 44 { 45 import core.stdc.stdlib; 46 auto state = cast(LuaState*)calloc(LuaState.sizeof, 1); 47 *state = LuaState(null); 48 state.registerPathApi(); 49 state.registerFsApi(); 50 state.registerProcApi(); 51 state.registerRegexApi(); 52 detectLuaRocks(state); 53 state.globalTable.set("LUMARSH_ARGS", args); 54 55 return state; 56 } 57 58 void detectLuaRocks(LuaState* lua) 59 { 60 import std.process; 61 62 const res = executeShell("luarocks --version"); 63 if(res.status != 0) 64 return; 65 66 lua.globalTable.set("LUA_PATH", executeShell("luarocks path --lua-version 5.1 --lr-path").output); 67 lua.globalTable.set("LUA_CPATH", executeShell("luarocks path --lua-version 5.1 --lr-cpath").output); 68 lua.doString(` 69 package.path = package.path .. ";" .. LUA_PATH 70 package.cpath = package.cpath .. ";" .. LUA_CPATH 71 require("luarocks.loader") 72 `); 73 }