Lua.hx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package lua;
  2. import haxe.extern.Rest;
  3. import haxe.Constraints.Function;
  4. import haxe.extern.Rest;
  5. /**
  6. These are all global static methods within Lua.
  7. **/
  8. @:native("_G")
  9. extern class Lua {
  10. /**
  11. A global variable that holds a string containing the current interpreter
  12. version.
  13. **/
  14. public static var _VERSION : String;
  15. public static var arg : Table<Int, String>;
  16. /**
  17. Pushes onto the stack the metatable in the registry.
  18. **/
  19. public static function getmetatable(tbl:Table<Dynamic,Dynamic>): Table<Dynamic,Dynamic>;
  20. /**
  21. Pops a table from the stack and sets it as the new metatable for the value
  22. at the given acceptable index.
  23. **/
  24. public static function setmetatable(tbl:Table<Dynamic,Dynamic>, mtbl: Table<Dynamic, Dynamic>): Table<Dynamic, Dynamic>;
  25. /**
  26. Pops a table from the stack and sets it as the new environment for the value
  27. at the given index. If the value at the given index is neither a function nor
  28. a thread nor a userdata, lua_setfenv returns `0`.
  29. Otherwise it returns `1`.
  30. **/
  31. public static function setfenv(i:Int , tbl:Table<Dynamic, Dynamic>): Void;
  32. /**
  33. Allows a program to traverse all fields of a table.
  34. Its first argument is a table and its second argument is an index in this
  35. table. `next` returns the next index of the table and its associated value.
  36. When `i` is `null`, `next` returns an initial index and its associated value.
  37. When called with the last index, or with `null` in an empty table, `next`
  38. returns `null`. In particular, you can use `next(t)` to check whether a
  39. table is empty.
  40. The order in which the indices are enumerated is not specified, even for
  41. numeric indices. (To traverse a table in numeric order, use a numerical for
  42. or the `ipairs` function).
  43. The behavior of next is undefined if, during the traversal, any value
  44. to a non-existent field in the table is assigned. Existing fields may
  45. however be modified. In particular, existing fields may be cleared.
  46. **/
  47. public static function next<K,V>(k:Table<K, V>, ?i : K): NextResult<K,V>;
  48. /**
  49. Receives an argument of any type and converts it to a string in a reasonable
  50. format.
  51. For complete control of how numbers are converted, use`NativeStringTools.format`.
  52. **/
  53. public static function tostring(v:Dynamic): String;
  54. public static function ipairs<K,V>(t:Table<K,V>): IPairsResult<K,V>;
  55. public static function pairs<K,V>(t:Table<K,V>): PairsResult<K,V> ;
  56. public static function require(module:String) : Dynamic;
  57. /**
  58. Converts the Lua value at the given acceptable base to `Int`.
  59. The Lua value must be a number or a string convertible to a number,
  60. otherwise `tonumber` returns `0`.
  61. **/
  62. public static function tonumber(str:String, ?base:Int): Int;
  63. /**
  64. Returns the Lua type of its only argument as a string.
  65. The possible results of this function are:
  66. * `"nil"` (a string, not the Lua value nil),
  67. * `"number"`
  68. * `"string"`
  69. * `"boolean"`
  70. * `"table"`
  71. * `"function"`
  72. * `"thread"`
  73. * `"userdata"`
  74. **/
  75. public static function type(v:Dynamic) : String;
  76. /**
  77. Receives any number of arguments, and prints their values to stdout,
  78. using the tostring function to convert them to strings.
  79. `print` is not intended for formatted output, but only as a quick way to show
  80. a value, typically for debugging.
  81. For complete control of how numbers are converted, use `NativeStringTools.format`.
  82. **/
  83. public static function print(v:haxe.extern.Rest<Dynamic>) : Void;
  84. /**
  85. If `n` is a number, returns all arguments after argument number `n`.
  86. Otherwise, `n` must be the string `"#"`, and select returns the total
  87. number of extra arguments it received.
  88. **/
  89. public static function select(n:Dynamic, rest:Rest<Dynamic>) : Dynamic;
  90. /**
  91. Gets the real value of `table[index]`, without invoking any metamethod.
  92. **/
  93. public static function rawget<K,V>(t:Table<K,V>, k:K) : V;
  94. /**
  95. Sets the real value of `table[index]` to value, without invoking any metamethod.
  96. **/
  97. public static function rawset<K,V>(t:Table<K,V>, k:K, v:V) : Void;
  98. /**
  99. This function is a generic interface to the garbage collector.
  100. It performs different functions according to its first argument.
  101. **/
  102. public static function collectgarbage(opt:CollectGarbageOption, ?arg:Int) : Int;
  103. /**
  104. Issues an error when the value of its argument `v` is `false` (i.e., `null`
  105. or `false`) otherwise, returns all its arguments. message is an error message.
  106. when absent, it defaults to "assertion failed!"
  107. **/
  108. public static function assert<T>(v:T, ?message:String) : T;
  109. /**
  110. Loads and runs the given file.
  111. **/
  112. public static function dofile(filename:String) : Void;
  113. /**
  114. Generates a Lua error. The error message (which can actually be a Lua value
  115. of any type) must be on the stack top. This function does a long jump,
  116. and therefore never returns.
  117. **/
  118. public static function error(message:String, ?level:Int) : Void;
  119. /**
  120. Calls a function in protected mode.
  121. **/
  122. public static function pcall(f:Function, rest:Rest<Dynamic>) : PCallResult;
  123. /**
  124. Returns `true` if the two values in acceptable indices `v1` and `v2` are
  125. primitively equal (that is, without calling metamethods).
  126. Otherwise returns `false`.
  127. Also returns `false` if any of the indices are non valid.
  128. **/
  129. public static function rawequal(v1:Dynamic, v2:Dynamic) : Bool;
  130. /**
  131. This function is similar to pcall, except that you can set a new error
  132. handler.
  133. **/
  134. public static function xpcall(f:Function, msgh:Function, rest:Rest<Dynamic> ) : PCallResult;
  135. /**
  136. Loads the chunk from file filename or from the standard input if no filename
  137. is given.
  138. **/
  139. public static function loadfile(filename:String) : LoadResult;
  140. /**
  141. Loads the chunk from given string.
  142. **/
  143. public static function load(code:haxe.extern.EitherType<String,Void->String>) : LoadResult;
  144. }
  145. /**
  146. Enum for describing garbage collection options
  147. **/
  148. @:enum
  149. abstract CollectGarbageOption(String) {
  150. var Stop = "stop";
  151. var Restart = "restart";
  152. var Collect = "collect";
  153. var Count = "count";
  154. var Step = "step";
  155. var SetPause = "setpause";
  156. var SetStepMul = "setstepmul";
  157. }
  158. @:multiReturn
  159. extern class PCallResult {
  160. var status : Bool;
  161. var value : Dynamic;
  162. }
  163. @:multiReturn
  164. extern class NextResult<K,V> {
  165. var index : K;
  166. var value : V;
  167. }
  168. @:multiReturn
  169. extern class IPairsResult<K,V> {
  170. var next : Table<K,V>->Int->NextResult<Int,V>;
  171. var table : Table<K,V>;
  172. var index : Int;
  173. }
  174. @:multiReturn
  175. extern class PairsResult<K,V> {
  176. var next : Table<K,V>->K->NextResult<K,V>;
  177. var table : Table<K,V>;
  178. var index : K;
  179. }
  180. @:multiReturn
  181. extern class LoadResult {
  182. var func : Function;
  183. var message : String;
  184. }