Skip to content

Commit

Permalink
Now more reliably throw a runtime error when indexing into a null v…
Browse files Browse the repository at this point in the history
…alue.
  • Loading branch information
JoeStrout committed Feb 1, 2023
1 parent ae2fb6e commit 2254ca4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions MiniScript-cpp/src/MiniScript/MiniscriptTAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ namespace MiniScript {
} break;
case Op::NotA:
return Value::Truth(!opA.BoolValue());
case Op::ElemBofA:
if (opA.IsNull()) {
TypeException("Null Reference Exception: can't index into null").raise();
} else {
TypeException("Type Exception: can't index into this type").raise();
}

default:
break;
}
Expand Down
6 changes: 6 additions & 0 deletions MiniScript-cs/MiniscriptTAC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ public Value Evaluate(Context context) {
}
case Op.NotA:
return opA != null && opA.BoolValue() ? ValNumber.zero : ValNumber.one;
case Op.ElemBofA:
if (opA is null) {
throw new TypeException("Null Reference Exception: can't index into null");
} else {
throw new TypeException("Type Exception: can't index into this type");
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions TestSuite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,18 @@ a[42]
----------------------------------------------------------------------
Runtime Error: __isa depth exceeded (perhaps a reference loop?) [line 3]
======================================================================
==== Trap null reference lookups.
a = null
print a[3]
----------------------------------------------------------------------
Runtime Error: Null Reference Exception: can't index into null [line 2]
======================================================================
==== Trap null reference lookups (#2).
a = null
print a.foo
----------------------------------------------------------------------
Runtime Error: Type Error (while attempting to look up foo) [line 2]
======================================================================
==== Error reporting of unexpected end-of-file.
for i in range(0,10)
print i
Expand Down

0 comments on commit 2254ca4

Please sign in to comment.