傾印函數的原始碼

lua-users home
wiki

[Zarutian]:我的建議,是添加一個方式,來傾印函數的原始碼。對除錯程式和序列化器來說,將會很有用。

我使用 Tcl 好一段時間,而且我發現程序「info body <procedure name>」非常實用,我也希望 Lua 也可以做到。

string.dump(<function)>) 可以擴充為 string.dump(<function>,<sourcecode?>),如果 <sourcecode> 不是 nil,則傳回函數的原始碼。

範例

function bla (n)
  print(n)
end
print(string.dump(bla,true))

將列印

function (n)
  print(n)
end

RecentChanges · preferences
edit · history
最後編輯於格林威治時間下午 8:21 2007 年 7 月 7 日 (diff)