aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LSL.lua
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/src/LSL.lua')
-rw-r--r--LuaSL/src/LSL.lua13
1 files changed, 9 insertions, 4 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)