Advertisement
YellowAfterlife

Haxe JS cast()

Aug 12th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.18 KB | None | 0 0
  1. js.Boot.__interfLoop = function(cc,cl) {
  2.     if(cc == null) return false;
  3.     if(cc == cl) return true;
  4.     var intf = cc.__interfaces__;
  5.     if(intf != null) {
  6.         var _g1 = 0, _g = intf.length;
  7.         while(_g1 < _g) {
  8.             var i = _g1++;
  9.             var i1 = intf[i];
  10.             if(i1 == cl || js.Boot.__interfLoop(i1,cl)) return true;
  11.         }
  12.     }
  13.     return js.Boot.__interfLoop(cc.__super__,cl);
  14. }
  15. js.Boot.__instanceof = function(o,cl) {
  16.     if(cl == null) return false;
  17.     switch(cl) {
  18.     case Int:
  19.         return (o|0) === o;
  20.     case Float:
  21.         return typeof(o) == "number";
  22.     case Bool:
  23.         return typeof(o) == "boolean";
  24.     case String:
  25.         return typeof(o) == "string";
  26.     case Dynamic:
  27.         return true;
  28.     default:
  29.         if(o != null) {
  30.             if(typeof(cl) == "function") {
  31.                 if(o instanceof cl) {
  32.                     if(cl == Array) return o.__enum__ == null;
  33.                     return true;
  34.                 }
  35.                 if(js.Boot.__interfLoop(o.__class__,cl)) return true;
  36.             }
  37.         } else return false;
  38.         if(cl == Class && o.__name__ != null) return true;
  39.         if(cl == Enum && o.__ename__ != null) return true;
  40.         return o.__enum__ == cl;
  41.     }
  42. }
  43. js.Boot.__cast = function(o,t) {
  44.     if(js.Boot.__instanceof(o,t)) return o; else throw "Cannot cast " + Std.string(o) + " to " + Std.string(t);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement