Skip to content

Seed and tape Java and .NET ports - #43

Merged
rjnienaber merged 1 commit into
mainfrom
feat/jvm-and-dotnet-runners
Sep 8, 2026
Merged

rjnienaber merged 1 commit into
mainfrom
feat/jvm-and-dotnet-runners

Conversation

@rjnienaber

Copy link
Copy Markdown
Owner

Two more languages a script can be played against without editing the port.

Both compile on the way to running, which is what a runner can join: neither
has an environment variable that puts a different generator behind the one a
program calls, so the shim has to get in front of the compiler instead.

Java

runners/java.yaml loads a javaagent that rewrites the port's own call sites
as each class loads — Math.random, Random and ThreadLocalRandom become
calls to prescript.Tape, which returns the next tape value and ignores the
generator it was handed.

  • Written against java.lang.classfile, standard since JDK 24, so the agent
    needs no bytecode library.
  • It rewrites the port and leaves the JDK alone: no platform class is patched
    and nothing is retransformed.
  • Skips the platform loader and named java.*/jdk.* modules, which matters
    because java rng.java compiles in-process and javac's own classes would
    otherwise draw from the tape.
  • Does not reach SecureRandom, SplittableRandom, or draws made from inside
    java.base — Collections.shuffle(list, rnd) calls nextInt at a call site
    the agent never sees.

It is the one shim that is compiled rather than read, so make shims builds it
and .gitignore keeps the jar out of the repository — a jar built from sources
here is easier to trust than one that arrived with them.

.NET

runners/dotnet.yaml compiles a namespace System { public class Random } into
the program ahead of the real one. The C# compiler prefers a type it is
compiling over the same type from a reference, so new Random() binds to the
shim with the port unchanged. The file is added to the compilation with
--property:CustomBeforeMicrosoftCommonTargets, which reaches a single .cs
file as readily as a .csproj.

Replacing the class replaces Shuffle, GetItems and Random.Shared along
with the draws, which makes it the widest of the shims. Separate assemblies
each get their own copy, so the draw sequence is shared through
AppDomain.SetData.

DOTNET_STARTUP_HOOKS was the obvious route and is a dead end: file-based apps
build with StartupHookSupport and IsDynamicCodeSupported both false, and
HarmonyLib refuses CoreCLR 10 outright. TERM: "dumb" is in the runner because
System.Console otherwise writes terminfo smkx into the transcript the first
time a .NET program reads a key.

Around them

  • docker/Dockerfile gains a Temurin JDK 25 and .NET SDK 10.0.400, pinned and
    checksummed like everything else, and reports both in
    $PRESCRIPT_ENVIRONMENT.
  • docker/run.sh gains NuGet and .NET artifact cache volumes. Not ~/.dotnet,
    which looks like the obvious one and breaks the SDK: it holds the first-run
    sentinel, and the first run is also what creates ~/.local/share.
  • The CI matrix installs both and runs make shims before the suite.
  • internal/play covers both in seeded and taped mode, and a runner whose shim
    has not been built now skips with a message rather than failing obscurely —
    or fails outright under PRESCRIPT_REQUIRE_RUNNERS, as in CI and the image.

Verified with make lint, PRESCRIPT_REQUIRE_RUNNERS=1 make test,
make examples, make image and make docker_test.

🤖 Generated with Claude Code

https://claude.ai/code/session_019f9gkZVUEo3jtv51GSb4vJ

Both languages compile on the way to running, which is what a runner can
join: neither has an environment variable that puts a different generator
behind the one a program calls.

Java ships a javaagent that rewrites the port's own call sites as each
class loads -- Math.random, Random and ThreadLocalRandom become calls to a
shim that returns the next tape value. Written against java.lang.classfile,
standard since JDK 24, so the agent carries no bytecode library; it rewrites
the port and leaves the JDK alone, so nothing is retransformed and no
platform class is patched. It is the one shim that has to be built, which is
what `make shims` is for.

.NET declares a System.Random of its own and hands it to the compiler ahead
of the real one, injected with an MSBuild targets file that reaches a single
.cs file as readily as a .csproj. Replacing the class replaces Shuffle,
GetItems and Random.Shared along with the draws, which makes it the widest
of the shims. DOTNET_STARTUP_HOOKS was the obvious route and is a dead end:
file-based apps build with startup hooks and dynamic code both switched off,
so a detour library has nothing to detour with.

The image grows a Temurin JDK and a .NET SDK, both pinned and checksummed
like everything else in it, and the CI matrix installs the same two.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019f9gkZVUEo3jtv51GSb4vJ
@rjnienaber
rjnienaber merged commit 023a5cd into main Sep 8, 2026
5 checks passed
@rjnienaber
rjnienaber deleted the feat/jvm-and-dotnet-runners branch September 8, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant