Std.hx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C)2005-2017 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. import lua.Boot;
  23. import lua.NativeStringTools;
  24. @:keepInit
  25. @:coreApi class Std {
  26. public static inline function is( v : Dynamic, t : Dynamic ) : Bool {
  27. return untyped lua.Boot.__instanceof(v,t);
  28. }
  29. public static inline function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  30. return untyped lua.Boot.__instanceof(value, c) ? cast value : null;
  31. }
  32. @:keep
  33. public static function string( s : Dynamic ) : String {
  34. return untyped lua.Boot.__string_rec(s);
  35. }
  36. public static function int( x : Float ) : Int {
  37. if (!Math.isFinite(x) || Math.isNaN(x)) return 0;
  38. else return lua.Boot.clamp(x);
  39. }
  40. public static function parseInt( x : String ) : Null<Int> {
  41. if (x == null) return null;
  42. var hexMatch = NativeStringTools.match(x, "^ *[%-+]*0[xX][%da-FA-F]*");
  43. if (hexMatch != null){
  44. return lua.Lua.tonumber(hexMatch.substr(2), 16);
  45. } else {
  46. var intMatch = NativeStringTools.match(x, "^ *[%-+]?%d*");
  47. if (intMatch != null){
  48. return lua.Lua.tonumber(intMatch);
  49. } else {
  50. return null;
  51. }
  52. }
  53. }
  54. public static function parseFloat( x : String ) : Float {
  55. if (x == null || x == "") return Math.NaN;
  56. var digitMatch = NativeStringTools.match(x, "^ *[%.%-+]?[0-9]%d*");
  57. if (digitMatch == null){
  58. return Math.NaN;
  59. }
  60. x = x.substr(digitMatch.length);
  61. var decimalMatch = NativeStringTools.match(x, "^%.%d*");
  62. if (decimalMatch == null) decimalMatch = "";
  63. x = x.substr(decimalMatch.length);
  64. var eMatch = NativeStringTools.match(x, "^[eE][+%-]?%d+");
  65. if (eMatch == null) eMatch = "";
  66. var result = lua.Lua.tonumber(digitMatch + decimalMatch + eMatch);
  67. return result != null ? result : Math.NaN;
  68. }
  69. public static function random( x : Int ) : Int {
  70. return untyped x <= 0 ? 0 : Math.floor(Math.random()*x);
  71. }
  72. static function __init__() : Void untyped {
  73. // lua workarounds for basic anonymous object functionality
  74. haxe.macro.Compiler.includeFile("lua/_lua/_hx_anon.lua");
  75. // class reflection metadata
  76. haxe.macro.Compiler.includeFile("lua/_lua/_hx_classes.lua");
  77. __feature__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", __lua_table__(["String"]),true));
  78. __feature__("Type.resolveClass",_hxClasses["Array"] = Array);
  79. __feature__("lua.Boot.isClass",Array.__name__ = __feature__("Type.getClassName",__lua_table__(["Array"]),true));
  80. }
  81. }