fix: some scope bug fixes

This commit is contained in:
TopchetoEU 2024-09-04 10:38:16 +03:00
parent e3f1bc0949
commit 3f25868f19
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 13 additions and 17 deletions

View File

@ -27,19 +27,17 @@ public class FunctionScope extends Scope {
else if (parent == null) throw new RuntimeException("Strict variables may be defined only in local scopes"); else if (parent == null) throw new RuntimeException("Strict variables may be defined only in local scopes");
else return parent.defineStrict(name, readonly, loc); else return parent.defineStrict(name, readonly, loc);
} }
public VariableDescriptor defineParam(String name, Location loc) { public VariableDescriptor defineParam(String name, boolean readonly, Location loc) {
return specials.add(name, false); return specials.add(name, readonly);
} }
public boolean hasArg(String name) { public boolean hasArg(String name) {
return specials.has(name); return specials.has(name);
} }
public VariableDescriptor get(String name, boolean capture, boolean skipSelf) { @Override public VariableDescriptor get(String name, boolean capture) {
if (!skipSelf) { if (specials.has(name)) return specials.get(name);
if (specials.has(name)) return specials.get(name); if (locals.has(name)) return locals.get(name);
if (locals.has(name)) return locals.get(name); if (captures.has(name)) return captures.get(name);
if (captures.has(name)) return captures.get(name);
}
var parentVar = parent.get(name, true); var parentVar = parent.get(name, true);
if (parentVar == null) return null; if (parentVar == null) return null;
@ -51,10 +49,6 @@ public class FunctionScope extends Scope {
return childVar; return childVar;
} }
@Override public VariableDescriptor get(String name, boolean capture) {
return get(name, capture, false);
}
@Override public boolean has(String name) { @Override public boolean has(String name) {
if (specials.has(name)) return true; if (specials.has(name)) return true;
if (locals.has(name)) return true; if (locals.has(name)) return true;
@ -83,7 +77,7 @@ public class FunctionScope extends Scope {
} }
public int offset() { public int offset() {
return captures.size() + locals.size(); return specials.size() + locals.size();
} }
public int[] getCaptureIndices() { public int[] getCaptureIndices() {
@ -93,6 +87,7 @@ public class FunctionScope extends Scope {
for (var el : captures) { for (var el : captures) {
assert childToParent.containsKey(el); assert childToParent.containsKey(el);
res[i] = childToParent.get(el).index(); res[i] = childToParent.get(el).index();
i++;
} }
return res; return res;

View File

@ -3,7 +3,10 @@ package me.topchetoeu.jscript.compilation.scope;
import me.topchetoeu.jscript.common.parsing.Location; import me.topchetoeu.jscript.common.parsing.Location;
public final class LocalScope extends Scope { public final class LocalScope extends Scope {
private final VariableList locals = new VariableList(); private final VariableList locals = new VariableList(() -> {
if (parent != null) return parent.offset();
else return 0;
});
@Override public int offset() { @Override public int offset() {
if (parent != null) return parent.offset() + locals.size(); if (parent != null) return parent.offset() + locals.size();

View File

@ -38,13 +38,11 @@ public final class VariableList implements Iterable<VariableDescriptor> {
public ListVar freeze() { public ListVar freeze() {
if (frozen) return this; if (frozen) return this;
this.frozen = true; this.frozen = true;
this.next = null;
if (prev == null) return this; if (prev == null) return this;
assert prev.frozen;
this.index = prev.index + 1; this.index = prev.index + 1;
this.next = null; this.next = null;
this.prev = null;
return this; return this;
} }