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 +++++++++---- LuaSL/src/LuaSL_compile.c | 2 +- LuaSL/src/LuaSL_main.c | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) (limited to 'LuaSL') 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) 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) fprintf(out, "--// Generated code goes here.\n\n"); fprintf(out, "local _bit = require(\"bit\")\n"); - 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. + fprintf(out, "local _LSL = require(\"LSL\")\n\n"); // TODO - Use the scripts UUID instead of the file name here. fprintf(out, "local _SID = [=[%s.lua.out]=]\n\n", compiler.fileName); 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) snprintf(buf, sizeof(buf), "%s/%s.events", path, name); // PD("Quitting Lua script %s", buf); - status = sendToChannel(buf, "_LSL.quit()"); + status = sendToChannel(buf, "quit()"); if (status) PE("Error trying to kill script %s : %s", buf, status); } -- cgit v1.1