Skip to content

Commit b11761a

Browse files
committed
Small fixes
1 parent f772173 commit b11761a

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

modules/main/src/main/java/com/annimon/ownlang/modules/http/http_http.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.annimon.ownlang.modules.http;
22

33
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
4-
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65
import java.io.IOException;
76
import java.io.UnsupportedEncodingException;
@@ -31,65 +30,59 @@ public final class http_http implements Function {
3130
@Override
3231
public Value execute(Value[] args) {
3332
String url, method;
33+
Function function;
3434
switch (args.length) {
3535
case 1: // http(url)
3636
url = args[0].asString();
3737
return process(url, "GET");
38-
38+
3939
case 2: // http(url, method) || http(url, callback)
4040
url = args[0].asString();
4141
if (args[1].type() == Types.FUNCTION) {
42-
return process(url, "GET", (FunctionValue) args[1]);
42+
return process(url, "GET", ValueUtils.consumeFunction(args[1], 1));
4343
}
4444
return process(url, args[1].asString());
45-
45+
4646
case 3: // http(url, method, params) || http(url, method, callback)
4747
url = args[0].asString();
4848
method = args[1].asString();
4949
if (args[2].type() == Types.FUNCTION) {
50-
return process(url, method, (FunctionValue) args[2]);
50+
return process(url, method, ValueUtils.consumeFunction(args[2], 2));
5151
}
52-
return process(url, method, args[2], FunctionValue.EMPTY);
53-
52+
return process(url, method, args[2], FunctionValue.EMPTY.getValue());
53+
5454
case 4: // http(url, method, params, callback)
55-
if (args[3].type() != Types.FUNCTION) {
56-
throw new TypeException("Fourth arg must be a function callback");
57-
}
5855
url = args[0].asString();
5956
method = args[1].asString();
60-
return process(url, method, args[2], (FunctionValue) args[3]);
61-
57+
function = ValueUtils.consumeFunction(args[3], 3);
58+
return process(url, method, args[2], function);
59+
6260
case 5: // http(url, method, params, headerParams, callback)
63-
if (args[3].type() != Types.MAP) {
64-
throw new TypeException("Third arg must be a map");
65-
}
66-
if (args[4].type() != Types.FUNCTION) {
67-
throw new TypeException("Fifth arg must be a function callback");
68-
}
6961
url = args[0].asString();
7062
method = args[1].asString();
71-
return process(url, method, args[2], (MapValue) args[3], (FunctionValue) args[4]);
72-
63+
MapValue options = ValueUtils.consumeMap(args[3], 3);
64+
function = ValueUtils.consumeFunction(args[4], 4);
65+
return process(url, method, args[2], options, function);
66+
7367
default:
7468
throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
7569
}
7670
}
7771

7872
private Value process(String url, String method) {
79-
return process(url, method, FunctionValue.EMPTY);
73+
return process(url, method, FunctionValue.EMPTY.getValue());
8074
}
8175

82-
private Value process(String url, String method, FunctionValue function) {
83-
return process(url, method, MapValue.EMPTY, function);
76+
private Value process(String url, String method, Function callback) {
77+
return process(url, method, MapValue.EMPTY, callback);
8478
}
8579

86-
private Value process(String url, String method, Value params, FunctionValue function) {
87-
return process(url, method, params, MapValue.EMPTY, function);
80+
private Value process(String url, String method, Value params, Function callback) {
81+
return process(url, method, params, MapValue.EMPTY, callback);
8882
}
8983

90-
private Value process(String url, String methodStr, Value requestParams, MapValue options, FunctionValue function) {
84+
private Value process(String url, String methodStr, Value requestParams, MapValue options, Function callback) {
9185
final String method = methodStr.toUpperCase();
92-
final Function callback = function.getValue();
9386
try {
9487
final Request.Builder builder = new Request.Builder()
9588
.url(url)

modules/main/src/main/java/com/annimon/ownlang/modules/robot/robot_exec.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,17 @@ public Value execute(Value[] args) {
2525
final Process process;
2626
if (args.length > 1) {
2727
process = Runtime.getRuntime().exec(toStringArray(args));
28-
} else switch (args[0].type()) {
29-
case Types.ARRAY:
30-
final ArrayValue array = (ArrayValue) args[0];
31-
process = Runtime.getRuntime().exec(toStringArray(array.getCopyElements()));
32-
break;
33-
34-
default:
35-
process = Runtime.getRuntime().exec(args[0].asString());
28+
} else if (args[0].type() == Types.ARRAY) {
29+
final ArrayValue array = (ArrayValue) args[0];
30+
process = Runtime.getRuntime().exec(toStringArray(array.getCopyElements()));
31+
} else {
32+
process = Runtime.getRuntime().exec(args[0].asString());
3633
}
37-
38-
switch (mode) {
39-
case EXEC_AND_WAIT:
40-
return NumberValue.of(process.waitFor());
41-
case EXEC:
42-
default:
43-
return NumberValue.ZERO;
34+
35+
if (mode == Mode.EXEC_AND_WAIT) {
36+
return NumberValue.of(process.waitFor());
4437
}
38+
return NumberValue.ZERO;
4539
} catch (Exception ex) {
4640
return NumberValue.ZERO;
4741
}

ownlang-core/src/main/java/com/annimon/ownlang/lib/NumberValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ public boolean equals(Object obj) {
120120
return Float.compare(value.floatValue(), other.floatValue()) == 0;
121121
}
122122
if (value instanceof Long || other instanceof Long) {
123-
return Long.compare(value.longValue(), other.longValue()) == 0;
123+
return value.longValue() == other.longValue();
124124
}
125-
return Integer.compare(value.intValue(), other.intValue()) == 0;
125+
return value.intValue() == other.intValue();
126126
}
127127

128128
@Override

ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private static void run(RunOptions options) {
178178
System.out.println(stagesData.getOrDefault(OptimizationStage.TAG_OPTIMIZATION_SUMMARY, ""));
179179
}
180180
if (options.showMeasurements) {
181-
System.out.println("======================");
181+
System.out.println("=".repeat(25));
182182
System.out.println(measurement.summary(TimeUnit.MILLISECONDS, true));
183183
}
184184
}

0 commit comments

Comments
 (0)