Programming in Lua 4e · 学习地图

按Programming in Lua第四版四个Part与33个官方章节规划学习,建立value、control、protocol、native四层依赖和逐章证据门禁。

为什么学习地图必须先还原33章原书坐标

本路线对应Roberto Ierusalimschy的Programming in Lua, Fourth Edition(Lua 5.3),不是把全书压缩成十个主题页。33个官方章节分为The Basics、Real Programming、Lua-isms和The C API四个Part;另加本导学与总复习,共35个可导航页面。每个chapter保留原书核心概念,再补充chapter-specific diagram、可运行code、failure injection和exercise evidence。

先预测:只会table和function能否解释weak table;只会coroutine.resume能否写稳定event loop;只会lua_getglobal能否保证C stack平衡;把Lua thread、state与OS thread混用会造成什么错误。地图不是阅读清单,而是依赖图:后面的ownership、error和isolation都建立在前面的value与control semantics上。

保证“学完”可以按33个单元逐一核查。

全书坐标:Four Parts与33 Chapters

Part I · The Basics(Chapters 1–8)

  1. Getting Started:chunks、lexical conventions、globals与interpreter truth。
  2. Interlude: The Eight-Queen Puzzle:完整回溯程序、board表示、冲突检测与solutions。
  3. Numbers:integer/float、operators、math library与randomness。
  4. Strings:immutable bytes、patterns前的string library与Unicode边界。
  5. Tables:identity、constructors、sequence、traversal与table library。
  6. Functions:multiple results、variadic functions、named arguments与tail calls。
  7. The External World:simple I/O、complete I/O model、其它system calls。
  8. Filling Some Gaps:local scope、control structures、goto与program exit。

这一Part的验收不是背“八种类型”,而是能写出representation invariant。下面的probe故意覆盖truth、integer/float、binary string、table identity和multiple results;运行前先写expected output。

local bytes = "A\0B"
local a, b = {}, {}
local function split() return 1, nil, 3 end
local x, y, z = split()
 
assert(not not 0 and not not "")
assert(math.type(3) == "integer" and math.type(3.0) == "float")
assert(#bytes == 3 and a ~= b)
assert(x == 1 and y == nil and z == 3)

是后续serialization、userdata和message schema的共同基础。

Part II · Real Programming(Chapters 9–19)

  1. Closures;10. Pattern Matching;11. Interlude: Most Frequent Words;12. Date and Time;13. Bits and Bytes;14. Data Structures;15. Data Files and Serialization;16. Compilation, Execution, and Errors;17. Modules and Packages;18. Iterators and the Generic for;19. Interlude: Markov Chain。

本Part把语法变成程序。Closures保存upvalue state,patterns建立mini-language,serialization处理graph identity,modules定义load/cache边界,generic for约束iterator triple。两个Interlude要求完整pipeline:输入、transform、state、output与known evidence全部可复现。

local function frequency(tokens)
  local counts = {}
  for token in tokens do
    counts[token] = (counts[token] or 0) + 1
  end
  return counts
end
 
local function words(text)
  return text:gmatch("[%a_][%w_]*")
end
 
local counts = frequency(words("lua C lua 5_3"))
assert(counts.lua == 2 and counts.C == 1 and counts["5_3"] == nil)

防止只展示片段而不证明整体行为。

Part III · Lua-isms(Chapters 20–26)

  1. Metatables and Metamethods;21. Object-Oriented Programming;22. The Environment;23. Garbage;24. Coroutines;25. Reflection;26. Interlude: Multithreading with Coroutines。

这里学习Lua最有辨识度的机制。Metamethod dispatch不是class magic,而是明确fallback protocol;environment是lexical _ENV与load policy;GC围绕reachability、weak references和finalizer;coroutine以resume/yield双向协议保存control state;reflection/debug是必须收紧的capability。

比记住API名字更可迁移。

local task = coroutine.create(function(initial)
  local command = coroutine.yield("ready", initial)
  assert(command == "finish")
  return "done"
end)
 
local ok, state, value = coroutine.resume(task, 7)
assert(ok and state == "ready" and value == 7)
local finished, result = coroutine.resume(task, "finish")
assert(finished and result == "done")

Part IV · The C API(Chapters 27–33)

  1. An Overview of the C API;28. Extending Your Application;29. Calling C from Lua;30. Techniques for Writing C Functions;31. User-Defined Types in C;32. Managing Resources;33. Threads and States。

这七章共享一条纪律:每个boundary写清entry stack、indices、top delta、result/error shape与resource owner。后续增加registry、C closures、userdata、finalizers、continuations和independent states,也不能破坏这份stack/lifetime contract。

是C API代码审查的最小单位。

从Value到Native的Dependency Path

逐章Mastery Gate

每章先预测,再运行最小完整程序,然后扰动边界,最后脱离页面重画机制。既覆盖知识,也覆盖故障路径。

本章回顾:地图连接原书坐标与可验证能力

  1. 本路线保留第四版四Part、33个官方章节,并以导学/总复习组成35页导航。
  2. The Basics建立value/representation;Real Programming建立完整pipeline;Lua-isms建立机制protocol;C API建立native ledger。
  3. Lua-only路线完成1-26,embedding路线在此基础上继续27-33,C API不能脱离Lua semantics。
  4. Dependency path由value、control、protocol、native逐层累积,遇到困难应回到最近缺失前置。
  5. 每章用predict、execute、perturb、explain四种证据验收,而不是用阅读时长验收。

术语表

讨论

评论区加载中…