Skip to content

Commit

Permalink
refactor: promisify, bump Jasm
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Aug 20, 2024
1 parent c4ee0a7 commit 385febc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
alias(libs.plugins.teavm) // order matters?
}

val thisVersion = "0.1.0"
val thisVersion = "0.2.0"

group = "run.slicer"
version = "$thisVersion-${libs.versions.jasm.get()}"
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (evt) => {
reader.onload = async (evt) => {
const start = Date.now();
console.log(`Disassembling ${file.name}...`);

document.getElementById("file-content").textContent = disassemble(new Uint8Array(evt.target.result));
document.getElementById("file-content").textContent = await disassemble(new Uint8Array(evt.target.result));

console.log(`Disassembled ${file.name} in ${Date.now() - start}ms.`);
};
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
jasm = "2.5.0"
jasm = "2.6.0"
teavm = "0.10.0"

[libraries]
Expand Down
2 changes: 1 addition & 1 deletion jasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ declare module "@run-slicer/jasm" {
indent?: string;
}

export function disassemble(b: Uint8Array, config?: DisassemblyConfig): string;
export function disassemble(b: Uint8Array, config?: DisassemblyConfig): Promise<string>;
}
28 changes: 19 additions & 9 deletions src/main/java/run/slicer/jasm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSExport;
import org.teavm.jso.core.JSObjects;
import org.teavm.jso.core.JSPromise;
import org.teavm.jso.core.JSString;

import java.io.IOException;
import java.io.UncheckedIOException;

public class Main {
@JSExport
public static String disassemble(@JSByRef byte[] b, DisassemblyOptions options) {
public static JSPromise<JSString> disassemble(@JSByRef byte[] b, DisassemblyOptions options) {
return disassemble0(b, options == null || JSObjects.isUndefined(options) ? JSObjects.create() : options);
}

private static String disassemble0(byte[] b, DisassemblyOptions options) {
final var ctx = new PrintContext<>(options.indent());
private static JSPromise<JSString> disassemble0(byte[] b, DisassemblyOptions options) {
return new JSPromise<>((resolve, reject) -> {
new Thread(() -> {
try {
final var ctx = new PrintContext<>(options.indent());

try {
new JvmClassPrinter(b).print(ctx);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try {
new JvmClassPrinter(b).print(ctx);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

return ctx.toString();
resolve.accept(JSString.valueOf(ctx.toString()));
} catch (Throwable e) {
reject.accept(e);
}
}).start();
});
}
}

0 comments on commit 385febc

Please sign in to comment.