aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-02-05 18:52:59 +1000
committerDavid Walter Seikel2012-02-05 18:52:59 +1000
commitca174fc7c2b22e902b93ef4183eb1f5235bcb5fa (patch)
tree7e873c1be6b829b7c6ce076867c68e197b41938b /LuaSL
parentDo script quitting differently, using the wire protocol method, which means I... (diff)
downloadSledjHamr-ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa.zip
SledjHamr-ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa.tar.gz
SledjHamr-ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa.tar.bz2
SledjHamr-ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa.tar.xz
Fix things so the wire protocol can be functions in the LSL.lua file, and _LSL can go back to being local to the script.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LSL.lua13
-rw-r--r--LuaSL/src/LuaSL_compile.c2
-rw-r--r--LuaSL/src/LuaSL_main.c2
3 files changed, 11 insertions, 6 deletions
diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua
index 6473cc2..8774121 100644
--- a/LuaSL/src/LSL.lua
+++ b/LuaSL/src/LSL.lua
@@ -25,6 +25,9 @@ global uniqueness) for this key or store the metatable as an
25upvalue--either way is a bit more efficient and less error prone. 25upvalue--either way is a bit more efficient and less error prone.
26]] 26]]
27 27
28-- http://www.lua.org/pil/15.4.html looks useful.
29-- http://www.lua.org/pil/15.5.html the last part about autoloading functions might be useful.
30
28local LSL = {}; 31local LSL = {};
29 32
30-- LSL constants. 33-- LSL constants.
@@ -309,9 +312,8 @@ function LSL.postIncrement(name) local temp = _G[name]; _G[name] = _G[name] + 1;
309local currentState = {} 312local currentState = {}
310local running = true; 313local running = true;
311 314
312-- Damn, looks like these sorts of functions have to be part of the table. 315-- Stuff called from the wire protocol has to be global, but I think this means just global to this file.
313-- Coz pcall() runs in the context of the script, not this module, so needs to call _LSL.quit(). 316function quit()
314function LSL.quit()
315 running = false 317 running = false
316end 318end
317 319
@@ -361,10 +363,13 @@ function LSL.mainLoop(SID, x)
361 while running do 363 while running do
362 local message = luaproc.receive(sid) 364 local message = luaproc.receive(sid)
363 if message then 365 if message then
364 result, errorMsg = loadstring(message) 366 result, errorMsg = loadstring(message) -- "The environment of the returned function is the global environment." Though normally, a function inherits it's environment from the function creating it. Which is what we want. lol
365 if nil == result then 367 if nil == result then
366 print("Not a valid event: " .. message .. " ERROR MESSAGE: " .. errorMsg) 368 print("Not a valid event: " .. message .. " ERROR MESSAGE: " .. errorMsg)
367 else 369 else
370 -- Set the functions environment to ours, for the protection of the script, coz loadstring sets it to the global environment instead.
371 -- TODO - On the other hand, we will need the global environment when we call event handlers. So we should probably stash it around here somewhere.
372 setfenv(result, getfenv(1))
368 status, result = pcall(result) 373 status, result = pcall(result)
369 if not status then 374 if not status then
370 print("Error from event: " .. message .. " ERROR MESSAGE: " .. result) 375 print("Error from event: " .. message .. " ERROR MESSAGE: " .. result)
diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c
index f840b69..72d94b9 100644
--- a/LuaSL/src/LuaSL_compile.c
+++ b/LuaSL/src/LuaSL_compile.c
@@ -2275,7 +2275,7 @@ boolean compileLSL(gameGlobals *game, char *script, boolean doConstants)
2275 2275
2276 fprintf(out, "--// Generated code goes here.\n\n"); 2276 fprintf(out, "--// Generated code goes here.\n\n");
2277 fprintf(out, "local _bit = require(\"bit\")\n"); 2277 fprintf(out, "local _bit = require(\"bit\")\n");
2278 fprintf(out, "_LSL = require(\"LSL\")\n\n"); // Local might be quicker, but looks like we need global for pcall(), it has it's own local stack I think. 2278 fprintf(out, "local _LSL = require(\"LSL\")\n\n");
2279 // TODO - Use the scripts UUID instead of the file name here. 2279 // TODO - Use the scripts UUID instead of the file name here.
2280 fprintf(out, "local _SID = [=[%s.lua.out]=]\n\n", compiler.fileName); 2280 fprintf(out, "local _SID = [=[%s.lua.out]=]\n\n", compiler.fileName);
2281 outputLeaf(out, OM_LUA, compiler.ast); 2281 outputLeaf(out, OM_LUA, compiler.ast);
diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c
index c441bc5..5f48c5e 100644
--- a/LuaSL/src/LuaSL_main.c
+++ b/LuaSL/src/LuaSL_main.c
@@ -123,7 +123,7 @@ static void dirList_quit(const char *name, const char *path, void *data)
123 123
124 snprintf(buf, sizeof(buf), "%s/%s.events", path, name); 124 snprintf(buf, sizeof(buf), "%s/%s.events", path, name);
125// PD("Quitting Lua script %s", buf); 125// PD("Quitting Lua script %s", buf);
126 status = sendToChannel(buf, "_LSL.quit()"); 126 status = sendToChannel(buf, "quit()");
127 if (status) 127 if (status)
128 PE("Error trying to kill script %s : %s", buf, status); 128 PE("Error trying to kill script %s : %s", buf, status);
129 } 129 }