Callback function uses caller's variable scope instead of declared scope
hi there
Still on CF 9.0.1.x
I have in the same directory 2 CFCs, each implementing an object that communicates with each other. Each CFC has the variables scope used. They are part of an emulator for a physical device. There are more cfc involved but I reduced it to the minimum test case.
The parent is MCU which instantiates any number of TARGETS. A target communicates back to MCU with a callback function provided by the MCU. The callback function uses MCU's clock to log the incoming message.
---
component MCU {
variables.mcu.clock = 0;
more of variables.mcu.*
Init () {
// Create all targets, call each target’s registerMCUListener to let them know whom to call back.
variables.mcu.targets = CreateObject ("component", "target").Init ();
variables.mcu.targets.registerMCUListener (cbFromTarget);
}
private numeric function cbFromTarget (struct msg) {
writedump (var=#variables#); // <<<< This shows the variables scope of TARGET, expected MCU
variables.mcu.events.list[variables.mcu.events.ptr] = {
clock = variables.mcu.clock, // CF chokes on this one because MCU is unknown
msg = arguments.msg
};
return 0;
}
}
---
---
component TARGET {
variables.tg.clock = 0;
more of variables.tg.*
public void function registerMCUListener (any mculistener) {
/*
* register the mcu listener function to notify of any events
*/
variables.tg.mcu.listener = arguments.mculistener;
}
private void function ToMCU (struct msg) {
/*
* use the registered mcu listener function to notify of any events
*/
if (isCustomFunction (variables.tg.mcu.listener)) {
variables.tg.mcu.listener (msg);
}
}
}
---
Idea: The cbFunc registers the message in the MCU’s variables scope
But it does not. The marked variables.mcu.clock is throwing an error because mcu.clock does not exist in the variables scope.
Observation: CF is using the variables scope of TARGET instead of MCU.
I confirmed that by dumping the variables scope just before that line.
Now: Is this a bug? Looks as if this is an compiler issue of binding *too late* …
Because it's urgent (as usual) .. any workarounds? Be aware that even those two simple CFCs have to communicate in both directions MCU <-> TARGET .. and of course there are more CFCs involved ...
PS: In CF 10 I could use the keyword function as a data type declaration. I did that but to no avail. Same error.
Thanks
