Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/main/java/com/annimon/ownlang/lib/ClassInstanceValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ClassInstanceValue implements Value {
private final String className;
private final MapValue thisMap;
private ClassMethod constructor;
private UserDefinedFunction toString;

public ClassInstanceValue(String name) {
this.className = name;
Expand All @@ -17,36 +18,40 @@ public ClassInstanceValue(String name) {
public MapValue getThisMap() {
return thisMap;
}

public String getClassName() {
return className;
}

public void addField(String name, Value value) {
thisMap.set(name, value);
}

public void addMethod(String name, ClassMethod method) {
if (name.equals("toString")) {
toString = method;
}
thisMap.set(name, method);
if (name.equals(className)) {
constructor = method;
}
}



public void callConstructor(Value[] args) {
if (constructor != null) {
constructor.execute(args);
}
}

public Value access(Value value) {
return thisMap.get(value);
}

public void set(Value key, Value value) {
final Value v = thisMap.get(key);
if (v == null) {
throw new RuntimeException("Unable to add new field "
throw new RuntimeException("Unable to add new field "
+ key.asString() + " to class " + className);
}
thisMap.set(key, value);
Expand All @@ -69,14 +74,17 @@ public double asNumber() {

@Override
public String asString() {
return "class " + className + "@" + thisMap;
if (toString != null) {
return toString.execute(new Value[] {}).asString();
}
return className + "@" + thisMap;
}

@Override
public int type() {
return Types.CLASS;
}

@Override
public int hashCode() {
int hash = 5;
Expand All @@ -99,7 +107,7 @@ public boolean equals(Object obj) {
public int compareTo(Value o) {
return asString().compareTo(o.asString());
}

@Override
public String toString() {
return asString();
Expand Down