mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Small fixes
This commit is contained in:
parent
f772173df1
commit
b11761a99a
@ -1,7 +1,6 @@
|
|||||||
package com.annimon.ownlang.modules.http;
|
package com.annimon.ownlang.modules.http;
|
||||||
|
|
||||||
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||||
import com.annimon.ownlang.exceptions.TypeException;
|
|
||||||
import com.annimon.ownlang.lib.*;
|
import com.annimon.ownlang.lib.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
@ -31,65 +30,59 @@ public final class http_http implements Function {
|
|||||||
@Override
|
@Override
|
||||||
public Value execute(Value[] args) {
|
public Value execute(Value[] args) {
|
||||||
String url, method;
|
String url, method;
|
||||||
|
Function function;
|
||||||
switch (args.length) {
|
switch (args.length) {
|
||||||
case 1: // http(url)
|
case 1: // http(url)
|
||||||
url = args[0].asString();
|
url = args[0].asString();
|
||||||
return process(url, "GET");
|
return process(url, "GET");
|
||||||
|
|
||||||
case 2: // http(url, method) || http(url, callback)
|
case 2: // http(url, method) || http(url, callback)
|
||||||
url = args[0].asString();
|
url = args[0].asString();
|
||||||
if (args[1].type() == Types.FUNCTION) {
|
if (args[1].type() == Types.FUNCTION) {
|
||||||
return process(url, "GET", (FunctionValue) args[1]);
|
return process(url, "GET", ValueUtils.consumeFunction(args[1], 1));
|
||||||
}
|
}
|
||||||
return process(url, args[1].asString());
|
return process(url, args[1].asString());
|
||||||
|
|
||||||
case 3: // http(url, method, params) || http(url, method, callback)
|
case 3: // http(url, method, params) || http(url, method, callback)
|
||||||
url = args[0].asString();
|
url = args[0].asString();
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
if (args[2].type() == Types.FUNCTION) {
|
if (args[2].type() == Types.FUNCTION) {
|
||||||
return process(url, method, (FunctionValue) args[2]);
|
return process(url, method, ValueUtils.consumeFunction(args[2], 2));
|
||||||
}
|
}
|
||||||
return process(url, method, args[2], FunctionValue.EMPTY);
|
return process(url, method, args[2], FunctionValue.EMPTY.getValue());
|
||||||
|
|
||||||
case 4: // http(url, method, params, callback)
|
case 4: // http(url, method, params, callback)
|
||||||
if (args[3].type() != Types.FUNCTION) {
|
|
||||||
throw new TypeException("Fourth arg must be a function callback");
|
|
||||||
}
|
|
||||||
url = args[0].asString();
|
url = args[0].asString();
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
return process(url, method, args[2], (FunctionValue) args[3]);
|
function = ValueUtils.consumeFunction(args[3], 3);
|
||||||
|
return process(url, method, args[2], function);
|
||||||
|
|
||||||
case 5: // http(url, method, params, headerParams, callback)
|
case 5: // http(url, method, params, headerParams, callback)
|
||||||
if (args[3].type() != Types.MAP) {
|
|
||||||
throw new TypeException("Third arg must be a map");
|
|
||||||
}
|
|
||||||
if (args[4].type() != Types.FUNCTION) {
|
|
||||||
throw new TypeException("Fifth arg must be a function callback");
|
|
||||||
}
|
|
||||||
url = args[0].asString();
|
url = args[0].asString();
|
||||||
method = args[1].asString();
|
method = args[1].asString();
|
||||||
return process(url, method, args[2], (MapValue) args[3], (FunctionValue) args[4]);
|
MapValue options = ValueUtils.consumeMap(args[3], 3);
|
||||||
|
function = ValueUtils.consumeFunction(args[4], 4);
|
||||||
|
return process(url, method, args[2], options, function);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
|
throw new ArgumentsMismatchException("From 1 to 5 arguments expected, got " + args.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Value process(String url, String method) {
|
private Value process(String url, String method) {
|
||||||
return process(url, method, FunctionValue.EMPTY);
|
return process(url, method, FunctionValue.EMPTY.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Value process(String url, String method, FunctionValue function) {
|
private Value process(String url, String method, Function callback) {
|
||||||
return process(url, method, MapValue.EMPTY, function);
|
return process(url, method, MapValue.EMPTY, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Value process(String url, String method, Value params, FunctionValue function) {
|
private Value process(String url, String method, Value params, Function callback) {
|
||||||
return process(url, method, params, MapValue.EMPTY, function);
|
return process(url, method, params, MapValue.EMPTY, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Value process(String url, String methodStr, Value requestParams, MapValue options, FunctionValue function) {
|
private Value process(String url, String methodStr, Value requestParams, MapValue options, Function callback) {
|
||||||
final String method = methodStr.toUpperCase();
|
final String method = methodStr.toUpperCase();
|
||||||
final Function callback = function.getValue();
|
|
||||||
try {
|
try {
|
||||||
final Request.Builder builder = new Request.Builder()
|
final Request.Builder builder = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
|
@ -25,23 +25,17 @@ public final class robot_exec implements Function {
|
|||||||
final Process process;
|
final Process process;
|
||||||
if (args.length > 1) {
|
if (args.length > 1) {
|
||||||
process = Runtime.getRuntime().exec(toStringArray(args));
|
process = Runtime.getRuntime().exec(toStringArray(args));
|
||||||
} else switch (args[0].type()) {
|
} else if (args[0].type() == Types.ARRAY) {
|
||||||
case Types.ARRAY:
|
final ArrayValue array = (ArrayValue) args[0];
|
||||||
final ArrayValue array = (ArrayValue) args[0];
|
process = Runtime.getRuntime().exec(toStringArray(array.getCopyElements()));
|
||||||
process = Runtime.getRuntime().exec(toStringArray(array.getCopyElements()));
|
} else {
|
||||||
break;
|
process = Runtime.getRuntime().exec(args[0].asString());
|
||||||
|
|
||||||
default:
|
|
||||||
process = Runtime.getRuntime().exec(args[0].asString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (mode) {
|
if (mode == Mode.EXEC_AND_WAIT) {
|
||||||
case EXEC_AND_WAIT:
|
return NumberValue.of(process.waitFor());
|
||||||
return NumberValue.of(process.waitFor());
|
|
||||||
case EXEC:
|
|
||||||
default:
|
|
||||||
return NumberValue.ZERO;
|
|
||||||
}
|
}
|
||||||
|
return NumberValue.ZERO;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
return NumberValue.ZERO;
|
return NumberValue.ZERO;
|
||||||
}
|
}
|
||||||
|
@ -120,9 +120,9 @@ public final class NumberValue implements Value {
|
|||||||
return Float.compare(value.floatValue(), other.floatValue()) == 0;
|
return Float.compare(value.floatValue(), other.floatValue()) == 0;
|
||||||
}
|
}
|
||||||
if (value instanceof Long || other instanceof Long) {
|
if (value instanceof Long || other instanceof Long) {
|
||||||
return Long.compare(value.longValue(), other.longValue()) == 0;
|
return value.longValue() == other.longValue();
|
||||||
}
|
}
|
||||||
return Integer.compare(value.intValue(), other.intValue()) == 0;
|
return value.intValue() == other.intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -178,7 +178,7 @@ public final class Main {
|
|||||||
System.out.println(stagesData.getOrDefault(OptimizationStage.TAG_OPTIMIZATION_SUMMARY, ""));
|
System.out.println(stagesData.getOrDefault(OptimizationStage.TAG_OPTIMIZATION_SUMMARY, ""));
|
||||||
}
|
}
|
||||||
if (options.showMeasurements) {
|
if (options.showMeasurements) {
|
||||||
System.out.println("======================");
|
System.out.println("=".repeat(25));
|
||||||
System.out.println(measurement.summary(TimeUnit.MILLISECONDS, true));
|
System.out.println(measurement.summary(TimeUnit.MILLISECONDS, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user