lua string.dump

string.dump在lua manual 上的描述是:

string.dump (function [, strip])

Returns a string containing a binary representation (a binary chunk) of the given function, so that a later load on this string returns a copy of the function (but with new upvalues). If strip is a true value, the binary representation may not include all debug information about the function, to save space.

Functions with upvalues have only their number of upvalues saved. When (re)loaded, those upvalues receive fresh instances containing nil. (You can use the debug library to serialize and reload the upvalues of a function in a way adequate to your needs.)

返回一个字符串,该字符串包含一个函数的二进制描述,之后可以用load加载这个函数的拷贝。如果strip参数为true,那么函数拷贝为减少空间将不会包含任何调试信息。

函数只保存upvalues的数目。当重新加载时,所有的upvalue设置为nil。

举个简单的例子:

function test(n)
	print("test:",n)
end

a = string.dump(test, true);
b = load(a);
b(6)  -->test:6
可以看出函数能被当成参数进行传递!string.dump实现了函数的序列化,能在不同的作用域进行传递。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua中,string.sub函数用于截取字符串的一部分。它接受三个参数,分别是字符串s、起始位置i和结束位置j。函数会返回从第i个字符到第j个字符之间的子串。在Lua中,字符串的索引从1开始计数,而可以使用负索引从字符串的结尾向前计数。例如,string.sub(s, 1, j)将返回字符串s的长度为j的前缀,而string.sub(s, j, -1)将返回从第j个字符开始的后缀。如果只提供了前两个参数,则默认结束位置为字符串的末尾,可以简写为string.sub(s, j)。另外,使用string.sub(s, 2, -2)可以获取去除第一个和最后一个字符后的子串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Lua中的string库(字符串函数库)总结](https://blog.csdn.net/php_Zhaop/article/details/72834384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [lua socket客户端 lua string.pack字符串打包](https://blog.csdn.net/beyond706/article/details/105949783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值