3 import flash.utils.ByteArray;
6 public function Writer(out : Output,ctx : Context) : void {
9 this.emptyIndex = Index.Idx(1);
11 protected var ctx : Context;
12 protected var data : Output;
13 protected var emptyIndex : Index;
14 protected function beginTag(id : int,len : int) : void {
16 this.data.writeUInt16((id << 6) | 63);
17 this.data.writeUInt32(len);
19 else this.data.writeUInt16((id << 6) | len);
21 protected function writeInt(n : int) : void {
22 OpWriter.writeInt(this.data,n);
24 protected function writeList(a : Array,write : Function) : void {
29 this.writeInt(a.length + 1);
31 var _g1 : int = 0, _g : int = a.length;
38 protected function writeList2(a : Array,write : Function) : void {
39 this.writeInt(a.length);
41 var _g1 : int = 0, _g : int = a.length;
48 protected function writeString(s : String) : void {
49 this.writeInt(s.length);
52 protected function writeIndex(i : Index) : void {
56 var n : int = $e.params[0];
62 protected function writeIndexOpt(i : Index) : void {
64 this.data.writeChar(0);
69 protected function writeNamespace(n : HNamespace) : void {
73 var id : Index = $e.params[0];
75 this.data.writeChar(22);
76 this.writeIndex((id == null?this.emptyIndex:id));
79 var ns : Index = $e.params[0];
81 this.data.writeChar(8);
86 protected function writeNsSet(n : Array) : void {
87 this.data.writeChar(n.length);
90 while(_g < n.length) {
91 var i : Index = n[_g];
97 protected function writeName(n : Name) : void {
101 var ns : Index = $e.params[1], id : Index = $e.params[0];
103 this.data.writeChar(7);
108 var nss : Index = $e.params[0];
110 this.data.writeChar(27);
111 this.writeIndex(nss);
115 protected function writeField(f : *) : void {
116 this.writeIndex(f.name);
117 var $e : enum = (f.kind);
120 var _const : * = $e.params[1], t : Index = $e.params[0];
122 this.data.writeChar((_const?6:0));
123 this.writeInt(f.slot);
124 this.writeIndexOpt(t);
125 this.data.writeChar(0);
128 var isOverride : * = $e.params[2], isFinal : * = $e.params[1], t2 : Index = $e.params[0];
130 var flags : int = ((isFinal?16:0)) | ((isOverride?32:0));
131 this.data.writeChar(1 | flags);
132 this.writeInt(f.slot);
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);
145 protected function writeStatics(c : *) : void {
146 this.writeIndex(c.statics);
147 this.writeList2(c.staticFields,this.writeField);
149 protected function writeMethodType(m : *) : void {
150 this.data.writeChar(m.args.length);
151 this.writeIndexOpt(m.ret);
153 var _g : int = 0, _g1 : Array = m.args;
154 while(_g < _g1.length) {
155 var a : Index = _g1[_g];
157 this.writeIndexOpt(a);
160 this.writeIndexOpt(null);
161 this.data.writeChar(0);
163 protected function writeMethod(m : *) : void {
164 this.writeIndex(m.type);
165 this.writeInt(m.maxStack);
166 this.writeInt(m.nRegs);
168 this.writeInt(m.maxScope);
169 var b : OpWriter = new OpWriter();
171 var _g : int = 0, _g1 : Array = m.opcodes;
172 while(_g < _g1.length) {
173 var o : OpCode = _g1[_g];
178 var codeStr : ByteArray = b.getBytes();
179 this.writeInt(codeStr.length);
180 this.data.writeBinary(codeStr);
181 this.writeList2([],null);
182 this.writeList2([],null);
184 protected function writeInitSlot(c : *) : void {
185 this.writeIndex(c.name);
186 this.data.writeChar(4);
188 this.writeIndex(c.index);
190 protected function writeInit(init : Index,classes : Array) : void {
191 this.writeIndex(init);
192 this.writeList2(classes,this.writeInitSlot);
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);
208 var _g : int = 0, _g1 : Array = d.classes;
209 while(_g < _g1.length) {
212 this.writeStatics(c);
215 this.writeList2([d.classes],function(f : Function,a1 : Index) : Function {
216 return function(a2 : Array) : void {
220 }(this.writeInit,d.init));
221 this.writeList2(d.methods,this.writeMethod);
223 protected function write() : void {
224 var out : Output = this.data;
227 var header : Output = new Output();
229 this.writeAs3Header();
231 var header1 : ByteArray = header.getBytes();
232 var len : int = 23 + 6 + header1.length + (header1.length >= 63?6:2);
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];
242 out.writeUInt16(7680);
246 this.beginTag(72,header1.length);
247 out.writeBinary(header1);
251 static public function write(out : Output,ctx : Context) : void {
252 var w : Writer = new Writer(out,ctx);