ScriptRuntimeException.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Antlr4.Runtime.Tree;
  6. namespace MoonSharp.Interpreter
  7. {
  8. [Serializable]
  9. public class ScriptRuntimeException : InterpreterException
  10. {
  11. public ScriptRuntimeException(Exception ex)
  12. : base(ex)
  13. {
  14. }
  15. public ScriptRuntimeException(string format, params object[] args)
  16. : base(format, args)
  17. {
  18. }
  19. public static ScriptRuntimeException ArithmeticOnNonNumber(DynValue l, DynValue r = null)
  20. {
  21. if (l.Type != DataType.Number && l.Type != DataType.String)
  22. return new ScriptRuntimeException("attempt to perform arithmetic on a {0} value", l.Type.ToLuaTypeString());
  23. else if (r != null && r.Type != DataType.Number && r.Type != DataType.String)
  24. return new ScriptRuntimeException("attempt to perform arithmetic on a {0} value", r.Type.ToLuaTypeString());
  25. else if (l.Type == DataType.String || (r != null && r.Type == DataType.String))
  26. return new ScriptRuntimeException("attempt to perform arithmetic on a string value");
  27. else
  28. throw new InternalErrorException("ArithmeticOnNonNumber - both are numbers/strings");
  29. }
  30. public static ScriptRuntimeException ConcatOnNonString(DynValue l, DynValue r)
  31. {
  32. if (l.Type != DataType.Number && l.Type != DataType.String)
  33. return new ScriptRuntimeException("attempt to concatenate a {0} value", l.Type.ToLuaTypeString());
  34. else if (r != null && r.Type != DataType.Number && r.Type != DataType.String)
  35. return new ScriptRuntimeException("attempt to concatenate a {0} value", r.Type.ToLuaTypeString());
  36. else
  37. throw new InternalErrorException("ConcatOnNonString - both are numbers/strings");
  38. }
  39. public static ScriptRuntimeException LenOnInvalidType(DynValue r)
  40. {
  41. return new ScriptRuntimeException("attempt to get length of a {0} value", r.Type.ToLuaTypeString());
  42. }
  43. public static ScriptRuntimeException CompareInvalidType(DynValue l, DynValue r)
  44. {
  45. if (l.Type.ToLuaTypeString() == r.Type.ToLuaTypeString())
  46. return new ScriptRuntimeException("attempt to compare two {0} values", l.Type.ToLuaTypeString());
  47. else
  48. return new ScriptRuntimeException("attempt to compare {0} with {1}", l.Type.ToLuaTypeString(), r.Type.ToLuaTypeString());
  49. }
  50. public static ScriptRuntimeException BadArgument(int argNum, string funcName, DataType expected, DataType got, bool allowNil)
  51. {
  52. return BadArgument(argNum, funcName, expected.ToString().ToLowerInvariant(), got.ToString().ToLowerInvariant(), allowNil);
  53. }
  54. public static ScriptRuntimeException BadArgument(int argNum, string funcName, string expected, string got, bool allowNil)
  55. {
  56. return new ScriptRuntimeException("bad argument #{0} to '{1}' ({2}{3} expected, got {4})",
  57. argNum + 1, funcName, allowNil ? "nil or " : "", expected, got);
  58. }
  59. public static ScriptRuntimeException IndexType(DynValue obj)
  60. {
  61. return new ScriptRuntimeException("attempt to index a {0} value", obj.Type.ToLuaTypeString());
  62. }
  63. public static ScriptRuntimeException LoopInIndex()
  64. {
  65. return new ScriptRuntimeException("loop in gettable");
  66. }
  67. public static ScriptRuntimeException LoopInNewIndex()
  68. {
  69. return new ScriptRuntimeException("loop in settable");
  70. }
  71. public static ScriptRuntimeException TableIndexIsNil()
  72. {
  73. return new ScriptRuntimeException("table index is nil");
  74. }
  75. public static ScriptRuntimeException TableIndexIsNaN()
  76. {
  77. return new ScriptRuntimeException("table index is NaN");
  78. }
  79. public static ScriptRuntimeException ConvertToNumberFailed(int stage)
  80. {
  81. switch (stage)
  82. {
  83. case 1:
  84. return new ScriptRuntimeException("'for' initial value must be a number");
  85. case 2:
  86. return new ScriptRuntimeException("'for' step must be a number");
  87. case 3:
  88. return new ScriptRuntimeException("'for' limit must be a number");
  89. default:
  90. return new ScriptRuntimeException("value must be a number");
  91. }
  92. }
  93. public static ScriptRuntimeException ConvertObjectFailed(object obj)
  94. {
  95. return new ScriptRuntimeException("cannot convert clr type {0}", obj.GetType());
  96. }
  97. public static ScriptRuntimeException ConvertObjectFailed(DataType t)
  98. {
  99. return new ScriptRuntimeException("cannot convert a {0} to a clr type", t.ToString().ToLowerInvariant());
  100. }
  101. public static ScriptRuntimeException ConvertObjectFailed(DataType t, Type t2)
  102. {
  103. return new ScriptRuntimeException("cannot convert a {0} to a clr type {1}", t.ToString().ToLowerInvariant(), t2.FullName);
  104. }
  105. public static ScriptRuntimeException UserDataArgumentTypeMismatch(DataType t, Type clrType)
  106. {
  107. return new ScriptRuntimeException("cannot find a conversion from a moon# {0} to a clr {1}", t.ToString().ToLowerInvariant(), clrType.FullName);
  108. }
  109. public static ScriptRuntimeException UserDataMissingField(string typename, string fieldname)
  110. {
  111. return new ScriptRuntimeException("cannot access field {0} of userdata<{1}>", fieldname, typename);
  112. }
  113. }
  114. }