Skip to content

Commit

Permalink
Added Value.object()
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Dec 6, 2022
1 parent e76027b commit f928308
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/meteordev/starscript/Starscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}. */
Expand Down Expand Up @@ -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. <br><br> 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();
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/org/meteordev/starscript/value/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -128,6 +140,7 @@ public String toString() {
Supplier<Value> s = getMap().getRaw("_toString");
return s == null ? "<map>" : s.get().toString();
}
case Object: return getObject().toString();
default: return "";
}
}
Expand Down Expand Up @@ -176,4 +189,13 @@ public Map(ValueMap fields) {
this.fields = fields;
}
}

private static class Object extends Value {
private final java.lang.Object object;

public Object(java.lang.Object object) {
super(ValueType.Object);
this.object = object;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/meteordev/starscript/value/ValueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public ValueMap set(String name, ValueMap map) {
return set(name, Value.map(map));
}

/** Sets an object variable supplier that always returns the same value for the provided name. <br><br> See {@link #set(String, Supplier)} for dot notation. */
public ValueMap set(String name, Object object) {
return set(name, Value.object(object));
}

/**
* Gets the variable supplier for the provided name. <br><br>
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/meteordev/starscript/value/ValueType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public enum ValueType {
Number,
String,
Function,
Map
Map,
Object
}

0 comments on commit f928308

Please sign in to comment.