Lua (十)函数尾调用

概念:在一个函数A中使用return返回另一个函数B(B函数调用是A函数的最后一个动作)。

尾调用不占用“堆栈”空间,不会出现“栈溢出”,可以起到优化存储空间的作用。

function FA()
	return math.abs(-1)
end

function FB()
	return FA()
end

print(FB())

--->
1
function Fun(num)
	if num > 0 then
		print(num)		
		return Fun(num-1)
	else
			return "END"
	end			
end

print(Fun(5))

--->
5
4
3
2
1
END
function FunA()
		return FunB()
end

function FunB()
	return 1,2
end

a,b = FunB()
print(a)
print(b)


function FunC()
		return (FunD())
end

function FunD()
	return 1,2
end
c,d = FunC()
print(c)
print(d)

--->
1
2
1
nil

 

### 统计 Lua 函数调用次数的方法 为了实现对 Lua 函数调用次数的统计,在 Unity 中可以采用拦截 Lua 函数调用的方式。通过修改 Lua 函数获取逻辑,使得每次调用特定 Lua 函数时都会触发一次计数操作。 #### 方法一:自定义封装 Lua 函数调用接口 可以在 C# 层面对 `LuaFunction.Call` 进行一层简单的包装,加入计数机制: ```csharp using System; using LuaInterface; public static class LuaCallCounter { private static Dictionary<LuaFunction, int> callCounts = new Dictionary<LuaFunction, int>(); public static void CallWithCount(LuaFunction func, params object[] args) { if (!callCounts.ContainsKey(func)) { callCounts[func] = 0; } callCounts[func]++; Console.WriteLine($"Calling function {func.Name}, total calls: {callCounts[func]}"); try { func.Call(args); } catch (Exception e) { Debug.LogError(e.Message); } } public static int GetCallCount(LuaFunction func) { return callCounts.TryGetValue(func, out var count) ? count : 0; } } ``` 此代码片段展示了如何创建一个静态类 `LuaCallCounter` 来跟踪不同 Lua 函数被调用的情况[^1]。 当需要调用 Lua 函数时不再直接使用 `luaFunction.Call()`,而是改为调用上述封装好的 `LuaCallCounter.CallWithCount(luaFunction)` 方法[^2]。 对于想要监控其调用频率的具体 Lua 函数,则只需记录下对应的 `LuaFunction` 实例即可随时查询该函数已被调用了多少次[^3]。 #### 方法二:利用 AOP 或者代理模式增强原有 API 行为 如果项目规模较大或者不希望改动现有业务逻辑太多的话,还可以考虑借助面向切面编程(AOP)的思想或者是动态代理技术来透明地增加日志/性能分析等功能,比如在每次实际执行前自动插入计数语句而不必显式更改源码中每处可能出现的位置[^4]。 不过这种方法相对复杂一些,通常适用于更专业的场景需求之下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值