-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from x0k/java
Java
- Loading branch information
Showing
56 changed files
with
55,590 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
apps/ppp/src/adapters/runtime/java/test-compiler-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]() {}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
apps/ppp/src/content/design-patterns/factory/java/code.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
apps/ppp/src/content/design-patterns/factory/java/factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}), | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.