Python on the Smartphone
Before you get all worked up thanks to the sensationalistic title, Jim Hugunin and Guido have nothing to do with this. After a conversation with Partha, I got all excited about the idea of porting my college project to the Smartphone with VS 2005's new C++ support for devices.
For those among you who didn't follow my previous blog avidly (and shame on you!), I hacked together a something-like-a-virtual-machine for my college finishing project. You can still see some blog posts I wrote back then (1, 2, 3) In the beginning, I envisioned it to be this be-all virtual machine which solved several issues relating to functional and dynamic languages on virtual machines and taking a stab at world peace in the process.
After a 3-week hack job, what I wound up with was something that could do a half-decent job of executing Python (since Python was the only language we wrote a compiler for - instead of the 3-4 languages we had planned originally). The interesting thing about this VM-rewrite was that Smoke (as the VM was called) allocated stack frames on the heap instead of using the C stack. While making our VM really slow when compared to the mainline virtual machines, it also gave us flexibility for doing nifty things like continuations and closures and having really deep recursive functions. For my project review, I showed off a recursive function calling itself several million times. When you have 2GB to play with, a lot of things become possible :-).
Enter Visual Studio for Devices
After joining Microsoft, I had pretty much forgotten about Smoke..until yesterday. After talking to Partha, I dusted off my old source code and started hacking away. I was prepared for the worst - after all, I was porting to a Magneto Smartphone, which is not only well...*not* a computer, but also runs on a totally different architecture (ARM).
To my pleasant surprise, I found that the code recompiled almost cleanly. I had to make very trivial fixes to the code. Fixes like
- GetProcAddress on WinCE has a Unicode version.
- On WinCE, there is no concept of a drive letter. All paths start with the root drive.
Within 10-15 minutes, I had the code compiling and running.
Taking it for a spin
I fired up a trusty editor and typed in the following code
if i == 0:
return i * fact(i-1)
We had written our Python compiler in Python itself. Unfortunately, the compiler needs the actual CPython runtime as we use modules that Smoke can't handle yet. After compiling the Python code to a .smoke file, I copied the compiled bytecode file over to the device using ActiveSync ( <plug>using the new uber-cool feature of VS2005's Device Emulator Manager which lets you hook up your emulator to ActiveSync and work with it like a normal device</plug>).
And voila! It ran perfectly at first try. Since the Smartphone doesn't have a console, I had to be content with seeing output inside Visual Studio. But hey - I'm not complaining :-).