diff options
Diffstat (limited to 'LuaSL/src')
-rw-r--r-- | LuaSL/src/LSL.lua | 95 |
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 | |||
26 | local LSL = {}; | 27 | local LSL = {}; |
27 | local SID = ""; | 28 | local SID = ""; |
28 | local scriptName = ""; | 29 | local scriptName = ""; |
29 | local running = true | 30 | local running = true |
30 | local paused = false | 31 | local paused = false |
31 | 32 | local currentState = {} | |
32 | 33 | local detectedKeys = {} | |
33 | -- Stuff called from the wire protocol has to be global, but I think this means just global to this file. | 34 | local detectedNames = {} |
34 | function stop() paused = true end | 35 | local waitAndProcess |
35 | function 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. |
41 | local print_table, print_table_start; | 41 | local print_table, print_table_start |
42 | 42 | ||
43 | function print_table_start(table, space, name) | 43 | function print_table_start(table, space, name) |
44 | print(space .. name .. ": "); | 44 | print(space .. name .. ": "); |
@@ -64,6 +64,31 @@ function msg(...) | |||
64 | end | 64 | end |
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 | |||
69 | events = {} | ||
70 | |||
71 | function stop() paused = true end | ||
72 | function quit() running = false end | ||
73 | |||
74 | function events.detectedKeys(list) | ||
75 | detectedKeys = list | ||
76 | end | ||
77 | |||
78 | function events.detectedNames(list) | ||
79 | detectedNames = list | ||
80 | end | ||
81 | |||
82 | function 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 = {} | ||
89 | end | ||
90 | |||
91 | |||
67 | -- LSL function and constant creation stuff. | 92 | -- LSL function and constant creation stuff. |
68 | 93 | ||
69 | local args2string -- Pre declare this. | 94 | local args2string -- Pre declare this. |
@@ -107,37 +132,10 @@ function mt.callAndReturn(name, ...) | |||
107 | end | 132 | end |
108 | 133 | ||
109 | function mt.callAndWait(name, ...) | 134 | function 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 | ||
141 | end | 139 | end |
142 | 140 | ||
143 | local function newConst(Type, name, value) | 141 | local 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 | ||
631 | local currentState = {} | ||
632 | |||
633 | function LSL.stateChange(x) | 629 | function 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.") | ||
673 | end | ||
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. | 677 | function 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 |
707 | end | 713 | end |
708 | 714 | ||
709 | |||
710 | -- Typecasting stuff. | 715 | -- Typecasting stuff. |
711 | 716 | ||
712 | function LSL.floatTypecast(x) | 717 | function LSL.floatTypecast(x) |