Skip to content
Open
Changes from 2 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
90 changes: 90 additions & 0 deletions Documentation/CSharpWasm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# CSharpWasm Documentation

## Purpose

CSharpWasm project enables users to execute C# code directly within their browser for SK Online (SKO).
The system relies on .NET 8 WebAssembly and Roslyn to execute user-submitted C# code while providing SplashKit functions through JavaScript interop.

The documentation provides essential information to help contributors understand the system architecture and build process and usage of the system.



## Project Layout

The following shows the structure of the `CSharpWasm/`

```
CSharpWasm/
├─ Properties/
│ ├─ AssemblyInfo.cs
│ └─ launchSettings.json
├─ .gitignore
├─ CSharpCodeRunner.cs
├─ CSharpWasm.csproj
├─ Program.cs
├─ SplashKitBindings.Generated.cs
├─ buildAndCopy.sh
└─ runtimeconfig.template.json
```

| File / Folder | Role | Responsibilities |
| -------------------------------- | ----------------- | ------------------------------------------------------------------------------------------ |
| `CSharpCodeRunner.cs` | Compiler & Runner |The `CompileAndRun` function performs main operations which include loading reference assemblies followed by Roslyn-based C# compilation and then running the `Main()` function when it exists.|
| `SplashKitBindings.Generated.cs` | C# ↔ JS Interop | The generated file contains `JSImport` bindings which expose SplashKitBackendWASM functions (graphics, audio, input).|
| `Program.cs` | Wasm Bootstrap |The code provides a basic starting point for browser execution.|
| `buildAndCopy.sh` | Build Script | Shell script that builds the project and copies DLLs and `_framework` files into `../CSharpWasmExpo/`.|
| `CSharpWasm.csproj` | Project File | Targets .NET 8 WebAssembly, includes `Microsoft.CodeAnalysis` (Roslyn).|
| `Properties/AssemblyInfo.cs` | Assembly Metadata | Marks supported platform as “browser”.|
| `Properties/launchSettings.json` | Launch Config | Settings for running/debugging locally in browser.|
| `runtimeconfig.template.json` | Runtime Template | Template for browser runtime behavior.|

## Prerequisites
Before starting the build process you need to verify that you have:
- The .NET 8 SDK needs to be installed on your system.
- You need to install the WebAssembly tools workload using the command dotnet workload install wasm-tools.
- Your project folder CSharpWasmExpo requires either a local development environment or a hosting solution to function.

## Build and Copy Process
You can create and distribute the artifacts through the following command which needs to be executed from the `CSharpWasm/` directory.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a note here that tells Windows users to use this script with WSL or MINGW to run the script

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thx


```bash
chmod +x buildAndCopy.sh # first time only
./buildAndCopy.sh
```

This script is responsible for
- Building project with `dotnet build`
- Required DLLs will be copied into the `../CSharpWasmExpo/bin/`
- WebAssembly runtime files need to be added to the Expo host at a path which depends on your specific setup.

## Usage in the Browser

```C#
Example of JavaScript call:

const code =
using System;
class Program {
static void Main() {
Console.WriteLine("Hello from C#!");
}
};

const result = await globalThis.CSharpCodeRunner.CompileAndRun(code);
console.log(result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this code would actually work with the current implementation.

You should just be able to execute raw C# code and it should work, no Javascript required

```

### Output Expected

```
Hello from C#!
```

- The program displays its output in the console after compilation completes successfully.
- The compiler returns a list of compilation errors when syntax or build problems occur.
- A program requires a valid entry point which should be static void Main().

## Troubleshooting
- No entry point: This means program lacks an entry point which should be defined through `static void Main()`.
- Error loading assembly: Then build script requires DLLs to exist in the `../CSharpWasmExpo/bin/` directory for successful execution.
- Check your submitted code lines against the error messages because they show exact locations of the problems.
Loading