A little while ago I came up with a solution for when you have a ‘doesnotunderstand’ error. This works because (almost) all the information you need is already in the debug frame object, expect from where the error occured, so the code has to search through all the code in the call stack in text format to find it. This is entirely a SC solution - no c++. See below for example.
Storing the character index for each bytecode would also work and make lookup easier. However, you’d still have to walk up the call stack to find the meaningful bit of code. One issue is DoesNotUnderstand as this doesn’t fail when you write 1.asdfds
, but only when the DoesNotUnderstand
error isn’t caught — sending any random message is valid behaviour in smalltalks. This means figuring out exactly where an ‘error’ has occurred is harder in SC than other languages.
The SC interpreter doesn’t know the program text
It is accessible in the DebugFrame Object.
{
\asdf;
1.getBackTrace.functionDef.sourceCode
}.()
The one exception to this seems to be when you have a function object written in a method. Further, methods delete their source code, but store the file name and the charPos
so it can be reloaded.
Here’s an example of what I got working, the code is a mess and needs refactoring but works - I’ve just been busy.
In file /home/jordan/Work/software/quarks/Piping/Classes/ScaleExt.sc: line 38
BorkedClass:throwVariable:
var b = 'good variable';
"someting".postln;
a + 1;
b.bad_message
^^^^^^^^^^^^^
//a.ahahahahaha(42)
}
Variable 'b' with value good variable (Symbol), did not understand the message 'bad_message'.
Now it can only give you the file name and line number when this is a class, when the code is passed at runtime you would still get the body of the code, but you’d not get a file name, and the line number would be relative to the function body. I don’t think this is a deal breaker though.