BasicMethods.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using MoonSharp.Interpreter.Execution;
  7. namespace MoonSharp.Interpreter.CoreLib
  8. {
  9. [MoonSharpModule]
  10. public class BasicMethods
  11. {
  12. //type (v)
  13. //----------------------------------------------------------------------------------------------------------------
  14. //Returns the type of its only argument, coded as a string. The possible results of this function are "nil"
  15. //(a string, not the value nil), "number", "string", "boolean", "table", "function", "thread", and "userdata".
  16. [MoonSharpMethod]
  17. public static DynValue type(ScriptExecutionContext executionContext, CallbackArguments args)
  18. {
  19. DynValue v = args[0];
  20. return DynValue.NewString(v.Type.ToLuaTypeString());
  21. }
  22. //assert (v [, message])
  23. //----------------------------------------------------------------------------------------------------------------
  24. //Issues an error when the value of its argument v is false (i.e., nil or false);
  25. //otherwise, returns all its arguments. message is an error message; when absent, it defaults to "assertion failed!"
  26. [MoonSharpMethod]
  27. public static DynValue assert(ScriptExecutionContext executionContext, CallbackArguments args)
  28. {
  29. DynValue v = args[0];
  30. DynValue message = args[1];
  31. if (!v.CastToBool())
  32. {
  33. if (message.IsNil())
  34. throw new ScriptRuntimeException("assertion failed!");
  35. else
  36. throw new ScriptRuntimeException(message.ToPrintString());
  37. }
  38. return DynValue.Nil;
  39. }
  40. // collectgarbage ([opt [, arg]])
  41. // ----------------------------------------------------------------------------------------------------------------
  42. // This function is mostly a stub towards the CLR GC. If mode is nil, "collect" or "restart", a GC is forced.
  43. [MoonSharpMethod]
  44. public static DynValue collectgarbage(ScriptExecutionContext executionContext, CallbackArguments args)
  45. {
  46. DynValue opt = args[0];
  47. string mode = opt.CastToString();
  48. if (mode == null || mode == "collect" || mode == "restart")
  49. GC.Collect(2, GCCollectionMode.Forced);
  50. return DynValue.Nil;
  51. }
  52. // error (message [, level])
  53. // ----------------------------------------------------------------------------------------------------------------
  54. // Terminates the last protected function called and returns message as the error message. Function error never returns.
  55. // Usually, error adds some information about the error position at the beginning of the message.
  56. // The level argument specifies how to get the error position.
  57. // With level 1 (the default), the error position is where the error function was called.
  58. // Level 2 points the error to where the function that called error was called; and so on.
  59. // Passing a level 0 avoids the addition of error position information to the message.
  60. [MoonSharpMethod]
  61. public static DynValue error(ScriptExecutionContext executionContext, CallbackArguments args)
  62. {
  63. DynValue message = args.AsType(0, "dofile", DataType.String, false);
  64. throw new ScriptRuntimeException(message.String);
  65. }
  66. // tostring (v)
  67. // ----------------------------------------------------------------------------------------------------------------
  68. // Receives a value of any type and converts it to a string in a reasonable format. (For complete control of how
  69. // numbers are converted, use string.format.)
  70. //
  71. // If the metatable of v has a "__tostring" field, then tostring calls the corresponding value with v as argument,
  72. // and uses the result of the call as its result.
  73. [MoonSharpMethod]
  74. public static DynValue tostring(ScriptExecutionContext executionContext, CallbackArguments args)
  75. {
  76. DynValue v = args[0];
  77. DynValue tail = executionContext.GetMetamethodTailCall(v, "__tostring", v);
  78. if (tail == null || tail.IsNil())
  79. return DynValue.NewString(v.ToPrintString());
  80. tail.TailCallData.Continuation = new CallbackFunction(__tostring_continuation);
  81. return tail;
  82. }
  83. private static DynValue __tostring_continuation(ScriptExecutionContext executionContext, CallbackArguments args)
  84. {
  85. DynValue b = args[0].ToScalar();
  86. if (b.IsNil())
  87. return b;
  88. if (b.Type != DataType.String)
  89. throw new ScriptRuntimeException("'tostring' must return a string");
  90. return b;
  91. }
  92. // tonumber (e [, base])
  93. // ----------------------------------------------------------------------------------------------------------------
  94. // When called with no base, tonumber tries to convert its argument to a number. If the argument is already
  95. // a number or a string convertible to a number (see §3.4.2), then tonumber returns this number; otherwise,
  96. // it returns nil.
  97. //
  98. // When called with base, then e should be a string to be interpreted as an integer numeral in that base.
  99. // The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either
  100. // upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. If the
  101. // string e is not a valid numeral in the given base, the function returns nil.
  102. [MoonSharpMethod]
  103. public static DynValue tonumber(ScriptExecutionContext executionContext, CallbackArguments args)
  104. {
  105. DynValue e = args[0];
  106. DynValue b = args.AsType(1, "tonumber", DataType.Number, true);
  107. if (b.IsNil())
  108. {
  109. if (e.Type == DataType.Number)
  110. return e;
  111. if (e.Type != DataType.String)
  112. return DynValue.Nil;
  113. double d;
  114. if (double.TryParse(e.String, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
  115. {
  116. return DynValue.NewNumber(d);
  117. }
  118. return DynValue.Nil;
  119. }
  120. else
  121. {
  122. //!COMPAT: tonumber supports only 2,8,10 or 16 as base
  123. DynValue ee = args.AsType(0, "tonumber", DataType.String, false);
  124. int bb = (int)b.Number;
  125. uint uiv = Convert.ToUInt32(ee.String, bb);
  126. return DynValue.NewNumber(uiv);
  127. }
  128. }
  129. [MoonSharpMethod]
  130. public static DynValue print(ScriptExecutionContext executionContext, CallbackArguments args)
  131. {
  132. StringBuilder sb = new StringBuilder();
  133. for (int i = 0; i < args.Count; i++)
  134. {
  135. if (i != 0)
  136. sb.Append('\t');
  137. if ((args[i].Type == DataType.Table) && (args[i].Table.MetaTable != null) &&
  138. (args[i].Table.MetaTable.RawGet("__tostring") != null))
  139. {
  140. var v = executionContext.GetOwnerScript().Call(args[i].Table.MetaTable.RawGet("__tostring"), args[i]);
  141. if (v.Type != DataType.String)
  142. throw new ScriptRuntimeException("'tostring' must return a string to 'print'");
  143. sb.Append(v.ToPrintString());
  144. }
  145. else
  146. {
  147. sb.Append(args[i].ToPrintString());
  148. }
  149. }
  150. executionContext.GetOwnerScript().DebugPrint(sb.ToString());
  151. return DynValue.Nil;
  152. }
  153. }
  154. }