fix: properly hande variable collisions

This commit is contained in:
TopchetoEU 2024-09-12 20:25:11 +03:00
parent 2a01b3d86e
commit 3e6816cb2c
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
4 changed files with 15 additions and 8 deletions

View File

@ -13,7 +13,7 @@ import me.topchetoeu.jscript.common.parsing.Source;
public class CompoundNode extends Node {
public final Node[] statements;
public final boolean hasScope;
public boolean hasScope;
public Location end;
@Override public void resolve(CompileResult target) {

View File

@ -107,6 +107,7 @@ public abstract class FunctionNode extends Node {
this.end = end;
this.params = params;
this.body = body;
this.body.hasScope = false;
}
public static void compileWithName(Node stm, CompileResult target, boolean pollute, String name) {

View File

@ -19,6 +19,13 @@ public class FunctionScope extends Scope {
public final boolean passtrough;
@Override public boolean hasNonStrict(String name) {
if (functionVarMap.containsKey(name)) return true;
if (blacklistNames.contains(name)) return true;
return false;
}
@Override public Variable define(Variable var, Location loc) {
checkNotEnded();
if (strictVarMap.containsKey(var.name)) throw alreadyDefinedErr(loc, var.name);
@ -32,13 +39,6 @@ public class FunctionScope extends Scope {
return variables.add(var);
}
}
@Override public Variable defineStrict(Variable var, Location loc) {
checkNotEnded();
if (functionVarMap.containsKey(var.name)) throw alreadyDefinedErr(loc, var.name);
if (blacklistNames.contains(var.name)) throw alreadyDefinedErr(loc, var.name);
return super.defineStrict(var, loc);
}
public Variable defineSpecial(Variable var, Location loc) {
checkNotEnded();
if (strictVarMap.containsKey(var.name)) throw alreadyDefinedErr(loc, var.name);

View File

@ -86,6 +86,11 @@ public class Scope {
return null;
}
/**
* Checks if this scope's function parent has a non-strict variable of the given name
*/
public boolean hasNonStrict(String name) { return false; }
/**
* Defines an ES2015-style variable
* @param readonly True if const, false if let
@ -96,6 +101,7 @@ public class Scope {
public Variable defineStrict(Variable var, Location loc) {
checkNotEnded();
if (strictVarMap.containsKey(var.name)) throw alreadyDefinedErr(loc, var.name);
if (hasNonStrict(var.name)) throw alreadyDefinedErr(loc, var.name);
strictVarMap.put(var.name, var);
return variables.add(var);