openresty / docker-openresty

Docker tooling for OpenResty
https://hub.docker.com/r/openresty/openresty
BSD 2-Clause "Simplified" License
935 stars 525 forks source link

Can't load the cjson module into luajit CLI #239

Open ADUDevEx opened 11 months ago

ADUDevEx commented 11 months ago

Environment

Computer : Mac Book M2 Docker : 24.0.6, build ed223bc820 Docker image : openresty/openresty:1.21.4.1-8-alpine-fat

Issue

CJSON is supposed to be available on the Docker image

Demo

~ $ docker run -ti openresty/openresty:1.21.4.1-8-alpine-fat sh
/ # env
HOSTNAME=6a0edb9ed3dc
LUA_PATH=/usr/local/openresty/site/lualib/?.ljbc;/usr/local/openresty/site/lualib/?/init.ljbc;/usr/local/openresty/lualib/?.ljbc;/usr/local/openresty/lualib/?/init.ljbc;/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
LUA_CPATH=/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so
PWD=/
/ # find / -name "*cjson*"
/usr/local/openresty/lualib/cjson.so
/usr/local/openresty/pod/lua-cjson-2.1.0.10
/usr/local/openresty/pod/lua-cjson-2.1.0.10/lua-cjson-2.1.0.10.pod
/ # luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/
JIT: ON fold cse dce fwd dse narrow loop abc sink fuse
> local cjson = require("cjson")
> if not cjson then
>> print("no")
>> end
no

/ # luarocks install lua-cjson
Installing https://luarocks.org/lua-cjson-2.1.0.10-1.src.rock
lua-cjson 2.1.0.10-1 depends on lua >= 5.1 (5.1-1 provided by VM)
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c lua_cjson.c -o lua_cjson.o
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c strbuf.c -o strbuf.o
gcc -O2 -fPIC -I/usr/local/openresty/luajit/include/luajit-2.1 -c fpconv.c -o fpconv.o
gcc  -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
No existing manifest. Attempting to rebuild...
lua-cjson 2.1.0.10-1 is now installed in /usr/local/openresty/luajit (license: MIT)

/ # find / -name "*cjson*"
/usr/local/openresty/luajit/share/lua/5.1/cjson
/usr/local/openresty/luajit/lib/lua/5.1/cjson.so
/usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-cjson
/usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-cjson/2.1.0.10-1/lua-cjson-2.1.0.10-1.rockspec
/usr/local/openresty/lualib/cjson.so
/usr/local/openresty/pod/lua-cjson-2.1.0.10
/usr/local/openresty/pod/lua-cjson-2.1.0.10/lua-cjson-2.1.0.10.pod
/var/cache/luarocks/https___luarocks.org/lua-cjson-2.1.0.10-1.src.rock

/ # luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/
JIT: ON fold cse dce fwd dse narrow loop abc sink fuse
> local cjson = require("cjson")
> if not cjson then
>> print("no")
>> end
no

I hope I'm not just overlooking a step to load it. Let me know if you need more information and thanks in advance for your help.

neomantra commented 11 months ago

The LuaRocks CJSON won't work without pat adjustment, but CJSON should be built-in.

Try these commands to verify:

> docker run -ti --rm openresty/openresty:1.21.4.1-8-alpine-fat sh -c "luajit -e \"print(require('cjson'))\""
table: 0x581979746f08

> docker run -ti --rm openresty/openresty:1.21.4.1-8-alpine-fat sh -c "luajit -e \"for k, v in pairs(require('cjson')) do print(k,v) end\""
new function: 0x23f05180eef0
null    userdata: NULL
empty_array_mt  table: 0x23f05180e0a8
array_mt    table: 0x23f05180e0f0
empty_array userdata: 0x7fffb48bd988
_NAME   cjson
_VERSION    2.1.0.10
encode  function: 0x23f05180e7c0
decode  function: 0x23f05180e838
encode_empty_table_as_object    function: 0x23f05180e8a0
decode_array_with_array_mt  function: 0x23f05180e988
encode_sparse_array function: 0x23f05180ea08
encode_max_depth    function: 0x23f05180e920
decode_max_depth    function: 0x23f05180eb48
encode_number_precision function: 0x23f05180ebc0
encode_keep_buffer  function: 0x23f05180ec38
encode_invalid_numbers  function: 0x23f05180ea48
decode_invalid_numbers  function: 0x23f05180eac0
encode_escape_forward_slash function: 0x23f05180ee70