Std.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 js.Boot;
  23. import js.Syntax;
  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 @:privateAccess js.Boot.__instanceof(v, t);
  32. }
  33. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S@:privateAccess {
  34. return if (js.Boot.__downcastCheck(value, c)) cast value else 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. @:pure
  41. public static function string(s:Dynamic):String {
  42. return @:privateAccess js.Boot.__string_rec(s, "");
  43. }
  44. public static inline function int(x:Float):Int {
  45. return (cast x) | 0;
  46. }
  47. @:pure
  48. public static function parseInt(x:String):Null<Int> {
  49. if(x != null) {
  50. for(i in 0...x.length) {
  51. var c = StringTools.fastCodeAt(x, i);
  52. if(c <= 8 || (c >= 14 && c != ' '.code && c != '-'.code)) {
  53. var nc = StringTools.fastCodeAt(x, i + 1);
  54. var v = js.Lib.parseInt(x, (nc == "x".code || nc == "X".code) ? 16 : 10);
  55. return Math.isNaN(v) ? null : cast v;
  56. }
  57. }
  58. }
  59. return null;
  60. }
  61. public static inline function parseFloat(x:String):Float {
  62. return js.Syntax.code("parseFloat({0})", x);
  63. }
  64. public static function random(x:Int):Int {
  65. return x <= 0 ? 0 : Math.floor(Math.random() * x);
  66. }
  67. static function __init__():Void
  68. untyped {
  69. __feature__("js.Boot.getClass", String.prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["String"] = String, String));
  70. __feature__("js.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
  71. __feature__("Type.resolveClass", $hxClasses["Array"] = Array);
  72. __feature__("js.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
  73. __feature__("Date.*", {
  74. __feature__("js.Boot.getClass",
  75. js.Syntax.code('Date').prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["Date"] = js.Syntax.code('Date'), js.Syntax.code('Date')));
  76. __feature__("js.Boot.isClass", js.Syntax.code('Date').__name__ = "Date");
  77. });
  78. __feature__("Int.*", js.Syntax.code('var Int = { };'));
  79. __feature__("Dynamic.*", js.Syntax.code('var Dynamic = { };'));
  80. __feature__("Float.*", js.Syntax.code('var Float = Number'));
  81. __feature__("Bool.*", js.Syntax.code('var Bool = Boolean'));
  82. __feature__("Class.*", js.Syntax.code('var Class = { };'));
  83. __feature__("Enum.*", js.Syntax.code('var Enum = { };'));
  84. #if (js_es < 5)
  85. __feature__("Array.map", if (Array.prototype.map == null) Array.prototype.map = function(f) {
  86. var a = [];
  87. for (i in 0...__this__.length)
  88. a[i] = f(__this__[i]);
  89. return a;
  90. });
  91. __feature__("Array.filter", if (Array.prototype.filter == null) Array.prototype.filter = function(f) {
  92. var a = [];
  93. for (i in 0...__this__.length) {
  94. var e = __this__[i];
  95. if (f(e))
  96. a.push(e);
  97. }
  98. return a;
  99. });
  100. #end
  101. }
  102. }