From 35fdb25d273dfc1e456b5663d930c72824c44cd1 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 29 Aug 2025 18:22:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=A9=E5=AE=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 SyscallCommand 类中的列表操作中增加了自动扩容逻辑 - 当访问的索引超过当前列表大小时,自动将列表大小扩展到该索引 - 这种实现允许在 append操作时直接使用 set 方法,而不需要额外判断 --- .../commands/system/control/SyscallCommand.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jcnc/snow/vm/commands/system/control/SyscallCommand.java b/src/main/java/org/jcnc/snow/vm/commands/system/control/SyscallCommand.java index 80428fa..2bdd5fe 100644 --- a/src/main/java/org/jcnc/snow/vm/commands/system/control/SyscallCommand.java +++ b/src/main/java/org/jcnc/snow/vm/commands/system/control/SyscallCommand.java @@ -400,9 +400,20 @@ public class SyscallCommand implements Command { // 必须是可变 List @SuppressWarnings("unchecked") java.util.List mlist = (java.util.List) list; - if (idx < 0 || idx >= mlist.size()) + if (idx < 0) { throw new IndexOutOfBoundsException("数组下标越界: " + idx + " (长度 " + mlist.size() + ")"); - mlist.set(idx, value); + } + // 允许自动扩容:当 idx == size 时等价于 append;当 idx > size 时以 null 填充到 idx-1,再 set(idx, value) + if (idx >= mlist.size()) { + // 以 null 填充直到 size == idx + while (mlist.size() < idx) { + mlist.add(null); + } + // 此时 size == idx,直接追加 + mlist.add(value); + } else { + mlist.set(idx, value); + } } else if (arrObj != null && arrObj.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(arrObj); if (idx < 0 || idx >= len)