From 2a5f6aa9aa7fecee8c547cb7030afe72c5d211eb Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:41:17 +0300 Subject: [PATCH] fix: use _STR and _INT variants of member instructions --- .../values/operations/IndexAssignNode.java | 48 +++++++++++++++---- .../values/operations/IndexNode.java | 16 ++++++- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexAssignNode.java b/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexAssignNode.java index e9e2e3d..8765690 100644 --- a/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexAssignNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexAssignNode.java @@ -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,21 +18,49 @@ public class IndexAssignNode extends Node { @Override public void compile(CompileResult target, boolean pollute) { if (operation != null) { object.compile(target, true); - index.compile(target, true); - target.add(Instruction.dup(2)); - target.add(Instruction.loadMember()); - value.compile(target, true); - target.add(Instruction.operation(operation)); + 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)); - target.add(Instruction.storeMember(pollute)).setLocationAndDebug(loc(), BreakpointType.STEP_IN); + target.add(Instruction.loadMember()); + value.compile(target, true); + target.add(Instruction.operation(operation)); + + target.add(Instruction.storeMember(pollute)); + } + target.setLocationAndDebug(loc(), BreakpointType.STEP_IN); } else { object.compile(target, true); - index.compile(target, true); - value.compile(target, true); - target.add(Instruction.storeMember(pollute)).setLocationAndDebug(loc(), BreakpointType.STEP_IN);; + 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.setLocationAndDebug(loc(), BreakpointType.STEP_IN);; } } diff --git a/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java b/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java index 85a6da4..d2791ac 100644 --- a/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java @@ -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()); - index.compile(target, true); - target.add(Instruction.loadMember()).setLocationAndDebug(loc(), BreakpointType.STEP_IN); + 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()); + } + + target.setLocationAndDebug(loc(), BreakpointType.STEP_IN); + if (!pollute) target.add(Instruction.discard()); } @Override public void compile(CompileResult target, boolean pollute) {