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,1609 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import * as acorn from 'acorn';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import { parse } from '../parser/swf-parser.js';
|
|
5
|
+
import { decode, decodeTag } from '../decoder/tag-decoder.js';
|
|
6
|
+
import { TAG } from '../decoder/tag-types.js';
|
|
7
|
+
import { SWFLIB_SOURCE } from '../runtime/index.js';
|
|
8
|
+
import { transpileAvm1, AVM1_RUNTIME_STUB } from '../transpiler/avm1/avm1-transpiler.js';
|
|
9
|
+
import { transpileAvm2, AVM2_RUNTIME_STUB } from '../transpiler/avm2/avm2-transpiler.js';
|
|
10
|
+
import { buildPreloadAliases, materializePreloadAliases, DF2 } from '../transpiler/avm1/avm1-ir.js';
|
|
11
|
+
import { transpileActionScript } from '../transpiler/actionscript.js';
|
|
12
|
+
import { DiagnosticsCollector } from '../util/diagnostics.js';
|
|
13
|
+
import { encodeSoundStream } from '../emitter/asset-encoder.js';
|
|
14
|
+
|
|
15
|
+
/** Build a minimal ABC buffer with 1 class, 1 method body, and 1 script. */
|
|
16
|
+
function buildMinimalAbc(codeBytes, { superName = 'Sprite', localCount = 2, paramCount = 0, traitKind = 0, instanceFlags = 0 } = {}) {
|
|
17
|
+
const bytes = [];
|
|
18
|
+
const u16 = (v) => bytes.push(v & 0xFF, (v >> 8) & 0xFF);
|
|
19
|
+
const u30 = (v) => {
|
|
20
|
+
let n = v >>> 0;
|
|
21
|
+
while (n >= 0x80) {
|
|
22
|
+
bytes.push((n & 0x7F) | 0x80);
|
|
23
|
+
n >>>= 7;
|
|
24
|
+
}
|
|
25
|
+
bytes.push(n);
|
|
26
|
+
};
|
|
27
|
+
const str = (s) => {
|
|
28
|
+
const enc = new TextEncoder().encode(s);
|
|
29
|
+
u30(enc.length);
|
|
30
|
+
for (const b of enc) bytes.push(b);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
u16(16); u16(46); // minor 16, major 46
|
|
34
|
+
u30(0); // ints
|
|
35
|
+
u30(0); // uints
|
|
36
|
+
u30(0); // doubles
|
|
37
|
+
|
|
38
|
+
// strings: 1="", 2="TestClass", 3=superName, 4="foo"
|
|
39
|
+
u30(5);
|
|
40
|
+
str('');
|
|
41
|
+
str('TestClass');
|
|
42
|
+
str(superName);
|
|
43
|
+
str('foo');
|
|
44
|
+
|
|
45
|
+
// namespaces: 1=PackageNamespace("")
|
|
46
|
+
u30(2);
|
|
47
|
+
bytes.push(0x16); u30(1);
|
|
48
|
+
|
|
49
|
+
// nsSets
|
|
50
|
+
u30(0);
|
|
51
|
+
|
|
52
|
+
// multinames: 1=TestClass, 2=superName, 3=foo
|
|
53
|
+
u30(4);
|
|
54
|
+
bytes.push(0x07); u30(1); u30(2);
|
|
55
|
+
bytes.push(0x07); u30(1); u30(3);
|
|
56
|
+
bytes.push(0x07); u30(1); u30(4);
|
|
57
|
+
|
|
58
|
+
// methods: 0=iinit, 1=cinit, 2=scriptInit
|
|
59
|
+
u30(3);
|
|
60
|
+
for (let m = 0; m < 3; m++) {
|
|
61
|
+
u30(paramCount); u30(0); u30(0); bytes.push(0);
|
|
62
|
+
for (let p = 0; p < paramCount; p++) u30(0);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// metadata
|
|
66
|
+
u30(0);
|
|
67
|
+
|
|
68
|
+
// classes: 1
|
|
69
|
+
u30(1);
|
|
70
|
+
// instance 0
|
|
71
|
+
u30(1); // nameIndex TestClass
|
|
72
|
+
u30(2); // superNameIndex
|
|
73
|
+
bytes.push(instanceFlags); // flags
|
|
74
|
+
u30(0); // ifaceCount
|
|
75
|
+
u30(0); // iinit
|
|
76
|
+
u30(1); // 1 slot trait
|
|
77
|
+
u30(3); bytes.push(traitKind & 0x0F); u30(1); u30(0); u30(0);
|
|
78
|
+
// class 0
|
|
79
|
+
u30(1); // cinit
|
|
80
|
+
u30(0); // traits
|
|
81
|
+
|
|
82
|
+
// scripts: 1
|
|
83
|
+
u30(1);
|
|
84
|
+
u30(2); u30(0);
|
|
85
|
+
|
|
86
|
+
// method bodies: 1 (for method 0)
|
|
87
|
+
u30(1);
|
|
88
|
+
u30(0); // methodIndex
|
|
89
|
+
u30(4); // maxStack
|
|
90
|
+
u30(localCount);
|
|
91
|
+
u30(1); u30(3); // scopeDepth
|
|
92
|
+
u30(codeBytes.length);
|
|
93
|
+
for (const b of codeBytes) bytes.push(b);
|
|
94
|
+
u30(0); // exceptions
|
|
95
|
+
u30(0); // traits
|
|
96
|
+
|
|
97
|
+
return new Uint8Array(bytes);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
describe('AVM2 transpiler', () => {
|
|
101
|
+
it('enforces const slot writes and sealed layout IDs while retaining dynamic and inherited slots', () => {
|
|
102
|
+
const constructor = [0xD0, 0x24, 7, 0x6D, 1, 0x47]; // this; pushbyte 7; setslot 1; returnvoid
|
|
103
|
+
const abcData = buildMinimalAbc(constructor, { superName: 'Object', traitKind: 6, instanceFlags: 1 });
|
|
104
|
+
const js = transpileAvm2(abcData, new DiagnosticsCollector());
|
|
105
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\n${AVM2_RUNTIME_STUB}\n${js}\nreturn { TestClass, _getSlot, _setSlot };`)();
|
|
106
|
+
const value = new runtime.TestClass();
|
|
107
|
+
expect(runtime._getSlot(value, 1)).toBe(7); // constructor initializer may set a const once
|
|
108
|
+
expect(() => runtime._setSlot(value, 1, 8, false)).toThrow(/constant AVM2 slot/);
|
|
109
|
+
expect(() => runtime._setSlot(value, 1, 9, true)).toThrow(/more than once/);
|
|
110
|
+
expect(() => runtime._getSlot(value, 2)).toThrow(/Unknown AVM2 slot ID 2 on sealed/);
|
|
111
|
+
expect(() => runtime._setSlot(value, 2, 10, false)).toThrow(/Unknown AVM2 slot ID 2 on sealed/);
|
|
112
|
+
expect(() => runtime._getSlot(value, 0)).toThrow(/Invalid AVM2 slot ID 0/);
|
|
113
|
+
|
|
114
|
+
// Child class metadata inherits slot and const identity from its parent.
|
|
115
|
+
function Parent() {}
|
|
116
|
+
Parent.__swoofDynamic = false;
|
|
117
|
+
Parent.__avm2InstanceSlots = { 1: 'inheritedValue' };
|
|
118
|
+
Parent.__avm2InstanceConstSlots = { 1: true };
|
|
119
|
+
function Child() {}
|
|
120
|
+
Child.prototype = Object.create(Parent.prototype);
|
|
121
|
+
Child.prototype.constructor = Child;
|
|
122
|
+
Child.__swoofDynamic = false;
|
|
123
|
+
Child.__avm2InstanceSlots = Object.create(Parent.__avm2InstanceSlots);
|
|
124
|
+
Child.__avm2InstanceConstSlots = Object.create(Parent.__avm2InstanceConstSlots);
|
|
125
|
+
const child = new Child();
|
|
126
|
+
child.inheritedValue = 3;
|
|
127
|
+
expect(runtime._getSlot(child, 1)).toBe(3);
|
|
128
|
+
expect(() => runtime._setSlot(child, 1, 4, false)).toThrow(/constant AVM2 slot/);
|
|
129
|
+
expect(() => runtime._getSlot(child, 2)).toThrow(/Unknown AVM2 slot ID 2 on sealed/);
|
|
130
|
+
|
|
131
|
+
const dynamicJs = transpileAvm2(buildMinimalAbc([0x47], { superName: 'Object' }), new DiagnosticsCollector());
|
|
132
|
+
const dynamicRuntime = new Function(`${SWFLIB_SOURCE}\n${AVM2_RUNTIME_STUB}\n${dynamicJs}\nreturn { TestClass, _getSlot, _setSlot };`)();
|
|
133
|
+
const dynamic = new dynamicRuntime.TestClass();
|
|
134
|
+
expect(dynamicRuntime._getSlot(dynamic, 20)).toBeUndefined();
|
|
135
|
+
dynamicRuntime._setSlot(dynamic, 20, 'fallback');
|
|
136
|
+
expect(dynamicRuntime._getSlot(dynamic, 20)).toBe('fallback');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('skips dead obfuscated bytes behind unconditional jump and opaque predicates', () => {
|
|
140
|
+
// getlocal0; jump +5 over [newarray 202670078]; pushtrue; iftrue +4 over [newobject 999999]; returnvoid
|
|
141
|
+
const code = [
|
|
142
|
+
0xD0,
|
|
143
|
+
0x10, 0x05, 0x00, 0x00,
|
|
144
|
+
0x56, 0xFE, 0xFF, 0xFF, 0x0F, // dead obfuscated newarray with huge u30
|
|
145
|
+
0x26, // pushtrue
|
|
146
|
+
0x11, 0x04, 0x00, 0x00, // opaque iftrue +4
|
|
147
|
+
0x55, 0xFF, 0xFF, 0x3F, // dead obfuscated newobject
|
|
148
|
+
0x47, // returnvoid
|
|
149
|
+
];
|
|
150
|
+
const abcData = buildMinimalAbc(code);
|
|
151
|
+
const diag = new DiagnosticsCollector();
|
|
152
|
+
const js = transpileAvm2(abcData, diag);
|
|
153
|
+
|
|
154
|
+
expect(diag.errors()).toHaveLength(0);
|
|
155
|
+
expect(diag.warnings()).toHaveLength(0);
|
|
156
|
+
expect(js).toContain('class TestClass extends Sprite');
|
|
157
|
+
const instance = new Function(`class Sprite {}\n${AVM2_RUNTIME_STUB}\n${js}\nreturn new TestClass();`)();
|
|
158
|
+
expect(Object.hasOwn(instance, 'foo')).toBe(true);
|
|
159
|
+
expect(instance.foo).toBeUndefined();
|
|
160
|
+
expect(() => acorn.parse(SWFLIB_SOURCE + '\n' + AVM2_RUNTIME_STUB + '\n' + js, { ecmaVersion: 2020 })).not.toThrow();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('decodes pushshort as u30 and produces valid ES2020', () => {
|
|
164
|
+
// pushshort 300 (u30: 0xAC 0x02), returnvalue
|
|
165
|
+
const code = [0x25, 0xAC, 0x02, 0x48];
|
|
166
|
+
const abcData = buildMinimalAbc(code, { superName: 'Object' });
|
|
167
|
+
const diag = new DiagnosticsCollector();
|
|
168
|
+
const js = transpileAvm2(abcData, diag);
|
|
169
|
+
|
|
170
|
+
expect(diag.warnings()).toHaveLength(0);
|
|
171
|
+
expect(js).toContain('return 300;');
|
|
172
|
+
expect(() => acorn.parse(SWFLIB_SOURCE + '\n' + AVM2_RUNTIME_STUB + '\n' + js, { ecmaVersion: 2020 })).not.toThrow();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('runs the common tail for a lookupswitch whose arms precede the dispatch', () => {
|
|
176
|
+
const code = [
|
|
177
|
+
0x10, 27, 0, 0, // 0: jump to the selector
|
|
178
|
+
0xd0, 0x24, 10, 0x61, 3, 0x10, 30, 0, 0, // 4: case 0, foo = 10
|
|
179
|
+
0xd0, 0x24, 20, 0x61, 3, 0x10, 21, 0, 0, // 13: case 1, foo = 20
|
|
180
|
+
0xd0, 0x24, 30, 0x61, 3, 0x10, 12, 0, 0, // 22: default, foo = 30
|
|
181
|
+
0xd1, // 31: getlocal1
|
|
182
|
+
0x1b, 0xf6, 0xff, 0xff, 1,
|
|
183
|
+
0xe4, 0xff, 0xff, 0xed, 0xff, 0xff, // 32: lookupswitch -> 22, 4, 13
|
|
184
|
+
0xd0, 0xd0, 0x66, 3, 0x24, 5, 0xc5, 0x61, 3, 0x47, // 43: foo += 5
|
|
185
|
+
];
|
|
186
|
+
const diagnostics = new DiagnosticsCollector();
|
|
187
|
+
const js = transpileAvm2(buildMinimalAbc(code, { superName: 'Object', localCount: 2, paramCount: 1 }), diagnostics);
|
|
188
|
+
expect(diagnostics.errors()).toHaveLength(0);
|
|
189
|
+
expect(diagnostics.warnings()).toHaveLength(0);
|
|
190
|
+
const TestClass = new Function(`${AVM2_RUNTIME_STUB}\n${js}\nreturn TestClass;`)();
|
|
191
|
+
expect([0, 1, 4].map(value => new TestClass(value).foo)).toEqual([15, 25, 35]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('uses a runtime stack dispatcher for inconsistent joins and preserves bounded loops', () => {
|
|
195
|
+
// The true arm reaches PC 11 with an empty stack; the false arm has one
|
|
196
|
+
// extra value. Both arms then write foo and enter a three-iteration loop.
|
|
197
|
+
const code = [
|
|
198
|
+
0xD1, // 0 getlocal1
|
|
199
|
+
0x12, 0x04, 0x00, 0x00, // 1 iffalse -> 9
|
|
200
|
+
0x10, 0x02, 0x00, 0x00, // 5 jump -> 11
|
|
201
|
+
0x24, 99, // 9 pushbyte 99
|
|
202
|
+
0xD0, // 11 getlocal0
|
|
203
|
+
0x24, 42, // 12 pushbyte 42
|
|
204
|
+
0x61, 0x03, // 14 setproperty foo
|
|
205
|
+
0x29, // 16 pop (only consumes the extra false-arm value)
|
|
206
|
+
0x24, 0, 0x63, 0x02, // 17 pushbyte 0; setlocal2
|
|
207
|
+
0x62, 0x02, 0x24, 3, // 21 loop: getlocal2; pushbyte 3
|
|
208
|
+
0x18, 0x09, 0x00, 0x00, // 25 ifge -> 38
|
|
209
|
+
0x62, 0x02, 0x91, 0x63, 0x02, // 29 getlocal2; increment; setlocal2
|
|
210
|
+
0x10, 0xEF, 0xFF, 0xFF, // 34 jump -> 21
|
|
211
|
+
0xD0, 0x62, 0x02, 0x61, 0x03, // 38 getlocal0; getlocal2; setproperty foo
|
|
212
|
+
0x47, // 43 returnvoid
|
|
213
|
+
];
|
|
214
|
+
const abcData = buildMinimalAbc(code, { superName: 'Object', localCount: 3, paramCount: 1 });
|
|
215
|
+
const diag = new DiagnosticsCollector();
|
|
216
|
+
const js = transpileAvm2(abcData, diag);
|
|
217
|
+
expect(diag.errors()).toHaveLength(0);
|
|
218
|
+
expect(js).toContain('var _avm2Stack = [];');
|
|
219
|
+
expect(js).toContain('_avm2Dispatch: while');
|
|
220
|
+
expect(js).toContain('var _avm2CheckBudget = _createAvm2ExecutionBudget();');
|
|
221
|
+
expect(() => acorn.parse(SWFLIB_SOURCE + '\n' + AVM2_RUNTIME_STUB + '\n' + js, { ecmaVersion: 2020 })).not.toThrow();
|
|
222
|
+
|
|
223
|
+
const priorSetDomainMemory = globalThis.__swoof_setDomainMemory;
|
|
224
|
+
const priorGetDefinitionByName = globalThis.getDefinitionByName;
|
|
225
|
+
try {
|
|
226
|
+
const TestClass = new Function(`${SWFLIB_SOURCE}\n${AVM2_RUNTIME_STUB}\n${js}\nreturn TestClass;`)();
|
|
227
|
+
expect(new TestClass(true).foo).toBe(3);
|
|
228
|
+
expect(new TestClass(false).foo).toBe(3);
|
|
229
|
+
} finally {
|
|
230
|
+
if (priorSetDomainMemory === undefined) delete globalThis.__swoof_setDomainMemory;
|
|
231
|
+
else globalThis.__swoof_setDomainMemory = priorSetDomainMemory;
|
|
232
|
+
if (priorGetDefinitionByName === undefined) delete globalThis.getDefinitionByName;
|
|
233
|
+
else globalThis.getDefinitionByName = priorGetDefinitionByName;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('transpiles raze-2.swf AVM2 without timeout and produces valid ES2020', async () => {
|
|
238
|
+
const bytes = new Uint8Array(readFileSync('archive/Swf/raze-2.swf'));
|
|
239
|
+
const diag = new DiagnosticsCollector();
|
|
240
|
+
const { doc } = await parse(bytes, diag);
|
|
241
|
+
const { tags } = decode(doc, diag);
|
|
242
|
+
const result = transpileActionScript(tags, diag);
|
|
243
|
+
|
|
244
|
+
expect(diag.errors()).toHaveLength(0);
|
|
245
|
+
expect(result.avm2Blocks.length).toBe(2);
|
|
246
|
+
expect(() => acorn.parse(result.fullJs, { ecmaVersion: 2020 })).not.toThrow();
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe('AVM1 transpiler', () => {
|
|
251
|
+
it('paints MovieClip drawing commands from createEmptyMovieClip', () => {
|
|
252
|
+
const displayList = Object.create(null);
|
|
253
|
+
const run = new Function('displayList', `${SWFLIB_SOURCE}\n${AVM1_RUNTIME_STUB}\n` +
|
|
254
|
+
`var clip = root.createEmptyMovieClip('overlay', 10);\n` +
|
|
255
|
+
`clip.beginFill(0xff0000, 50);\n` +
|
|
256
|
+
`clip.lineStyle(2, 0x0000ff);\n` +
|
|
257
|
+
`clip.moveTo(1, 2); clip.lineTo(10, 2); clip.curveTo(12, 8, 4, 10); clip.endFill();\n` +
|
|
258
|
+
`return displayList[10];`);
|
|
259
|
+
const entry = run(displayList);
|
|
260
|
+
expect(entry.name).toBe('overlay');
|
|
261
|
+
expect(entry._dynamicDraws).toHaveLength(1);
|
|
262
|
+
expect(entry._dynamicDraws[0].fill).toBe('rgba(255,0,0,0.5)');
|
|
263
|
+
expect(entry._dynamicDraws[0].stroke.color).toBe('rgba(0,0,255,1)');
|
|
264
|
+
expect(entry._dynamicDraws[0].path).toEqual([
|
|
265
|
+
['M', 1, 2], ['L', 10, 2], ['Q', 12, 8, 4, 10], ['Z']
|
|
266
|
+
]);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('supports StoreRegister, Increment, Decrement, Jump, and valid ES2020 output', () => {
|
|
270
|
+
// Push 41; StoreRegister 1; Increment; Pop; Jump +0; End
|
|
271
|
+
const bytecode = new Uint8Array([
|
|
272
|
+
0x96, 0x05, 0x00, 0x07, 41, 0, 0, 0, // Push int 41
|
|
273
|
+
0x87, 0x01, 0x00, 0x01, // StoreRegister 1
|
|
274
|
+
0x50, // Increment
|
|
275
|
+
0x17, // Pop
|
|
276
|
+
0x99, 0x02, 0x00, 0x00, 0x00, // Jump +0
|
|
277
|
+
0x00, // End
|
|
278
|
+
]);
|
|
279
|
+
const diag = new DiagnosticsCollector();
|
|
280
|
+
const js = transpileAvm1(bytecode, diag);
|
|
281
|
+
|
|
282
|
+
expect(diag.warnings()).toHaveLength(0);
|
|
283
|
+
expect(js).toContain('_registers[1] = 41;');
|
|
284
|
+
expect(js).toContain('(41 + 1);');
|
|
285
|
+
const wrapped = `${SWFLIB_SOURCE}\n${AVM1_RUNTIME_STUB}\nrunClipScript(root, function () {\n${js}\n});`;
|
|
286
|
+
expect(() => acorn.parse(wrapped, { ecmaVersion: 2020 })).not.toThrow();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('emits this.x for GetVariable/SetVariable string literals', () => {
|
|
290
|
+
// Push "timer" + int 0; SetVariable; Push "timer"; GetVariable; Pop; End
|
|
291
|
+
const bytecode = new Uint8Array([
|
|
292
|
+
0x96, 0x0c, 0x00, // Push length 12
|
|
293
|
+
0x00, 0x74, 0x69, 0x6d, 0x65, 0x72, 0x00, // string "timer"
|
|
294
|
+
0x07, 0x00, 0x00, 0x00, 0x00, // int 0
|
|
295
|
+
0x1D, // SetVariable
|
|
296
|
+
0x96, 0x07, 0x00, // Push length 7
|
|
297
|
+
0x00, 0x74, 0x69, 0x6d, 0x65, 0x72, 0x00, // string "timer"
|
|
298
|
+
0x1C, // GetVariable
|
|
299
|
+
0x17, // Pop
|
|
300
|
+
0x00,
|
|
301
|
+
]);
|
|
302
|
+
const js = transpileAvm1(bytecode, new DiagnosticsCollector());
|
|
303
|
+
expect(js).toContain('this.timer = 0;');
|
|
304
|
+
expect(js).toMatch(/this\.timer/);
|
|
305
|
+
expect(js).not.toContain('_setScope');
|
|
306
|
+
expect(js).not.toContain('_scope(');
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('maps DefineFunction2 flags with Adobe MSB bitfields (0x6a → root only)', () => {
|
|
310
|
+
const aliases = buildPreloadAliases(0x6a);
|
|
311
|
+
expect(aliases.get(1)).toBe('root');
|
|
312
|
+
expect([...aliases.values()]).not.toContain('arguments');
|
|
313
|
+
expect(buildPreloadAliases(DF2.PRELOAD_THIS | DF2.PRELOAD_ROOT).get(1)).toBe('this');
|
|
314
|
+
expect(buildPreloadAliases(DF2.PRELOAD_THIS | DF2.PRELOAD_ROOT).get(2)).toBe('root');
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('materializes preload parent as assignable local (no invalid LHS)', () => {
|
|
318
|
+
const { aliases, preamble } = materializePreloadAliases(DF2.PRELOAD_PARENT);
|
|
319
|
+
expect(preamble[0]).toMatch(/^var _r1 = \(this\.parent \|\| root\);$/);
|
|
320
|
+
expect(aliases.get(1)).toBe('_r1');
|
|
321
|
+
// StoreRegister-style assign must be valid
|
|
322
|
+
expect(() => acorn.parse(`function f(){ ${preamble.join('\n')} ${aliases.get(1)} = 0; }`, { ecmaVersion: 2020 })).not.toThrow();
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('preloads arguments with callee for mx.utils.Delegate-style bindings', () => {
|
|
326
|
+
expect(buildPreloadAliases(DF2.PRELOAD_ARGUMENTS).get(1))
|
|
327
|
+
.toBe('_avm1PreloadArguments(arguments, _avm1Function)');
|
|
328
|
+
// PreloadThis then arguments → regs 1, 2
|
|
329
|
+
expect(buildPreloadAliases(DF2.PRELOAD_THIS | DF2.PRELOAD_ARGUMENTS).get(2))
|
|
330
|
+
.toBe('_avm1PreloadArguments(arguments, _avm1Function)');
|
|
331
|
+
|
|
332
|
+
// DefineFunction2 anon, 0 params, regCount=2, flags=PreloadArguments (0x04)
|
|
333
|
+
// body: Push register 1; GetMember "callee"; Return
|
|
334
|
+
const body = [
|
|
335
|
+
0x96, 0x02, 0x00, 0x04, 0x01, // Push register 1
|
|
336
|
+
0x96, 0x08, 0x00, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x65, 0x65, 0x00, // Push "callee"
|
|
337
|
+
0x4E, // GetMember
|
|
338
|
+
0x3E, // Return
|
|
339
|
+
];
|
|
340
|
+
const header = [
|
|
341
|
+
0x00, 0x00, 0x00, 0x02, 0x04, 0x00,
|
|
342
|
+
body.length & 0xff, (body.length >> 8) & 0xff,
|
|
343
|
+
];
|
|
344
|
+
const bytecode = new Uint8Array([
|
|
345
|
+
0x96, 0x03, 0x00, 0x00, 0x66, 0x00, // Push "f"
|
|
346
|
+
0x8E, header.length & 0xff, (header.length >> 8) & 0xff,
|
|
347
|
+
...header,
|
|
348
|
+
...body,
|
|
349
|
+
0x1D, // SetVariable
|
|
350
|
+
0x00,
|
|
351
|
+
]);
|
|
352
|
+
const js = transpileAvm1(bytecode, new DiagnosticsCollector());
|
|
353
|
+
expect(js).toContain('_avm1PreloadArguments(arguments, _avm1Function)');
|
|
354
|
+
expect(js).toContain('var _avm1Function =');
|
|
355
|
+
expect(js).not.toContain('Array.prototype.slice.call(arguments)');
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('nests DefineFunction2 body inside the function (not in parent)', () => {
|
|
359
|
+
// Push "f";
|
|
360
|
+
// DefineFunction2 anon, 0 params, regCount=2, flags=PreloadThis (0x01),
|
|
361
|
+
// body: Push int 99; Return
|
|
362
|
+
// SetVariable; End
|
|
363
|
+
const body = [
|
|
364
|
+
0x96, 0x05, 0x00, 0x07, 99, 0, 0, 0, // Push int 99
|
|
365
|
+
0x3E, // Return
|
|
366
|
+
];
|
|
367
|
+
const header = [
|
|
368
|
+
0x00, // empty name
|
|
369
|
+
0x00, 0x00, // 0 params
|
|
370
|
+
0x02, // registerCount
|
|
371
|
+
0x01, 0x00, // flags: PreloadThis
|
|
372
|
+
body.length & 0xff, (body.length >> 8) & 0xff,
|
|
373
|
+
];
|
|
374
|
+
const bytecode = new Uint8Array([
|
|
375
|
+
0x96, 0x03, 0x00, 0x00, 0x66, 0x00, // Push "f"
|
|
376
|
+
0x8E, header.length & 0xff, (header.length >> 8) & 0xff,
|
|
377
|
+
...header,
|
|
378
|
+
...body,
|
|
379
|
+
0x1D, // SetVariable
|
|
380
|
+
0x00,
|
|
381
|
+
]);
|
|
382
|
+
const js = transpileAvm1(bytecode, new DiagnosticsCollector());
|
|
383
|
+
expect(js).toContain('this.f = ');
|
|
384
|
+
expect(js).toContain('return 99;');
|
|
385
|
+
const fnIdx = js.indexOf('function');
|
|
386
|
+
const retIdx = js.indexOf('return 99;');
|
|
387
|
+
expect(fnIdx).toBeGreaterThanOrEqual(0);
|
|
388
|
+
expect(retIdx).toBeGreaterThan(fnIdx);
|
|
389
|
+
expect(js).not.toMatch(/_registers\[1\]\s*=\s*arguments/);
|
|
390
|
+
expect(js).not.toContain('_this.');
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('transpiles age-of-war.swf AVM1 scripts cleanly with idiomatic this', async () => {
|
|
394
|
+
const bytes = new Uint8Array(readFileSync('archive/Swf/age-of-war.swf'));
|
|
395
|
+
const diag = new DiagnosticsCollector();
|
|
396
|
+
const { doc } = await parse(bytes, diag);
|
|
397
|
+
const { tags } = decode(doc, diag);
|
|
398
|
+
const result = transpileActionScript(tags, diag);
|
|
399
|
+
|
|
400
|
+
expect(diag.errors()).toHaveLength(0);
|
|
401
|
+
expect(result.avm1Scripts.length).toBeGreaterThan(0);
|
|
402
|
+
expect(() => acorn.parse(result.fullJs, { ecmaVersion: 2020 })).not.toThrow();
|
|
403
|
+
|
|
404
|
+
const sample = result.avm1Scripts.find(s => s.jsSource.includes('onEnterFrame'));
|
|
405
|
+
expect(sample).toBeTruthy();
|
|
406
|
+
expect(sample.jsSource).toContain('this.onEnterFrame');
|
|
407
|
+
// Preload root is materialized as _r1 = root and display names reach the member helpers.
|
|
408
|
+
expect(sample.jsSource).toMatch(/PAUSED/);
|
|
409
|
+
expect(sample.jsSource).toMatch(/var _r\d+ = root/);
|
|
410
|
+
expect(sample.jsSource).not.toContain('arguments.PAUSED');
|
|
411
|
+
expect(sample.jsSource).not.toContain('(this.parent || root) =');
|
|
412
|
+
expect(sample.jsSource).not.toContain('(this._parent || _root) =');
|
|
413
|
+
expect(sample.jsSource).not.toContain('_setScope(');
|
|
414
|
+
expect(sample.jsSource).not.toContain('_this.');
|
|
415
|
+
expect(sample.jsSource).not.toContain('._x');
|
|
416
|
+
expect(sample.jsSource).not.toContain('_root');
|
|
417
|
+
expect(sample.jsSource).not.toMatch(/this\.KillMe\s*\(/);
|
|
418
|
+
expect(sample.jsSource).toMatch(/\/\* sprite_\d+:/);
|
|
419
|
+
expect(sample.jsSource).toContain('"_yscale"');
|
|
420
|
+
expect(sample.jsSource).toContain('"_visible"');
|
|
421
|
+
expect(sample.jsSource).toMatch(/_setMember(?:\.call\(this, |\()_avm1GetVariable\(_avm1Locals, "hb", this, this\), /);
|
|
422
|
+
expect(sample.jsSource).toMatch(/_setMember(?:\.call\(this, |\()_avm1GetVariable\(_avm1Locals, "e\d+", this, this\), /);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe('swflib Vector3D math', () => {
|
|
427
|
+
it('implements AS3 vector arithmetic, projection, comparison, and 3D measures', () => {
|
|
428
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
429
|
+
const { Vector3D } = runtime.builtins;
|
|
430
|
+
const a = new Vector3D(1, 2, 3, 7);
|
|
431
|
+
const b = new Vector3D(4, 6, 3, 9);
|
|
432
|
+
|
|
433
|
+
expect(a.add(b)).toMatchObject({ x: 5, y: 8, z: 6, w: 0 });
|
|
434
|
+
expect(a.subtract(b)).toMatchObject({ x: -3, y: -4, z: 0, w: 0 });
|
|
435
|
+
expect(a).toMatchObject({ x: 1, y: 2, z: 3, w: 7 });
|
|
436
|
+
expect(a.dotProduct(b)).toBe(25);
|
|
437
|
+
expect(a.crossProduct(b)).toMatchObject({ x: -12, y: 9, z: -2, w: 0 });
|
|
438
|
+
expect(Vector3D.distance(new Vector3D(0, 0, 0), new Vector3D(2, 3, 6))).toBe(7);
|
|
439
|
+
expect(Vector3D.angleBetween(Vector3D.X_AXIS, Vector3D.Y_AXIS)).toBeCloseTo(Math.PI / 2);
|
|
440
|
+
expect(Vector3D.angleBetween(Vector3D.X_AXIS, Vector3D.X_AXIS)).toBeCloseTo(0);
|
|
441
|
+
|
|
442
|
+
const normalized = new Vector3D(3, 4, 0, 5);
|
|
443
|
+
expect(normalized.normalize()).toBe(5);
|
|
444
|
+
expect(normalized).toMatchObject({ x: 0.6, y: 0.8, z: 0, w: 5 });
|
|
445
|
+
normalized.scaleBy(2);
|
|
446
|
+
expect(normalized).toMatchObject({ x: 1.2, y: 1.6, z: 0, w: 5 });
|
|
447
|
+
normalized.project();
|
|
448
|
+
expect(normalized).toMatchObject({ x: 0.24, y: 0.32, z: 0, w: 5 });
|
|
449
|
+
|
|
450
|
+
const copy = new Vector3D();
|
|
451
|
+
copy.copyFrom(a);
|
|
452
|
+
expect(copy.equals(a, true)).toBe(true);
|
|
453
|
+
copy.negate();
|
|
454
|
+
expect(copy.nearEquals(new Vector3D(-1, -2, -3, 7), 1e-9, true)).toBe(true);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe('swflib display controls', () => {
|
|
459
|
+
it('composes Matrix3D transforms in AS3 append/prepend order', () => {
|
|
460
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
461
|
+
const { Matrix3D, Vector3D } = runtime.builtins;
|
|
462
|
+
const appended = new Matrix3D();
|
|
463
|
+
appended.appendScale(2, 3, 4).appendTranslation(10, 20, 30);
|
|
464
|
+
const a = appended.transformVector(new Vector3D(1, 1, 1, 1));
|
|
465
|
+
expect([a.x, a.y, a.z, a.w]).toEqual([12, 23, 34, 1]);
|
|
466
|
+
const prepended = new Matrix3D();
|
|
467
|
+
prepended.prependTranslation(10, 20, 30).appendScale(2, 3, 4);
|
|
468
|
+
const b = prepended.transformVector(new Vector3D(0, 0, 0, 1));
|
|
469
|
+
expect([b.x, b.y, b.z]).toEqual([20, 60, 120]);
|
|
470
|
+
const rotated = new Matrix3D().appendRotation(90, Vector3D.Z_AXIS);
|
|
471
|
+
const c = rotated.transformVector(new Vector3D(1, 0, 0, 1));
|
|
472
|
+
expect(c.x).toBeCloseTo(0);
|
|
473
|
+
expect(c.y).toBeCloseTo(1);
|
|
474
|
+
const pivoted = new Matrix3D().appendRotation(90, Vector3D.Z_AXIS, new Vector3D(1, 0, 0));
|
|
475
|
+
const pivotResult = pivoted.transformVector(new Vector3D(1, 0, 0, 1));
|
|
476
|
+
expect(pivotResult.x).toBeCloseTo(1);
|
|
477
|
+
expect(pivotResult.y).toBeCloseTo(0);
|
|
478
|
+
expect(rotated.position).toMatchObject({ x: 0, y: 0, z: 0 });
|
|
479
|
+
rotated.position = new Vector3D(5, 6, 7);
|
|
480
|
+
expect(rotated.position).toMatchObject({ x: 5, y: 6, z: 7 });
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it('computes Matrix3D determinants and inverts composed transforms safely', () => {
|
|
484
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
485
|
+
const { Matrix3D, Vector3D } = runtime.builtins;
|
|
486
|
+
const identity = new Matrix3D();
|
|
487
|
+
expect(identity.determinant).toBe(1);
|
|
488
|
+
expect(identity.invert()).toBe(true);
|
|
489
|
+
|
|
490
|
+
const matrix = new Matrix3D().appendRotation(37, Vector3D.Y_AXIS).appendTranslation(12, -8, 21);
|
|
491
|
+
expect(matrix.determinant).toBeCloseTo(1);
|
|
492
|
+
const original = matrix.clone();
|
|
493
|
+
const inverse = matrix.clone();
|
|
494
|
+
expect(inverse.invert()).toBe(true);
|
|
495
|
+
const product = original.clone().append(inverse);
|
|
496
|
+
product.rawData.forEach((value, i) => expect(value).toBeCloseTo(identity.rawData[i], 9));
|
|
497
|
+
const point = new Vector3D(3, -2, 9, 1);
|
|
498
|
+
const roundTrip = inverse.transformVector(original.transformVector(point));
|
|
499
|
+
expect(roundTrip.x).toBeCloseTo(point.x);
|
|
500
|
+
expect(roundTrip.y).toBeCloseTo(point.y);
|
|
501
|
+
expect(roundTrip.z).toBeCloseTo(point.z);
|
|
502
|
+
|
|
503
|
+
const singular = new Matrix3D().appendScale(2, 0, 3).appendTranslation(1, 2, 3);
|
|
504
|
+
const before = singular.rawData.slice();
|
|
505
|
+
expect(singular.determinant).toBe(0);
|
|
506
|
+
expect(singular.invert()).toBe(false);
|
|
507
|
+
expect(singular.rawData).toEqual(before);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('transposes column-major Matrix3D data and transforms batches of xyz vectors', () => {
|
|
511
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
512
|
+
const { Matrix3D, Vector3D } = runtime.builtins;
|
|
513
|
+
const matrix = new Matrix3D(Array.from({ length: 16 }, (_, i) => i + 1));
|
|
514
|
+
matrix.transpose();
|
|
515
|
+
expect(matrix.rawData).toEqual([1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16]);
|
|
516
|
+
|
|
517
|
+
const transform = new Matrix3D([
|
|
518
|
+
2, 0, 0, 0,
|
|
519
|
+
0, 3, 0, 0,
|
|
520
|
+
0, 0, 4, 0,
|
|
521
|
+
10, -5, 7, 1,
|
|
522
|
+
]);
|
|
523
|
+
const input = [1, 2, 3, -4, 0.5, 2];
|
|
524
|
+
const output = [];
|
|
525
|
+
expect(transform.transformVectors(input, output)).toBeUndefined();
|
|
526
|
+
expect(output).toEqual([12, 1, 19, 2, -3.5, 15]);
|
|
527
|
+
for (let i = 0; i < input.length; i += 3) {
|
|
528
|
+
const single = transform.transformVector(new Vector3D(input[i], input[i + 1], input[i + 2]));
|
|
529
|
+
expect(output[i]).toBeCloseTo(single.x);
|
|
530
|
+
expect(output[i + 1]).toBeCloseTo(single.y);
|
|
531
|
+
expect(output[i + 2]).toBeCloseTo(single.z);
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it('decomposes and recomposes affine Matrix3D translation, Euler rotation, and scale', () => {
|
|
536
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
537
|
+
const { Matrix3D, Vector3D } = runtime.builtins;
|
|
538
|
+
const source = new Matrix3D();
|
|
539
|
+
expect(source.recompose([
|
|
540
|
+
new Vector3D(11, -4, 20),
|
|
541
|
+
new Vector3D(25, -35, 70),
|
|
542
|
+
new Vector3D(2, 3, 4),
|
|
543
|
+
])).toBe(true);
|
|
544
|
+
const components = source.decompose();
|
|
545
|
+
expect(components).toHaveLength(3);
|
|
546
|
+
const result = new Matrix3D();
|
|
547
|
+
expect(result.recompose(components)).toBe(true);
|
|
548
|
+
result.rawData.forEach((value, i) => expect(value).toBeCloseTo(source.rawData[i], 8));
|
|
549
|
+
for (const style of ['quaternion', 'axisAngle']) {
|
|
550
|
+
const oriented = source.decompose(style);
|
|
551
|
+
expect(oriented).toHaveLength(3);
|
|
552
|
+
const roundTrip = new Matrix3D();
|
|
553
|
+
expect(roundTrip.recompose(oriented, style)).toBe(true);
|
|
554
|
+
roundTrip.rawData.forEach((value, i) => expect(value).toBeCloseTo(source.rawData[i], 7));
|
|
555
|
+
}
|
|
556
|
+
const identity = new Matrix3D();
|
|
557
|
+
const zeroAxis = identity.decompose('axisAngle');
|
|
558
|
+
expect(zeroAxis[1].w).toBeCloseTo(0);
|
|
559
|
+
expect(zeroAxis[1].z).toBeCloseTo(1);
|
|
560
|
+
const zeroAxisMatrix = new Matrix3D();
|
|
561
|
+
zeroAxisMatrix.recompose([zeroAxis[0], new Vector3D(0, 0, 0, Math.PI), zeroAxis[2]], 'axisAngle');
|
|
562
|
+
zeroAxisMatrix.rawData.forEach((value, i) => expect(value).toBeCloseTo(identity.rawData[i], 8));
|
|
563
|
+
const gimbal = new Matrix3D();
|
|
564
|
+
gimbal.recompose([new Vector3D(), new Vector3D(35, 90, -70), new Vector3D(1, 1, 1)]);
|
|
565
|
+
for (const style of ['quaternion', 'axisAngle']) {
|
|
566
|
+
const parts = gimbal.decompose(style), roundTrip = new Matrix3D();
|
|
567
|
+
expect(roundTrip.recompose(parts, style)).toBe(true);
|
|
568
|
+
roundTrip.rawData.forEach((value, i) => expect(value).toBeCloseTo(gimbal.rawData[i], 7));
|
|
569
|
+
}
|
|
570
|
+
const sheared = source.clone();
|
|
571
|
+
sheared.rawData[4] += 0.2;
|
|
572
|
+
expect(sheared.decompose()).toBeNull();
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
it('interpolates Matrix3D translation and rotation and normalizes scale', () => {
|
|
576
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
577
|
+
const { Matrix3D, Vector3D } = runtime.builtins;
|
|
578
|
+
const make = (translation, rotation, scale) => {
|
|
579
|
+
const matrix = new Matrix3D();
|
|
580
|
+
matrix.recompose([new Vector3D(...translation), new Vector3D(...rotation), new Vector3D(...scale)]);
|
|
581
|
+
return matrix;
|
|
582
|
+
};
|
|
583
|
+
const from = make([0, 2, 4], [0, 0, 0], [1, 2, 3]);
|
|
584
|
+
const to = make([10, 6, 8], [0, 0, 0], [3, 4, 5]);
|
|
585
|
+
const fromBefore = from.rawData.slice(), toBefore = to.rawData.slice();
|
|
586
|
+
const middle = Matrix3D.interpolate(from, to, 0.5);
|
|
587
|
+
expect(middle).not.toBe(from);
|
|
588
|
+
expect(middle.position).toMatchObject({ x: 5, y: 4, z: 6 });
|
|
589
|
+
const parts = middle.decompose();
|
|
590
|
+
expect(parts[1].z).toBeCloseTo(0);
|
|
591
|
+
expect(parts[2]).toMatchObject({ x: 1, y: 1, z: 1 });
|
|
592
|
+
expect(from.rawData).toEqual(fromBefore);
|
|
593
|
+
expect(to.rawData).toEqual(toBefore);
|
|
594
|
+
|
|
595
|
+
const receiver = from.clone();
|
|
596
|
+
expect(receiver.interpolateTo(to, 0.5)).toBeUndefined();
|
|
597
|
+
receiver.rawData.forEach((value, i) => expect(value).toBeCloseTo(middle.rawData[i], 9));
|
|
598
|
+
|
|
599
|
+
const halfTurn = Matrix3D.interpolate(
|
|
600
|
+
make([0, 0, 0], [0, 0, 170], [1, 1, 1]),
|
|
601
|
+
make([0, 0, 0], [0, 0, -170], [1, 1, 1]), 0.5);
|
|
602
|
+
const turned = halfTurn.transformVector(new Vector3D(1, 0, 0));
|
|
603
|
+
expect(turned.x).toBeCloseTo(-1);
|
|
604
|
+
expect(turned.y).toBeCloseTo(0);
|
|
605
|
+
|
|
606
|
+
const shortest = Matrix3D.interpolate(
|
|
607
|
+
make([0, 0, 0], [100, 0, 0], [1, 1, 1]),
|
|
608
|
+
make([0, 0, 0], [-100, 0, 0], [1, 1, 1]), 0.5);
|
|
609
|
+
expect(shortest.transformVector(Vector3D.Y_AXIS).y).toBeCloseTo(-1);
|
|
610
|
+
|
|
611
|
+
const mixedAxis = Matrix3D.interpolate(
|
|
612
|
+
make([0, 0, 0], [90, 0, 0], [1, 1, 1]),
|
|
613
|
+
make([0, 0, 0], [0, 90, 0], [1, 1, 1]), 0.5);
|
|
614
|
+
const projected = mixedAxis.transformVector(Vector3D.Z_AXIS);
|
|
615
|
+
expect(projected.x).toBeCloseTo(2 / 3);
|
|
616
|
+
expect(projected.y).toBeCloseTo(-2 / 3);
|
|
617
|
+
expect(projected.z).toBeCloseTo(1 / 3);
|
|
618
|
+
|
|
619
|
+
const scaledRotation = Matrix3D.interpolate(
|
|
620
|
+
new Matrix3D().appendScale(2, 2, 2),
|
|
621
|
+
new Matrix3D().appendScale(4, 4, 4).appendRotation(90, Vector3D.Z_AXIS), 0.5);
|
|
622
|
+
// Flash Player 32's trace rounds these values to six decimal places.
|
|
623
|
+
const scaledPoint = scaledRotation.transformVector(Vector3D.X_AXIS);
|
|
624
|
+
expect(scaledPoint.x).toBeCloseTo(0.529999, 6);
|
|
625
|
+
expect(scaledPoint.y).toBeCloseTo(0.847998, 6);
|
|
626
|
+
|
|
627
|
+
const sheared = from.clone();
|
|
628
|
+
sheared.rawData[4] += 0.25;
|
|
629
|
+
const unchanged = sheared.rawData.slice();
|
|
630
|
+
expect(sheared.interpolateTo(to, 0.5)).toBeUndefined();
|
|
631
|
+
expect(sheared.rawData).toEqual(unchanged);
|
|
632
|
+
expect(Matrix3D.interpolate(sheared, to, 0.5).rawData).toEqual(unchanged);
|
|
633
|
+
const singular = new Matrix3D().appendScale(1, 0, 1);
|
|
634
|
+
expect(Matrix3D.interpolate(from, singular, 0.5).rawData).toEqual(fromBefore);
|
|
635
|
+
expect(Matrix3D.interpolate(from, to, Infinity).rawData).toEqual(fromBefore);
|
|
636
|
+
expect(() => Matrix3D.interpolate(null, to, 0.5)).toThrow(/2007.*fromMat/);
|
|
637
|
+
expect(() => receiver.interpolateTo(null, 0.5)).toThrow(/2007.*toMat/);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
it('round-trips attached Transform matrices and preserves their basis when moved', () => {
|
|
641
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
642
|
+
const display = new runtime.builtins.DisplayObject();
|
|
643
|
+
display.x = 12; display.y = -4; display.scaleX = 2; display.scaleY = 3; display.rotation = 90;
|
|
644
|
+
const live = display.transform.matrix;
|
|
645
|
+
expect(live.tx).toBe(12);
|
|
646
|
+
expect(live.ty).toBe(-4);
|
|
647
|
+
expect(live.a).toBeCloseTo(0);
|
|
648
|
+
expect(live.b).toBeCloseTo(2);
|
|
649
|
+
expect(live.c).toBeCloseTo(-3);
|
|
650
|
+
expect(live.d).toBeCloseTo(0);
|
|
651
|
+
display.x = 19;
|
|
652
|
+
expect(display.transform.matrix.tx).toBe(19);
|
|
653
|
+
|
|
654
|
+
const explicit = new runtime.builtins.Matrix(2, 0.5, 1.5, 3, 70, 80);
|
|
655
|
+
display.transform.matrix = explicit;
|
|
656
|
+
expect(display.x).toBe(70);
|
|
657
|
+
expect(display.y).toBe(80);
|
|
658
|
+
display.x = 4;
|
|
659
|
+
const calls = [];
|
|
660
|
+
const ctx = {
|
|
661
|
+
canvas: { width: 100, height: 100 }, globalAlpha: 1,
|
|
662
|
+
save() {}, restore() {}, transform(...args) { calls.push(args); },
|
|
663
|
+
};
|
|
664
|
+
runtime.renderDisplayTree(ctx, display);
|
|
665
|
+
expect(calls).toEqual([[2, 0.5, 1.5, 3, 4, 80]]);
|
|
666
|
+
expect(display.transform.matrix).toBe(explicit);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it('retains skew through scale, reflection, and rotation of an attached transform', () => {
|
|
670
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
671
|
+
const { Matrix, Point, Sprite } = runtime.builtins;
|
|
672
|
+
const parent = new Sprite();
|
|
673
|
+
parent.x = 10;
|
|
674
|
+
const display = parent.addChild(new Sprite());
|
|
675
|
+
display.transform.matrix = new Matrix(2, 0, 3, 4, 5, 6);
|
|
676
|
+
expect(display.scaleY).toBe(5);
|
|
677
|
+
|
|
678
|
+
display.scaleX = -4;
|
|
679
|
+
const matrix = display.transform.matrix;
|
|
680
|
+
expect([matrix.a, matrix.c, matrix.d]).toEqual([-4, 3, 4]);
|
|
681
|
+
display.scaleY = 0;
|
|
682
|
+
expect(display.transform.matrix.d).toBe(0);
|
|
683
|
+
display.scaleY = 10;
|
|
684
|
+
expect(display.transform.matrix.c).toBeCloseTo(6);
|
|
685
|
+
expect(matrix.d).toBeCloseTo(8);
|
|
686
|
+
|
|
687
|
+
display.rotation = 90;
|
|
688
|
+
expect(display.transform.matrix.b).toBeCloseTo(-4);
|
|
689
|
+
expect(matrix.a).toBeCloseTo(0);
|
|
690
|
+
expect(matrix.c).toBeCloseTo(-8);
|
|
691
|
+
expect(matrix.d).toBeCloseTo(6);
|
|
692
|
+
matrix.tx = 11;
|
|
693
|
+
expect(display.x).toBe(11);
|
|
694
|
+
expect(display.scaleX).toBe(-4);
|
|
695
|
+
const world = display.localToGlobal(new Point(2, 1));
|
|
696
|
+
expect(world.x).toBeCloseTo(13);
|
|
697
|
+
expect(world.y).toBeCloseTo(4);
|
|
698
|
+
const local = display.globalToLocal(world);
|
|
699
|
+
expect(local.x).toBeCloseTo(2);
|
|
700
|
+
expect(local.y).toBeCloseTo(1);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
it('uses either a 2D matrix or an explicit Matrix3D on the same transform', () => {
|
|
704
|
+
const { builtins } = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
705
|
+
const { Matrix, Matrix3D, Point, Sprite } = builtins;
|
|
706
|
+
const display = new Sprite();
|
|
707
|
+
const affine = new Matrix(1, 0, 0.5, 1, 10, 20);
|
|
708
|
+
display.transform.matrix = affine;
|
|
709
|
+
expect(display.transform.matrix3D).toBeNull();
|
|
710
|
+
|
|
711
|
+
const projected = new Matrix3D();
|
|
712
|
+
display.transform.matrix3D = projected;
|
|
713
|
+
expect(display.transform.matrix).toBeNull();
|
|
714
|
+
expect(display.transform.matrix3D).toBe(projected);
|
|
715
|
+
|
|
716
|
+
display.transform.matrix = affine;
|
|
717
|
+
expect(display.transform.matrix3D).toBeNull();
|
|
718
|
+
expect(display.localToGlobal(new Point(2, 4))).toEqual(new Point(14, 24));
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it('combines a display matrix with its ancestors and the canvas viewport', () => {
|
|
722
|
+
const canvas = {
|
|
723
|
+
width: 100, height: 50,
|
|
724
|
+
style: {}, addEventListener() {},
|
|
725
|
+
getBoundingClientRect: () => ({ left: 3, top: 7, width: 50, height: 100 }),
|
|
726
|
+
};
|
|
727
|
+
const { builtins, stage } = new Function('document', `${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)({
|
|
728
|
+
getElementById: id => id === 'stage' ? canvas : null,
|
|
729
|
+
});
|
|
730
|
+
const { Matrix, Point, Sprite, StageQuality } = builtins;
|
|
731
|
+
stage.x = 100;
|
|
732
|
+
const parent = stage.addChild(new Sprite());
|
|
733
|
+
parent.scaleX = 2;
|
|
734
|
+
parent.y = 10;
|
|
735
|
+
const display = parent.addChild(new Sprite());
|
|
736
|
+
display.transform.matrix = new Matrix(2, 0, 1, 3, 4, 5);
|
|
737
|
+
const combined = display.transform.concatenatedMatrix;
|
|
738
|
+
expect(combined.transformPoint(new Point(1, 2))).toEqual(new Point(11, 49));
|
|
739
|
+
combined.tx = 999;
|
|
740
|
+
expect(display.x).toBe(4);
|
|
741
|
+
|
|
742
|
+
stage.removeChild(parent);
|
|
743
|
+
stage.quality = StageQuality.MEDIUM;
|
|
744
|
+
const detached = display.transform.concatenatedMatrix;
|
|
745
|
+
expect([detached.a, detached.b, detached.c, detached.d, detached.tx, detached.ty]).toEqual([20, 0, 1, 30, 4, 5]);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('reports Transform.pixelBounds in stage coordinates for a display subtree', () => {
|
|
749
|
+
const { builtins, stage } = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
750
|
+
const { Rectangle, Shape, Sprite } = builtins;
|
|
751
|
+
const root = stage.addChild(new Sprite());
|
|
752
|
+
root.x = 10;
|
|
753
|
+
root.y = -3;
|
|
754
|
+
root.scaleX = 2;
|
|
755
|
+
const shape = root.addChild(new Shape());
|
|
756
|
+
shape.x = 5;
|
|
757
|
+
shape.y = 2;
|
|
758
|
+
shape.graphics.beginFill(0);
|
|
759
|
+
shape.graphics.drawRect(-2, 1, 3, 4);
|
|
760
|
+
const other = root.addChild(new Shape());
|
|
761
|
+
other.x = -4;
|
|
762
|
+
other.y = 10;
|
|
763
|
+
other.graphics.beginFill(0);
|
|
764
|
+
other.graphics.drawRect(0, 0, 2, 2);
|
|
765
|
+
|
|
766
|
+
expect(shape.transform.pixelBounds).toEqual(new Rectangle(16, 0, 6, 4));
|
|
767
|
+
expect(root.transform.pixelBounds).toEqual(new Rectangle(2, 0, 20, 9));
|
|
768
|
+
root.removeChild(other);
|
|
769
|
+
expect(root.transform.pixelBounds).toEqual(shape.transform.pixelBounds);
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
it('changes RGB offsets through ColorTransform.color without changing alpha', () => {
|
|
773
|
+
const { builtins } = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
774
|
+
const { ColorTransform, Sprite } = builtins;
|
|
775
|
+
const display = new Sprite();
|
|
776
|
+
display.transform.colorTransform = new ColorTransform(1, 1, 1, 0.4, 1, 2, 3, 25);
|
|
777
|
+
const tint = display.transform.colorTransform;
|
|
778
|
+
tint.color = 0xaa123456;
|
|
779
|
+
display.transform.colorTransform = tint;
|
|
780
|
+
expect(tint.color).toBe(0x123456);
|
|
781
|
+
expect([tint.redOffset, tint.greenOffset, tint.blueOffset]).toEqual([0x12, 0x34, 0x56]);
|
|
782
|
+
expect([tint.redMultiplier, tint.greenMultiplier, tint.blueMultiplier]).toEqual([0, 0, 0]);
|
|
783
|
+
expect([tint.alphaMultiplier, tint.alphaOffset]).toEqual([0.4, 25]);
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
it('shares local alpha with colorTransform and composes color from child to parent', () => {
|
|
787
|
+
const { builtins } = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
788
|
+
const { ColorTransform, Sprite } = builtins;
|
|
789
|
+
const root = new Sprite();
|
|
790
|
+
const parentColor = new ColorTransform(0.5, 1, 0.75, 0.75, 10, -5, 20, 8);
|
|
791
|
+
root.transform.colorTransform = parentColor;
|
|
792
|
+
const child = root.addChild(new Sprite());
|
|
793
|
+
child.transform.colorTransform = new ColorTransform(0.25, 0.5, 1, 0.5, 40, 20, 4, 16);
|
|
794
|
+
expect(child.alpha).toBe(0.5);
|
|
795
|
+
root.alpha = 0.5;
|
|
796
|
+
expect(root.transform.colorTransform.alphaMultiplier).toBe(0.5);
|
|
797
|
+
expect(parentColor.alphaMultiplier).toBe(0.75);
|
|
798
|
+
|
|
799
|
+
const combined = child.transform.concatenatedColorTransform;
|
|
800
|
+
expect(combined).toEqual(new ColorTransform(0.125, 0.5, 0.75, 0.25, 30, 15, 23, 16));
|
|
801
|
+
combined.alphaMultiplier = 0;
|
|
802
|
+
expect(child.alpha).toBe(0.5);
|
|
803
|
+
root.removeChild(child);
|
|
804
|
+
expect(child.transform.concatenatedColorTransform).toEqual(child.transform.colorTransform);
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
it('updates the shared AVM1 clip tint through legacy Color objects', () => {
|
|
808
|
+
const { builtins } = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
809
|
+
const target = {};
|
|
810
|
+
const writer = new builtins.As2Color(target);
|
|
811
|
+
const reader = new builtins.As2Color(target);
|
|
812
|
+
writer.setTransform({ ra: 50, rb: 11, aa: 75, ab: 9 });
|
|
813
|
+
expect(reader.getTransform()).toEqual({ ra: 50, rb: 11, ga: 100, gb: 0, ba: 100, bb: 0, aa: 75, ab: 9 });
|
|
814
|
+
writer.setRGB(0x0066ff);
|
|
815
|
+
expect(reader.getRGB()).toBe(0x0066ff);
|
|
816
|
+
expect(reader.getTransform()).toEqual({ ra: 0, rb: 0, ga: 0, gb: 102, ba: 0, bb: 255, aa: 75, ab: 9 });
|
|
817
|
+
expect(target._swoofAvm1ColorTransform).toBe(target.colorTransform);
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
it('projects Matrix3D transforms through the active perspective projection', () => {
|
|
821
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
822
|
+
const { builtins, stage } = runtime;
|
|
823
|
+
stage.stageWidth = 800;
|
|
824
|
+
stage.stageHeight = 400;
|
|
825
|
+
const projection = new builtins.PerspectiveProjection();
|
|
826
|
+
projection.fieldOfView = 90;
|
|
827
|
+
projection.projectionCenter = new builtins.Point(100, 100);
|
|
828
|
+
const display = new builtins.DisplayObject();
|
|
829
|
+
display.transform.matrix3D = new builtins.Matrix3D([
|
|
830
|
+
1, 0, 0, 0,
|
|
831
|
+
0, 1, 0, 0,
|
|
832
|
+
0, 0, 1, 0,
|
|
833
|
+
20, 30, 100, 1,
|
|
834
|
+
]);
|
|
835
|
+
display.transform.perspectiveProjection = projection;
|
|
836
|
+
const calls = [];
|
|
837
|
+
const ctx = {
|
|
838
|
+
canvas: { width: 800, height: 400 }, globalAlpha: 1,
|
|
839
|
+
save() {}, restore() {}, transform(...args) { calls.push(args); },
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
runtime.renderDisplayTree(ctx, display);
|
|
843
|
+
expect(calls).toHaveLength(1);
|
|
844
|
+
calls[0].forEach((value, index) => expect(value).toBeCloseTo([0.8, 0, 0, 0.8, 36, 44][index]));
|
|
845
|
+
expect(runtime.packages.display.PerspectiveProjection).toBe(builtins.PerspectiveProjection);
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
it('warps perspective-varying Matrix3D objects across a padded canvas mesh', () => {
|
|
849
|
+
const previousOffscreenCanvas = globalThis.OffscreenCanvas;
|
|
850
|
+
const created = [];
|
|
851
|
+
class FakeOffscreenCanvas {
|
|
852
|
+
constructor(width, height) {
|
|
853
|
+
this.width = width; this.height = height; this.calls = []; created.push(this);
|
|
854
|
+
this.ctx = {
|
|
855
|
+
canvas: this, globalAlpha: 1,
|
|
856
|
+
save() {}, restore() {}, translate() {}, rotate() {}, scale() {},
|
|
857
|
+
transform: (...args) => this.calls.push(args),
|
|
858
|
+
beginPath() {}, moveTo() {}, lineTo() {}, closePath() {}, clip() {}, drawImage() {},
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
getContext() { return this.ctx; }
|
|
862
|
+
}
|
|
863
|
+
globalThis.OffscreenCanvas = FakeOffscreenCanvas;
|
|
864
|
+
try {
|
|
865
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
866
|
+
runtime.stage.stageWidth = 800;
|
|
867
|
+
runtime.stage.stageHeight = 400;
|
|
868
|
+
const projection = new runtime.builtins.PerspectiveProjection();
|
|
869
|
+
projection.fieldOfView = 90;
|
|
870
|
+
projection.projectionCenter = new runtime.builtins.Point(400, 200);
|
|
871
|
+
const display = new runtime.builtins.DisplayObject();
|
|
872
|
+
display.transform.matrix3D = new runtime.builtins.Matrix3D([
|
|
873
|
+
1, 0, 0, 0.0001,
|
|
874
|
+
0, 1, 0, 0,
|
|
875
|
+
0, 0, 1, 0,
|
|
876
|
+
0, 0, 0, 1,
|
|
877
|
+
]);
|
|
878
|
+
display.transform.perspectiveProjection = projection;
|
|
879
|
+
const target = {
|
|
880
|
+
canvas: { width: 800, height: 400 }, globalAlpha: 1, transforms: 0,
|
|
881
|
+
save() {}, restore() {}, transform() { this.transforms++; }, drawImage() {},
|
|
882
|
+
beginPath() {}, moveTo() {}, lineTo() {}, closePath() {}, clip() {},
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
runtime.renderDisplayTree(target, display);
|
|
886
|
+
const mildPerspectiveTriangles = target.transforms;
|
|
887
|
+
display.transform.matrix3D = new runtime.builtins.Matrix3D([
|
|
888
|
+
1, 0, 0, 0.001,
|
|
889
|
+
0, 1, 0, 0,
|
|
890
|
+
0, 0, 1, 0,
|
|
891
|
+
0, 0, 0, 1,
|
|
892
|
+
]);
|
|
893
|
+
target.transforms = 0;
|
|
894
|
+
runtime.renderDisplayTree(target, display);
|
|
895
|
+
const firstPassTriangles = target.transforms;
|
|
896
|
+
runtime.renderDisplayTree(target, display);
|
|
897
|
+
|
|
898
|
+
expect(created).toHaveLength(1);
|
|
899
|
+
expect(created[0].width).toBe(808);
|
|
900
|
+
expect(created[0].height).toBe(408);
|
|
901
|
+
expect(mildPerspectiveTriangles).toBeGreaterThan(2);
|
|
902
|
+
expect(firstPassTriangles).toBeGreaterThan(mildPerspectiveTriangles);
|
|
903
|
+
expect(firstPassTriangles).toBeLessThanOrEqual(4096);
|
|
904
|
+
expect(target.transforms).toBe(firstPassTriangles * 2);
|
|
905
|
+
} finally {
|
|
906
|
+
if (previousOffscreenCanvas === undefined) delete globalThis.OffscreenCanvas;
|
|
907
|
+
else globalThis.OffscreenCanvas = previousOffscreenCanvas;
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
it('clips Matrix3D mesh triangles crossing the perspective near plane', () => {
|
|
912
|
+
const previousOffscreenCanvas = globalThis.OffscreenCanvas;
|
|
913
|
+
class FakeOffscreenCanvas {
|
|
914
|
+
constructor(width, height) {
|
|
915
|
+
this.width = width; this.height = height;
|
|
916
|
+
this.ctx = {
|
|
917
|
+
canvas: this, globalAlpha: 1,
|
|
918
|
+
save() {}, restore() {}, translate() {}, rotate() {}, scale() {}, transform() {},
|
|
919
|
+
beginPath() {}, moveTo() {}, lineTo() {}, closePath() {}, clip() {}, drawImage() {},
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
getContext() { return this.ctx; }
|
|
923
|
+
}
|
|
924
|
+
globalThis.OffscreenCanvas = FakeOffscreenCanvas;
|
|
925
|
+
try {
|
|
926
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
927
|
+
runtime.stage.stageWidth = 800;
|
|
928
|
+
runtime.stage.stageHeight = 400;
|
|
929
|
+
const projection = new runtime.builtins.PerspectiveProjection();
|
|
930
|
+
projection.fieldOfView = 90;
|
|
931
|
+
projection.projectionCenter = new runtime.builtins.Point(0, 0);
|
|
932
|
+
const display = new runtime.builtins.DisplayObject();
|
|
933
|
+
display.transform.matrix3D = new runtime.builtins.Matrix3D([
|
|
934
|
+
1, 0, -1, 0,
|
|
935
|
+
0, 1, 0, 0,
|
|
936
|
+
0, 0, 1, 0,
|
|
937
|
+
0, 0, 0, 1,
|
|
938
|
+
]);
|
|
939
|
+
display.transform.perspectiveProjection = projection;
|
|
940
|
+
const clippedTriangles = [];
|
|
941
|
+
let path = [];
|
|
942
|
+
const target = {
|
|
943
|
+
canvas: { width: 800, height: 400 }, globalAlpha: 1,
|
|
944
|
+
save() {}, restore() {}, transform() {}, drawImage() {},
|
|
945
|
+
beginPath() { path = []; }, moveTo(x, y) { path.push([x, y]); },
|
|
946
|
+
lineTo(x, y) { path.push([x, y]); }, closePath() {},
|
|
947
|
+
clip() { clippedTriangles.push(path); },
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
runtime.renderDisplayTree(target, display);
|
|
951
|
+
|
|
952
|
+
// Crossing triangles are clipped then refined by projected midpoint
|
|
953
|
+
// error, rather than dropped as a whole at the camera boundary.
|
|
954
|
+
expect(clippedTriangles.length).toBeGreaterThan(36);
|
|
955
|
+
expect(clippedTriangles.length).toBeLessThanOrEqual(4096);
|
|
956
|
+
expect(clippedTriangles.flat().every(point => point.every(Number.isFinite))).toBe(true);
|
|
957
|
+
expect(clippedTriangles.flat().every(point => Math.abs(point[0]) < 1e6 && Math.abs(point[1]) < 1e6)).toBe(true);
|
|
958
|
+
} finally {
|
|
959
|
+
if (previousOffscreenCanvas === undefined) delete globalThis.OffscreenCanvas;
|
|
960
|
+
else globalThis.OffscreenCanvas = previousOffscreenCanvas;
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
it('uses clipped Matrix3D projection for display bounds and hit tests', () => {
|
|
965
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
966
|
+
runtime.stage.stageWidth = 800;
|
|
967
|
+
runtime.stage.stageHeight = 400;
|
|
968
|
+
const projection = new runtime.builtins.PerspectiveProjection();
|
|
969
|
+
projection.fieldOfView = 90;
|
|
970
|
+
projection.projectionCenter = new runtime.builtins.Point(0, 0);
|
|
971
|
+
const display = new runtime.builtins.DisplayObject();
|
|
972
|
+
display.width = 100;
|
|
973
|
+
display.height = 40;
|
|
974
|
+
display.transform.matrix3D = new runtime.builtins.Matrix3D([
|
|
975
|
+
1, 0, 0, -0.001,
|
|
976
|
+
0, 1, 0, 0,
|
|
977
|
+
0, 0, 1, 0,
|
|
978
|
+
0, 0, 0, 1,
|
|
979
|
+
]);
|
|
980
|
+
display.transform.perspectiveProjection = projection;
|
|
981
|
+
|
|
982
|
+
const bounds = display.getBounds();
|
|
983
|
+
expect(bounds.x).toBeCloseTo(0);
|
|
984
|
+
expect(bounds.y).toBeCloseTo(0);
|
|
985
|
+
expect(bounds.width).toBeGreaterThan(100);
|
|
986
|
+
expect(bounds.height).toBeGreaterThan(40);
|
|
987
|
+
expect(display.hitTestPoint(50, 20)).toBe(true);
|
|
988
|
+
expect(display.hitTestPoint(150, 20)).toBe(false);
|
|
989
|
+
const globalPoint = display.localToGlobal(new runtime.builtins.Point(40, 20));
|
|
990
|
+
const localPoint = display.globalToLocal(globalPoint);
|
|
991
|
+
expect(localPoint.x).toBeCloseTo(40);
|
|
992
|
+
expect(localPoint.y).toBeCloseTo(20);
|
|
993
|
+
const overlap = new runtime.builtins.DisplayObject();
|
|
994
|
+
overlap.x = 105;
|
|
995
|
+
overlap.width = 10;
|
|
996
|
+
overlap.height = 10;
|
|
997
|
+
expect(display.hitTestObject(overlap)).toBe(true);
|
|
998
|
+
|
|
999
|
+
display.transform.matrix3D = new runtime.builtins.Matrix3D([
|
|
1000
|
+
1, 0, 0, 0,
|
|
1001
|
+
0, 1, 0, 0,
|
|
1002
|
+
0, 0, 1, 0,
|
|
1003
|
+
0, 0, -500, 1,
|
|
1004
|
+
]);
|
|
1005
|
+
const clipped = display.getBounds();
|
|
1006
|
+
expect(clipped.width).toBe(0);
|
|
1007
|
+
expect(clipped.height).toBe(0);
|
|
1008
|
+
expect(display.hitTestPoint(1, 1)).toBe(false);
|
|
1009
|
+
expect(display.hitTestObject(overlap)).toBe(false);
|
|
1010
|
+
expect(Number.isNaN(display.localToGlobal(new runtime.builtins.Point(1, 1)).x)).toBe(true);
|
|
1011
|
+
expect(Number.isNaN(display.globalToLocal(new runtime.builtins.Point(1, 1)).x)).toBe(true);
|
|
1012
|
+
display.visible = false;
|
|
1013
|
+
expect(display.hitTestPoint(10, 10)).toBe(false);
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
it('shares the nearest ancestor camera across Matrix3D rendering, hit testing, and coordinates', () => {
|
|
1017
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1018
|
+
const { stage, builtins: { Sprite, Matrix3D, Point, PerspectiveProjection, Vector3D } } = runtime;
|
|
1019
|
+
stage.stageWidth = 800;
|
|
1020
|
+
stage.stageHeight = 400;
|
|
1021
|
+
const outer = new Sprite(), middle = new Sprite(), shape = new Sprite();
|
|
1022
|
+
stage.addChild(outer);
|
|
1023
|
+
outer.addChild(middle);
|
|
1024
|
+
middle.addChild(shape);
|
|
1025
|
+
middle.x = 30;
|
|
1026
|
+
middle.y = 10;
|
|
1027
|
+
shape.width = 100;
|
|
1028
|
+
shape.height = 100;
|
|
1029
|
+
shape.transform.matrix3D = new Matrix3D().appendTranslation(0, 0, 100);
|
|
1030
|
+
|
|
1031
|
+
const defaults = stage.transform.perspectiveProjection;
|
|
1032
|
+
expect(defaults).toBeInstanceOf(PerspectiveProjection);
|
|
1033
|
+
expect(defaults.projectionCenter).toMatchObject({ x: 400, y: 200 });
|
|
1034
|
+
expect(shape.localToGlobal(new Point(0, 0)).x).toBeGreaterThan(30);
|
|
1035
|
+
stage.stageWidth = 1000;
|
|
1036
|
+
expect(stage.transform.perspectiveProjection).toBe(defaults);
|
|
1037
|
+
expect(defaults.projectionCenter.x).toBe(500);
|
|
1038
|
+
|
|
1039
|
+
const camera = new PerspectiveProjection();
|
|
1040
|
+
camera.focalLength = 400;
|
|
1041
|
+
camera.projectionCenter = new Point(100, 50);
|
|
1042
|
+
outer.transform.perspectiveProjection = camera;
|
|
1043
|
+
const expected = shape.localToGlobal(new Point(10, 20));
|
|
1044
|
+
expect([expected.x, expected.y]).toEqual([52, 34]);
|
|
1045
|
+
const relative = shape.globalToLocal(expected);
|
|
1046
|
+
expect(relative.x).toBeCloseTo(10);
|
|
1047
|
+
expect(relative.y).toBeCloseTo(20);
|
|
1048
|
+
expect(shape.hitTestPoint(expected.x, expected.y)).toBe(true);
|
|
1049
|
+
expect(shape.hitTestPoint(25, 10)).toBe(false);
|
|
1050
|
+
expect(shape.getBounds()).toMatchObject({ x: 14, y: 8, width: 80, height: 80 });
|
|
1051
|
+
expect(shape.globalToLocal3D(expected)).toMatchObject({ x: 10, y: 20, z: 0 });
|
|
1052
|
+
expect(shape.local3DToGlobal(new Vector3D(10, 20, 0))).toEqual(expected);
|
|
1053
|
+
const deeper = shape.local3DToGlobal(new Vector3D(10, 20, 100));
|
|
1054
|
+
expect(deeper).toMatchObject({ x: 60 });
|
|
1055
|
+
expect(deeper.y).toBeCloseTo(110 / 3);
|
|
1056
|
+
const inheritedDepth = middle.local3DToGlobal(new Vector3D(10, 20, 200));
|
|
1057
|
+
expect(inheritedDepth.x).toBeCloseTo(deeper.x);
|
|
1058
|
+
expect(inheritedDepth.y).toBeCloseTo(deeper.y);
|
|
1059
|
+
|
|
1060
|
+
const projectionMatrix = new Matrix3D();
|
|
1061
|
+
expect(camera.toMatrix3DToOutput(projectionMatrix)).toBe(projectionMatrix);
|
|
1062
|
+
expect(camera.toMatrix3D().rawData).toEqual(projectionMatrix.rawData);
|
|
1063
|
+
const point = projectionMatrix.transformVector(new Vector3D(10 + middle.x, 20 + middle.y, 100));
|
|
1064
|
+
expect([point.x / point.w, point.y / point.w]).toEqual([expected.x, expected.y]);
|
|
1065
|
+
|
|
1066
|
+
const transforms = [], paints = [];
|
|
1067
|
+
shape.graphics._paint = () => paints.push('shape');
|
|
1068
|
+
const ctx = { globalAlpha: 1, canvas: { width: 800, height: 400 }, save() {}, restore() {},
|
|
1069
|
+
translate() {}, rotate() {}, scale() {}, transform(...args) { transforms.push(args); } };
|
|
1070
|
+
runtime.renderDisplayTree(ctx, outer);
|
|
1071
|
+
expect(transforms).toContainEqual([0.8, 0, 0, 0.8, 14, 8]);
|
|
1072
|
+
expect(paints).toEqual(['shape']);
|
|
1073
|
+
shape.transform.matrix3D = new Matrix3D().appendTranslation(0, 0, -500);
|
|
1074
|
+
runtime.renderDisplayTree(ctx, outer);
|
|
1075
|
+
expect(paints).toEqual(['shape']);
|
|
1076
|
+
expect(shape.hitTestPoint(expected.x, expected.y)).toBe(false);
|
|
1077
|
+
expect(shape.globalToLocal3D(expected).x).toBeNaN();
|
|
1078
|
+
});
|
|
1079
|
+
|
|
1080
|
+
it('round-trips local/global points through explicit skew matrices and parents', () => {
|
|
1081
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1082
|
+
const parent = new runtime.builtins.DisplayObjectContainer();
|
|
1083
|
+
const child = new runtime.builtins.DisplayObject();
|
|
1084
|
+
parent.transform.matrix = new runtime.builtins.Matrix(1.5, 0.25, -0.4, 2, 30, -12);
|
|
1085
|
+
child.transform.matrix = new runtime.builtins.Matrix(2, 1, 0.5, 3, 10, -4);
|
|
1086
|
+
parent.addChild(child);
|
|
1087
|
+
|
|
1088
|
+
const local = new runtime.builtins.Point(7, 9);
|
|
1089
|
+
const global = child.localToGlobal(local);
|
|
1090
|
+
const roundTrip = child.globalToLocal(global);
|
|
1091
|
+
|
|
1092
|
+
expect(global.x).toBeCloseTo(60.75);
|
|
1093
|
+
expect(global.y).toBeCloseTo(55.125);
|
|
1094
|
+
expect(roundTrip.x).toBeCloseTo(local.x);
|
|
1095
|
+
expect(roundTrip.y).toBeCloseTo(local.y);
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
it('copies Transform.colorTransform values and paints changes after assignment', () => {
|
|
1099
|
+
const previousOffscreenCanvas = globalThis.OffscreenCanvas;
|
|
1100
|
+
let paintedPixels = null;
|
|
1101
|
+
function makeContext(canvas) {
|
|
1102
|
+
let pixels = new Uint8ClampedArray([10, 20, 30, 255]);
|
|
1103
|
+
return {
|
|
1104
|
+
canvas, globalAlpha: 1, filter: 'none', save() {}, restore() {},
|
|
1105
|
+
translate() {}, rotate() {}, scale() {}, transform() {}, clearRect() {}, drawImage() {},
|
|
1106
|
+
getImageData() { return { data: pixels }; },
|
|
1107
|
+
putImageData(image) { pixels = image.data; paintedPixels = Array.from(pixels); },
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
globalThis.OffscreenCanvas = class {
|
|
1111
|
+
constructor(width, height) { this.width = width; this.height = height; this._ctx = makeContext(this); }
|
|
1112
|
+
getContext() { return this._ctx; }
|
|
1113
|
+
};
|
|
1114
|
+
try {
|
|
1115
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1116
|
+
const display = new runtime.builtins.DisplayObject();
|
|
1117
|
+
const color = new runtime.builtins.ColorTransform();
|
|
1118
|
+
display.transform.colorTransform = color;
|
|
1119
|
+
expect(display.colorTransform).toEqual(color);
|
|
1120
|
+
color.redOffset = 7;
|
|
1121
|
+
expect(display.colorTransform.redOffset).toBe(0);
|
|
1122
|
+
display.transform.colorTransform = color;
|
|
1123
|
+
expect(() => { display.transform.colorTransform = null; }).toThrow(/2007/);
|
|
1124
|
+
const ctx = makeContext({ width: 32, height: 32 });
|
|
1125
|
+
runtime.renderDisplayTree(ctx, display);
|
|
1126
|
+
expect(paintedPixels[0]).toBe(17);
|
|
1127
|
+
} finally {
|
|
1128
|
+
if (previousOffscreenCanvas === undefined) delete globalThis.OffscreenCanvas;
|
|
1129
|
+
else globalThis.OffscreenCanvas = previousOffscreenCanvas;
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
it('applies ordered pixel blur, glow and drop shadow on display objects', () => {
|
|
1134
|
+
const previousOffscreenCanvas = globalThis.OffscreenCanvas;
|
|
1135
|
+
const applied = [];
|
|
1136
|
+
function makeContext(canvas) {
|
|
1137
|
+
let activeFilter = 'none';
|
|
1138
|
+
const stack = [];
|
|
1139
|
+
return {
|
|
1140
|
+
canvas, globalAlpha: 1, globalCompositeOperation: 'source-over',
|
|
1141
|
+
get filter() { return activeFilter; }, set filter(value) { activeFilter = value; },
|
|
1142
|
+
save() { stack.push(activeFilter); }, restore() { activeFilter = stack.pop() || 'none'; },
|
|
1143
|
+
translate() {}, rotate() {}, scale() {}, transform() {}, clearRect() {},
|
|
1144
|
+
drawImage() {},
|
|
1145
|
+
getImageData() {
|
|
1146
|
+
const data = new Uint8ClampedArray(canvas.width * canvas.height * 4);
|
|
1147
|
+
data[3] = 255;
|
|
1148
|
+
return { data };
|
|
1149
|
+
},
|
|
1150
|
+
putImageData({ data }) {
|
|
1151
|
+
const filtered = data.findIndex((value, index) => index >= 4 && index % 4 === 3 && value > 0);
|
|
1152
|
+
applied.push(filtered > 0 ? [...data.slice(filtered - 3, filtered + 1)] : null);
|
|
1153
|
+
},
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
globalThis.OffscreenCanvas = class {
|
|
1157
|
+
constructor(width, height) { this.width = width; this.height = height; this._ctx = makeContext(this); }
|
|
1158
|
+
getContext() { return this._ctx; }
|
|
1159
|
+
};
|
|
1160
|
+
try {
|
|
1161
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1162
|
+
const { builtins, renderDisplayTree } = runtime;
|
|
1163
|
+
const display = new builtins.DisplayObject();
|
|
1164
|
+
display.filters = [
|
|
1165
|
+
new builtins.BlurFilter(8, 4, 2),
|
|
1166
|
+
new builtins.GlowFilter(0x33aaee, 0.5, 6, 8, 2),
|
|
1167
|
+
new builtins.DropShadowFilter(10, 90, 0x112233, 0.75, 4, 8, 1.5),
|
|
1168
|
+
];
|
|
1169
|
+
const ctx = makeContext({ width: 64, height: 64 });
|
|
1170
|
+
renderDisplayTree(ctx, display);
|
|
1171
|
+
expect(applied).toHaveLength(3);
|
|
1172
|
+
expect(applied[0].slice(0, 3)).toEqual([0, 0, 0]);
|
|
1173
|
+
expect(applied[1].slice(0, 3)).toEqual([51, 170, 238]);
|
|
1174
|
+
expect(applied[2].slice(0, 3)).toEqual([17, 34, 51]);
|
|
1175
|
+
expect(display.filters[2].clone()).toMatchObject({ distance: 10, angle: 90, color: 0x112233, strength: 1.5 });
|
|
1176
|
+
expect(runtime.packages.filters.GlowFilter).toBe(builtins.GlowFilter);
|
|
1177
|
+
} finally {
|
|
1178
|
+
if (previousOffscreenCanvas === undefined) delete globalThis.OffscreenCanvas;
|
|
1179
|
+
else globalThis.OffscreenCanvas = previousOffscreenCanvas;
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
it('renders SimpleButton up, over, and down states through browser input', () => {
|
|
1184
|
+
const previousPath2D = globalThis.Path2D;
|
|
1185
|
+
const previousDocument = globalThis.document;
|
|
1186
|
+
const listeners = Object.create(null);
|
|
1187
|
+
const stateSize = 20;
|
|
1188
|
+
const canvas = {
|
|
1189
|
+
width: 100, height: 100, style: {},
|
|
1190
|
+
addEventListener(type, listener) { listeners[type] = listener; },
|
|
1191
|
+
getBoundingClientRect() { return { left: 0, top: 0, width: 100, height: 100 }; },
|
|
1192
|
+
};
|
|
1193
|
+
globalThis.document = {
|
|
1194
|
+
readyState: 'complete',
|
|
1195
|
+
getElementById(id) { return id === 'stage' ? canvas : null; },
|
|
1196
|
+
// The fixture uses a single filled square for all four states.
|
|
1197
|
+
createElement() {
|
|
1198
|
+
return { getContext: () => ({ isPointInPath: (_path, x, y) => x >= 0 && y >= 0 && x < stateSize && y < stateSize }) };
|
|
1199
|
+
},
|
|
1200
|
+
};
|
|
1201
|
+
globalThis.Path2D = class {
|
|
1202
|
+
moveTo() {} lineTo() {} quadraticCurveTo() {} bezierCurveTo() {} arc() {} closePath() {}
|
|
1203
|
+
};
|
|
1204
|
+
try {
|
|
1205
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1206
|
+
const { builtins, stage, renderDisplayTree } = runtime;
|
|
1207
|
+
const button = new builtins.SimpleButton();
|
|
1208
|
+
const makeState = (color) => {
|
|
1209
|
+
const shape = new builtins.Shape();
|
|
1210
|
+
shape.graphics.beginFill(color);
|
|
1211
|
+
shape.graphics.drawRect(0, 0, stateSize, stateSize);
|
|
1212
|
+
shape.graphics.endFill();
|
|
1213
|
+
return shape;
|
|
1214
|
+
};
|
|
1215
|
+
button.upState = makeState(0xff0000);
|
|
1216
|
+
button.overState = makeState(0x00ff00);
|
|
1217
|
+
button.downState = makeState(0x0000ff);
|
|
1218
|
+
button.hitTestState = button.upState;
|
|
1219
|
+
stage.addChild(button);
|
|
1220
|
+
const painted = [];
|
|
1221
|
+
const ctx = {
|
|
1222
|
+
canvas, globalAlpha: 1,
|
|
1223
|
+
save() {}, restore() {}, translate() {}, rotate() {}, scale() {}, transform() {},
|
|
1224
|
+
fill(path) { painted.push(this.fillStyle); }, stroke() {},
|
|
1225
|
+
};
|
|
1226
|
+
const paint = (type, x, y, buttons) => {
|
|
1227
|
+
painted.length = 0;
|
|
1228
|
+
listeners[type]({ clientX: x, clientY: y, button: 0, buttons });
|
|
1229
|
+
renderDisplayTree(ctx, stage);
|
|
1230
|
+
return painted[0];
|
|
1231
|
+
};
|
|
1232
|
+
expect(paint('mousemove', 50, 50, 0)).toBe('#ff0000');
|
|
1233
|
+
expect(paint('mousemove', 5, 5, 0)).toBe('#00ff00');
|
|
1234
|
+
expect(paint('mousedown', 5, 5, 1)).toBe('#0000ff');
|
|
1235
|
+
expect(paint('mousemove', 50, 50, 1)).toBe('#ff0000');
|
|
1236
|
+
expect(paint('mousemove', 5, 5, 1)).toBe('#0000ff');
|
|
1237
|
+
expect(paint('mouseup', 5, 5, 0)).toBe('#00ff00');
|
|
1238
|
+
} finally {
|
|
1239
|
+
if (previousPath2D === undefined) delete globalThis.Path2D;
|
|
1240
|
+
else globalThis.Path2D = previousPath2D;
|
|
1241
|
+
if (previousDocument === undefined) delete globalThis.document;
|
|
1242
|
+
else globalThis.document = previousDocument;
|
|
1243
|
+
}
|
|
1244
|
+
});
|
|
1245
|
+
|
|
1246
|
+
it('sends URLVariables and request headers through same-origin-capable URLLoader', () => {
|
|
1247
|
+
const previousXhr = globalThis.XMLHttpRequest;
|
|
1248
|
+
let sent = null;
|
|
1249
|
+
class FakeXHR {
|
|
1250
|
+
constructor() { this.headers = {}; this.status = 200; this.responseText = 'accepted'; }
|
|
1251
|
+
open(method, url) { this.method = method; this.url = url; }
|
|
1252
|
+
setRequestHeader(name, value) { this.headers[name] = value; }
|
|
1253
|
+
send(body) { sent = this; this.body = body; this.onload(); }
|
|
1254
|
+
}
|
|
1255
|
+
globalThis.XMLHttpRequest = FakeXHR;
|
|
1256
|
+
try {
|
|
1257
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1258
|
+
const { URLRequest, URLVariables, URLLoader } = runtime.builtins;
|
|
1259
|
+
const request = new URLRequest('/score');
|
|
1260
|
+
request.method = 'POST';
|
|
1261
|
+
request.data = new URLVariables();
|
|
1262
|
+
request.data.name = 'A Flash Game';
|
|
1263
|
+
request.requestHeaders = [{ name: 'X-Game', value: 'swoof' }];
|
|
1264
|
+
const loader = new URLLoader();
|
|
1265
|
+
loader.load(request);
|
|
1266
|
+
expect(sent.method).toBe('POST');
|
|
1267
|
+
expect(sent.url).toBe('/score');
|
|
1268
|
+
expect(sent.body).toBe('name=A%20Flash%20Game');
|
|
1269
|
+
expect(sent.headers['X-Game']).toBe('swoof');
|
|
1270
|
+
expect(sent.headers['Content-Type']).toBe('application/x-www-form-urlencoded; charset=UTF-8');
|
|
1271
|
+
expect(loader.data).toBe('accepted');
|
|
1272
|
+
} finally {
|
|
1273
|
+
if (previousXhr === undefined) delete globalThis.XMLHttpRequest;
|
|
1274
|
+
else globalThis.XMLHttpRequest = previousXhr;
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1278
|
+
it('records allowDomain grants for runtime domain checks', () => {
|
|
1279
|
+
const previousLocation = globalThis.location;
|
|
1280
|
+
globalThis.location = { hostname: 'game.example.com', host: 'game.example.com' };
|
|
1281
|
+
try {
|
|
1282
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1283
|
+
const security = runtime.builtins.Security;
|
|
1284
|
+
security.allowDomain('*.partner.test');
|
|
1285
|
+
security.allowInsecureDomain('legacy.test');
|
|
1286
|
+
expect(security.domain).toBe('game.example.com');
|
|
1287
|
+
expect(security.isDomainAllowed('cdn.partner.test')).toBe(true);
|
|
1288
|
+
expect(security.isDomainAllowed('legacy.test')).toBe(false);
|
|
1289
|
+
expect(security.isDomainAllowed('legacy.test', true)).toBe(true);
|
|
1290
|
+
expect(security.isDomainAllowed('unlisted.test')).toBe(false);
|
|
1291
|
+
} finally {
|
|
1292
|
+
if (previousLocation === undefined) delete globalThis.location;
|
|
1293
|
+
else globalThis.location = previousLocation;
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
|
|
1297
|
+
it('defers scoped policy-file requests and caches both successful and failed results', async () => {
|
|
1298
|
+
const requests = [];
|
|
1299
|
+
const load = async (url) => {
|
|
1300
|
+
requests.push(url);
|
|
1301
|
+
if (url.endsWith('/blocked.xml')) throw new TypeError('Fetch rejected');
|
|
1302
|
+
if (url.endsWith('/missing.xml')) return { ok: false, status: 404 };
|
|
1303
|
+
return { ok: true, text: async () => '<allow-access-from domain="example.org" />' };
|
|
1304
|
+
};
|
|
1305
|
+
const runtime = new Function('fetch', `${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)(load);
|
|
1306
|
+
const security = runtime.builtins.Security;
|
|
1307
|
+
expect(security.loadPolicyFile('https://example.org/assets/missing.xml')).toBeUndefined();
|
|
1308
|
+
security.loadPolicyFile('https://example.org/assets/missing.xml#duplicate');
|
|
1309
|
+
security.loadPolicyFile('https://example.org/assets/blocked.xml');
|
|
1310
|
+
security.loadPolicyFile('https://example.org/assets/policy.xml');
|
|
1311
|
+
await Promise.resolve();
|
|
1312
|
+
expect(requests).toEqual([]);
|
|
1313
|
+
expect(security._checkPolicyFiles('https://example.org/data.xml')).toBeUndefined();
|
|
1314
|
+
expect(security._checkPolicyFiles('http://example.org/assets/data.xml')).toBeUndefined();
|
|
1315
|
+
const first = security._checkPolicyFiles('https://example.org/assets/data.xml');
|
|
1316
|
+
const second = security._checkPolicyFiles('https://example.org/assets/MoreData.xml');
|
|
1317
|
+
expect(await first).toEqual([false, false, true]);
|
|
1318
|
+
expect(await second).toEqual([false, false, true]);
|
|
1319
|
+
security.loadPolicyFile('https://example.org/assets/missing.xml');
|
|
1320
|
+
expect(await security._checkPolicyFiles('https://example.org/assets/data.xml')).toEqual([false, false, true]);
|
|
1321
|
+
expect(security._policyFiles['https://example.org/assets/policy.xml']).toEqual(['example.org']);
|
|
1322
|
+
expect(requests).toEqual(['https://example.org/assets/missing.xml', 'https://example.org/assets/blocked.xml', 'https://example.org/assets/policy.xml']);
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
it('supports ApplicationDomain definitions and selects domain memory without a global bridge', () => {
|
|
1326
|
+
const previousBridge = globalThis.__swoof_setDomainMemory;
|
|
1327
|
+
let assigned = null;
|
|
1328
|
+
globalThis.__swoof_setDomainMemory = (memory) => { assigned = memory; };
|
|
1329
|
+
try {
|
|
1330
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1331
|
+
const { ApplicationDomain, ByteArray } = runtime.builtins;
|
|
1332
|
+
const domain = ApplicationDomain.currentDomain;
|
|
1333
|
+
expect(domain.hasDefinition('flash.utils.ByteArray')).toBe(true);
|
|
1334
|
+
expect(domain.getDefinition('flash.utils::ByteArray')).toBe(ByteArray);
|
|
1335
|
+
expect(domain.hasDefinition('missing.Definition')).toBe(false);
|
|
1336
|
+
expect(() => domain.getDefinition('missing.Definition')).toThrow(/Definition not found/);
|
|
1337
|
+
const child = new ApplicationDomain(domain);
|
|
1338
|
+
expect(child.parentDomain).toBe(domain);
|
|
1339
|
+
expect(child.getDefinition('flash.utils.ByteArray')).toBe(ByteArray);
|
|
1340
|
+
expect(child._registerDefinition('app.ChildOnly', { name: 'child' })).toEqual({ name: 'child' });
|
|
1341
|
+
expect(child.getDefinition('app::ChildOnly')).toEqual({ name: 'child' });
|
|
1342
|
+
expect(domain.hasDefinition('app.ChildOnly')).toBe(false);
|
|
1343
|
+
const sibling = new ApplicationDomain(domain);
|
|
1344
|
+
expect(sibling.hasDefinition('app.ChildOnly')).toBe(false);
|
|
1345
|
+
expect(() => sibling.getDefinition('app.ChildOnly')).toThrow(/Definition not found/);
|
|
1346
|
+
const childShadow = child._registerDefinition('flash.utils.ByteArray', function ShadowByteArray() {});
|
|
1347
|
+
expect(childShadow).toBe(ByteArray);
|
|
1348
|
+
expect(child.getDefinition('flash.utils.ByteArray')).toBe(ByteArray);
|
|
1349
|
+
|
|
1350
|
+
const memory = new ByteArray();
|
|
1351
|
+
memory.writeUnsignedByte(123);
|
|
1352
|
+
memory.length = ApplicationDomain.MIN_DOMAIN_MEMORY_LENGTH;
|
|
1353
|
+
domain.domainMemory = memory;
|
|
1354
|
+
expect(domain.domainMemory).toBe(memory);
|
|
1355
|
+
expect(assigned).toBeNull();
|
|
1356
|
+
expect(runtime.packages.system.ApplicationDomain).toBe(ApplicationDomain);
|
|
1357
|
+
} finally {
|
|
1358
|
+
if (previousBridge === undefined) delete globalThis.__swoof_setDomainMemory;
|
|
1359
|
+
else globalThis.__swoof_setDomainMemory = previousBridge;
|
|
1360
|
+
}
|
|
1361
|
+
});
|
|
1362
|
+
|
|
1363
|
+
it('keeps ApplicationDomain packages and generated definitions with their runtime', () => {
|
|
1364
|
+
const previousLookup = Object.getOwnPropertyDescriptor(globalThis, '__swoof_getDefinition');
|
|
1365
|
+
const previousBridge = Object.getOwnPropertyDescriptor(globalThis, '__swoof_setDomainMemory');
|
|
1366
|
+
const previousGetDefinitionByName = Object.getOwnPropertyDescriptor(globalThis, 'getDefinitionByName');
|
|
1367
|
+
const loadRuntime = (bundle) => new Function(
|
|
1368
|
+
`${SWFLIB_SOURCE}\n${bundle}\nreturn __swoof_swflib;`
|
|
1369
|
+
)();
|
|
1370
|
+
try {
|
|
1371
|
+
const first = loadRuntime(`${AVM2_RUNTIME_STUB}
|
|
1372
|
+
_classes['fixture.First'] = class First {};
|
|
1373
|
+
`);
|
|
1374
|
+
Object.defineProperty(globalThis, '__swoof_getDefinition', {
|
|
1375
|
+
configurable: true, writable: true, value: () => first.builtins.ByteArray,
|
|
1376
|
+
});
|
|
1377
|
+
const second = loadRuntime('');
|
|
1378
|
+
const third = loadRuntime(`${AVM2_RUNTIME_STUB}
|
|
1379
|
+
_classes['fixture.Third'] = class Third {};
|
|
1380
|
+
`);
|
|
1381
|
+
|
|
1382
|
+
const firstDomain = first.builtins.ApplicationDomain.currentDomain;
|
|
1383
|
+
const secondDomain = second.builtins.ApplicationDomain.currentDomain;
|
|
1384
|
+
const thirdDomain = third.builtins.ApplicationDomain.currentDomain;
|
|
1385
|
+
expect(first.builtins.ByteArray).not.toBe(second.builtins.ByteArray);
|
|
1386
|
+
for (const [runtime, domain] of [[first, firstDomain], [second, secondDomain], [third, thirdDomain]]) {
|
|
1387
|
+
expect(runtime.packages.utils.ByteArray).toBe(runtime.builtins.ByteArray);
|
|
1388
|
+
expect(domain.getDefinition('flash.utils::ByteArray')).toBe(runtime.builtins.ByteArray);
|
|
1389
|
+
expect(domain.getDefinition('ByteArray')).toBe(runtime.builtins.ByteArray);
|
|
1390
|
+
expect(domain.hasDefinition('missing.ByteArray')).toBe(false);
|
|
1391
|
+
}
|
|
1392
|
+
expect(firstDomain.getDefinition('fixture::First').name).toBe('First');
|
|
1393
|
+
expect(thirdDomain.getDefinition('fixture::Third').name).toBe('Third');
|
|
1394
|
+
expect(firstDomain.hasDefinition('fixture.Third')).toBe(false);
|
|
1395
|
+
expect(secondDomain.hasDefinition('fixture.First')).toBe(false);
|
|
1396
|
+
expect(thirdDomain.hasDefinition('fixture.First')).toBe(false);
|
|
1397
|
+
} finally {
|
|
1398
|
+
for (const [name, descriptor] of [
|
|
1399
|
+
['__swoof_getDefinition', previousLookup],
|
|
1400
|
+
['__swoof_setDomainMemory', previousBridge],
|
|
1401
|
+
['getDefinitionByName', previousGetDefinitionByName],
|
|
1402
|
+
]) {
|
|
1403
|
+
if (descriptor) Object.defineProperty(globalThis, name, descriptor);
|
|
1404
|
+
else delete globalThis[name];
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
});
|
|
1408
|
+
|
|
1409
|
+
it('finds nested hit objects in display order and respects masks', () => {
|
|
1410
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1411
|
+
const { builtins } = runtime;
|
|
1412
|
+
const root = new builtins.DisplayObjectContainer();
|
|
1413
|
+
const parent = new builtins.Sprite();
|
|
1414
|
+
const child = new builtins.Shape();
|
|
1415
|
+
child.graphics.beginFill(0xffffff);
|
|
1416
|
+
child.graphics.drawRect(0, 0, 30, 30);
|
|
1417
|
+
child.graphics.endFill();
|
|
1418
|
+
parent.addChild(child);
|
|
1419
|
+
root.addChild(parent);
|
|
1420
|
+
expect(root.getObjectsUnderPoint(new builtins.Point(10, 10))).toEqual([child, parent]);
|
|
1421
|
+
expect(root.getObjectsUnderPoint(new builtins.Point(50, 50))).toEqual([]);
|
|
1422
|
+
|
|
1423
|
+
const masked = new builtins.Shape();
|
|
1424
|
+
masked.graphics.beginFill(0xffffff);
|
|
1425
|
+
masked.graphics.drawRect(0, 0, 30, 30);
|
|
1426
|
+
masked.graphics.endFill();
|
|
1427
|
+
const mask = new builtins.Shape();
|
|
1428
|
+
mask.graphics.beginFill(0xffffff);
|
|
1429
|
+
mask.graphics.drawRect(0, 0, 5, 5);
|
|
1430
|
+
mask.graphics.endFill();
|
|
1431
|
+
masked.mask = mask;
|
|
1432
|
+
root.addChild(masked);
|
|
1433
|
+
expect(root.getObjectsUnderPoint(new builtins.Point(10, 10))).not.toContain(masked);
|
|
1434
|
+
expect(root.getObjectsUnderPoint(new builtins.Point(2, 2))).toContain(masked);
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
it('reports container identity and live display-list ancestry through contains', () => {
|
|
1438
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1439
|
+
const { Sprite, Shape } = runtime.builtins;
|
|
1440
|
+
const form = new Sprite();
|
|
1441
|
+
const section = new Sprite();
|
|
1442
|
+
const row = new Sprite();
|
|
1443
|
+
const indicator = new Shape();
|
|
1444
|
+
const otherForm = new Sprite();
|
|
1445
|
+
form.addChild(section);
|
|
1446
|
+
section.addChild(row);
|
|
1447
|
+
row.addChild(indicator);
|
|
1448
|
+
row.visible = false;
|
|
1449
|
+
|
|
1450
|
+
expect(form.contains(form)).toBe(true);
|
|
1451
|
+
expect(form.contains(section)).toBe(true);
|
|
1452
|
+
expect(form.contains(indicator)).toBe(true);
|
|
1453
|
+
expect(section.contains(form)).toBe(false);
|
|
1454
|
+
expect(form.contains(otherForm)).toBe(false);
|
|
1455
|
+
expect(form.contains(null)).toBe(false);
|
|
1456
|
+
|
|
1457
|
+
otherForm.addChild(section);
|
|
1458
|
+
expect(form.contains(indicator)).toBe(false);
|
|
1459
|
+
expect(otherForm.contains(indicator)).toBe(true);
|
|
1460
|
+
row.removeChild(indicator);
|
|
1461
|
+
expect(otherForm.contains(indicator)).toBe(false);
|
|
1462
|
+
});
|
|
1463
|
+
});
|
|
1464
|
+
|
|
1465
|
+
describe('Graphics fill and line fidelity', () => {
|
|
1466
|
+
it('applies spread, linearRGB interpolation, caps, and joins in Canvas replay', () => {
|
|
1467
|
+
const previousPath2D = globalThis.Path2D;
|
|
1468
|
+
globalThis.Path2D = class {
|
|
1469
|
+
moveTo() {} lineTo() {} quadraticCurveTo() {} bezierCurveTo() {} arc() {} closePath() {}
|
|
1470
|
+
};
|
|
1471
|
+
try {
|
|
1472
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1473
|
+
const gradientStops = [];
|
|
1474
|
+
const gradient = { addColorStop(position, color) { gradientStops.push({ position, color }); } };
|
|
1475
|
+
const ctx = {
|
|
1476
|
+
globalAlpha: 1, lineCap: '', lineJoin: '', lineWidth: 0, miterLimit: 0,
|
|
1477
|
+
save() {}, restore() {}, transform() {},
|
|
1478
|
+
createLinearGradient() { return gradient; }, fill() {}, stroke() {},
|
|
1479
|
+
};
|
|
1480
|
+
const graphics = new runtime.builtins.Graphics();
|
|
1481
|
+
graphics.beginGradientFill('linear', [0x000000, 0xffffff], [1, 1], [0, 255], new runtime.builtins.Matrix(), 'reflect', 'linearRGB');
|
|
1482
|
+
graphics.lineStyle(3, 0xff0000, 1, false, 'normal', 'square', 'bevel', 4);
|
|
1483
|
+
graphics.drawRect(0, 0, 20, 20);
|
|
1484
|
+
graphics._paint(ctx);
|
|
1485
|
+
expect(gradientStops.length).toBeGreaterThan(2);
|
|
1486
|
+
expect(gradientStops[0].position).toBeLessThan(gradientStops[gradientStops.length - 1].position);
|
|
1487
|
+
expect(ctx.lineCap).toBe('square');
|
|
1488
|
+
expect(ctx.lineJoin).toBe('bevel');
|
|
1489
|
+
expect(ctx.miterLimit).toBe(4);
|
|
1490
|
+
} finally {
|
|
1491
|
+
if (previousPath2D === undefined) delete globalThis.Path2D;
|
|
1492
|
+
else globalThis.Path2D = previousPath2D;
|
|
1493
|
+
}
|
|
1494
|
+
});
|
|
1495
|
+
});
|
|
1496
|
+
|
|
1497
|
+
describe('Stage layout', () => {
|
|
1498
|
+
it('applies showAll, exactFit, noScale, and alignment to the canvas wrapper', () => {
|
|
1499
|
+
const oldDocument = globalThis.document;
|
|
1500
|
+
const oldWindow = globalThis.window;
|
|
1501
|
+
const canvas = { width: 640, height: 480, style: {}, addEventListener() {}, getBoundingClientRect() { return { left: 0, top: 0, width: 640, height: 480 }; } };
|
|
1502
|
+
const wrapper = { style: {} };
|
|
1503
|
+
const body = { style: {} };
|
|
1504
|
+
globalThis.document = {
|
|
1505
|
+
readyState: 'complete', body,
|
|
1506
|
+
getElementById(id) { return id === 'stage' ? canvas : id === 'stage-wrap' ? wrapper : null; },
|
|
1507
|
+
};
|
|
1508
|
+
globalThis.window = { innerWidth: 320, innerHeight: 200, addEventListener() {} };
|
|
1509
|
+
try {
|
|
1510
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1511
|
+
const stage = runtime.stage;
|
|
1512
|
+
stage.scaleMode = 'showAll';
|
|
1513
|
+
expect(parseFloat(wrapper.style.width)).toBeCloseTo(266.667, 2);
|
|
1514
|
+
expect(wrapper.style.height).toBe('200px');
|
|
1515
|
+
stage.scaleMode = 'exactFit';
|
|
1516
|
+
expect(canvas.style.width).toBe('320px');
|
|
1517
|
+
expect(canvas.style.height).toBe('200px');
|
|
1518
|
+
stage.scaleMode = 'noScale';
|
|
1519
|
+
stage.align = 'BR';
|
|
1520
|
+
expect(wrapper.style.width).toBe('320px');
|
|
1521
|
+
expect(wrapper.style.height).toBe('200px');
|
|
1522
|
+
expect(canvas.width).toBe(320);
|
|
1523
|
+
expect(canvas.height).toBe(200);
|
|
1524
|
+
expect(body.style.justifyContent).toBe('flex-end');
|
|
1525
|
+
expect(body.style.alignItems).toBe('flex-end');
|
|
1526
|
+
} finally {
|
|
1527
|
+
if (oldDocument === undefined) delete globalThis.document;
|
|
1528
|
+
else globalThis.document = oldDocument;
|
|
1529
|
+
if (oldWindow === undefined) delete globalThis.window;
|
|
1530
|
+
else globalThis.window = oldWindow;
|
|
1531
|
+
}
|
|
1532
|
+
});
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
describe('SoundStream MP3 support', () => {
|
|
1536
|
+
it('decodes stream headers and combines MP3 block payloads', () => {
|
|
1537
|
+
const head = decodeTag({ code: TAG.SoundStreamHead, data: Uint8Array.of(0x0f, 0x2f, 0x80, 0x04, 0, 0) });
|
|
1538
|
+
const block = decodeTag({ code: TAG.SoundStreamBlock, data: Uint8Array.of(0x80, 0x04, 0, 0, 0xff, 0xfb, 0x90, 0x64) });
|
|
1539
|
+
expect(head.streamCompression).toBe(2);
|
|
1540
|
+
expect(head.streamRate).toBe(3);
|
|
1541
|
+
expect(head.streamStereo).toBe(true);
|
|
1542
|
+
expect(head.sampleCount).toBe(1152);
|
|
1543
|
+
const encoded = encodeSoundStream([head, block]);
|
|
1544
|
+
expect(encoded.ext).toBe('mp3');
|
|
1545
|
+
expect([...encoded.data]).toEqual([0xff, 0xfb, 0x90, 0x64]);
|
|
1546
|
+
});
|
|
1547
|
+
|
|
1548
|
+
it('decodes StartSound in/out points, loops, and envelope points', () => {
|
|
1549
|
+
const sound = decodeTag({
|
|
1550
|
+
code: TAG.StartSound,
|
|
1551
|
+
data: Uint8Array.of(7, 0, 0x0f, 10, 0, 0, 0, 20, 0, 0, 0, 2, 0, 1, 5, 0, 0, 0, 0, 0x40, 0, 0x20),
|
|
1552
|
+
});
|
|
1553
|
+
expect(sound.soundId).toBe(7);
|
|
1554
|
+
expect(sound.soundInfo.inPoint).toBe(10);
|
|
1555
|
+
expect(sound.soundInfo.outPoint).toBe(20);
|
|
1556
|
+
expect(sound.soundInfo.loopCount).toBe(2);
|
|
1557
|
+
expect(sound.soundInfo.envelope).toEqual([{ position: 5, leftLevel: 16384, rightLevel: 8192 }]);
|
|
1558
|
+
});
|
|
1559
|
+
});
|
|
1560
|
+
|
|
1561
|
+
describe('SoundChannel behavior', () => {
|
|
1562
|
+
it('uses millisecond offsets and repeats the requested number of plays', () => {
|
|
1563
|
+
const previousAudio = globalThis.Audio;
|
|
1564
|
+
class FakeAudio {
|
|
1565
|
+
constructor(src) { this.src = src || ''; this.currentTime = 0; this.volume = 1; this.paused = true; this.listeners = {}; this.playCount = 0; }
|
|
1566
|
+
cloneNode() { return new FakeAudio(this.src); }
|
|
1567
|
+
play() { this.paused = false; this.playCount++; return Promise.resolve(); }
|
|
1568
|
+
pause() { this.paused = true; }
|
|
1569
|
+
addEventListener(type, callback) { this.listeners[type] = callback; }
|
|
1570
|
+
}
|
|
1571
|
+
globalThis.Audio = FakeAudio;
|
|
1572
|
+
try {
|
|
1573
|
+
const runtime = new Function(`${SWFLIB_SOURCE}\nreturn __swoof_swflib;`)();
|
|
1574
|
+
const sound = new runtime.builtins.Sound();
|
|
1575
|
+
sound.url = 'audio.mp3';
|
|
1576
|
+
const transform = new runtime.builtins.SoundTransform(0.4, -0.25);
|
|
1577
|
+
const channel = sound.play(250, 2, transform);
|
|
1578
|
+
expect(channel._audio.currentTime).toBe(0.25);
|
|
1579
|
+
expect(channel._audio.volume).toBe(0.4);
|
|
1580
|
+
expect(channel.position).toBe(250);
|
|
1581
|
+
let completed = 0;
|
|
1582
|
+
channel.addEventListener('soundComplete', () => completed++);
|
|
1583
|
+
channel._audio.listeners.ended();
|
|
1584
|
+
expect(channel._audio.playCount).toBe(2);
|
|
1585
|
+
expect(completed).toBe(0);
|
|
1586
|
+
channel._audio.listeners.ended();
|
|
1587
|
+
expect(completed).toBe(1);
|
|
1588
|
+
|
|
1589
|
+
const linked = new runtime.builtins.Sound();
|
|
1590
|
+
linked._swoofSoundHost = {
|
|
1591
|
+
exportAssets: [{ name: 'Theme', id: 9 }],
|
|
1592
|
+
assets: [{ id: 9, sampleRate: 22050, sampleCount: 66150 }],
|
|
1593
|
+
loaded: { 9: new Audio('theme.mp3') },
|
|
1594
|
+
};
|
|
1595
|
+
linked.attachSound('Theme');
|
|
1596
|
+
expect(linked.duration).toBe(3000);
|
|
1597
|
+
let linkedComplete = 0;
|
|
1598
|
+
linked.onSoundComplete = () => linkedComplete++;
|
|
1599
|
+
const linkedChannel = linked.start(1.5, 1);
|
|
1600
|
+
expect(linkedChannel._audio.src).toBe('theme.mp3');
|
|
1601
|
+
expect(linked.position).toBe(1500);
|
|
1602
|
+
linkedChannel._audio.listeners.ended();
|
|
1603
|
+
expect(linkedComplete).toBe(1);
|
|
1604
|
+
} finally {
|
|
1605
|
+
if (previousAudio === undefined) delete globalThis.Audio;
|
|
1606
|
+
else globalThis.Audio = previousAudio;
|
|
1607
|
+
}
|
|
1608
|
+
});
|
|
1609
|
+
});
|