问题表现

lua_State *L = lua_open(); /* opens Lua */
luaopen_base(L); /* opens the basic library */
luaopen_table(L); /* opens the table library */
luaopen_io(L);  /* opens the I/O library */ <---- 这里出错。
luaopen_string(L);  /* opens the string lib. */
luaopen_math(L); /* opens the math lib. */

以上 C++ 代码调用 Lua C 库时,运行时提示:

PANIC: unprotected error in call to Lua API (no calling environment)

原因

Lua 5.1 API 变化。

“The luaopen_* functions (to open libraries) cannot be called directly,
like a regular C function. They must be called through Lua, like a Lua
function.”
http://lua-users.org/lists/lua-l/2007-04/msg00383.html
http://www.lua.org/manual/5.1/manual.html#7.3

解决办法

法一:简单粗暴

改成

lua_State *L = lua_open(); /* opens Lua */
luaL_openlibs(L);

此法一口气加载了很多常用库,做测试环境合适。但是会额外加载一些当次测试可能不需要的库。

法二:真正等价的改法

这个是真正的等价改法。

也就是这种用法:

luaopen_table(L);

应该改成:

lua_pushcfunction(L, luaopen_table);
lua_pushliteral(L, LUA_TABLIBNAME);
lua_call(L, 1, 0);// 或者
lua_pushcfunction(L,luaopen_table);
lua_call(L,0,0);

lua_pushliteral 里面用的变量在 lualib.h 可查:

Function        | Name
----------------+-----------------
luaopen_base    | ""
luaopen_table   | LUA_TABLIBNAME
luaopen_io      | LUA_IOLIBNAME
luaopen_os      | LUA_OSLIBNAME
luaopen_string  | LUA_STRLIBNAME
luaopen_math    | LUA_MATHLIBNAME
luaopen_debug   | LUA_DBLIBNAME
luaopen_package | LUA_LOADLIBNAME

参考

  • LuaL_openlibs() and sandboxing scripts
  • 初学lua --lua嵌入c++的一个问题(初始化lua出错,版本问题)
  • luaopen functions
  • unprotected error in call to Lua API

Lua 报错 PANIC: unprotected error in call to Lua API (no calling environment) 解决办法相关推荐

  1. 解决docer login报错 panic: runtime error: invalid memory address or nil pointer dereference

    问题描述 不知道自己做了什么操作,然后进行docker login的时候,突然出现以下错误 panic: runtime error: invalid memory address or nil po ...

  2. 避坑:git在push本地文件到远程时,报错ailed to push some refs to https://xx/xx.git的解决办法

    git使用踩坑记录 发生问题描述 问题原因及解决办法 发生问题描述 在执行git push -u origin master命令push文件到github时,push失败,报错内容如下: To git ...

  3. 报错Command line is too long. Shorten the command line xxx【解决办法】

    运行springboot项目的时候,出现报错,报错信息如下: Error running OrderServiceBoot. Command line is too long. Shorten the ...

  4. 安装django-rest-swagger报错 command: 'D:\Anaconda\python.exe' -u -c 'import sys, setuptools...的解决办法

    在python环境中使用pip安装django-rest-swagger时,报了一个很奇怪的错误,以下是完整的报错信息: (base) C:\Users\Administrator>pip in ...

  5. is this mysql server_远程连接MySQL数据库报错:is not allowed to connect to this MYSQL server的解决办法...

    1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入MySQL后,更改 "mysql" 数据库里的 " ...

  6. 关于新版SDK报错You need to use a Theme.AppCompat theme的两种解决办法 - 转

    android的一个小问题: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme ( ...

  7. Python 用smtplib库发邮件报错:[WinError 10061] 由于目标计算机积极拒绝,无法连接。解决办法

    看了菜鸟的教程,有这段代码: mail = smtplib.SMTP("localhost") 然后就报错了.菜鸟的教程太老了,Python迭代这么快,根本跟不上. 把这个里面的参 ...

  8. 启动jupyter notebook 报错:ImportError:DLL load failed,找不到指定模块的解决办法

    问题描述: 今天点击jupyternotebook的时候发现,无法启动了.然后使用下面的命令,在cmd窗口也无法启动 jupyter notebook 报错信息(当时忘记截图了):DLL load f ...

  9. 项目启动报错No appropriate protocol (protocol is disabled or cipher suites are inappropriate) 解决办法

    错误描述: 项目启动报错 Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is di ...

最新文章

  1. ubuntu 14.04设备OVS虚拟OpenFlow交换机配置汇总
  2. 【PP生产订单】入门介绍(六)
  3. 文本挖掘(part6)--共现矩阵
  4. vuex的结构有哪些参数?
  5. 查询 service monitor 时发生内部错误_通过Service访问应用 (1)
  6. LeetCode 624. 数组列表中的最大距离
  7. 【kafka】Kafka 源码解析:Group 协调管理机制
  8. jsf如何与数据库连接_JSF数据库示例– MySQL JDBC
  9. python花瓣飘零_Python 爬虫: 抓取花瓣网图片
  10. handsome对应php文件,基于handsome主题的一些美化总结
  11. html js鼠标滚轮事件,JS事件-鼠标滚轮事件
  12. 【毕业季·进击的技术er】青春不散场 恰同学少年
  13. Java并发编程synchronized详解
  14. 通过替换音效文件,实现在安卓手机上使用wp/nokia/wm10按键键盘声音
  15. 介绍一种Android 平台 不需要获取imei imsi 无权限就能获取手机运营商的方法
  16. 天猫数据爬取解决找不到json文件的问题
  17. 基于单片机的多功能电子医药盒设计
  18. 玩转微信小程序 之 初步了解微信小程序(2019/04/05)
  19. 高标清双向变换 万能 视频格式转换器
  20. typescript是什么_为什么不应该害怕TypeScript

热门文章

  1. 【JAVA】java解析HTML代码
  2. 如何查看笔记本电脑固态硬盘接口与接口协议
  3. LCD液晶屏驱动详解
  4. 【良心教程】保姆级Python爬虫入门教程(一)——爬虫之初见
  5. 国科大2019年大数据分析课件作业 考试-程学旗 靳小龙 刘盛华
  6. 们指点如果在android平台下开启jbox2d的debugDraw功能
  7. 我国有较大的AI大模型应用市场,在应用领域具有优势
  8. springMVC+阿里云API = 实现发送手机验证码短信
  9. 龙讯|LT9721高性能HDMI/MIPItoTypeC TO DP转换器
  10. 小百对python的缺省参数的理解