Lua解释器源码(一):源码通览

一、 Lua 源码结构

This is the complete source code of Lua 5.1, with links to header files, functions, macros, types, and variables. You can browse the modules below or find a name in the index.

Includes

lua.h
lauxlib.h
lualib.h
luaconf.h

Core

lapi.c
lcode.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
llex.c

lmem.c
lobject.c
lopcodes.c
lparser.c
lstate.c
lstring.c
ltable.c
ltm.c
lundump.c
lvm.c
lzio.c

 

lapi.h
lcode.h
ldebug.h
ldo.h

lfunc.h
lgc.h
llex.h
llimits.h
lmem.h
lobject.h
lopcodes.h
lparser.h
lstate.h
lstring.h
ltable.h
ltm.h
lundump.h
lvm.h
lzio.h

Libraries

lauxlib.c
lbaselib.c
ldblib.c
liolib.c
lmathlib.c
loadlib.c
loslib.c
lstrlib.c
ltablib.c
linit.c

Interpreter

lua.c

Compiler

luac.c
print.c

二、 Lua 源码阅读顺序

Recommended reading order:

  1. lmathlib.c, lstrlib.c: get familiar with the external C API. Don't bother with the pattern matcher though. Just the easy functions.
  2. lapi.c: Check how the API is implemented internally. Only skim this to get a feeling for the code. Cross-reference to lua.h and luaconf.h as needed.
  3. lobject.h: tagged values and object representation. skim through this first. you'll want to keep a window with this file open all the time.
  4. lstate.h: state objects. ditto.
  5. lopcodes.h: bytecode instruction format and opcode definitions. easy.
  6. lvm.c: scroll down to luaV_execute, the main interpreter loop. see how all of the instructions are implemented. skip the details for now. reread later.
  7. ldo.c: calls, stacks, exceptions, coroutines. tough read.
  8. lstring.c: string interning. cute, huh?
  9. ltable.c: hash tables and arrays. tricky code.
  10. ltm.c: metamethod handling, reread all of lvm.c now.
  11. You may want to reread lapi.c now.
  12. ldebug.c: surprise waiting for you. abstract interpretation is used to find object names for tracebacks. does bytecode verification, too.
  13. lparser.c, lcode.c: recursive descent parser, targetting a register-based VM. start from chunk() and work your way through. read the expression parser and the code generator parts last.
  14. lgc.c: incremental garbage collector. take your time.
  15. Read all the other files as you see references to them. Don't let your stack get too deep though.

三、 Lua 接口

Lua供外部程序使用的标准API定义在lua.h文件中,在lapi.c中实现,使用lua_xxx的命名风格。这些API功能在官方文档里有解释。此外,在lauxlib.h中定义的API封装了标准API,它们以luaL_xxx方式命名,供库开发使用。

  • lua_ - lapi.c/h + luaconf.h, debug.c
  • luaL_ - lauxlib.c/h, linit.c

Lua供内部模块之间调用的API使用luaX_xxx的命名风格,X表示了内部模块名。它们都禁止被外部程序调用。主要如下:

  • luaA_ - lapi.c
  • luaB_ - lbaselib.c
  • luaC_ - lgc.c
  • luaD_ - ldo.c
  • luaE_ - lstate.c
  • luaF_ - lfunc.c
  • luaG_ - ldebug.c
  • luaH_ - ltable.c
  • luaI_ - lauxlib.c
  • luaK_ - lcode.c
  • luaM_ - lmem.c
  • luaO_ - lobject.c
  • luaP_ - lopcodes.c
  • luaS_ - lstring.c
  • luaT_ - ltm.c
  • luaU_ - lundump.c
  • luaV_ - lvm.c
  • luaX_ - llex.c
  • luaY_ - lparser.c
  • luaZ_ - lzio.c

其它内容还包括lua整数相关的操作luai_和打开lua标准库luaopen_等相关操作:

  • luai_ - luaconf.h
  • luaopen_ - luaconf.h + lualib.h (lbaselib.c, ldblib.c, liolib.c, lmathlib.c, loadlib.c, loslib.c, lstrlib.c, ltablib.c)

参考

  1. 风云:lua源码欣赏

发表评论