fix some multithreading bugs, better trycatch

This commit is contained in:
Daniella / Tove 2022-04-27 01:48:46 +02:00
parent fc4c4914b9
commit d2d7abaaaa

View file

@ -21,7 +21,11 @@ public class ISBPL {
public ISBPLDebugger.IPC debuggerIPC = new ISBPLDebugger.IPC();
ArrayList<ISBPLType> types = new ArrayList<>();
HashMap<String, ISBPLCallable> level0 = new HashMap<>();
final ISBPLThreadLocal<Stack<HashMap<String, ISBPLCallable>>> functionStack = ISBPLThreadLocal.withInitial(() -> { Stack<HashMap<String, ISBPLCallable>> stack = new Stack<>(); stack.push(level0); return stack; });
final ISBPLThreadLocal<Stack<HashMap<String, ISBPLCallable>>> functionStack = ISBPLThreadLocal.withInitial(() -> {
Stack<HashMap<String, ISBPLCallable>> stack = new Stack<>();
stack.push(level0);
return stack;
});
final ISBPLThreadLocal<Stack<File>> fileStack = ISBPLThreadLocal.withInitial(Stack::new);
HashMap<ISBPLCallable, Object> varLinks = new HashMap<>();
HashMap<Object, ISBPLObject> vars = new HashMap<>();
@ -31,6 +35,7 @@ public class ISBPL {
ArrayList<String> included = new ArrayList<>();
HashMap<String, ISBPLCallable> natives = new HashMap<>();
private Object syncMakeThread = new Object();
private ISBPLKeyword getKeyword(String word) {
switch (word) {
case "native":
@ -113,6 +118,15 @@ public class ISBPL {
else {
throw error;
}
} catch (Exception e) {
if (Arrays.asList(allowed).contains("Java") || allowed.length == 1 && allowed[0].equals("all")) {
stack.push(toISBPL(e));
stack.push(toISBPLString(e.getClass().getName()));
stack.push(toISBPLString("Java"));
}
else {
throw e;
}
}
return i.get();
};
@ -148,12 +162,19 @@ public class ISBPL {
};
case "fork":
return (idx, words, file, stack) -> {
synchronized(syncMakeThread) {
idx++;
AtomicInteger i = new AtomicInteger(idx);
Stack<ISBPLObject> s = (Stack<ISBPLObject>) stack.clone();
Stack<ISBPLObject> s = new Stack<>();
for(ISBPLObject obj : stack) {
s.push(obj);
}
ISBPLCallable block = readBlock(i, words, file, false);
long tid = Thread.currentThread().getId();
Stack<HashMap<String, ISBPLCallable>> fs = (Stack<HashMap<String, ISBPLCallable>>) functionStack.get().clone();
Stack<HashMap<String, ISBPLCallable>> fs = new Stack<>();
for(HashMap<String, ISBPLCallable> map : functionStack.get()) {
fs.push(map);
}
new Thread(() -> {
debuggerIPC.run.put(Thread.currentThread().getId(), debuggerIPC.run.getOrDefault(tid, -1) == -1 ? -1 : 0);
debuggerIPC.stack.put(Thread.currentThread().getId(), s);
@ -167,6 +188,7 @@ public class ISBPL {
}
}).start();
return i.get();
}
};
default:
return null;
@ -950,7 +972,9 @@ public class ISBPL {
return nullObj;
}
private Object syncJIOClass = new Object();
public ISBPLObject toISBPL(Class<?> clazz) {
synchronized(syncJIOClass) {
ISBPLType type = getType(clazz.getName());
if(type == null) {
type = registerType(clazz.getName());
@ -1066,6 +1090,7 @@ public class ISBPL {
}
return new ISBPLObject(type, null);
}
}
public Object[] resolve(AtomicInteger mid, Stack<ISBPLObject> stack, int paramCount, Class<?>[][] paramTypes) {
ISBPLObject[] o = new ISBPLObject[paramCount];
@ -1098,7 +1123,9 @@ public class ISBPL {
return params[mid.get()];
}
private Object syncFromISBPL = new Object();
private Object fromISBPL(ISBPLObject o) {
synchronized(syncFromISBPL) {
ISBPLType type = o.type;
if (type.equals(getType("null"))) {
return null;
@ -1115,6 +1142,7 @@ public class ISBPL {
}
return o.object;
}
}
public Object primitiveDefault(Class<?> expectedType) {
if(expectedType == long.class)
@ -1137,6 +1165,7 @@ public class ISBPL {
}
public Object fromISBPL(ISBPLObject o, Class<?> expectedType) {
synchronized(syncFromISBPL) {
ISBPLType type = o.type;
if (type.equals(getType("null"))) {
return null;
@ -1182,8 +1211,11 @@ public class ISBPL {
typeError(o.type.name, expectedType.getName());
return o.object;
}
}
private Object syncToISBPL = new Object();
public ISBPLObject toISBPL(Object object) {
synchronized (syncToISBPL) {
if(object == null) {
return new ISBPLObject(getType("null"), 0);
}
@ -1214,6 +1246,7 @@ public class ISBPL {
o.object = object;
return o;
}
}
public void addFunction(ISBPLType type, String name, ISBPLCallable callable) {
type.methods.put(name, callable);
@ -1300,7 +1333,7 @@ public class ISBPL {
return new ISBPLObject(getType("string"), objects);
}
public ISBPLType registerType(String name) {
public synchronized ISBPLType registerType(String name) {
ISBPLType type = new ISBPLType(name, types.size());
types.add(type);
return type;