问题标签 [lua-5.1]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
182 浏览

lua - 调用 String.pack 返回未找到打包方法

我正在运行 Zerobrane 1.8 和 LUA 5.3。但是,在解释器中,字符串函数没有“pack()”方法/子函数,即

返回错误:“打包方法为零。”

我已经在其他在线版本的 lua 解释器中尝试过,但遇到了同样的问题。然而,参考手册

https://www.lua.org/manual/5.3/manual.html#pdf-string.pack 清楚地表明这个函数应该存在。我在这里想念什么?

0 投票
2 回答
288 浏览

regex - Match repeatable string as a "whole word" in Lua 5.1

My environment:

  • Lua 5.1
  • Absolutely no libraries with a native component (like a C .so/.dll) can be used
  • I can run any arbitrary pure Lua 5.1 code, but I can't access os and several other packages that would allow access to the native filesystem, shell commands or anything like that, so all functionality must be implemented in Lua itself (only).
  • I've already managed to pull in LuLpeg. I can probably pull in other pure Lua libraries.

I need to write a function that returns true if the input string matches an arbitrary sequence of letters and numbers as a whole word that repeats one or more times, and may have punctuation at the beginning or end of the entire matching substring. I use "whole word" in the same sense as the PCRE word boundary \b.

To demonstrate the idea, here's an incorrect attempt using the re module of LuLpeg; it seems to work with negative lookaheads but not negative lookbehinds:

Here are example strings and the expected return value (the quotes are syntactical as if typed into the Lua interpreter, not literal parts of the string; this is done to make trailing/leading spaces obvious):

  • input: " one !tvtvtv! two", word: tv, return value: true
  • input: "I'd", word: d, return value: false
  • input: "tv", word: tv, return value: true
  • input: " tvtv! ", word: tv, return value: true
  • input: " epon ", word: nope, return value: false
  • input: " eponnope ", word: nope, return value: false
  • input: "atv", word: tv, return value: false

If I had a full PCRE regex library I could do this quickly, but I don't because I can't link to C, and I haven't found any pure Lua implementations of PCRE or similar.

I'm not certain if LPEG is flexible enough (using LPEG directly or through its re module) to do what I want, but I'm pretty sure the built-in Lua functions can't do what I want, because it can't handle repeating sequences of characters. (tv)+ does not work with Lua's builtin string:match function and similar.

Interesting resources I've been scouring to try to figure out how to do this, to no avail:

0 投票
1 回答
318 浏览

string - 如何在 LUA 5.1 中分隔字符串中没有空格的表情符号

我想使用 LUA 5.1 的拆分功能来拆分不带空格的表情符号字符串并在它们之间添加空格,但我不能正确地做到这一点。所以我这样做是这样的,但它是错误的:

在浏览器 Firefox 65 中查看!

我的错误结果:⌚ ↔

等待结果:‍‍‍ ‍‍ ⌚ ↔</p>

0 投票
3 回答
791 浏览

lua - 如何在 Lua 中使用反射(在运行时)?

我正在尝试在 Lua 中使用 Busted(我没有编写,也不允许我此时出于业务原因重构)对代码进行单元测试,并且在这个类模块中没有任何概念,也没有依赖注入。因此,我想替换文件顶部所需的一些模块,即local log = require("path.to.module.logger"):new()用我制作的用于跟踪方法调用次数的模拟记录器,logger:trace()例如times()Java 中的Mockito 中的with。在 Java 中,我可以为此目的使用 Reflection.Utils。Lua 中的什么等价物可以帮助使这个不可测试的代码可测试?

我已经尝试log使用此示例创建一个具有相同变量名的全局变量并将其设置为等于我的模拟:https ://www.lua.org/pil/14.2.html

本地 _M = {}

本地日志 = require("path.to.module.logger"):new()

...

function _M.init(...) log:trace("debug") # 我希望这个日志实例不是上面的那个,而是我在运行时注入到模块中的那个

0 投票
1 回答
450 浏览

lua - LUA:将对象插入表会覆盖表的其他成员

我需要在 Lua 中创建一个对象表。但是当我将一个对象插入到表中时,之前插入的所有成员都会更改值。所有这些对象的哈希码是不同的

