Chapter 10. Looking Under the Hood

This chapter covers various ways of looking at the inner workings of Lua. To continue the automotive metaphor of the title, you can write many useful programs without knowing the things in this chapter, just as you can drive a car without knowing how to change the oil. However, the knowledge and techniques in this chapter will help you find bugs, and let you do things with Lua that otherwise wouldn't be possible. You'll learn about the following:

  • Using bytecode

  • Controlling Lua's memory management

  • Understanding the implementation of tables and strings

  • Looking directly inside functions to see and alter their local variables

  • Tracing the execution of your code on a call-by-call, line-by-line, or instruction-by-instruction basis

Bytecode and luac

As mentioned in Chapter 3, before Lua can execute source code, it needs to compile it into bytecode, which is then interpreted by the bytecode interpreter. Another name for the bytecode interpreter is the virtual machine, because it's kind of like an imaginary computer (imaginary in the sense no one has ever built one out of silicon —it's just simulated by your real computer). Lua comes with two programs: the command-line interpreter lua, and the bytecode compiler luac. The main purpose of luac is to compile Lua source code to bytecode and save that bytecode to a file (so it can be run later), but it can also output a human-readable listing of the bytecode.

Get Beginning Lua Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.