Overview of function prefixes and header files

Internal function prefixes and header files
Prefix Header Abstraction
luaL_ lauxlib.h Auxiliary library (extends public API)
luaK_ lcode.h Code generator
N/A lctype.h Replacement for ctype.h
luaG_ ldebug.h Debug hooks?
luaD_ ldo.h Calls; register-window stack
luaF_ lfunc.h Closures and free varibles (“upvalues”)
luaC_ lgc.h Garbage collection
luaX_ llex.h Lexical analysis
N/A llimits.h Sizes, limits, numeric-operation macros
luaM_ lmem.h Allocation
luaO_ lobject.h Key representations: Values, type tags, conversions
N/A lopcodes.h Instruction decoding; opcode table
N/A lparser.h Parser; classification of names
luaE_ lstate.h Interpreter state; call stack
luaS_ lstring.h Strings
luaH_ ltable.h (Hash) tables
luaT_ ltm.h Tag methods (metamethods)
lua_ lua.h Public (C) API
N/A luaconf.h Configuration for portability (uninteresting)
luaopen_ lualib.h Access to standard libraries
luaV_ lvm.h Bytecode interpreter & operations
luaZ_ lzio.h Buffered streams

Documentation to read

Before tackling any code, read this documentation:

Code to read

Note: When reading source code, you may want to use the Unix “tags” facility, which gives you a fast way to jump to the definition of any identifier. If you use Emacs or vi, look up the Unix commands etags or ctags.

On line 160 of header file lstate.h, understand these fields of the struct lua_State:

On line 65 of header file lstate.h, understand the CallInfo structure. Ignore fields having to do with yields.

On line 97 of lapi.c, read the implementation of lua_checkstack. Note differences between ci->top and L->top.

On line 60 of lapi.c, read the first two cases in the implementation of index2addr.

On line 1130 of lvm.c, read the implementation of the OP_CALL opcode, and also the implementation of luaD_precall on line 413 of ldo.c. Be prepared to compare the two main cases of luaD_precall. Ignore the “hook” code.

Finally, the implementation of pcall:

Discussion questions

  1. How is the Lua call stack represented?

  2. How does it present an interleaving of Lua activations with C activations? What’s actually happening with the C stack?

  3. How does the Lua call stack work with the register-window stack (also known as “the Lua stack”)?

  4. In the virtual machine, how does the CALL instruction work?

  5. In the API, how does function lua_call work?

  6. How is the Lua primitive pcall implemented?