Skip to content

Commit 7067d25

Browse files
authored
Simulator: Define exceptions for callbacks that allow param-storing. (#29)
1 parent 62d0de3 commit 7067d25

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

Simulator.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ call any callback in the plugin. The Scenario uses this functionality to impleme
1414
Use the processCallbackRequest() function to call into the plugin.
1515
Use the queueCallbackRequest() function to add a call into the plugin into a queue.
1616
Use the processAllQueuedCallbackRequests() function to process the callback queue until it is empty.
17+
18+
The callback request is a table containing the following members:
19+
- Function - function in the plugin to call
20+
- ParamValues - array of values that get passed as parameters. If not present, ParamTypes is used instead.
21+
- ParamTypes - array of type descriptions (tables with {Type = <>}) from which the parameters are synthesized. Only used if ParamValues not present.
22+
- Notes - string description of the callback (used for logging)
23+
- AllowsStore - optional dictionary of index -> true, parameters at specified indices may be stored by the callback
1724
--]]
1825

1926

@@ -131,7 +138,7 @@ function Simulator:afterCallClearObjects(a_Request, a_Params, a_Returns)
131138
-- Change the objects in parameters so that any access to them results in an error:
132139
local requestNotes = a_Request.Notes
133140
for idx, param in ipairs(a_Params) do
134-
if (type(param) == "userdata") then
141+
if ((type(param) == "userdata") and not(a_Request.AllowsStore[idx])) then
135142
getmetatable(param).__index = function()
136143
self.logger:error(3, "Attempting to use an object that has been stored from a callback %q.", requestNotes)
137144
end
@@ -204,7 +211,7 @@ function Simulator:beforeCallGCObjects(a_Request, a_Params)
204211
a_Request.uncollectedParams = {}
205212
for idx, param in ipairs(a_Params) do
206213
local t = type(param)
207-
if (t == "userdata") then
214+
if ((t == "userdata") and not (a_Request.AllowsStore[idx])) then
208215
local paramType = self:typeOf(param)
209216
a_Request.uncollectedParams[idx] = paramType
210217
local mt = getmetatable(param)
@@ -1075,6 +1082,7 @@ function Simulator:initializePlugin()
10751082
Function = self.sandbox.Initialize,
10761083
ParamValues = { self.sandbox.cPluginManager:Get():GetCurrentPlugin() },
10771084
Notes = "Initialize()",
1085+
AllowsStore = {[1] = true},
10781086
}
10791087
)
10801088
if not(res[1]) then
@@ -1328,6 +1336,7 @@ function Simulator:processCallbackRequest(a_Request)
13281336
assert(self)
13291337
assert(a_Request)
13301338
assert(a_Request.Function)
1339+
a_Request.AllowsStore = a_Request.AllowsStore or {}
13311340

13321341
if (a_Request.Notes) then
13331342
self.logger:debug("Calling request \"%s\".", a_Request.Notes)

0 commit comments

Comments
 (0)