-
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.
- Loading branch information
Showing
97 changed files
with
413 additions
and
379 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
50 changes: 39 additions & 11 deletions
50
apps/ppp/src/problems/design-patterns/(factory)/payment-systems/csharp/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 |
---|---|---|
@@ -1,18 +1,46 @@ | ||
import { makeRemoteTestCompilerFactory } from "testing/actor"; | ||
|
||
import Worker from "@/adapters/runtime/dotnet/test-worker?worker"; | ||
|
||
// Only type imports are allowed | ||
|
||
import type { TestCompilerFactory } from "testing"; | ||
import { makeExecutionCode } from "dotnet-runtime"; | ||
|
||
import { DotnetTestCompilerFactory } from "@/adapters/runtime/dotnet/test-compiler-factory"; | ||
import type { DotnetTestWorkerConfig } from "@/adapters/runtime/dotnet/test-worker"; | ||
|
||
import type { Input, Output } from "../tests-data"; | ||
|
||
import definitions from "./definitions.cs?raw"; | ||
import executionCode from "./execution-code.cs?raw"; | ||
export const factory: TestCompilerFactory<Input, Output> = | ||
makeRemoteTestCompilerFactory( | ||
Worker, | ||
( | ||
ctx, | ||
{ dotnetTestCompilerFactory, makeExecutionCode }: DotnetTestWorkerConfig | ||
) => { | ||
const definitions = `struct Args { | ||
[JsonPropertyName("base")] | ||
public int Base { get; set; } | ||
[JsonPropertyName("amount")] | ||
public int Amount { get; set; } | ||
export const factory: TestCompilerFactory<Input, Output> = async (ctx, out) => { | ||
return new DotnetTestCompilerFactory(out).create(ctx, { | ||
executionCode: makeExecutionCode({ | ||
additionalDefinitions: definitions, | ||
executionCode: executionCode, | ||
}), | ||
}); | ||
[JsonPropertyName("paymentSystem")] | ||
public string SystemType { get; set; } | ||
}`; | ||
const executionCode = `var args = JsonSerializer.Deserialize<Args>(jsonArguments); | ||
var type = args.SystemType switch { | ||
"paypal" => SystemType.PayPal, | ||
"webmoney" => SystemType.WebMoney, | ||
"catbank" => SystemType.CatBank, | ||
_ => throw new System.Exception("Unknown payment type") | ||
}; | ||
var result = Solution.Payment(type, args.Base, args.Amount); | ||
`; | ||
return dotnetTestCompilerFactory.create(ctx, { | ||
executionCode: makeExecutionCode({ | ||
additionalDefinitions: definitions, | ||
executionCode, | ||
}), | ||
}); | ||
} | ||
); |
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 |
---|---|---|
@@ -1,256 +1,5 @@ | ||
import { dotnet } from "./compiler/dotnet.js"; | ||
|
||
const { setModuleImports, getAssemblyExports, getConfig } = | ||
await dotnet.create(); | ||
|
||
setModuleImports("main.js", { | ||
logger: { | ||
debug: (msg) => console.log(msg), | ||
info: (msg) => console.info(msg), | ||
warn: (msg) => console.warn(msg), | ||
error: (msg) => console.error(msg), | ||
}, | ||
const worker = new Worker(new URL("./worker.js", import.meta.url), { | ||
type: "module" | ||
}); | ||
|
||
const config = getConfig(); | ||
const exports = await getAssemblyExports(config.mainAssemblyName); | ||
|
||
const libs = [ | ||
"Humanizer.dll", | ||
"Microsoft.Bcl.AsyncInterfaces.dll", | ||
"Microsoft.CSharp.dll", | ||
// "Microsoft.CodeAnalysis.CSharp.Workspaces.dll", | ||
// "Microsoft.CodeAnalysis.CSharp.dll", | ||
// "Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll", | ||
// "Microsoft.CodeAnalysis.VisualBasic.dll", | ||
// "Microsoft.CodeAnalysis.Workspaces.dll", | ||
// "Microsoft.CodeAnalysis.dll", | ||
// "Microsoft.JSInterop.WebAssembly.dll", | ||
// "Microsoft.JSInterop.dll", | ||
// "Microsoft.VisualBasic.Core.dll", | ||
// "Microsoft.VisualBasic.dll", | ||
// "Microsoft.Win32.Primitives.dll", | ||
// "Microsoft.Win32.Registry.dll", | ||
"System.AppContext.dll", | ||
"System.Buffers.dll", | ||
"System.Collections.Concurrent.dll", | ||
"System.Collections.Immutable.dll", | ||
"System.Collections.NonGeneric.dll", | ||
"System.Collections.Specialized.dll", | ||
"System.Collections.dll", | ||
// "System.ComponentModel.Annotations.dll", | ||
// "System.ComponentModel.DataAnnotations.dll", | ||
// "System.ComponentModel.EventBasedAsync.dll", | ||
// "System.ComponentModel.Primitives.dll", | ||
// "System.ComponentModel.TypeConverter.dll", | ||
// "System.ComponentModel.dll", | ||
// "System.Composition.AttributedModel.dll", | ||
// "System.Composition.Convention.dll", | ||
// "System.Composition.Hosting.dll", | ||
// "System.Composition.Runtime.dll", | ||
// "System.Composition.TypedParts.dll", | ||
// "System.Configuration.dll", | ||
"System.Console.dll", | ||
"System.Core.dll", | ||
// "System.Data.Common.dll", | ||
// "System.Data.DataSetExtensions.dll", | ||
// "System.Data.dll", | ||
// "System.Diagnostics.Contracts.dll", | ||
"System.Diagnostics.Debug.dll", | ||
// "System.Diagnostics.DiagnosticSource.dll", | ||
// "System.Diagnostics.FileVersionInfo.dll", | ||
// "System.Diagnostics.Process.dll", | ||
// "System.Diagnostics.StackTrace.dll", | ||
// "System.Diagnostics.TextWriterTraceListener.dll", | ||
// "System.Diagnostics.Tools.dll", | ||
// "System.Diagnostics.TraceSource.dll", | ||
// "System.Diagnostics.Tracing.dll", | ||
// "System.Drawing.Primitives.dll", | ||
// "System.Drawing.dll", | ||
// "System.Dynamic.Runtime.dll", | ||
// "System.Formats.Asn1.dll", | ||
// "System.Formats.Tar.dll", | ||
"System.Globalization.Calendars.dll", | ||
"System.Globalization.Extensions.dll", | ||
"System.Globalization.dll", | ||
// "System.IO.Compression.Brotli.dll", | ||
// "System.IO.Compression.FileSystem.dll", | ||
// "System.IO.Compression.ZipFile.dll", | ||
// "System.IO.Compression.dll", | ||
// "System.IO.FileSystem.AccessControl.dll", | ||
// "System.IO.FileSystem.DriveInfo.dll", | ||
// "System.IO.FileSystem.Primitives.dll", | ||
// "System.IO.FileSystem.Watcher.dll", | ||
"System.IO.FileSystem.dll", | ||
// "System.IO.IsolatedStorage.dll", | ||
// "System.IO.MemoryMappedFiles.dll", | ||
// "System.IO.Pipelines.dll", | ||
// "System.IO.Pipes.AccessControl.dll", | ||
// "System.IO.Pipes.dll", | ||
"System.IO.UnmanagedMemoryStream.dll", | ||
"System.IO.dll", | ||
"System.Linq.Expressions.dll", | ||
"System.Linq.Parallel.dll", | ||
"System.Linq.Queryable.dll", | ||
"System.Linq.dll", | ||
"System.Memory.dll", | ||
"System.Net.Http.Json.dll", | ||
"System.Net.Http.dll", | ||
"System.Net.HttpListener.dll", | ||
// "System.Net.Mail.dll", | ||
"System.Net.NameResolution.dll", | ||
"System.Net.NetworkInformation.dll", | ||
// "System.Net.Ping.dll", | ||
"System.Net.Primitives.dll", | ||
// "System.Net.Quic.dll", | ||
"System.Net.Requests.dll", | ||
// "System.Net.Security.dll", | ||
// "System.Net.ServicePoint.dll", | ||
"System.Net.Sockets.dll", | ||
"System.Net.WebClient.dll", | ||
"System.Net.WebHeaderCollection.dll", | ||
// "System.Net.WebProxy.dll", | ||
// "System.Net.WebSockets.Client.dll", | ||
// "System.Net.WebSockets.dll", | ||
"System.Net.dll", | ||
"System.Numerics.Vectors.dll", | ||
"System.Numerics.dll", | ||
"System.ObjectModel.dll", | ||
"System.Private.CoreLib.dll", | ||
// "System.Private.DataContractSerialization.dll", | ||
"System.Private.Uri.dll", | ||
// "System.Private.Xml.Linq.dll", | ||
// "System.Private.Xml.dll", | ||
// "System.Reflection.DispatchProxy.dll", | ||
// "System.Reflection.Emit.ILGeneration.dll", | ||
// "System.Reflection.Emit.Lightweight.dll", | ||
// "System.Reflection.Emit.dll", | ||
// "System.Reflection.Extensions.dll", | ||
// "System.Reflection.Metadata.dll", | ||
// "System.Reflection.Primitives.dll", | ||
// "System.Reflection.TypeExtensions.dll", | ||
"System.Reflection.dll", | ||
"System.Resources.Reader.dll", | ||
"System.Resources.ResourceManager.dll", | ||
"System.Resources.Writer.dll", | ||
// "System.Runtime.CompilerServices.Unsafe.dll", | ||
// "System.Runtime.CompilerServices.VisualC.dll", | ||
"System.Runtime.Extensions.dll", | ||
"System.Runtime.Handles.dll", | ||
// "System.Runtime.InteropServices.JavaScript.dll", | ||
"System.Runtime.InteropServices.RuntimeInformation.dll", | ||
"System.Runtime.InteropServices.dll", | ||
// "System.Runtime.Intrinsics.dll", | ||
// "System.Runtime.Loader.dll", | ||
// "System.Runtime.Numerics.dll", | ||
// "System.Runtime.Serialization.Formatters.dll", | ||
// "System.Runtime.Serialization.Json.dll", | ||
// "System.Runtime.Serialization.Primitives.dll", | ||
// "System.Runtime.Serialization.Xml.dll", | ||
// "System.Runtime.Serialization.dll", | ||
"System.Runtime.dll", | ||
// "System.Security.AccessControl.dll", | ||
// "System.Security.Claims.dll", | ||
// "System.Security.Cryptography.Algorithms.dll", | ||
// "System.Security.Cryptography.Cng.dll", | ||
// "System.Security.Cryptography.Csp.dll", | ||
// "System.Security.Cryptography.Encoding.dll", | ||
// "System.Security.Cryptography.OpenSsl.dll", | ||
// "System.Security.Cryptography.Primitives.dll", | ||
// "System.Security.Cryptography.X509Certificates.dll", | ||
// "System.Security.Cryptography.dll", | ||
// "System.Security.Principal.Windows.dll", | ||
"System.Security.Principal.dll", | ||
"System.Security.SecureString.dll", | ||
"System.Security.dll", | ||
// "System.ServiceModel.Web.dll", | ||
// "System.ServiceProcess.dll", | ||
// "System.Text.Encoding.CodePages.dll", | ||
"System.Text.Encoding.Extensions.dll", | ||
"System.Text.Encoding.dll", | ||
"System.Text.Encodings.Web.dll", | ||
"System.Text.Json.dll", | ||
"System.Text.RegularExpressions.dll", | ||
// "System.Threading.Channels.dll", | ||
// "System.Threading.Overlapped.dll", | ||
// "System.Threading.Tasks.Dataflow.dll", | ||
"System.Threading.Tasks.Extensions.dll", | ||
"System.Threading.Tasks.Parallel.dll", | ||
"System.Threading.Tasks.dll", | ||
"System.Threading.Thread.dll", | ||
"System.Threading.ThreadPool.dll", | ||
"System.Threading.Timer.dll", | ||
"System.Threading.dll", | ||
// "System.Transactions.Local.dll", | ||
"System.Transactions.dll", | ||
"System.ValueTuple.dll", | ||
"System.Web.HttpUtility.dll", | ||
// "System.Web.dll", | ||
// "System.Windows.dll", | ||
// "System.Xml.Linq.dll", | ||
// "System.Xml.ReaderWriter.dll", | ||
// "System.Xml.Serialization.dll", | ||
// "System.Xml.XDocument.dll", | ||
// "System.Xml.XPath.XDocument.dll", | ||
// "System.Xml.XPath.dll", | ||
// "System.Xml.XmlDocument.dll", | ||
// "System.Xml.XmlSerializer.dll", | ||
// "System.Xml.dll", | ||
"System.dll", | ||
"WebAssembly.dll", | ||
// "WindowsBase.dll", | ||
"compiler.dll", | ||
"mscorlib.dll", | ||
"netstandard.dll", | ||
]; | ||
|
||
let status = await exports.Compiler.Init(`${location.origin}/lib`, libs); | ||
if (status !== 0) { | ||
throw new Error("Init failed"); | ||
} | ||
|
||
status = await exports.Compiler.Compile([ | ||
`using System; | ||
namespace ppp | ||
{ | ||
public class Program | ||
{ | ||
public static int Payment(int baseValue, int amount) | ||
{ | ||
Console.WriteLine("Called payment"); | ||
return baseValue + amount; | ||
} | ||
} | ||
} | ||
`, | ||
`using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Diagnostics.CodeAnalysis; | ||
namespace test | ||
{ | ||
public class Program { | ||
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")] | ||
public static string Test(string jsonArguments) { | ||
var args = JsonSerializer.Deserialize<List<int>>(jsonArguments); | ||
var result = ppp.Program.Payment(args[0], args[1]); | ||
return JsonSerializer.Serialize(result); | ||
} | ||
} | ||
} | ||
`, | ||
]); | ||
if (status !== 0) { | ||
throw new Error(`Compilation failed: ${status}`); | ||
} | ||
|
||
status = await exports.Compiler.Run("test.Program", "Test", [JSON.stringify([1, 2])]); | ||
if (status !== 0) { | ||
throw new Error("Run failed"); | ||
} | ||
|
||
const result = await exports.Compiler.GetResultAsString(); | ||
|
||
console.log(result); | ||
|
||
await exports.Compiler.DisposeAssembly(); | ||
worker.postMessage("start") |
Oops, something went wrong.