Skip to content

Commit 2692263

Browse files
committed
feat(api): add new methods for asm/disasm single mode
1 parent f973b17 commit 2692263

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

raung-asm/src/main/java/io/github/skylot/raung/asm/api/IRaungAsm.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,25 @@ public interface IRaungAsm {
2424
IRaungAsm output(Path out);
2525

2626
/**
27-
* Preform assemble according to provided options.
27+
* Execute according to provided options.
2828
*/
2929
void execute();
3030

3131
/**
32-
* Single mode: process specified input file and return result as byte array.
32+
* Single mode: process specified input file and return result as a byte array.
3333
* Any set inputs or output will be ignored.
3434
*/
3535
byte[] executeForSingleClass(Path input);
3636

3737
/**
38-
* Single mode: process specified input stream and return result as byte array.
38+
* Single mode: process specified input stream and return result as a byte array.
3939
* Any set inputs or output will be ignored.
4040
*/
4141
byte[] executeForInputStream(InputStream input);
42+
43+
/**
44+
* Single mode: process specified input raung code and return result as a byte array.
45+
* Any set inputs or output will be ignored.
46+
*/
47+
byte[] executeForString(String raungCodeStr);
4248
}

raung-asm/src/main/java/io/github/skylot/raung/asm/impl/RaungAsmBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.skylot.raung.asm.impl;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.InputStream;
5+
import java.nio.charset.StandardCharsets;
46
import java.nio.file.Path;
57
import java.util.ArrayList;
68
import java.util.List;
@@ -62,6 +64,17 @@ public byte[] executeForInputStream(InputStream input) {
6264
}
6365
}
6466

67+
@Override
68+
public byte[] executeForString(String code) {
69+
try (InputStream input = new ByteArrayInputStream(code.getBytes(StandardCharsets.UTF_8))) {
70+
return RaungAsmExecutor.processInputStream(this, input, "<input>");
71+
} catch (RaungAsmException e) {
72+
throw e;
73+
} catch (Exception e) {
74+
throw new RaungAsmException("Failed to process input code", e);
75+
}
76+
}
77+
6578
@Override
6679
public String toString() {
6780
return "RaungAsmArgs{input=" + inputs + ", output=" + output + '}';

raung-disasm/src/main/java/io/github/skylot/raung/disasm/api/IRaungDisasm.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,10 @@ public interface IRaungDisasm {
7171
* Any set inputs or output will be ignored.
7272
*/
7373
String executeForInputStream(InputStream input);
74+
75+
/**
76+
* Single mode: process specified input class file bytes and return result as string.
77+
* Any set inputs or output will be ignored.
78+
*/
79+
String executeForBytes(byte[] bytes);
7480
}

raung-disasm/src/main/java/io/github/skylot/raung/disasm/impl/RaungDisasmBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ public String executeForInputStream(InputStream input) {
125125
}
126126
}
127127

128+
@Override
129+
public String executeForBytes(byte[] bytes) {
130+
try {
131+
return RaungDisasmExecutor.processBytes(this, bytes);
132+
} catch (Exception e) {
133+
throw new RaungDisasmException("Failed to process input bytes", e);
134+
}
135+
}
136+
128137
@Override
129138
public String toString() {
130139
return "RaungDisasmBuilder{"

raung-disasm/src/main/java/io/github/skylot/raung/disasm/impl/RaungDisasmExecutor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public static String processInputStream(RaungDisasmBuilder args, InputStream in)
3939
return runForInputStream(args, in).getResult();
4040
}
4141

42+
public static String processBytes(RaungDisasmBuilder args, byte[] bytes) {
43+
ValidateDisasmArgs.processOptions(args);
44+
return execute(args, new ClassReader(bytes)).getResult();
45+
}
46+
4247
private static RaungClassVisitor runForSingleClass(RaungDisasmBuilder args, Path clsFile) {
4348
try (InputStream in = new BufferedInputStream(Files.newInputStream(clsFile, StandardOpenOption.READ))) {
4449
return runForInputStream(args, in);
@@ -48,7 +53,10 @@ private static RaungClassVisitor runForSingleClass(RaungDisasmBuilder args, Path
4853
}
4954

5055
private static RaungClassVisitor runForInputStream(RaungDisasmBuilder args, InputStream in) throws IOException {
51-
ClassReader reader = new ClassReader(in);
56+
return execute(args, new ClassReader(in));
57+
}
58+
59+
private static RaungClassVisitor execute(RaungDisasmBuilder args, ClassReader reader) {
5260
RaungClassVisitor visitor = new RaungClassVisitor(args);
5361
reader.accept(visitor, 0); // TODO: add option for skip frames (if '.auto frames' will be used)
5462
return visitor;

0 commit comments

Comments
 (0)