Nothing much, but it is a huge milestone for me to learn raylib, zig and web assembly
After the departure on discord, and the reduction of my social media consumption time, I decided to explore something I have been wanting to try, learning a programming language so that I can build some music tools or even some simple games just for fun. After all the efforts, I can finally able to build my first web assembly although it is nothing but a “Hi Mate! I am Accipiter Nova.” in my own conlang, but this is a huge step since after all, I can build my apps upon this template.
The first step is usually the toughest, and I have gotten a lot of hurdles before I can compile even just a simple web app.
Starting from the choice of languages. Originally, I want to find an good programming language that is simple to use, but even for that, I have gone through multiple iterations.
As a java dev, java was definitely my first choice because fundamentally, it is not a bad language as people claimed, especially their recent updates included a lot of great features like pattern matching, records, python like String Literal (STR.”format \{something}”) and reduction of the main function without mandatory putting that long stream of public class Main public static void main… However, I have gotten one great challenge, using dynamic libraries with java because I couldn’t figure out how to successfully build a java program with injecting local dependency using maven which is the package manager I frequently used for my enterprise projects, and this is the reason why my java based VOXCOM 1610 has such a funky programming sequence without using the SunVox library. In addition, since my aim is to build some real time audio applications, like a plugin, java might not be the best option despite being the fifth fastest language since it has a garbage collector, clearing the unused data when it is necessary. I heard about people saying that we can’t predict when the garbage collector is triggered, and if it is run, it cause some latency; thus, I don’t want to gamble the performance of audio software with java.
Python and JavaScript are out of the question because not only they are slow which are not suitable for my use case, but also they are dynamically typed, meaning that the input of some functions can be anything, and I had a huge pain in the backend on working on some of the assignments which are written in Python in this particular reason, resulting in reading the whole project just to understand what is the parameter for a single function. For small scripts, they does the job really well, but I don’t think they are good options for large systems.
Followed by rust, I do like this language so much because I have learnt and cleared the confusion so much at the lower level concept when I was learning C, and this languages help me massively on improving my coding quality because it encourages me on using functional concepts and understand how ownership works between references; nevertheless, GUI has been an issue in rust that there are not many good libraries natively supported with rust, especially when it is combined with the borrow checkers, lifetimes and macros, the experience of using them is rough. Considering the time I have spent and yet I can even build a simple application or data structure, I have to find something else.
Here is when I have found zig. I didn’t really know about this language initially because I didn’t really hear much mentions in the audio programming and web application crowd, until one day, I have came across a video called “Top 5 Fastest Programming Languages: …”. While I am surprised by the performance of java, rust and C was in second and third place shocked me. “Not even C and rust be the first place, nor it is assembly language, what will that gonna be?” “Number 1, zig.”, Dave said. This was the first time I ever heard about an obscure name appeared in the top fastest programming list, and this sparked my curiosity and I want to learn more about the language. After the end of this semester, I started to take zig seriously and trying to understand the features of it, and I found this might be an ideal language for audio programming because it is a compiled language, same class as C and C++, meaning the performance is the same if not better. The zig build system might look a bit daunting at first, but it is an awesome system since it makes cross compile much less painful, compared to Cmake. More importantly, I found the syntax of the language is a bit rust like, which I am a fan of, and it doesn’t have all the pain when I use rust. Along with all other cool features in zig, I just found it has combined many of the best of some other languages (at least my perspective), and I found it is joyful and fast to work with; thus, I have finally settled down my language of choice.
Meanwhile, remembered I have mentioned that VOXCOM 1610 assembler? There have been some on going issues, but I didn’t know how to fix; especially, how can I run the program in different platforms so that people can compile their assembly for the VOXCOM computer? Then the idea of using web assembly comes in mind since as long as someone have a browser, they can run the webassembly program anywhere, and this faced my second boss: how to compile a zig program into a webassembly that compile my VOXCOM assembly? I have been struggled for awhile because there aren’t many documentations regarding to the topic, or some blogs only covers the bare minimum which is not usable for larger applications. Fortunately, thanks to an experience user on the zig forum, Sze who have clearly pointed and explained the issues about my code, and he have provided a great example project to demonstrate how to cross compile between executable and webassembly which really helps me understand more about the how does the build file works in zig.
However, there is a final boss in the source code which I found that is the most challenging, the Emscripten sdk. In the webassembly source code of the example project I was studying, there were a stream of command like -o, -sFULL-ES3, and -sUSE_GLFW which was a bit confusing at times. I have puzzled a bit because I have never seen command like these while there are many of them. Luckily, the provided message contains an error message to point out that I need to install the emscripten sdk, and all these commands I have mentioned earlier are the configuration for the sdk which are used for determine the type of graphic driver, the way to bridge between async and sync languages, predefined memory usage, and etc. I did spent quite a bit of time in their document just to understand all the parameters used for the example project, but fortunately, the document is comprehensive, so I can understand the command reasonably well.
Finally, with all the twists and turns, my very first web assembly is now complied. I am so excited to a point I want to make a blog post to share my excitement because I know this has opened a new horizon of possibilities that I can’t wait to try, For now, after I have cleaned the zig webassembly template, I am going to have a look to raylib so that I can learn more about how to build a UI with their available functions.
Stay Tuned and Soar Beyond!