Lib.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C)2005-2018 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 ) return null;
  47. o = untyped o[f];
  48. }
  49. return o;
  50. }
  51. public static function getURL( url : flash.net.URLRequest, ?target : String ) {
  52. var f = untyped __global__["flash.net.navigateToURL"];
  53. if( target == null )
  54. f(url);
  55. else
  56. (cast f)(url,target);
  57. }
  58. public static function fscommand( cmd : String, ?param : String ) {
  59. untyped __global__["flash.system.fscommand"](cmd,if( param == null ) "" else param);
  60. }
  61. public static function trace( arg : Dynamic ) {
  62. untyped __global__["trace"](arg);
  63. }
  64. public static function describeType( value : Dynamic ) : flash.xml.XML {
  65. return untyped __global__["flash.utils.describeType"](value);
  66. }
  67. public static function attach( name : String ) : flash.display.MovieClip {
  68. var cl = untyped __as__(__global__["flash.utils.getDefinitionByName"](name),Class);
  69. return untyped __new__(cl);
  70. }
  71. public inline static function as<T>( v : Dynamic, c : Class<T> ) : Null<T> {
  72. return untyped __as__(v,c);
  73. }
  74. public static function redirectTraces() {
  75. if (flash.external.ExternalInterface.available)
  76. haxe.Log.trace = traceToConsole;
  77. }
  78. static function traceToConsole(v : Dynamic, ?inf : haxe.PosInfos ) {
  79. var type = if( inf != null && inf.customParams != null ) inf.customParams[0] else null;
  80. if( type != "warn" && type != "info" && type != "debug" && type != "error" )
  81. type = if( inf == null ) "error" else "log";
  82. var str = if( inf == null ) "" else inf.fileName + ":" + inf.lineNumber + " : ";
  83. try str += Std.string(v) catch( e : Dynamic ) str += "????";
  84. str = str.split("\\").join("\\\\");
  85. flash.external.ExternalInterface.call("console."+type,str);
  86. }
  87. }