Skip to content

Commit fd22a45

Browse files
committed
fix: improve debugger snapshot handling and error message formatting
- Track stack depth for proper snapshot management in try-catch blocks - Update error message format in catch blocks to include quotes - Ensure proper cleanup of debugger snapshots for caught errors - Update test cases to match new error message format
1 parent ca22739 commit fd22a45

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

evaluator/evalTryCatch.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,30 @@ import (
1212
func evalTryCatch(node *ast.TryCatchStmt, env *environment.Environment, dbgr *debugger.Debugger) (*shared.RuntimeValue, *errors.RuntimeError) {
1313
scope := environment.NewEnvironment(env)
1414

15+
// I realized that just removing the snapshot at catching the error
16+
// is not enough, we have to make sure the call stack has the same
17+
// length as things go in and out of this function, so that the snapshots
18+
// isnt messed up. snapshots are meant to be taken only upon uncaught errors
19+
// and not on caught errors
20+
var stackDepth int
21+
if dbgr != nil {
22+
stackDepth = len(dbgr.CallStack)
23+
}
24+
1525
for _, stmt := range node.Try {
26+
1627
_, err := Evaluate(stmt, &scope, dbgr)
1728
if err != nil {
1829
// Because we are successfully catching this error,
1930
// we can safely delete the snapshot
31+
// Here we will remove everything other than the stackDepth
32+
// we saved earlier
2033
if dbgr != nil && len(dbgr.Snapshots) > 0 {
21-
dbgr.Snapshots = dbgr.Snapshots[:len(dbgr.Snapshots)-1]
34+
dbgr.Snapshots = dbgr.Snapshots[:stackDepth]
2235
}
2336
scope = environment.NewEnvironment(env)
2437

25-
scope.DeclareVar(node.CatchVar, shared.RuntimeValue{Type: shared.String, Value: "Runtime Error: " + err.Message}, false)
38+
scope.DeclareVar(node.CatchVar, shared.RuntimeValue{Type: shared.String, Value: "\"Runtime Error: " + err.Message + "\""}, false)
2639

2740
var lastResult *shared.RuntimeValue = nil
2841
for _, stmt := range node.Catch {

evaluator/evaluator_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ func TestTryCatch(t *testing.T) {
11361136
input: "let error = 'error not triggered'\ntry {undefinedVariable} catch e {error = e}\nerror",
11371137
output: shared.RuntimeValue{
11381138
Type: shared.String,
1139-
Value: "Runtime Error: Cannot resolve variable `undefinedVariable`",
1139+
Value: "\"Runtime Error: Cannot resolve variable `undefinedVariable`\"",
11401140
},
11411141
},
11421142
{
@@ -1157,14 +1157,14 @@ func TestTryCatch(t *testing.T) {
11571157
input: "let error = 'error not triggered'\ntry {let x = y} catch e {error = e}\nerror",
11581158
output: shared.RuntimeValue{
11591159
Type: shared.String,
1160-
Value: "Runtime Error: Cannot resolve variable `y`",
1160+
Value: "\"Runtime Error: Cannot resolve variable `y`\"",
11611161
},
11621162
},
11631163
{
11641164
input: "let error = 'error not triggered'\ntry {let x = 10\nx = x + y} catch e {error = e}\nerror",
11651165
output: shared.RuntimeValue{
11661166
Type: shared.String,
1167-
Value: "Runtime Error: Cannot resolve variable `y`",
1167+
Value: "\"Runtime Error: Cannot resolve variable `y`\"",
11681168
},
11691169
},
11701170
{
@@ -1178,7 +1178,7 @@ func TestTryCatch(t *testing.T) {
11781178
input: "let error = 'error not triggered'\ntry {let obj = {foo: 'bar'}\nobj.foo()} catch e {error = e}\nerror",
11791179
output: shared.RuntimeValue{
11801180
Type: shared.String,
1181-
Value: "Runtime Error: Cannot invoke a non-function (attempted to call a string).",
1181+
Value: "\"Runtime Error: Cannot invoke a non-function (attempted to call a string).\"",
11821182
},
11831183
},
11841184
{
@@ -1192,7 +1192,7 @@ func TestTryCatch(t *testing.T) {
11921192
input: "let error = 'error not triggered'\ntry {let arr = [1, 2, 3]\narr.foo()} catch e {error = e}\nerror",
11931193
output: shared.RuntimeValue{
11941194
Type: shared.String,
1195-
Value: "Runtime Error: Cannot access property of array by non-number (attempting to access properties by Identifier).",
1195+
Value: "\"Runtime Error: Cannot access property of array by non-number (attempting to access properties by Identifier).\"",
11961196
},
11971197
},
11981198
{
@@ -1473,7 +1473,7 @@ func TestContinueKeyword(t *testing.T) {
14731473
input: "let err = ''\ntry {continue} catch e {err = e}\nerr",
14741474
output: shared.RuntimeValue{
14751475
Type: shared.String,
1476-
Value: "Runtime Error: `continue` statement used outside of a loop context.",
1476+
Value: "\"Runtime Error: `continue` statement used outside of a loop context.\"",
14771477
},
14781478
},
14791479
}
@@ -1647,7 +1647,7 @@ func TestBreakKeyword(t *testing.T) {
16471647
input: "let err = ''\ntry {break} catch e {err = e}\nerr",
16481648
output: shared.RuntimeValue{
16491649
Type: shared.String,
1650-
Value: "Runtime Error: `break` statement used outside of a loop context.",
1650+
Value: "\"Runtime Error: `break` statement used outside of a loop context.\"",
16511651
},
16521652
},
16531653
}

0 commit comments

Comments
 (0)