aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-02-23 20:45:51 +1000
committerDavid Walter Seikel2012-02-23 20:45:51 +1000
commitea2d670f88f14212cd73bfc201fabfb6a32059f6 (patch)
tree86bad5bfc8ead14444abcf2066b9580c0256590d /LuaSL
parentHave the returns from external function calls include the word "return" at th... (diff)
downloadSledjHamr-ea2d670f88f14212cd73bfc201fabfb6a32059f6.zip
SledjHamr-ea2d670f88f14212cd73bfc201fabfb6a32059f6.tar.gz
SledjHamr-ea2d670f88f14212cd73bfc201fabfb6a32059f6.tar.bz2
SledjHamr-ea2d670f88f14212cd73bfc201fabfb6a32059f6.tar.xz
Make events work. Use a common communicatons loop. A bit of rearranging.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/LSL.lua95
1 files changed, 50 insertions, 45 deletions
diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua
index 15ac38f..be1c7fd 100644
--- a/LuaSL/src/LSL.lua
+++ b/LuaSL/src/LSL.lua
@@ -23,22 +23,22 @@ upvalue--either way is a bit more efficient and less error prone.
23-- http://www.lua.org/pil/15.4.html looks useful. 23-- http://www.lua.org/pil/15.4.html looks useful.
24-- http://www.lua.org/pil/15.5.html the last part about autoloading functions might be useful. 24-- http://www.lua.org/pil/15.5.html the last part about autoloading functions might be useful.
25 25
26
26local LSL = {}; 27local LSL = {};
27local SID = ""; 28local SID = "";
28local scriptName = ""; 29local scriptName = "";
29local running = true 30local running = true
30local paused = false 31local paused = false
31 32local currentState = {}
32 33local detectedKeys = {}
33-- Stuff called from the wire protocol has to be global, but I think this means just global to this file. 34local detectedNames = {}
34function stop() paused = true end 35local waitAndProcess
35function quit() running = false end
36 36
37 37
38-- Debugging aids 38-- Debugging aids
39 39
40-- Functions to print tables. 40-- Functions to print tables.
41local print_table, print_table_start; 41local print_table, print_table_start
42 42
43function print_table_start(table, space, name) 43function print_table_start(table, space, name)
44 print(space .. name .. ": "); 44 print(space .. name .. ": ");
@@ -64,6 +64,31 @@ function msg(...)
64end 64end
65 65
66 66
67-- Stuff called from the wire protocol has to be global, but I think this means just global to this file.
68
69events = {}
70
71function stop() paused = true end
72function quit() running = false end
73
74function events.detectedKeys(list)
75 detectedKeys = list
76end
77
78function events.detectedNames(list)
79 detectedNames = list
80end
81
82function events.touch_start(number)
83--print_table_start(detectedKeys, "", "detectedKeys")
84--print_table_start(detectedNames, "", "detectedNames")
85--print(scriptName .. ".touch_start(" .. number .. ")\n")
86 if nil ~= currentState.touch_start then currentState.touch_start(number) end
87 detectedKeys = {}
88 detectedNames = {}
89end
90
91
67-- LSL function and constant creation stuff. 92-- LSL function and constant creation stuff.
68 93
69local args2string -- Pre declare this. 94local args2string -- Pre declare this.
@@ -107,37 +132,10 @@ function mt.callAndReturn(name, ...)
107end 132end
108 133
109function mt.callAndWait(name, ...) 134function mt.callAndWait(name, ...)
110 local func = functions[name]
111
112 mt.callAndReturn(name, ...); 135 mt.callAndReturn(name, ...);
113 -- Eventually a sendForth() is called, which should end up passing through SendToChannel(). 136 -- Eventually a sendForth() is called, which should end up passing through SendToChannel().
114 -- Wait for the result, which should be a Lua value as a string. 137 -- Wait for the result, which should be a Lua value as a string.
115 local message = luaproc.receive(SID) 138 return waitAndProcess(true)
116 if message then
117 result, errorMsg = loadstring("return " .. 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
118 if nil == result then
119 msg("Not a valid result: " .. message .. " ERROR MESSAGE: " .. errorMsg)
120 else
121 -- Set the functions environment to ours, for the protection of the script, coz loadstring sets it to the global environment instead.
122 setfenv(result, getfenv(1))
123 status, result = pcall(result)
124 if not status then
125 msg("Error from result: " .. message .. " ERROR MESSAGE: " .. result)
126 elseif result then
127 return result
128 end
129 end
130 end
131
132 if "float" == func.Type then return 0.0
133 elseif "integer" == func.Type then return 0
134 elseif "key" == func.Type then return LSL.NULL_KEY
135 elseif "list" == func.Type then return {}
136 elseif "string" == func.Type then return ""
137 elseif "rotation" == func.Type then return LSL.ZERO_ROTATION
138 elseif "vector" == func.Type then return LSL.ZERO_VECTOR
139 end
140 return nil
141end 139end
142 140
143local function newConst(Type, name, value) 141local function newConst(Type, name, value)
@@ -628,8 +626,6 @@ function LSL.postIncrement(name) local temp = _G[name]; _G[name] = _G[name] + 1;
628 626
629-- State stuff 627-- State stuff
630 628
631local currentState = {}
632
633function LSL.stateChange(x) 629function LSL.stateChange(x)
634 if currentState ~= x then -- Changing to the same state is a NOP. 630 if currentState ~= x then -- Changing to the same state is a NOP.
635 -- TODO - Should clear out pending events, except timer() 631 -- TODO - Should clear out pending events, except timer()
@@ -672,32 +668,42 @@ function LSL.mainLoop(sid, name, x)
672 end 668 end
673 669
674 LSL.stateChange(x); 670 LSL.stateChange(x);
671 waitAndProcess(false)
672 msg("Script quitting.")
673end
675 674
676 -- Need a FIFO queue of incoming events. Which will be in the C main thread, coz that's listening on the socket for us. 675-- Need a FIFO queue of incoming events. Which will be in the C main thread, coz that's listening on the socket for us.
677 -- The ecore_con stuff ends up being a sorta FIFO queue of the commands coming from OpenSim. 676-- The ecore_con stuff ends up being a FIFO queue of the commands coming from OpenSim. So no worries.
678 -- Plus, I think the luaproc message system manages a FIFO queue for us as well. 677function waitAndProcess(returnWanted)
679 -- Might still need one. lol 678 local Type = "event"
680 679
680 if returnWanted then Type = "result" end
681 while running do 681 while running do
682 local message = luaproc.receive(sid) 682 local message = luaproc.receive(SID)
683 if message then 683 if message then
684 -- TODO - should we be discarding return values while paused? I don't think so, so we need to process those,
684 if paused then 685 if paused then
685 if "start()" == message then paused = false end 686 if "start()" == message then paused = false end
686 else 687 else
687 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 688 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
688 if nil == result then 689 if nil == result then
689 msg("Not a valid event: " .. message .. " ERROR MESSAGE: " .. errorMsg) 690 msg("Not a valid " .. Type .. ": " .. message .. " ERROR MESSAGE: " .. errorMsg)
690 else 691 else
691 -- Set the functions environment to ours, for the protection of the script, coz loadstring sets it to the global environment instead. 692 -- Set the functions environment to ours, for the protection of the script, coz loadstring sets it to the global environment instead.
692 -- 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. 693 -- 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.
693 setfenv(result, getfenv(1)) 694 setfenv(result, getfenv(1))
694 status, result = pcall(result) 695 status, result = pcall(result)
695 if not status then 696 if not status then
696 msg("Error from event: " .. message .. " ERROR MESSAGE: " .. result) 697 msg("Error from " .. Type .. ": " .. message .. " ERROR MESSAGE: " .. result)
697 elseif result then 698 elseif result then
699 -- Check if we are waiting for a return, and got it.
700 if returnWanted and string.match(message, "^return ") then
701 return result
702 end
703 -- Otherwise, just run it and keep looping.
698 status, errorMsg = luaproc.send(sid, result) 704 status, errorMsg = luaproc.send(sid, result)
699 if not status then 705 if not status then
700 msg("Error sending results from event: " .. message .. " ERROR MESSAGE: " .. errorMsg) 706 msg("Error sending results from " .. Type .. ": " .. message .. " ERROR MESSAGE: " .. errorMsg)
701 end 707 end
702 end 708 end
703 end 709 end
@@ -706,7 +712,6 @@ function LSL.mainLoop(sid, name, x)
706 end 712 end
707end 713end
708 714
709
710-- Typecasting stuff. 715-- Typecasting stuff.
711 716
712function LSL.floatTypecast(x) 717function LSL.floatTypecast(x)