Score
0
Watch 17 Star 113 Fork 19

孤星可 / mini-jvmJavaLGPL-3.0

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
guxingke authored .
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Mini-jvm Gitter

CircleCIGitHubGitHub commit activityGitHub last commit

使用 Java 8 实现 JVM

特性

元循环(Metacircular)

mini-jvm on mini-jvm on hotspot. 可以在 mini-jvm 里运行 mini-jvm .

$ java -jar jvm-core/target/mini-jvm.jar -jar jvm-core/target/mini-jvm.jar -jar test.jar
# Hello World!

动机

  1. 尝试了解 JVM 原理, Learning by doing
  2. 纸上得来终觉浅, 实践
  3. 用简单的代码帮助 Javaer 理解 JVM

快速体验 [Macos 用户]

Hello world

brew tap guxingke/repo && brew install mini-jvm

cat <<EOF > HelloWorld.java
public class HelloWorld {
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("hello");
      return;
    }

    for(int i = 0; i < args.length; i ++) {
      System.out.println(args[i]);
    }
  }
}
EOF

javac HelloWorld.java

# no args
mini-jvm HelloWold
# => hello

# with program args
mini-jvm HelloWold hello mini-jvm
# =>  hello
# =>  mini-jvm

# 输入 mini-jvm -help 了解更多.

彩蛋 1 , 理解 JVM 基于栈的解释器.

cat <<EOF > Sum10.java
public class Sum10 {
  public static void main(String[] args) {
    int sum = sum10();
    System.out.println(sum);
  }
  
  public static int sum10() {
    int sum = 0;
    for (int i = 1; i <= 10; i++) {
      sum += i;
    }
    return sum;
  }
}
EOF

javac Sum10.java
java Sum10
# => 55

# 生成类汇编语言
# mini-jvm --bc [classfile] [method]
mini-jvm --bc Sum10.class sum10 > sum10.bc

# 解释上一步生成的 sum10.bc
# mini-jvm -- [bytecode file]
mini-jvm -- sum10.bc
# => 55

# ================================

快速体验 [其他操作系统]

需要自行下载打包. Dev

规划

  • Class 文件解析 90%
  • 字节码执行 90%
  • 类加载 90%
  • 方法调用 90%
  • 实例化 90%
  • Native 方法 90%
  • 异常处理 60%
  • self-booting 70%

局限

  1. 不实现 GC
  2. 不实现多线程

变更记录

  • 实现了元循环(Metacircular)
  • 反射特性基本可用
  • 增加简单的调试器 bin/jdb.
  • 支持 Lambda 调用, closure, currying 可用.
  • Hello World 级别可用

个人记录

参考

Comments ( 6 )

Sign in for post a comment

Java
1
https://gitee.com/guxingke/mini-jvm.git
git@gitee.com:guxingke/mini-jvm.git
guxingke
mini-jvm
mini-jvm
master

Help Search