lua string.dump

本文介绍了Lua中string.dump函数的使用,用于序列化函数并能在不同作用域间传递。详细解释了函数复制、upvalues及调试信息的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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实现了函数的序列化,能在不同的作用域进行传递。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值