fix JIO calls
This commit is contained in:
parent
9db8d592d7
commit
120e827d32
1 changed files with 82 additions and 28 deletions
|
@ -3,12 +3,7 @@ import java.lang.reflect.*;
|
||||||
import java.net.BindException;
|
import java.net.BindException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Stack;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
@ -921,14 +916,10 @@ public class ISBPL {
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case "jio.context":
|
case "jio.context":
|
||||||
func = (stack) -> {
|
func = (stack) -> stack.push(toISBPL(this));
|
||||||
stack.push(toISBPL(this));
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
case "null":
|
case "null":
|
||||||
func = (stack) -> {
|
func = (stack) -> stack.push(new ISBPLObject(getType("null"), 0));
|
||||||
stack.push(new ISBPLObject(getType("null"), 0));
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
case "delete":
|
case "delete":
|
||||||
func = (stack) -> {
|
func = (stack) -> {
|
||||||
|
@ -992,15 +983,28 @@ public class ISBPL {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
HashMap<String, ArrayList<Method>> methods = new HashMap<>();
|
||||||
for (Method method : clazz.getDeclaredMethods()) {
|
for (Method method : clazz.getDeclaredMethods()) {
|
||||||
addFunction(type, method.getName() + method.getParameterCount(), stack -> {
|
ArrayList<Method> methodList = methods.get(method.getName() + method.getParameterCount());
|
||||||
method.setAccessible(true);
|
if(methodList == null)
|
||||||
|
methodList = new ArrayList<>();
|
||||||
|
methodList.add(method);
|
||||||
|
methods.put(method.getName() + method.getParameterCount(), methodList);
|
||||||
|
}
|
||||||
|
for (Map.Entry<String, ArrayList<Method>> entry : methods.entrySet()) {
|
||||||
|
addFunction(type, entry.getKey(), stack -> {
|
||||||
Object o = stack.pop().object;
|
Object o = stack.pop().object;
|
||||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
// Resolve
|
||||||
Object[] params = new Object[method.getParameterCount()];
|
AtomicInteger mid = new AtomicInteger(0);
|
||||||
for (int i = params.length - 1 ; i >= 0 ; i--) {
|
ArrayList<Method> ms = entry.getValue();
|
||||||
params[i] = fromISBPL(stack.pop(), parameterTypes[i]);
|
Class<?>[][] paramTypes = new Class<?>[ms.size()][ms.get(0).getParameterCount()];
|
||||||
|
for (int i = 0 ; i < ms.size() ; i++) {
|
||||||
|
paramTypes[i] = ms.get(i).getParameterTypes();
|
||||||
}
|
}
|
||||||
|
Object[] params = resolve(mid, stack, entry.getValue().get(0).getParameterCount(), paramTypes);
|
||||||
|
Method method = ms.get(mid.get());
|
||||||
|
|
||||||
|
method.setAccessible(true);
|
||||||
if(debug)
|
if(debug)
|
||||||
System.err.println("Java Call: " + method + " - " + Arrays.toString(params));
|
System.err.println("Java Call: " + method + " - " + Arrays.toString(params));
|
||||||
try {
|
try {
|
||||||
|
@ -1015,15 +1019,28 @@ public class ISBPL {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
HashMap<Integer, ArrayList<Constructor<?>>> constructors = new HashMap<>();
|
||||||
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
|
||||||
addFunction(type, "new" + constructor.getParameterCount(), stack -> {
|
ArrayList<Constructor<?>> constructorList = constructors.get(constructor.getParameterCount());
|
||||||
constructor.setAccessible(true);
|
if (constructorList == null)
|
||||||
|
constructorList = new ArrayList<>();
|
||||||
|
constructorList.add(constructor);
|
||||||
|
constructors.put(constructor.getParameterCount(), constructorList);
|
||||||
|
}
|
||||||
|
for (Map.Entry<Integer, ArrayList<Constructor<?>>> entry : constructors.entrySet()) {
|
||||||
|
addFunction(type, "new" + entry.getKey(), stack -> {
|
||||||
stack.pop();
|
stack.pop();
|
||||||
Class<?>[] parameterTypes = constructor.getParameterTypes();
|
|
||||||
Object[] params = new Object[constructor.getParameterCount()];
|
AtomicInteger mid = new AtomicInteger(0);
|
||||||
for (int i = params.length - 1 ; i >= 0 ; i--) {
|
ArrayList<Constructor<?>> ms = entry.getValue();
|
||||||
params[i] = fromISBPL(stack.pop(), parameterTypes[i]);
|
Class<?>[][] paramTypes = new Class<?>[ms.size()][ms.get(0).getParameterCount()];
|
||||||
|
for (int i = 0 ; i < ms.size() ; i++) {
|
||||||
|
paramTypes[i] = ms.get(i).getParameterTypes();
|
||||||
}
|
}
|
||||||
|
Object[] params = resolve(mid, stack, entry.getValue().get(0).getParameterCount(), paramTypes);
|
||||||
|
Constructor<?> constructor = ms.get(mid.get());
|
||||||
|
|
||||||
|
constructor.setAccessible(true);
|
||||||
if(debug)
|
if(debug)
|
||||||
System.err.println("Java Call: new - " + Arrays.toString(params));
|
System.err.println("Java Call: new - " + Arrays.toString(params));
|
||||||
try {
|
try {
|
||||||
|
@ -1041,6 +1058,37 @@ public class ISBPL {
|
||||||
return new ISBPLObject(type, null);
|
return new ISBPLObject(type, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object[] resolve(AtomicInteger mid, Stack<ISBPLObject> stack, int paramCount, Class<?>[][] paramTypes) {
|
||||||
|
ISBPLObject[] o = new ISBPLObject[paramCount];
|
||||||
|
Object[][] params = new Object[paramTypes.length][paramCount];
|
||||||
|
int[] methodIDs = new int[paramCount];
|
||||||
|
Arrays.fill(methodIDs, -1);
|
||||||
|
for (int i = params[0].length - 1 ; i >= 0 ; i--) {
|
||||||
|
o[i] = stack.pop();
|
||||||
|
for (int j = 0 ; j < paramTypes.length ; j++) {
|
||||||
|
Class<?>[] m = paramTypes[j];
|
||||||
|
try {
|
||||||
|
params[j][i] = fromISBPL(o[i], m[i]);
|
||||||
|
methodIDs[i] = 1 << j;
|
||||||
|
}
|
||||||
|
catch (ISBPLError ignored) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int msize = paramTypes.length;
|
||||||
|
for (int i = 0 ; i < msize; i++) {
|
||||||
|
boolean works = true;
|
||||||
|
for (int j = 0 ; j < methodIDs.length ; j++) {
|
||||||
|
if ((methodIDs[j] & 1 << i) == 0) {
|
||||||
|
works = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(works)
|
||||||
|
mid.set(i);
|
||||||
|
}
|
||||||
|
return params[mid.get()];
|
||||||
|
}
|
||||||
|
|
||||||
private Object fromISBPL(ISBPLObject o) {
|
private Object fromISBPL(ISBPLObject o) {
|
||||||
ISBPLType type = o.type;
|
ISBPLType type = o.type;
|
||||||
if (type.equals(getType("null"))) {
|
if (type.equals(getType("null"))) {
|
||||||
|
@ -1064,8 +1112,12 @@ public class ISBPL {
|
||||||
if (type.equals(getType("null"))) {
|
if (type.equals(getType("null"))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (type.equals(getType("string")))
|
if (type.equals(getType("string"))) {
|
||||||
return toJavaString(o);
|
if(expectedType.isAssignableFrom(String.class))
|
||||||
|
return toJavaString(o);
|
||||||
|
else
|
||||||
|
typeError("string", expectedType.getName());
|
||||||
|
}
|
||||||
if (type.equals(getType("array"))) {
|
if (type.equals(getType("array"))) {
|
||||||
ISBPLObject[] isbplArray = ((ISBPLObject[]) o.object);
|
ISBPLObject[] isbplArray = ((ISBPLObject[]) o.object);
|
||||||
Object array = new Object[isbplArray.length];
|
Object array = new Object[isbplArray.length];
|
||||||
|
@ -1078,6 +1130,8 @@ public class ISBPL {
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
if(!expectedType.isAssignableFrom(o.object.getClass()))
|
||||||
|
typeError(o.type.name, expectedType.getName());
|
||||||
return o.object;
|
return o.object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue