Introduction to Lua & The Virtual Machine

Introduction to Lua & The Virtual Machine in Lua: Created in 1993 at PUC-Rio in Brazil, Lua is the premier embeddable scripting language in computer science.

Created in 1993 at PUC-Rio in Brazil, Lua is the premier embeddable scripting language in computer science. It powers the user interfaces, scripting engines, and gameplay logic of major platforms including Roblox (Luau), World of Warcraft, Love2D, Baldur's Gate, Redis (Lua scripting), OpenResty / NGINX, and aerospace telemetry systems.


Why Lua Dominates Embedded Systems

  1. Microscopic Footprint: The entire compiled Lua interpreter and standard library is approximately 300 KB.
  2. Execution Velocity: Lua is among the fastest dynamically typed languages in existence, and with LuaJIT, its performance approaches raw compiled C.
  3. Register-Based Virtual Machine: Unlike the Java Virtual Machine (JVM) or Python VM which are stack-based, the Lua VM (since Lua 5.0) is register-based. Instructions map directly to CPU registers, reducing instruction dispatch overhead and memory copying.
  4. Seamless C Interoperability: Lua was designed from day one to be embedded inside host C and C++ applications rather than running as a standalone operating system process.

Global vs Local Scoping: The Performance Difference

In Lua, variables are GLOBAL by default. Failing to specify local attaches the variable to the global environment table (_G), which triggers expensive hash lookups on every access and pollutes the global namespace:

-- BAD: Pollutes _G, requires hash-table lookup on every read
playerScore = 100

-- GOOD: Allocated in a high-speed Lua VM register slot
local playerScore = 100
local MAX_LIVES = 3
local isInvincible = false

Core Rule: ALWAYS declare variables and functions as local unless you explicitly intend to expose them globally across all loaded scripts.


The Eight Primitive Types of Lua

Lua is dynamically typed: variables do not hold types; values do.

local val

val = nil           -- Absence of value (falsy)
val = true          -- Boolean (true or false)
val = 42.5          -- Number (64-bit IEEE float; Lua 5.3+ also has 64-bit integer subtype)
val = "CodeAcademix"-- String (immutable byte sequences)
val = function() end-- Function (first-class value!)
val = {}            -- Table (the ONLY data structure in Lua)
val = coroutine.create(function() end) -- Thread (coroutine)
-- Userdata: Raw C memory pointers managed by the host application

Truthiness in Lua (The Crucial Difference)

In JavaScript and Python, 0, "", and NaN are considered falsey.

In Lua, ONLY false and nil are falsey. Everything else—including the number 0, an empty string "", and an empty table {}—evaluates to true!

local count = 0
if count then
    print("Zero is TRUE in Lua!") -- This ALWAYS prints!
end

local text = ""
if text then
    print("Empty string is also TRUE in Lua!") -- This ALWAYS prints!
end

Exercises

  1. Write a script demonstrating that 0 evaluates to true in a conditional branch.
  2. Inspect the global environment table _G using a loop and identify the built-in functions present.
  3. Explain why register-based virtual machines execute fewer bytecode instructions than stack-based virtual machines.