Lib.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. package flash;
  23. /**
  24. Platform-specific Flash Library. Provides some platform-specific
  25. functions for the Flash target.
  26. **/
  27. class Lib {
  28. public static var current:flash.display.MovieClip;
  29. public inline static function getTimer():Int {
  30. return untyped __global__["flash.utils.getTimer"]();
  31. }
  32. public static function eval(path:String):Dynamic {
  33. var p = path.split(".");
  34. var fields = new Array();
  35. var o:Dynamic = null;
  36. while (p.length > 0) {
  37. try {
  38. o = untyped __global__["flash.utils.getDefinitionByName"](p.join("."));
  39. } catch (e:Dynamic) {
  40. fields.unshift(p.pop());
  41. }
  42. if (o != null)
  43. break;
  44. }
  45. for (f in fields) {
  46. if (o == null)
  47. return null;
  48. o = untyped o[f];
  49. }
  50. return o;
  51. }
  52. public static function getURL(url:flash.net.URLRequest, ?target:String) {
  53. var f = untyped __global__["flash.net.navigateToURL"];
  54. if (target == null)
  55. f(url);
  56. else
  57. (cast f)(url, target);
  58. }
  59. public static function fscommand(cmd:String, ?param:String) {
  60. untyped __global__["flash.system.fscommand"](cmd, if (param == null) "" else param);
  61. }
  62. public static function trace(arg:Dynamic) {
  63. untyped __global__["trace"](arg);
  64. }
  65. public static function describeType(value:Dynamic):flash.xml.XML {
  66. return untyped __global__["flash.utils.describeType"](value);
  67. }
  68. public static function attach(name:String):flash.display.MovieClip {
  69. var cl = untyped __as__(__global__["flash.utils.getDefinitionByName"](name), Class);
  70. return untyped __new__(cl);
  71. }
  72. public inline static function as<T>(v:Dynamic, c:Class<T>):Null<T> {
  73. return untyped __as__(v, c);
  74. }
  75. public static function redirectTraces() {
  76. if (flash.external.ExternalInterface.available)
  77. haxe.Log.trace = traceToConsole;
  78. }
  79. static function traceToConsole(v:Dynamic, ?inf:haxe.PosInfos) {
  80. var type = if (inf != null && inf.customParams != null) inf.customParams[0] else null;
  81. if (type != "warn" && type != "info" && type != "debug" && type != "error")
  82. type = if (inf == null) "error" else "log";
  83. var str = if (inf == null) "" else inf.fileName + ":" + inf.lineNumber + " : ";
  84. try
  85. str += Std.string(v)
  86. catch (e:Dynamic)
  87. str += "????";
  88. str = str.split("\\").join("\\\\");
  89. flash.external.ExternalInterface.call("console." + type, str);
  90. }
  91. }