public void MathOperation(LuaState luaState, TokenType opType) { LuaValue luaValue1 = luaState.Pop(); LuaValue luaValue2 = luaState.Pop(); luaState.Push(operatorDic[opType](luaValue1, luaValue2)); try { luaState.Push(operatorDic[opType](luaValue1, luaValue2)); } catch (Exception e) { LuaValue result = null; try { if (luaState.CallMetaFunc(luaValue1, luaValue2, GetMetaFuncName(opType), out result)) { luaState.Push(result); } else { throw new Exception("没有找到对应的元方法进行计算!"); } } catch (Exception ee) { throw e; } } }
public void Len(LuaState luaState, int index) { var value = luaState.Get(index); if (value.Type == LuaValueType.String) { luaState.Push(new LuaValue((double)((value.OValue as string).Length))); } else if (value.Type == LuaValueType.Table) { luaState.Push(new LuaValue((double)((value.OValue as Table.LuaTable).Len()))); } else { LuaValue result = null; try { if (luaState.CallMetaFunc(value, value, GetMetaFuncName(TokenType.Len), out result)) { luaState.Push(result); } } catch { throw new Exception("无法计算该变量长度!"); } } }
/// <summary> /// 连续为寄存器置n个nil值,寄存器起始索引为a,数量为b,c无效 /// </summary> /// <param name="i"></param> public void LoadNil(Instruction i) { int a = 0, b = 0, c = 0; i.ABC(ref a, ref b, ref c); luaState.Push(new LuaValue()); for (int j = a + 1; j <= a + b; j++) { luaState.CopyTo(-1, j); } luaState.Pop(1); }
public void Concat(LuaState luaState, int number) { if (number == 0) { luaState.Push(new LuaValue("", LuaValueType.String)); } else if (number >= 2) { while (number > 1) { string str1 = null; string str2 = null; if (luaState.Get(luaState.AbsIndex(-1)).ToString(ref str1) && luaState.Get(luaState.AbsIndex(-2)).ToString(ref str2)) { luaState.Pop(2); luaState.Push(new LuaValue(str1 + str2, LuaValueType.String)); --number; continue; } else { LuaValue result = null; try { var b = luaState.Pop(); var a = luaState.Pop(); if (luaState.CallMetaFunc(a, b, GetMetaFuncName(TokenType.Len), out result)) { luaState.Push(result); } } catch { throw new Exception("该类型的变量无法进行拼接"); } } } } else { throw new Exception("该类型的变量无法进行拼接"); } }