Playground in the browser
The Playground sample compiles and runs Typhon code in the browser. The Typhon compiler, Roslyn and the program run in a Blazor WebAssembly app, without a server.
samples/Playground is a Blazor WebAssembly app that compiles and runs Typhon in the browser. The page has an editor with syntax highlighting, example programs, and the type mode and big-int options of the typhon command. Run shows what the program printed, its diagnostics, a Python-style traceback and the C# that Typhon generated. A line under the output gives the compile time, split into Typhon → C# and C# → IL, and the run time of the program alone. Nothing runs on a server: the Typhon compiler, Roslyn and the program run in the browser’s .NET runtime.
This website hosts the playground at /playground/, built from the same Typhon commit as the documentation. Its pages are served in a sandbox with an origin of their own, so a program cannot reach the storage of the rest of the site.
Running the sample
dotnet run --project samples/Playground
dotnet publish samples/Playground -c Release -o out/playground
dotnet run starts a development server at http://localhost:5180. dotnet publish writes static files to out/playground/wwwroot, which any web server can host. The project builds without a .NET workload.
How it works
The page compiles a program the way typhon run compiles a script. Typhon turns main.ty into C#, and Roslyn compiles the C# against the precompiled Typhon runtime. A browser has no assembly files that Roslyn could reference, so the build ships the reference assemblies of the .NET targeting pack for the framework assemblies that Typhon programs use, and the page downloads them once.
Each run loads the program together with a fresh copy of the Typhon runtime into its own load context, so nothing carries over from a previous run. Standard output, standard error and input() are redirected to the page, and a traceback maps the program’s frames to the lines of main.ty. sys.exit ends only the program.
Limits
- A program is one file,
main.ty. It cannot import other.tymodules or Typhon.NumPy. - The browser is a sandbox. Programs have no network access, and
open()works on an in-memory file system that lasts until the page is reloaded. input()reads the text in the stdin box when Run is pressed.- Programs run on the page’s only thread. An endless loop freezes the tab, and the output appears when the program ends.
Size and speed
The published app consists of 115 files with about 30 MB, or 8.7 MB with Brotli compression. Roslyn and the .NET runtime make up most of it. In a test in headless Chromium, the page had loaded and run its first example after about 5.5 seconds. The next program took about 4 seconds to compile and run, and later programs took less than a second. The sample’s README lists further measurements.
Programs run slower in the playground than with the typhon command. The .NET runtime in the browser has no JIT compiler: it interprets the IL and compiles only frequently run code paths to WebAssembly. Ahead-of-time compilation to WebAssembly happens when an app is built, so it cannot apply to programs that the page compiles. On one machine, the N-body benchmark with 1,000,000 steps took 3.5 to 6 seconds in the playground, 0.6 to 1.3 seconds with typhon run and 16 seconds under CPython 3.14. The playground therefore shows what Typhon compiles and prints, and the benchmark page shows how fast the compiled code runs.