summaryrefslogtreecommitdiff
path: root/tools/gen_class.py
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-07 11:00:16 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-07 11:00:16 +0100
commite7ad22515e1d79f02f8cf2c88435e26854552a46 (patch)
treee237ab8083b2919f8ddd18af762e9a03f01a1919 /tools/gen_class.py
parent64223f61fc8413ad69ba0e814fbbc552a6c57c33 (diff)
Bump kotlin to 2.4.21
Support <clinit>
Diffstat (limited to 'tools/gen_class.py')
-rwxr-xr-xtools/gen_class.py59
1 files changed, 53 insertions, 6 deletions
diff --git a/tools/gen_class.py b/tools/gen_class.py
index 8e1e0f4..ba0a92f 100755
--- a/tools/gen_class.py
+++ b/tools/gen_class.py
@@ -495,11 +495,52 @@ def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum):
return code_attr(cp, max_stack, count_locals(desc, False), b)
-def clinit_body(cp, dotname, loaded_ref):
+def clinit_body(cp, dotname, loaded_ref, internal, companion=None):
b = ldc(cp.string(dotname))
b += bytes([0xb8]) + struct.pack(">H", loaded_ref)
+ max_stack = 2
+ if companion:
+ # new; dup; aconst_null -> 3 on the stack before invokespecial pops
+ max_stack = 3
+ # mirror the real <clinit>'s companion-object init:
+ # new Companion; dup; aconst_null; invokespecial Companion.<init>;
+ # putstatic Companion
+ # (leaving the static null makes real code touching
+ # <This>.Companion NPE before any stub method is ever called)
+ fname, fdesc, cname, ctor = companion
+ b += bytes([0xbb]) + struct.pack(">H", cp.cls(cname)) # new
+ b += b"\x59" # dup
+ b += b"\x01" # aconst_null
+ b += bytes([0xb7]) + struct.pack(
+ ">H", cp.methodref(cname, "<init>", ctor)) # invokespecial
+ b += bytes([0xb3]) + struct.pack(
+ ">H", cp.fieldref(internal, fname, fdesc)) # putstatic
b += b"\xb1" # return
- return code_attr(cp, 2, 0, b)
+ return code_attr(cp, max_stack, 0, b)
+
+
+def companion_init(name, info, defs):
+ """If class `name` (internal) declares a static `<name>$Companion` field,
+ describe how to instantiate it in <clinit>. Returns
+ (field_name, field_desc, companion_internal, ctor_desc) or None.
+ Kotlin companion objects are built by the enclosing class's real
+ <clinit> through the synthetic DefaultConstructorMarker ctor."""
+ marker_ctor = "(Lkotlin/jvm/internal/DefaultConstructorMarker;)V"
+ for f in info.get("fields", []):
+ if not (f["access"] & A_STATIC):
+ continue
+ t = f["desc"][1:-1]
+ if t != name + "$Companion" or len(f["desc"]) <= 2:
+ continue
+ cname = t
+ ce = defs.get(cname)
+ ctors = {mm["desc"] for mm in ce[1].get("methods", [])
+ if mm["name"] == "<init>"} if ce else set()
+ if marker_ctor in ctors:
+ return (f["name"], f["desc"], cname, marker_ctor)
+ if "()V" in ctors:
+ return (f["name"], f["desc"], cname, "()V")
+ return None
def bridge_body(cp, dotname, name, target_desc, is_static):
@@ -538,7 +579,7 @@ def field_bytes(cp, name, desc, access):
# ----------------------------------------------------------------------
# class emission
# ----------------------------------------------------------------------
-def emit_class(cp, name, info, is_iface, dotname):
+def emit_class(cp, name, info, is_iface, dotname, companion=None):
this_idx = cp.cls(name)
super_name = (info.get("super") or OBJ) if not is_iface else OBJ
super_idx = cp.cls(super_name)
@@ -604,10 +645,12 @@ def emit_class(cp, name, info, is_iface, dotname):
call_ref, is_enum)
methods.append(method_bytes(cp, "<init>", "()V",
A_PUBLIC, body))
- # synthesized static initializer -> KotlinProxyLog.loaded(name)
+ # synthesized static initializer -> KotlinProxyLog.loaded(name),
+ # plus the companion-object init when the class has one
methods.append(method_bytes(cp, "<clinit>", "()V",
A_STATIC | A_PRIVATE,
- clinit_body(cp, dotname, loaded_ref)))
+ clinit_body(cp, dotname, loaded_ref,
+ name, companion)))
out = bytearray()
out += struct.pack(">IHH", MAGIC, MINOR, MAJOR)
@@ -727,6 +770,9 @@ def main():
pass
for s in seeds:
s0.add(s)
+ bad = sorted(s for s in seeds if s not in defs)
+ if bad:
+ sys.exit("error: seed(s) not found in def jars:\n " + "\n ".join(bad))
# transitive closure (skipped with --no-closure: emit exactly the seed set)
closure = set(s0)
@@ -782,7 +828,8 @@ def main():
else:
_, info = e
is_iface = bool(info["access"] & A_INTERFACE)
- data = emit_class(cp, name, info, is_iface, dot)
+ companion = None if is_iface else companion_init(name, info, defs)
+ data = emit_class(cp, name, info, is_iface, dot, companion)
n_full += 1
if is_iface:
n_iface += 1