-
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 #10 from x0k/rust
Rust
- Loading branch information
Showing
73 changed files
with
1,107 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use flake |
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,3 @@ | ||
*.wasm filter=lfs diff=lfs merge=lfs -text | ||
*.rlib filter=lfs diff=lfs merge=lfs -text | ||
*.so filter=lfs diff=lfs merge=lfs -text |
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,4 @@ | ||
[submodule "packages/testing-rust/rust"] | ||
path = packages/testing-rust/rust | ||
url = https://github.com/bjorn3/rust | ||
branch = compile_rustc_for_wasm15 |
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 |
---|---|---|
@@ -1 +1,21 @@ | ||
# Programming Patterns Practice | ||
|
||
## Development | ||
|
||
Clone the repository: | ||
|
||
``` | ||
git clone https://github.com/x0k/ppp.git | ||
git submodule update --init | ||
```` | ||
The artifacts saved in the repository must be created in the nix development environment. | ||
```console | ||
nix develop | ||
mk a/build | ||
``` | ||
|
||
## See also | ||
|
||
- Simple build automation tool [mk](https://github.com/x0k/mk) |
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
38 changes: 38 additions & 0 deletions
38
apps/ppp/src/adapters/workers/rust-worker-description.svelte
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,38 @@ | ||
<script lang="ts"> | ||
import { version } from "testing-rust/version"; | ||
</script> | ||
|
||
<p> | ||
Rust {version} | ||
</p> | ||
|
||
<p> | ||
Your code is interpreted by <a | ||
href="https://github.com/rust-lang/miri" | ||
class="link" | ||
target="_blank">Miri</a | ||
> (compiled to WebAssembly) in a web worker environment. | ||
</p> | ||
|
||
<p>Anything not supported by Miri or WASM is unsupported.</p> | ||
|
||
<p> | ||
This solution is based on <a | ||
class="link" | ||
href="https://github.com/bjorn3" | ||
target="_blank">bjorn3</a | ||
> | ||
work on | ||
<a | ||
class="link" | ||
target="_blank" | ||
href="https://github.com/rust-lang/miri/issues/722#issuecomment-1960849880" | ||
>porting Rust to WebAssembly</a | ||
> | ||
and | ||
<a | ||
href="https://github.com/bjorn3/browser_wasi_shim" | ||
class="link" | ||
target="_blank">browser WASI shim</a | ||
>. | ||
</p> |
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,14 @@ | ||
enum PaymentSystemType { | ||
PayPal, | ||
WebMoney, | ||
CatBank, | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
fn payment(tp: PaymentSystemType, base: isize, amount: isize) -> isize { | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
apps/ppp/src/content/design-patterns/factory/rust/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,31 @@ | ||
// Only type imports are allowed | ||
import type { UniversalFactory } from "testing/actor"; | ||
|
||
import type { RustUniversalFactoryData } from "@/lib/workers/rust"; | ||
|
||
import type { Input, Output } from "../tests-data"; | ||
import type { PaymentSystemType } from "../reference"; | ||
|
||
export const factory: UniversalFactory< | ||
Input, | ||
Output, | ||
RustUniversalFactoryData<Input, Output> | ||
> = ({ makeTestRunnerFactory }) => { | ||
const RUST_PAYMENT_SYSTEM_TYPES: Record<PaymentSystemType, string> = { | ||
paypal: "PaymentSystemType::PayPal", | ||
webmoney: "PaymentSystemType::WebMoney", | ||
"cat-bank": "PaymentSystemType::CatBank", | ||
}; | ||
return makeTestRunnerFactory( | ||
({ paymentSystem, amount, base }) => | ||
`let str = payment(${RUST_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}).to_string(); | ||
let output_content = str.as_bytes();`, | ||
(result) => { | ||
const r = parseInt(result, 10); | ||
if (isNaN(r)) { | ||
throw new Error(`Invalid result type: ${result}, expected number`); | ||
} | ||
return r; | ||
} | ||
); | ||
}; |
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 rustCode } from "./code.rs?raw"; | ||
export { factory as rustFactory } 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
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,77 @@ | ||
import type { TestRunnerFactory } from "testing"; | ||
import { startTestRunnerActor } from "testing/actor"; | ||
import { RustTestRunner, wasiRuntimeFactory } from "testing-rust"; | ||
|
||
// @ts-expect-error .wasm is an asset | ||
import miriWasmUrl from "testing-rust/miri.wasm"; | ||
import { COLOR } from "libs/logger"; | ||
import type { Context } from "libs/context"; | ||
|
||
const libsUrls = import.meta.glob("/node_modules/testing-rust/dist/lib/*", { | ||
eager: true, | ||
import: "default", | ||
}) as Record<string, string>; | ||
|
||
export interface RustUniversalFactoryData<I, O> { | ||
RustTestRunner: typeof RustTestRunner; | ||
wasiRuntimeFactory: typeof wasiRuntimeFactory; | ||
makeTestRunnerFactory: ( | ||
generateOutputContentCode: (input: I) => string, | ||
transformResult: (result: string) => O | ||
) => TestRunnerFactory<I, O>; | ||
} | ||
|
||
// TODO: manual cache for large assets | ||
function loadLibs(ctx: Context) { | ||
return Promise.all( | ||
Object.entries(libsUrls).map(async ([lib, url]) => { | ||
const response = await fetch(url, { | ||
signal: ctx.signal, | ||
cache: "force-cache", | ||
}); | ||
const buffer = await response.arrayBuffer(); | ||
return [lib.slice(36), buffer] as [string, ArrayBuffer]; | ||
}) | ||
); | ||
} | ||
|
||
startTestRunnerActor< | ||
unknown, | ||
unknown, | ||
RustUniversalFactoryData<unknown, unknown> | ||
>((universalFactory) => | ||
universalFactory({ | ||
RustTestRunner, | ||
wasiRuntimeFactory, | ||
makeTestRunnerFactory: (generateOutputContentCode, transformResult) => { | ||
class TestRunner extends RustTestRunner<unknown, unknown> { | ||
protected override generateOutputContentCode(input: unknown): string { | ||
return generateOutputContentCode(input); | ||
} | ||
protected override transformResult(data: string): unknown { | ||
return transformResult(data); | ||
} | ||
} | ||
return async (ctx, { code, out }) => | ||
new TestRunner( | ||
code, | ||
wasiRuntimeFactory( | ||
out, | ||
{ | ||
write(text) { | ||
out.write(`${COLOR.ERROR}${text}${COLOR.RESET}`); | ||
}, | ||
writeln(text) { | ||
out.writeln(`${COLOR.ERROR}${text}${COLOR.RESET}`); | ||
}, | ||
}, | ||
await loadLibs(ctx) | ||
), | ||
await WebAssembly.compileStreaming( | ||
fetch(miriWasmUrl, { signal: ctx.signal, cache: "force-cache" }) | ||
), | ||
"case_output" | ||
); | ||
}, | ||
}) | ||
); |
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.