Skip to content

Commit aaa3c8f

Browse files
authored
Run on a jlinked image-mode JDK instead of the exploded layout (#37)
1 parent eb5a538 commit aaa3c8f

33 files changed

Lines changed: 517 additions & 298 deletions

‎AGENTS.md‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Node, which is how the tests run.
3232
IndexedDB and one VM per about:blank iframe. Keep these working.
3333
- The release form is an npm package of the built files only
3434
(`java-web.js`, `java-web-worker.js`, `node.js` (the Node entry), `cli.js` (the
35-
`java-web` command), `boot.pack`, `jdk/<module>.jar`,
36-
`jdk/javac.pack`, `jdk/jdk.localedata.txt`, and OpenJDK's notices in `jdk/legal/`), versioned, with
35+
`java-web` command), `boot.pack`, `jdk/image.jar`,
36+
`jdk/javac.pack`, and OpenJDK's notices in `jdk/legal/`), versioned, with
3737
nothing built at install. A GitHub Actions workflow triggered by a release
3838
publishes it to npm; never publish by hand.
3939

@@ -42,7 +42,7 @@ Node, which is how the tests run.
4242
- `src/classfile.ts` class file parser
4343
- `src/runtime.ts` classes, methods, fields, objects, arrays
4444
- `src/interp.ts` bytecode interpreter and per-thread run loop
45-
- `src/jvm.ts` loading, linking, initialization, resolution, scheduler, boot
45+
- `src/jvm.ts` loading, linking, initialization, resolution, scheduler, boot; `src/jimage.ts` reads the linked JDK image
4646
- `src/indy.ts` invokedynamic, method handles
4747
- `src/snapshot.ts` boot snapshots: saving and restoring the VM as it is after boot
4848
- `src/library.ts` library mode in the VM: object handles, calls, field access,
@@ -76,12 +76,9 @@ Node, which is how the tests run.
7676

7777
## Glossary
7878

79-
- **java.home image**: `build/jdk`, the Linux x64 JDK's class library in the
80-
exploded layout (`modules/<module>/...` instead of a `lib/modules` jimage),
81-
built the same on every host. Mounted at
82-
`/jdk` in the VFS. It has no `lib/modules`, which would switch the JDK to
83-
image mode; `lib/resources.jimage` holds only the non-class resources, for
84-
`jrt:` URLs.
79+
- **java.home image**: `build/jdk`, a linked image with the Linux x64 JDK's
80+
class library in `lib/modules`, built the same on every host and mounted at
81+
`/jdk` in the VFS.
8582
- **VFS**: the virtual file system the Java side sees (`src/fs.ts`).
8683
- **native**: a JS function implementing a Java `native` method, keyed by
8784
`class.name(desc)`. A plain native returns its result; a generator native

‎docs/browser.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ The npm package holds the built files: `java-web.js`, `java-web-worker.js`,
55
and load `java-web.js` with a classic `<script>` tag. It finds the other files
66
next to itself, or where `cheerpjInit({ runtime: "/path/to/dist/" })` says.
77

8-
- The server should answer range requests for the `jdk/<module>.jar` files,
9-
so a page reads only the classes it needs. A host that compresses ranged
10-
responses still works, at the cost of one full download per file.
8+
- The server should answer range requests for `jdk/image.jar`, the whole JDK
9+
image, so a page reads only the classes it needs. `boot.pack` holds what a
10+
typical start reads, and `jdk/javac.pack` what javac adds. A host that
11+
compresses ranged responses still works, at the cost of one full download of
12+
`jdk/image.jar` (about 120 MB).
1113
- The JIT compiles hot Java methods with `new Function`, so a page with a
1214
Content Security Policy needs `'unsafe-eval'` in `script-src`. Without it,
1315
Java still runs, only interpreted.

‎docs/development.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ same download on Linux x64) into `~/.cache/java-web`, and checks each SHA-256.
66
The runtime image comes from the Linux x64 JDK, so every machine, a Mac
77
included, builds the same image, with the Linux class library whose natives
88
java-web implements. The host JDK is the HotSpot reference the tests run, linked
9-
at `build/hotspot`.
9+
at `build/hotspot`. The build links the selected modules into `build/jdk/lib/modules`;
10+
`lib/packages.txt` indexes packages for the boot class loader. `pnpm build`
11+
puts it, with the files around it, in `dist/jdk/image.jar`, read by byte range.
12+
It packs the byte ranges a typical start reads into `boot.pack`, and those javac
13+
adds into `jdk/javac.pack`. Without `localeData`, the VM leaves `jdk.localedata`
14+
out with `jdk.module.limitmods`, as `--limit-modules` does.
1015

