Skip to content

Commit 28914b6

Browse files
committed
Better assertion line and error handling.
1 parent 74484e5 commit 28914b6

File tree

16 files changed

+330
-266
lines changed

16 files changed

+330
-266
lines changed

src/main/java/mx/kenzie/skript/compiler/SkriptLangSpec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private SkriptLangSpec() {
129129
new AddEffect(),
130130
new DeleteEffect(),
131131
new RemoveEffect(),
132+
new AssertWithErrorEffect(),
132133
new AssertEffect(),
133134
new ExitEffect(),
134135
new ExitThreadEffect(),
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
package mx.kenzie.skript.error;
22

33
public class ScriptAssertionError extends AssertionError {
4+
private final int line;
5+
private final Class<?> script;
46

5-
public ScriptAssertionError() {
7+
public ScriptAssertionError(Class<?> script, int line) {
68
super();
9+
this.line = line;
10+
this.script = script;
11+
712
}
813

9-
public ScriptAssertionError(Object detailMessage) {
10-
super(detailMessage);
14+
public ScriptAssertionError(Class<?> script, int line, String message) {
15+
super(message);
16+
this.line = line;
17+
this.script = script;
1118
}
1219

13-
public ScriptAssertionError(boolean detailMessage) {
14-
super(detailMessage);
15-
}
16-
17-
public ScriptAssertionError(char detailMessage) {
18-
super(detailMessage);
19-
}
20-
21-
public ScriptAssertionError(int detailMessage) {
22-
super(detailMessage);
23-
}
24-
25-
public ScriptAssertionError(long detailMessage) {
26-
super(detailMessage);
27-
}
28-
29-
public ScriptAssertionError(float detailMessage) {
30-
super(detailMessage);
31-
}
32-
33-
public ScriptAssertionError(double detailMessage) {
34-
super(detailMessage);
35-
}
36-
37-
public ScriptAssertionError(String message, Throwable cause) {
38-
super(message, cause);
20+
@Override
21+
public String toString() {
22+
final String source;
23+
final String message = getLocalizedMessage();
24+
if (script == null) source = "";
25+
else source = script.getName().replace('.', '/');
26+
if (line < 1) {
27+
if (script == null) {
28+
if (message == null)
29+
return "Assertion failed. (Unknown source/line)";
30+
else
31+
return "Assertion failed: " + message + " (Unknown source/line)";
32+
} else {
33+
if (message == null)
34+
return "Assertion failed. (" + source + ")";
35+
else
36+
return "Assertion failed: " + message + " (" + source + ")";
37+
}
38+
} else {
39+
if (script == null) {
40+
if (message == null)
41+
return "Assertion failed. (Line " + line + ")";
42+
else
43+
return "Assertion failed: " + message + " (Line " + line + ")";
44+
} else {
45+
if (message == null)
46+
return "Assertion failed. (" + source + " line " + line + ")";
47+
else
48+
return "Assertion failed: " + message + " (" + source + " line " + line + ")";
49+
}
50+
}
3951
}
4052

4153
@Override
42-
public Throwable fillInStackTrace() {
54+
public synchronized Throwable fillInStackTrace() {
4355
return this;
4456
}
4557
}
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
package mx.kenzie.skript.lang.syntax.flow;
22

3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.WriteInstruction;
35
import mx.kenzie.skript.api.note.ForceExtract;
46
import mx.kenzie.skript.api.syntax.Effect;
7+
import mx.kenzie.skript.compiler.CompileState;
58
import mx.kenzie.skript.compiler.Context;
69
import mx.kenzie.skript.compiler.Pattern;
710
import mx.kenzie.skript.compiler.SkriptLangSpec;
811
import mx.kenzie.skript.error.ScriptAssertionError;
912
import mx.kenzie.skript.lang.element.StandardElements;
1013
import mx.kenzie.skript.lang.handler.StandardHandlers;
1114

15+
import java.lang.reflect.Method;
16+
1217
public class AssertEffect extends Effect {
1318

1419
public AssertEffect() {
1520
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "assert %Boolean%");
16-
try {
17-
handlers.put(StandardHandlers.RUN, this.getClass().getMethod("assertion", Object.class));
18-
} catch (NoSuchMethodException e) {
19-
e.printStackTrace();
20-
}
21+
handlers.put(StandardHandlers.RUN, findMethod(AssertEffect.class, "assertion", Object.class, Class.class, int.class));
22+
}
23+
24+
@Override
25+
public void compile(Context context, Pattern.Match match) throws Throwable {
26+
final MethodBuilder method = context.getMethod();
27+
assert method != null;
28+
final Method target = handlers.get(StandardHandlers.RUN);
29+
assert target != null;
30+
final int line = context.lineNumber();
31+
method.writeCode(WriteInstruction.loadClassConstant(method.finish().getType()));
32+
method.writeCode(WriteInstruction.push(line));
33+
this.writeCall(method, target, context);
34+
context.setState(CompileState.CODE_BODY);
2135
}
2236

2337
@Override
@@ -27,13 +41,13 @@ public Pattern.Match match(String thing, Context context) {
2741
}
2842

2943
@ForceExtract
30-
public static void assertion(Object object) {
44+
public static void assertion(Object object, Class<?> script, int line) {
3145
if (object == null)
32-
throw new ScriptAssertionError();
46+
throw new ScriptAssertionError(script, line);
3347
else if (object instanceof Boolean boo && !boo)
34-
throw new ScriptAssertionError();
48+
throw new ScriptAssertionError(script, line);
3549
else if (object instanceof Number number && number.intValue() == 0)
36-
throw new ScriptAssertionError();
50+
throw new ScriptAssertionError(script, line);
3751
}
3852

3953
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package mx.kenzie.skript.lang.syntax.flow;
2+
3+
import mx.kenzie.foundation.MethodBuilder;
4+
import mx.kenzie.foundation.WriteInstruction;
5+
import mx.kenzie.skript.api.note.ForceExtract;
6+
import mx.kenzie.skript.api.syntax.Effect;
7+
import mx.kenzie.skript.compiler.CompileState;
8+
import mx.kenzie.skript.compiler.Context;
9+
import mx.kenzie.skript.compiler.Pattern;
10+
import mx.kenzie.skript.compiler.SkriptLangSpec;
11+
import mx.kenzie.skript.error.ScriptAssertionError;
12+
import mx.kenzie.skript.lang.element.StandardElements;
13+
import mx.kenzie.skript.lang.handler.StandardHandlers;
14+
15+
import java.lang.reflect.Method;
16+
17+
public class AssertWithErrorEffect extends Effect {
18+
19+
public AssertWithErrorEffect() {
20+
super(SkriptLangSpec.LIBRARY, StandardElements.EFFECT, "assert %Boolean%: %String%");
21+
handlers.put(StandardHandlers.RUN, findMethod(AssertWithErrorEffect.class, "assertion", Object.class, Object.class, Class.class, int.class));
22+
}
23+
24+
@Override
25+
public void compile(Context context, Pattern.Match match) throws Throwable {
26+
final MethodBuilder method = context.getMethod();
27+
assert method != null;
28+
final Method target = handlers.get(StandardHandlers.RUN);
29+
assert target != null;
30+
final int line = context.lineNumber();
31+
method.writeCode(WriteInstruction.loadClassConstant(method.finish().getType()));
32+
method.writeCode(WriteInstruction.push(line));
33+
this.writeCall(method, target, context);
34+
context.setState(CompileState.CODE_BODY);
35+
}
36+
37+
@Override
38+
public Pattern.Match match(String thing, Context context) {
39+
if (!thing.startsWith("assert ")) return null;
40+
if (!thing.contains(": ")) return null;
41+
return super.match(thing, context);
42+
}
43+
44+
@ForceExtract
45+
public static void assertion(Object object, Object message, Class<?> script, int line) {
46+
if (object == null)
47+
throw new ScriptAssertionError(script, line, message + "");
48+
else if (object instanceof Boolean boo && !boo)
49+
throw new ScriptAssertionError(script, line, message + "");
50+
else if (object instanceof Number number && number.intValue() == 0)
51+
throw new ScriptAssertionError(script, line, message + "");
52+
}
53+
54+
}

src/main/java/mx/kenzie/skript/lang/syntax/generic/CurrentScriptExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public CurrentScriptExpression() {
1818

1919
@Override
2020
public Type getReturnType() {
21-
return CommonTypes.EVENT;
21+
return CommonTypes.STRING;
2222
}
2323

2424
@Override

src/test/resources/atomics.bsk

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,52 @@
33
function basic_use:
44
trigger:
55
set {@var} to 6
6-
assert {@var} is 6
7-
assert {var} is null
6+
assert {@var} is 6: "Atomic retrieval failed."
7+
assert {var} is null: "Non-atomic var erroneously set."
88
set {@var} to {@var} + 1
9-
assert {@var} is 7
9+
assert {@var} is 7: "Atomic set/alteration failed."
1010

1111
function passed_use:
1212
trigger:
1313
set {@var} to 6
14-
assert {@var} is 6
14+
assert {@var} is 6: "Atomic retrieval failed."
1515
run a new runnable:
16-
assert {@var} is 6
16+
assert {@var} is 6: "Runnable retrieval failed."
1717
set {@var} to {@var} + 1
18-
assert {@var} is 7
19-
assert {@var} is 7
18+
assert {@var} is 7: "Runnable atomic change failed."
19+
assert {@var} is 7: "Runnable did not change atomic value."
2020

2121
function as_simple_parameter:
2222
trigger:
2323
set {@var} to 6
24-
assert {@var} is 6
24+
assert {@var} is 6: "Atomic retrieval failed."
2525
run test_param_normal({@var}) // @var is atomic-unwrapped
26-
assert {@var} is 6
26+
assert {@var} is 6: "Atomic unboxing failed - atomic value changed."
2727

2828
function as_atomic_parameter:
2929
trigger:
3030
set {@var} to 6
31-
assert {@var} is 6
31+
assert {@var} is 6: "Atomic retrieval failed."
3232
run test_param_atomic({@var})
33-
assert {@var} is 7
33+
assert {@var} is 7: "Atomic parameter use failed - atomic value did not change."
3434

3535
function reverse_case:
3636
trigger:
3737
set {var} to 6
38-
assert {var} is 6
38+
assert {var} is 6: "Variable retrieval failed."
3939
run test_param_atomic({var}) // var is atomic-wrapped
40-
assert {var} is 6
40+
assert {var} is 6: "Atomic boxing altered non-atomic var copy."
4141
run test_param_normal({var}) // var is unchanged
42-
assert {var} is 6
42+
assert {var} is 6: "Separate function altered non-atomic variable."
4343

4444
function test_param_normal (var):
4545
trigger:
46-
assert {var} is 6
46+
assert {var} is 6: "Parameter was not unboxed properly."
4747
set {var} to {var} + 1
48-
assert {var} is 7
48+
assert {var} is 7: "Variable failed to change."
4949

5050
function test_param_atomic (@var):
5151
trigger:
52-
assert {@var} is 6
52+
assert {@var} is 6: "Parameter was not boxed properly."
5353
set {@var} to {@var} + 1
54-
assert {@var} is 7
54+
assert {@var} is 7: "Atomic variable failed to update."

src/test/resources/events.bsk

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
on load:
22
trigger:
3-
print "Loaded."
3+
assert true
44

55
on any load:
66
trigger:
77
set {class} to class of event
88
set {name} to name of {class}
9-
print "The event class is: " + {name}
10-
print "The loaded script is: " + event-name
11-
print "The script's path is: " + event-script
9+
assert {class} exists: "Class-of failed."
10+
assert {name} is "Load": "Class name not retrieved."
11+
assert event-name is "events": "Event-name value not retrieved correctly."
12+
assert event-script is "skript.events": "Event-script value not retrieved correctly."
1213

1314
on any load:
1415
trigger:
15-
print "Done."
16+
assert true

0 commit comments

Comments
 (0)