diff --git a/src/main/java/org/meteordev/starscript/Starscript.java b/src/main/java/org/meteordev/starscript/Starscript.java
index 5a226b9..c9e0ba1 100644
--- a/src/main/java/org/meteordev/starscript/Starscript.java
+++ b/src/main/java/org/meteordev/starscript/Starscript.java
@@ -154,6 +154,13 @@ public String popString(String errorMsg) {
return a.getString();
}
+ /** Pops a value from the stack and returns it as Object. Calls {@link Starscript#error(String, Object...)} with the provided message if the value is not Object. */
+ public Object popObject(String errorMsg) {
+ Value a = pop();
+ if (!a.isObject()) error(errorMsg);
+ return a.getString();
+ }
+
// Helpers
/** Throws a {@link StarscriptError}. */
@@ -198,6 +205,11 @@ public ValueMap set(String name, ValueMap map) {
return globals.set(name, map);
}
+ /** Sets an object variable supplier that always returns the same value for the provided name.
See {@link ValueMap#set(String, Supplier)} for dot notation. */
+ public ValueMap set(String name, Object object) {
+ return globals.set(name, object);
+ }
+
/** Removes all values from the globals. */
public void clear() {
globals.clear();
diff --git a/src/main/java/org/meteordev/starscript/value/Value.java b/src/main/java/org/meteordev/starscript/value/Value.java
index 087315e..0e21ff9 100644
--- a/src/main/java/org/meteordev/starscript/value/Value.java
+++ b/src/main/java/org/meteordev/starscript/value/Value.java
@@ -34,6 +34,9 @@ public static Value function(SFunction function) {
public static Value map(ValueMap fields) {
return new Map(fields);
}
+ public static Value object(java.lang.Object object) {
+ return new Object(object);
+ }
public boolean isNull() {
return type == ValueType.Null;
@@ -53,6 +56,9 @@ public boolean isFunction() {
public boolean isMap() {
return type == ValueType.Map;
}
+ public boolean isObject() {
+ return type == ValueType.Object;
+ }
public boolean getBool() {
return ((Boolean) this).bool;
@@ -69,6 +75,9 @@ public SFunction getFunction() {
public ValueMap getMap() {
return ((Map) this).fields;
}
+ public java.lang.Object getObject() {
+ return ((Object) this).object;
+ }
public boolean isTruthy() {
switch (type) {
@@ -78,12 +87,13 @@ public boolean isTruthy() {
case Number:
case String:
case Function:
- case Map: return true;
+ case Map:
+ case Object: return true;
}
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(java.lang.Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@@ -97,20 +107,22 @@ public boolean equals(Object o) {
case String: return getString().equals(value.getString());
case Function: return getFunction() == value.getFunction();
case Map: return getMap() == value.getMap();
+ case Object: return getObject().equals(value.getObject());
default: return false;
}
}
@Override
public int hashCode() {
- int result = super.hashCode();
+ int result = 31 * super.hashCode();
switch (type) {
- case Boolean: result = 31 * result + (getBool() ? 1 : 0); break;
- case Number: long temp = Double.doubleToLongBits(getNumber()); result = 31 * result + (int) (temp ^ (temp >>> 32)); break;
- case String: String string = getString(); result = 31 * result + string.hashCode(); break;
- case Function: result = 31 * result + getFunction().hashCode(); break;
- case Map: result = 31 * result + getMap().hashCode(); break;
+ case Boolean: result += java.lang.Boolean.hashCode(getBool()); break;
+ case Number: result += Double.hashCode(getNumber()); break;
+ case String: result += getString().hashCode(); break;
+ case Function: result += getFunction().hashCode(); break;
+ case Map: result += getMap().hashCode(); break;
+ case Object: result += getObject().hashCode(); break;
}
return result;
@@ -128,6 +140,7 @@ public String toString() {
Supplier s = getMap().getRaw("_toString");
return s == null ? "