Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions packages/java-runtime/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BrowserFS from 'browserfs';
import type ZipFS from 'browserfs/dist/node/backend/ZipFS';
import type { FSModule } from 'browserfs/dist/node/core/FS';

export { FSModule };
Expand Down Expand Up @@ -35,35 +36,27 @@ function recursiveCopy(srcFolder: string, destFolder: string) {
processDir(srcFolder, destFolder);
}

// Seeds an in-memory filesystem out of the doppio.zip artifact:
// `classes/` (doppio's own classes) and `vendor/` (JCL jars). The ZipFS
// mount is used only as a transient extraction source and is unmounted
// right after, so neither the zip buffer nor its index outlive init.
export async function initFs(libZipData: ArrayBuffer): Promise<FSModule> {
await new Promise<void>((resolve, reject) =>
BrowserFS.configure(
{
fs: 'MountableFileSystem',
options: {
'/tmp': {
fs: 'InMemory',
options: {}
},
'/home': {
fs: 'InMemory',
options: {}
},
'/zip': {
fs: 'ZipFS',
options: {
zipData: Buffer.from(libZipData)
}
},
'/sys': {
fs: 'InMemory',
options: {}
}
}
},
(e) => (e ? reject(e) : resolve())
const zipFs = await new Promise<ZipFS>((resolve, reject) =>
BrowserFS.FileSystem.ZipFS.Create({ zipData: Buffer.from(libZipData) }, (e, created) =>
e ? reject(e) : resolve(created!)
)
);
recursiveCopy('/zip', '/sys');
const mfs = new BrowserFS.FileSystem.MountableFileSystem();
mfs.mount('/tmp', new BrowserFS.FileSystem.InMemory());
mfs.mount('/home', new BrowserFS.FileSystem.InMemory());
mfs.mount('/sys', new BrowserFS.FileSystem.InMemory());
mfs.mount('/zip', zipFs);
BrowserFS.initialize(mfs);
try {
recursiveCopy('/zip/classes', '/sys/classes');
recursiveCopy('/zip/vendor', '/sys/vendor');
} finally {
mfs.umount('/zip');
}
return fs;
}
40 changes: 30 additions & 10 deletions packages/java-runtime/src/java-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import type { File } from 'libs/compiler';
import { Context, withCancel } from 'libs/context';
import { inContext, withCancel, type Context } from 'libs/context';

import { FSModule } from './fs';
import { JVMFactory } from './jvm-factory';

export class JavaCompiler {
private lastSourcesKey: string | null = null;

constructor(
protected readonly jvmFactory: JVMFactory,
protected readonly fs: FSModule
) {}

// Skips javac entirely when the sources are unchanged since the last
// successful compilation and compiled classes are still present.
async compile(ctx: Context, files: File[]) {
const key = files.map((file) => `${file.filename}\u0000${file.content}`).join('\u0001');
if (key === this.lastSourcesKey && this.hasClasses()) {
return;
}
const paths = files.map((file) => this.writeFile(file));
// The JVM is single-use: cancel its context when done so the
// factory disposes its stdio wiring, otherwise output leaks
// into subsequent runs.
const [jvmCtx, cancel] = withCancel(ctx);
const jvm = await this.jvmFactory(jvmCtx);
return new Promise<void>((resolve, reject) => {
jvm.runClass('util.Javac', paths, (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error('Compilation failed'));
}
});
}).finally(cancel);
const code = await inContext(
jvmCtx,
new Promise<number>((resolve) => {
jvm.runClass('util.Javac', paths, resolve);
})
).finally(cancel);
if (code !== 0) {
throw new Error('Compilation failed');
}
this.lastSourcesKey = key;
}

protected writeFile(file: File): string {
Expand All @@ -31,4 +43,12 @@ export class JavaCompiler {
this.fs.writeFileSync(path, file.content);
return path;
}

private hasClasses(): boolean {
try {
return this.fs.readdirSync('/home').some((name) => name.endsWith('.class'));
} catch {
return false;
}
}
}
4 changes: 2 additions & 2 deletions patches/doppiojvm@0.5.0.patch

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading