diff --git a/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java b/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java index 8d38d50..213f020 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/FunctionScope.java @@ -5,8 +5,7 @@ import java.util.HashMap; public final class FunctionScope { protected final VariableList locals = new VariableList(VariableIndex.IndexType.LOCALS); - protected final VariableList catches = new VariableList(VariableIndex.IndexType.LOCALS, this.locals); - protected final VariableList capturables = new VariableList(VariableIndex.IndexType.CAPTURABLES, this.catches); + protected final VariableList capturables = new VariableList(VariableIndex.IndexType.CAPTURABLES, this.locals); private final VariableList captures = new VariableList(VariableIndex.IndexType.CAPTURES); private final HashMap localsMap = new HashMap<>(); @@ -38,8 +37,10 @@ public final class FunctionScope { public Variable define(Variable var) { if (passthrough) return null; else { - var old = get(var.name, false); - if (old != null) return old; + var catchVar = getCatchVar(var.name); + if (catchVar != null) return catchVar; + if (localsMap.containsKey(var.name)) return localsMap.get(var.name); + if (capturesMap.containsKey(var.name)) throw new RuntimeException("HEY!"); localsMap.put(var.name, var); return locals.add(var); @@ -58,7 +59,7 @@ public final class FunctionScope { */ public Variable defineCatch(String name) { var var = new Variable(name, false); - this.catches.add(var); + this.locals.add(var); this.catchesMap.add(var); return var; } @@ -87,7 +88,7 @@ public final class FunctionScope { var parentVar = parent.get(name, true); if (parentVar == null) return null; - var childVar = captures.add(parentVar.clone()); + var childVar = captures.add(parentVar.clone().setIndexSupplier(null)); capturesMap.put(childVar.name, childVar); childToParent.put(childVar, parentVar); parentToChild.put(parentVar, childVar); @@ -103,7 +104,6 @@ public final class FunctionScope { * @param capture Whether or not to execute this capturing logic */ public Variable get(Variable var, boolean capture) { - if (catches.has(var)) return addCaptured(var, capture); if (captures.has(var)) return addCaptured(var, capture); if (locals.has(var)) return addCaptured(var, capture); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableIndex.java b/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableIndex.java index 278ab88..ea01ac9 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableIndex.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableIndex.java @@ -45,14 +45,6 @@ public final class VariableIndex { default: throw new UnsupportedOperationException("Unknown index type " + type); } } - public final Instruction toInit() { - switch (type) { - case CAPTURES: throw new UnsupportedOperationException("Unknown index type " + type); - case CAPTURABLES: return Instruction.storeVar(index, false, true); - case LOCALS: return Instruction.storeVar(index, false, true); - default: throw new UnsupportedOperationException("Unknown index type " + type); - } - } public VariableIndex(VariableIndex.IndexType type, int index) { this.type = type; diff --git a/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableList.java b/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableList.java index 9943083..414b4d2 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableList.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/scope/VariableList.java @@ -13,7 +13,7 @@ public final class VariableList implements Iterable { public VariableNode next; public VariableNode prev; public int index; - public int indexIteration; + public int indexIteration = -1; public VariableList list() { return VariableList.this; } @@ -120,6 +120,7 @@ public final class VariableList implements Iterable { node.prev = null; varMap.remove(node.var); + node.var.setIndexSupplier(null); return node.var; } @@ -178,7 +179,10 @@ public final class VariableList implements Iterable { */ public VariableList(IndexType type, VariableList prev) { this.indexType = type; - this.offset = prev::size; + this.offset = () -> { + if (prev.offset != null) return prev.offset.getAsInt() + prev.size(); + else return prev.size(); + }; } public VariableList(IndexType type) { this.indexType = type;