Shift-zoom to go in/out by 3 levels
[potlatch2.git] / hxasm / Writer.as
1 package hxasm {
2         
3         import flash.utils.ByteArray;
4         
5         public class Writer {
6                 public function Writer(out : Output,ctx : Context) : void {
7                         this.data = out;
8                         this.ctx = ctx;
9                         this.emptyIndex = Index.Idx(1);
10                 }
11                 protected var ctx : Context;
12                 protected var data : Output;
13                 protected var emptyIndex : Index;
14                 protected function beginTag(id : int,len : int) : void {
15                         if(len >= 63) {
16                                 this.data.writeUInt16((id << 6) | 63);
17                                 this.data.writeUInt32(len);
18                         }
19                         else this.data.writeUInt16((id << 6) | len);
20                 }
21                 protected function writeInt(n : int) : void {
22                         OpWriter.writeInt(this.data,n);
23                 }
24                 protected function writeList(a : Array,write : Function) : void {
25                         if(a.length == 0) {
26                                 this.writeInt(0);
27                                 return;
28                         }
29                         this.writeInt(a.length + 1);
30                         {
31                                 var _g1 : int = 0, _g : int = a.length;
32                                 while(_g1 < _g) {
33                                         var i : int = _g1++;
34                                         write(a[i]);
35                                 }
36                         }
37                 }
38                 protected function writeList2(a : Array,write : Function) : void {
39                         this.writeInt(a.length);
40                         {
41                                 var _g1 : int = 0, _g : int = a.length;
42                                 while(_g1 < _g) {
43                                         var i : int = _g1++;
44                                         write(a[i]);
45                                 }
46                         }
47                 }
48                 protected function writeString(s : String) : void {
49                         this.writeInt(s.length);
50                         this.data.write(s);
51                 }
52                 protected function writeIndex(i : Index) : void {
53                         var $e : enum = (i);
54                         switch( $e.index ) {
55                         case 0:
56                         var n : int = $e.params[0];
57                         {
58                                 this.writeInt(n);
59                         }break;
60                         }
61                 }
62                 protected function writeIndexOpt(i : Index) : void {
63                         if(i == null) {
64                                 this.data.writeChar(0);
65                                 return;
66                         }
67                         this.writeIndex(i);
68                 }
69                 protected function writeNamespace(n : HNamespace) : void {
70                         var $e : enum = (n);
71                         switch( $e.index ) {
72                         case 0:
73                         var id : Index = $e.params[0];
74                         {
75                                 this.data.writeChar(22);
76                                 this.writeIndex((id == null?this.emptyIndex:id));
77                         }break;
78                         case 1:
79                         var ns : Index = $e.params[0];
80                         {
81                                 this.data.writeChar(8);
82                                 this.writeIndex(ns);
83                         }break;
84                         }
85                 }
86                 protected function writeNsSet(n : Array) : void {
87                         this.data.writeChar(n.length);
88                         {
89                                 var _g : int = 0;
90                                 while(_g < n.length) {
91                                         var i : Index = n[_g];
92                                         ++_g;
93                                         this.writeIndex(i);
94                                 }
95                         }
96                 }
97                 protected function writeName(n : Name) : void {
98                         var $e : enum = (n);
99                         switch( $e.index ) {
100                         case 0:
101                         var ns : Index = $e.params[1], id : Index = $e.params[0];
102                         {
103                                 this.data.writeChar(7);
104                                 this.writeIndex(ns);
105                                 this.writeIndex(id);
106                         }break;
107                         case 1:
108                         var nss : Index = $e.params[0];
109                         {
110                                 this.data.writeChar(27);
111                                 this.writeIndex(nss);
112                         }break;
113                         }
114                 }
115                 protected function writeField(f : *) : void {
116                         this.writeIndex(f.name);
117                         var $e : enum = (f.kind);
118                         switch( $e.index ) {
119                         case 0:
120                         var _const : * = $e.params[1], t : Index = $e.params[0];
121                         {
122                                 this.data.writeChar((_const?6:0));
123                                 this.writeInt(f.slot);
124                                 this.writeIndexOpt(t);
125                                 this.data.writeChar(0);
126                         }break;
127                         case 1:
128                         var isOverride : * = $e.params[2], isFinal : * = $e.params[1], t2 : Index = $e.params[0];
129                         {
130                                 var flags : int = ((isFinal?16:0)) | ((isOverride?32:0));
131                                 this.data.writeChar(1 | flags);
132                                 this.writeInt(f.slot);
133                                 this.writeIndex(t2);
134                         }break;
135                         }
136                 }
137                 protected function writeClass(c : *) : void {
138                         this.writeIndex(c.name);
139                         this.writeIndex(c.superclass);
140                         this.data.writeChar(0);
141                         this.writeList2([],null);
142                         this.writeIndex(c.constructorType);
143                         this.writeList2(c.fields,this.writeField);
144                 }
145                 protected function writeStatics(c : *) : void {
146                         this.writeIndex(c.statics);
147                         this.writeList2(c.staticFields,this.writeField);
148                 }
149                 protected function writeMethodType(m : *) : void {
150                         this.data.writeChar(m.args.length);
151                         this.writeIndexOpt(m.ret);
152                         {
153                                 var _g : int = 0, _g1 : Array = m.args;
154                                 while(_g < _g1.length) {
155                                         var a : Index = _g1[_g];
156                                         ++_g;
157                                         this.writeIndexOpt(a);
158                                 }
159                         }
160                         this.writeIndexOpt(null);
161                         this.data.writeChar(0);
162                 }
163                 protected function writeMethod(m : *) : void {
164                         this.writeIndex(m.type);
165                         this.writeInt(m.maxStack);
166                         this.writeInt(m.nRegs);
167                         this.writeInt(0);
168                         this.writeInt(m.maxScope);
169                         var b : OpWriter = new OpWriter();
170                         {
171                                 var _g : int = 0, _g1 : Array = m.opcodes;
172                                 while(_g < _g1.length) {
173                                         var o : OpCode = _g1[_g];
174                                         ++_g;
175                                         b.write(o);
176                                 }
177                         }
178                         var codeStr : ByteArray = b.getBytes();
179                         this.writeInt(codeStr.length);
180                         this.data.writeBinary(codeStr);
181                         this.writeList2([],null);
182                         this.writeList2([],null);
183                 }
184                 protected function writeInitSlot(c : *) : void {
185                         this.writeIndex(c.name);
186                         this.data.writeChar(4);
187                         this.writeInt(1);
188                         this.writeIndex(c.index);
189                 }
190                 protected function writeInit(init : Index,classes : Array) : void {
191                         this.writeIndex(init);
192                         this.writeList2(classes,this.writeInitSlot);
193                 }
194                 protected function writeAs3Header() : void {
195                         var d : * = this.ctx.getDatas();
196                         this.data.writeInt32(3014672);
197                         this.writeList(d.ints,this.writeInt);
198                         this.writeList([],null);
199                         this.writeList(d.floats,this.data.writeDouble);
200                         this.writeList(d.strings,this.writeString);
201                         this.writeList(d.namespaces,this.writeNamespace);
202                         this.writeList(d.nssets,this.writeNsSet);
203                         this.writeList(d.names,this.writeName);
204                         this.writeList2(d.mtypes,this.writeMethodType);
205                         this.writeList2([],null);
206                         this.writeList2(d.classes,this.writeClass);
207                         {
208                                 var _g : int = 0, _g1 : Array = d.classes;
209                                 while(_g < _g1.length) {
210                                         var c : * = _g1[_g];
211                                         ++_g;
212                                         this.writeStatics(c);
213                                 }
214                         }
215                         this.writeList2([d.classes],function(f : Function,a1 : Index) : Function {
216                                 return function(a2 : Array) : void {
217                                         f(a1,a2);
218                                         return;
219                                 }
220                         }(this.writeInit,d.init));
221                         this.writeList2(d.methods,this.writeMethod);
222                 }
223                 protected function write() : void {
224                         var out : Output = this.data;
225                         out.write("FWS");
226                         out.writeChar(9);
227                         var header : Output = new Output();
228                         this.data = header;
229                         this.writeAs3Header();
230                         this.data = out;
231                         var header1 : ByteArray = header.getBytes();
232                         var len : int = 23 + 6 + header1.length + (header1.length >= 63?6:2);
233                         out.writeInt32(len);
234                         {
235                                 var _g : int = 0, _g1 : Array = [120,0,3,232,0,0,11,184,0];
236                                 while(_g < _g1.length) {
237                                         var c : int = _g1[_g];
238                                         ++_g;
239                                         out.writeChar(c);
240                                 }
241                         }
242                         out.writeUInt16(7680);
243                         out.writeUInt16(1);
244                         this.beginTag(69,4);
245                         out.writeInt32(25);
246                         this.beginTag(72,header1.length);
247                         out.writeBinary(header1);
248                         this.beginTag(1,0);
249                         this.beginTag(0,0);
250                 }
251                 static public function write(out : Output,ctx : Context) : void {
252                         var w : Writer = new Writer(out,ctx);
253                         w.write();
254                 }
255         }
256 }