diff options
author | David Walter Seikel | 2012-02-05 18:52:59 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-02-05 18:52:59 +1000 |
commit | ca174fc7c2b22e902b93ef4183eb1f5235bcb5fa (patch) | |
tree | 7e873c1be6b829b7c6ce076867c68e197b41938b /LuaSL/src/LSL.lua | |
parent | Do script quitting differently, using the wire protocol method, which means I... (diff) | |
download | SledjHamr-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 '')
-rw-r--r-- | LuaSL/src/LSL.lua | 13 |
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 | |||
25 | upvalue--either way is a bit more efficient and less error prone. | 25 | upvalue--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 | |||
28 | local LSL = {}; | 31 | local 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; | |||
309 | local currentState = {} | 312 | local currentState = {} |
310 | local running = true; | 313 | local 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(). | 316 | function quit() |
314 | function LSL.quit() | ||
315 | running = false | 317 | running = false |
316 | end | 318 | end |
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) |