如何在不影响其他成员的情况下插入对象?我在网上搜索过,我认为这是在 5.2 中修复的。但我需要使用5.1。

有人可以帮忙吗,谢谢

解决了

0 投票
1 回答
1236 浏览

nginx - 将嵌套表存储在 nginx 共享内存中

我正在使用 open-resty 和 lua 创建一个用于重定向请求的服务器。重定向是基于来自 lua 数据树结构(嵌套表)的一些数据完成的

我正在寻找一种方法来在启动时填充这些数据,然后在工作人员之间共享数据。

ngx.ctx 可以保存任意数据,但仅在请求期间持续。共享字典持续到最后,但只能保存原语列表。

我读过可以跨 lua 模块共享数据。因为模块在启动时只被实例化一次。代码是这样的

然后在 nginx.conf

这第三种可能性线程安全吗?还有其他东西可以实现这一目标吗?

这方面的文档不多,这就是为什么在这里发布它。任何信息都会有所帮助,谢谢

0 投票
0 回答
96 浏览

lua - 在沙箱中加载非标准 lua 库(Lua 5.1/Luajit)

我正在尝试让自己熟悉 lua,为此我正在玩编码游戏 berrybots ( http://berrybots.com/ )。它使用 C++ 引擎,用户/玩家可以提供 lua 脚本来编写自己的“机器人 AI”来对抗其他机器人。

我正在编写自己的机器人,但我想使用一些更多的数字密集型想法,所以我需要导入/需要一个外部数字库(我猜是 numlua,https://github.com/carvalho/numlua)沙盒用户脚本。我不介意修改 berrybots 的原始代码以使其工作,并且我已经假设在沙箱中没有办法解决这个问题(但也许有?)。

在 C++ 端启动沙箱并加载标准库

我也想加载 numlua 库,我通常可以在任何 lua 脚本中使用

但显然不在沙箱中。

我知道那luaL_register是用来注册 C 函数的,但我无法让它工作,我认为应该有一种更简单的方法来导入 numlua,而无需返回到 numlua 的 C API。

我不在乎该解决方案是否会带来安全风险,因为我只打算自己使用它。为了完整起见,拥有(合理)安全的解决方案会很好。

我是 lua 新手(主要熟悉 Python/Cython/C),所以如果我忽略了一些明显的东西,请原谅我。

0 投票
2 回答
467 浏览

lua - 为什么 Lua 的 newproxy 被弃用和删除?

所以在 Lua 的 5.1 中,newproxy被弃用了。在 5.2 中,它被删除了。但为什么?我现在应该使用什么来构建用户数据?

我正在尝试实现伪类以获得乐趣,并且用户数据更容易。它们使实现不变性和限制对某些键的写入变得更加容易。

0 投票
2 回答
406 浏览

lua - 如何使lua搜索模块与首先调用require的模块位于同一文件夹中?

说我有一个项目文件夹,例如:

其中main.lua

a.lua我只使用字符串"b",试图告诉 lua - 找到一个文件,其名称b.lua在 first 的同一文件夹中a.lua

b.lua

然后我运行命令lua main.lua并得到错误:

我知道像这样package.path = package.path..';'..'lib/?.lua'的解决方案,但是如果结构更改为:

我不想再次修改 package.path。不管结构是什么,require("b")ina.lua总是会让luab在first的同一个文件夹中搜索a.lua

0 投票
2 回答
204 浏览

regex - 在 lua 5.1 中使用 string.gmatch 拆分字符串时包含空匹配项

我有一个逗号分隔的输入字符串,需要支持空条目。所以一个类似的字符串a,b,c,,d应该产生一个有 5 个条目的表,其中第 4 个是一个空值。

一个简化的例子

此代码输出

9

在 Lua 5.1 中,虽然只有 5 个条目。

我可以将*正则表达式中的 更改为+- 然后它报告 4 个条目a,b,c,d,但不是空的。似乎这种行为已在 Lua 5.2 中修复,因为上面的代码在 lua 5.2 中运行良好,但我不得不为 lua 5.1 找到解决方案

我目前的实现

有关如何修复的任何建议?