NextToken uses wrong stack
by Tom Spilman · in Torque Game Engine · 07/23/2005 (4:52 pm) · 5 replies
On working on the Enhanced TelnetDebugger i ran into this bug.
NextToken takes a string as the second parameter which will get the returned token on it. To return that value NextToken does this:
This works fine for main.cs where the evalstate stack is empty, but it doesn't in this case:
So when other.cs is executed from within setup() the eval stack is no longer empty. nextToken sees this and instead of making token2 a global it makes it a local on the setup() function stack.
(Side node... another bug/feature is that script exec'd from a function could in theory modify locals within in that function.)
Unless my logic is flawed this is a bug in NextToken. Please let me know if anyone thinks this isn't a bug... it effects my work on the debugger enhancements.
With the TelnetDebugger enhancements above i've sort of fixed this by...
1. exec'd code gets a stack frame with a null scopeName.
2. changed NextToken to do this...
I should post my updated resource soon.
NextToken takes a string as the second parameter which will get the returned token on it. To return that value NextToken does this:
// set local variable if inside a function
if (gEvalState.stack.size())
Con::setLocalVariable(token,tmp);
else
Con::setVariable(token,tmp);This works fine for main.cs where the evalstate stack is empty, but it doesn't in this case:
// main.cs
nextToken("first;second", token, ";");
echo( $token );
function setup()
{
exec( "starter.fps/other.cs" );
echo( %token2 ); // should be blank!
}
setup();// starter.fps/other.cs
nextToken("first;second", token2, ";");
echo( $token2 ); // blank!So when other.cs is executed from within setup() the eval stack is no longer empty. nextToken sees this and instead of making token2 a global it makes it a local on the setup() function stack.
(Side node... another bug/feature is that script exec'd from a function could in theory modify locals within in that function.)
Unless my logic is flawed this is a bug in NextToken. Please let me know if anyone thinks this isn't a bug... it effects my work on the debugger enhancements.
With the TelnetDebugger enhancements above i've sort of fixed this by...
1. exec'd code gets a stack frame with a null scopeName.
2. changed NextToken to do this...
// set local variable if inside a function
if (gEvalState.stack.size() &&
gEvalState.stack.last()->scopeName)
Con::setLocalVariable(token,tmp);
else
Con::setVariable(token,tmp);I should post my updated resource soon.
About the author
Tom is a programmer and co-owner of Sickhead Games, LLC.
#2
07/25/2005 (3:36 pm)
I've noted this issue (#216), and will be getting to it soon. Thanks for bringing this up. On first inspection, the fix is correct. Does 1 already happen, or did you have to add code to do it?
#3
07/25/2005 (4:14 pm)
@Ben - No plain 1.4 doesn't create stack frames when exec'ing... it actually works on the existing stack frame which means exec'ed script files can change locals within the calling function. The debugger enhancements needed a stack frame when exec'ing so that the callstack made sense to the guy debugging. I'll be updating the resource soon which includes the fix for this bug.
#4
07/25/2005 (5:21 pm)
Ok, great. Thanks, Tom.
#5
So, the upshot is that Tom and I will be exploring some different ways of addressing the current file/scope for debugging, but that the way things are is correct in terms of runtime behavior.
08/08/2005 (9:31 pm)
After more discussion with Tom and looking at the problem, I think that how things are vis a vis exec and frames is correct - but not useful for debugging.So, the upshot is that Tom and I will be exploring some different ways of addressing the current file/scope for debugging, but that the way things are is correct in terms of runtime behavior.
Associate Paul Dana
...but also seems like scary behaviour to CHANGE...what if some script codes depend upon this behavior?
...even so...sure does seem like a bug.