feat(muscript-runtime): enhance GetOrAt heuristics
This commit is contained in:
parent
df78e10c6a
commit
1f51e9025d
|
@ -198,19 +198,19 @@ public class Runtime {
|
|||
}
|
||||
case GetOrAt(var location, var leftExpr, var nameOrIndex) -> {
|
||||
var left = evaluate(leftExpr, scope);
|
||||
var right = evaluate(nameOrIndex, scope);
|
||||
if (Dynamic.isNull(left)) throw locationalException(expr, "Could not get \"" + evaluate(nameOrIndex, scope) + "\" because left is null");
|
||||
if (left.isObject()) {
|
||||
var o = left.asObject();
|
||||
var n = evaluate(asString(nameOrIndex), scope);
|
||||
if (!o.has(n)) throw locationalException(expr, "Object does not contain \"" + n + "\"");
|
||||
yield o.get(n);
|
||||
} else if (left.isList()) {
|
||||
if (left.isList() && right.isNumber()) {
|
||||
var l = left.asList();
|
||||
int idx = (int) evaluate(asNumber(nameOrIndex), scope);
|
||||
if (idx < 0 || idx >= l.size()) throw locationalException(expr, "Index " + idx + " is out of range for list with size " + l.size());
|
||||
yield l.get(idx);
|
||||
}
|
||||
throw new DynamicTypeConversionException("object or list", left);
|
||||
} else if (left.isObject()) {
|
||||
var o = left.asObject();
|
||||
var n = right.asString().getValue();
|
||||
if (!o.has(n)) throw locationalException(expr, "Object does not contain \"" + n + "\"");
|
||||
yield o.get(n);
|
||||
} else throw new DynamicTypeConversionException("object or list", left);
|
||||
}
|
||||
case Bind(var location, var callable, var parameter) -> {
|
||||
var param = evaluate(parameter, scope);
|
||||
|
|
Loading…
Reference in New Issue