mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-18 11:29:33 +00:00
refactor: extract Java signature parser
Retain Scala model bridges and generic inner construction to preserve ABI.
This commit is contained in:
1 parent
1ad2b0f513
commit
8581d30fe6
4 files changed
+205
-80
No files matched your search
@@ -1331,3 +1331,18 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec
|
||||
private closures disappear, reducing the packaged inventory from 460 to 455 classes. The existing ledger row now
|
||||
describes the retained Java forwarders; no new divergence was added. `multipart/asm/ScalaSignature.scala` is next
|
||||
as the remaining parser/nested-model unit, with ASM compiler algorithm changes still deferred.
|
||||
- Extracted `ScalaSignature` table decoding, name and literal evaluation, collection and object/class lookup into one
|
||||
package-private Java helper. The Scala shell retains the complete nested model and five generic construction
|
||||
branches. That boundary is required: primitive literal case classes expose both primitive and erased `Object`
|
||||
`value()` methods, which Java cannot declare together, while Java misreads the outer parameter in five Scala 2.11
|
||||
generic inner constructors. A full model port is deferred until it has an explicit bridge strategy.
|
||||
- The parser keeps direct case-class construction, path-dependent return types, Scala `List`/`IndexedSeq`/`Option`
|
||||
results, table mutation behavior, annotation grouping, signed literal conversion and the existing partial tag
|
||||
interpretation. All 69 retained signature types' public names, descriptors and generic declarations match the saved
|
||||
`1ad2b0f` reference. The disassembly of 188 otherwise unchanged model, compiler, analyser, factory and generator
|
||||
types is identical; all 39 generated dump names and SHA-256 hashes also match.
|
||||
- No tests were added, per the current user instruction. Formatting/checkstyle/build passes all 276 existing JVM
|
||||
tests and Java 8 Forge passes all 116 tests, including the external Scala-trait path. Four private parser/lookup
|
||||
closures are replaced by one Java helper, reducing the packaged inventory from 455 to 452 classes and leaving 206
|
||||
Java files plus 9 Scala files / 1,883 nonblank Scala lines. No new compatibility divergence needs a ledger entry.
|
||||
`multipart/asm/StackAnalyser.scala` is the next bounded target; keep its control flow and nested model separable.
|
||||
@@ -12,8 +12,8 @@ what breaks, and what is left. The other documents hold the reasoning.
|
||||
| `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client |
|
||||
| `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command |
|
||||
|
||||
Branch: `algent/java`. Base: `master`. 163 commits including the API-cleanup plan, divergence-log cleanup and ports
|
||||
through `MultipartMixinFactory`.
|
||||
Branch: `algent/java`. Base: `master`. 164 commits including the API-cleanup plan, divergence-log cleanup and ports
|
||||
through the `ScalaSignature` parser extraction.
|
||||
|
||||
## The one rule that matters
|
||||
|
||||
@@ -278,7 +278,7 @@ across the port. It is also where the two shim constraints in the gotchas list w
|
||||
`rayTraceAll`'s index production is now characterized, closing the gap the read-path cleanup left: the index written
|
||||
into `ExtendedMOP.data` is what `reduceMOP` hands back to every click, activate, harvest and pick block.
|
||||
|
||||
205 Java files, 9 Scala files, 1,933 Scala lines left (non-blank; that is the metric this figure has always used).
|
||||
206 Java files, 9 Scala files, 1,883 Scala lines left (non-blank; that is the metric this figure has always used).
|
||||
|
||||
## What is left, and in what order
|
||||
|
||||
@@ -601,11 +601,18 @@ stack-analyser, base-factory and generator types have identical disassembly. The
|
||||
pass unchanged. Removing five private closures reduces the packaged inventory from 460 to 455 classes. No tests were
|
||||
added, and no new compatibility divergence was introduced.
|
||||
|
||||
**Next: `multipart/asm/ScalaSignature.scala`.** The remaining parser and its nested model form a larger compatibility
|
||||
unit. Preserve nested binary names, case-class/companion surfaces, outer-instance bindings, flags and literal
|
||||
decoding, Scala collections and annotation parsing. Keep ProjectRed's external Scala-trait path working; compare
|
||||
generated dumps against a saved reference. Leave `StackAnalyser` and the ASM compiler algorithms unchanged apart
|
||||
from necessary source-call adjustments.
|
||||
The `ScalaSignature` table/name decoder, most tag evaluation, collection and symbol lookups are now implemented in
|
||||
the package-private Java `ScalaSignatureParser`. The nested model remains in Scala: its primitive literal case classes
|
||||
publish both primitive `value()` methods and erased `Object value()` bridges, which Java source cannot declare
|
||||
together, and Java misreads the outer parameter in five Scala 2.11 generic inner constructors. Those construction
|
||||
branches also remain in Scala. Moving the model needs an explicit bytecode-bridge strategy; do not lose those methods
|
||||
or outer-instance bindings merely to reduce the Scala count.
|
||||
|
||||
**Next: `multipart/asm/StackAnalyser.scala`.** Treat its analyser control flow and nested model separately if the same
|
||||
Java-source limitations appear. Preserve every nested binary name, constructor, outer/implicit instruction binding,
|
||||
case-class surface, mutable Scala collection descriptor and the current opcode behavior (including its existing
|
||||
primitive-cast and `NEWARRAY` limitations). Keep `ASMMixinCompiler` algorithms unchanged and compare all generated
|
||||
dumps against a saved reference.
|
||||
The remaining generated microblock traits still need the Phase 7 abstract-Java-mixin and side-only-member support
|
||||
before their Java mixin inputs are safe; do not bypass those prerequisites by flattening their inheritance.
|
||||
|
||||
|
||||
@@ -34,16 +34,8 @@ object ScalaSignature {
|
||||
class ScalaSignature(val bytes: Bytes) {
|
||||
val major = bytes.arr(0).toInt
|
||||
val minor = bytes.arr(1).toInt
|
||||
val table = {
|
||||
val bcr = bytes.reader
|
||||
bcr.pos_$eq(2)
|
||||
Array.tabulate(bcr.readNat) { i =>
|
||||
val start = bcr.pos
|
||||
val tpe = bcr.readByte
|
||||
val len = bcr.readNat
|
||||
bcr.advance(len, new SigEntry(i, start, Bytes(bytes.arr, bcr.pos, len)))
|
||||
}
|
||||
}
|
||||
val table =
|
||||
ScalaSignatureParser.readTable(this, bytes).asInstanceOf[Array[SigEntry]]
|
||||
|
||||
trait SymbolRef extends Flags {
|
||||
def full: String
|
||||
@@ -209,70 +201,25 @@ class ScalaSignature(val bytes: Bytes) {
|
||||
def getValue[T](name: String) = values(name).asInstanceOf[T]
|
||||
}
|
||||
|
||||
def evalS(i: Int): String = {
|
||||
val e = table(i)
|
||||
val bc = e.bytes
|
||||
val bcr = bc.reader
|
||||
e.id match {
|
||||
case 1 | 2 => bcr.readString(bc.len)
|
||||
case 3 => NoSymbol.full
|
||||
case 9 | 10 =>
|
||||
var s = evalS(bcr.readNat)
|
||||
if (bc.pos + bc.len > bcr.pos)
|
||||
s = evalS(bcr.readNat) + "." + s
|
||||
s
|
||||
}
|
||||
}
|
||||
def evalS(i: Int): String = ScalaSignatureParser.evalS(this, i)
|
||||
|
||||
def evalT[T](i: Int) = eval(i).asInstanceOf[T]
|
||||
|
||||
def evalList[T](bcr: ByteCodeReader) = {
|
||||
val l = List.newBuilder
|
||||
while (bcr.more)
|
||||
l += evalT(bcr.readNat)
|
||||
l.result()
|
||||
}
|
||||
def evalList[T](bcr: ByteCodeReader): List[Nothing] =
|
||||
ScalaSignatureParser.evalList(this, bcr).asInstanceOf[List[Nothing]]
|
||||
|
||||
def eval(i: Int): Any = {
|
||||
// only parse the ones that matter for this project
|
||||
val e = table(i)
|
||||
val bcr = e.bytes.reader
|
||||
|
||||
def nat = bcr.readNat
|
||||
def evalS = this.evalS(nat)
|
||||
def evalT[T] = this.evalT[T](nat)
|
||||
val id = e.id
|
||||
def evalT[T] = this.evalT[T](bcr.readNat)
|
||||
def evalList[T] = this.evalList[T](bcr)
|
||||
|
||||
e.id match {
|
||||
case 1 | 2 => this.evalS(i)
|
||||
case 3 => NoSymbol
|
||||
case 6 => ClassSymbol(evalS, evalT, nat, nat)
|
||||
case 7 => ObjectSymbol(evalS, evalT, nat, nat)
|
||||
case 8 => MethodSymbol(evalS, evalT, nat, nat)
|
||||
case 9 | 10 => ExternalSymbol(this.evalS(i))
|
||||
case 11 | 12 => NoType // 12 is actually NoPrefixType (no lower bound)
|
||||
case 13 => ThisType(evalT)
|
||||
case 14 => SingleType(evalT, evalT)
|
||||
case 16 => TypeRefType(evalT, evalT, evalList)
|
||||
case 19 => ClassType(evalT, evalList)
|
||||
case 20 => MethodType(evalT, evalList)
|
||||
case 21 | 48 =>
|
||||
ParameterlessType(
|
||||
evalT
|
||||
) // 48 is actually a bounded super type, but it should work fine for this project
|
||||
case 25 => BooleanLiteral(bcr.readLong != 0)
|
||||
case 26 => ByteLiteral(bcr.readLong.toByte)
|
||||
case 27 => ShortLiteral(bcr.readLong.toShort)
|
||||
case 28 => CharLiteral(bcr.readLong.toChar)
|
||||
case 29 => IntLiteral(bcr.readLong.toInt)
|
||||
case 30 => LongLiteral(bcr.readLong)
|
||||
case 31 =>
|
||||
FloatLiteral(java.lang.Float.intBitsToFloat(bcr.readLong.toInt))
|
||||
case 32 => DoubleLiteral(java.lang.Double.longBitsToDouble(bcr.readLong))
|
||||
case 33 => StringLiteral(evalS)
|
||||
case 34 => NullLiteral
|
||||
case 35 => TypeLiteral(evalT)
|
||||
case 36 => EnumLiteral(evalT)
|
||||
// Java misreads the outer parameter in these Scala 2.11 generic constructors.
|
||||
id match {
|
||||
case 16 => TypeRefType(evalT, evalT, evalList)
|
||||
case 19 => ClassType(evalT, evalList)
|
||||
case 20 => MethodType(evalT, evalList)
|
||||
case 40 =>
|
||||
AnnotationInfo(
|
||||
evalT,
|
||||
@@ -280,15 +227,17 @@ class ScalaSignature(val bytes: Bytes) {
|
||||
evalList.grouped(2).map(g => (g(0), g(1))).toMap
|
||||
)
|
||||
case 44 => ArrayLiteral(evalList)
|
||||
case _ => e
|
||||
case _ => ScalaSignatureParser.eval(this, i, e, bcr, id)
|
||||
}
|
||||
}
|
||||
|
||||
def collect[T](id: Int) = (0 until table.length).collect {
|
||||
case i if table(i).id == id => evalT(i): T
|
||||
}
|
||||
def collect[T](id: Int): scala.collection.immutable.IndexedSeq[T] =
|
||||
ScalaSignatureParser.collect[T](this, id)
|
||||
|
||||
def findObject(name: String) = collect[ObjectSymbol](7).find(_.full == name)
|
||||
def findClass(name: String) =
|
||||
collect[ClassSymbol](6).find(c => !c.isModule && c.full == name)
|
||||
def findObject(name: String): Option[ObjectSymbol] =
|
||||
ScalaSignatureParser
|
||||
.findObject(this, name)
|
||||
.asInstanceOf[Option[ObjectSymbol]]
|
||||
def findClass(name: String): Option[ClassSymbol] =
|
||||
ScalaSignatureParser.findClass(this, name).asInstanceOf[Option[ClassSymbol]]
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package codechicken.multipart.asm;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import codechicken.multipart.asm.ScalaSignature.Bytes;
|
||||
import scala.MatchError;
|
||||
import scala.None$;
|
||||
import scala.Option;
|
||||
import scala.Some;
|
||||
import scala.collection.Iterator;
|
||||
import scala.collection.immutable.IndexedSeq;
|
||||
import scala.collection.immutable.IndexedSeq$;
|
||||
import scala.collection.immutable.List;
|
||||
import scala.collection.immutable.List$;
|
||||
import scala.collection.mutable.Builder;
|
||||
|
||||
/** Parser implementation behind the retained Scala case-class and path-dependent API. */
|
||||
final class ScalaSignatureParser {
|
||||
|
||||
private ScalaSignatureParser() {}
|
||||
|
||||
// Keep path-dependent model types out of signatures read by Scala 2.11's Java parser.
|
||||
static Object readTable(ScalaSignature sig, Bytes bytes) {
|
||||
ByteCodeReader reader = bytes.reader();
|
||||
reader.pos_$eq(2);
|
||||
// Scala Array.tabulate produces an empty array for a negative (overflowed) count.
|
||||
ScalaSignature.SigEntry[] table = new ScalaSignature.SigEntry[Math.max(0, reader.readNat())];
|
||||
for (int i = 0; i < table.length; i++) {
|
||||
int start = reader.pos();
|
||||
reader.readByte();
|
||||
int length = reader.readNat();
|
||||
table[i] = reader
|
||||
.advance(length, sig.new SigEntry(i, start, new Bytes(sig.bytes().arr(), reader.pos(), length)));
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
static String evalS(ScalaSignature sig, int index) {
|
||||
ScalaSignature.SigEntry entry = sig.table()[index];
|
||||
Bytes bytes = entry.bytes();
|
||||
ByteCodeReader reader = bytes.reader();
|
||||
byte id = entry.id();
|
||||
switch (id) {
|
||||
case 1:
|
||||
case 2:
|
||||
return reader.readString(bytes.len());
|
||||
case 3:
|
||||
return sig.NoSymbol().full();
|
||||
case 9:
|
||||
case 10:
|
||||
String name = sig.evalS(reader.readNat());
|
||||
if (bytes.pos() + bytes.len() > reader.pos()) {
|
||||
name = sig.evalS(reader.readNat()) + "." + name;
|
||||
}
|
||||
return name;
|
||||
default:
|
||||
throw new MatchError(Byte.valueOf(id));
|
||||
}
|
||||
}
|
||||
|
||||
static List<?> evalList(ScalaSignature sig, ByteCodeReader reader) {
|
||||
Builder<Object, List<Object>> builder = List$.MODULE$.newBuilder();
|
||||
while (reader.more()) {
|
||||
builder.$plus$eq(sig.evalT(reader.readNat()));
|
||||
}
|
||||
return builder.result();
|
||||
}
|
||||
|
||||
static Object eval(ScalaSignature sig, int index, Object entry, ByteCodeReader reader, byte id) {
|
||||
// The Scala optimizer constructs case classes directly, without initializing their companions.
|
||||
switch (id) {
|
||||
case 1:
|
||||
case 2:
|
||||
return sig.evalS(index);
|
||||
case 3:
|
||||
return sig.NoSymbol();
|
||||
case 6:
|
||||
return sig.new ClassSymbol(sig.evalS(reader.readNat()), sig.evalT(reader.readNat()), reader.readNat(),
|
||||
reader.readNat());
|
||||
case 7:
|
||||
return sig.new ObjectSymbol(sig.evalS(reader.readNat()), sig.evalT(reader.readNat()), reader.readNat(),
|
||||
reader.readNat());
|
||||
case 8:
|
||||
return sig.new MethodSymbol(sig.evalS(reader.readNat()), sig.evalT(reader.readNat()), reader.readNat(),
|
||||
reader.readNat());
|
||||
case 9:
|
||||
case 10:
|
||||
return sig.new ExternalSymbol(sig.evalS(index));
|
||||
case 11:
|
||||
case 12: // NoPrefixType is treated as NoType in the reference.
|
||||
return sig.NoType();
|
||||
case 13:
|
||||
return sig.new ThisType(sig.evalT(reader.readNat()));
|
||||
case 14:
|
||||
return sig.new SingleType(sig.evalT(reader.readNat()), sig.evalT(reader.readNat()));
|
||||
case 21:
|
||||
case 48: // Retain the reference's parameterless interpretation of a bounded super type.
|
||||
return sig.new ParameterlessType(sig.evalT(reader.readNat()));
|
||||
case 25:
|
||||
return sig.new BooleanLiteral(reader.readLong() != 0);
|
||||
case 26:
|
||||
return sig.new ByteLiteral((byte) reader.readLong());
|
||||
case 27:
|
||||
return sig.new ShortLiteral((short) reader.readLong());
|
||||
case 28:
|
||||
return sig.new CharLiteral((char) reader.readLong());
|
||||
case 29:
|
||||
return sig.new IntLiteral((int) reader.readLong());
|
||||
case 30:
|
||||
return sig.new LongLiteral(reader.readLong());
|
||||
case 31:
|
||||
return sig.new FloatLiteral(Float.intBitsToFloat((int) reader.readLong()));
|
||||
case 32:
|
||||
return sig.new DoubleLiteral(Double.longBitsToDouble(reader.readLong()));
|
||||
case 33:
|
||||
return sig.new StringLiteral(sig.evalS(reader.readNat()));
|
||||
case 34:
|
||||
return sig.NullLiteral();
|
||||
case 35:
|
||||
return sig.new TypeLiteral(sig.evalT(reader.readNat()));
|
||||
case 36:
|
||||
return sig.new EnumLiteral(sig.evalT(reader.readNat()));
|
||||
default:
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
static <T> IndexedSeq<T> collect(ScalaSignature sig, int id) {
|
||||
int length = sig.table().length;
|
||||
Builder<T, IndexedSeq<T>> builder = IndexedSeq$.MODULE$.newBuilder();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (sig.table()[i].id() == id) builder.$plus$eq(sig.evalT(i));
|
||||
}
|
||||
return builder.result();
|
||||
}
|
||||
|
||||
static Option<?> findObject(ScalaSignature sig, String name) {
|
||||
Iterator<ScalaSignature.ObjectSymbol> symbols = sig.<ScalaSignature.ObjectSymbol>collect(7).iterator();
|
||||
while (symbols.hasNext()) {
|
||||
ScalaSignature.ObjectSymbol symbol = symbols.next();
|
||||
if (Objects.equals(symbol.full(), name)) return new Some<>(symbol);
|
||||
}
|
||||
return None$.MODULE$;
|
||||
}
|
||||
|
||||
static Option<?> findClass(ScalaSignature sig, String name) {
|
||||
Iterator<ScalaSignature.ClassSymbol> symbols = sig.<ScalaSignature.ClassSymbol>collect(6).iterator();
|
||||
while (symbols.hasNext()) {
|
||||
ScalaSignature.ClassSymbol symbol = symbols.next();
|
||||
if (!symbol.isModule() && Objects.equals(symbol.full(), name)) return new Some<>(symbol);
|
||||
}
|
||||
return None$.MODULE$;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user