Contributing Editorial
by Milo Christiansen, the author of Fengari
Reimplementing Lua: A few helpful hints for your own VM
implementation would be more trouble than it is worth. In Sometimes you have a project where access to a Lua VM would be a very nice thing to have. Sometimes using the referencethese situations you can often find a ready-made solution created in your language of choice by some kind soul, but once in a while there simply isn't anything that fits your needs.
At heart the Lua VM is a simple beast, and reimplementing it in another language is fairly straight forward. I won't go into a great deal of detail here, but I will give you a poke or two in a good direction to get started. Before you start, there are a few things you really ought to get:
- The bytecode reference is invaluable. This may or may not be complete, as when I used it (a few years ago) there was some stuff missing. The older 5.1 document and a bit of peeking at the official Lua source code will quickly fill in any holes it may have.
- Speaking of the source code, you will need a copy. You won't need to look much up, but sometimes there is no substitute for just looking at the source. If you can find the source for implementations in languages similar to the one you are targeting, they could come in handy as well.
- Last but not least, you need a copy of the Lua test suite. You probably won't be able to use these tests unmodified (since it is highly unlikely that you will implement all of the debug package for example), but they provide a solid starting point for your own tests.
First and foremost: Use luac. Do not write your own compiler. You may want one at some point, but while writing and testing the VM, compiler bugs are the last thing you want to deal with. The Lua binary format is simple and easy to load for the most part, so just use the reference compiler to start. Once you have a working loader, write some code to dump a Lua program as an assembly listing. This allows you to make sure your loader actually works and lets you test instruction decoding in one fairly simple bit of code. From here it is just a matter of writing the basic structure, then filling in the VM loop one instruction at a time. The instructions are actually pretty easy for the most part, the value stack, call stack, and types (particularly tables and meta-methods) tend to be the biggest issues. When in doubt about how to do something, it is often worth a peek at the reference implementation and/or other implementation in languages similar to the one you are using. For example, when I was writing my Lua VM I had two other implementations of older Lua versions I could look at written in the same language (they ended up being less helpful than I would have liked, but were still pretty useful).
Writing a Lua VM is a good introduction to making VMs for scripting languages, and I found it to be quite a lot of fun!
|
|