Std.hx 3.5 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. return lua.Boot.clamp(x);
  38. }
  39. public static function parseInt( x : String ) : Null<Int> {
  40. if (x == null) return null;
  41. var hexMatch = NativeStringTools.match(x, "^ *[%-+]*0[xX][%da-FA-F]*");
  42. if (hexMatch != null){
  43. return lua.Lua.tonumber(hexMatch.substr(2), 16);
  44. } else {
  45. var intMatch = NativeStringTools.match(x, "^ *[%-+]?%d*");
  46. if (intMatch != null){
  47. return lua.Lua.tonumber(intMatch);
  48. } else {
  49. return null;
  50. }
  51. }
  52. }
  53. public static function parseFloat( x : String ) : Float {
  54. if (x == null || x == "") return Math.NaN;
  55. var digitMatch = NativeStringTools.match(x, "^ *[%.%-+]?[0-9]%d*");
  56. if (digitMatch == null){
  57. return Math.NaN;
  58. }
  59. x = x.substr(digitMatch.length);
  60. var decimalMatch = NativeStringTools.match(x, "^%.%d*");
  61. if (decimalMatch == null) decimalMatch = "";
  62. x = x.substr(decimalMatch.length);
  63. var eMatch = NativeStringTools.match(x, "^[eE][+%-]?%d+");
  64. if (eMatch == null) eMatch = "";
  65. var result = lua.Lua.tonumber(digitMatch + decimalMatch + eMatch);
  66. return result != null ? result : Math.NaN;
  67. }
  68. public static function random( x : Int ) : Int {
  69. return untyped x <= 0 ? 0 : Math.floor(Math.random()*x);
  70. }
  71. static function __init__() : Void untyped {
  72. // lua workarounds for basic anonymous object functionality
  73. haxe.macro.Compiler.includeFile("lua/_lua/_hx_anon.lua");
  74. // class reflection metadata
  75. haxe.macro.Compiler.includeFile("lua/_lua/_hx_classes.lua");
  76. __feature__("lua.Boot.getClass", String.prototype.__class__ = __feature__("Type.resolveClass",_hxClasses["String"] = String,String));
  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. }