fix: use _STR and _INT variants of member instructions

This commit is contained in:
TopchetoEU 2024-09-04 10:41:17 +03:00
parent 78d233a6bd
commit 2a5f6aa9aa
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 53 additions and 11 deletions

View File

@ -6,6 +6,8 @@ import me.topchetoeu.jscript.common.Instruction.BreakpointType;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.compilation.values.constants.NumberNode;
import me.topchetoeu.jscript.compilation.values.constants.StringNode;
public class IndexAssignNode extends Node {
public final Node object;
@ -16,6 +18,20 @@ public class IndexAssignNode extends Node {
@Override public void compile(CompileResult target, boolean pollute) {
if (operation != null) {
object.compile(target, true);
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.loadMember((int)num.value));
value.compile(target, true);
target.add(Instruction.operation(operation));
target.add(Instruction.storeMember((int)num.value, pollute));
}
else if (index instanceof StringNode str) {
target.add(Instruction.loadMember(str.value));
value.compile(target, true);
target.add(Instruction.operation(operation));
target.add(Instruction.storeMember(str.value, pollute));
}
else {
index.compile(target, true);
target.add(Instruction.dup(2));
@ -23,14 +39,28 @@ public class IndexAssignNode extends Node {
value.compile(target, true);
target.add(Instruction.operation(operation));
target.add(Instruction.storeMember(pollute)).setLocationAndDebug(loc(), BreakpointType.STEP_IN);
target.add(Instruction.storeMember(pollute));
}
target.setLocationAndDebug(loc(), BreakpointType.STEP_IN);
}
else {
object.compile(target, true);
if (index instanceof NumberNode num && (int)num.value == num.value) {
value.compile(target, true);
target.add(Instruction.storeMember((int)num.value, pollute));
}
else if (index instanceof StringNode str) {
value.compile(target, true);
target.add(Instruction.storeMember(str.value, pollute));
}
else {
index.compile(target, true);
value.compile(target, true);
target.add(Instruction.storeMember(pollute));
}
target.add(Instruction.storeMember(pollute)).setLocationAndDebug(loc(), BreakpointType.STEP_IN);;
target.setLocationAndDebug(loc(), BreakpointType.STEP_IN);;
}
}

View File

@ -11,6 +11,7 @@ import me.topchetoeu.jscript.compilation.AssignableNode;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.JavaScript;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.compilation.values.constants.NumberNode;
import me.topchetoeu.jscript.compilation.values.constants.StringNode;
public class IndexNode extends Node implements AssignableNode {
@ -24,8 +25,19 @@ public class IndexNode extends Node implements AssignableNode {
object.compile(target, true);
if (dupObj) target.add(Instruction.dup());
if (index instanceof NumberNode num && (int)num.value == num.value) {
target.add(Instruction.loadMember((int)num.value));
}
else if (index instanceof StringNode str) {
target.add(Instruction.loadMember(str.value));
}
else {
index.compile(target, true);
target.add(Instruction.loadMember()).setLocationAndDebug(loc(), BreakpointType.STEP_IN);
target.add(Instruction.loadMember());
}
target.setLocationAndDebug(loc(), BreakpointType.STEP_IN);
if (!pollute) target.add(Instruction.discard());
}
@Override public void compile(CompileResult target, boolean pollute) {