Std.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import lua.Boot;
  23. import lua.NativeStringTools;
  24. @:keepInit
  25. @:coreApi class Std {
  26. @:deprecated('Std.is is deprecated. Use Std.isOfType instead.')
  27. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  28. return isOfType(v, t);
  29. }
  30. public static inline function isOfType(v:Dynamic, t:Dynamic):Bool {
  31. return untyped lua.Boot.__instanceof(v, t);
  32. }
  33. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  34. return untyped lua.Boot.__instanceof(value, c) ? cast value : null;
  35. }
  36. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  37. public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  38. return downcast(value, c);
  39. }
  40. @:keep
  41. public static function string(s:Dynamic) : String {
  42. return untyped _hx_tostring(s, 0);
  43. }
  44. public static function int(x:Float):Int {
  45. if (!Math.isFinite(x) || Math.isNaN(x))
  46. return 0;
  47. else
  48. return lua.Boot.clampInt32(x);
  49. }
  50. public static function parseInt(x:String):Null<Int> {
  51. if (x == null)
  52. return null;
  53. untyped {
  54. __lua__("local sign, numString = {0}", NativeStringTools.match(x, "^%s*([%-+]?)0[xX]([%da-fA-F]*)"));
  55. if (numString != null)
  56. return switch sign {
  57. case '-': -lua.Lua.tonumber(numString, 16);
  58. case _: lua.Lua.tonumber(numString, 16);
  59. }
  60. }
  61. final intMatch = NativeStringTools.match(x, "^%s*[%-+]?%d*");
  62. if (intMatch == null)
  63. return null;
  64. return lua.Lua.tonumber(intMatch);
  65. }
  66. public static function parseFloat(x:String):Float {
  67. if (x == null || x == "")
  68. return Math.NaN;
  69. var digitMatch = NativeStringTools.match(x, "^%s*[%.%-+]?[0-9]%d*");
  70. if (digitMatch == null) {
  71. return Math.NaN;
  72. }
  73. x = x.substr(digitMatch.length);
  74. var decimalMatch = NativeStringTools.match(x, "^%.%d*");
  75. if (decimalMatch == null)
  76. decimalMatch = "";
  77. x = x.substr(decimalMatch.length);
  78. var eMatch = NativeStringTools.match(x, "^[eE][+%-]?%d+");
  79. if (eMatch == null)
  80. eMatch = "";
  81. var result = lua.Lua.tonumber(digitMatch + decimalMatch + eMatch);
  82. return result != null ? result : Math.NaN;
  83. }
  84. public static function random(x:Int):Int {
  85. return untyped x <= 0 ? 0 : Math.floor(Math.random() * x);
  86. }
  87. static function __init__():Void
  88. untyped {
  89. __feature__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
  90. __feature__("Type.resolveClass", _hxClasses["Array"] = Array);
  91. __feature__("lua.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
  92. }
  93. }