Skip to content

Commit

Permalink
Merge pull request #14 from x0k/java
Browse files Browse the repository at this point in the history
Java
  • Loading branch information
x0k authored Aug 9, 2024
2 parents df2b5ef + 982febe commit c3fc241
Show file tree
Hide file tree
Showing 56 changed files with 55,590 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.so filter=lfs diff=lfs merge=lfs -text
*.dat filter=lfs diff=lfs merge=lfs -text
*.dll filter=lfs diff=lfs merge=lfs -text
*.jar filter=lfs diff=lfs merge=lfs -text
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
[submodule "packages/gleam-runtime/gleam"]
path = packages/gleam-runtime/gleam
url = https://github.com/gleam-lang/gleam.git
[submodule "packages/java-runtime/doppio"]
path = packages/java-runtime/doppio
url = https://github.com/plasma-umass/doppio.git
3 changes: 2 additions & 1 deletion apps/ppp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"php-runtime": "workspace:*",
"python-runtime": "workspace:*",
"rust-runtime": "workspace:*",
"typescript-runtime": "workspace:*"
"typescript-runtime": "workspace:*",
"java-runtime": "workspace:*"
},
"devDependencies": {
"@iconify-json/lucide": "^1.1.202",
Expand Down
1 change: 1 addition & 0 deletions apps/ppp/src/adapters/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const MONACO_LANGUAGE_ID: Record<Language, string> = {
[Language.Rust]: "rust",
[Language.Gleam]: Language.Gleam,
[Language.CSharp]: "csharp",
[Language.Java]: "java",
};

const LANGUAGE_ID_SCOPE_NAME = {
Expand Down
2 changes: 2 additions & 0 deletions apps/ppp/src/adapters/runtime/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import GoDescription from "./go/description.svelte";
import RustDescription from "./rust/description.svelte";
import GleamDescription from "./gleam/description.svelte";
import DotnetDescription from "./dotnet/description.svelte";
import JavaDescription from "./java/description.svelte";

export const DESCRIPTIONS: Record<Language, Component> = {
[Language.JavaScript]: JsDescription,
Expand All @@ -20,4 +21,5 @@ export const DESCRIPTIONS: Record<Language, Component> = {
[Language.Rust]: RustDescription,
[Language.Gleam]: GleamDescription,
[Language.CSharp]: DotnetDescription,
[Language.Java]: JavaDescription,
};
18 changes: 18 additions & 0 deletions apps/ppp/src/adapters/runtime/java/description.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import { version } from "java-runtime/version";
</script>

<p>
{version}
</p>

<p>
Your code is compiled by <code>Javac</code> and executed in
<a target="_blank" class="link" href="https://github.com/plasma-umass/doppio/"
>DoppioJVM</a
> in a web worker environment.
</p>

<p>
Public class <code>Test</code> is reserved.
</p>
83 changes: 83 additions & 0 deletions apps/ppp/src/adapters/runtime/java/test-compiler-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { Context } from "libs/context";
import type { Writer } from "libs/io";
import type { TestCompiler } from "testing";

import {
JavaCompiler,
JavaTestProgram,
initFs,
makeJVMFactory,
} from "java-runtime";

// @ts-expect-error vite url import
import libZipUrl from "java-runtime/doppio.zip";

export interface Options<I, O> {
className?: string;
classDefinitions: string;
mainMethodBody: string;
nativesFactory: (
input: I,
saveOutput: (output: O) => void
) => Record<string, Function>;
}

export class JavaTestCompilerFactory {
constructor(private readonly writer: Writer) {}
async create<I, O>(
ctx: Context,
{
className = "Test",
classDefinitions,
mainMethodBody,
nativesFactory,
}: Options<I, O>
): Promise<TestCompiler<I, O>> {
const jvmFactory = makeJVMFactory(this.writer);
const libZipData = await fetch(libZipUrl, {
signal: ctx.signal,
cache: "force-cache",
}).then((response) => response.arrayBuffer());
const fs = await initFs(libZipData);
const compiler = new JavaCompiler(
jvmFactory,
`/home/${className}.java`,
fs
);
class TestProgram extends JavaTestProgram<I, O> {
private output?: O;
private saveOutput(output: O) {
this.output = output;
}
protected override getNatives(input: I): Record<string, Function> {
return nativesFactory(input, this.saveOutput.bind(this));
}
protected override getResult(): O {
if (this.output === undefined) {
throw new Error("No output");
}
return this.output;
}
}
return {
async compile(ctx, files) {
if (files.length !== 1) {
throw new Error("Compilation of multiple files is not implemented");
}
await compiler.compile(
ctx,
`${files[0].content}
public class ${className} {
${classDefinitions}
public static void main(String[] args) {
${mainMethodBody}
}
}`
);
return new TestProgram(className, jvmFactory);
},
[Symbol.dispose]() {},
};
}
}
16 changes: 16 additions & 0 deletions apps/ppp/src/adapters/runtime/java/test-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { util } from 'java-runtime'
import { startTestCompilerActor } from "testing/actor";

import { JavaTestCompilerFactory } from "./test-compiler-factory";

export interface JavaTestWorkerConfig {
javaTestCompilerFactory: JavaTestCompilerFactory;
util: typeof util;
}

startTestCompilerActor<JavaTestWorkerConfig>((ctx, out, factory) =>
factory(ctx, {
javaTestCompilerFactory: new JavaTestCompilerFactory(out),
util,
})
);
1 change: 1 addition & 0 deletions apps/ppp/src/components/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export const LANG_ICONS: Record<Language, string> = {
[Language.Rust]: "vscode-icons:file-type-rust",
[Language.Gleam]: "vscode-icons:file-type-gleam",
[Language.CSharp]: "vscode-icons:file-type-csharp",
[Language.Java]: "vscode-icons:file-type-java",
};
1 change: 1 addition & 0 deletions apps/ppp/src/components/editor/panel/panel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
runner[Symbol.dispose]();
}
} catch (err) {
console.error(err);
logger.error(stringifyError(err));
} finally {
isRunning = false;
Expand Down
5 changes: 5 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/editor.astro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { contentId } = Astro.props;
import { rustCode, rustFactory } from "./rust";
import { gleamCode, gleamFactory } from "./gleam";
import { csCode, csFactory } from "./csharp";
import { javaCode, javaFactory } from './java'

mountEditor(testCases, {
[Language.JavaScript]: {
Expand Down Expand Up @@ -55,5 +56,9 @@ const { contentId } = Astro.props;
initialValue: csCode,
factory: csFactory,
},
[Language.Java]: {
initialValue: javaCode,
factory: javaFactory
}
});
</script>
11 changes: 11 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/java/code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum SystemType {
PAY_PAL,
WEB_MONEY,
CAT_BANK
}

class Payment {
public static int execute(SystemType type, int base, int amount) {
throw new RuntimeException("Not implemented");
}
}
46 changes: 46 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/java/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { makeRemoteTestCompilerFactory } from "testing/actor";

import Worker from "@/adapters/runtime/java/test-worker?worker";

// Only type imports are allowed

import type { TestCompilerFactory } from "testing";

import type { JavaTestWorkerConfig } from "@/adapters/runtime/java/test-worker";

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

export const factory: TestCompilerFactory<Input, Output> =
makeRemoteTestCompilerFactory(
Worker,
(ctx, { javaTestCompilerFactory, util }: JavaTestWorkerConfig) => {
const JAVA_PAYMENT_SYSTEM_TYPES: Record<PaymentSystemType, string> = {
paypal: "PAY_PAL",
webmoney: "WEB_MONEY",
"cat-bank": "CAT_BANK",
};
return javaTestCompilerFactory.create(ctx, {
classDefinitions: `static native String getSystemType();
static native int getBase();
static native int getAmount();
static native void saveResult(int result);`,
mainMethodBody: `saveResult(Payment.execute(
SystemType.valueOf(getSystemType()),
getBase(),
getAmount()
));`,
nativesFactory: (input, save) => ({
// @ts-expect-error TODO: import thread type
"getSystemType()Ljava/lang/String;": (t) =>
util.initString(
t.getBsCl(),
JAVA_PAYMENT_SYSTEM_TYPES[input.paymentSystem]
),
"getBase()I": () => input.base,
"getAmount()I": () => input.amount,
"saveResult(I)V": (_: unknown, result: number) => save(result),
}),
});
}
);
2 changes: 2 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/java/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as javaCode } from "./code.java?raw";
export { factory as javaFactory } from "./factory"
2 changes: 2 additions & 0 deletions apps/ppp/src/shared/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum Language {
Rust = "rust",
Gleam = "gleam",
CSharp = "csharp",
Java = "java",
}

export const LANGUAGE_TITLE: Record<Language, string> = {
Expand All @@ -18,4 +19,5 @@ export const LANGUAGE_TITLE: Record<Language, string> = {
[Language.Rust]: "Rust",
[Language.Gleam]: "Gleam",
[Language.CSharp]: "CSharp",
[Language.Java]: "Java",
};
25 changes: 21 additions & 4 deletions flake.lock

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

Loading

0 comments on commit c3fc241

Please sign in to comment.