From f6af7ff3c64b9b82846b75406f987139385b9912 Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Sun, 8 Dec 2024 19:52:38 +0200 Subject: [PATCH] Add readme * Add readme * test bages * add license * blue --- LICENSE | 21 ++++++++++ README.md | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..220e024d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Igor Rudenko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..0f3bd67f --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ +# rusty-jvm +![License](https://img.shields.io/github/license/hextriclosan/rusty-jvm) +![Build Status](https://github.com/hextriclosan/rusty-jvm/actions/workflows/ci.yml/badge.svg) +![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macos%20%7C%20windows-blue) + +## Introduction +This project is an attempt to implement JVM in Rust. +The project is for educational purposes only. +It is not intended to be a full-featured JVM implementation, but rather a simple one that can run small Java programs. +The main goal of this project is to learn how JVM works and how to implement it in Rust. +There is no dependency on any existing JVM implementation, everything that regards java is implemented from scratch. +There is no garbage collection (yet, I hope). + +The standard library classes are used in the project originated from _OpenJDK JDK 23 General-Availability Release_ are located in the [lib](lib) directory. + +Refer [integration tests](tests/test_data) for examples of supported Java features. + +## Getting Started + +#### Prerequisites +Ensure you have the following: +- A machine running Windows, macOS, or Linux. +- Rust installed and configured. +- The RUSTY_JAVA_HOME environment variable set to the root of this project. + +#### Example Program +This program calculates the total attack power of a group of units in a game. +It features abstract classes, interfaces, and polymorphism to demonstrate the capabilities of rusty-jvm. +```java +package game; + +import java.util.ArrayList; +import java.util.List; + +public class Demo { + public static void main(String[] args) { + ControlGroup controlGroup = new ControlGroup(); + controlGroup.addUnits(new Zealot(), new DarkTemplar(), new Unit() { + @Override + public int damage() { + return 4; + } + }); + + int groupAttackPower = controlGroup.damage(); + System.out.print("Group attack power is "); + System.out.println(groupAttackPower); + } +} + +interface Unit { + int damage(); +} + +abstract class AbstractUnit implements Unit { + public AbstractUnit() { + System.out.println(say()); + } + protected abstract String say(); +} + +class Zealot extends AbstractUnit { + @Override + public int damage() { + return 8; + } + @Override + public String say() { + return "We embrace the glory of battle!"; + } +} + +class DarkTemplar extends AbstractUnit { + @Override + public int damage() { + return 45; + } + @Override + public String say() { + return "Battle is upon us!"; + } +} + +class ControlGroup implements Unit { + private final List units = new ArrayList<>(); + public void addUnits(Unit... units) { + for (Unit unit : units) { + this.units.add(unit); + } + } + @Override + public int damage() { + int totalAttackPower = 0; + for (Unit unit : units) { + totalAttackPower += unit.damage(); + } + return totalAttackPower; + } +} +``` +#### Steps to Run +1. Compile the program using the Java compiler +```shell +javac -d . Demo.java +``` +2. Run it using rusty-jvm: +```shell +cargo run -- game.Demo +``` +#### Expected Output +If everything is set up correctly, you should see +``` +We embrace the glory of battle! +Battle is upon us! +Group attack power is 57 +```