From 5181c6f7199232388afa77c490fea7ac983af4d2 Mon Sep 17 00:00:00 2001 From: Algent Date: Thu, 3 Sep 2026 19:47:10 +0200 Subject: [PATCH] refactor: port compiler startup to Java --- JAVA_MIGRATION.md | 6 +-- JAVA_MIGRATION_HANDOFF.md | 22 +++++----- docs/migration/HISTORY.md | 21 +++++++++ .../multipart/asm/ASMMixinCompiler.scala | 34 ++++++-------- .../multipart/asm/CompilerBootstrap.java | 44 +++++++++++++++++++ 5 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/asm/CompilerBootstrap.java diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 46dbc8d..3f3f338 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -25,9 +25,9 @@ No trustworthy tool will produce a maintainable Java port automatically. A decom Work continues on `algent/java`. The low-coupling queue, core part/tile types, registries, handlers, factories, placement/render helpers, both generators and all built-in tile traits are Java. Six generated microblock trait -files and the compiler/signature/analyser compatibility shells remain Scala. Composite generation and Java-trait -rewriting now delegate to Java helpers. The next bounded target is an assessment of the remaining compiler -startup/model shell; retain ScalaSignature path-dependent bridges and keep compiler behavior changes separate. +files and the compiler/signature/analyser compatibility shells remain Scala. Composite generation, Java-trait +rewriting and compiler startup now delegate to Java helpers. The next bounded target is abstract-Java-mixin support, +separate from Java-path side-only filtering; retain the ScalaSignature path-dependent model bridges. Start with [JAVA_MIGRATION_HANDOFF.md](JAVA_MIGRATION_HANDOFF.md) for the exact source/test baseline, workflow and Java-source limitations. Read both the [ABI inventory](JAVA_MIGRATION_ABI_INVENTORY.md) and diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index e116985..4afffe3 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -16,24 +16,24 @@ migration checkout; `codex/tile-compatibility-fixes` was deleted after its fixes ## Current state and next target -**333 plain-JVM tests and 200 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **213 Java -files and 9 Scala files / 1,196 nonblank Scala lines**. The packaged inventory has 436 classes. +**333 plain-JVM tests and 207 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **214 Java +files and 9 Scala files / 1,188 nonblank Scala lines**. The packaged inventory has 437 classes. -Latest bounded port: `ASMMixinCompiler.registerJavaTrait` delegates to Java `JavaTraitRegistration`, after nine -characterization tests passed against untouched Scala (`85ad2e4`). Clean verification after stopping Gradle matched -428 non-target class APIs, all five non-target compiler closures, 3,635 non-target method bodies and all 102 generated -dump names/hashes. The five `@Mod` annotations match both packaged versions. Local reference jars, reports and scripts -are in `run/migration-java-trait-reference/`; `clean` preserves this ignored directory. +Latest bounded port: `ASMMixinCompiler` startup reflection, map construction and sanity warmup delegate to Java +`CompilerBootstrap`, after seven characterization tests passed against untouched Scala (`e9d81d6`). Clean verification +after stopping Gradle matched 431 non-target class APIs, all five compiler closures, 3,653 non-target method bodies and +all 102 generated dump names/hashes. The five `@Mod` annotations match both packaged versions. Local reference jars, +reports and scripts are in `run/migration-compiler-startup-reference/`; `clean` preserves this ignored directory. -**Next: assess the remaining `ASMMixinCompiler` startup/model shell and select the next isolated extraction.** The -ScalaSignature path-dependent model bridges must remain Scala-facing. Keep abstract-Java-mixin support, Java-path -side-only filtering and other compiler changes as separate behavior targets with fresh characterization. +**Next: allow abstract Java mixins as a separate compiler behavior target.** This is the first prerequisite for +porting the remaining microblock traits. Characterize rejection, base selection and abstract-method handling first; +keep Java-path side-only filtering as its own following target. The ScalaSignature path-dependent model bridges remain. Remaining Scala units: | Files under `src/main/scala/codechicken` | Why they remain | | --- | --- | -| `multipart/asm/ASMMixinCompiler.scala` | Composition, Java-trait rewriting, startup and nested model bridges | +| `multipart/asm/ASMMixinCompiler.scala` | Retained nested models, construction callbacks and Scala entry-point shell | | `multipart/asm/ScalaSignature.scala` | Named models, primitive/erased bridges and five generic inner-construction branches | | `multipart/asm/StackAnalyser.scala` | Class/companion/model shell over Java `StackAnalyserLogic` | | `microblock/MicroblockTraits.scala`, `FaceMicroblockTraits.scala`, `CornerMicroblockTraits.scala`, `EdgeMicroblockTraits.scala`, `HollowMicroblockTraits.scala`, `TMicroOcclusion.scala` | Generated traits; need abstract Java mixins and Java-path side-only member handling first | diff --git a/docs/migration/HISTORY.md b/docs/migration/HISTORY.md index 059bbb8..684b846 100644 --- a/docs/migration/HISTORY.md +++ b/docs/migration/HISTORY.md @@ -969,3 +969,24 @@ differences belong in the [divergence ledger](../../JAVA_MIGRATION_DIVERGENCES.m Scala-compilation guard and dev config remain correct. Sources total 213 Java files and 9 Scala files / 1,196 nonblank Scala lines. Next: assess the remaining compiler startup/model shell before selecting another extraction; retained ScalaSignature model bridges remain the Java-source limit. + +### 2026-09-03 — compiler startup + +- Added seven Forge characterization tests for the `ASMMixinCompiler` singleton startup, passing against untouched + Scala and committed first as `e9d81d6`. They execute reflective definition and transformer invocation on isolated + launch loaders, verify the live transformer-exception set, exercise sanity-checker byte loading and freeze loader, + member-accessibility and independent mutable-map behavior. +- Moved reflection lookup/access, map construction and sanity warmup into Java `CompilerBootstrap` while retaining the + singleton's field names, descriptors, modifiers, accessors and initialization order. The packaged inventory grows + from 436 to 437 only for the helper; the shared classfile ledger covers it and no effective divergence was added. +- Saved the reference source/jar, reports, 102 generated outputs and checks in ignored + `run/migration-compiler-startup-reference/`. Clean verification after stopping Gradle matches 431 non-target class + APIs, 3,653 non-target method bodies, all five compiler closures and every output name/hash. Formatting, checkstyle, + build and Forge pass: 333 JVM / 207 Forge, zero failures/errors/skips. All five packaged `@Mod` versions, the forced + Scala-compilation guard and dev config remain correct. Sources total 214 Java files and 9 Scala files / 1,188 + nonblank Scala lines. +- Audited the preserved Java-rewriter defects against current registrations and supplied consumers. The ABI inventory + exposes only ProjectRed's external Scala-trait registration, which bypasses this path; current Java mixins have no + wide state fields and the Forge registration suite remains green. Wide getter maxima, primitive-array analysis and + malformed inherited-call casts are real but dormant for current consumers. Abstract Java mixins and Java-path side + filtering remain required before the microblock traits can move, and will be separate behavior targets. diff --git a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala index e6290ae..5e0bb70 100644 --- a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala +++ b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala @@ -13,31 +13,23 @@ import net.minecraft.launchwrapper.LaunchClassLoader import ASMImplicits._ object ASMMixinCompiler { - val cl = getClass.getClassLoader.asInstanceOf[LaunchClassLoader] - val m_defineClass = classOf[ClassLoader].getDeclaredMethod( - "defineClass", - classOf[Array[Byte]], - Integer.TYPE, - Integer.TYPE - ) - val m_runTransformers = classOf[LaunchClassLoader].getDeclaredMethod( - "runTransformers", - classOf[String], - classOf[String], - classOf[Array[Byte]] - ) + val cl = CompilerBootstrap.loader(getClass) + val m_defineClass = CompilerBootstrap.defineClassMethod() + val m_runTransformers = CompilerBootstrap.runTransformersMethod() val f_transformerExceptions = - classOf[LaunchClassLoader].getDeclaredField("transformerExceptions") - m_defineClass.setAccessible(true) - m_runTransformers.setAccessible(true) - f_transformerExceptions.setAccessible(true) + CompilerBootstrap.transformerExceptionsField() + CompilerBootstrap.open( + m_defineClass, + m_runTransformers, + f_transformerExceptions + ) - private val traitByteMap = MMap[String, Array[Byte]]() - private val mixinMap = MMap[String, MixinInfo]() + private val traitByteMap = CompilerBootstrap.mutableMap[String, Array[Byte]]() + private val mixinMap = CompilerBootstrap.mutableMap[String, MixinInfo]() def define(name: String, bytes: Array[Byte]) = ClassBytes.define(name, bytes) - getBytes("cpw/mods/fml/common/asm/FMLSanityChecker") + CompilerBootstrap.warmupSanityChecker() def getBytes(name: String): Array[Byte] = ClassBytes.getBytes(name) @@ -100,7 +92,7 @@ object ASMMixinCompiler { def moduleName = name } - private val infoCache = MMap[String, ClassInfo]() + private val infoCache = CompilerBootstrap.mutableMap[String, ClassInfo]() def remClassInfo(name: String) = infoCache.remove(name) implicit def getClassInfo(name: String) = diff --git a/src/main/scala/codechicken/multipart/asm/CompilerBootstrap.java b/src/main/scala/codechicken/multipart/asm/CompilerBootstrap.java new file mode 100644 index 0000000..625d374 --- /dev/null +++ b/src/main/scala/codechicken/multipart/asm/CompilerBootstrap.java @@ -0,0 +1,44 @@ +package codechicken.multipart.asm; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import net.minecraft.launchwrapper.LaunchClassLoader; + +import scala.collection.mutable.HashMap; +import scala.collection.mutable.Map; + +/** Reflection and storage initialization for the retained compiler singleton. */ +final class CompilerBootstrap { + + private CompilerBootstrap() {} + + static LaunchClassLoader loader(Class owner) { + return (LaunchClassLoader) owner.getClassLoader(); + } + + static Method defineClassMethod() throws NoSuchMethodException { + return ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class); + } + + static Method runTransformersMethod() throws NoSuchMethodException { + return LaunchClassLoader.class.getDeclaredMethod("runTransformers", String.class, String.class, byte[].class); + } + + static Field transformerExceptionsField() throws NoSuchFieldException { + return LaunchClassLoader.class.getDeclaredField("transformerExceptions"); + } + + static void open(AccessibleObject... members) { + for (AccessibleObject member : members) member.setAccessible(true); + } + + static Map mutableMap() { + return new HashMap<>(); + } + + static void warmupSanityChecker() { + ASMMixinCompiler$.MODULE$.getBytes("cpw/mods/fml/common/asm/FMLSanityChecker"); + } +}