示例工程 01-mental-model

01-mental-model/02-luac-bytecode/README.md

markdown · 0.7 KB

# 02 · luac-bytecode
 
**演示**:`load` 把源码编译成函数;编译一次可以多次调用;`string.dump` 输出字节码。
**运行时**:`lua54`、`luajit`(字节码格式不同,只断言非空)。
**运行**:
 
```sh
.tools/bin/lua54 scripts/run-example.lua examples/01-mental-model/02-luac-bytecode
.tools/bin/lua54 scripts/test-example.lua examples/01-mental-model/02-luac-bytecode
```
 
**预期输出**(lua 5.4.9 / macOS arm64):
 
```text
first=3 second=3
bytecode_bytes=80
```
 
**看点**:chunk 是编译单元,不是「文件」;`load` 的第二个参数只是 chunk 名。
**注意**:字节码长度随版本/平台变化,不要把它当稳定契约。