Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java 8 classes. Again. #28

Merged
merged 6 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jar-tool/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
108 changes: 108 additions & 0 deletions jar-tool/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package;

import haxe.io.Path;
import sys.FileSystem;
import java.StdTypes.Int8;
import java.NativeArray;
import java.io.FileOutputStream;
import java.io.File;
import java.nio.file.Files;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes.Opcodes_Statics;
import java.Lib.println;

using StringTools;

/**
* HOW TO USE:
*
* 1) Unzip JAR file into some folder. For example "jar_content";
* 2) Run `java -jar Main.jar jar_content`;
* 3) Pick up generated `hxjava-std.jar`.
*/
final class Main {
static function main():Void {
function process(dir:String) {
if (FileSystem.exists(dir)) {
for (file in FileSystem.readDirectory(dir)) {
final path = Path.join([dir, file]);
if (!FileSystem.isDirectory(path)) {
if (path.endsWith(".class")) {
println('Process $path... ');
final obj:NativeArray<Int8> = Files.readAllBytes(new File(path).toPath());
final reader = new ClassReader(obj);
final writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the COMPUTE_FRAMES bit. I suppose that means it already re-calculates stack frames, so we don't have to do anything by hand.

final visitor = new CustomClassVisitor(writer);

reader.accept(visitor, 0);

final stream = new FileOutputStream(path);
stream.write(writer.toByteArray());
}
} else {
process(Path.addTrailingSlash(path));
}
}
} else {
println('Wrong path. $dir is not exists');
}
}

final args:Array<String> = Sys.args();
if (args.length > 0) {
final dir = args[0];
process(dir);
println("\nPacking jar...\n");
if (Sys.command('jar', ["cvf", "hxjava-std.jar", "-C", dir, "."]) == 0) {
println("\nDone!\n");
} else {
println("Oops, something went wrong.");
}
}
}
}

@:nativeGen
class CustomClassVisitor extends ClassVisitor {

public function new(visitor:ClassVisitor) {
super(Opcodes_Statics.ASM7, visitor);
}

@:overload
override function visit(version:Int, access:Int, name:String, signature:String, superName:String, interfaces:NativeArray<String>):Void {
cv.visit(version, access, name, signature, superName, interfaces);
}

/**
* Starts the visit of the method's code, if any (i.e. non abstract method).
*/
@:overload
override function visitMethod(access:Int, name:String, desc:String, signature:String, exceptions:NativeArray<String>):MethodVisitor {
final mv:MethodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
if (mv != null) {
return new CustomMethodVisitor(mv);
}
return mv;
}
}

@:nativeGen
class CustomMethodVisitor extends MethodVisitor {
final target:MethodVisitor;

public function new(target:MethodVisitor) {
super(Opcodes_Statics.ASM7, null);
this.target = target;
}

@:overload
override function visitCode() {
target.visitCode();
target.visitMaxs(0, 0);
target.visitEnd();
}
}
Binary file added jar-tool/asm-7.1.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions jar-tool/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-m Main
-D jvm
--java ./out

--java-lib asm-7.1.jar
Binary file modified lib/hxjava-std.jar
Binary file not shown.