2
0

Lua.hx 7.3 KB

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