I Built a Browser Engine in Swift. Here's Why.

Tuesday, March 31, 2026

Intro

I don’t know exactly when. But, I have a dream to shift my career from a web developer into web browser engineer. I wish I could make it happen in a closer future. At the time this post is published, there are popular browser engines in this world:

The Ladybird is a new browser engine from scratch by Andreas Kling (he was ex-Apple who works for WebKit - as far as I follow him on Twitter and I watch him on YouTube). In a video, Andreas mentioned that he and the team have a plan to move the Ladybird code from C/C++ into Swift. I’m excited and waiting that day happen. But, it’s not. Instead, Andreas and the team decide to move it into Rust with help from AI - because the C/C++ interopability on Swift doesn’t match the expectation criteria for them. Honestly, I’m sad because I’m really wanna see the browser engine build with Swift from scratch.

Rather than waiting and waiting to see that day happen, why don’t I made a try to make a browser engine with Swift from scratch?

No one is perfect, but making progress moves you closer to it.

Target

  1. The browser engine can read URL, parse the content from URL with HTML parser, CSS parser, and JavaScript Engine then show it in a page in a tab. That’s it for a good start.

  2. It doesn’t have to be perfect or using fancy things like C/C++ interopability.

But, I need a solid and robust resource to teach me how to make a web browser.

No no no. Please don’t suggest me the WHATWG HTML Standard. It’s too too too big. 😵‍💫

Luckily I found it! The Browser Engineering book - created by Pavel Panchekha and Chris Harrelson. This book covers the foundation to make the web browser engine using Python. I have follow the chapter since October 2025. I save it on GitHub repository called brownie. Now, I’m in the last chapter and soon it will be finished. :)

Reasons

There are three reasons why I choose Swift as programming language to make a browser engine:

  1. Curiosity

The real-world browser engines mostly build with C/C++. Recently, Rust joined this competition with Servo and followed by Zig with Lightpanda. So, I took another approach. I use Swift is a programming language because it has well integration with GUI from macOS. I don’t need to waste my time to seeking the GUI desktop engine. I’m also would like to see how far Swift can be used as programming language to build a browser engine.

  1. Everything is object

Everything is object. The web has Document Object Model (DOM). Swift has object-oriented programming paradigm. That’s it. For more detail information, you may ask your LLM. ;)

How does the DOM relate to Swift’s OOP model in the context of building a browser engine?

  1. Learning

I’m a beginner in building browser engine area. Mostly, I’m just a consumer who use the browser. Now, it’s time to learn how to build it.

Starting Point

Artificial Intelligence has turned into Large Language Model (LLM) that turned into a product called Claude (one of them). The code program is no longer mystery. The another mystery is Do I understand the meaning of the code program?. I think in this LLM era, I still need to learn how to program with programming language in order to understand the meaning of code program.

At the first try, I just throw all the things from the brownie and tell the Claude to port it into Swift. Errrr… It doesn’t work as expected. Then, I change my workflow.

First, I split the parts of brownie into three parts (branches):

The part 4 is really hard to implement. If I put wrong reusable state then the browser will be terrible shape. I imagine like playing minesweeper game.

Second, I create a Swift project for web browser engine. I called it ToyStack.

# Swift convention name for project name is PascalCase
mkdir ToyStack
cd ToyStack
# By default swift will create package for type Library.
# But I set the type to be Executable because I execute a program to run a browser
# So, you will get Sources directory, .gitignore file, and Package.swift
swift package init --type=executable --name ToyStack

Inside the ToyStack I put the brownie project and switch into ch01-10 branch. Then, I start chat with Claude with the prompt like this.

I want to make a browser engine with Swift programming language. Currently, I have brownie - a browser engine with Python that comes from Browser Engineering book. Your task is read the Python code inside the brownie and porting it into Swift. Create a plan with step by step which one is first, second, and so on. You’re a guider and give me the code BUT you’re NOT ALLOWED to edit the code. I’m a learner and will re-type the code program to get better understanding of the meaning of code.

The ToyStack has covered the chapter 14 along with some exercises from the Browser Engineering. It doesn’t have any third-party dependencies. It’s only using Swift and SwiftUI. The rendering engine is built from scratch by follow the Browser Engineering book. Meanwhile for JavaScript engine, I use JavaScriptCore (Safari) instead of build from scratch. I guess I will try to built it using Swift too in the future.

Closing

Thanks to the help from AI, the code is no longer mystery and build a “toy” browser engine can be done in days instead of weeks or months. But, for the real-world browser engine is another story. My experience when make the browser engine with Swift is mixed feeling. Mostly, I’m confused with the output of code program because I have less experience with Swift programming language. Even along with the web browser term called Chrome - it’s a control for managing tabs, back and forward button, address bar input, bookmark site. I was thinking the Chrome is just the product name of Google Chrome. 🙈

If I want to make a description on how the browser engine works maybe like this:

The ToyStack doesn’t finished yet. There are things that is still on going - add features and optimized. Maybe I wanna play a game inside the ToyStack one day. ;)

GitHub repository: kresnasatya/ToyStack.