标签搜索

python访问Lua

anker
2025-12-27 / 0 评论 / 6 阅读 / 正在检测是否收录...

安装环境:

apt-get install liblua5.4-dev
pip3 install lupa

Python访问Lua虚拟机

获取虚拟机:

import lupa.lua54 as lupa
from lupa import LuaRuntime
lua = LuaRuntime(unpack_returned_tuples=True)

访问Lua全局表:lua.globals()["print"] = print

执行Lua代码1: lua.execute('print("hello")')

执行Lua代码2: ret = lua.eval("_VERSION") , ret 是一个Lua变量封装变量

判定Lua变量类型: lupa.lua_type

封装变量给Lua访问:

ret = lupa.as_itemgetter(py_dict) #Lua索引`ret`时优先取数据
ret = lupa.as_attrgetter(py_dict) #Lua索引`ret`时优先取属性方法

Lua访问Python

Lua脚本内置Python变量
python.builtinsuserdatapython内置类型,包含print/len/type/isinstance/list/dict等等
python.argsfunctiontodo
python.as_functionfunctiontodo
python.evaluserdata用于执行python字符串代码
python.set_overflow_handlerfunctiontodo
python.noneuserdata类似lua中的nil, 遍历pyobj时可以直接相等判定
python.enumeratefunction类似ipairs,遍历pyobj, 返回indexvalue。 但是index从0开始,如果是Dictvalue其实是key
python.iterfunction遍历pyobj, 一次只返回一个值,list类型的元素值或者dict类型的key
python.as_itemgetterfunction设置pyobj数据在Lua索引时访问优先
python.as_attrgetterfunction设置pyobj数据在Lua索引时属性方法优先索引
python.iterexfunction类似pairs, 遍历pyobj, 返回keyvalue
0

评论

博主关闭了当前页面的评论