Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Apple's Swift Playgrounds app for iPad execute code?

Tags:

ios

swift

ipad

llvm

How does the Swift Playgrounds app for iPad execute code?

Have they ported the LLVM toolchain to ARM and are compiling on the fly or are they using a completely different way to execute Swift code?

How can I accomplish something similiar in iOS (i.e., dynamic execution of Swift code)?

like image 784
antfarm Avatar asked Nov 14 '16 16:11

antfarm


Video Answer


1 Answers

Neither of Apple's general-audience nor developer marketing materials don't say much more than that Swift Playgrounds is "real code". But it's possible to verify for yourself (or at least find some strong evidence for) that Swift Playgrounds does indeed run the full compiler toolchain and produce native arm64 code.

Tip: if you download an App Store app in iTunes on your Mac (or Windows box, too, I guess?), you can find the corresponding .ipa file in your local filesystem. Unless you're a beer person, IPAs are just zip archives containing the app bundle that gets installed onto an iOS device when you sync, so you can unzip them with your favorite utility (including the builtin Archive Utility in macOS, or the unzip command line tool) and poke around a bit at their inner workings.

If you do this with Swift Playgrounds, you'll find a nearly complete replica of the SDKs and toolchains found inside Xcode, including some files specific to linking binaries for the arm64 architecture. (You won't find any command line binaries for the compiler & related tools, but there's nothing new about running parts of the LLVM toolchain as libraries.) You can also find some evidence of arm64 as a compiler-host architecture in the Swift compiler's open source code.

So, yes — short of official confirmation from Apple, it looks like Swift Playgrounds compiles and runs native code on iOS devices. (If you think about it a bit, it doesn't make much sense to do it otherwise — running Swift code in some sort of interpreted/emulated environment would kill performance. And probably make Apple's stated goal of providing access to the entire iOS toolkit incredibly difficult.)

And no, third-party apps still can't do that. Since 2010 it's been possible for third party apps to use tools that provide embedded scripting environments — game engines use this to speed up game design & development, and other apps provide coding environments for Python and Lua, for example. But you can probably imagine that allowing third-party apps to compile and run native code could be a security nightmare...

like image 83
rickster Avatar answered Nov 09 '22 23:11

rickster