Skip to content

Commit 2e434b4

Browse files
committed
Update built-in libraries for script
1 parent c951e67 commit 2e434b4

25 files changed

+755
-648
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.relogiclabs.jschema.exception;
2+
3+
import com.relogiclabs.jschema.message.ErrorDetail;
4+
5+
public class InvalidReceiverStateException extends CommonException {
6+
public InvalidReceiverStateException(ErrorDetail detail) {
7+
super(detail);
8+
}
9+
}

src/main/java/com/relogiclabs/jschema/exception/NotFoundClassException.java renamed to src/main/java/com/relogiclabs/jschema/exception/NoClassFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.relogiclabs.jschema.message.ErrorDetail;
44

5-
public class NotFoundClassException extends CommonException {
6-
public NotFoundClassException(ErrorDetail detail) {
5+
public class NoClassFoundException extends CommonException {
6+
public NoClassFoundException(ErrorDetail detail) {
77
super(detail);
88
}
99
}

src/main/java/com/relogiclabs/jschema/exception/NoValueReceivedException.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.relogiclabs.jschema.exception;
2+
3+
public class UpdateNotSupportedException extends CommonException {
4+
public UpdateNotSupportedException(String code, String message) {
5+
super(code, message);
6+
}
7+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.relogiclabs.jschema.internal.library;
2+
3+
import com.relogiclabs.jschema.internal.engine.ScriptScope;
4+
import com.relogiclabs.jschema.internal.script.GArray;
5+
import com.relogiclabs.jschema.internal.script.GInteger;
6+
import com.relogiclabs.jschema.type.EArray;
7+
import com.relogiclabs.jschema.type.EInteger;
8+
import com.relogiclabs.jschema.type.EType;
9+
import com.relogiclabs.jschema.type.EValue;
10+
import lombok.Getter;
11+
12+
import java.util.List;
13+
14+
import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.areEqual;
15+
import static com.relogiclabs.jschema.message.ErrorCode.AFND01;
16+
import static com.relogiclabs.jschema.message.ErrorCode.FILL01;
17+
import static com.relogiclabs.jschema.type.EUndefined.UNDEFINED;
18+
19+
public final class ArrayLibrary extends CommonLibrary {
20+
private static final String Length_M0 = "length#0";
21+
private static final String Find_Bn = "find";
22+
private static final String Find_M1 = "find#1";
23+
private static final String Find_M2 = "find#2";
24+
private static final String Fill_Bn = "fill";
25+
private static final String Fill_M2 = "fill#2";
26+
private static final String Copy_M0 = "copy#0";
27+
28+
private static final String Start_Id = "start";
29+
private static final String Length_Id = "length";
30+
31+
@Getter
32+
private static final ArrayLibrary instance = new ArrayLibrary();
33+
34+
private ArrayLibrary() {
35+
addMethod(Length_M0, ArrayLibrary::lengthMethod);
36+
addMethod(Find_M1, ArrayLibrary::findMethod1);
37+
addMethod(Find_M2, ArrayLibrary::findMethod2);
38+
addMethod(Fill_M2, ArrayLibrary::fillMethod);
39+
addMethod(Copy_M0, ArrayLibrary::copyMethod);
40+
}
41+
42+
@Override
43+
protected EType getType() {
44+
return EType.ARRAY;
45+
}
46+
47+
private static GInteger lengthMethod(EValue self, List<EValue> arguments, ScriptScope scope) {
48+
return GInteger.from(((EArray) self).size());
49+
}
50+
51+
private static EValue findMethod1(EValue self, List<EValue> arguments, ScriptScope scope) {
52+
var runtime = scope.getRuntime();
53+
var array = (EArray) self;
54+
var value = arguments.get(0);
55+
for(var i = 0; i < array.size(); i++)
56+
if(areEqual(array.get(i), value, runtime)) return GInteger.from(i);
57+
return UNDEFINED;
58+
}
59+
60+
private static EValue findMethod2(EValue self, List<EValue> arguments, ScriptScope scope) {
61+
var runtime = scope.getRuntime();
62+
var array = (EArray) self;
63+
var value = arguments.get(0);
64+
if(!(arguments.get(1) instanceof EInteger a1)) throw failOnInvalidArgumentType(AFND01,
65+
arguments.get(1), Find_Bn, Start_Id, self);
66+
var start = (int) a1.getValue();
67+
for(var i = start; i < array.size(); i++)
68+
if(areEqual(array.get(i), value, runtime)) return GInteger.from(i);
69+
return UNDEFINED;
70+
}
71+
72+
private static EValue fillMethod(EValue self, List<EValue> arguments, ScriptScope scope) {
73+
if(!(arguments.get(1) instanceof EInteger a1)) throw failOnInvalidArgumentType(FILL01,
74+
arguments.get(1), Fill_Bn, Length_Id, self);
75+
var length = (int) a1.getValue();
76+
return GArray.filledFrom(arguments.get(0), length);
77+
}
78+
79+
private static EValue copyMethod(EValue self, List<EValue> arguments, ScriptScope scope) {
80+
return new GArray(((EArray) self).elements());
81+
}
82+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.relogiclabs.jschema.internal.library;
2+
3+
import com.relogiclabs.jschema.exception.ScriptArgumentException;
4+
import com.relogiclabs.jschema.exception.ScriptCommonException;
5+
import com.relogiclabs.jschema.internal.engine.ScriptScope;
6+
import com.relogiclabs.jschema.internal.script.GString;
7+
import com.relogiclabs.jschema.type.EType;
8+
import com.relogiclabs.jschema.type.EValue;
9+
import lombok.Getter;
10+
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import static com.relogiclabs.jschema.internal.engine.ScriptTreeHelper.stringify;
16+
import static com.relogiclabs.jschema.internal.tree.EFunction.VARIADIC_ARITY;
17+
import static com.relogiclabs.jschema.message.ErrorCode.MNVK01;
18+
19+
public class CommonLibrary {
20+
private static final String Type_M0 = "type#0";
21+
private static final String String_M0 = "string#0";
22+
private final Map<String, MethodEvaluator> methods;
23+
24+
@Getter
25+
private static final CommonLibrary instance = new CommonLibrary();
26+
27+
CommonLibrary() {
28+
this.methods = new HashMap<>(20);
29+
addMethod(Type_M0, CommonLibrary::typeMethod);
30+
addMethod(String_M0, CommonLibrary::stringMethod);
31+
}
32+
33+
public MethodEvaluator getMethod(String name, int argCount) {
34+
var method = methods.get(name + "#" + argCount);
35+
if(method != null) return method;
36+
method = methods.get(name + "#" + VARIADIC_ARITY);
37+
if(method != null) return method;
38+
throw failOnMethodNotFound(name, argCount, getType());
39+
}
40+
41+
protected void addMethod(String name, MethodEvaluator evaluator) {
42+
methods.put(name, evaluator);
43+
}
44+
45+
protected EType getType() {
46+
return EType.ANY;
47+
}
48+
49+
private static GString typeMethod(EValue self, List<EValue> arguments, ScriptScope scope) {
50+
return GString.from(self.getType().getName());
51+
}
52+
53+
private static GString stringMethod(EValue self, List<EValue> arguments, ScriptScope scope) {
54+
return GString.from(stringify(self));
55+
}
56+
57+
private static ScriptCommonException failOnMethodNotFound(String name, int argCount, EType type) {
58+
return new ScriptCommonException(MNVK01, "Method '" + name + "' with " + argCount
59+
+ " parameter(s) of " + type + " not found");
60+
}
61+
62+
protected static ScriptArgumentException failOnInvalidArgumentType(String code, EValue argument,
63+
String method, String parameter, EValue self) {
64+
return new ScriptArgumentException(code, "Invalid " + argument.getType() + " argument for '"
65+
+ parameter + "' parameter in '" + method + "' method of " + self.getType());
66+
}
67+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.relogiclabs.jschema.internal.library;
2+
3+
import com.relogiclabs.jschema.internal.engine.ScriptScope;
4+
import com.relogiclabs.jschema.type.EValue;
5+
6+
import java.util.List;
7+
8+
@FunctionalInterface
9+
public interface FunctionEvaluator {
10+
EValue evaluate(ScriptScope scope, List<EValue> arguments);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.relogiclabs.jschema.internal.library;
2+
3+
import com.relogiclabs.jschema.internal.engine.ScriptScope;
4+
import com.relogiclabs.jschema.type.EValue;
5+
6+
import java.util.List;
7+
8+
@FunctionalInterface
9+
public interface MethodEvaluator {
10+
EValue evaluate(EValue self, List<EValue> arguments, ScriptScope scope);
11+
}

src/main/java/com/relogiclabs/jschema/internal/library/NFunction.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/com/relogiclabs/jschema/internal/library/NHandler.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)