swf2js 0.1.2 → 0.4.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.
- package/README.md +81 -14
- package/package.json +11 -7
- package/studio/actionscript-roundtrip.test.js +54 -0
- package/studio/actionscript-scripts.test.js +169 -0
- package/studio/animated-image.test.js +137 -0
- package/studio/arithmetic.test.js +16 -0
- package/studio/as3-import.test.js +110 -0
- package/studio/audio-info.test.js +44 -0
- package/studio/components.test.js +152 -0
- package/studio/document-editing.test.js +92 -0
- package/studio/examples/01-space-mission.json +1628 -0
- package/studio/examples/02-clockwork-vault.json +1079 -0
- package/studio/examples/03-neon-synth.json +1364 -0
- package/studio/examples/04-kinetic-poster.json +923 -0
- package/studio/examples/05-signal-relay.json +1131 -0
- package/studio/examples/06-calculator.json +564 -0
- package/studio/frame-editing.test.js +129 -0
- package/studio/frame-events.test.js +117 -0
- package/studio/groups.test.js +88 -0
- package/studio/index.html +962 -0
- package/studio/interaction-triggers.test.js +37 -0
- package/studio/linked-actions.test.js +48 -0
- package/studio/lottie.test.js +208 -0
- package/studio/masks.test.js +43 -0
- package/studio/media-assets.test.js +62 -0
- package/studio/paint-tools.test.js +60 -0
- package/studio/playback.test.js +17 -0
- package/studio/project-io.test.js +405 -0
- package/studio/project-preview.test.js +475 -0
- package/studio/scene-transition.test.js +32 -0
- package/studio/server/archive-companions.js +27 -0
- package/studio/server/as3-library.js +91 -0
- package/studio/server/assets.js +162 -0
- package/studio/server/audio-info.js +82 -0
- package/studio/server/font-files.js +332 -0
- package/studio/server/http.js +135 -0
- package/studio/server/linked-actions.js +93 -0
- package/studio/server/png-decode.js +1 -0
- package/studio/server/project-movie.js +1164 -0
- package/studio/server/project-preview.js +152 -0
- package/studio/server/projects.js +370 -0
- package/studio/server/report.js +29 -0
- package/studio/server/routes.js +93 -0
- package/studio/server/swf-authoring.js +90 -0
- package/studio/server/swf-control-runtime.as +513 -0
- package/studio/server/swf-controls.js +119 -0
- package/studio/server/swf-export.js +293 -0
- package/studio/server/swf-import.js +1675 -0
- package/studio/server/swf-morphs.js +64 -0
- package/studio/server/swf-routes.js +42 -0
- package/studio/server/swf-streams.js +80 -0
- package/studio/server/swf-transition-runtime.as +52 -0
- package/studio/server/tween-recovery.js +106 -0
- package/studio/server-api.test.js +283 -0
- package/studio/server.js +320 -0
- package/studio/src/animated-image.js +423 -0
- package/studio/src/appearance-inspector.js +63 -0
- package/studio/src/arithmetic.js +50 -0
- package/studio/src/authored-controls.js +42 -0
- package/studio/src/busy.js +41 -0
- package/studio/src/canvas.js +430 -0
- package/studio/src/clipper-browser.js +2 -0
- package/studio/src/color-transform.js +38 -0
- package/studio/src/component-inspector.js +126 -0
- package/studio/src/components.js +268 -0
- package/studio/src/display-frame.js +262 -0
- package/studio/src/document-editing.js +69 -0
- package/studio/src/event-editor.js +150 -0
- package/studio/src/frame-editing.js +150 -0
- package/studio/src/frame-events.js +128 -0
- package/studio/src/frame-image.js +28 -0
- package/studio/src/groups.js +125 -0
- package/studio/src/handler-editor.js +122 -0
- package/studio/src/history.js +83 -0
- package/studio/src/imported-paths.js +99 -0
- package/studio/src/index.js +3582 -0
- package/studio/src/interaction-triggers.js +42 -0
- package/studio/src/json-patch.js +77 -0
- package/studio/src/layer-icons.js +25 -0
- package/studio/src/lottie-export.js +948 -0
- package/studio/src/lottie-import.js +1138 -0
- package/studio/src/masks.js +69 -0
- package/studio/src/media-assets.js +188 -0
- package/studio/src/media-library.js +193 -0
- package/studio/src/model.js +431 -0
- package/studio/src/numeric-fields.js +46 -0
- package/studio/src/object-list.js +57 -0
- package/studio/src/paint-tools.js +76 -0
- package/studio/src/placement-effects.js +74 -0
- package/studio/src/playback.js +10 -0
- package/studio/src/preview-audio.js +64 -0
- package/studio/src/preview-interactions.js +106 -0
- package/studio/src/preview-recorder.js +335 -0
- package/studio/src/preview-surface.js +338 -0
- package/studio/src/project-io.js +905 -0
- package/studio/src/project-session.js +375 -0
- package/studio/src/projects-dialog.js +319 -0
- package/studio/src/scene-transition.js +129 -0
- package/studio/src/script-editor.js +175 -0
- package/studio/src/script-references.js +74 -0
- package/studio/src/scripted-controls.js +146 -0
- package/studio/src/scripts.js +185 -0
- package/studio/src/stage-guides.js +101 -0
- package/studio/src/svg-animation.js +416 -0
- package/studio/src/svg-context.js +522 -0
- package/studio/src/svg-frame.js +413 -0
- package/studio/src/svg-import.js +1208 -0
- package/studio/src/swf-recording.js +104 -0
- package/studio/src/symbol-library.js +39 -0
- package/studio/src/symbols.js +62 -0
- package/studio/src/text-fields.js +58 -0
- package/studio/src/theme.js +43 -0
- package/studio/src/timeline.js +321 -0
- package/studio/src/vector-cut.js +103 -0
- package/studio/src/vector-geometry.js +254 -0
- package/studio/src/vector-style.js +146 -0
- package/studio/src/video-encode.js +84 -0
- package/studio/src/video-export.js +331 -0
- package/studio/src/video-mux.js +339 -0
- package/studio/src/video-playback.js +281 -0
- package/studio/src/workspace.js +344 -0
- package/studio/stroke-style.test.js +119 -0
- package/studio/styles.css +1547 -0
- package/studio/svg-export.test.js +162 -0
- package/studio/svg-import.test.js +318 -0
- package/studio/swf-authoring.test.js +140 -0
- package/studio/swf-export.test.js +324 -0
- package/studio/swf-import.test.js +489 -0
- package/studio/symbols.test.js +68 -0
- package/studio/tests/accessibility-interactions.mjs +138 -0
- package/studio/tests/appearance-interactions.mjs +103 -0
- package/studio/tests/control-interactions.mjs +60 -0
- package/studio/tests/event-interactions.mjs +117 -0
- package/studio/tests/external-resources.mjs +174 -0
- package/studio/tests/frame-image.mjs +71 -0
- package/studio/tests/html-export.mjs +68 -0
- package/studio/tests/import-files.mjs +131 -0
- package/studio/tests/lottie.mjs +125 -0
- package/studio/tests/mask-interactions.mjs +148 -0
- package/studio/tests/media-interactions.mjs +109 -0
- package/studio/tests/paint-interactions.mjs +64 -0
- package/studio/tests/project-interactions.mjs +244 -0
- package/studio/tests/script-interactions.mjs +178 -0
- package/studio/tests/scripted-control-interactions.mjs +107 -0
- package/studio/tests/svg-export.mjs +149 -0
- package/studio/tests/text-field-interactions.mjs +64 -0
- package/studio/tests/theme-interactions.mjs +53 -0
- package/studio/tests/timeline-browser.mjs +507 -0
- package/studio/tests/transition-interactions.mjs +94 -0
- package/studio/tests/trigger-interactions.mjs +93 -0
- package/studio/tests/video-export.mjs +243 -0
- package/studio/tests/workspace-interactions.mjs +226 -0
- package/studio/tween-recovery.test.js +31 -0
- package/studio/vector-style.test.js +56 -0
- package/studio/video-export.test.js +326 -0
- package/studiospec/actionscript.md +88 -0
- package/studiospec/architecture.md +378 -0
- package/studiospec/controls.md +100 -0
- package/studiospec/file-support.md +268 -0
- package/studiospec/lottie.md +69 -0
- package/studiospec/requirements.md +135 -0
- package/studiospec/stroke-style.md +95 -0
- package/studiospec/svg-export.md +83 -0
- package/studiospec/svg-import.md +139 -0
- package/studiospec/swooflib-integration.md +115 -0
- package/studiospec/tasks.md +573 -0
- package/studiospec/video-export.md +127 -0
- package/swf2js/__tests__/actionscript.test.js +1609 -0
- package/swf2js/__tests__/adpcm-audio.test.js +43 -0
- package/swf2js/__tests__/as2-compiler.test.js +153 -0
- package/swf2js/__tests__/as3-mixed-depth.test.js +498 -0
- package/swf2js/__tests__/authored-sprite-hit.test.js +118 -0
- package/swf2js/__tests__/authored-sprite-sizing.test.js +164 -0
- package/swf2js/__tests__/avm1-action-prefix.test.js +134 -0
- package/swf2js/__tests__/avm1-attach-movie.test.js +1151 -0
- package/swf2js/__tests__/avm1-boxhead-budget.test.js +73 -0
- package/swf2js/__tests__/avm1-cfg-bubble-tanks.test.js +101 -0
- package/swf2js/__tests__/avm1-cfg-mario-kart.test.js +184 -0
- package/swf2js/__tests__/avm1-cfg-mario.test.js +31 -0
- package/swf2js/__tests__/avm1-cfg-pacman.test.js +49 -0
- package/swf2js/__tests__/avm1-checkers-gameplay.test.js +122 -0
- package/swf2js/__tests__/avm1-clip-actions.test.js +2002 -0
- package/swf2js/__tests__/avm1-clone-drag.test.js +626 -0
- package/swf2js/__tests__/avm1-decompiler.test.js +102 -0
- package/swf2js/__tests__/avm1-dynamic-locals.test.js +159 -0
- package/swf2js/__tests__/avm1-formal-parameters.test.js +71 -0
- package/swf2js/__tests__/avm1-key-events.test.js +52 -0
- package/swf2js/__tests__/avm1-load-bitmap.test.js +75 -0
- package/swf2js/__tests__/avm1-loading-host-actions.test.js +764 -0
- package/swf2js/__tests__/avm1-multibyte-strings.test.js +63 -0
- package/swf2js/__tests__/avm1-pacman-gameplay.test.js +115 -0
- package/swf2js/__tests__/avm1-shape-hittest.test.js +311 -0
- package/swf2js/__tests__/avm1-shared-bodies.test.js +57 -0
- package/swf2js/__tests__/avm1-shared-runtime.test.js +1364 -0
- package/swf2js/__tests__/avm1-snake-gameplay.test.js +139 -0
- package/swf2js/__tests__/avm1-super.test.js +70 -0
- package/swf2js/__tests__/avm1-target-path.test.js +325 -0
- package/swf2js/__tests__/avm1-try.test.js +229 -0
- package/swf2js/__tests__/avm2-activation-closures.test.js +65 -0
- package/swf2js/__tests__/avm2-applytype.test.js +180 -0
- package/swf2js/__tests__/avm2-call-ops.test.js +232 -0
- package/swf2js/__tests__/avm2-callsuperid.test.js +231 -0
- package/swf2js/__tests__/avm2-class-initializer.test.js +219 -0
- package/swf2js/__tests__/avm2-custom-namespace.test.js +139 -0
- package/swf2js/__tests__/avm2-describe-type.test.js +96 -0
- package/swf2js/__tests__/avm2-dictionary.test.js +96 -0
- package/swf2js/__tests__/avm2-domain-memory.test.js +332 -0
- package/swf2js/__tests__/avm2-e4x.test.js +326 -0
- package/swf2js/__tests__/avm2-exceptions.test.js +81 -0
- package/swf2js/__tests__/avm2-findprop.test.js +110 -0
- package/swf2js/__tests__/avm2-getlex.test.js +166 -0
- package/swf2js/__tests__/avm2-global-slot.test.js +106 -0
- package/swf2js/__tests__/avm2-interface.test.js +95 -0
- package/swf2js/__tests__/avm2-multiname-precedence.test.js +148 -0
- package/swf2js/__tests__/avm2-named-namespace-types.test.js +106 -0
- package/swf2js/__tests__/avm2-optional-args.test.js +100 -0
- package/swf2js/__tests__/avm2-outer-scopes.test.js +101 -0
- package/swf2js/__tests__/avm2-package-internal-class.test.js +106 -0
- package/swf2js/__tests__/avm2-protected-namespace.test.js +105 -0
- package/swf2js/__tests__/avm2-protected.test.js +214 -0
- package/swf2js/__tests__/avm2-qwop-gameplay.test.js +116 -0
- package/swf2js/__tests__/avm2-runtime-namespaces.test.js +159 -0
- package/swf2js/__tests__/avm2-script-traits.test.js +89 -0
- package/swf2js/__tests__/avm2-shared-runtime.test.js +52 -0
- package/swf2js/__tests__/avm2-slot-ops.test.js +117 -0
- package/swf2js/__tests__/avm2-type-ops.test.js +224 -0
- package/swf2js/__tests__/bevel-filter.test.js +116 -0
- package/swf2js/__tests__/bitmap-button-mask.test.js +106 -0
- package/swf2js/__tests__/bitmap-data-draw.test.js +388 -0
- package/swf2js/__tests__/bitmap-decoder.test.js +108 -0
- package/swf2js/__tests__/bitmapdata-color-bounds.test.js +70 -0
- package/swf2js/__tests__/bitmapdata-copy-channel.test.js +91 -0
- package/swf2js/__tests__/bitmapdata-copy-pixels.test.js +136 -0
- package/swf2js/__tests__/bitmapdata-fill-compare.test.js +241 -0
- package/swf2js/__tests__/bitmapdata-filter.test.js +232 -0
- package/swf2js/__tests__/bitmapdata-hit-test.test.js +71 -0
- package/swf2js/__tests__/bitmapdata-merge.test.js +84 -0
- package/swf2js/__tests__/bitmapdata-palette-map.test.js +92 -0
- package/swf2js/__tests__/bitmapdata-pixels-io.test.js +198 -0
- package/swf2js/__tests__/bitmapdata-threshold.test.js +97 -0
- package/swf2js/__tests__/bitmapdata-vector.test.js +82 -0
- package/swf2js/__tests__/blur-filter.test.js +48 -0
- package/swf2js/__tests__/bundle-emitter.test.js +394 -0
- package/swf2js/__tests__/button-actions.test.js +370 -0
- package/swf2js/__tests__/button-color-transform.test.js +48 -0
- package/swf2js/__tests__/button-decoder.test.js +106 -0
- package/swf2js/__tests__/button-hit-test.test.js +825 -0
- package/swf2js/__tests__/button-scaling-grid.test.js +124 -0
- package/swf2js/__tests__/button-sound.test.js +105 -0
- package/swf2js/__tests__/bytearray-coercion.test.js +23 -0
- package/swf2js/__tests__/bytearray-object-serialization.test.js +380 -0
- package/swf2js/__tests__/cfg-switch-loop.test.js +103 -0
- package/swf2js/__tests__/clip-depth-mask.test.js +119 -0
- package/swf2js/__tests__/clipped-bitmap-fill.test.js +118 -0
- package/swf2js/__tests__/companion-swf.test.js +638 -0
- package/swf2js/__tests__/context-menu.test.js +149 -0
- package/swf2js/__tests__/convolution-filter.test.js +74 -0
- package/swf2js/__tests__/definefont4.test.js +280 -0
- package/swf2js/__tests__/display-displacement.test.js +161 -0
- package/swf2js/__tests__/display-list.test.js +80 -0
- package/swf2js/__tests__/display-mask-render.test.js +656 -0
- package/swf2js/__tests__/displayobject-stage.test.js +320 -0
- package/swf2js/__tests__/embedded-script-audio.test.js +131 -0
- package/swf2js/__tests__/embedded-swf.test.js +202 -0
- package/swf2js/__tests__/embedded-textfield.test.js +107 -0
- package/swf2js/__tests__/embedded-timeline-audio.test.js +182 -0
- package/swf2js/__tests__/embedded-timeline-input.test.js +420 -0
- package/swf2js/__tests__/embedded-timer-lifecycle.test.js +145 -0
- package/swf2js/__tests__/event-priority-order.test.js +125 -0
- package/swf2js/__tests__/event-weak-listeners.test.js +53 -0
- package/swf2js/__tests__/event-will-trigger.test.js +43 -0
- package/swf2js/__tests__/flash-text-engine-content.test.js +115 -0
- package/swf2js/__tests__/flash-text-engine-format.test.js +95 -0
- package/swf2js/__tests__/flash-text-engine-line.test.js +314 -0
- package/swf2js/__tests__/font-encoder.test.js +95 -0
- package/swf2js/__tests__/font-outline-text.test.js +659 -0
- package/swf2js/__tests__/glow-shadow-filter.test.js +68 -0
- package/swf2js/__tests__/graphics-bitmap-smoothing.test.js +32 -0
- package/swf2js/__tests__/graphics-fill-lifetime.test.js +36 -0
- package/swf2js/__tests__/graphics-gradient-matrix.test.js +53 -0
- package/swf2js/__tests__/graphics-stroke-hit-test.test.js +457 -0
- package/swf2js/__tests__/hit-test-primitives.test.js +414 -0
- package/swf2js/__tests__/input-events.test.js +714 -0
- package/swf2js/__tests__/integration/corpus.test.js +41 -0
- package/swf2js/__tests__/loader-embedded-swf.test.js +210 -0
- package/swf2js/__tests__/loader-info.test.js +81 -0
- package/swf2js/__tests__/local-connection.test.js +122 -0
- package/swf2js/__tests__/morph-focal-gradient.test.js +51 -0
- package/swf2js/__tests__/morph-topology.test.js +267 -0
- package/swf2js/__tests__/movie-data-codec.test.js +128 -0
- package/swf2js/__tests__/nellymoser-audio.test.js +84 -0
- package/swf2js/__tests__/opaque-background-cache.test.js +352 -0
- package/swf2js/__tests__/path-stream.test.js +81 -0
- package/swf2js/__tests__/place-object-filters.test.js +88 -0
- package/swf2js/__tests__/place-object3-decoder.test.js +116 -0
- package/swf2js/__tests__/place-object3-image.test.js +85 -0
- package/swf2js/__tests__/player-version.test.js +46 -0
- package/swf2js/__tests__/root-textfield.test.js +287 -0
- package/swf2js/__tests__/scaling-grid.test.js +183 -0
- package/swf2js/__tests__/shape-decoder.test.js +361 -0
- package/swf2js/__tests__/shape-line-style2.test.js +138 -0
- package/swf2js/__tests__/shape-nonscaling-stroke.test.js +131 -0
- package/swf2js/__tests__/shape-pixel-hint.test.js +101 -0
- package/swf2js/__tests__/simplebutton-pointer.test.js +1477 -0
- package/swf2js/__tests__/socket-offline.test.js +67 -0
- package/swf2js/__tests__/sound-avm1-stop.test.js +99 -0
- package/swf2js/__tests__/sound-envelope.test.js +110 -0
- package/swf2js/__tests__/sound-extract.test.js +168 -0
- package/swf2js/__tests__/sound-id3.test.js +283 -0
- package/swf2js/__tests__/sound-load.test.js +259 -0
- package/swf2js/__tests__/sound-mixer-spectrum.test.js +261 -0
- package/swf2js/__tests__/sound-stream-sprite.test.js +624 -0
- package/swf2js/__tests__/sound-transform.test.js +104 -0
- package/swf2js/__tests__/speex-audio.test.js +77 -0
- package/swf2js/__tests__/stage-noscale.test.js +45 -0
- package/swf2js/__tests__/swf-encoder.test.js +134 -0
- package/swf2js/__tests__/swflib-assemble.test.js +20 -0
- package/swf2js/__tests__/textfield-html-text.test.js +368 -0
- package/swf2js/__tests__/textfield-line-layout.test.js +339 -0
- package/swf2js/__tests__/textfield-programmatic-overlay.test.js +2506 -0
- package/swf2js/__tests__/textfield-scroll.test.js +79 -0
- package/swf2js/__tests__/textfield-selection.test.js +383 -0
- package/swf2js/__tests__/timeline-effect-surfaces.test.js +295 -0
- package/swf2js/__tests__/timeline-named-anchor.test.js +139 -0
- package/swf2js/__tests__/timeline-scenes.test.js +1723 -0
- package/swf2js/__tests__/urlloader-http-status.test.js +236 -0
- package/{src → swf2js}/cli.js +43 -11
- package/swf2js/compiler/as2-classes.js +156 -0
- package/swf2js/compiler/as2-parser.js +598 -0
- package/swf2js/compiler/avm1-codegen.js +897 -0
- package/swf2js/compiler/index.js +28 -0
- package/{src → swf2js}/decoder/button-decoder.js +3 -1
- package/swf2js/decoder/png.js +114 -0
- package/{src → swf2js}/decoder/shape-decoder.js +20 -10
- package/{src → swf2js}/decoder/tag-decoder.js +5 -3
- package/{src → swf2js}/decoder/tag-types.js +4 -0
- package/swf2js/decompiler/avm1-decompiler.js +1138 -0
- package/{src → swf2js}/emitter/asset-encoder.js +66 -113
- package/{src → swf2js}/emitter/bundle-emitter.js +209 -78
- package/swf2js/emitter/external-resources.js +39 -0
- package/swf2js/emitter/movie-data-codec.js +91 -0
- package/swf2js/encoder/bit-writer.js +182 -0
- package/swf2js/encoder/shape-encoder.js +310 -0
- package/swf2js/encoder/swf-builder.js +639 -0
- package/swf2js/encoder/swf-writer.js +55 -0
- package/{src/runtime/swflib/assemble.js → swf2js/runtime/assemble-swooflib.js} +4 -3
- package/{src → swf2js}/runtime/avm1-runtime.js +102 -63
- package/{src → swf2js}/runtime/index.js +1 -1
- package/{src → swf2js}/runtime/movie-runtime.js +28 -2
- package/{src → swf2js}/transpiler/actionscript.js +82 -19
- package/{src → swf2js}/transpiler/avm1/avm1-opcodes.js +5 -3
- package/{src → swf2js}/transpiler/avm1/avm1-transpiler.js +69 -6
- package/{src → swf2js}/transpiler/avm2/avm2-transpiler.js +1 -1
- package/{src → swf2js}/transpiler/timeline/display-list.js +41 -22
- package/{src → swf2js}/transpiler/timeline/timeline-codegen.js +97 -36
- package/swooflib/README.md +569 -0
- package/swooflib/adpcm.js +147 -0
- package/{src/runtime/swflib → swooflib}/builtins.js +6 -0
- package/{src/runtime/swflib → swooflib}/context-menu.js +7 -1
- package/swooflib/control-definitions.js +42 -0
- package/swooflib/control-widget.js +763 -0
- package/swooflib/controls.js +300 -0
- package/{src/runtime/swflib → swooflib}/display.js +16 -10
- package/{src/runtime/swflib → swooflib}/events.js +11 -0
- package/{src/runtime/swflib → swooflib}/flash.js +6 -5
- package/{src/runtime/swflib → swooflib}/graphics.js +81 -19
- package/{src/runtime/swflib → swooflib}/input.js +12 -14
- package/{src/runtime/swflib → swooflib}/loader.js +52 -22
- package/swooflib/movie-network.js +86 -0
- package/swooflib/path-stream.js +169 -0
- package/{src/runtime/swflib → swooflib}/render.js +3 -13
- package/{src/runtime/swflib → swooflib}/sound.js +25 -2
- package/{src/runtime/swflib → swooflib}/stage.js +3 -3
- package/swooflib/stream-time.js +21 -0
- package/{src/runtime/swflib → swooflib}/system.js +88 -37
- package/{src/runtime/swflib → swooflib}/text-paint.js +14 -3
- package/{src/runtime/swflib → swooflib}/text.js +8 -0
- package/swooflib/xml-node.js +53 -0
- package/src/emitter/timeline-data-codec.js +0 -58
- /package/{src → swf2js}/decoder/symbol-table.js +0 -0
- /package/{src → swf2js}/emitter/embedded-swf.js +0 -0
- /package/{src → swf2js}/emitter/font-encoder.js +0 -0
- /package/{src → swf2js}/model/swf-document.js +0 -0
- /package/{src → swf2js}/model/transpile-result.js +0 -0
- /package/{src → swf2js}/parser/bit-reader.js +0 -0
- /package/{src → swf2js}/parser/decompressor.js +0 -0
- /package/{src → swf2js}/parser/swf-parser.js +0 -0
- /package/{src → swf2js}/pretty-printer/pretty-printer.js +0 -0
- /package/{src → swf2js}/runtime/avm2-runtime.js +0 -0
- /package/{src → swf2js}/runtime/embedded-movie-runtime.js +0 -0
- /package/{src → swf2js}/transpiler/avm1/avm1-ir.js +0 -0
- /package/{src → swf2js}/transpiler/avm1/avm1-rewrite.js +0 -0
- /package/{src → swf2js}/transpiler/avm2/abc-reader.js +0 -0
- /package/{src → swf2js}/transpiler/cfg-structurer.js +0 -0
- /package/{src → swf2js}/util/diagnostics.js +0 -0
- /package/{src → swf2js}/util/logger.js +0 -0
- /package/{src/runtime/swflib → swooflib}/bitmap-image.js +0 -0
- /package/{src/runtime/swflib → swooflib}/bitmap.js +0 -0
- /package/{src/runtime/swflib → swooflib}/bytearray.js +0 -0
- /package/{src/runtime/swflib → swooflib}/compiled-swf.js +0 -0
- /package/{src/runtime/swflib → swooflib}/cursor-flag.js +0 -0
- /package/{src/runtime/swflib → swooflib}/dictionary.js +0 -0
- /package/{src/runtime/swflib → swooflib}/filters.js +0 -0
- /package/{src/runtime/swflib → swooflib}/geom.js +0 -0
- /package/{src/runtime/swflib → swooflib}/glyph-metrics.js +0 -0
- /package/{src/runtime/swflib → swooflib}/host.js +0 -0
- /package/{src/runtime/swflib → swooflib}/inherit.js +0 -0
- /package/{src/runtime/swflib → swooflib}/marks.js +0 -0
- /package/{src/runtime/swflib → swooflib}/matrix3d.js +0 -0
- /package/{src/runtime/swflib → swooflib}/pointer-bridge.js +0 -0
- /package/{src/runtime/swflib → swooflib}/projection.js +0 -0
- /package/{src/runtime/swflib → swooflib}/sprite-drag.js +0 -0
- /package/{src/runtime/swflib → swooflib}/stage-ref.js +0 -0
- /package/{src/runtime/swflib → swooflib}/text-field.js +0 -0
- /package/{src/runtime/swflib → swooflib}/transform.js +0 -0
- /package/{src/runtime/swflib → swooflib}/xml.js +0 -0
|
@@ -0,0 +1,1138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AVM1 bytecode → ActionScript source (studiospec/actionscript.md §4).
|
|
3
|
+
*
|
|
4
|
+
* The output must compile back (swf2js/compiler) to equivalent bytecode, so readability never
|
|
5
|
+
* wins over exactness: values kept in registers read back as `_rN` variables, conditions keep
|
|
6
|
+
* their stack form, and control flow the structurer cannot express falls back to its labeled
|
|
7
|
+
* dispatcher. Anything with no ActionScript form (AS2 class plumbing, Flash 4 frame waits, stack
|
|
8
|
+
* values that cross statements) throws a DecompileError, and Studio keeps such a script as bytecode.
|
|
9
|
+
*
|
|
10
|
+
* Expressions are rebuilt by symbolic execution of each basic block. `&&`, `||` and `?:`, whose
|
|
11
|
+
* value crosses a branch, are folded back into expressions before blocks are formed.
|
|
12
|
+
*/
|
|
13
|
+
import { HAS_LENGTH, OP } from '../transpiler/avm1/avm1-opcodes.js';
|
|
14
|
+
import { DF2 } from '../transpiler/avm1/avm1-ir.js';
|
|
15
|
+
import { structureCfg } from '../transpiler/cfg-structurer.js';
|
|
16
|
+
import { CLIP_PROPERTIES } from '../compiler/avm1-codegen.js';
|
|
17
|
+
|
|
18
|
+
export class DecompileError extends Error {
|
|
19
|
+
constructor(message, offset) {
|
|
20
|
+
super(offset === undefined ? message : `${message} (at byte ${offset})`);
|
|
21
|
+
this.name = 'DecompileError';
|
|
22
|
+
this.reason = message;
|
|
23
|
+
this.offset = offset;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ---- Expressions: { text, prec } plus optional facts used by later folding ----
|
|
28
|
+
|
|
29
|
+
const PREC = { sequence: 1, assign: 3, conditional: 4, or: 5, and: 6, bitor: 7, bitxor: 8, bitand: 9, equality: 10,
|
|
30
|
+
relational: 11, shift: 12, additive: 13, multiplicative: 14, unary: 15, postfix: 16, call: 18, member: 19, primary: 20 };
|
|
31
|
+
|
|
32
|
+
const BINARY = {
|
|
33
|
+
[OP.Add2]: ['+', PREC.additive], [OP.Subtract]: ['-', PREC.additive], [OP.Multiply]: ['*', PREC.multiplicative],
|
|
34
|
+
[OP.Divide]: ['/', PREC.multiplicative], [OP.Modulo]: ['%', PREC.multiplicative],
|
|
35
|
+
[OP.LessThan2]: ['<', PREC.relational], [OP.Greater]: ['>', PREC.relational], [OP.InstanceOf]: ['instanceof', PREC.relational],
|
|
36
|
+
[OP.Equals2]: ['==', PREC.equality], [OP.StrictEquals]: ['===', PREC.equality],
|
|
37
|
+
[OP.BitAnd]: ['&', PREC.bitand], [OP.BitOr]: ['|', PREC.bitor], [OP.BitXor]: ['^', PREC.bitxor],
|
|
38
|
+
[OP.BitLShift]: ['<<', PREC.shift], [OP.BitRShift]: ['>>', PREC.shift], [OP.BitURShift]: ['>>>', PREC.shift],
|
|
39
|
+
};
|
|
40
|
+
const NEGATED = { '<': '>=', '>': '<=', '==': '!=', '===': '!==' };
|
|
41
|
+
|
|
42
|
+
const RESERVED = new Set(['break', 'case', 'catch', 'continue', 'default', 'delete', 'do', 'else', 'false', 'finally', 'for',
|
|
43
|
+
'function', 'if', 'in', 'instanceof', 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'var',
|
|
44
|
+
'void', 'while', 'with', 'class', 'interface', 'extends', 'implements', 'import', 'undefined', 'NaN', 'Infinity', 'newline']);
|
|
45
|
+
/** Names the compiler turns into actions when called; a CallFunction with such a name is written another way. */
|
|
46
|
+
const ACTION_NAMES = new Set(['trace', 'stop', 'play', 'nextFrame', 'prevFrame', 'stopAllSounds', 'toggleHighQuality', 'stopDrag',
|
|
47
|
+
'removeMovieClip', 'call', 'random', 'getTimer', 'int', 'chr', 'ord', 'mbchr', 'mbord', 'length', 'mblength', 'substring',
|
|
48
|
+
'mbsubstring', 'targetPath', 'eval', 'gotoAndPlay', 'gotoAndStop', 'getVersion', 'set', 'getURL', 'fscommand', 'loadMovie',
|
|
49
|
+
'loadMovieNum', 'loadVariables', 'loadVariablesNum', 'unloadMovie', 'unloadMovieNum', 'duplicateMovieClip', 'startDrag',
|
|
50
|
+
'setProperty', 'getProperty', 'tellTarget', 'on', 'onClipEvent']);
|
|
51
|
+
|
|
52
|
+
const isIdentifier = name => typeof name === 'string' && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) && !RESERVED.has(name);
|
|
53
|
+
const quote = text => JSON.stringify(text).replace(/[\u2028\u2029]/g, char => `\\u${char.charCodeAt(0).toString(16)}`);
|
|
54
|
+
|
|
55
|
+
const expr = (text, prec = PREC.primary, extra) => ({ text, prec, ...extra });
|
|
56
|
+
const literal = value => {
|
|
57
|
+
if (value === undefined) return expr('undefined', PREC.primary, { literal: true, value });
|
|
58
|
+
if (value === null) return expr('null', PREC.primary, { literal: true, value });
|
|
59
|
+
if (typeof value === 'string') return expr(quote(value), PREC.primary, { literal: true, value });
|
|
60
|
+
if (typeof value === 'boolean') return expr(String(value), PREC.primary, { literal: true, value });
|
|
61
|
+
if (Number.isNaN(value)) return expr('NaN', PREC.primary, { literal: true, value });
|
|
62
|
+
if (value === Infinity) return expr('Infinity', PREC.primary, { literal: true, value });
|
|
63
|
+
if (value === -Infinity) return expr('-Infinity', PREC.unary, { literal: true, value });
|
|
64
|
+
if (Object.is(value, -0)) return expr('-0', PREC.unary, { literal: true, value });
|
|
65
|
+
return expr(String(value), value < 0 ? PREC.unary : PREC.primary, { literal: true, value });
|
|
66
|
+
};
|
|
67
|
+
const wrap = (e, prec) => e.prec < prec ? `(${e.text})` : e.text;
|
|
68
|
+
const binary = (left, op, right, prec) => expr(`${wrap(left, prec)} ${op} ${wrap(right, prec + 1)}`, prec, { binary: { left, op, right } });
|
|
69
|
+
const memberText = (object, name) => {
|
|
70
|
+
const base = object.literal && typeof object.value === 'number' ? `(${object.text})` : wrap(object, PREC.member);
|
|
71
|
+
return name.literal && isIdentifier(name.value) ? `${base}.${name.value}` : `${base}[${name.text}]`;
|
|
72
|
+
};
|
|
73
|
+
const callArgs = args => args.map(arg => wrap(arg, PREC.assign)).join(', ');
|
|
74
|
+
|
|
75
|
+
// ---- Actions ----
|
|
76
|
+
|
|
77
|
+
function readString(bytes, start, swfVersion) {
|
|
78
|
+
let end = start;
|
|
79
|
+
while (end < bytes.length && bytes[end] !== 0) end++;
|
|
80
|
+
const slice = bytes.subarray(start, end);
|
|
81
|
+
const text = swfVersion >= 6 ? new TextDecoder().decode(slice) : String.fromCharCode(...slice);
|
|
82
|
+
return { text, next: end + 1 };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function decodeActions(bytes, start, end) {
|
|
86
|
+
const actions = [];
|
|
87
|
+
let pc = start;
|
|
88
|
+
while (pc < end) {
|
|
89
|
+
const op = bytes[pc];
|
|
90
|
+
if (op === 0) { actions.push({ offset: pc, op, data: new Uint8Array(0), size: 1 }); pc++; continue; }
|
|
91
|
+
let data = new Uint8Array(0), size = 1;
|
|
92
|
+
if (op >= 0x80) {
|
|
93
|
+
const length = bytes[pc + 1] | (bytes[pc + 2] << 8);
|
|
94
|
+
data = bytes.subarray(pc + 3, pc + 3 + length);
|
|
95
|
+
size = 3 + length;
|
|
96
|
+
}
|
|
97
|
+
actions.push({ offset: pc, op, data, size });
|
|
98
|
+
pc += size;
|
|
99
|
+
}
|
|
100
|
+
return actions;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const ui16 = (data, at) => data[at] | (data[at + 1] << 8);
|
|
104
|
+
const si16 = (data, at) => { const value = ui16(data, at); return value & 0x8000 ? value - 0x10000 : value; };
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Decompile AVM1 bytecode (one action list) to ActionScript.
|
|
108
|
+
* Options: `swfVersion` (string encoding and naming rules; default 8).
|
|
109
|
+
*/
|
|
110
|
+
export function decompileAvm1(bytecode, { swfVersion = 8 } = {}) {
|
|
111
|
+
const bytes = bytecode instanceof Uint8Array ? bytecode : new Uint8Array(bytecode);
|
|
112
|
+
const state = { swfVersion, pool: [] };
|
|
113
|
+
const lines = unit(bytes, 0, bytes.length, state, { registers: new Map(), inFunction: false });
|
|
114
|
+
return reindent(lines.join('\n'));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Decompile the actions in [start, end) as one body. `scope.registers` names registers
|
|
119
|
+
* (DefineFunction2 parameters and preloads); `scope.exits` maps byte offsets outside the body to
|
|
120
|
+
* the statement that leaves it (`break;`/`continue;` of an enclosing for…in).
|
|
121
|
+
*/
|
|
122
|
+
function unit(bytes, start, end, state, scope) {
|
|
123
|
+
const actions = decodeActions(bytes, start, end);
|
|
124
|
+
const indexAt = new Map(actions.map((action, index) => [action.offset, index]));
|
|
125
|
+
const endOffset = end;
|
|
126
|
+
const fail = (message, action) => { throw new DecompileError(message, action?.offset); };
|
|
127
|
+
|
|
128
|
+
// Jump targets, and which actions jump there. Jumps inside nested bodies (functions, with,
|
|
129
|
+
// try) belong to those bodies, which are decompiled on their own.
|
|
130
|
+
const target = action => action.offset + action.size + si16(action.data, 0);
|
|
131
|
+
const nested = [];
|
|
132
|
+
for (const action of actions) {
|
|
133
|
+
const after = action.offset + action.size;
|
|
134
|
+
if (action.op === OP.DefineFunction || action.op === OP.DefineFunction2) {
|
|
135
|
+
const data = action.data;
|
|
136
|
+
nested.push([after, after + ui16(data, data.length - 2)]);
|
|
137
|
+
} else if (action.op === OP.With) {
|
|
138
|
+
nested.push([after, after + ui16(action.data, 0)]);
|
|
139
|
+
} else if (action.op === OP.Try) {
|
|
140
|
+
nested.push([after, after + ui16(action.data, 1) + ui16(action.data, 3) + ui16(action.data, 5)]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const inNested = offset => nested.some(([from, to]) => offset >= from && offset < to);
|
|
144
|
+
const sources = new Map();
|
|
145
|
+
actions.forEach((action, index) => {
|
|
146
|
+
if (action.op !== OP.Jump && action.op !== OP.If) return;
|
|
147
|
+
if (inNested(action.offset)) return;
|
|
148
|
+
const to = target(action);
|
|
149
|
+
if (!sources.has(to)) sources.set(to, []);
|
|
150
|
+
sources.get(to).push(index);
|
|
151
|
+
});
|
|
152
|
+
const indexOfOffset = offset => {
|
|
153
|
+
if (offset === endOffset) return actions.length;
|
|
154
|
+
if (!indexAt.has(offset)) return -1;
|
|
155
|
+
return indexAt.get(offset);
|
|
156
|
+
};
|
|
157
|
+
// Folds being built, innermost last: { join, sources }. Flash lets nested `&&`, `||` and `?:`
|
|
158
|
+
// jump straight to their enclosing expression's join.
|
|
159
|
+
const openFolds = [];
|
|
160
|
+
/** Whether every jump into (fromIndex, toIndex] comes from inside [fromIndex, toIndex) or from an enclosing fold to its join. */
|
|
161
|
+
const sealed = (fromIndex, toIndex) => {
|
|
162
|
+
for (const [offset, list] of sources) {
|
|
163
|
+
const index = indexOfOffset(offset);
|
|
164
|
+
if (index <= fromIndex || index > toIndex) continue;
|
|
165
|
+
const outside = list.filter(source => source < fromIndex || source >= toIndex);
|
|
166
|
+
if (outside.some(source => !openFolds.some(fold => fold.join === index && fold.sources.includes(source)))) return false;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
};
|
|
170
|
+
/** Run a sub-range of a fold, with the fold's own jumps to `join` allowed inside it. */
|
|
171
|
+
const runFoldPart = (from, to, join, foldSources) => {
|
|
172
|
+
openFolds.push({ join, sources: foldSources });
|
|
173
|
+
try {
|
|
174
|
+
return run(from, to, { stack: [] });
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (error instanceof DecompileError) return null;
|
|
177
|
+
throw error;
|
|
178
|
+
} finally {
|
|
179
|
+
openFolds.pop();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const registerName = number => scope.registers.get(number) ?? `_r${number}`;
|
|
184
|
+
const usedRegisters = new Set();
|
|
185
|
+
// Names used as variables (a register is never declared under a name a variable already has).
|
|
186
|
+
const variableNames = scope.variableNames ?? new Set();
|
|
187
|
+
// Pushes whose first value (a register read) belongs to the assignment before them.
|
|
188
|
+
const consumedReads = new Set();
|
|
189
|
+
// Join points of folded expressions: positions inside an expression, never block starts.
|
|
190
|
+
const consumedJoins = new Set();
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Symbolically run actions [from, to). Stops at a branch or jump that is not part of an
|
|
194
|
+
* expression, at `stopAtLeaders` boundaries, or at `to`. Returns the stack, the statements
|
|
195
|
+
* and how the run ended.
|
|
196
|
+
*/
|
|
197
|
+
function run(from, to, { stack = [], leaders = null } = {}) {
|
|
198
|
+
const stmts = [];
|
|
199
|
+
let i = from;
|
|
200
|
+
const pop = () => {
|
|
201
|
+
if (!stack.length) fail('A value is used that was pushed before this point (stack across statements)', actions[i]);
|
|
202
|
+
const value = stack.pop();
|
|
203
|
+
// Read before a statement that ran since: printed in place, it would be read after it.
|
|
204
|
+
if (value.stale) fail('A value is used after statements that ran since it was read (stack across statements)', actions[i]);
|
|
205
|
+
return value;
|
|
206
|
+
};
|
|
207
|
+
const popCount = () => {
|
|
208
|
+
const count = pop();
|
|
209
|
+
if (!count.literal || typeof count.value !== 'number' || count.value < 0 || count.value > 65535) fail('Argument count is not a constant', actions[i]);
|
|
210
|
+
return count.value;
|
|
211
|
+
};
|
|
212
|
+
const statement = text => {
|
|
213
|
+
stmts.push(text);
|
|
214
|
+
for (let k = 0; k < stack.length; k++) if (!stack[k].literal) stack[k] = { ...stack[k], stale: true };
|
|
215
|
+
};
|
|
216
|
+
const nameOf = e => {
|
|
217
|
+
if (!e.literal) return null;
|
|
218
|
+
return String(e.value);
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Assign `value` to `targetText`. When the stored value is read back right away (Flash keeps
|
|
222
|
+
* it in a register for that), the assignment is an expression: returns true after pushing it.
|
|
223
|
+
*/
|
|
224
|
+
const assignTo = (targetText, value) => {
|
|
225
|
+
// Postfix with its value used: x = Increment(r = x); then r is read.
|
|
226
|
+
if (value.step && value.of.stored !== undefined && value.of.value.text === targetText && readsBackRegister(value.of.stored, i + 1)) {
|
|
227
|
+
consumedReads.add(i + 1);
|
|
228
|
+
stack.push(expr(`${targetText}${value.step}`, PREC.postfix, { assignment: true }));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (value.stored !== undefined && readsBackRegister(value.stored, i + 1)) {
|
|
232
|
+
consumedReads.add(i + 1);
|
|
233
|
+
const text = assignmentText(targetText, value.value, true);
|
|
234
|
+
stack.push(expr(text, /^(\+\+|--)/.test(text) ? PREC.unary : PREC.assign, { assignment: true }));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
statement(`${assignmentText(targetText, value, false)};`);
|
|
238
|
+
};
|
|
239
|
+
// A value read back right after being stored: `name = v` as an expression.
|
|
240
|
+
const readsBackRegister = (register, nextIndex) => {
|
|
241
|
+
const next = actions[nextIndex];
|
|
242
|
+
if (!next || next.op !== OP.Push || next.data[0] !== 4 || next.data[1] !== register) return false;
|
|
243
|
+
// Only this read: the next action that touches the register must store it again.
|
|
244
|
+
for (let k = nextIndex + 1; k < actions.length; k++) {
|
|
245
|
+
const action = actions[k];
|
|
246
|
+
if (action.op === OP.StoreRegister && action.data[0] === register) return true;
|
|
247
|
+
if (action.op === OP.Push && pushReadsRegister(action, register)) return false;
|
|
248
|
+
if (action.op === OP.DefineFunction || action.op === OP.DefineFunction2) continue;
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
while (i < to) {
|
|
254
|
+
if (leaders && i !== from && leaders.has(i) && !consumedJoins.has(i)) return { stack, stmts, end: i, kind: 'fallthrough' };
|
|
255
|
+
const action = actions[i];
|
|
256
|
+
const { op, data } = action;
|
|
257
|
+
switch (op) {
|
|
258
|
+
case 0x00: i = to; continue;
|
|
259
|
+
case OP.ConstantPool: {
|
|
260
|
+
const count = ui16(data, 0);
|
|
261
|
+
const pool = [];
|
|
262
|
+
let at = 2;
|
|
263
|
+
for (let k = 0; k < count; k++) {
|
|
264
|
+
const { text, next } = readString(data, at, state.swfVersion);
|
|
265
|
+
pool.push(text);
|
|
266
|
+
at = next;
|
|
267
|
+
}
|
|
268
|
+
state.pool = pool;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case OP.Push: {
|
|
272
|
+
let at = consumedReads.has(i) ? 2 : 0;
|
|
273
|
+
while (at < data.length) {
|
|
274
|
+
const type = data[at++];
|
|
275
|
+
switch (type) {
|
|
276
|
+
case 0: { const { text, next } = readString(data, at, state.swfVersion); stack.push(literal(text)); at = next; break; }
|
|
277
|
+
case 1: stack.push(literal(new DataView(data.buffer, data.byteOffset + at, 4).getFloat32(0, true))); at += 4; break;
|
|
278
|
+
case 2: stack.push(literal(null)); break;
|
|
279
|
+
case 3: stack.push(literal(undefined)); break;
|
|
280
|
+
case 4: {
|
|
281
|
+
const register = data[at++];
|
|
282
|
+
usedRegisters.add(register);
|
|
283
|
+
stack.push(expr(registerName(register), PREC.primary, { register }));
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case 5: stack.push(literal(!!data[at++])); break;
|
|
287
|
+
case 6: {
|
|
288
|
+
const buffer = new Uint8Array(8);
|
|
289
|
+
buffer.set(data.subarray(at + 4, at + 8), 0);
|
|
290
|
+
buffer.set(data.subarray(at, at + 4), 4);
|
|
291
|
+
stack.push(literal(new Float64Array(buffer.buffer)[0]));
|
|
292
|
+
at += 8;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
case 7: stack.push(literal(new DataView(data.buffer, data.byteOffset + at, 4).getInt32(0, true))); at += 4; break;
|
|
296
|
+
case 8: case 9: {
|
|
297
|
+
const index = type === 8 ? data[at++] : ui16(data, at);
|
|
298
|
+
if (type === 9) at += 2;
|
|
299
|
+
if (index >= state.pool.length) fail('Constant pool index out of range', action);
|
|
300
|
+
stack.push(literal(state.pool[index]));
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
default: fail(`Unknown push type ${type}`, action);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
case OP.Pop: {
|
|
309
|
+
const value = pop();
|
|
310
|
+
if (value.assignment) statement(`${value.text};`);
|
|
311
|
+
else if (value.register !== undefined && value.text === registerName(value.register)) { /* reading a register for nothing */ }
|
|
312
|
+
else statement(`${value.text};`);
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case OP.PushDuplicate: {
|
|
316
|
+
const top = pop();
|
|
317
|
+
// `a && b` / `a || b`: Dup [Not] If join; Pop; b; join:
|
|
318
|
+
const folded = foldLogical(i, top, stack);
|
|
319
|
+
if (folded) { i = folded; continue; }
|
|
320
|
+
// Otherwise only a value without side effects can be written twice.
|
|
321
|
+
if (!top.literal && top.register === undefined && !top.variable) fail('A value is used twice (stack duplicate)', action);
|
|
322
|
+
stack.push(top, top);
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
case OP.Swap: { const b = pop(), a = pop(); stack.push(b, a); break; }
|
|
326
|
+
case OP.Not: {
|
|
327
|
+
const value = pop();
|
|
328
|
+
if (value.binary && NEGATED[value.binary.op]) {
|
|
329
|
+
const { left, op: o, right } = value.binary;
|
|
330
|
+
stack.push({ ...binary(left, NEGATED[o], right, o === '<' || o === '>' ? PREC.relational : PREC.equality), fromNot: value });
|
|
331
|
+
} else {
|
|
332
|
+
stack.push(expr(`!${wrap(value, PREC.unary)}`, PREC.unary, { not: value }));
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case OP.Increment: case OP.Decrement: {
|
|
337
|
+
const value = pop();
|
|
338
|
+
stack.push(expr(`Number(${value.text}) ${op === OP.Increment ? '+' : '-'} 1`, PREC.additive, { step: op === OP.Increment ? '++' : '--', of: value }));
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
case OP.ToInteger: stack.push(expr(`int(${pop().text})`, PREC.call)); break;
|
|
342
|
+
case OP.ToNumber: stack.push(expr(`+${wrap(pop(), PREC.unary)}`, PREC.unary)); break;
|
|
343
|
+
case OP.ToString: stack.push(expr(`String(${pop().text})`, PREC.call)); break;
|
|
344
|
+
case OP.TypeOf: stack.push(expr(`typeof ${wrap(pop(), PREC.unary)}`, PREC.unary)); break;
|
|
345
|
+
case OP.GetTime: stack.push(expr('getTimer()', PREC.call)); break;
|
|
346
|
+
case OP.RandomNumber: stack.push(expr(`random(${pop().text})`, PREC.call)); break;
|
|
347
|
+
case OP.Chr: stack.push(expr(`chr(${pop().text})`, PREC.call)); break;
|
|
348
|
+
case OP.Ord: stack.push(expr(`ord(${pop().text})`, PREC.call)); break;
|
|
349
|
+
case OP.MBAsciiToChar: stack.push(expr(`mbchr(${pop().text})`, PREC.call)); break;
|
|
350
|
+
case OP.MBCharToAscii: stack.push(expr(`mbord(${pop().text})`, PREC.call)); break;
|
|
351
|
+
case OP.StringLength: stack.push(expr(`length(${pop().text})`, PREC.call)); break;
|
|
352
|
+
case OP.MBStringLength: stack.push(expr(`mblength(${pop().text})`, PREC.call)); break;
|
|
353
|
+
case OP.StringExtract: case OP.MBStringExtract: {
|
|
354
|
+
const count = pop(), index = pop(), string = pop();
|
|
355
|
+
stack.push(expr(`${op === OP.StringExtract ? 'substring' : 'mbsubstring'}(${callArgs([string, index, count])})`, PREC.call));
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case OP.TargetPath: stack.push(expr(`targetPath(${pop().text})`, PREC.call)); break;
|
|
359
|
+
case OP.Add: case OP.Equals: case OP.LessThan: case OP.StringAdd: case OP.StringEquals: case OP.StringLessThan: case OP.StringGreater: {
|
|
360
|
+
// Flash 4 operators: written with explicit conversions.
|
|
361
|
+
const b = pop(), a = pop();
|
|
362
|
+
const numeric = op === OP.Add || op === OP.Equals || op === OP.LessThan;
|
|
363
|
+
const conv = e => expr(`${numeric ? 'Number' : 'String'}(${e.text})`, PREC.call);
|
|
364
|
+
const symbol = { [OP.Add]: '+', [OP.Equals]: '==', [OP.LessThan]: '<', [OP.StringAdd]: '+', [OP.StringEquals]: '==', [OP.StringLessThan]: '<', [OP.StringGreater]: '>' }[op];
|
|
365
|
+
const prec = symbol === '+' ? PREC.additive : symbol === '==' ? PREC.equality : PREC.relational;
|
|
366
|
+
stack.push(binary(conv(a), symbol, conv(b), prec));
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
case OP.And: case OP.Or: {
|
|
370
|
+
const b = pop(), a = pop();
|
|
371
|
+
stack.push(binary(expr(`Boolean(${a.text})`, PREC.call), op === OP.And ? '&&' : '||', expr(`Boolean(${b.text})`, PREC.call), op === OP.And ? PREC.and : PREC.or));
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
case OP.GetVariable: {
|
|
375
|
+
const name = pop();
|
|
376
|
+
const text = nameOf(name);
|
|
377
|
+
if (text !== null) variableNames.add(text);
|
|
378
|
+
if (text !== null && isIdentifier(text) && !CLIP_PROPERTIES.includes(text)) stack.push(expr(text, PREC.primary, { variable: text }));
|
|
379
|
+
else if (text === 'this') stack.push(expr('this', PREC.primary, { variable: 'this' }));
|
|
380
|
+
else if (text === '/:$version') stack.push(expr('getVersion()', PREC.call));
|
|
381
|
+
else stack.push(expr(`eval(${name.text})`, PREC.call));
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
case OP.SetVariable: {
|
|
385
|
+
const value = pop(), name = pop();
|
|
386
|
+
const text = nameOf(name);
|
|
387
|
+
if (text !== null) variableNames.add(text);
|
|
388
|
+
const top = stack.at(-1);
|
|
389
|
+
if (value.step && value.of.variable === text && value.of.stored === undefined && top?.variable === text && !top.stale
|
|
390
|
+
&& actions[i - 4]?.op === OP.GetVariable && actions[i - 2]?.op === OP.GetVariable) {
|
|
391
|
+
// `x++` as a value, as Flash MX writes it: read x, then x = x + 1 as its own step.
|
|
392
|
+
stack[stack.length - 1] = expr(`${text}${value.step}`, PREC.postfix, { assignment: true });
|
|
393
|
+
} else if (text !== null && isIdentifier(text) && !CLIP_PROPERTIES.includes(text)) {
|
|
394
|
+
assignTo(text, value);
|
|
395
|
+
} else {
|
|
396
|
+
statement(`set(${callArgs([name, value])});`);
|
|
397
|
+
}
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case OP.DefineLocal: { const value = pop(), name = pop(); const text = nameOf(name); variableNames.add(text); if (!isIdentifier(text)) fail('Local variable with an unusual name', action); statement(`var ${text} = ${wrap(value, PREC.assign)};`); break; }
|
|
401
|
+
case OP.DefineLocal2: { const name = pop(); const text = nameOf(name); variableNames.add(text); if (!isIdentifier(text)) fail('Local variable with an unusual name', action); statement(`var ${text};`); break; }
|
|
402
|
+
case OP.Delete: { const name = pop(), object = pop(); stack.push(expr(`delete ${memberText(object, name)}`, PREC.unary)); break; }
|
|
403
|
+
case OP.Delete2: { const name = pop(); const text = nameOf(name); if (!isIdentifier(text)) fail('delete of an unusual name', action); stack.push(expr(`delete ${text}`, PREC.unary)); break; }
|
|
404
|
+
case OP.GetMember: { const name = pop(), object = pop(); stack.push(expr(memberText(object, name), PREC.member, { member: { object, name } })); break; }
|
|
405
|
+
case OP.SetMember: {
|
|
406
|
+
const value = pop(), name = pop(), object = pop();
|
|
407
|
+
assignTo(memberText(object, name), value);
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case OP.GetProperty: {
|
|
411
|
+
const index = pop(), clip = pop();
|
|
412
|
+
if (!index.literal || typeof index.value !== 'number' || !CLIP_PROPERTIES[index.value]) fail('Clip property index is not a constant', action);
|
|
413
|
+
const property = CLIP_PROPERTIES[index.value];
|
|
414
|
+
stack.push(clip.literal && clip.value === '' ? expr(property) : expr(`getProperty(${clip.text}, ${property})`, PREC.call));
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
case OP.SetProperty: {
|
|
418
|
+
const value = pop(), index = pop(), clip = pop();
|
|
419
|
+
if (!index.literal || typeof index.value !== 'number' || !CLIP_PROPERTIES[index.value]) fail('Clip property index is not a constant', action);
|
|
420
|
+
const property = CLIP_PROPERTIES[index.value];
|
|
421
|
+
if (clip.literal && clip.value === '') {
|
|
422
|
+
assignTo(property, value);
|
|
423
|
+
} else {
|
|
424
|
+
statement(`setProperty(${clip.text}, ${property}, ${wrap(value, PREC.assign)});`);
|
|
425
|
+
}
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case OP.StoreRegister: {
|
|
429
|
+
const register = data[0];
|
|
430
|
+
usedRegisters.add(register);
|
|
431
|
+
const value = pop();
|
|
432
|
+
const name = registerName(register);
|
|
433
|
+
// `StoreRegister; Pop` is a plain assignment; otherwise the value stays on the stack.
|
|
434
|
+
if (actions[i + 1]?.op === OP.Pop) {
|
|
435
|
+
statement(`${name} = ${wrap(value, PREC.assign)};`);
|
|
436
|
+
i += 2;
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
stack.push(expr(`${name} = ${wrap(value, PREC.assign)}`, PREC.assign, { stored: register, value, assignment: true }));
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
case OP.InitArray: {
|
|
443
|
+
const count = popCount();
|
|
444
|
+
const items = [];
|
|
445
|
+
for (let k = 0; k < count; k++) items.push(pop());
|
|
446
|
+
stack.push(expr(`[${callArgs(items)}]`));
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case OP.InitObject: {
|
|
450
|
+
const count = popCount();
|
|
451
|
+
const pairs = [];
|
|
452
|
+
for (let k = 0; k < count; k++) {
|
|
453
|
+
const value = pop(), name = pop();
|
|
454
|
+
if (!name.literal) fail('Object literal with a computed name', action);
|
|
455
|
+
const key = String(name.value);
|
|
456
|
+
pairs.unshift(`${isIdentifier(key) || /^(0|[1-9]\d*)$/.test(key) ? key : quote(key)}: ${wrap(value, PREC.assign)}`);
|
|
457
|
+
}
|
|
458
|
+
stack.push(expr(`{${pairs.join(', ')}}`));
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
case OP.CallFunction: {
|
|
462
|
+
const name = pop();
|
|
463
|
+
const count = popCount();
|
|
464
|
+
const args = [];
|
|
465
|
+
for (let k = 0; k < count; k++) args.push(pop());
|
|
466
|
+
const text = nameOf(name);
|
|
467
|
+
// `play()` written in ActionScript is the Play action, so a real call to a function named
|
|
468
|
+
// like a global action has no source form.
|
|
469
|
+
if (text !== null && (ACTION_NAMES.has(text) || CLIP_PROPERTIES.includes(text))) fail(`Calls a function named ${text}, which ActionScript reads as the ${text} action`, action);
|
|
470
|
+
const callee = text !== null && isIdentifier(text) ? text : `eval(${name.text})`;
|
|
471
|
+
stack.push(expr(`${callee}(${callArgs(args)})`, PREC.call, { call: true }));
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
case OP.CallMethod: {
|
|
475
|
+
const name = pop(), object = pop();
|
|
476
|
+
const count = popCount();
|
|
477
|
+
const args = [];
|
|
478
|
+
for (let k = 0; k < count; k++) args.push(pop());
|
|
479
|
+
const blank = name.literal && (name.value === undefined || name.value === '');
|
|
480
|
+
// Calling a value: `f()` would be read as CallFunction, so the callee is parenthesized.
|
|
481
|
+
const callee = blank ? (object.variable ? `(${object.text})` : wrap(object, PREC.call)) : memberText(object, name);
|
|
482
|
+
stack.push(expr(`${callee}(${callArgs(args)})`, PREC.call, { call: true }));
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
case OP.NewObject: {
|
|
486
|
+
const name = pop();
|
|
487
|
+
const count = popCount();
|
|
488
|
+
const args = [];
|
|
489
|
+
for (let k = 0; k < count; k++) args.push(pop());
|
|
490
|
+
const text = nameOf(name);
|
|
491
|
+
if (!isIdentifier(text)) fail('new with a computed class name', action);
|
|
492
|
+
stack.push(expr(`new ${text}(${callArgs(args)})`, PREC.call));
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
case OP.NewMethod: {
|
|
496
|
+
const name = pop(), object = pop();
|
|
497
|
+
const count = popCount();
|
|
498
|
+
const args = [];
|
|
499
|
+
for (let k = 0; k < count; k++) args.push(pop());
|
|
500
|
+
const callee = name.literal && (name.value === undefined || name.value === '') ? object.text : memberText(object, name);
|
|
501
|
+
// `new a.b.C(…)` reads its callee without calls; anything else goes in parentheses.
|
|
502
|
+
stack.push(expr(`new ${/^[\w$]+(\.[\w$]+)*$/.test(callee) ? callee : `(${callee})`}(${callArgs(args)})`, PREC.call));
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
case OP.Return: statement(`return ${wrap(pop(), PREC.assign)};`); return { stack, stmts, end: i + 1, kind: 'return' };
|
|
506
|
+
case OP.Throw: statement(`throw ${wrap(pop(), PREC.assign)};`); return { stack, stmts, end: i + 1, kind: 'throw' };
|
|
507
|
+
case OP.Trace: statement(`trace(${pop().text});`); break;
|
|
508
|
+
case OP.Stop: statement('stop();'); break;
|
|
509
|
+
case OP.Play: statement('play();'); break;
|
|
510
|
+
case OP.NextFrame: statement('nextFrame();'); break;
|
|
511
|
+
case OP.PrevFrame: statement('prevFrame();'); break;
|
|
512
|
+
case OP.StopSounds: statement('stopAllSounds();'); break;
|
|
513
|
+
case OP.ToggleQuality: statement('toggleHighQuality();'); break;
|
|
514
|
+
case OP.StopDrag: statement('stopDrag();'); break;
|
|
515
|
+
case OP.RemoveSprite: statement(`removeMovieClip(${pop().text});`); break;
|
|
516
|
+
case OP.CallFrame: statement(`call(${pop().text});`); break;
|
|
517
|
+
case OP.GotoFrame: case OP.GotoLabel: {
|
|
518
|
+
const frame = op === OP.GotoFrame ? String(ui16(data, 0) + 1) : quote(readString(data, 0, state.swfVersion).text);
|
|
519
|
+
const play = actions[i + 1]?.op === OP.Play && !(leaders?.has(i + 1)) && !sources.has(actions[i + 1].offset);
|
|
520
|
+
statement(`${play ? 'gotoAndPlay' : 'gotoAndStop'}(${frame});`);
|
|
521
|
+
if (play) i++;
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
case OP.GotoFrame2: {
|
|
525
|
+
const frame = pop();
|
|
526
|
+
const play = data[0] & 1;
|
|
527
|
+
let frameText = frame.text;
|
|
528
|
+
if (data[0] & 2) {
|
|
529
|
+
const bias = ui16(data, 1);
|
|
530
|
+
if (!frame.literal || typeof frame.value !== 'number') fail('Scene frame offset on a computed frame', action);
|
|
531
|
+
frameText = String(frame.value + bias);
|
|
532
|
+
}
|
|
533
|
+
statement(`${play ? 'gotoAndPlay' : 'gotoAndStop'}(${frameText});`);
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
case OP.GetURL: {
|
|
537
|
+
const url = readString(data, 0, state.swfVersion);
|
|
538
|
+
const window = readString(data, url.next, state.swfVersion).text;
|
|
539
|
+
if (url.text.startsWith('FSCommand:')) statement(`fscommand(${quote(url.text.slice(10))}, ${quote(window)});`);
|
|
540
|
+
else statement(`getURL(${quote(url.text)}, ${quote(window)});`);
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
case OP.GetURL2: {
|
|
544
|
+
const windowTarget = pop(), url = pop();
|
|
545
|
+
const flags = data[0];
|
|
546
|
+
const methodText = (flags & 3) === 1 ? ', "GET"' : (flags & 3) === 2 ? ', "POST"' : '';
|
|
547
|
+
if ((flags & 0xc0) === 0x40) {
|
|
548
|
+
if (url.literal && url.value === '' && !methodText) statement(`unloadMovie(${windowTarget.text});`);
|
|
549
|
+
else statement(`loadMovie(${callArgs([url, windowTarget])}${methodText});`);
|
|
550
|
+
} else if ((flags & 0xc0) === 0xc0) {
|
|
551
|
+
statement(`loadVariables(${callArgs([url, windowTarget])}${methodText});`);
|
|
552
|
+
} else if ((flags & 0xc0) === 0x80) {
|
|
553
|
+
const level = windowTarget.literal && /^_level(\d+)$/.exec(String(windowTarget.value));
|
|
554
|
+
if (!level) fail('loadVariablesNum with a computed level', action);
|
|
555
|
+
statement(`loadVariablesNum(${url.text}, ${level[1]}${methodText});`);
|
|
556
|
+
} else if (url.binary?.op === '+' && url.binary.left.literal && url.binary.left.value === 'FSCommand:' && !methodText) {
|
|
557
|
+
statement(`fscommand(${callArgs([url.binary.right, windowTarget])});`);
|
|
558
|
+
} else {
|
|
559
|
+
statement(`getURL(${callArgs([url, windowTarget])}${methodText});`);
|
|
560
|
+
}
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case OP.CloneSprite: {
|
|
564
|
+
const depth = pop(), name = pop(), source = pop();
|
|
565
|
+
const plain = depth.binary?.op === '+' && depth.binary.right.literal && depth.binary.right.value === 16384
|
|
566
|
+
? depth.binary.left : binary(depth, '-', literal(16384), PREC.additive);
|
|
567
|
+
statement(`duplicateMovieClip(${callArgs([source, name, plain])});`);
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
case OP.StartDrag: {
|
|
571
|
+
const clip = pop(), lock = pop(), constrain = pop();
|
|
572
|
+
if (!constrain.literal) fail('startDrag with a computed constraint flag', action);
|
|
573
|
+
if (constrain.value && constrain.value !== '0') {
|
|
574
|
+
const y2 = pop(), x2 = pop(), y1 = pop(), x1 = pop();
|
|
575
|
+
statement(`startDrag(${callArgs([clip, lock, x1, y1, x2, y2])});`);
|
|
576
|
+
} else {
|
|
577
|
+
statement(`startDrag(${callArgs([clip, lock])});`);
|
|
578
|
+
}
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
case OP.SetTarget: case OP.SetTarget2: {
|
|
582
|
+
// tellTarget(x) { … }: SetTarget x … SetTarget "" in straight-line code.
|
|
583
|
+
const clip = op === OP.SetTarget ? literal(readString(data, 0, state.swfVersion).text) : pop();
|
|
584
|
+
if (op === OP.SetTarget && clip.value === '') fail('Unmatched end of tellTarget', action);
|
|
585
|
+
let close = i + 1;
|
|
586
|
+
while (close < actions.length && !(actions[close].op === OP.SetTarget && actions[close].data[0] === 0)) {
|
|
587
|
+
if (actions[close].op === OP.SetTarget || actions[close].op === OP.SetTarget2) fail('Nested tellTarget', action);
|
|
588
|
+
close++;
|
|
589
|
+
}
|
|
590
|
+
if (close >= actions.length || close > to) fail('tellTarget without its end', action);
|
|
591
|
+
// The body is decompiled on its own: no jump may enter or leave it.
|
|
592
|
+
for (let k = i + 1; k < close; k++) {
|
|
593
|
+
const inner = actions[k];
|
|
594
|
+
if ((inner.op === OP.If || inner.op === OP.Jump) && !inNested(inner.offset)) {
|
|
595
|
+
const t = indexOfOffset(target(inner));
|
|
596
|
+
if (t <= i || t > close) fail('tellTarget body jumps out of it', action);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (!sealed(i, close)) fail('A jump enters a tellTarget body', action);
|
|
600
|
+
const body = unit(bytes, actions[i + 1].offset, actions[close].offset, state, { ...scope, exits: new Map() });
|
|
601
|
+
statement(`tellTarget(${clip.text}) {\n${body.join('\n')}\n}`);
|
|
602
|
+
i = close + 1;
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
case OP.DefineFunction: case OP.DefineFunction2: {
|
|
606
|
+
const fn = functionDefinition(action);
|
|
607
|
+
if (fn.name) statement(fn.text);
|
|
608
|
+
else stack.push(expr(fn.text, PREC.primary, { fn: true }));
|
|
609
|
+
i = fn.after;
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
case OP.With: {
|
|
613
|
+
const object = pop();
|
|
614
|
+
const size = ui16(data, 0);
|
|
615
|
+
const bodyStart = action.offset + action.size;
|
|
616
|
+
const inner = unit(bytes, bodyStart, bodyStart + size, state, { ...scope, exits: new Map() });
|
|
617
|
+
statement(`with (${object.text}) {\n${inner.join('\n')}\n}`);
|
|
618
|
+
i = indexOfOffset(bodyStart + size);
|
|
619
|
+
if (i < 0) fail('with block ends inside an action', action);
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
case OP.Try: {
|
|
623
|
+
const flags = data[0];
|
|
624
|
+
const sizes = [ui16(data, 1), ui16(data, 3), ui16(data, 5)];
|
|
625
|
+
let catchName;
|
|
626
|
+
if (flags & 4) {
|
|
627
|
+
catchName = registerName(data[7]);
|
|
628
|
+
usedRegisters.add(data[7]);
|
|
629
|
+
} else {
|
|
630
|
+
catchName = readString(data, 7, state.swfVersion).text;
|
|
631
|
+
}
|
|
632
|
+
let at = action.offset + action.size;
|
|
633
|
+
const part = size => { const lines = unit(bytes, at, at + size, state, { ...scope, exits: new Map() }); at += size; return lines; };
|
|
634
|
+
const tryLines = part(sizes[0]), catchLines = part(sizes[1]), finallyLines = part(sizes[2]);
|
|
635
|
+
let text = `try {\n${tryLines.join('\n')}\n}`;
|
|
636
|
+
if (flags & 1) {
|
|
637
|
+
if (!isIdentifier(catchName)) fail('catch variable with an unusual name', action);
|
|
638
|
+
text += ` catch (${catchName}) {\n${catchLines.join('\n')}\n}`;
|
|
639
|
+
}
|
|
640
|
+
if (flags & 2) text += ` finally {\n${finallyLines.join('\n')}\n}`;
|
|
641
|
+
if (!(flags & 3)) fail('try without catch or finally', action);
|
|
642
|
+
statement(text);
|
|
643
|
+
i = indexOfOffset(at);
|
|
644
|
+
if (i < 0) fail('try block ends inside an action', action);
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
case OP.Enumerate: case OP.Enumerate2: {
|
|
648
|
+
const folded = forIn(i, pop(), stmts);
|
|
649
|
+
if (!folded) fail('for…in in a form not recognized', action);
|
|
650
|
+
i = folded;
|
|
651
|
+
continue;
|
|
652
|
+
}
|
|
653
|
+
case OP.If: {
|
|
654
|
+
const condition = pop();
|
|
655
|
+
const folded = foldConditional(i, condition, stack);
|
|
656
|
+
if (folded) { i = folded; continue; }
|
|
657
|
+
// Not an expression: the branch ends the block.
|
|
658
|
+
return { stack, stmts, end: i + 1, kind: 'branch', action, condition };
|
|
659
|
+
}
|
|
660
|
+
case OP.Jump: return { stack, stmts, end: i + 1, kind: 'jump', action };
|
|
661
|
+
case 0x89: break; // ActionStrictMode
|
|
662
|
+
default: {
|
|
663
|
+
const binaryOp = BINARY[op];
|
|
664
|
+
if (binaryOp) {
|
|
665
|
+
const b = pop(), a = pop();
|
|
666
|
+
stack.push(binary(a, binaryOp[0], b, binaryOp[1]));
|
|
667
|
+
break;
|
|
668
|
+
}
|
|
669
|
+
fail(`Action 0x${op.toString(16)} has no ActionScript form`, action);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
i++;
|
|
673
|
+
}
|
|
674
|
+
return { stack, stmts, end: i, kind: 'end' };
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/** `a && b` / `a || b` at a PushDuplicate (index `dup`). Returns the index after the join, or 0. */
|
|
678
|
+
function foldLogical(dup, left, stack) {
|
|
679
|
+
let k = dup + 1;
|
|
680
|
+
let isAnd = false;
|
|
681
|
+
if (actions[k]?.op === OP.Not) { isAnd = true; k++; }
|
|
682
|
+
const branch = actions[k];
|
|
683
|
+
if (branch?.op !== OP.If || actions[k + 1]?.op !== OP.Pop) return 0;
|
|
684
|
+
const join = indexOfOffset(target(branch));
|
|
685
|
+
if (join <= k + 1 || !sealed(dup, join)) return 0;
|
|
686
|
+
const right = runFoldPart(k + 2, join, join, [k]);
|
|
687
|
+
if (!right || right.kind !== 'end' || right.stmts.length || right.stack.length !== 1) return 0;
|
|
688
|
+
stack.push(binary(left, isAnd ? '&&' : '||', right.stack[0], isAnd ? PREC.and : PREC.or));
|
|
689
|
+
consumedJoins.add(join);
|
|
690
|
+
return join;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** `c ? a : b` at an If (index `at`) whose condition was popped. Returns the index after the join, or 0. */
|
|
694
|
+
function foldConditional(at, condition, stack) {
|
|
695
|
+
const branch = actions[at];
|
|
696
|
+
const yes = indexOfOffset(target(branch));
|
|
697
|
+
if (yes <= at + 1) return 0;
|
|
698
|
+
const jump = actions[yes - 1];
|
|
699
|
+
if (jump?.op !== OP.Jump) return 0;
|
|
700
|
+
const join = indexOfOffset(target(jump));
|
|
701
|
+
if (join < yes || !sealed(at, join)) return 0;
|
|
702
|
+
const no = runFoldPart(at + 1, yes - 1, join, [at, yes - 1]);
|
|
703
|
+
const then = no && runFoldPart(yes, join, join, [at, yes - 1]);
|
|
704
|
+
if (!no || !then || no.kind !== 'end' || then.kind !== 'end' || no.stmts.length || then.stmts.length || no.stack.length !== 1 || then.stack.length !== 1) return 0;
|
|
705
|
+
stack.push(expr(`${wrap(condition, PREC.or)} ? ${wrap(then.stack[0], PREC.assign)} : ${wrap(no.stack[0], PREC.assign)}`, PREC.conditional));
|
|
706
|
+
consumedJoins.add(join);
|
|
707
|
+
consumedJoins.add(yes);
|
|
708
|
+
return join;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* for…in: [object] Enumerate2; L: StoreRegister r; Push null; Equals2; If end; <name = r>; body; Jump L;
|
|
713
|
+
* [drain: Push null; Equals2; Not; If drain;] end:
|
|
714
|
+
*/
|
|
715
|
+
function forIn(at, object, stmts) {
|
|
716
|
+
const head = at + 1;
|
|
717
|
+
const store = actions[head], pushNull = actions[head + 1], equals = actions[head + 2], exit = actions[head + 3];
|
|
718
|
+
if (store?.op !== OP.StoreRegister || pushNull?.op !== OP.Push || pushNull.data.length !== 1 || pushNull.data[0] !== 2
|
|
719
|
+
|| equals?.op !== OP.Equals2 || exit?.op !== OP.If) return 0;
|
|
720
|
+
const register = store.data[0];
|
|
721
|
+
const end = indexOfOffset(target(exit));
|
|
722
|
+
if (end <= head + 3) return 0;
|
|
723
|
+
// The loop's back jump, and the drain that `break` uses.
|
|
724
|
+
let back = end - 1, drain = null;
|
|
725
|
+
const d = actions.slice(end - 4, end);
|
|
726
|
+
if (d.length === 4 && d[0].op === OP.Push && d[0].data.length === 1 && d[0].data[0] === 2 && d[1].op === OP.Equals2
|
|
727
|
+
&& d[2].op === OP.Not && d[3].op === OP.If && target(d[3]) === d[0].offset) {
|
|
728
|
+
drain = end - 4;
|
|
729
|
+
back = end - 5;
|
|
730
|
+
}
|
|
731
|
+
if (actions[back]?.op !== OP.Jump || target(actions[back]) !== store.offset) return 0;
|
|
732
|
+
if (!sealed(head, end) && !jumpsOnlyToLoop(head, end, store.offset, drain === null ? actions[end]?.offset ?? endOffset : actions[drain].offset)) return 0;
|
|
733
|
+
// The loop variable: Push name, register r; SetVariable | DefineLocal.
|
|
734
|
+
const assign = actions[head + 4];
|
|
735
|
+
let variable = null, bodyStart = head + 4;
|
|
736
|
+
if (assign?.op === OP.Push) {
|
|
737
|
+
// Names may come from the constant pool.
|
|
738
|
+
const values = pushValues(assign).map(value => value.type === 'constant' && typeof state.pool?.[value.value] === 'string'
|
|
739
|
+
? { type: 'string', value: state.pool[value.value] } : value);
|
|
740
|
+
if (values.length === 2 && values[0].type === 'string' && values[1].type === 'register' && values[1].value === register && isIdentifier(values[0].value)) {
|
|
741
|
+
const setter = actions[head + 5]?.op;
|
|
742
|
+
if (setter === OP.SetVariable || setter === OP.DefineLocal) {
|
|
743
|
+
variable = `${setter === OP.DefineLocal ? 'var ' : ''}${values[0].value}`;
|
|
744
|
+
bodyStart = head + 6;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (!variable) {
|
|
749
|
+
usedRegisters.add(register);
|
|
750
|
+
variable = registerName(register);
|
|
751
|
+
}
|
|
752
|
+
const exits = new Map([[store.offset, 'continue;'], [drain === null ? (actions[end]?.offset ?? endOffset) : actions[drain].offset, 'break;']]);
|
|
753
|
+
const bodyEnd = actions[back].offset;
|
|
754
|
+
const body = unit(bytes, actions[bodyStart].offset, bodyEnd, state, { ...scope, exits, usedRegisters });
|
|
755
|
+
stmts.push(`for (${variable} in ${object.text}) {\n${body.join('\n')}\n}`);
|
|
756
|
+
return end;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function jumpsOnlyToLoop(from, to, continueOffset, breakOffset) {
|
|
760
|
+
for (let k = from; k < to; k++) {
|
|
761
|
+
const action = actions[k];
|
|
762
|
+
if (action.op !== OP.Jump && action.op !== OP.If) continue;
|
|
763
|
+
const t = target(action);
|
|
764
|
+
const index = indexOfOffset(t);
|
|
765
|
+
if (index > from && index <= to) continue;
|
|
766
|
+
if (t === continueOffset || t === breakOffset) continue;
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function functionDefinition(action) {
|
|
773
|
+
const data = action.data;
|
|
774
|
+
let at = 0;
|
|
775
|
+
const name = readString(data, at, state.swfVersion);
|
|
776
|
+
at = name.next;
|
|
777
|
+
const count = ui16(data, at);
|
|
778
|
+
at += 2;
|
|
779
|
+
const registers = new Map();
|
|
780
|
+
const params = [];
|
|
781
|
+
if (action.op === OP.DefineFunction2) {
|
|
782
|
+
at += 1; // register count
|
|
783
|
+
const flags = ui16(data, at);
|
|
784
|
+
at += 2;
|
|
785
|
+
let next = 1;
|
|
786
|
+
const preload = (flag, text) => { if (flags & flag) registers.set(next++, text); };
|
|
787
|
+
preload(DF2.PRELOAD_THIS, 'this');
|
|
788
|
+
preload(DF2.PRELOAD_ARGUMENTS, 'arguments');
|
|
789
|
+
preload(DF2.PRELOAD_SUPER, 'super');
|
|
790
|
+
preload(DF2.PRELOAD_ROOT, '_root');
|
|
791
|
+
preload(DF2.PRELOAD_PARENT, '_parent');
|
|
792
|
+
preload(DF2.PRELOAD_GLOBAL, '_global');
|
|
793
|
+
for (let k = 0; k < count; k++) {
|
|
794
|
+
const register = data[at++];
|
|
795
|
+
const param = readString(data, at, state.swfVersion);
|
|
796
|
+
at = param.next;
|
|
797
|
+
params.push(param.text);
|
|
798
|
+
if (register) registers.set(register, param.text);
|
|
799
|
+
}
|
|
800
|
+
} else {
|
|
801
|
+
for (let k = 0; k < count; k++) {
|
|
802
|
+
const param = readString(data, at, state.swfVersion);
|
|
803
|
+
at = param.next;
|
|
804
|
+
params.push(param.text);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
const size = ui16(data, at);
|
|
808
|
+
if (params.some(param => !isIdentifier(param))) throw new DecompileError('Function parameter with an unusual name', action.offset);
|
|
809
|
+
const unusualName = name.text && !isIdentifier(name.text);
|
|
810
|
+
if (unusualName && scope.inFunction) throw new DecompileError('Function with an unusual name inside a function', action.offset);
|
|
811
|
+
const bodyStart = action.offset + action.size;
|
|
812
|
+
const savedPool = state.pool;
|
|
813
|
+
const inner = new Set();
|
|
814
|
+
const variables = new Set();
|
|
815
|
+
const lines = unit(bytes, bodyStart, bodyStart + size, state, { registers, inFunction: true, usedRegisters: inner, variableNames: variables, exits: new Map() });
|
|
816
|
+
state.pool = savedPool;
|
|
817
|
+
// Registers that are not parameters or preloads are the function's locals, when their name
|
|
818
|
+
// appears in the result (a register read back into an assignment leaves no name).
|
|
819
|
+
const bodyText = lines.join('\n');
|
|
820
|
+
const locals = [...inner].filter(register => !registers.has(register)).sort((a, b) => a - b)
|
|
821
|
+
.map(register => `_r${register}`).filter(local => !variables.has(local) && new RegExp(`\\b${local}\\b`).test(bodyText));
|
|
822
|
+
// One declaration per line, as separate DefineLocal2 actions read back.
|
|
823
|
+
const declare = action.op === OP.DefineFunction2 ? locals.map(local => `var ${local};`) : [];
|
|
824
|
+
const shownName = name.text && !unusualName ? `${name.text} ` : '';
|
|
825
|
+
let text = `function ${shownName}(${params.join(', ')}) {\n${[...declare, ...lines].join('\n')}\n}`;
|
|
826
|
+
// A named function on a timeline is a timeline variable; an unusual name is set by string.
|
|
827
|
+
if (unusualName) text = `set(${quote(name.text)}, ${text});`;
|
|
828
|
+
// Skip past the body in the enclosing walk.
|
|
829
|
+
const after = indexOfOffset(bodyStart + size);
|
|
830
|
+
if (after < 0) throw new DecompileError('Function ends inside an action', action.offset);
|
|
831
|
+
return { name: name.text, after, text: text.replace(/^function ([\w$]+) \(/, 'function $1(') };
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// ---- Blocks ----
|
|
835
|
+
// Walk in address order. Folded expressions consume their branches; the remaining branches and
|
|
836
|
+
// jumps end blocks, and every jump target starts one.
|
|
837
|
+
const leaders = new Set([...sources.keys()].map(indexOfOffset).filter(value => value >= 0));
|
|
838
|
+
const blocks = [];
|
|
839
|
+
for (let index = 0; index < actions.length;) {
|
|
840
|
+
const result = run(index, actions.length, { stack: [], leaders });
|
|
841
|
+
blocks.push({ ...result, start: index });
|
|
842
|
+
index = result.end;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// Basic blocks for the structurer.
|
|
846
|
+
const ids = blocks.map(block => actions[block.start]?.offset ?? endOffset);
|
|
847
|
+
const exitId = endOffset;
|
|
848
|
+
const cfgBlocks = [];
|
|
849
|
+
let synthetic = endOffset + 1;
|
|
850
|
+
const exits = scope.exits ?? new Map();
|
|
851
|
+
const extra = [];
|
|
852
|
+
const resolve = offset => {
|
|
853
|
+
if (offset === endOffset) return exitId;
|
|
854
|
+
if (exits.has(offset)) {
|
|
855
|
+
const id = synthetic++;
|
|
856
|
+
extra.push({ id, stmts: [exits.get(offset)], term: 'return' });
|
|
857
|
+
return id;
|
|
858
|
+
}
|
|
859
|
+
if (indexOfOffset(offset) < 0) throw new DecompileError('Jump into the middle of an action or out of its block', offset);
|
|
860
|
+
return offset;
|
|
861
|
+
};
|
|
862
|
+
blocks.forEach((block, n) => {
|
|
863
|
+
if (block.stack.length) {
|
|
864
|
+
throw new DecompileError('Values left on the stack between statements', actions[block.start]?.offset);
|
|
865
|
+
}
|
|
866
|
+
const id = ids[n];
|
|
867
|
+
const nextOffset = block.end < actions.length ? actions[block.end].offset : endOffset;
|
|
868
|
+
if (block.kind === 'branch') {
|
|
869
|
+
// `if (c) A else B` compiles to: c; Not; If → B. Hand the structurer the positive test
|
|
870
|
+
// with the targets swapped, so that the output keeps the source's order.
|
|
871
|
+
let condition = lastConditionFor(block) && block.condition;
|
|
872
|
+
let yes = resolve(target(block.action)), no = resolve(nextOffset);
|
|
873
|
+
const positive = condition.not ?? condition.fromNot;
|
|
874
|
+
if (positive) {
|
|
875
|
+
condition = positive;
|
|
876
|
+
[yes, no] = [no, yes];
|
|
877
|
+
}
|
|
878
|
+
if (yes === no) {
|
|
879
|
+
// `if (c) {}`: the test runs for its side effects and goes on either way.
|
|
880
|
+
cfgBlocks.push({ id, stmts: [...block.stmts, `if (${condition.text}) {\n}`], term: 'fallthrough', next: yes });
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
cfgBlocks.push({ id, stmts: block.stmts, term: 'branch', cond: condition.text, condExpr: condition, trueTarget: yes, falseTarget: no });
|
|
884
|
+
} else if (block.kind === 'jump') {
|
|
885
|
+
cfgBlocks.push({ id, stmts: block.stmts, term: 'jump', next: resolve(target(block.action)) });
|
|
886
|
+
} else if (block.kind === 'return' || block.kind === 'throw') {
|
|
887
|
+
cfgBlocks.push({ id, stmts: block.stmts, term: block.kind });
|
|
888
|
+
} else {
|
|
889
|
+
cfgBlocks.push({ id, stmts: block.stmts, term: 'fallthrough', next: resolve(nextOffset) });
|
|
890
|
+
}
|
|
891
|
+
});
|
|
892
|
+
cfgBlocks.push({ id: exitId, stmts: [], term: 'return' }, ...extra);
|
|
893
|
+
cfgBlocks.sort((a, b) => a.id - b.id);
|
|
894
|
+
|
|
895
|
+
function lastConditionFor(block) {
|
|
896
|
+
if (!block.condition) throw new DecompileError('Branch without a condition', block.action.offset);
|
|
897
|
+
return block.condition.text;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
const implicitExit = 'break _exit;';
|
|
901
|
+
foldSwitches(cfgBlocks, state.swfVersion);
|
|
902
|
+
const dispatcher = () => dispatch(cfgBlocks, cfgBlocks[0].id);
|
|
903
|
+
let lines = cfgBlocks.length === 2 && cfgBlocks[0].term !== 'branch'
|
|
904
|
+
? cfgBlocks[0].stmts.slice()
|
|
905
|
+
: structureCfg(cfgBlocks, cfgBlocks[0].id, implicitExit, dispatcher);
|
|
906
|
+
lines = tidy(lines, new Map(cfgBlocks.filter(block => block.term === 'branch').map(block => [block.cond, block.condExpr])));
|
|
907
|
+
if (lines.at(-1)?.trim() === implicitExit) lines.pop();
|
|
908
|
+
if (lines.some(line => line.trim() === implicitExit)) lines = ['_exit: {', ...lines, '}'];
|
|
909
|
+
if (scope.usedRegisters) for (const register of usedRegisters) scope.usedRegisters.add(register);
|
|
910
|
+
return lines.flatMap(line => line.split('\n'));
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Negate a condition. In a condition only truthiness matters (If converts with ToBoolean), so
|
|
915
|
+
* `!(a < b)` and `a >= b` are the same test; both compile to LessThan2 with or without Not.
|
|
916
|
+
*/
|
|
917
|
+
function negate(text, conditions) {
|
|
918
|
+
const condition = conditions.get(text);
|
|
919
|
+
if (condition?.binary && NEGATED[condition.binary.op]) {
|
|
920
|
+
const { left, op, right } = condition.binary;
|
|
921
|
+
return binary(left, NEGATED[op], right, op === '<' || op === '>' ? PREC.relational : PREC.equality).text;
|
|
922
|
+
}
|
|
923
|
+
if (condition?.binary && Object.values(NEGATED).includes(condition.binary.op)) {
|
|
924
|
+
const back = Object.keys(NEGATED).find(key => NEGATED[key] === condition.binary.op);
|
|
925
|
+
const { left, right } = condition.binary;
|
|
926
|
+
return binary(left, back, right, back === '<' || back === '>' ? PREC.relational : PREC.equality).text;
|
|
927
|
+
}
|
|
928
|
+
if (condition?.not) return condition.not.text;
|
|
929
|
+
return condition && condition.prec >= PREC.unary ? `!${condition.text}` : `!(${text})`;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/** Canonical structured output: no empty branches, no `!(…)` around a comparison. */
|
|
933
|
+
function tidy(lines, conditions) {
|
|
934
|
+
const known = [...conditions.keys()].sort((a, b) => b.length - a.length);
|
|
935
|
+
const simplify = line => {
|
|
936
|
+
for (const text of known) {
|
|
937
|
+
const negated = `!(${text})`;
|
|
938
|
+
if (!line.includes(negated)) continue;
|
|
939
|
+
const result = negate(text, conditions);
|
|
940
|
+
// The rewritten comparison can be negated back (`do … while` exits).
|
|
941
|
+
if (!conditions.has(result)) conditions.set(result, { text: result, not: conditions.get(text) ?? { text } });
|
|
942
|
+
line = line.split(negated).join(result);
|
|
943
|
+
}
|
|
944
|
+
return line;
|
|
945
|
+
};
|
|
946
|
+
// `if (c) break;` on one line, as the structurer writes it, is braced like every other branch.
|
|
947
|
+
const out = lines.flatMap(line => line.split('\n')).map(simplify).flatMap(line => {
|
|
948
|
+
const single = /^(\s*)if \((.*)\) (break|continue);$/.exec(line);
|
|
949
|
+
return single ? [`${single[1]}if (${single[2]}) {`, `${single[3]};`, '}'] : [line];
|
|
950
|
+
});
|
|
951
|
+
/** The index of the line that closes the block opened on line k. */
|
|
952
|
+
const closeOf = k => {
|
|
953
|
+
let depth = 0;
|
|
954
|
+
for (let m = k; m < out.length; m++) {
|
|
955
|
+
for (const char of out[m].replace(/"(?:[^"\\]|\\.)*"/g, '')) {
|
|
956
|
+
if (char === '{') depth++;
|
|
957
|
+
else if (char === '}') depth--;
|
|
958
|
+
}
|
|
959
|
+
if (depth === 0) return m;
|
|
960
|
+
}
|
|
961
|
+
return -1;
|
|
962
|
+
};
|
|
963
|
+
// `case x:` arms come wrapped in their own braces; the braces add nothing.
|
|
964
|
+
for (let k = 1; k < out.length; k++) {
|
|
965
|
+
if (out[k].trim() !== '{' || !/^(case .*|default):$/.test(out[k - 1].trim())) continue;
|
|
966
|
+
const close = closeOf(k);
|
|
967
|
+
if (close > k && out[close].trim() === '}') {
|
|
968
|
+
out.splice(close, 1);
|
|
969
|
+
out.splice(k, 1);
|
|
970
|
+
k--;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
for (let k = 0; k < out.length - 1; k++) {
|
|
974
|
+
const head = /^(\s*)(if|while) \((.*)\) \{$/.exec(out[k]);
|
|
975
|
+
const next = out[k + 1].trim();
|
|
976
|
+
if (next === '} else {' && head?.[2] === 'if') {
|
|
977
|
+
// `if (c) {} else { … }` → `if (!c) { … }`
|
|
978
|
+
out[k] = `${head[1]}if (${negate(head[3], conditions)}) {`;
|
|
979
|
+
out.splice(k + 1, 1);
|
|
980
|
+
k--;
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
if (out[k].trim() === '} else {' && next === '}') {
|
|
984
|
+
out.splice(k, 1);
|
|
985
|
+
k--;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
for (let k = 0; k < out.length; k++) {
|
|
989
|
+
if (!/^\s*while \(true\) \{$/.test(out[k])) continue;
|
|
990
|
+
let close = closeOf(k);
|
|
991
|
+
// A `continue;` that ends the loop body does nothing.
|
|
992
|
+
if (out[close - 1]?.trim() === 'continue;') {
|
|
993
|
+
out.splice(close - 1, 1);
|
|
994
|
+
close--;
|
|
995
|
+
}
|
|
996
|
+
// `while (true) { …; if (c) { break; } }` with no `continue` is `do { … } while (!c);`, which
|
|
997
|
+
// is how Flash and this compiler write do…while.
|
|
998
|
+
const exit = /^\s*if \((.*)\) \{$/.exec(out[close - 3] ?? '');
|
|
999
|
+
if (!exit || close - 3 <= k || out[close - 2].trim() !== 'break;' || out[close - 1].trim() !== '}') continue;
|
|
1000
|
+
const body = out.slice(k + 1, close - 3);
|
|
1001
|
+
if (body.some(line => /\bcontinue\b/.test(line))) continue;
|
|
1002
|
+
out.splice(close - 3, 4, `} while (${negate(exit[1], conditions)});`);
|
|
1003
|
+
out[k] = out[k].replace('while (true) {', 'do {');
|
|
1004
|
+
}
|
|
1005
|
+
return out;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* A chain of `register === value` tests on one value, as Flash and this compiler write `switch`,
|
|
1010
|
+
* becomes one switch block for the structurer. Only the comparison the compiler writes back for
|
|
1011
|
+
* `case` is folded (`===` from SWF 6, `==` before), so the result compiles to the same tests.
|
|
1012
|
+
*/
|
|
1013
|
+
function foldSwitches(blocks, swfVersion) {
|
|
1014
|
+
const equals = swfVersion >= 6 ? '===' : '==';
|
|
1015
|
+
const byId = new Map(blocks.map(block => [block.id, block]));
|
|
1016
|
+
const predecessors = new Map();
|
|
1017
|
+
for (const block of blocks) {
|
|
1018
|
+
for (const next of [block.next, block.trueTarget, block.falseTarget]) {
|
|
1019
|
+
if (next != null) predecessors.set(next, (predecessors.get(next) ?? 0) + 1);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
const test = (block, register) => {
|
|
1023
|
+
const condition = block?.condExpr;
|
|
1024
|
+
if (block?.term !== 'branch' || !condition?.binary || condition.binary.op !== equals) return null;
|
|
1025
|
+
const left = condition.binary.left;
|
|
1026
|
+
if (register === undefined) {
|
|
1027
|
+
// First test: on the stored value (`(_r0 = x) === v`), or on a register set just before.
|
|
1028
|
+
if (left.stored !== undefined) return { register: left.stored, discriminant: left.value, value: condition.binary.right };
|
|
1029
|
+
const set = block.stmts.at(-1)?.match(/^(_r\d+) = ([\s\S]*);$/);
|
|
1030
|
+
if (left.register !== undefined && set && set[1] === `_r${left.register}`) {
|
|
1031
|
+
return { register: left.register, discriminant: { text: set[2] }, value: condition.binary.right, dropStatement: true };
|
|
1032
|
+
}
|
|
1033
|
+
return null;
|
|
1034
|
+
}
|
|
1035
|
+
if (left.register !== register || block.stmts.length) return null;
|
|
1036
|
+
return { register, value: condition.binary.right };
|
|
1037
|
+
};
|
|
1038
|
+
for (const start of [...blocks]) {
|
|
1039
|
+
const first = test(start);
|
|
1040
|
+
if (!first) continue;
|
|
1041
|
+
const cases = [{ values: [first.value.text], target: start.trueTarget }];
|
|
1042
|
+
const chain = [];
|
|
1043
|
+
let next = byId.get(start.falseTarget);
|
|
1044
|
+
for (;;) {
|
|
1045
|
+
const more = test(next, first.register);
|
|
1046
|
+
if (!more || predecessors.get(next.id) !== 1) break;
|
|
1047
|
+
chain.push(next);
|
|
1048
|
+
const last = cases.at(-1);
|
|
1049
|
+
if (last.target === next.trueTarget) last.values.push(more.value.text);
|
|
1050
|
+
else cases.push({ values: [more.value.text], target: next.trueTarget });
|
|
1051
|
+
next = byId.get(next.falseTarget);
|
|
1052
|
+
}
|
|
1053
|
+
if (cases.length + chain.length < 2) continue;
|
|
1054
|
+
// The value must not be read again: the switch no longer stores it.
|
|
1055
|
+
const name = `_r${first.register}`;
|
|
1056
|
+
const others = blocks.filter(block => block !== start && !chain.includes(block));
|
|
1057
|
+
if (others.some(block => block.stmts.some(text => new RegExp(`\b${name}\b`).test(text)) || new RegExp(`\b${name}\b`).test(block.cond ?? ''))) continue;
|
|
1058
|
+
// A jump block with nothing in it after the last test leads to the default (or the end).
|
|
1059
|
+
let fallback = next?.id ?? null;
|
|
1060
|
+
if (next && next.term === 'jump' && !next.stmts.length && predecessors.get(next.id) === 1) fallback = next.next;
|
|
1061
|
+
Object.assign(start, {
|
|
1062
|
+
term: 'switch', switchExpr: first.discriminant.text, switchCases: cases, switchDefault: fallback,
|
|
1063
|
+
stmts: first.dropStatement ? start.stmts.slice(0, -1) : start.stmts,
|
|
1064
|
+
});
|
|
1065
|
+
delete start.cond;
|
|
1066
|
+
delete start.trueTarget;
|
|
1067
|
+
delete start.falseTarget;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/** The structurer's fallback for control flow with no structured form, written in ActionScript. */
|
|
1072
|
+
function dispatch(blocks, entry) {
|
|
1073
|
+
const out = [`var _pc = ${entry};`, 'while (_pc != -1) {', 'switch (_pc) {'];
|
|
1074
|
+
for (const block of blocks) {
|
|
1075
|
+
out.push(`case ${block.id}:`, ...block.stmts);
|
|
1076
|
+
if (block.term === 'branch') out.push(`_pc = (${block.cond}) ? ${block.trueTarget} : ${block.falseTarget};`, 'break;');
|
|
1077
|
+
else if (block.term === 'jump' || block.term === 'fallthrough') out.push(`_pc = ${block.next ?? -1};`, 'break;');
|
|
1078
|
+
else if (block.stmts.length && /^(return|throw|break|continue)\b/.test(block.stmts.at(-1).trim())) out.push('break;');
|
|
1079
|
+
else out.push('_pc = -1;', 'break;');
|
|
1080
|
+
}
|
|
1081
|
+
out.push('default:', '_pc = -1;', '}', '}');
|
|
1082
|
+
return out;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function pushValues(action) {
|
|
1086
|
+
const values = [];
|
|
1087
|
+
const data = action.data;
|
|
1088
|
+
let at = 0;
|
|
1089
|
+
while (at < data.length) {
|
|
1090
|
+
const type = data[at++];
|
|
1091
|
+
if (type === 0) {
|
|
1092
|
+
let end = at;
|
|
1093
|
+
while (end < data.length && data[end] !== 0) end++;
|
|
1094
|
+
values.push({ type: 'string', value: new TextDecoder().decode(data.subarray(at, end)) });
|
|
1095
|
+
at = end + 1;
|
|
1096
|
+
} else if (type === 4) values.push({ type: 'register', value: data[at++] });
|
|
1097
|
+
else if (type === 5 || type === 8) { values.push({ type: type === 5 ? 'boolean' : 'constant', value: data[at++] }); }
|
|
1098
|
+
else if (type === 9) { values.push({ type: 'constant', value: ui16(data, at) }); at += 2; }
|
|
1099
|
+
else if (type === 1 || type === 7) { values.push({ type: 'number' }); at += 4; }
|
|
1100
|
+
else if (type === 6) { values.push({ type: 'number' }); at += 8; }
|
|
1101
|
+
else values.push({ type: type === 2 ? 'null' : 'undefined' });
|
|
1102
|
+
}
|
|
1103
|
+
return values;
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
function pushReadsRegister(action, register) {
|
|
1107
|
+
return pushValues(action).some(value => value.type === 'register' && value.value === register);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
/** `x = x + 1` → `x++` etc.: exact rewrites (the compiler writes the same bytecode for both forms). */
|
|
1111
|
+
function assignmentText(targetText, value, asExpression) {
|
|
1112
|
+
// As a value, `++x` gives the new value, as Increment then the stored register do.
|
|
1113
|
+
if (value.step && value.of.text === targetText) return asExpression ? `${value.step}${targetText}` : `${targetText}${value.step}`;
|
|
1114
|
+
if (value.binary && value.binary.left.text === targetText && !value.binary.left.call) {
|
|
1115
|
+
const op = value.binary.op;
|
|
1116
|
+
if (['+', '-', '*', '/', '%', '&', '|', '^', '<<', '>>', '>>>'].includes(op)) return `${targetText} ${op}= ${wrap(value.binary.right, PREC.assign)}`;
|
|
1117
|
+
}
|
|
1118
|
+
return `${targetText} = ${wrap(value, PREC.assign)}`;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/** Re-indent by braces (strings and comments aware enough for generated code). */
|
|
1122
|
+
function reindent(text) {
|
|
1123
|
+
const out = [];
|
|
1124
|
+
let depth = 0;
|
|
1125
|
+
for (const raw of text.split('\n')) {
|
|
1126
|
+
const line = raw.trim();
|
|
1127
|
+
if (!line) continue;
|
|
1128
|
+
const code = line.replace(/"(?:[^"\\]|\\.)*"/g, '""');
|
|
1129
|
+
const leadingClose = /^[}\]]/.test(code) ? 1 : 0;
|
|
1130
|
+
const caseLine = /^(case\b.*|default):$/.test(code);
|
|
1131
|
+
out.push(`${' '.repeat(Math.max(0, depth - leadingClose - (caseLine ? 1 : 0)))}${line}`);
|
|
1132
|
+
for (const char of code) {
|
|
1133
|
+
if (char === '{') depth++;
|
|
1134
|
+
else if (char === '}') depth--;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
return out.join('\n') + (out.length ? '\n' : '');
|
|
1138
|
+
}
|