Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / core / DescribeType.as
blobf7e3b90478b0a4a1a6c7c7cb10f51ad2fef1ded1
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 package avmplus
42 use namespace AS3;
43 // -------------- internal --------------
45 [native("DescribeTypeClass::describeTypeJSON")]
46 internal native function describeTypeJSON(o:*, flags:uint):Object;
48 internal const extendsXml:XML = <extendsClass />;
49 internal const implementsXml:XML = <implementsInterface />;
50 internal const constructorXml:XML = <constructor />;
51 internal const constantXml:XML = <constant />;
52 internal const variableXml:XML = <variable />;
53 internal const accessorXml:XML= <accessor />;
54 internal const methodXml:XML = <method />;
55 internal const parameterXml:XML = <parameter />;
56 internal const metadataXml:XML = <metadata />;
57 internal const argXml:XML = <arg />;
58 internal const typeXml:XML = <type />;
59 internal const factoryXml:XML = <factory />;
62 internal function describeParams(x:XML, parameters:Object):void
64 var c:XMLList = x.*;
65 for (var i in parameters)
67 var p = parameters[i];
68 var f:XML = parameterXml.copy();
69 f.@index = i+1;
70 f.@type = p.type;
71 f.@optional = p.optional;
73 c[c.length()] = f;
77 internal function describeMetadata(x:XML, metadata:Array):void
79 var c:XMLList = x.*;
80 for each (var md in metadata)
82 var m:XML = metadataXml.copy();
83 m.@name = md.name;
84 for each (var i in md.value)
86 var a:XML = argXml.copy()
87 a.@key = i.key;
88 a.@value = i.value;
90 m.AS3::appendChild(a);
92 c[c.length()] = m;
96 internal function finish(e:XML, i:Object):void
98 if (i.uri !== null) e.@uri = i.uri;
99 if (i.metadata !== null) describeMetadata(e, i.metadata);
102 internal function describeTraits(x:XML, traits:Object):void
104 var c:XMLList = x.*;
106 for each (var i in traits.bases)
108 var base:String = i;
110 var e:XML = extendsXml.copy();
111 e.@type = base;
113 c[c.length()] = e;
115 for each (var i in traits.interfaces)
117 var interf:String = i;
119 var e:XML = implementsXml.copy();
120 e.@type = interf;
122 c[c.length()] = e;
124 if (traits.constructor !== null)
126 var e:XML = constructorXml.copy();
127 describeParams(e, traits.constructor);
128 c[c.length()] = e;
131 for each (var i in traits.variables)
133 var variable:Object = i;
135 var e:XML = (variable.access == "readonly") ? constantXml.copy() : variableXml.copy();
136 e.@name = variable.name;
137 e.@type = variable.type;
139 finish(e, variable);
141 c[c.length()] = e;
143 for each (var i in traits.accessors)
145 var accessor:Object = i;
147 var e:XML = accessorXml.copy();
148 e.@name = accessor.name;
149 e.@access = accessor.access;
150 e.@type = accessor.type;
151 e.@declaredBy = accessor.declaredBy;
153 finish(e, accessor);
155 c[c.length()] = e;
157 for each (var i in traits.methods)
159 var method:Object = i;
161 var e:XML = methodXml.copy();
162 e.@name = method.name;
163 e.@declaredBy = method.declaredBy;
164 e.@returnType = method.returnType;
166 describeParams(e, method.parameters);
167 finish(e, method);
169 c[c.length()] = e;
171 describeMetadata(x, traits.metadata);
174 // -------------- public --------------
176 // this bit replicates a bug in Flash9/10, where a method that uses a custom namespace
177 // won't be in the output if any of its base classes (or interfaces) also define a method
178 // in that custom namespace.
179 public const HIDE_NSURI_METHODS:uint = 0x0001;
180 public const INCLUDE_BASES:uint = 0x0002;
181 public const INCLUDE_INTERFACES:uint = 0x0004;
182 public const INCLUDE_VARIABLES:uint = 0x0008;
183 public const INCLUDE_ACCESSORS:uint = 0x0010;
184 public const INCLUDE_METHODS:uint = 0x0020;
185 public const INCLUDE_METADATA:uint = 0x0040;
186 public const INCLUDE_CONSTRUCTOR:uint = 0x0080;
187 public const INCLUDE_TRAITS:uint = 0x0100;
188 public const USE_ITRAITS:uint = 0x0200;
189 // if set, hide everything from the base Object class
190 public const HIDE_OBJECT:uint = 0x0400;
192 // set of flags that replicates the behavior of flash.util.describeType in FlashPlayer 9 & 10
193 public const FLASH10_FLAGS:uint = INCLUDE_BASES |
194 INCLUDE_INTERFACES |
195 INCLUDE_VARIABLES |
196 INCLUDE_ACCESSORS |
197 INCLUDE_METHODS |
198 INCLUDE_METADATA |
199 INCLUDE_CONSTRUCTOR |
200 INCLUDE_TRAITS |
201 // include this buggy behavior by default, to match legacy Flash behavior
202 HIDE_NSURI_METHODS |
203 // Flash hides everything in class Object
204 HIDE_OBJECT;
206 public function describeType(value:*, flags:uint):XML
208 var o:Object = describeTypeJSON(value, flags);
209 var x:XML = typeXml.copy();
210 x.@name = o.name;
211 if (o.traits.bases.length)
212 x.@base = o.traits.bases[0];
213 x.@isDynamic = o.isDynamic;
214 x.@isFinal = o.isFinal;
215 x.@isStatic = o.isStatic;
216 describeTraits(x, o.traits);
218 var oi:Object = describeTypeJSON(value, flags | USE_ITRAITS);
219 if (oi !== null)
221 var e:XML = factoryXml.copy();
222 e.@type = oi.name;
223 describeTraits(e, oi.traits);
224 x.AS3::appendChild(e);
227 return x;
230 [native("DescribeTypeClass::getQualifiedClassName")]
231 public native function getQualifiedClassName(value:*):String;
233 [native("DescribeTypeClass::getQualifiedSuperclassName")]
234 public native function getQualifiedSuperclassName(value:*):String;