您的位置:首页 > 编程语言 > Lua

lua string.dump

2015-06-26 18:16 1141 查看
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实现了函数的序列化,能在不同的作用域进行传递。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: