swf2js 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE-GPL +680 -0
  2. package/README.md +208 -2
  3. package/package.json +24 -4
  4. package/src/cli.js +272 -0
  5. package/src/decoder/button-decoder.js +136 -0
  6. package/src/decoder/shape-decoder.js +786 -0
  7. package/src/decoder/symbol-table.js +36 -0
  8. package/src/decoder/tag-decoder.js +1190 -0
  9. package/src/decoder/tag-types.js +258 -0
  10. package/src/emitter/asset-encoder.js +708 -0
  11. package/src/emitter/bundle-emitter.js +596 -0
  12. package/src/emitter/embedded-swf.js +246 -0
  13. package/src/emitter/font-encoder.js +365 -0
  14. package/src/emitter/timeline-data-codec.js +58 -0
  15. package/src/model/swf-document.js +59 -0
  16. package/src/model/transpile-result.js +32 -0
  17. package/src/parser/bit-reader.js +237 -0
  18. package/src/parser/decompressor.js +91 -0
  19. package/src/parser/swf-parser.js +164 -0
  20. package/src/pretty-printer/pretty-printer.js +137 -0
  21. package/src/runtime/avm1-runtime.js +4907 -0
  22. package/src/runtime/avm2-runtime.js +1591 -0
  23. package/src/runtime/embedded-movie-runtime.js +208 -0
  24. package/src/runtime/index.js +17 -0
  25. package/src/runtime/movie-runtime.js +306 -0
  26. package/src/runtime/swflib.js +14050 -0
  27. package/src/transpiler/actionscript.js +390 -0
  28. package/src/transpiler/avm1/avm1-ir.js +205 -0
  29. package/src/transpiler/avm1/avm1-opcodes.js +149 -0
  30. package/src/transpiler/avm1/avm1-rewrite.js +291 -0
  31. package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
  32. package/src/transpiler/avm2/abc-reader.js +416 -0
  33. package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
  34. package/src/transpiler/cfg-structurer.js +876 -0
  35. package/src/transpiler/timeline/display-list.js +643 -0
  36. package/src/transpiler/timeline/timeline-codegen.js +6354 -0
  37. package/src/util/diagnostics.js +50 -0
  38. package/src/util/logger.js +40 -0
