lua string.dump

来源:互联网 发布:00后直播软件 编辑:程序博客网 时间:2024/06/05 06:44

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

0 0
原创粉丝点击