Readit News logoReadit News
jacobx commented on Bitrig's Swift Interpreter: From Code to Bytecode   bitrig.app/blog/interpret... · Posted by u/jacobx
jacobx · 6 months ago
Hi HN! We're back with another post describing more detail about how we built Bitrig's interpreter for Swift in Swift, this time going into how we're converting the code into bytecode.
jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
koinedad · 6 months ago
This is really cool, will definitely try it out. Does it allow you to move this to your computer later and actually publish the apps? What’s the end goal here?
jacobx · 6 months ago
Yep! You can always export your code to take it to your computer.

We also have functionality in bitrig to build your app and send it to App Store Connect so you can deploy it on TestFlight.

jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
bartvk · 6 months ago
Do you plan to support iPad? If so, how high is it on the backlog?
jacobx · 6 months ago
We have a Mac app that we're beta testing with our existing subscribers. Is there a reason you want this on iPad instead of the Mac?
jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
ks2048 · 6 months ago
It looks like it is in the App Store, so I guess my question is already answered - but I wonder if they had trouble or concerns getting it accepted.

Section 2.5.2 in "App Review Guidelines" (https://developer.apple.com/app-store/review/guidelines/):

    2.5.2 Apps should be self-contained in their bundles, and may not read or write data outside the designated container area, nor may they download, install, or execute code which introduces or changes features or functionality of the app, including other apps. Educational apps designed to teach, develop, or allow students to test executable code may, in limited circumstances, download code provided that such code is not used for other purposes. Such apps must make the source code provided by the app completely viewable and editable by the user.

jacobx · 6 months ago
Yeah there was definitely some back and forth about it before we were eventually approved.

In a sense, this isn't very different from what React Native does (run interpreted code that calls out into native code), just with Swift instead of JavaScript. There used to be JavaScript-specific requirements in the guidelines, but that has been loosened since Swift Playgrounds was released. Now there are Python IDEs, Jupyter Notebooks, and other apps running arbitrary code in the App Store.

jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
nomel · 6 months ago
Very very neat.

What's the performance like?

jacobx · 6 months ago
There's definitely a cost: everything is type-erased and there's a lot more indirection than there would be if the code was compiled. But you usually don't hit performance issues because most app code (especially in the UI) is just a thin layer calling into the OS frameworks. The framework code does the heavy lifting and is all compiled.

The places you can hit performance issues are things like when the app itself has a tight loop that's doing a lot of work.

jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
mtlynch · 6 months ago
>Bitrig dynamically generates and runs Swift apps on your phone. Normally this would require compiling and signing with Xcode, and you can’t do that on an iPhone.

>To make it possible to instantly run your app, we built a Swift interpreter. But it’s an unusual interpreter, since it interprets from Swift… to Swift.

I can't understand what they're talking about, and I don't know if it's because I know too little about programming languages or if it's because they're using unusual semantics.

"a compiled language" makes no sense to me because the language itself doesn't determine whether it's compiled or interpreted. It's just a function of what compilers/interpreters exist. I could write an interpreter for C or a compiler for Python, but the languages themselves aren't compiled or interpreted.

I also don't understand what it means to "interpret" Swift to Swift. Do they mean they compile Swift to Swift (or the more modern "transpile" which means the same thing)? But it sounds like they're doing something dynamically at runtime, so it sounds more like decompiling machine code back to Swift, but the rest of the post doesn't match that interpretation.

jacobx · 6 months ago
I refer to Swift as a "compiled language" because no officially provided interpreter exists for it.

Bitrig runs Swift apps which are dynamically generated by an LLM on the iPhone, despite the iPhone strict security provisions (e.g. inability to write executable pages of memory). The way we do this is by parsing the generated Swift code and mapping that to the compiled calls to the libraries that come in the OS. It's pretty weird, but we think it's worth it to get the ability to immediately render the generated code on the phone.

jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
ushakov · 6 months ago
Do you think you can make it run in Jupyter as a Kernel?
jacobx · 6 months ago
That's not really been something we'd been considering, but yeah I think we probably could. We're primarily using the interpreter to render SwiftUI views, but it supports running arbitrary Swift expressions or statements.
jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
kelvinjps10 · 6 months ago
Your website won't let me scroll to see the content on Firefox Android
jacobx · 6 months ago
I just pushed a change that should fix scrolling on Android browsers.
jacobx commented on How we built an interpreter for Swift   bitrig.app/blog/swift-int... · Posted by u/jacobx
dfabulich · 6 months ago
Could you use this to support hot module replacement? Replacing a SwiftUI view in a live app without restarting the process?
jacobx · 6 months ago
Yeah you could! The only caveat is that either the whole app, or at least the part of the app showing the view you want to replace, would have to be running via the interpreter.

We're very interested in using the interpreter to improve the Swift developer experience in more ways like that.

u/jacobx

KarmaCake day51August 2, 2021View Original