fix bug in multi

This commit is contained in:
Daniella / Tove 2022-05-15 19:49:43 +02:00
parent 3e914cc4c5
commit e639991c17
2 changed files with 58 additions and 55 deletions

View file

@ -28,6 +28,9 @@ public class ISBPL {
throw new Error(e); throw new Error(e);
} }
} }
public static PrintStream gOutputStream = System.out;
public static PrintStream gErrorStream = System.err;
static boolean debug = false, printCalls = false; static boolean debug = false, printCalls = false;
public ISBPLDebugger.IPC debuggerIPC = new ISBPLDebugger.IPC(); public ISBPLDebugger.IPC debuggerIPC = new ISBPLDebugger.IPC();
@ -100,7 +103,7 @@ public class ISBPL {
array.checkTypeMulti(getType("array"), getType("string")); array.checkTypeMulti(getType("array"), getType("string"));
String[] allowed; String[] allowed;
if(debug) if(debug)
System.out.println("try with " + array); ISBPL.gOutputStream.println("try with " + array);
if(array.type.name.equals("string")) { if(array.type.name.equals("string")) {
allowed = new String[] { toJavaString(array) }; allowed = new String[] { toJavaString(array) };
} }
@ -246,7 +249,7 @@ public class ISBPL {
} }
stack.push(new ISBPLObject(getType("int"), type.id)); stack.push(new ISBPLObject(getType("int"), type.id));
if(debug) if(debug)
System.err.println("Constructing type " + type); ISBPL.gErrorStream.println("Constructing type " + type);
return i.get(); return i.get();
}; };
case "string!": case "string!":
@ -445,14 +448,14 @@ public class ISBPL {
func = (Stack<ISBPLObject> stack) -> { func = (Stack<ISBPLObject> stack) -> {
ISBPLObject c = stack.pop(); ISBPLObject c = stack.pop();
c.checkType(getType("char")); c.checkType(getType("char"));
System.out.print(((char) c.object)); ISBPL.gOutputStream.print(((char) c.object));
}; };
break; break;
case "eputchar": case "eputchar":
func = (Stack<ISBPLObject> stack) -> { func = (Stack<ISBPLObject> stack) -> {
ISBPLObject c = stack.pop(); ISBPLObject c = stack.pop();
c.checkType(getType("char")); c.checkType(getType("char"));
System.err.print(((char) c.object)); ISBPL.gErrorStream.print(((char) c.object));
}; };
break; break;
case "_file": case "_file":
@ -1088,7 +1091,7 @@ public class ISBPL {
for (Object o : clazz.getEnumConstants()) { for (Object o : clazz.getEnumConstants()) {
addFunction(type, o.toString(), stack -> { addFunction(type, o.toString(), stack -> {
if(debug) if(debug)
System.err.println("Java GetEnum: " + o); ISBPL.gErrorStream.println("Java GetEnum: " + o);
stack.push(toISBPL(o)); stack.push(toISBPL(o));
}); });
} }
@ -1097,7 +1100,7 @@ public class ISBPL {
addFunction(type, field.getName(), stack -> { addFunction(type, field.getName(), stack -> {
forceAccessible(field); forceAccessible(field);
if(debug) if(debug)
System.err.println("Java Get: " + field); ISBPL.gErrorStream.println("Java Get: " + field);
try { try {
stack.push(toISBPL(field.get(stack.pop().object))); stack.push(toISBPL(field.get(stack.pop().object)));
} }
@ -1107,7 +1110,7 @@ public class ISBPL {
addFunction(type, "=" + field.getName(), stack -> { addFunction(type, "=" + field.getName(), stack -> {
forceAccessible(field); forceAccessible(field);
if(debug) if(debug)
System.err.println("Java Set: " + field); ISBPL.gErrorStream.println("Java Set: " + field);
try { try {
field.set(stack.pop().object, fromISBPL(stack.pop(), field.getType())); field.set(stack.pop().object, fromISBPL(stack.pop(), field.getType()));
} }
@ -1138,7 +1141,7 @@ public class ISBPL {
forceAccessible(method); forceAccessible(method);
if(debug) if(debug)
System.err.println("Java Call: " + method + " - " + Arrays.toString(params)); ISBPL.gErrorStream.println("Java Call: " + method + " - " + Arrays.toString(params));
try { try {
Object r = method.invoke(o, params); Object r = method.invoke(o, params);
if(method.getReturnType() != void.class) if(method.getReturnType() != void.class)
@ -1174,7 +1177,7 @@ public class ISBPL {
forceAccessible(constructor); forceAccessible(constructor);
if(debug) if(debug)
System.err.println("Java Call: " + constructor + " - " + Arrays.toString(params)); ISBPL.gErrorStream.println("Java Call: " + constructor + " - " + Arrays.toString(params));
try { try {
Object r = constructor.newInstance(params); Object r = constructor.newInstance(params);
stack.push(toISBPL(r)); stack.push(toISBPL(r));
@ -1198,7 +1201,7 @@ public class ISBPL {
int[] methodIDs = new int[paramCount]; int[] methodIDs = new int[paramCount];
Arrays.fill(methodIDs, 0); Arrays.fill(methodIDs, 0);
if(debug) if(debug)
System.err.println("Consideration of " + paramTypes.length + " methods..."); ISBPL.gErrorStream.println("Consideration of " + paramTypes.length + " methods...");
for (int i = paramCount - 1 ; i >= 0 ; i--) { for (int i = paramCount - 1 ; i >= 0 ; i--) {
o[i] = stack.pop(); o[i] = stack.pop();
for (int j = 0 ; j < paramTypes.length ; j++) { for (int j = 0 ; j < paramTypes.length ; j++) {
@ -1207,15 +1210,15 @@ public class ISBPL {
params[j][i] = fromISBPL(o[i], m[i]); params[j][i] = fromISBPL(o[i], m[i]);
methodIDs[i] += 1 << j; methodIDs[i] += 1 << j;
if(debug) if(debug)
System.err.println("Consideration: Pass " + i + " " + Arrays.toString(paramTypes[j])); ISBPL.gErrorStream.println("Consideration: Pass " + i + " " + Arrays.toString(paramTypes[j]));
} }
catch (ISBPLError ignored) { catch (ISBPLError ignored) {
if(debug) if(debug)
System.err.println("Consideration: Fail " + i + " " + Arrays.toString(paramTypes[j])); ISBPL.gErrorStream.println("Consideration: Fail " + i + " " + Arrays.toString(paramTypes[j]));
} }
} }
if(debug) if(debug)
System.err.println("Considerartion: " + i + " " + Integer.toBinaryString(methodIDs[i] + (1 << 31)).substring(32 - paramTypes.length)); ISBPL.gErrorStream.println("Considerartion: " + i + " " + Integer.toBinaryString(methodIDs[i] + (1 << 31)).substring(32 - paramTypes.length));
} }
int msize = paramTypes.length; int msize = paramTypes.length;
for (int i = 0 ; i < msize; i++) { for (int i = 0 ; i < msize; i++) {
@ -1229,11 +1232,11 @@ public class ISBPL {
if(works) { if(works) {
mid.set(i); mid.set(i);
if(debug) if(debug)
System.err.println("Considering: " + Arrays.toString(paramTypes[i])); ISBPL.gErrorStream.println("Considering: " + Arrays.toString(paramTypes[i]));
} }
} }
if(debug) if(debug)
System.err.println("Considered " + paramTypes.length + " methods. Result: " + Arrays.toString(paramTypes[mid.get()])); ISBPL.gErrorStream.println("Considered " + paramTypes.length + " methods. Result: " + Arrays.toString(paramTypes[mid.get()]));
return params[mid.get()]; return params[mid.get()];
} }
@ -1472,7 +1475,7 @@ public class ISBPL {
for (int x = 0 ; x < frameStack.get().size() ; x++) { for (int x = 0 ; x < frameStack.get().size() ; x++) {
s.append("\t"); s.append("\t");
} }
System.err.println(s + word + "\t\t" + (debug ? stack : "")); ISBPL.gErrorStream.println(s + word + "\t\t" + (debug ? stack : ""));
} }
while (debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1) == 0) Thread.sleep(1); while (debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1) == 0) Thread.sleep(1);
int rid = debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1); int rid = debuggerIPC.run.getOrDefault(Thread.currentThread().getId(), -1);
@ -1502,11 +1505,11 @@ public class ISBPL {
// Doing this nonrecursively because it's much better despite the slight readability disadvantage. // Doing this nonrecursively because it's much better despite the slight readability disadvantage.
if(stack.peek() == null) { if(stack.peek() == null) {
// We need to dump things immediately. // We need to dump things immediately.
System.err.println("!!! ISBPL WORD PARSER ERROR !!!"); ISBPL.gErrorStream.println("!!! ISBPL WORD PARSER ERROR !!!");
System.err.println("This is most likely due to a garbage collector malfunction."); ISBPL.gErrorStream.println("This is most likely due to a garbage collector malfunction.");
System.err.println("Stack: " + stack); ISBPL.gErrorStream.println("Stack: " + stack);
System.err.println("LastWords: " + lastWords); ISBPL.gErrorStream.println("LastWords: " + lastWords);
System.err.println("FileStack: " + fileStack); ISBPL.gErrorStream.println("FileStack: " + fileStack);
} }
ISBPLType type = stack.peek().type; ISBPLType type = stack.peek().type;
Queue<ISBPLType> types = new LinkedList<>(); Queue<ISBPLType> types = new LinkedList<>();
@ -1556,11 +1559,11 @@ public class ISBPL {
e.printStackTrace(); e.printStackTrace();
} }
catch (Throwable t) { catch (Throwable t) {
if(debug) System.out.println("Passing exception " + t + " to caller."); if(debug) ISBPL.gOutputStream.println("Passing exception " + t + " to caller.");
if(stopExceptions) { if(stopExceptions) {
t.printStackTrace(); t.printStackTrace();
System.out.println("Current Words: "); ISBPL.gOutputStream.println("Current Words: ");
System.out.println(Arrays.toString(words)); ISBPL.gOutputStream.println(Arrays.toString(words));
dump(stack); dump(stack);
} }
throw t; throw t;
@ -1643,36 +1646,36 @@ public class ISBPL {
public void dump(Stack<ISBPLObject> stack) { public void dump(Stack<ISBPLObject> stack) {
try { try {
System.err.println("VAR DUMP\n----------------"); ISBPL.gErrorStream.println("VAR DUMP\n----------------");
for (ISBPLFrame map : frameStack.get()) { for (ISBPLFrame map : frameStack.get()) {
HashMap<String, ISBPLCallable> all = map.all(); HashMap<String, ISBPLCallable> all = map.all();
for (String key : all.keySet()) { for (String key : all.keySet()) {
if (key.startsWith("=")) { if (key.startsWith("=")) {
all.get(key.substring(1)).call(stack); all.get(key.substring(1)).call(stack);
System.err.println("\t" + key.substring(1) + ": \t" + stack.pop()); ISBPL.gErrorStream.println("\t" + key.substring(1) + ": \t" + stack.pop());
} }
} }
System.err.println("----------------"); ISBPL.gErrorStream.println("----------------");
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY."); ISBPL.gErrorStream.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY.");
} }
boolean fixed = false; boolean fixed = false;
while (!fixed) { while (!fixed) {
try { try {
System.err.println("STACK DUMP"); ISBPL.gErrorStream.println("STACK DUMP");
for (ISBPLObject object : stack) { for (ISBPLObject object : stack) {
System.err.println("\t" + object); ISBPL.gErrorStream.println("\t" + object);
} }
fixed = true; fixed = true;
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("!!! STACK CORRUPTED!"); ISBPL.gErrorStream.println("!!! STACK CORRUPTED!");
stack.pop(); stack.pop();
System.err.println("Popped. Trying again."); ISBPL.gErrorStream.println("Popped. Trying again.");
} }
} }
} }
@ -1700,23 +1703,23 @@ public class ISBPL {
isbpl.level0.add("dump", isbpl::dump); isbpl.level0.add("dump", isbpl::dump);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line; String line;
System.out.print("> "); ISBPL.gOutputStream.print("> ");
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
try { try {
isbpl.interpret(new File("_shell"), line, stack); isbpl.interpret(new File("_shell"), line, stack);
} catch(ISBPLError e) { } catch(ISBPLError e) {
System.out.println("Error: " + e.type + ": " + e.message); ISBPL.gOutputStream.println("Error: " + e.type + ": " + e.message);
} catch(Throwable e) { } catch(Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.print("\n> "); ISBPL.gOutputStream.print("\n> ");
} }
} }
} catch (ISBPLStop stop) { } catch (ISBPLStop stop) {
System.exit(isbpl.exitCode); System.exit(isbpl.exitCode);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.out.println(stack); ISBPL.gOutputStream.println(stack);
} }
} }
@ -1749,7 +1752,7 @@ public class ISBPL {
theSafe.putBoolean(thing, theSafe.objectFieldOffset(FakeAccessibleObject.class.getDeclaredField("override")), true); theSafe.putBoolean(thing, theSafe.objectFieldOffset(FakeAccessibleObject.class.getDeclaredField("override")), true);
} catch(Exception e) { //we are doomed } catch(Exception e) { //we are doomed
e.printStackTrace(); e.printStackTrace();
System.err.println("Failed to set accessible property. We are doomed."); ISBPL.gErrorStream.println("Failed to set accessible property. We are doomed.");
System.exit(1); System.exit(1);
} }
} }
@ -1994,7 +1997,7 @@ class ISBPLDebugger extends Thread {
//noinspection resource //noinspection resource
socket = new ServerSocket(Integer.parseInt(System.getenv().getOrDefault("DEBUG", "9736"))); socket = new ServerSocket(Integer.parseInt(System.getenv().getOrDefault("DEBUG", "9736")));
port = socket.getLocalPort(); port = socket.getLocalPort();
System.err.println("Debugger listening on :" + socket.getLocalPort()); ISBPL.gErrorStream.println("Debugger listening on :" + socket.getLocalPort());
} }
catch (BindException e) { catch (BindException e) {
while (socket == null) { while (socket == null) {
@ -2002,7 +2005,7 @@ class ISBPLDebugger extends Thread {
//noinspection resource //noinspection resource
socket = new ServerSocket((int) (Math.random() * 5000 + 5000)); socket = new ServerSocket((int) (Math.random() * 5000 + 5000));
port = socket.getLocalPort(); port = socket.getLocalPort();
System.err.println("Debugger listening on :" + socket.getLocalPort()); ISBPL.gErrorStream.println("Debugger listening on :" + socket.getLocalPort());
} }
catch (BindException ignored) { } catch (BindException ignored) { }
} }
@ -2059,51 +2062,51 @@ class ISBPLDebugger extends Thread {
boolean fixed = false; boolean fixed = false;
while (!fixed) { while (!fixed) {
try { try {
System.err.println("Stack recovered: " + isbpl.debuggerIPC.stack.get(tid)); ISBPL.gErrorStream.println("Stack recovered: " + isbpl.debuggerIPC.stack.get(tid));
fixed = true; fixed = true;
} }
catch (Exception e1) { catch (Exception e1) {
e.printStackTrace(); e.printStackTrace();
System.err.println("!!! STACK CORRUPTED!"); ISBPL.gErrorStream.println("!!! STACK CORRUPTED!");
isbpl.debuggerIPC.stack.get(tid).pop(); isbpl.debuggerIPC.stack.get(tid).pop();
System.err.println("Popped. Trying again."); ISBPL.gErrorStream.println("Popped. Trying again.");
} }
} }
} }
break; break;
case "dump": case "dump":
try { try {
System.err.println("VAR DUMP\n----------------"); ISBPL.gErrorStream.println("VAR DUMP\n----------------");
for (ISBPLFrame map : isbpl.frameStack.map.get(tid)) { for (ISBPLFrame map : isbpl.frameStack.map.get(tid)) {
HashMap<String, ISBPLCallable> all = map.all(); HashMap<String, ISBPLCallable> all = map.all();
for (String key : all.keySet()) { for (String key : all.keySet()) {
if (key.startsWith("=")) { if (key.startsWith("=")) {
all.get(key.substring(1)).call(isbpl.debuggerIPC.stack.get(tid)); all.get(key.substring(1)).call(isbpl.debuggerIPC.stack.get(tid));
System.err.println("\t" + key.substring(1) + ": \t" + isbpl.debuggerIPC.stack.get(tid).pop()); ISBPL.gErrorStream.println("\t" + key.substring(1) + ": \t" + isbpl.debuggerIPC.stack.get(tid).pop());
} }
} }
System.err.println("----------------"); ISBPL.gErrorStream.println("----------------");
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY."); ISBPL.gErrorStream.println("!!! VARS CORRUPTED! CANNOT FIX AUTOMATICALLY.");
} }
case "stack": case "stack":
boolean fixed = false; boolean fixed = false;
while (!fixed) { while (!fixed) {
try { try {
System.err.println("STACK DUMP"); ISBPL.gErrorStream.println("STACK DUMP");
for (ISBPLObject object : isbpl.debuggerIPC.stack.get(tid)) { for (ISBPLObject object : isbpl.debuggerIPC.stack.get(tid)) {
System.err.println("\t" + object); ISBPL.gErrorStream.println("\t" + object);
} }
fixed = true; fixed = true;
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.err.println("!!! STACK CORRUPTED!"); ISBPL.gErrorStream.println("!!! STACK CORRUPTED!");
isbpl.debuggerIPC.stack.get(tid).pop(); isbpl.debuggerIPC.stack.get(tid).pop();
System.err.println("Popped. Trying again."); ISBPL.gErrorStream.println("Popped. Trying again.");
} }
} }
break; break;
@ -2123,10 +2126,10 @@ class ISBPLDebugger extends Thread {
System.exit(255); System.exit(255);
break; break;
case "threads": case "threads":
System.err.println("THREAD DUMP"); ISBPL.gErrorStream.println("THREAD DUMP");
for (Thread thread : Thread.getAllStackTraces().keySet()) { for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (isbpl.debuggerIPC.stack.containsKey(thread.getId())) if (isbpl.debuggerIPC.stack.containsKey(thread.getId()))
System.err.println(thread.getId() + "\t" + thread.getName()); ISBPL.gErrorStream.println(thread.getId() + "\t" + thread.getName());
} }
break; break;
case "setthread": case "setthread":
@ -2134,10 +2137,10 @@ class ISBPLDebugger extends Thread {
long l = Long.parseLong(line.split(" ")[1]); long l = Long.parseLong(line.split(" ")[1]);
if (isbpl.debuggerIPC.stack.containsKey(l)) { if (isbpl.debuggerIPC.stack.containsKey(l)) {
tid = l; tid = l;
System.err.println("Set TID=" + l); ISBPL.gErrorStream.println("Set TID=" + l);
} }
else else
System.err.println("Thread not valid"); ISBPL.gErrorStream.println("Thread not valid");
break; break;
default: default:
break; break;

View file

@ -35,4 +35,4 @@ def Context construct Context {
"multi.isbpl:_eval" File new1 run stack jContext interpret3 "multi.isbpl:_eval" File new1 run stack jContext interpret3
stack stack
} }
} } =Context