@@ -0,0 +1,416 @@
1
+ /**
2
+ * @fileoverview ABC binary format reader.
3
+ * Parses the ABC (ActionScript Byte Code) format embedded in DoABC tags.
4
+ * Spec: AVM2 Overview — Adobe ActionScript Virtual Machine 2 (AVM2) Overview
5
+ */
6
+
7
+ /**
8
+ * @typedef {Object} AbcConstantPool
9
+ * @property {number[]} ints
10
+ * @property {number[]} uints
11
+ * @property {number[]} doubles
12
+ * @property {string[]} strings
13
+ * @property {AbcNamespace[]} namespaces
14
+ * @property {AbcNamespace[][]} nsSets
15
+ * @property {AbcMultiname[]} multinames
16
+ */
17
+
18
+ /**
19
+ * @typedef {Object} AbcNamespace
20
+ * @property {number} kind
21
+ * @property {number} nameIndex
22
+ */
23
+
24
+ /**
25
+ * @typedef {Object} AbcMultiname
26
+ * @property {number} kind
27
+ * @property {string} localName
28
+ * @property {string} [ns]
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} AbcMethod
33
+ * @property {number} returnType
34
+ * @property {number[]} paramTypes
35
+ * @property {number} nameIndex
36
+ * @property {number} flags
37
+ * @property {{valueIndex:number, kind:number}[]} optionalValues
38
+ * @property {AbcMethodBody|null} body
39
+ */
40
+
41
+ /**
42
+ * @typedef {Object} AbcMethodBody
43
+ * @property {number} methodIndex
44
+ * @property {number} maxStack
45
+ * @property {number} localCount
46
+ * @property {number} initScopeDepth
47
+ * @property {number} maxScopeDepth
48
+ * @property {Uint8Array} code
49
+ * @property {AbcException[]} exceptions
50
+ * @property {AbcTrait[]} traits
51
+ */
52
+
53
+ /**
54
+ * @typedef {Object} AbcException
55
+ * @property {number} from
56
+ * @property {number} to
57
+ * @property {number} target
58
+ * @property {number} excType
59
+ * @property {number} varName
60
+ */
61
+
62
+ /**
63
+ * @typedef {Object} AbcInstance
64
+ * @property {number} nameIndex
65
+ * @property {number} superNameIndex
66
+ * @property {number} flags
67
+ * @property {number[]} interfaces
68
+ * @property {number} iinit
69
+ * @property {AbcTrait[]} traits
70
+ */
71
+
72
+ /**
73
+ * @typedef {Object} AbcClass
74
+ * @property {number} cinit
75
+ * @property {AbcTrait[]} traits
76
+ */
77
+
78
+ /**
79
+ * @typedef {Object} AbcTrait
80
+ * @property {number} nameIndex
81
+ * @property {number} kind
82
+ * @property {number} [slotId]
83
+ * @property {number} [dispId]
84
+ * @property {number} [methodIndex]
85
+ * @property {number} [valueIndex]
86
+ * @property {number} [valueKind]
87
+ * @property {number} [typeNameIndex]
88
+ * @property {number[]} [metadataIndices]
89
+ */
90
+
91
+ /**
92
+ * @typedef {Object} AbcScript
93
+ * @property {number} init
94
+ * @property {AbcTrait[]} traits
95
+ */
96
+
97
+ /**
98
+ * @typedef {Object} AbcFile
99
+ * @property {AbcConstantPool} constantPool
100
+ * @property {AbcMethod[]} methods
101
+ * @property {{nameIndex:number, entries:number[][]}[]} metadata
102
+ * @property {AbcInstance[]} instances
103
+ * @property {AbcClass[]} classes
104
+ * @property {AbcScript[]} scripts
105
+ */
106
+
107
+ const utf8Decoder = new TextDecoder('utf-8', { fatal: false });
108
+
109
+ export class AbcReader {
110
+ /** @param {Uint8Array} data */
111
+ constructor(data) {
112
+ this.data = data;
113
+ this.pos = 0;
114
+ }
115
+
116
+ /** @returns {AbcFile} */
117
+ read() {
118
+ const minorVersion = this.readU16();
119
+ const majorVersion = this.readU16();
120
+
121
+ const constantPool = this.readConstantPool();
122
+ const methods = this.readMethods(constantPool);
123
+ const metadata = this.readMetadata();
124
+ const { instances, classes } = this.readClassDefs(methods.length);
125
+ const scripts = this.readScripts();
126
+ this.readMethodBodies(methods);
127
+
128
+ return { constantPool, methods, metadata, instances, classes, scripts };
129
+ }
130
+
131
+ readConstantPool() {
132
+ const ints = [0];
133
+ const intCount = this.readU30();
134
+ for (let i = 1; i < intCount; i++) ints.push(this.readS32());
135
+
136
+ const uints = [0];
137
+ const uintCount = this.readU30();
138
+ for (let i = 1; i < uintCount; i++) uints.push(this.readU32());
139
+
140
+ const doubles = [NaN];
141
+ const doubleCount = this.readU30();
142
+ for (let i = 1; i < doubleCount; i++) doubles.push(this.readF64());
143
+
144
+ const strings = [''];
145
+ const stringCount = this.readU30();
146
+ for (let i = 1; i < stringCount; i++) strings.push(this.readString());
147
+
148
+ const namespaces = [{ kind: 0, nameIndex: 0 }];
149
+ const nsCount = this.readU30();
150
+ for (let i = 1; i < nsCount; i++) {
151
+ namespaces.push({ kind: this.readU8(), nameIndex: this.readU30() });
152
+ }
153
+
154
+ const nsSets = [[]];
155
+ const nsSetCount = this.readU30();
156
+ for (let i = 1; i < nsSetCount; i++) {
157
+ const count = this.readU30();
158
+ const set = [];
159
+ for (let j = 0; j < count; j++) set.push(this.readU30());
160
+ nsSets.push(set);
161
+ }
162
+
163
+ const multinames = [{ kind: 0, localName: '' }];
164
+ const multinameCount = this.readU30();
165
+ for (let i = 1; i < multinameCount; i++) {
166
+ multinames.push(this.readMultiname(strings, namespaces, nsSets));
167
+ }
168
+
169
+ return { ints, uints, doubles, strings, namespaces, nsSets, multinames };
170
+ }
171
+
172
+ /** @param {AbcConstantPool} cp */
173
+ readMultiname(strings, namespaces, nsSets) {
174
+ const kind = this.readU8();
175
+ switch (kind) {
176
+ case 0x07: // QName
177
+ case 0x0D: { // QNameA
178
+ const nsIdx = this.readU30();
179
+ const nameIdx = this.readU30();
180
+ const ns = namespaces[nsIdx];
181
+ return { kind, localName: strings[nameIdx] ?? '', ns: strings[ns?.nameIndex ?? 0] ?? '', nsKind: ns?.kind ?? 0, nsIndex: nsIdx };
182
+ }
183
+ case 0x0F: // RTQName
184
+ case 0x10: { // RTQNameA
185
+ const nameIdx = this.readU30();
186
+ return { kind, localName: strings[nameIdx] ?? '' };
187
+ }
188
+ case 0x11: // RTQNameL
189
+ case 0x12: // RTQNameLA
190
+ return { kind, localName: '' };
191
+ case 0x09: // Multiname
192
+ case 0x0E: { // MultinameA
193
+ const nameIdx = this.readU30();
194
+ const nsSetIdx = this.readU30();
195
+ const nsSet = (nsSets[nsSetIdx] || []).map((nsIndex) => {
196
+ const ns = namespaces[nsIndex];
197
+ return {
198
+ kind: ns?.kind ?? 0,
199
+ nsIndex,
200
+ ns: strings[ns?.nameIndex ?? 0] ?? '',
201
+ };
202
+ });
203
+ return { kind, localName: strings[nameIdx] ?? '', nsSet };
204
+ }
205
+ case 0x1B: // MultinameL
206
+ case 0x1C: { // MultinameLA
207
+ const nsSetIdx = this.readU30();
208
+ const nsSet = (nsSets[nsSetIdx] || []).map((nsIndex) => {
209
+ const ns = namespaces[nsIndex];
210
+ return {
211
+ kind: ns?.kind ?? 0,
212
+ nsIndex,
213
+ ns: strings[ns?.nameIndex ?? 0] ?? '',
214
+ };
215
+ });
216
+ return { kind, localName: '', nsSet };
217
+ }
218
+ case 0x1D: { // Generic type
219
+ const typeBaseIndex = this.readU30();
220
+ const count = this.readU30();
221
+ const typeParameterIndices = [];
222
+ for (let i = 0; i < count; i++) typeParameterIndices.push(this.readU30());
223
+ return { kind, localName: '', typeBaseIndex, typeParameterIndices };
224
+ }
225
+ default:
226
+ return { kind, localName: '' };
227
+ }
228
+ }
229
+
230
+ /** @param {AbcConstantPool} cp */
231
+ readMethods(cp) {
232
+ const count = this.readU30();
233
+ const methods = [];
234
+ for (let i = 0; i < count; i++) {
235
+ const paramCount = this.readU30();
236
+ const returnType = this.readU30();
237
+ const paramTypes = [];
238
+ for (let p = 0; p < paramCount; p++) paramTypes.push(this.readU30());
239
+ const nameIndex = this.readU30();
240
+ const flags = this.readU8();
241
+ const optionalValues = [];
242
+ if (flags & 0x08) { // HAS_OPTIONAL
243
+ const optCount = this.readU30();
244
+ for (let o = 0; o < optCount; o++) optionalValues.push({ valueIndex: this.readU30(), kind: this.readU8() });
245
+ }
246
+ if (flags & 0x80) { // HAS_PARAM_NAMES
247
+ for (let p = 0; p < paramCount; p++) this.readU30();
248
+ }
249
+ methods.push({ returnType, paramTypes, nameIndex, flags, optionalValues, body: null });
250
+ }
251
+ return methods;
252
+ }
253
+
254
+ readMetadata() {
255
+ const count = this.readU30();
256
+ const metadata = [];
257
+ for (let i = 0; i < count; i++) {
258
+ const nameIndex = this.readU30();
259
+ const itemCount = this.readU30();
260
+ const keys = Array.from({ length: itemCount }, () => this.readU30());
261
+ const entries = keys.map(key => [key, this.readU30()]);
262
+ metadata.push({ nameIndex, entries });
263
+ }
264
+ return metadata;
265
+ }
266
+
267
+ readClassDefs(methodCount) {
268
+ const count = this.readU30();
269
+ const instances = [];
270
+ for (let i = 0; i < count; i++) {
271
+ const nameIndex = this.readU30();
272
+ const superNameIndex = this.readU30();
273
+ const flags = this.readU8();
274
+ const protectedNsIndex = flags & 0x08 ? this.readU30() : 0;
275
+ const ifaceCount = this.readU30();
276
+ const interfaces = [];
277
+ for (let j = 0; j < ifaceCount; j++) interfaces.push(this.readU30());
278
+ const iinit = this.readU30();
279
+ const traits = this.readTraits();
280
+ instances.push({ nameIndex, superNameIndex, flags, protectedNsIndex, interfaces, iinit, traits });
281
+ }
282
+ const classes = [];
283
+ for (let i = 0; i < count; i++) {
284
+ const cinit = this.readU30();
285
+ const traits = this.readTraits();
286
+ classes.push({ cinit, traits });
287
+ }
288
+ return { instances, classes };
289
+ }
290
+
291
+ readTraits() {
292
+ const count = this.readU30();
293
+ const traits = [];
294
+ for (let i = 0; i < count; i++) {
295
+ const nameIndex = this.readU30();
296
+ const kindAndAttrs = this.readU8();
297
+ const kind = kindAndAttrs & 0x0F;
298
+ const attrs = (kindAndAttrs >> 4) & 0x0F;
299
+ let trait = { nameIndex, kind, attrs };
300
+ switch (kind) {
301
+ case 0: case 6: // Slot, Const
302
+ trait.slotId = this.readU30();
303
+ trait.typeNameIndex = this.readU30();
304
+ trait.valueIndex = this.readU30();
305
+ if (trait.valueIndex !== 0) trait.valueKind = this.readU8();
306
+ break;
307
+ case 1: case 2: case 3: // Method, Getter, Setter
308
+ trait.dispId = this.readU30();
309
+ trait.methodIndex = this.readU30();
310
+ break;
311
+ case 4: // Class
312
+ trait.slotId = this.readU30();
313
+ trait.classIndex = this.readU30();
314
+ break;
315
+ case 5: // Function
316
+ trait.slotId = this.readU30();
317
+ trait.methodIndex = this.readU30();
318
+ break;
319
+ }
320
+ if (attrs & 0x04) { // ATTR_Metadata
321
+ const metaCount = this.readU30();
322
+ trait.metadataIndices = Array.from({ length: metaCount }, () => this.readU30());
323
+ }
324
+ traits.push(trait);
325
+ }
326
+ return traits;
327
+ }
328
+
329
+ readScripts() {
330
+ const count = this.readU30();
331
+ const scripts = [];
332
+ for (let i = 0; i < count; i++) {
333
+ const init = this.readU30();
334
+ const traits = this.readTraits();
335
+ scripts.push({ init, traits });
336
+ }
337
+ return scripts;
338
+ }
339
+
340
+ /** @param {AbcMethod[]} methods */
341
+ readMethodBodies(methods) {
342
+ const count = this.readU30();
343
+ for (let i = 0; i < count; i++) {
344
+ if (this.pos >= this.data.length) break;
345
+ const methodIndex = this.readU30();
346
+ const maxStack = this.readU30();
347
+ const localCount = this.readU30();
348
+ const initScopeDepth = this.readU30();
349
+ const maxScopeDepth = this.readU30();
350
+ const codeLen = this.readU30();
351
+ if (this.pos + codeLen > this.data.length) break;
352
+ const code = this.data.slice(this.pos, this.pos + codeLen);
353
+ this.pos += codeLen;
354
+ const excCount = this.readU30();
355
+ const exceptions = [];
356
+ for (let e = 0; e < excCount; e++) {
357
+ exceptions.push({
358
+ from: this.readU30(), to: this.readU30(), target: this.readU30(),
359
+ excType: this.readU30(), varName: this.readU30(),
360
+ });
361
+ }
362
+ const traits = this.readTraits();
363
+ if (methodIndex < methods.length) {
364
+ methods[methodIndex].body = { methodIndex, maxStack, localCount, initScopeDepth, maxScopeDepth, code, exceptions, traits };
365
+ }
366
+ }
367
+ }
368
+
369
+ // ---- Primitive readers ----
370
+
371
+ readU8() {
372
+ return this.data[this.pos++];
373
+ }
374
+
375
+ readU16() {
376
+ const v = this.data[this.pos] | (this.data[this.pos+1] << 8);
377
+ this.pos += 2;
378
+ return v;
379
+ }
380
+
381
+ readU32() {
382
+ return this.readU30();
383
+ }
384
+
385
+ readF64() {
386
+ const buf = new ArrayBuffer(8);
387
+ new Uint8Array(buf).set(this.data.slice(this.pos, this.pos + 8));
388
+ this.pos += 8;
389
+ return new Float64Array(buf)[0];
390
+ }
391
+
392
+ /** Variable-length unsigned 32-bit (U30/U32 in ABC spec) */
393
+ readU30() {
394
+ let result = 0, shift = 0;
395
+ while (shift < 35 && this.pos < this.data.length) {
396
+ const b = this.data[this.pos++];
397
+ result |= (b & 0x7F) << shift;
398
+ if (!(b & 0x80)) break;
399
+ shift += 7;
400
+ }
401
+ return result >>> 0;
402
+ }
403
+
404
+ /** ABC integers use the same variable-length bits for uint and int. */
405
+ readS32() {
406
+ return this.readU32() | 0;
407
+ }
408
+
409
+ readString() {
410
+ const len = this.readU30();
411
+ const end = Math.min(this.pos + len, this.data.length);
412
+ const s = utf8Decoder.decode(this.data.subarray(this.pos, end));
413
+ this.pos = end;
414
+ return s;
415
+ }
416
+ }