~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/Type.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2008-06-15 11:04:09 UTC
  • mfrom: (2.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615110409-7pyykgwmk5v0cues
Tags: 1:1.19-3
* Remove bashism in script.
  (Closes: #484390)
* Upgrade to Policy 3.8.0 by including a README.source explaining how to
  use dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/**
3
 
        An abstract type that represents a Class.
4
 
**/
5
 
enum Class {
6
 
}
7
 
 
8
 
/**
9
3
        An abstract type that represents an Enum.
 
4
        See [Type] for the haXe Reflection API.
10
5
**/
11
 
enum Enum {
 
6
extern class Enum {
12
7
}
13
8
 
14
9
/**
15
10
        The diffent possible runtime types of a value.
 
11
        See [Type] for the haXe Reflection API.
16
12
**/
17
13
enum ValueType {
18
14
        TNull;
21
17
        TBool;
22
18
        TObject;
23
19
        TFunction;
24
 
        TClass( c : Class );
 
20
        TClass( c : Class<Dynamic> );
25
21
        TEnum( e : Enum );
26
22
        TUnknown;
27
23
}
28
24
 
 
25
/**
 
26
        The haXe Reflection API enables you to retreive informations about any value,
 
27
        Classes and Enums at runtime.
 
28
**/
29
29
class Type {
30
30
 
31
31
        /**
32
 
                Returns the class of an object
33
 
        **/
34
 
        public static function getClass( o : Dynamic ) : Class untyped {
 
32
                Converts a value to an Enum or returns [null] if the value is not an Enum.
 
33
        **/
 
34
        public static function toEnum( t : Dynamic ) : Enum untyped {
 
35
                try {
 
36
                #if flash9
 
37
                        if( !t.__isenum )
 
38
                                return null;
 
39
                #else true
 
40
                        if( t.__ename__ == null )
 
41
                                return null;
 
42
                #end
 
43
                        return t;
 
44
                } catch( e : Dynamic ) {
 
45
                }
 
46
                return null;
 
47
        }
 
48
 
 
49
        /**
 
50
                Converts a value to a Class or returns [null] if the value is not a Class.
 
51
        **/
 
52
        public static function toClass( t : Dynamic ) : Class<Dynamic> untyped {
 
53
                try {
 
54
                #if flash9
 
55
                        if( !t.hasOwnProperty("prototype") )
 
56
                                return null;
 
57
                #else true
 
58
                        if( t.__name__ == null )
 
59
                                return null;
 
60
                #end
 
61
                        return t;
 
62
                } catch( e : Dynamic ) {
 
63
                }
 
64
                return null;
 
65
        }
 
66
 
 
67
        /**
 
68
                Returns the class of a value or [null] if this value is not a Class instance.
 
69
        **/
 
70
        public static function getClass<T>( o : T ) : Class<T> untyped {
35
71
                #if flash9
36
72
                        var cname = __global__["flash.utils.getQualifiedClassName"](o);
37
73
                        if( cname == "null" || cname == "Object" || cname == "int" || cname == "Number" || cname == "Boolean" )
38
74
                                return null;
39
75
                        if( o.hasOwnProperty("prototype") )
40
76
                                return null;
41
 
                        var c = __global__["flash.utils.getDefinitionByName"](cname);
 
77
                        var c = __as__(__global__["flash.utils.getDefinitionByName"](cname),Class);
42
78
                        if( c.__isenum )
43
79
                                return null;
44
80
                        return c;
64
100
        }
65
101
 
66
102
        /**
67
 
                Returns the class of an object
 
103
                Returns the enum of a value or [null] if this value is not an Enum instance.
68
104
        **/
69
105
        public static function getEnum( o : Dynamic ) : Enum untyped {
70
106
                #if flash9
74
110
                        // getEnum(Enum) should be null
75
111
                        if( o.hasOwnProperty("prototype") )
76
112
                                return null;
77
 
                        var c = __global__["flash.utils.getDefinitionByName"](cname);
 
113
                        var c = __as__(__global__["flash.utils.getDefinitionByName"](cname),Class);
78
114
                        if( !c.__isenum )
79
115
                                return null;
80
116
                        return c;
94
130
 
95
131
 
96
132
        /**
97
 
                Returns the super-class of a class
 
133
                Returns the super-class of a class, or null if no super class.
98
134
        **/
99
 
        public static function getSuperClass( c : Class ) : Class untyped {
 
135
        public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic> untyped {
100
136
                #if flash9
101
137
                        var cname = __global__["flash.utils.getQualifiedSuperclassName"](c);
102
138
                        if( cname == "Object" )
103
139
                                return null;
104
 
                        return __global__["flash.utils.getDefinitionByName"](cname);
 
140
                        return __as__(__global__["flash.utils.getDefinitionByName"](cname),Class);
105
141
                #else true
106
142
                        return c.__super__;
107
143
                #end
109
145
 
110
146
 
111
147
        /**
112
 
                Returns the complete name of the class of an object
 
148
                Returns the complete name of a class.
113
149
        **/
114
 
        public static function getClassName( c : Class ) : String {
 
150
        public static function getClassName( c : Class<Dynamic> ) : String {
115
151
                if( c == null )
116
152
                        return null;
117
153
                #if flash9
118
 
                        var name = untyped __global__["flash.utils.getQualifiedClassName"](c);
119
 
                        if( name == "flash::FlashXml__" )
120
 
                                return "Xml";
121
 
                        return name;
 
154
                        var str : String = untyped __global__["flash.utils.getQualifiedClassName"](c);
 
155
                        return str.split("::").join(".");
122
156
                #else true
123
157
                        var a : Array<String> = untyped c.__name__;
124
158
                        return a.join(".");
126
160
        }
127
161
 
128
162
        /**
129
 
                Returns the complete name of the class of an object
 
163
                Returns the complete name of an enum.
130
164
        **/
131
165
        public static function getEnumName( e : Enum ) : String {
132
166
                #if flash9
139
173
        }
140
174
 
141
175
        /**
142
 
                Evaluates a class from a name
 
176
                Evaluates a class from a name. The class must have been compiled
 
177
                to be accessible.
143
178
        **/
144
 
        public static function resolveClass( name : String ) : Class {
145
 
                var cl : Class;
 
179
        public static function resolveClass( name : String ) : Class<Dynamic> {
 
180
                var cl : Class<Dynamic>;
146
181
                untyped {
147
182
                #if flash9
148
183
                        try {
149
 
                                cl = __global__["flash.utils.getDefinitionByName"](name);
 
184
                                cl = __as__(__global__["flash.utils.getDefinitionByName"](name),Class);
150
185
                                if( cl.__isenum )
151
186
                                        return null;
152
187
                                return cl; // skip test below
180
215
 
181
216
 
182
217
        /**
183
 
                Evaluates an enum from a name
 
218
                Evaluates an enum from a name. The enum must have been compiled
 
219
                to be accessible.
184
220
        **/
185
221
        public static function resolveEnum( name : String ) : Enum {
186
222
                var e : Dynamic;
219
255
                return e;
220
256
        }
221
257
 
 
258
        /**
 
259
                Creates an instance of the given class with the list of constructor arguments.
 
260
        **/
 
261
        public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
 
262
                #if flash9
 
263
                        return switch( args.length ) {
 
264
                        case 0: __new__(cl);
 
265
                        case 1: __new__(cl,args[0]);
 
266
                        case 2: __new__(cl,args[0],args[1]);
 
267
                        case 3: __new__(cl,args[0],args[1],args[2]);
 
268
                        case 4: __new__(cl,args[0],args[1],args[2],args[3]);
 
269
                        case 5: __new__(cl,args[0],args[1],args[2],args[3],args[4]);
 
270
                        default: throw "Too many arguments";
 
271
                        }
 
272
                #else flash
 
273
                        var o = { __constructor__ : cl, __proto__ : cl.prototype };
 
274
                        cl["apply"](o,args);
 
275
                        return o;
 
276
                #else neko
 
277
                        return untyped __dollar__call(__dollar__objget(cl,__dollar__hash("new".__s)),cl,args.__neko());
 
278
                #else js
 
279
                        if( args.length >= 6 ) throw "Too many arguments";
 
280
                        return untyped __new__(cl,args[0],args[1],args[2],args[3],args[4],args[5]);
 
281
                #else error
 
282
                #end
 
283
        }
222
284
 
223
285
        /**
224
286
                Similar to [Reflect.createInstance] excepts that the constructor is not called.
 
287
                This enables you to create an instance without any side-effect.
225
288
        **/
226
 
        public static function createEmptyInstance( cl : Class ) untyped {
 
289
        public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
227
290
                #if flash9
228
 
                        return cl.__construct__.call(null,null);
 
291
                        try {
 
292
                                flash.Boot.skip_constructor = true;
 
293
                                var i = __new__(cl);
 
294
                                flash.Boot.skip_constructor = false;
 
295
                                return i;
 
296
                        } catch( e : Dynamic ) {
 
297
                                flash.Boot.skip_constructor = false;
 
298
                                throw e;
 
299
                        }
 
300
                        return null;
229
301
                #else flash
230
302
                        var o : Dynamic = __new__(_global["Object"]);
231
303
                        o.__proto__ = cl.prototype;
257
329
        #end
258
330
 
259
331
        /**
260
 
                Returns the list of instance fields
 
332
                Returns the list of instance fields.
261
333
        **/
262
 
        public static function getInstanceFields( c : Class ) : Array<String> {
 
334
        public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
263
335
                #if flash9
264
336
                        return describe(c,true);
265
337
                #else true
280
352
        }
281
353
 
282
354
        /**
283
 
                Returns the list of class static fields
 
355
                Returns the list of a class static fields.
284
356
        **/
285
 
        public static function getClassFields( c : Class ) : Array<String> {
 
357
        public static function getClassFields( c : Class<Dynamic> ) : Array<String> {
286
358
                #if flash9
287
359
                        var a = describe(c,false);
288
360
                        a.remove("__construct__");
308
380
                Returns all the available constructor names for an enum.
309
381
        **/
310
382
        public static function getEnumConstructs( e : Enum ) : Array<String> {
311
 
                #if flash9
312
 
                        return describe(e,false);
313
 
                #else true
314
 
                        var a = Reflect.fields(e);
315
 
                        a.remove(__unprotect__("__ename__"));
316
 
                        #if neko
317
 
                        a.remove("prototype");
318
 
                        #end
319
 
                        return a;
320
 
                #end
 
383
                return untyped e.__constructs__;
321
384
        }
322
385
 
323
386
        /**
354
417
                case "Boolean": return TBool;
355
418
                case "Object": return TObject;
356
419
                default:
 
420
                        var c : Dynamic;
357
421
                        try {
358
 
                                var c = __global__["flash.utils.getDefinitionByName"](cname);
 
422
                                c = __global__["flash.utils.getDefinitionByName"](cname);
359
423
                                if( v.hasOwnProperty("prototype") )
360
424
                                        return TObject;
361
425
                                if( c.__isenum )
362
426
                                        return TEnum(c);
363
427
                                return TClass(c);
364
428
                        } catch( e : Dynamic ) {
365
 
                                return TFunction;
 
429
                                if( cname == "builtin.as$0::MethodClosure" || cname.indexOf("-") != -1 )
 
430
                                        return TFunction;
 
431
                                return if( c == null ) TFunction else TClass(c);
366
432
                        }
367
433
                }
 
434
                return null;
368
435
                #else (flash || js)
369
436
                switch( #if flash __typeof__ #else true __js__("typeof") #end(v) ) {
370
437
                #if flash
403
470
                #end
404
471
        }
405
472
 
 
473
        /**
 
474
                Recursively compare two enums constructors and parameters.
 
475
        **/
 
476
        public static function enumEq<T>( a : T, b : T ) : Bool untyped {
 
477
                if( a == b )
 
478
                        return true;
 
479
                #if neko
 
480
                try {
 
481
                        if( a.__enum__ == null || a.tag != b.tag )
 
482
                                return false;
 
483
                } catch( e : Dynamic ) {
 
484
                        return false;
 
485
                }
 
486
                for( i in 0...__dollar__asize(a.args) )
 
487
                        if( !enumEq(a.args[i],b.args[i]) )
 
488
                                return false;
 
489
                #else flash9
 
490
                try {
 
491
                        if( a.tag != b.tag )
 
492
                                return false;
 
493
                        for( i in 0...a.params.length )
 
494
                                if( !enumEq(a.params[i],b.params[i]) )
 
495
                                        return false;
 
496
                } catch( e : Dynamic ) {
 
497
                        return false;
 
498
                }
 
499
                #else true
 
500
                if( a[0] != b[0] )
 
501
                        return false;
 
502
                for( i in 2...a.length )
 
503
                        if( !enumEq(a[i],b[i]) )
 
504
                                return false;
 
505
                var e = a.__enum__;
 
506
                if( e != b.__enum__ || e == null )
 
507
                        return false;
 
508
                #end
 
509
                return true;
 
510
        }
 
511
 
 
512
        /**
 
513
                Returns the constructor of an enum
 
514
        **/
 
515
        public static function enumConstructor( e : Dynamic ) : String {
 
516
        #if neko
 
517
                return new String(e.tag);
 
518
        #else flash9
 
519
                return e.tag;
 
520
        #else true
 
521
                return e[0];
 
522
        #end
 
523
        }
 
524
 
 
525
        /**
 
526
                Returns the parameters of an enum
 
527
        **/
 
528
        public static function enumParameters( e : Dynamic ) : Array<Dynamic> {
 
529
        #if neko
 
530
                return if( e.args == null ) [] else untyped Array.new1(e.args,__dollar__asize(e.args));
 
531
        #else flash9
 
532
                return if( e.params == null ) [] else e.params;
 
533
        #else true
 
534
                return e.slice(2);
 
535
        #end
 
536
        }
 
537
 
 
538
        /**
 
539
                Returns the index of the constructor of an enum
 
540
        **/
 
541
        public static function enumIndex( e : Dynamic ) : Int {
 
542
        #if (neko || flash9)
 
543
                return e.index;
 
544
        #else true
 
545
                return e[1];
 
546
        #end
 
547
        }
 
548
 
406
549
}
407
550