feat: 添加对 GraalVM native-image 的支持

- 在 LocalVariableStore 中添加对 native-image环境的检测
- 在 VMUtils 中实现 isNativeImage 方法
- 更新 module-info.java,添加对 org.graalvm.nativeimage 的依赖
- 优化调试模式下的 UI 显示逻辑,避免在 native-image 环境中显示 Swing 窗口
This commit is contained in:
Luke 2025-07-23 10:42:38 +08:00
parent ebb9524db0
commit 80deaa9c4f
3 changed files with 32 additions and 5 deletions

View File

@ -4,6 +4,7 @@ module org.jcnc.snow.compiler {
uses CLICommand;
requires java.desktop;
requires java.logging;
requires org.graalvm.nativeimage;
exports org.jcnc.snow.compiler.ir.core;
exports org.jcnc.snow.compiler.ir.instruction;
}

View File

@ -6,6 +6,8 @@ import org.jcnc.snow.vm.utils.LoggingUtils;
import java.util.ArrayList;
import static org.jcnc.snow.vm.utils.VMUtils.isNativeImage;
/**
* The {@code LocalVariableStore} represents a simple dynamically-sized
* local-variable table (<em>frame locals</em>) of the VM.
@ -84,6 +86,9 @@ public class LocalVariableStore {
}
}
/* ---------- internal helpers ---------- */
/**
* Clears all variables (used when a stack frame is popped).
*/
@ -106,9 +111,6 @@ public class LocalVariableStore {
}
}
/* ---------- internal helpers ---------- */
/**
* Ensures backing list can hold {@code minCapacity} slots.
*/
@ -120,12 +122,18 @@ public class LocalVariableStore {
}
/**
* Mode-specific UI hook (unchanged).
* Mode-specific UI hook for debugging.
* <p>
* If debug mode is enabled and not running inside a GraalVM native-image,
* this method will open the Swing-based variable inspector window.
* In native-image environments (where AWT/Swing is unavailable),
* the window will not be displayed.
*/
private void handleMode() {
/* no-op */
if (SnowConfig.isDebug()) {
if (isNativeImage()) return;
LocalVariableStoreSwing.display(this, "Local Variable Table");
}
}
}

View File

@ -1,5 +1,6 @@
package org.jcnc.snow.vm.utils;
import org.graalvm.nativeimage.ImageInfo;
import org.jcnc.snow.vm.engine.VirtualMachineEngine;
/**
@ -39,4 +40,21 @@ public class VMUtils {
vm.printStack();
vm.printLocalVariables();
}
/**
* Detects if the current runtime environment is a GraalVM native-image.
* <p>
* Uses GraalVM's {@code org.graalvm.nativeimage.ImageInfo.inImageCode()} API to determine
* if the application is running as a native executable. If the class is not present
* (for example, in a standard JVM), returns {@code false}.
*
* @return {@code true} if running inside a GraalVM native-image, otherwise {@code false}
*/
public static boolean isNativeImage() {
try {
return ImageInfo.inImageCode();
} catch (Throwable t) {
return false;
}
}
}