Null check in unpack
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-06-29 15:14:01 +02:00
parent 487f063cb0
commit 8665f69228
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 21 additions and 5 deletions

View File

@ -32,7 +32,7 @@ public class Call extends DynamicExpr {
DList arg;
try {
Dynamic<?> lv = left.get(dataRoot);
if (lv == null || lv instanceof DNull) throw new LocationalException(location, "Cannot invoke null");
if (Dynamic.isNull(lv)) throw new LocationalException(location, "Cannot invoke null");
dc = lv.asCallable();
arg = DFinal.of(args.stream().flatMap(e -> e.get(dataRoot)).toArray(Dynamic[]::new));
} catch (DynamicTypeConversionException e) {

View File

@ -31,7 +31,7 @@ public class Get extends DynamicExpr {
@Override
public Dynamic<?> get(Scope dataRoot) {
Dynamic<?> left = this.left.get(dataRoot);
if (left == null || left instanceof DNull) throw new LocationalException(location, "Could not get \"" + name.asStringExpr().get(dataRoot) + "\" because left is null");
if (Dynamic.isNull(left)) throw new LocationalException(location, "Could not get \"" + name.asStringExpr().get(dataRoot) + "\" because left is null");
if (left instanceof DObject o) {
return o.get(name.asStringExpr().get(dataRoot));
} else if (left instanceof DList l) {

View File

@ -3,12 +3,14 @@ package io.gitlab.jfronny.muscript.ast.dynamic.unpack;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.data.dynamic.DynamicTypeConversionException;
import io.gitlab.jfronny.muscript.ast.BoolExpr;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.annotations.CanThrow;
import io.gitlab.jfronny.muscript.annotations.UncheckedDynamic;
import io.gitlab.jfronny.muscript.ast.dynamic.DynamicCoerce;
import io.gitlab.jfronny.muscript.error.LocationalException;
import java.io.IOException;
@ -25,7 +27,9 @@ public class BoolUnpack extends BoolExpr {
@Override
public Boolean get(Scope dataRoot) {
try {
return inner.get(dataRoot).asBool().getValue();
Dynamic<?> iv = inner.get(dataRoot);
if (Dynamic.isNull(iv)) throw new LocationalException(location, "Cannot unpack null");
return iv.asBool().getValue();
} catch (DynamicTypeConversionException e) {
throw e.locational(location);
}

View File

@ -3,12 +3,14 @@ package io.gitlab.jfronny.muscript.ast.dynamic.unpack;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.data.dynamic.DynamicTypeConversionException;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.NumberExpr;
import io.gitlab.jfronny.muscript.annotations.CanThrow;
import io.gitlab.jfronny.muscript.annotations.UncheckedDynamic;
import io.gitlab.jfronny.muscript.ast.dynamic.DynamicCoerce;
import io.gitlab.jfronny.muscript.error.LocationalException;
import java.io.IOException;
@ -25,7 +27,9 @@ public class NumberUnpack extends NumberExpr {
@Override
public Double get(Scope dataRoot) {
try {
return inner.get(dataRoot).asNumber().getValue();
Dynamic<?> iv = inner.get(dataRoot);
if (Dynamic.isNull(iv)) throw new LocationalException(location, "Cannot unpack null");
return iv.asNumber().getValue();
} catch (DynamicTypeConversionException e) {
throw e.locational(location);
}

View File

@ -3,12 +3,14 @@ package io.gitlab.jfronny.muscript.ast.dynamic.unpack;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.data.dynamic.DynamicTypeConversionException;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.StringExpr;
import io.gitlab.jfronny.muscript.annotations.CanThrow;
import io.gitlab.jfronny.muscript.annotations.UncheckedDynamic;
import io.gitlab.jfronny.muscript.ast.dynamic.DynamicCoerce;
import io.gitlab.jfronny.muscript.error.LocationalException;
import java.io.IOException;
@ -25,7 +27,9 @@ public class StringUnpack extends StringExpr {
@Override
public String get(Scope dataRoot) {
try {
return inner.get(dataRoot).asString().getValue();
Dynamic<?> iv = inner.get(dataRoot);
if (Dynamic.isNull(iv)) throw new LocationalException(location, "Cannot unpack null");
return iv.asString().getValue();
} catch (DynamicTypeConversionException e) {
throw e.locational(location);
}

View File

@ -36,6 +36,10 @@ public sealed interface Dynamic<T> permits DBool, DNumber, DString, DObject, DLi
return sb.toString();
}
static boolean isNull(Dynamic<?> dynamic) {
return dynamic == null || dynamic instanceof DNull;
}
default void serialize(ExprWriter writer) throws IOException {
toExpr().decompile(writer);
}