Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build Swift Web Playground

 Build Swift Web Playground

Playground (a.k.a REPL) is one of the most useful tools for understanding a programming language. Swift provides a very easy to use playground that makes it very easy for us to try out Swift code and see the results.

If you look at other programming languages, most of the modern languages have some kind of playground mechanism. In addition, there are many languages that provide a web playground.

The web playground gives us many more power. It can be used to try out an idea on your smartphone, to share problematic code and execution results with others for discussion, or to provide "working documentation" where you can run sample code for documentation.

Unfortunately, the official Swift web playground is not yet available, but you can use server-side Swift and web technologies to create your own web-powered Swift Playground!

Here is Swift web playground I created and host => https://swiftfiddle.com/ .

I usually work in iOS and macOS programming, so the experience with server-side Swift and web programming are very interesting to me. I'd like to see more community-based Swift execution environments like this one, so I'd like to share the insights I've gained from building this playground, such as how to sandboxing execution, limit resources and switch Swift versions etc.

Kishikawa Katsumi

April 16, 2021
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. Build Swift Web Playground Agenda • What is Online Playground

    • Build Your Own Swift Web Playground • Frontend & Backend • Sandboxing • Support Multiple Swift Versions • Support External Libraries • Share & Embed • API • Deploy & Update • Wrap up
  2. • Write code on the browser • Send the code

    to a server • Run the code • Return the result • Show the result Build Your Own Swift Web Playground Overview
  3. • Write code on the browser • Run the code

    • Show the result Build Your Own Swift Web Playground Overview Compile Run
  4. Build Your Own Swift Web Playground Web Application Server •

    Vapor https://vapor.codes/ • Leaf https://github.com/vapor/leaf
  5. • Syntax highlighting • Line numbers • Smart indentation •

    Auto complete • Show compiler errors & warnings Build Your Own Swift Web Playground The Code Editor
  6. Build Your Own Swift Web Playground The Code Editor •

    Ace https://ace.c9.io/ • CodeMirror https://codemirror.net/ • Monaco Editor https://microsoft.github.io/monaco-editor/
  7. Build Your Own Swift Web Playground Sandboxing • The sandbox

    isolates the environment in which the code is run. • The sandbox to run untrusted code and return the output. • The sandbox restricts the resources that the untrusted code can access to protect the host. • The sandbox safely stops non-terminating programs.
  8. Build Your Own Swift Web Playground Sandboxing Container Compile Run

    Container Compile Run Container Compile Run
  9. API

  10. Discord Bot • Compile and execute code snippets posted on

    Discord and reply with results. • Do not need to run the posted code in your hand to see how it works. • It's a great way to ask questions and discuss strange behavior. • The Swift community in Japan is using these useful bots on Discord every day.
 https://discord.gg/4Scjz4J
  11. Other Swift Web Playground • Replit https://replit.com/ • Wandbox https://wandbox.org/

    • Online Swift Playground http://online.swiftplayground.run/ • iSwift https://iswift.org/playground
  12. Wrap up • Swift does not provide an o ffi

    cial Web Playground. • But you can create your own. • Web programming and server-side programming in Swift is easier than you think. • If Swift can be easily run on the web, it helps much more people will start learning Swift. • Hopefully, many more excellent Swift Web Playgrounds will be created. • Visit swift fi ddle.com and github.com/kishikawakatsumi/swift-playground/