From ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 5 Feb 2012 18:52:59 +1000 Subject: 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. --- LuaSL/src/LSL.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'LuaSL/src/LSL.lua') 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 upvalue--either way is a bit more efficient and less error prone. ]] +-- http://www.lua.org/pil/15.4.html looks useful. +-- http://www.lua.org/pil/15.5.html the last part about autoloading functions might be useful. + local LSL = {}; -- LSL constants. @@ -309,9 +312,8 @@ function LSL.postIncrement(name) local temp = _G[name]; _G[name] = _G[name] + 1; local currentState = {} local running = true; --- Damn, looks like these sorts of functions have to be part of the table. --- Coz pcall() runs in the context of the script, not this module, so needs to call _LSL.quit(). -function LSL.quit() +-- Stuff called from the wire protocol has to be global, but I think this means just global to this file. +function quit() running = false end @@ -361,10 +363,13 @@ function LSL.mainLoop(SID, x) while running do local message = luaproc.receive(sid) if message then - result, errorMsg = loadstring(message) + 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 if nil == result then print("Not a valid event: " .. message .. " ERROR MESSAGE: " .. errorMsg) else + -- Set the functions environment to ours, for the protection of the script, coz loadstring sets it to the global environment instead. + -- 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. + setfenv(result, getfenv(1)) status, result = pcall(result) if not status then print("Error from event: " .. message .. " ERROR MESSAGE: " .. result) -- cgit v1.1