1116
```sh
1217
pnpm install

‎docs/node.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ npx @project516/java-web [options] -jar app.jar [args]
2121
| `--locale-data` | locales other than English (see [support.md](support.md#locales)) |
2222
| `-Xmx` | the largest array, and what `Runtime.maxMemory()` reports (default and at most 1g, rounded up to 2 MB steps, below 2m an error); the total heap is not counted |
2323
| `--max-rss MB` | exit when resident memory passes this (default 2048) |
24-
| `--jdk DIR` | an exploded JDK image instead of the packaged one |
24+
| `--jdk DIR` | a java-web JDK image instead of the packaged one |
2525
| `--debug` | log VM events |
2626

2727
Other `-X` and HotSpot-only options are ignored.

‎docs/support.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ java-web targets headless Java: console apps, libraries and javac.
99
- `java.desktop`'s headless parts work: 2D geometry, image pixels, `Graphics2D`
1010
drawing on a `BufferedImage`, and JavaBeans. So does `java.xml`, including
1111
XML Schema validation and DTDs from the JDK's built-in catalog.
12-
- `jrt:/` URLs read the JDK's resources but not its class files.
1312
- `Deflater` writes valid DEFLATE streams using stored blocks. Compression
1413
levels and strategies currently do not reduce output size; compressed bytes
1514
and compression ratios differ from HotSpot's zlib output.
@@ -19,14 +18,14 @@ java-web targets headless Java: console apps, libraries and javac.
1918
## Locales
2019

2120
Locales other than English need `cheerpjInit({ localeData: true })`, or
22-
`--locale-data` under Node (`localeData: true` for `runNode`). Turn it on
21+
`--locale-data` under Node (`localeData: true` for `runNode`). Adding
22+
`jdk.localedata` or `ALL-SYSTEM` with `--add-modules` turns it on too. Turn it on
2323
when a program formats numbers, dates or currencies, or prints month and day
2424
names, for a locale other than English: for example
2525
`String.format(Locale.GERMANY, ...)`, or a German default locale. Without it,
2626
those locales format as English. It is off by default because, once on, it
2727
slows the first formatting call of every program, English ones included, by
28-
a few hundred milliseconds. With it on, a page also fetches the locale file
29-
list (`jdk/jdk.localedata.txt`, 160 KB) at start, then only the locales it
28+
up to a second on slow devices. With it on, a page reads only the locales it
3029
uses.
3130

3231
## Graphics and text

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"dist/boot.pack",
3838
"dist/jdk/*.jar",
3939
"dist/jdk/javac.pack",
40-
"dist/jdk/jdk.localedata.txt",
4140
"dist/jdk/legal/"
4241
],
4342
"publishConfig": {

‎scripts/ExplodedModuleInfo.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77

8-
// Drops ModuleHashes from module-info.class files, as a JDK exploded build
9-
// has none: exploded modules cannot be hashed, and boot fails when java.base
10-
// records hashes of other modules.
8+
// Drop hashes of the original JDK modules before linking a different set.
119
void main(String[] args) throws Exception {
1210
ClassFile cf = ClassFile.of();
1311
for (String a : args) {

‎scripts/HashedModules.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.lang.classfile.Attributes;
2+
import java.lang.classfile.ClassFile;
3+
import java.nio.file.Path;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
7+
// Prints a jmod --hash-modules pattern for the modules a java.base
8+
// module-info.class records hashes of, among the given ones.
9+
// Args: <java.base module-info.class> <modules, space separated>
10+
void main(String[] args) throws Exception {
11+
var hashes = ClassFile.of().parse(Path.of(args[0])).findAttribute(Attributes.moduleHashes()).orElseThrow();
12+
Set<String> linked = Set.of(args[1].split(" "));
13+
System.out.println(hashes.hashes().stream().map(h -> h.moduleName().name().stringValue())
14+
.filter(linked::contains).sorted().collect(Collectors.joining("|", "^(", ")$")));
15+
}

‎scripts/LinuxImage.java‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import java.io.DataInputStream;
21
import java.io.IOException;
32
import java.lang.module.ModuleDescriptor;
43
import java.lang.module.ModuleDescriptor.Requires;
@@ -14,9 +13,7 @@
1413
import java.util.Set;
1514
import java.util.TreeSet;
1615

17-
// Builds the java.home image from a Linux JDK without jlink: keeps the root
18-
// modules and the modules they require, as jlink does without --bind-services,
19-
// and copies the conf/, lib/ and legal/ files of those modules.
16+
// Copies the conf/, lib/ and legal/ files of the linked modules.
2017
// Args: <JDK home> <modules extracted from its lib/modules> <root modules, comma separated> <output dir>
2118
void main(String[] args) throws IOException {
2219
Path jdk = Path.of(args[0]), src = Path.of(args[1]), out = Path.of(args[3]);
@@ -36,7 +33,6 @@ void main(String[] args) throws IOException {
3633
// Each module's files are listed in jdk.jlink as type|kind|hash or link target|path
3734
Path lists = src.resolve("jdk.jlink/jdk/tools/jlink/internal/runtimelink");
3835
for (String name : keep) {
39-
Files.move(src.resolve(name), out.resolve("modules").resolve(name));
4036
Path list = lists.resolve("fs_" + name + "_files");
4137
if (Files.notExists(list)) continue;
4238
for (String line : Files.readAllLines(list)) {
@@ -54,30 +50,8 @@ void main(String[] args) throws IOException {
5450
}
5551
}
5652

57-
restoreSystemModulesMap(src, out);
5853

5954
String version = Files.readAllLines(jdk.resolve("release")).stream()
6055
.filter(l -> l.startsWith("JAVA_VERSION=")).findFirst().orElseThrow();
6156
Files.writeString(out.resolve("release"), version + "\nMODULES=\"" + String.join(" ", keep) + "\"\n");
6257
}
63-
64-
// jlink's system-modules plugin generated SystemModules classes for the whole
65-
// JDK and pointed SystemModulesMap at them. The image has no lib/modules, so
66-
// the JDK must find its modules by scanning, which the original map allows.
67-
// diff_java.base holds the original bytes for undoing the plugin.
68-
void restoreSystemModulesMap(Path src, Path out) throws IOException {
69-
Path diff = src.resolve("jdk.jlink/jdk/tools/jlink/internal/runtimelink/diff_java.base");
70-
String dir = "/java.base/jdk/internal/module/";
71-
try (var in = new DataInputStream(Files.newInputStream(diff))) {
72-
in.readInt();
73-
for (int n = in.readInt(); n > 0; n--) {
74-
short kind = in.readShort();
75-
String name = new String(in.readNBytes(in.readInt()));
76-
byte[] original = in.readNBytes(in.readInt());
77-
if (!name.startsWith(dir)) continue;
78-
Path file = out.resolve("modules").resolve(name.substring(1));
79-
if (kind == 1) Files.delete(file);
80-
else Files.write(file, original);
81-
}
82-
}
83-
}

‎scripts/ResourceImage.java‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)