Std.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @:keepInit
  24. @:coreApi class Std {
  25. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  26. return @:privateAccess js.Boot.__instanceof(v, t);
  27. }
  28. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S@:privateAccess {
  29. return if (js.Boot.__downcastCheck(value, c)) cast value else null;
  30. }
  31. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  32. public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  33. return downcast(value, c);
  34. }
  35. @:pure
  36. public static function string(s:Dynamic):String {
  37. return @:privateAccess js.Boot.__string_rec(s, "");
  38. }
  39. public static inline function int(x:Float):Int {
  40. return (cast x) | 0;
  41. }
  42. @:pure
  43. public static function parseInt(x:String):Null<Int> {
  44. var v = untyped __js__('parseInt({0}, {0} && {0}[0]=="0" && ({0}[1]=="x" || {0}[1]=="X") ? 16 : 10)', x);
  45. if (untyped __js__("isNaN")(v))
  46. return null;
  47. return cast v;
  48. }
  49. public static inline function parseFloat(x:String):Float {
  50. return untyped __js__("parseFloat")(x);
  51. }
  52. public static function random(x:Int):Int {
  53. return x <= 0 ? 0 : Math.floor(Math.random() * x);
  54. }
  55. static function __init__():Void
  56. untyped {
  57. __feature__("js.Boot.getClass", String.prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["String"] = String, String));
  58. __feature__("js.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
  59. __feature__("Type.resolveClass", $hxClasses["Array"] = Array);
  60. __feature__("js.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
  61. __feature__("Date.*", {
  62. __feature__("js.Boot.getClass",
  63. __js__('Date').prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["Date"] = __js__('Date'), __js__('Date')));
  64. __feature__("js.Boot.isClass", __js__('Date').__name__ = "Date");
  65. });
  66. __feature__("Int.*", __js__('var Int = { };'));
  67. __feature__("Dynamic.*", __js__('var Dynamic = { };'));
  68. __feature__("Float.*", __js__('var Float = Number'));
  69. __feature__("Bool.*", __js__('var Bool = Boolean'));
  70. __feature__("Class.*", __js__('var Class = { };'));
  71. __feature__("Enum.*", __js__('var Enum = { };'));
  72. #if (js_es < 5)
  73. __feature__("Array.map", if (Array.prototype.map == null) Array.prototype.map = function(f) {
  74. var a = [];
  75. for (i in 0...__this__.length)
  76. a[i] = f(__this__[i]);
  77. return a;
  78. });
  79. __feature__("Array.filter", if (Array.prototype.filter == null) Array.prototype.filter = function(f) {
  80. var a = [];
  81. for (i in 0...__this__.length) {
  82. var e = __this__[i];
  83. if (f(e))
  84. a.push(e);
  85. }
  86. return a;
  87. });
  88. #end
  89. }
  90. }