标准Lua和LuaJIT的关系
LuaJIT只是兼容了Lua5.1的语法,并对Lua5.2和5.3做了选择性支持。
另外,OpenResty并没有直接使用LuaJIT官方提供的版本,而是维护了自己的LuaJIT分支,并扩展了很多独有的API
LuaJIT的性能优化,本质上是让尽可能多的Lua代码可以被JIT编译器直接生成机器码,不用再解释执行。类似于jvm里的JIT
Lua的特别之处
-
Lua的下标从1开始
-
使用
..来拼接字符串'hello'..', world'>>hello, world -
只有
table这一种数据结构,可以同时包括数组和哈希表,如果没有明确用键值对的方式赋值,table就会默认用数字作为key,从1开始。local color = {first="red", "blue", third="green", "yellow"}
print(color["first"]) --> red
print(color["third"]) --> green
print(color[1]) --> blue
print(color[2]) --> yellow
print(color[3]) --> nil如果要获取table的长度,在混合使用的情况,也不容易,只有全是序列的时候,才能获取到正确的长度,序列必须所有元素可以用正整数下标访问,并且不能有空洞,即不能有nil
openresty对获取table的长度方面做了扩展。
-
默认是全局变量
在lua中,变量默认是全局的,会被放到名为_G的table中,不加local的变量会在全局表中查找,所以建议总是使用
local来声明变量,即使require module时。local xxx = require('xxx')
local s = 'hello'
LuaJIT
FFI
除了兼容5.1的语法,并支持JIT之外,LuaJIT还结合了FFI(Foreign Function Interface),支持直接在Lua代码中调用外部的C函数和使用C的数据结构
local ffi = require("ffi")
ffi.cdef[[
int printf(const char *fmt, ...);
]]
ffi.C.printf("Hello %s!", "world")
上述示例支持我们在lua调用C的printf函数,类似的,FFI可以用来调用NGINX、OpenSSL的C函数,完成更多功能。FFI的方式比传统的Lua/C API方式性能更好。see lua-resty-core
table扩展
LuaJIT扩展了table的相关函数 table.new和table.clear,是两个在性能优化方面非常重要的函数。
luajit-openresty is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with the LuaJIT formula.
If you need to have luajit-openresty first in your PATH, run:
echo 'export PATH="/usr/local/opt/luajit-openresty/bin:$PATH"' >> ~/.zshrc
For compilers to find luajit-openresty you may need to set:
export LDFLAGS="-L/usr/local/opt/luajit-openresty/lib"
export CPPFLAGS="-I/usr/local/opt/luajit-openresty/include"
==> Summary
🍺 /usr/local/Cellar/luajit-openresty/2.1-20230410: 38 files, 1.8MB
参考:
00.lua安装
#brew install luajit
#brew install [email protected]
注意,还有一个luajit-openresty,比较了一下,跟luajit基本是一样的,不知道有啥差别
01 luarocks安装
这里踩坑比较多
尝试brew安装(失败)
首先是brew install luarocks,在安装最新版本luarocks的同时,也安装依赖lua5.4,但是我们要用的是luajit或者lua5.1
其次,通过查询网络文章【LuaJIT版】从零开始在 macOS 上配置 Lua 开发环境 - iOS122 - 博客园,发现可以使用brew来安装支持luajit的luarocks,具体命令 :
brew tap mesca/luarocks
brew install luarocks51 --with-luajit
不知道是我的brew版本的问题还是什么原因,报--with-luajit非法参数
macOS 上安装 Lua 及 Luarocks | TripleZ's Blog 这个文章里的内容也一样,不好使,同上,似乎是因为formulae太老,brew已经不支持了GitHub - mesca/homebrew-luarocks: Provides non-conflicting alternative versions of LuaRocks
本地编译安装
最后,直接下载了官方的tar包https://luarocks.github.io/luarocks/releases/,编译本地安装
编译及安装命令
tar -zxvf luarocks-3.9.2.tar.gz
cd luarocks-3.9.2
./configure --prefix=/usr/local/luarocks-3.9.2 --lua-version=5.1 --with-lua-bin=/usr/local/opt/[email protected]/bin --with-lua=/usr/local/opt/[email protected] --with-lua-include=/usr/local/opt/[email protected]/include/lua5.1 --with-lua-lib=/usr/local/opt/[email protected]/lib
make
sudo make install
基本是根据lua5.1来安装luarocks,因为luajit在执行时,会搜索lua5.1的目录。
这里踩坑有两个:
1.把prefix=/usr/local/luarocks-3.9.2中的local拼写错误,写成了loacl,导致后面安装失败,报错Operation not permitted
2.官方configure的帮助文档中的--with-lua-include=DIR Lua's includes dir. [LUA_DIR/include]这行有问题,报lua路径依赖错误,参考macOS lua debug 环境搭建避坑指南_lua_incdir_EricLi404的博客-CSDN博客发现我本地的include路径,也是在LUA_DIR/include/lua5.1下
至此,终于成功安装了luarocks,真是艰难。
如何安装直接支持luajit的luarocks,依然没有头绪,但是因为luajit执行时会搜索lua5.1的目录,所以也还ok。待后面有空再研究吧。