Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Jun 18, 2024
1 parent 1fbc4eb commit 046819c
Show file tree
Hide file tree
Showing 11 changed files with 3,702 additions and 3,737 deletions.
7,327 changes: 3,659 additions & 3,668 deletions dist/cfr.js

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions src/main/java/dev/cephx/cfr/Main.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package dev.cephx.cfr;

import dev.cephx.cfr.impl.ClassFileSourceImpl;
import dev.cephx.cfr.impl.OutputSinkFactoryImpl;
import org.benf.cfr.reader.api.CfrDriver;
import org.teavm.jso.JSExport;
import org.teavm.jso.core.JSObjects;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
@JSExport
public static String decompile(String name, DecompilerOptions options) throws Throwable {
public static String decompile(String name, Options options) throws Throwable {
return decompile0(name, options == null || JSObjects.isUndefined(options) ? JSObjects.create() : options);
}

private static String decompile0(String name, DecompilerOptions options) throws Throwable {
private static String decompile0(String name, Options options) throws Throwable {
final var sinkFactory = new OutputSinkFactoryImpl();
new CfrDriver.Builder()
.withClassFileSource(new ClassSourceImpl(options::source))
.withClassFileSource(new ClassFileSourceImpl(options.source()::get))
.withOutputSink(sinkFactory)
.withOptions(
Arrays.stream(options.getOptions())
.collect(Collectors.toMap(DecompilerOptions.Option::getName, DecompilerOptions.Option::getValue))
)
.withOptions(options.rawOptions())
.build()
.analyse(List.of(name));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;

public interface DecompilerOptions extends JSObject {
import java.util.HashMap;
import java.util.Map;

public interface Options extends JSObject {
@JSBody(script = "return this.options ? Object.entries(this.options) : [];")
Option[] getOptions();
Option[] options();

// can't use the logical OR, it breaks the TeaVM minifier
@JSBody(script = "return this.source ? this.source : (() => { return null; });")
Source getSource();
default Map<String, String> rawOptions() {
final Map<String, String> options = new HashMap<>();
for (final Options.Option option : this.options()) {
options.put(option.name(), option.value());
}

default byte[] source(String name) {
return this.getSource().get(name);
return options;
}

// can't use the logical OR, it breaks the TeaVM minifier
@JSBody(script = "return this.source ? this.source : (() => { return null; });")
Source source();

interface Option extends JSObject {
@JSBody(script = "return this[0];")
String getName();
String name();

@JSBody(script = "return this[1];")
String getValue();
String value();
}

@JSFunctor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cephx.cfr;
package dev.cephx.cfr.impl;

import org.benf.cfr.reader.api.ClassFileSource;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
Expand All @@ -7,7 +7,7 @@
import java.util.List;
import java.util.function.Function;

record ClassSourceImpl(Function<String, byte[]> source) implements ClassFileSource {
public record ClassFileSourceImpl(Function<String, byte[]> source) implements ClassFileSource {
@Override
public void informAnalysisRelativePathDetail(String usePath, String classFilePath) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.cephx.cfr;
package dev.cephx.cfr.impl;

import org.benf.cfr.reader.api.OutputSinkFactory;

import java.util.Collection;
import java.util.List;

final class OutputSinkFactoryImpl implements OutputSinkFactory {
public final class OutputSinkFactoryImpl implements OutputSinkFactory {
private Throwable exception;
private String output;

Expand Down
1 change: 0 additions & 1 deletion src/teavm/java/dev/cephx/cfr/teavm/CFRPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ public class CFRPlugin implements TeaVMPlugin {
@Override
public void install(TeaVMHost host) {
host.add(new MethodStubTransformer());
host.add(new InsnStubTransformer());
}
}
43 changes: 0 additions & 43 deletions src/teavm/java/dev/cephx/cfr/teavm/InsnStubTransformer.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public void transformClass(ClassHolder cls, ClassHolderTransformerContext contex
this.stubWithNullConstant(cls.getMethod(new MethodDescriptor("getContentByFromReflectedClass", String.class, byte[].class)));
this.stubWithBooleanConstant(cls.getMethod(new MethodDescriptor("CheckJrt", boolean.class)), false);
}
case "org.benf.cfr.reader.util.output.LoggerFactory" -> this.stubWithNullConstant(cls.getMethod(new MethodDescriptor("getHandler", Handler.class)));
case "org.benf.cfr.reader.util.output.LoggerFactory" -> {
this.stubWithNullConstant(cls.getMethod(new MethodDescriptor("getHandler", Handler.class)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.cephx.cfr.teavm.classlib.java.util.logging;

public abstract class THandler {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.teavm.classlib.java.util.logging.TLogRecord;
import org.teavm.jso.JSBody;

// changes: implemented #isLoggable and #setLevel
// changes: implemented #isLoggable, #setLevel, #addHandler and #setUseParentHandlers
public class TLogger {
public static final String GLOBAL_LOGGER_NAME = "global";
private static TMap<String, TLogger> loggerCache = new THashMap<>();
Expand Down Expand Up @@ -291,6 +291,12 @@ public void setLevel(TLevel level) {
this.level = level;
}

public void addHandler(THandler handler) {
}

public void setUseParentHandlers(boolean useParentHandlers) {
}

@JSBody(params = "message", script = ""
+ "if (console) {"
+ "console.info(message);"
Expand Down
3 changes: 2 additions & 1 deletion src/teavm/resources/META-INF/teavm.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mapClass|dev.cephx.cfr.teavm.classlib.java.util.logging.TLogger=java.util.logging.Logger
mapClass|dev.cephx.cfr.teavm.classlib.java.util.logging.TLogger=java.util.logging.Logger
mapClass|dev.cephx.cfr.teavm.classlib.java.util.logging.THandler=java.util.logging.Handler

0 comments on commit 046819c

Please sign in to comment.