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.
Files changed (416) hide show
  1. package/README.md +81 -14
  2. package/package.json +11 -7
  3. package/studio/actionscript-roundtrip.test.js +54 -0
  4. package/studio/actionscript-scripts.test.js +169 -0
  5. package/studio/animated-image.test.js +137 -0
  6. package/studio/arithmetic.test.js +16 -0
  7. package/studio/as3-import.test.js +110 -0
  8. package/studio/audio-info.test.js +44 -0
  9. package/studio/components.test.js +152 -0
  10. package/studio/document-editing.test.js +92 -0
  11. package/studio/examples/01-space-mission.json +1628 -0
  12. package/studio/examples/02-clockwork-vault.json +1079 -0
  13. package/studio/examples/03-neon-synth.json +1364 -0
  14. package/studio/examples/04-kinetic-poster.json +923 -0
  15. package/studio/examples/05-signal-relay.json +1131 -0
  16. package/studio/examples/06-calculator.json +564 -0
  17. package/studio/frame-editing.test.js +129 -0
  18. package/studio/frame-events.test.js +117 -0
  19. package/studio/groups.test.js +88 -0
  20. package/studio/index.html +962 -0
  21. package/studio/interaction-triggers.test.js +37 -0
  22. package/studio/linked-actions.test.js +48 -0
  23. package/studio/lottie.test.js +208 -0
  24. package/studio/masks.test.js +43 -0
  25. package/studio/media-assets.test.js +62 -0
  26. package/studio/paint-tools.test.js +60 -0
  27. package/studio/playback.test.js +17 -0
  28. package/studio/project-io.test.js +405 -0
  29. package/studio/project-preview.test.js +475 -0
  30. package/studio/scene-transition.test.js +32 -0
  31. package/studio/server/archive-companions.js +27 -0
  32. package/studio/server/as3-library.js +91 -0
  33. package/studio/server/assets.js +162 -0
  34. package/studio/server/audio-info.js +82 -0
  35. package/studio/server/font-files.js +332 -0
  36. package/studio/server/http.js +135 -0
  37. package/studio/server/linked-actions.js +93 -0
  38. package/studio/server/png-decode.js +1 -0
  39. package/studio/server/project-movie.js +1164 -0
  40. package/studio/server/project-preview.js +152 -0
  41. package/studio/server/projects.js +370 -0
  42. package/studio/server/report.js +29 -0
  43. package/studio/server/routes.js +93 -0
  44. package/studio/server/swf-authoring.js +90 -0
  45. package/studio/server/swf-control-runtime.as +513 -0
  46. package/studio/server/swf-controls.js +119 -0
  47. package/studio/server/swf-export.js +293 -0
  48. package/studio/server/swf-import.js +1675 -0
  49. package/studio/server/swf-morphs.js +64 -0
  50. package/studio/server/swf-routes.js +42 -0
  51. package/studio/server/swf-streams.js +80 -0
  52. package/studio/server/swf-transition-runtime.as +52 -0
  53. package/studio/server/tween-recovery.js +106 -0
  54. package/studio/server-api.test.js +283 -0
  55. package/studio/server.js +320 -0
  56. package/studio/src/animated-image.js +423 -0
  57. package/studio/src/appearance-inspector.js +63 -0
  58. package/studio/src/arithmetic.js +50 -0
  59. package/studio/src/authored-controls.js +42 -0
  60. package/studio/src/busy.js +41 -0
  61. package/studio/src/canvas.js +430 -0
  62. package/studio/src/clipper-browser.js +2 -0
  63. package/studio/src/color-transform.js +38 -0
  64. package/studio/src/component-inspector.js +126 -0
  65. package/studio/src/components.js +268 -0
  66. package/studio/src/display-frame.js +262 -0
  67. package/studio/src/document-editing.js +69 -0
  68. package/studio/src/event-editor.js +150 -0
  69. package/studio/src/frame-editing.js +150 -0
  70. package/studio/src/frame-events.js +128 -0
  71. package/studio/src/frame-image.js +28 -0
  72. package/studio/src/groups.js +125 -0
  73. package/studio/src/handler-editor.js +122 -0
  74. package/studio/src/history.js +83 -0
  75. package/studio/src/imported-paths.js +99 -0
  76. package/studio/src/index.js +3582 -0
  77. package/studio/src/interaction-triggers.js +42 -0
  78. package/studio/src/json-patch.js +77 -0
  79. package/studio/src/layer-icons.js +25 -0
  80. package/studio/src/lottie-export.js +948 -0
  81. package/studio/src/lottie-import.js +1138 -0
  82. package/studio/src/masks.js +69 -0
  83. package/studio/src/media-assets.js +188 -0
  84. package/studio/src/media-library.js +193 -0
  85. package/studio/src/model.js +431 -0
  86. package/studio/src/numeric-fields.js +46 -0
  87. package/studio/src/object-list.js +57 -0
  88. package/studio/src/paint-tools.js +76 -0
  89. package/studio/src/placement-effects.js +74 -0
  90. package/studio/src/playback.js +10 -0
  91. package/studio/src/preview-audio.js +64 -0
  92. package/studio/src/preview-interactions.js +106 -0
  93. package/studio/src/preview-recorder.js +335 -0
  94. package/studio/src/preview-surface.js +338 -0
  95. package/studio/src/project-io.js +905 -0
  96. package/studio/src/project-session.js +375 -0
  97. package/studio/src/projects-dialog.js +319 -0
  98. package/studio/src/scene-transition.js +129 -0
  99. package/studio/src/script-editor.js +175 -0
  100. package/studio/src/script-references.js +74 -0
  101. package/studio/src/scripted-controls.js +146 -0
  102. package/studio/src/scripts.js +185 -0
  103. package/studio/src/stage-guides.js +101 -0
  104. package/studio/src/svg-animation.js +416 -0
  105. package/studio/src/svg-context.js +522 -0
  106. package/studio/src/svg-frame.js +413 -0
  107. package/studio/src/svg-import.js +1208 -0
  108. package/studio/src/swf-recording.js +104 -0
  109. package/studio/src/symbol-library.js +39 -0
  110. package/studio/src/symbols.js +62 -0
  111. package/studio/src/text-fields.js +58 -0
  112. package/studio/src/theme.js +43 -0
  113. package/studio/src/timeline.js +321 -0
  114. package/studio/src/vector-cut.js +103 -0
  115. package/studio/src/vector-geometry.js +254 -0
  116. package/studio/src/vector-style.js +146 -0
  117. package/studio/src/video-encode.js +84 -0
  118. package/studio/src/video-export.js +331 -0
  119. package/studio/src/video-mux.js +339 -0
  120. package/studio/src/video-playback.js +281 -0
  121. package/studio/src/workspace.js +344 -0
  122. package/studio/stroke-style.test.js +119 -0
  123. package/studio/styles.css +1547 -0
  124. package/studio/svg-export.test.js +162 -0
  125. package/studio/svg-import.test.js +318 -0
  126. package/studio/swf-authoring.test.js +140 -0
  127. package/studio/swf-export.test.js +324 -0
  128. package/studio/swf-import.test.js +489 -0
  129. package/studio/symbols.test.js +68 -0
  130. package/studio/tests/accessibility-interactions.mjs +138 -0
  131. package/studio/tests/appearance-interactions.mjs +103 -0
  132. package/studio/tests/control-interactions.mjs +60 -0
  133. package/studio/tests/event-interactions.mjs +117 -0
  134. package/studio/tests/external-resources.mjs +174 -0
  135. package/studio/tests/frame-image.mjs +71 -0
  136. package/studio/tests/html-export.mjs +68 -0
  137. package/studio/tests/import-files.mjs +131 -0
  138. package/studio/tests/lottie.mjs +125 -0
  139. package/studio/tests/mask-interactions.mjs +148 -0
  140. package/studio/tests/media-interactions.mjs +109 -0
  141. package/studio/tests/paint-interactions.mjs +64 -0
  142. package/studio/tests/project-interactions.mjs +244 -0
  143. package/studio/tests/script-interactions.mjs +178 -0
  144. package/studio/tests/scripted-control-interactions.mjs +107 -0
  145. package/studio/tests/svg-export.mjs +149 -0
  146. package/studio/tests/text-field-interactions.mjs +64 -0
  147. package/studio/tests/theme-interactions.mjs +53 -0
  148. package/studio/tests/timeline-browser.mjs +507 -0
  149. package/studio/tests/transition-interactions.mjs +94 -0
  150. package/studio/tests/trigger-interactions.mjs +93 -0
  151. package/studio/tests/video-export.mjs +243 -0
  152. package/studio/tests/workspace-interactions.mjs +226 -0
  153. package/studio/tween-recovery.test.js +31 -0
  154. package/studio/vector-style.test.js +56 -0
  155. package/studio/video-export.test.js +326 -0
  156. package/studiospec/actionscript.md +88 -0
  157. package/studiospec/architecture.md +378 -0
  158. package/studiospec/controls.md +100 -0
  159. package/studiospec/file-support.md +268 -0
  160. package/studiospec/lottie.md +69 -0
  161. package/studiospec/requirements.md +135 -0
  162. package/studiospec/stroke-style.md +95 -0
  163. package/studiospec/svg-export.md +83 -0
  164. package/studiospec/svg-import.md +139 -0
  165. package/studiospec/swooflib-integration.md +115 -0
  166. package/studiospec/tasks.md +573 -0
  167. package/studiospec/video-export.md +127 -0
  168. package/swf2js/__tests__/actionscript.test.js +1609 -0
  169. package/swf2js/__tests__/adpcm-audio.test.js +43 -0
  170. package/swf2js/__tests__/as2-compiler.test.js +153 -0
  171. package/swf2js/__tests__/as3-mixed-depth.test.js +498 -0
  172. package/swf2js/__tests__/authored-sprite-hit.test.js +118 -0
  173. package/swf2js/__tests__/authored-sprite-sizing.test.js +164 -0
  174. package/swf2js/__tests__/avm1-action-prefix.test.js +134 -0
  175. package/swf2js/__tests__/avm1-attach-movie.test.js +1151 -0
  176. package/swf2js/__tests__/avm1-boxhead-budget.test.js +73 -0
  177. package/swf2js/__tests__/avm1-cfg-bubble-tanks.test.js +101 -0
  178. package/swf2js/__tests__/avm1-cfg-mario-kart.test.js +184 -0
  179. package/swf2js/__tests__/avm1-cfg-mario.test.js +31 -0
  180. package/swf2js/__tests__/avm1-cfg-pacman.test.js +49 -0
  181. package/swf2js/__tests__/avm1-checkers-gameplay.test.js +122 -0
  182. package/swf2js/__tests__/avm1-clip-actions.test.js +2002 -0
  183. package/swf2js/__tests__/avm1-clone-drag.test.js +626 -0
  184. package/swf2js/__tests__/avm1-decompiler.test.js +102 -0
  185. package/swf2js/__tests__/avm1-dynamic-locals.test.js +159 -0
  186. package/swf2js/__tests__/avm1-formal-parameters.test.js +71 -0
  187. package/swf2js/__tests__/avm1-key-events.test.js +52 -0
  188. package/swf2js/__tests__/avm1-load-bitmap.test.js +75 -0
  189. package/swf2js/__tests__/avm1-loading-host-actions.test.js +764 -0
  190. package/swf2js/__tests__/avm1-multibyte-strings.test.js +63 -0
  191. package/swf2js/__tests__/avm1-pacman-gameplay.test.js +115 -0
  192. package/swf2js/__tests__/avm1-shape-hittest.test.js +311 -0
  193. package/swf2js/__tests__/avm1-shared-bodies.test.js +57 -0
  194. package/swf2js/__tests__/avm1-shared-runtime.test.js +1364 -0
  195. package/swf2js/__tests__/avm1-snake-gameplay.test.js +139 -0
  196. package/swf2js/__tests__/avm1-super.test.js +70 -0
  197. package/swf2js/__tests__/avm1-target-path.test.js +325 -0
  198. package/swf2js/__tests__/avm1-try.test.js +229 -0
  199. package/swf2js/__tests__/avm2-activation-closures.test.js +65 -0
  200. package/swf2js/__tests__/avm2-applytype.test.js +180 -0
  201. package/swf2js/__tests__/avm2-call-ops.test.js +232 -0
  202. package/swf2js/__tests__/avm2-callsuperid.test.js +231 -0
  203. package/swf2js/__tests__/avm2-class-initializer.test.js +219 -0
  204. package/swf2js/__tests__/avm2-custom-namespace.test.js +139 -0
  205. package/swf2js/__tests__/avm2-describe-type.test.js +96 -0
  206. package/swf2js/__tests__/avm2-dictionary.test.js +96 -0
  207. package/swf2js/__tests__/avm2-domain-memory.test.js +332 -0
  208. package/swf2js/__tests__/avm2-e4x.test.js +326 -0
  209. package/swf2js/__tests__/avm2-exceptions.test.js +81 -0
  210. package/swf2js/__tests__/avm2-findprop.test.js +110 -0
  211. package/swf2js/__tests__/avm2-getlex.test.js +166 -0
  212. package/swf2js/__tests__/avm2-global-slot.test.js +106 -0
  213. package/swf2js/__tests__/avm2-interface.test.js +95 -0
  214. package/swf2js/__tests__/avm2-multiname-precedence.test.js +148 -0
  215. package/swf2js/__tests__/avm2-named-namespace-types.test.js +106 -0
  216. package/swf2js/__tests__/avm2-optional-args.test.js +100 -0
  217. package/swf2js/__tests__/avm2-outer-scopes.test.js +101 -0
  218. package/swf2js/__tests__/avm2-package-internal-class.test.js +106 -0
  219. package/swf2js/__tests__/avm2-protected-namespace.test.js +105 -0
  220. package/swf2js/__tests__/avm2-protected.test.js +214 -0
  221. package/swf2js/__tests__/avm2-qwop-gameplay.test.js +116 -0
  222. package/swf2js/__tests__/avm2-runtime-namespaces.test.js +159 -0
  223. package/swf2js/__tests__/avm2-script-traits.test.js +89 -0
  224. package/swf2js/__tests__/avm2-shared-runtime.test.js +52 -0
  225. package/swf2js/__tests__/avm2-slot-ops.test.js +117 -0
  226. package/swf2js/__tests__/avm2-type-ops.test.js +224 -0
  227. package/swf2js/__tests__/bevel-filter.test.js +116 -0
  228. package/swf2js/__tests__/bitmap-button-mask.test.js +106 -0
  229. package/swf2js/__tests__/bitmap-data-draw.test.js +388 -0
  230. package/swf2js/__tests__/bitmap-decoder.test.js +108 -0
  231. package/swf2js/__tests__/bitmapdata-color-bounds.test.js +70 -0
  232. package/swf2js/__tests__/bitmapdata-copy-channel.test.js +91 -0
  233. package/swf2js/__tests__/bitmapdata-copy-pixels.test.js +136 -0
  234. package/swf2js/__tests__/bitmapdata-fill-compare.test.js +241 -0
  235. package/swf2js/__tests__/bitmapdata-filter.test.js +232 -0
  236. package/swf2js/__tests__/bitmapdata-hit-test.test.js +71 -0
  237. package/swf2js/__tests__/bitmapdata-merge.test.js +84 -0
  238. package/swf2js/__tests__/bitmapdata-palette-map.test.js +92 -0
  239. package/swf2js/__tests__/bitmapdata-pixels-io.test.js +198 -0
  240. package/swf2js/__tests__/bitmapdata-threshold.test.js +97 -0
  241. package/swf2js/__tests__/bitmapdata-vector.test.js +82 -0
  242. package/swf2js/__tests__/blur-filter.test.js +48 -0
  243. package/swf2js/__tests__/bundle-emitter.test.js +394 -0
  244. package/swf2js/__tests__/button-actions.test.js +370 -0
  245. package/swf2js/__tests__/button-color-transform.test.js +48 -0
  246. package/swf2js/__tests__/button-decoder.test.js +106 -0
  247. package/swf2js/__tests__/button-hit-test.test.js +825 -0
  248. package/swf2js/__tests__/button-scaling-grid.test.js +124 -0
  249. package/swf2js/__tests__/button-sound.test.js +105 -0
  250. package/swf2js/__tests__/bytearray-coercion.test.js +23 -0
  251. package/swf2js/__tests__/bytearray-object-serialization.test.js +380 -0
  252. package/swf2js/__tests__/cfg-switch-loop.test.js +103 -0
  253. package/swf2js/__tests__/clip-depth-mask.test.js +119 -0
  254. package/swf2js/__tests__/clipped-bitmap-fill.test.js +118 -0
  255. package/swf2js/__tests__/companion-swf.test.js +638 -0
  256. package/swf2js/__tests__/context-menu.test.js +149 -0
  257. package/swf2js/__tests__/convolution-filter.test.js +74 -0
  258. package/swf2js/__tests__/definefont4.test.js +280 -0
  259. package/swf2js/__tests__/display-displacement.test.js +161 -0
  260. package/swf2js/__tests__/display-list.test.js +80 -0
  261. package/swf2js/__tests__/display-mask-render.test.js +656 -0
  262. package/swf2js/__tests__/displayobject-stage.test.js +320 -0
  263. package/swf2js/__tests__/embedded-script-audio.test.js +131 -0
  264. package/swf2js/__tests__/embedded-swf.test.js +202 -0
  265. package/swf2js/__tests__/embedded-textfield.test.js +107 -0
  266. package/swf2js/__tests__/embedded-timeline-audio.test.js +182 -0
  267. package/swf2js/__tests__/embedded-timeline-input.test.js +420 -0
  268. package/swf2js/__tests__/embedded-timer-lifecycle.test.js +145 -0
  269. package/swf2js/__tests__/event-priority-order.test.js +125 -0
  270. package/swf2js/__tests__/event-weak-listeners.test.js +53 -0
  271. package/swf2js/__tests__/event-will-trigger.test.js +43 -0
  272. package/swf2js/__tests__/flash-text-engine-content.test.js +115 -0
  273. package/swf2js/__tests__/flash-text-engine-format.test.js +95 -0
  274. package/swf2js/__tests__/flash-text-engine-line.test.js +314 -0
  275. package/swf2js/__tests__/font-encoder.test.js +95 -0
  276. package/swf2js/__tests__/font-outline-text.test.js +659 -0
  277. package/swf2js/__tests__/glow-shadow-filter.test.js +68 -0
  278. package/swf2js/__tests__/graphics-bitmap-smoothing.test.js +32 -0
  279. package/swf2js/__tests__/graphics-fill-lifetime.test.js +36 -0
  280. package/swf2js/__tests__/graphics-gradient-matrix.test.js +53 -0
  281. package/swf2js/__tests__/graphics-stroke-hit-test.test.js +457 -0
  282. package/swf2js/__tests__/hit-test-primitives.test.js +414 -0
  283. package/swf2js/__tests__/input-events.test.js +714 -0
  284. package/swf2js/__tests__/integration/corpus.test.js +41 -0
  285. package/swf2js/__tests__/loader-embedded-swf.test.js +210 -0
  286. package/swf2js/__tests__/loader-info.test.js +81 -0
  287. package/swf2js/__tests__/local-connection.test.js +122 -0
  288. package/swf2js/__tests__/morph-focal-gradient.test.js +51 -0
  289. package/swf2js/__tests__/morph-topology.test.js +267 -0
  290. package/swf2js/__tests__/movie-data-codec.test.js +128 -0
  291. package/swf2js/__tests__/nellymoser-audio.test.js +84 -0
  292. package/swf2js/__tests__/opaque-background-cache.test.js +352 -0
  293. package/swf2js/__tests__/path-stream.test.js +81 -0
  294. package/swf2js/__tests__/place-object-filters.test.js +88 -0
  295. package/swf2js/__tests__/place-object3-decoder.test.js +116 -0
  296. package/swf2js/__tests__/place-object3-image.test.js +85 -0
  297. package/swf2js/__tests__/player-version.test.js +46 -0
  298. package/swf2js/__tests__/root-textfield.test.js +287 -0
  299. package/swf2js/__tests__/scaling-grid.test.js +183 -0
  300. package/swf2js/__tests__/shape-decoder.test.js +361 -0
  301. package/swf2js/__tests__/shape-line-style2.test.js +138 -0
  302. package/swf2js/__tests__/shape-nonscaling-stroke.test.js +131 -0
  303. package/swf2js/__tests__/shape-pixel-hint.test.js +101 -0
  304. package/swf2js/__tests__/simplebutton-pointer.test.js +1477 -0
  305. package/swf2js/__tests__/socket-offline.test.js +67 -0
  306. package/swf2js/__tests__/sound-avm1-stop.test.js +99 -0
  307. package/swf2js/__tests__/sound-envelope.test.js +110 -0
  308. package/swf2js/__tests__/sound-extract.test.js +168 -0
  309. package/swf2js/__tests__/sound-id3.test.js +283 -0
  310. package/swf2js/__tests__/sound-load.test.js +259 -0
  311. package/swf2js/__tests__/sound-mixer-spectrum.test.js +261 -0
  312. package/swf2js/__tests__/sound-stream-sprite.test.js +624 -0
  313. package/swf2js/__tests__/sound-transform.test.js +104 -0
  314. package/swf2js/__tests__/speex-audio.test.js +77 -0
  315. package/swf2js/__tests__/stage-noscale.test.js +45 -0
  316. package/swf2js/__tests__/swf-encoder.test.js +134 -0
  317. package/swf2js/__tests__/swflib-assemble.test.js +20 -0
  318. package/swf2js/__tests__/textfield-html-text.test.js +368 -0
  319. package/swf2js/__tests__/textfield-line-layout.test.js +339 -0
  320. package/swf2js/__tests__/textfield-programmatic-overlay.test.js +2506 -0
  321. package/swf2js/__tests__/textfield-scroll.test.js +79 -0
  322. package/swf2js/__tests__/textfield-selection.test.js +383 -0
  323. package/swf2js/__tests__/timeline-effect-surfaces.test.js +295 -0
  324. package/swf2js/__tests__/timeline-named-anchor.test.js +139 -0
  325. package/swf2js/__tests__/timeline-scenes.test.js +1723 -0
  326. package/swf2js/__tests__/urlloader-http-status.test.js +236 -0
  327. package/{src → swf2js}/cli.js +43 -11
  328. package/swf2js/compiler/as2-classes.js +156 -0
  329. package/swf2js/compiler/as2-parser.js +598 -0
  330. package/swf2js/compiler/avm1-codegen.js +897 -0
  331. package/swf2js/compiler/index.js +28 -0
  332. package/{src → swf2js}/decoder/button-decoder.js +3 -1
  333. package/swf2js/decoder/png.js +114 -0
  334. package/{src → swf2js}/decoder/shape-decoder.js +20 -10
  335. package/{src → swf2js}/decoder/tag-decoder.js +5 -3
  336. package/{src → swf2js}/decoder/tag-types.js +4 -0
  337. package/swf2js/decompiler/avm1-decompiler.js +1138 -0
  338. package/{src → swf2js}/emitter/asset-encoder.js +66 -113
  339. package/{src → swf2js}/emitter/bundle-emitter.js +209 -78
  340. package/swf2js/emitter/external-resources.js +39 -0
  341. package/swf2js/emitter/movie-data-codec.js +91 -0
  342. package/swf2js/encoder/bit-writer.js +182 -0
  343. package/swf2js/encoder/shape-encoder.js +310 -0
  344. package/swf2js/encoder/swf-builder.js +639 -0
  345. package/swf2js/encoder/swf-writer.js +55 -0
  346. package/{src/runtime/swflib/assemble.js → swf2js/runtime/assemble-swooflib.js} +4 -3
  347. package/{src → swf2js}/runtime/avm1-runtime.js +102 -63
  348. package/{src → swf2js}/runtime/index.js +1 -1
  349. package/{src → swf2js}/runtime/movie-runtime.js +28 -2
  350. package/{src → swf2js}/transpiler/actionscript.js +82 -19
  351. package/{src → swf2js}/transpiler/avm1/avm1-opcodes.js +5 -3
  352. package/{src → swf2js}/transpiler/avm1/avm1-transpiler.js +69 -6
  353. package/{src → swf2js}/transpiler/avm2/avm2-transpiler.js +1 -1
  354. package/{src → swf2js}/transpiler/timeline/display-list.js +41 -22
  355. package/{src → swf2js}/transpiler/timeline/timeline-codegen.js +97 -36
  356. package/swooflib/README.md +569 -0
  357. package/swooflib/adpcm.js +147 -0
  358. package/{src/runtime/swflib → swooflib}/builtins.js +6 -0
  359. package/{src/runtime/swflib → swooflib}/context-menu.js +7 -1
  360. package/swooflib/control-definitions.js +42 -0
  361. package/swooflib/control-widget.js +763 -0
  362. package/swooflib/controls.js +300 -0
  363. package/{src/runtime/swflib → swooflib}/display.js +16 -10
  364. package/{src/runtime/swflib → swooflib}/events.js +11 -0
  365. package/{src/runtime/swflib → swooflib}/flash.js +6 -5
  366. package/{src/runtime/swflib → swooflib}/graphics.js +81 -19
  367. package/{src/runtime/swflib → swooflib}/input.js +12 -14
  368. package/{src/runtime/swflib → swooflib}/loader.js +52 -22
  369. package/swooflib/movie-network.js +86 -0
  370. package/swooflib/path-stream.js +169 -0
  371. package/{src/runtime/swflib → swooflib}/render.js +3 -13
  372. package/{src/runtime/swflib → swooflib}/sound.js +25 -2
  373. package/{src/runtime/swflib → swooflib}/stage.js +3 -3
  374. package/swooflib/stream-time.js +21 -0
  375. package/{src/runtime/swflib → swooflib}/system.js +88 -37
  376. package/{src/runtime/swflib → swooflib}/text-paint.js +14 -3
  377. package/{src/runtime/swflib → swooflib}/text.js +8 -0
  378. package/swooflib/xml-node.js +53 -0
  379. package/src/emitter/timeline-data-codec.js +0 -58
  380. /package/{src → swf2js}/decoder/symbol-table.js +0 -0
  381. /package/{src → swf2js}/emitter/embedded-swf.js +0 -0
  382. /package/{src → swf2js}/emitter/font-encoder.js +0 -0
  383. /package/{src → swf2js}/model/swf-document.js +0 -0
  384. /package/{src → swf2js}/model/transpile-result.js +0 -0
  385. /package/{src → swf2js}/parser/bit-reader.js +0 -0
  386. /package/{src → swf2js}/parser/decompressor.js +0 -0
  387. /package/{src → swf2js}/parser/swf-parser.js +0 -0
  388. /package/{src → swf2js}/pretty-printer/pretty-printer.js +0 -0
  389. /package/{src → swf2js}/runtime/avm2-runtime.js +0 -0
  390. /package/{src → swf2js}/runtime/embedded-movie-runtime.js +0 -0
  391. /package/{src → swf2js}/transpiler/avm1/avm1-ir.js +0 -0
  392. /package/{src → swf2js}/transpiler/avm1/avm1-rewrite.js +0 -0
  393. /package/{src → swf2js}/transpiler/avm2/abc-reader.js +0 -0
  394. /package/{src → swf2js}/transpiler/cfg-structurer.js +0 -0
  395. /package/{src → swf2js}/util/diagnostics.js +0 -0
  396. /package/{src → swf2js}/util/logger.js +0 -0
  397. /package/{src/runtime/swflib → swooflib}/bitmap-image.js +0 -0
  398. /package/{src/runtime/swflib → swooflib}/bitmap.js +0 -0
  399. /package/{src/runtime/swflib → swooflib}/bytearray.js +0 -0
  400. /package/{src/runtime/swflib → swooflib}/compiled-swf.js +0 -0
  401. /package/{src/runtime/swflib → swooflib}/cursor-flag.js +0 -0
  402. /package/{src/runtime/swflib → swooflib}/dictionary.js +0 -0
  403. /package/{src/runtime/swflib → swooflib}/filters.js +0 -0
  404. /package/{src/runtime/swflib → swooflib}/geom.js +0 -0
  405. /package/{src/runtime/swflib → swooflib}/glyph-metrics.js +0 -0
  406. /package/{src/runtime/swflib → swooflib}/host.js +0 -0
  407. /package/{src/runtime/swflib → swooflib}/inherit.js +0 -0
  408. /package/{src/runtime/swflib → swooflib}/marks.js +0 -0
  409. /package/{src/runtime/swflib → swooflib}/matrix3d.js +0 -0
  410. /package/{src/runtime/swflib → swooflib}/pointer-bridge.js +0 -0
  411. /package/{src/runtime/swflib → swooflib}/projection.js +0 -0
  412. /package/{src/runtime/swflib → swooflib}/sprite-drag.js +0 -0
  413. /package/{src/runtime/swflib → swooflib}/stage-ref.js +0 -0
  414. /package/{src/runtime/swflib → swooflib}/text-field.js +0 -0
  415. /package/{src/runtime/swflib → swooflib}/transform.js +0 -0
  416. /package/{src/runtime/swflib → swooflib}/xml.js +0 -0
@@ -0,0 +1,1364 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { runInNewContext } from 'node:vm';
3
+ import { AVM1_RUNTIME_FACTORY_SOURCE } from '../runtime/index.js';
4
+ import { TAG } from '../decoder/tag-types.js';
5
+ import { transpileActionScript } from '../transpiler/actionscript.js';
6
+ import { OP } from '../transpiler/avm1/avm1-opcodes.js';
7
+ import { timelineHelpersSource } from '../transpiler/timeline/timeline-codegen.js';
8
+ import { DiagnosticsCollector } from '../util/diagnostics.js';
9
+
10
+ function buildMovieFactory(actionScript, console = globalThis.console) {
11
+ return runInNewContext(`
12
+ var __swoof_swflib = {
13
+ createAvm1Runtime: ${AVM1_RUNTIME_FACTORY_SOURCE},
14
+ timelineHelpers: ${timelineHelpersSource()},
15
+ };
16
+ function Stage() {}
17
+ (function bootMovie(characterId, SWF_VERSION, STAGE_WIDTH, STAGE_HEIGHT) {
18
+ 'use strict';
19
+ var window = { __swoof: {} };
20
+ var displayList = {}, spritePlay = {}, CHARACTERS = { [characterId]: { type: 'shape' } };
21
+ var ensureSpritePlay, runSpriteFrameScripts, FRAME_SCRIPTS = [], SPRITE_FRAME_SCRIPTS = {};
22
+ var rootInitializers = [];
23
+ var timeline = { registerRootInitializer: (frame, script) => rootInitializers.push([frame, script]) };
24
+ var EXPORT_ASSETS = [{ name: 'sharedName', id: characterId }];
25
+ var root = { stop: function () { window.stopped = true; } };
26
+ var __swoof_root = root;
27
+ ${actionScript}
28
+ return { root, hostRoot: window.__swoof.root, globals, FRAME_SCRIPTS, SPRITE_FRAME_SCRIPTS, displayList, window, hostStage: Stage, rootInitializers,
29
+ dispatchClipEvent: _enterFrameAll, runClipScript, nativeRegisterClass: Object.registerClass };
30
+ });
31
+ `, { console });
32
+ }
33
+
34
+ describe('shared AVM1 player', () => {
35
+ it.each([5, 8])('keeps a SWF %i callback receiver and falls back to its defining sprite for missing bare reads', version => {
36
+ function push(name) {
37
+ const data = [0, ...Buffer.from(name), 0];
38
+ return [OP.Push, data.length, 0, ...data];
39
+ }
40
+ const names = ['ownerValue', 'receiverValue', 'rootValue', 'globalValue', 'undefinedValue'];
41
+ const handler = names.flatMap(name => [
42
+ ...push('this'), OP.GetVariable, ...push('read_' + name), ...push(name), OP.GetVariable, OP.SetMember,
43
+ ]);
44
+ handler.push(OP.End);
45
+ const header = [0, 0, 0, 1, 0, 0, handler.length & 255, handler.length >> 8];
46
+ const action = Uint8Array.of(
47
+ ...push('button'), OP.GetVariable, ...push('onRelease'),
48
+ OP.DefineFunction2, header.length, 0, ...header, ...handler, OP.SetMember, OP.End,
49
+ );
50
+ const { fullJs } = transpileActionScript([{ type: 'Sprite', characterId: 20, tags: [{ code: TAG.DoAction, data: action }] }],
51
+ new DiagnosticsCollector(), { swfVersion: version, sharedAvm1Runtime: true });
52
+ const { root, globals, SPRITE_FRAME_SCRIPTS, runClipScript } = buildMovieFactory(fullJs)(1, version);
53
+ const menu = root.createEmptyMovieClip('menu', 1);
54
+ const button = menu.createEmptyMovieClip('button', 1);
55
+ Object.assign(menu, Object.fromEntries(names.map(name => [name, 'owner'])));
56
+ button.receiverValue = 'receiver';
57
+ button.undefinedValue = undefined;
58
+ root.rootValue = 'root';
59
+ globals.globalValue = 'global';
60
+ runClipScript(menu, SPRITE_FRAME_SCRIPTS[20][0][0]);
61
+ runClipScript(button, button.onRelease);
62
+ expect(button.read_ownerValue).toBe(version >= 6 ? 'owner' : undefined);
63
+ expect(button.read_receiverValue).toBe('receiver');
64
+ expect(button.read_rootValue).toBe('root');
65
+ expect(button.read_globalValue).toBe('global');
66
+ expect(button.read_undefinedValue).toBeUndefined();
67
+ expect(menu.read_ownerValue).toBeUndefined();
68
+ });
69
+
70
+ it.each([5, 8])('resolves existing timeline writes from ordinary plain-object methods in SWF %i', version => {
71
+ const pushString = value => withPush(withString(value));
72
+ const define = (params, body) => {
73
+ const header = [0, params.length, 0, ...params.flatMap(name => [...Buffer.from(name), 0]),
74
+ body.length & 255, body.length >> 8];
75
+ return [OP.DefineFunction, header.length, 0, ...header, ...body];
76
+ };
77
+ const callChoose = (receiver, value) => [
78
+ ...withPush(withString(value), withNumber(1)), ...withGet(receiver), ...pushString('choose'), OP.CallMethod,
79
+ ];
80
+ const choose = define(['pick'], [
81
+ ...pushString('item'), ...withGet('pick'), OP.SetVariable,
82
+ ...withPush(withString('owned'), withString('updated')), OP.SetVariable,
83
+ ...withPush(withString('newBare'), withString('receiver')), OP.SetVariable,
84
+ ...withGet('this'), ...withPush(withString('explicit'), withString('changed')), OP.SetMember,
85
+ ...withPush(withString('pick'), withString('formal')), OP.SetVariable,
86
+ ...withGet('pick'), OP.Return, OP.End,
87
+ ]);
88
+ const ctor = define(['chooser'], [
89
+ ...withGet('this'), ...pushString('choose'), ...withGet('chooser'), OP.SetMember,
90
+ ...callChoose('this', 'constructed'), OP.Pop, OP.End,
91
+ ]);
92
+ const movie = compileWithFrame([
93
+ ...withPush(withString('listener'), withNumber(0)), OP.InitObject, OP.SetVariable,
94
+ ...withGet('listener'), ...withPush(withString('owned'), withString('initial')), OP.SetMember,
95
+ ...withGet('listener'), ...pushString('choose'), ...choose, OP.SetMember,
96
+ ...pushString('answer'), ...callChoose('listener', 'Tickle'), OP.SetVariable,
97
+ ...pushString('ItemChooser'), ...ctor, OP.SetVariable,
98
+ ...withGet('ItemChooser'), ...pushString('prototype'), ...withGet('Object'), ...pushString('prototype'), OP.GetMember, OP.SetMember,
99
+ ...pushString('constructed'), ...withGet('listener'), ...pushString('choose'), OP.GetMember,
100
+ ...withPush(withNumber(1), withString('ItemChooser')), OP.NewObject, OP.SetVariable,
101
+ ], version);
102
+ const { root, runClipScript, FRAME_SCRIPTS } = movie;
103
+ const menu = root.createEmptyMovieClip('menu', 1);
104
+ root.item = 'root';
105
+ Object.assign(menu, { item: 'Open Hand', owned: 'timeline', explicit: 'unchanged', pick: 'timeline' });
106
+ runClipScript(menu, FRAME_SCRIPTS[0][0]);
107
+ expect(root.item).toBe('root');
108
+ expect(menu.item).toBe(version >= 6 ? 'Tickle' : 'Open Hand');
109
+ expect(menu).toMatchObject({ owned: 'timeline', explicit: 'unchanged', pick: 'timeline', answer: 'formal' });
110
+ expect(menu.listener).toMatchObject({ owned: 'updated', newBare: 'receiver', explicit: 'changed' });
111
+ expect(menu.listener.item).toBe(version >= 6 ? undefined : 'Tickle');
112
+ expect(menu.newBare).toBeUndefined();
113
+ expect(menu.listener.pick).toBeUndefined();
114
+ expect(menu.constructed.item).toBe('constructed');
115
+ });
116
+
117
+ it('reports original ActionThrow values from timeline scripts and direct clip callbacks', () => {
118
+ const bytecode = Uint8Array.of(0x96, 8, 0, 0, ...Buffer.from('thrown'), 0, 0x1c, 0x2a, 0);
119
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }],
120
+ new DiagnosticsCollector(), { sharedAvm1Runtime: true });
121
+ const warnings = [];
122
+ const { root, FRAME_SCRIPTS, runClipScript, dispatchClipEvent } =
123
+ buildMovieFactory(fullJs, { warn: (...args) => warnings.push(args) })(1, 7);
124
+ const script = FRAME_SCRIPTS[0][0];
125
+ root.onEnterFrame = script;
126
+
127
+ const values = [undefined, null, false, 0, '', { message: 'explicit value' }];
128
+ for (const value of values) {
129
+ root.thrown = value;
130
+ runClipScript(root, script);
131
+ }
132
+ expect(warnings).toHaveLength(values.length);
133
+ values.forEach((value, index) => {
134
+ expect(warnings[index][0]).toBe('swoof: script error');
135
+ expect(warnings[index][1]).toBe(value);
136
+ });
137
+ dispatchClipEvent();
138
+ expect(warnings[6][0]).toBe('swoof: onEnterFrame');
139
+ expect(warnings[6][1]).toBe(root.thrown);
140
+
141
+ const hostError = new TypeError('host getter failed');
142
+ Object.defineProperty(root, 'thrown', { get() { throw hostError; } });
143
+ runClipScript(root, script);
144
+ expect(warnings[7][0]).toBe('swoof: script error');
145
+ expect(warnings[7][1]).toBe(hostError);
146
+ });
147
+
148
+ it('delivers frame and mouse broadcasts once to every attached clip in a dense movie', () => {
149
+ const script = transpileActionScript([{ type: 'DoAction', bytecode: Uint8Array.of(0) }], new DiagnosticsCollector(), {
150
+ sharedAvm1Runtime: true,
151
+ });
152
+ const { root, dispatchClipEvent } = buildMovieFactory(script.fullJs)(1, 10);
153
+ const frameOrder = [], mouseOrder = [];
154
+ for (let i = 0; i < 150; i++) {
155
+ const clip = root.createEmptyMovieClip('tile' + i, i + 1);
156
+ clip.onEnterFrame = () => frameOrder.push(i);
157
+ clip.onMouseDown = () => mouseOrder.push(i);
158
+ }
159
+ root.tile149.removeMovieClip();
160
+ const attached = Array.from({ length: 149 }, (_, i) => i);
161
+ dispatchClipEvent();
162
+ dispatchClipEvent('onMouseDown');
163
+ expect(frameOrder).toEqual(attached);
164
+ expect(mouseOrder).toEqual(attached);
165
+ });
166
+
167
+ it('instantiates isolated movies and preserves the host root hooks', () => {
168
+ const bytecode = Uint8Array.of(7, 0); // Stop
169
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }],
170
+ new DiagnosticsCollector(), { sharedAvm1Runtime: true });
171
+ const bootMovie = buildMovieFactory(fullJs);
172
+ const first = bootMovie(9);
173
+ const second = bootMovie(12);
174
+
175
+ expect(first.root).toBe(first.hostRoot);
176
+ expect(second.root).toBe(second.hostRoot);
177
+ first.FRAME_SCRIPTS[0][0].call(first.root);
178
+ expect(first.window.stopped).toBe(true);
179
+ expect(second.window.stopped).toBeUndefined();
180
+ expect(first.nativeRegisterClass).toBeUndefined();
181
+
182
+ function LinkedClip() { this.linked = true; }
183
+ first.globals.Object.registerClass('sharedName', LinkedClip);
184
+ const firstChild = first.root.attachMovie('sharedName', 'copy', 3);
185
+ const secondChild = second.root.attachMovie('sharedName', 'copy', 3);
186
+ expect(firstChild.linked).toBe(true);
187
+ expect(firstChild._swoofCharacterId).toBe(9);
188
+ expect(firstChild.parent).toBe(first.root);
189
+ expect(secondChild._swoofCharacterId).toBe(12);
190
+ expect(secondChild.parent).toBe(second.root);
191
+ first.root.attachMovie(9, 'art', 5);
192
+ expect(first.displayList[5].characterId).toBe(9);
193
+ expect(second.displayList[5]).toBeUndefined();
194
+ expect(second.displayList[3].characterId).toBe(12);
195
+ });
196
+
197
+ it('keeps registered symbol classes separate from authored global bindings', () => {
198
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode: Uint8Array.of(0) }],
199
+ new DiagnosticsCollector(), { sharedAvm1Runtime: true });
200
+ const bootMovie = buildMovieFactory(fullJs);
201
+ const first = bootMovie(9, 7);
202
+ const second = bootMovie(12, 7);
203
+ const namespace = { Child() {} };
204
+ function LinkedClip() { this.version = 1; }
205
+ function ReplacementClip() { this.version = 2; }
206
+
207
+ first.globals.sharedName = namespace;
208
+ first.globals.Object.registerClass('sharedName', LinkedClip);
209
+ expect(first.globals.sharedName).toBe(namespace);
210
+ expect(first.root.attachMovie('sharedName', 'first', 1).version).toBe(1);
211
+
212
+ second.globals.Object.registerClass('sharedName', LinkedClip);
213
+ expect(second.globals.sharedName).toBe(LinkedClip);
214
+ second.globals.Object.registerClass('sharedName', ReplacementClip);
215
+ expect(second.globals.sharedName).toBe(ReplacementClip);
216
+ second.globals.sharedName = undefined;
217
+ second.globals.Object.registerClass('sharedName', LinkedClip);
218
+ expect(second.globals.sharedName).toBeUndefined();
219
+ expect(second.root.attachMovie('sharedName', 'second', 1).version).toBe(1);
220
+ expect(first.globals.sharedName).toBe(namespace);
221
+ });
222
+
223
+ it('runs root library initializers before sprite frame scripts and preserves script order', () => {
224
+ const setClipFlag = (name) => {
225
+ const text = Array.from(name, character => character.charCodeAt(0));
226
+ return Uint8Array.of(0x96, text.length + 4, 0, 0, ...text, 0, 5, 1, 0x1d, 0);
227
+ };
228
+ const { fullJs } = transpileActionScript([
229
+ { type: 'DoInitAction', spriteId: 7, bytecode: setClipFlag('initialized') },
230
+ { type: 'Sprite', characterId: 7, tags: [
231
+ { code: TAG.ShowFrame },
232
+ { code: TAG.DoAction, data: setClipFlag('firstScript') },
233
+ { code: TAG.DoAction, data: setClipFlag('secondScript') },
234
+ ] },
235
+ ], new DiagnosticsCollector(), { sharedAvm1Runtime: true });
236
+ expect(fullJs).not.toContain('var _ss');
237
+ const movie = buildMovieFactory(fullJs)(1);
238
+ expect(movie.root.initialized).toBeUndefined();
239
+ expect(movie.rootInitializers).toEqual([[0, expect.any(Function)]]);
240
+ movie.runClipScript(movie.root, movie.rootInitializers[0][1]);
241
+ expect(movie.root.initialized).toBe(true);
242
+ const clip = movie.root.createEmptyMovieClip('authoredInstance', 1);
243
+ expect(clip.initialized).toBeUndefined();
244
+ expect(clip.firstScript).toBeUndefined();
245
+ const [first, second] = movie.SPRITE_FRAME_SCRIPTS[7][1];
246
+ first.call(clip);
247
+ expect(clip.firstScript).toBe(true);
248
+ expect(clip.secondScript).toBeUndefined();
249
+ second.call(clip);
250
+ expect(clip.secondScript).toBe(true);
251
+ });
252
+
253
+ it('binds duplicate keys and load handlers in authored order at frame entry', () => {
254
+ const stop = Uint8Array.of(7, 0);
255
+ const { fullJs } = transpileActionScript([
256
+ { type: 'ShowFrame' },
257
+ { type: 'PlaceObject2', name: 'listener', characterId: 9, clipActions: [
258
+ { eventFlags: 0x20000 | 0x01 | 0x200 | 0x40000, keyCode: 65, bytecode: stop },
259
+ { eventFlags: 0x20000 | 0x01, keyCode: 65, bytecode: stop },
260
+ ] },
261
+ ], new DiagnosticsCollector(), { sharedAvm1Runtime: true });
262
+ expect(fullJs).toContain('_avm1ClipAction(null, 1, "listener", 9,');
263
+ expect(fullJs).not.toContain('mc.onLoad = ');
264
+ const movie = buildMovieFactory(fullJs)(1);
265
+ const events = [];
266
+ const clip = movie.root.createEmptyMovieClip('listener', 4);
267
+ clip.stop = function () { events.push(this); };
268
+ const [first, second] = movie.FRAME_SCRIPTS[1];
269
+ expect(events).toEqual([]);
270
+
271
+ first();
272
+ const original = clip.onLoad;
273
+ expect(clip._swoofSpriteId).toBe(9);
274
+ expect(clip.onInitialize).toBe(original);
275
+ expect(clip.onConstruct).toBe(original);
276
+ expect(clip._swoofKeyPressHandlers['65:1:0']).toBe(original);
277
+ expect(events).toEqual([clip, clip, clip]);
278
+
279
+ second();
280
+ expect(clip.onLoad).not.toBe(original);
281
+ expect(clip._swoofKeyPressHandlers['65:1:1']).toBe(clip.onLoad);
282
+ expect(events).toEqual([clip, clip, clip, clip]);
283
+
284
+ first();
285
+ expect(clip._swoofKeyPressHandlers['65:1:0']).not.toBe(original);
286
+ expect(Object.keys(clip._swoofKeyPressHandlers)).toEqual(['65:1:0', '65:1:1']);
287
+ });
288
+
289
+ it('keeps the runaway budget per script invocation', () => {
290
+ const bytecode = Uint8Array.of(
291
+ 0x96, 6, 0, 0, 116, 97, 107, 101, 0, // Push "take"
292
+ 0x1c, // GetVariable
293
+ 0x9d, 2, 0, 1, 0, // If to offset 16
294
+ 0x00, // End on the false path
295
+ 0x99, 2, 0, 0xfb, 0xff, // Jump back to itself when taken
296
+ );
297
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }],
298
+ new DiagnosticsCollector(), { sharedAvm1Runtime: true });
299
+ expect(fullJs).toContain('var _avm1CheckBudget = _avm1BudgetGuard();');
300
+ expect(fullJs).not.toContain('execution limit exceeded');
301
+ const { FRAME_SCRIPTS } = buildMovieFactory(fullJs)(1);
302
+ const script = FRAME_SCRIPTS[0][0];
303
+ expect(() => script.call({ take: true })).toThrow(/bytecode offset 0x10/);
304
+ expect(() => script.call({ take: false })).not.toThrow();
305
+ });
306
+ });
307
+
308
+ it('selects an implicit timeline without changing the activation receiver, stack, or locals', () => {
309
+ const strings = (...values) => values.flatMap(value => [0, ...Buffer.from(value), 0]);
310
+ const number = value => [7, value, 0, 0, 0];
311
+ const action = (opcode, data) => [opcode, data.length, 0, ...data];
312
+ const push = (...values) => action(0x96, values.flat());
313
+ const bytecode = Uint8Array.from([
314
+ ...push(strings('local'), number(7)), 0x3c, // DefineLocal
315
+ ...push(strings('copied', 'score')), 0x1c, // Evaluate GetVariable before switching.
316
+ ...action(0x8b, [...Buffer.from('first'), 0]), 0x1d, // SetTarget immediate, SetVariable
317
+ ...push(strings('local'), number(8)), 0x1d, // Keep local writes local.
318
+ ...push(strings('localCopy', 'local')), 0x1c, 0x1d,
319
+ ...push(number(12)), ...action(0x87, [1]), 0x17, // StoreRegister, Pop
320
+ ...push(strings('this')), 0x1c, ...push(strings('original'), number(9)), 0x4f,
321
+ ...push(strings(''), number(0), number(11)), 0x23, // SetProperty _x on the active clip.
322
+ ...push(strings('second')), 0x20, // Relative SetTarget2 starts at the original clip.
323
+ ...action(0x81, [2, 0]), // GotoFrame
324
+ ...push(strings('registerCopy'), [4, 1]), 0x1d,
325
+ ...push(strings('targetLocal'), number(19)), 0x3c,
326
+ ...push(strings('missing')), 0x20, 0x07, // Invalid clip makes Stop a no-op.
327
+ ...action(0x8b, [0]), 0x07, // Restore original clip and Stop.
328
+ 0,
329
+ ]);
330
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }],
331
+ new DiagnosticsCollector(), { sharedAvm1Runtime: true });
332
+ const movie = buildMovieFactory(fullJs)(1);
333
+ const gotos = [];
334
+ let rootStops = 0;
335
+ movie.root.stop = () => rootStops++;
336
+ movie.root.score = 22;
337
+ movie.root.first = { gotoAndStop() {}, score: 33 };
338
+ movie.root.second = { gotoAndStop(frame) { gotos.push(frame); } };
339
+
340
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
341
+
342
+ expect(movie.root.first).toMatchObject({ copied: 22, localCopy: 8, x: 11 });
343
+ expect(Object.keys(movie.root.first)).not.toContain('local');
344
+ expect(movie.root.local).toBe(8);
345
+ expect(movie.root.original).toBe(9);
346
+ expect(gotos).toEqual([3]);
347
+ expect(movie.root.second.registerCopy).toBe(12);
348
+ expect(movie.root.second.targetLocal).toBe(19);
349
+ expect(rootStops).toBe(1);
350
+ expect('missing' in movie.root).toBe(false);
351
+ });
352
+
353
+ const withString = text => [0, ...Buffer.from(text), 0];
354
+ const withNumber = value => [7, value, 0, 0, 0];
355
+ const withPush = (...values) => {
356
+ const payload = values.flat();
357
+ return [0x96, payload.length, 0, ...payload];
358
+ };
359
+ const withGet = name => [...withPush(withString(name)), 0x1c];
360
+ const withSet = (name, value) => [...withPush(withString(name), withNumber(value)), 0x1d];
361
+
362
+ it('reads AVM1 Stage dimensions from each movie instead of the shared AS3 Stage class', () => {
363
+ const readDimension = (property, result) => [
364
+ ...withPush(withString(result)), ...withGet('Stage'),
365
+ ...withPush(withString(property)), OP.GetMember, OP.SetVariable,
366
+ ];
367
+ const bytecode = Uint8Array.from([
368
+ ...readDimension('width', 'stageWidth'), ...readDimension('height', 'stageHeight'), OP.End,
369
+ ]);
370
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }], new DiagnosticsCollector(), {
371
+ sharedAvm1Runtime: true, swfVersion: 7,
372
+ });
373
+ const bootMovie = buildMovieFactory(fullJs);
374
+ const first = bootMovie(1, 7, 550, 400);
375
+ const second = bootMovie(1, 7, 320, 240);
376
+ for (const movie of [first, second]) movie.FRAME_SCRIPTS[0][0].call(movie.root);
377
+
378
+ expect(first.root).toMatchObject({ stageWidth: 550, stageHeight: 400 });
379
+ expect(second.root).toMatchObject({ stageWidth: 320, stageHeight: 240 });
380
+ expect(first.globals.Stage).not.toBe(second.globals.Stage);
381
+ expect(first.hostStage).toBe(second.hostStage);
382
+ expect(first.hostStage.width).toBeUndefined();
383
+ expect(first.hostStage.height).toBeUndefined();
384
+ });
385
+
386
+ it('resolves an unqualified package in an AVM1 constructor with a plain object receiver', () => {
387
+ const body = [
388
+ ...withPush(withNumber(0)), ...withGet('com'),
389
+ ...withPush(withString('encoders')), 0x4e, // GetMember
390
+ ...withPush(withString('BaseN')), 0x53, 0x3e, // NewMethod, Return
391
+ ];
392
+ const header = [...Buffer.from('LoadEncoder'), 0, 0, 0, body.length, 0];
393
+ const bytecode = Uint8Array.from([0x9b, header.length, 0, ...header, ...body, 0]);
394
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }], new DiagnosticsCollector(), {
395
+ sharedAvm1Runtime: true, swfVersion: 10,
396
+ });
397
+ const movie = buildMovieFactory(fullJs)(1, 10);
398
+ class BaseN {}
399
+ class LocalEncoder {}
400
+ movie.globals.com = { encoders: { BaseN } };
401
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
402
+ expect(new movie.root.LoadEncoder()).toBeInstanceOf(BaseN);
403
+ expect(movie.root.LoadEncoder.call({ com: { encoders: { BaseN: LocalEncoder } } })).toBeInstanceOf(LocalEncoder);
404
+ });
405
+ const withBlock = body => [0x94, 2, 0, body.length & 255, body.length >> 8, ...body];
406
+
407
+ function compileWithFrame(bytes, swfVersion) {
408
+ const bytecode = Uint8Array.from([...bytes, 0]);
409
+ const diagnostics = new DiagnosticsCollector();
410
+ const { fullJs } = transpileActionScript([{ type: 'DoAction', bytecode }], diagnostics, { sharedAvm1Runtime: true, swfVersion });
411
+ expect(diagnostics.errors()).toEqual([]);
412
+ return buildMovieFactory(fullJs)(1, swfVersion);
413
+ }
414
+
415
+ it.each([5, 6, 8])('supplies the native enabled default for SWF %i MovieClips', swfVersion => {
416
+ const readMember = (result, object, key) => [
417
+ ...withPush(withString(result)), ...withGet(object), ...withPush(withString(key)), OP.GetMember, OP.SetVariable,
418
+ ];
419
+ const movie = compileWithFrame([
420
+ ...withPush(withString('bareEnabled')), ...withGet('enabled'), OP.SetVariable,
421
+ ...readMember('clipEnabled', 'control', swfVersion === 6 ? 'EnAbLeD' : 'enabled'),
422
+ ...readMember('plainEnabled', 'data', 'enabled'),
423
+ ], swfVersion);
424
+ const { root, globals, FRAME_SCRIPTS } = movie;
425
+ root.control = root.createEmptyMovieClip('control', 1);
426
+ root.data = {};
427
+ const run = () => FRAME_SCRIPTS[0][0].call(root);
428
+ run();
429
+ const expected = swfVersion >= 6 ? true : undefined;
430
+ expect([root.bareEnabled, root.clipEnabled, root.plainEnabled]).toEqual([expected, expected, undefined]);
431
+ expect(new globals.MovieClip().enabled).toBe(expected);
432
+ expect(Object.keys(root.control)).not.toContain('enabled');
433
+
434
+ if (swfVersion >= 6) {
435
+ root.control.enabled = false;
436
+ run();
437
+ expect(root.clipEnabled).toBe(false);
438
+ delete root.control.enabled;
439
+ Object.setPrototypeOf(root.control, { get enabled() { return false; } });
440
+ run();
441
+ expect(root.clipEnabled).toBe(false);
442
+ }
443
+ });
444
+
445
+ it('installs accessors on an ordinary AVM1 class prototype through addProperty', () => {
446
+ const load = () => compileWithFrame([
447
+ ...withPush(withString('method')), ...withGet('modelPrototype'), ...withPush(withString('addProperty')), 0x4e, 0x1d,
448
+ ...withPush(withString('installed')), ...withGet('setter'), ...withGet('getter'),
449
+ ...withPush(withString('level'), withNumber(3)), ...withGet('modelPrototype'), ...withPush(withString('addProperty')), 0x52, 0x1d,
450
+ ...withGet('model'), ...withPush(withString('level'), withNumber(9)), 0x4f, // SetMember
451
+ ], 8);
452
+ function initialize(movie, modelPrototype, defaultLevel) {
453
+ const model = Object.create(modelPrototype);
454
+ Object.assign(movie.root, {
455
+ modelPrototype, model,
456
+ getter() { return this.savedLevel ?? defaultLevel; },
457
+ setter(value) { this.savedLevel = value; },
458
+ });
459
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
460
+ return model;
461
+ }
462
+ const first = load();
463
+ const firstPrototype = {};
464
+ const firstModel = initialize(first, firstPrototype, 8);
465
+ expect(first.root.installed).toBe(true);
466
+ expect(typeof first.root.method).toBe('function');
467
+ expect(firstModel.level).toBe(9);
468
+ expect(firstModel.savedLevel).toBe(9);
469
+ expect(Object.create(firstPrototype).level).toBe(8);
470
+ expect('addProperty' in firstPrototype).toBe(false);
471
+
472
+ const second = load();
473
+ const secondPrototype = {};
474
+ initialize(second, secondPrototype, 4);
475
+ expect(Object.create(secondPrototype).level).toBe(4);
476
+ expect(second.root.method).not.toBe(first.root.method);
477
+ const override = () => 'own method';
478
+ initialize(second, { addProperty: override }, 4);
479
+ expect(second.root.method).toBe(override);
480
+ expect(second.root.installed).toBe('own method');
481
+ initialize(second, { addProperty: undefined }, 4);
482
+ expect(second.root.method).toBeUndefined();
483
+ expect(second.root.installed).toBeUndefined();
484
+ });
485
+
486
+ it.each([6, 7])('keeps inherited component accessors distinct from display properties in SWF %i', version => {
487
+ const fn = (params, body) => {
488
+ const header = [0, params.length, 0, ...params.flatMap(name => [...Buffer.from(name), 0]), body.length, 0];
489
+ return [OP.DefineFunction, header.length, 0, ...header, ...body];
490
+ };
491
+ const getter = fn([], [...withGet('this'), ...withPush(withString('_xscale')), OP.GetMember, OP.Return]);
492
+ const setter = fn(['value'], [
493
+ ...withGet('this'), ...withPush(withString('_xscale')), ...withGet('value'), OP.SetMember,
494
+ ...withPush([3]), OP.Return,
495
+ ]);
496
+ const movie = compileWithFrame([
497
+ ...withPush(withString('installed')), ...setter, ...getter,
498
+ ...withPush(withString('scaleX'), withNumber(3)), ...withGet('modelPrototype'),
499
+ ...withPush(withString('addProperty')), OP.CallMethod, OP.SetVariable,
500
+ ...withGet('first'), ...withPush(withString(version < 7 ? 'SCALEX' : 'scaleX'), withNumber(75)), OP.SetMember,
501
+ ...withGet('second'), ...withPush(withString('_xscale'), withNumber(60)), OP.SetMember,
502
+ ...withPush(withString('firstRead')), ...withGet('first'), ...withPush(withString('scaleX')), OP.GetMember, OP.SetVariable,
503
+ ...withPush(withString('secondRead')), ...withGet('second'), ...withPush(withString('scaleX')), OP.GetMember, OP.SetVariable,
504
+ ], version);
505
+ const { root, globals } = movie;
506
+ root.modelPrototype = new globals.MovieClip();
507
+ const first = root.createEmptyMovieClip('first', 1);
508
+ const second = root.createEmptyMovieClip('second', 2);
509
+ Object.setPrototypeOf(first, root.modelPrototype);
510
+ Object.setPrototypeOf(second, root.modelPrototype);
511
+ movie.FRAME_SCRIPTS[0][0].call(root);
512
+
513
+ expect(root.installed).toBe(true);
514
+ expect([first._xscale, second._xscale, root.modelPrototype._xscale]).toEqual([75, 60, undefined]);
515
+ expect([first.scaleX, second.scaleX, root.modelPrototype.scaleX]).toEqual([75, 60, 100]);
516
+ expect([root.firstRead, root.secondRead]).toEqual([75, 60]);
517
+ });
518
+
519
+ it('persists root and sprite frame declarations across actions and keeps each instance independent', () => {
520
+ const declare = Uint8Array.from([
521
+ ...withPush(withString('count'), withNumber(7)), 0x3c,
522
+ ...withGet('bindingName'), ...withPush(withNumber(25)), 0x3c,
523
+ ...withPush(withString('saved')), 0x41,
524
+ ...withPush(withString('empty')), 0x41,
525
+ 0,
526
+ ]);
527
+ const next = Uint8Array.from([
528
+ ...withPush(withString('count')), ...withGet('count'), 0x50, 0x1d,
529
+ ...withPush(withString('observed')), ...withGet('calculated'), 0x1d,
530
+ 0,
531
+ ]);
532
+ const tags = [
533
+ { type: 'DoAction', bytecode: declare }, { type: 'ShowFrame' }, { type: 'DoAction', bytecode: next },
534
+ { type: 'Sprite', characterId: 7, tags: [
535
+ { code: TAG.DoAction, data: declare }, { code: TAG.ShowFrame }, { code: TAG.DoAction, data: next },
536
+ ] },
537
+ ];
538
+ const { fullJs } = transpileActionScript(tags, new DiagnosticsCollector(), { sharedAvm1Runtime: true, swfVersion: 8 });
539
+ const { root, FRAME_SCRIPTS, SPRITE_FRAME_SCRIPTS } = buildMovieFactory(fullJs)(7, 8);
540
+ const first = root.createEmptyMovieClip('first', 1), second = root.createEmptyMovieClip('second', 2);
541
+ for (const clip of [root, first, second]) Object.assign(clip, { bindingName: 'calculated', saved: 9 });
542
+
543
+ FRAME_SCRIPTS[0][0].call(root);
544
+ SPRITE_FRAME_SCRIPTS[7][0][0].call(first);
545
+ SPRITE_FRAME_SCRIPTS[7][0][0].call(second);
546
+ for (const clip of [root, first, second]) {
547
+ expect(clip).toMatchObject({ count: 7, calculated: 25, saved: 9 });
548
+ expect(Object.hasOwn(clip, 'empty')).toBe(true);
549
+ expect(clip.empty).toBeUndefined();
550
+ }
551
+
552
+ FRAME_SCRIPTS[1][0].call(root);
553
+ SPRITE_FRAME_SCRIPTS[7][1][0].call(first);
554
+ expect(root).toMatchObject({ count: 8, observed: 25 });
555
+ expect(first).toMatchObject({ count: 8, observed: 25 });
556
+ expect(second.count).toBe(7);
557
+ expect(second.observed).toBeUndefined();
558
+ });
559
+
560
+ it('shares an authored clip load declaration with later events and keeps explicit function locals separate', () => {
561
+ const functionBody = [
562
+ ...withPush(withString('speed'), withNumber(100)), 0x3c,
563
+ ...withGet('speed'), 0x3e,
564
+ ];
565
+ const functionHeader = [...Buffer.from('readScratch'), 0, 0, 0, functionBody.length, 0];
566
+ const onLoad = Uint8Array.from([
567
+ ...withPush(withString('speed'), withNumber(7)), 0x3c,
568
+ 0x9b, functionHeader.length, 0, ...functionHeader, ...functionBody,
569
+ 0,
570
+ ]);
571
+ const onEnterFrame = Uint8Array.from([
572
+ ...withPush(withString('speed')), ...withGet('speed'), 0x50, 0x1d, 0,
573
+ ]);
574
+ const tags = [{ type: 'PlaceObject2', name: 'controller', characterId: 9, clipActions: [
575
+ { eventFlags: 1, bytecode: onLoad }, { eventFlags: 2, bytecode: onEnterFrame },
576
+ ] }];
577
+ const { fullJs } = transpileActionScript(tags, new DiagnosticsCollector(), { sharedAvm1Runtime: true, swfVersion: 8 });
578
+ const { root, FRAME_SCRIPTS } = buildMovieFactory(fullJs)(9, 8);
579
+ const controller = root.createEmptyMovieClip('controller', 4);
580
+ for (const action of FRAME_SCRIPTS[0]) action.call(root);
581
+ expect(controller.speed).toBe(7);
582
+ expect(controller.readScratch()).toBe(100);
583
+ expect(controller.speed).toBe(7);
584
+ controller.onEnterFrame();
585
+ expect(controller.speed).toBe(8);
586
+ expect(root.speed).toBeUndefined();
587
+ });
588
+
589
+ it('retains nested With objects after exit in SWF 6 while keeping the invocation receiver', () => {
590
+ const body = [
591
+ ...withGet('key'), ...withGet('key'), 0x1c, 0x50, 0x1d, // Increment by computed name.
592
+ ...withGet('this'), ...withPush(withString('outerValue')), ...withGet('label'), 0x4f,
593
+ ...withGet('this'), ...withPush(withString('parameterValue')), ...withGet('key'), 0x4f,
594
+ ...withGet('score'), 0x3e,
595
+ ];
596
+ const header = [0, 1, 0, ...Buffer.from('key'), 0, body.length, 0];
597
+ const define = [0x9b, header.length, 0, ...header, ...body];
598
+ for (const version of [5, 6, 7]) {
599
+ const movie = compileWithFrame([
600
+ ...withGet('outer'), ...withBlock([
601
+ ...withGet('inner'), ...withBlock([
602
+ ...withGet('this'), ...withPush(withString('callback')), ...define, 0x4f,
603
+ ]),
604
+ ]),
605
+ ], version);
606
+ const inner = { score: 10, key: 'wrong' };
607
+ movie.root.outer = { label: 17, score: 50, inner };
608
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
609
+ const receiver = { score: 30, label: 40, callback: movie.root.callback };
610
+ expect(receiver.callback('score')).toBe(version >= 6 ? 11 : 31);
611
+ expect(receiver.outerValue).toBe(version >= 6 ? 17 : 40);
612
+ expect(receiver.parameterValue).toBe('score');
613
+ expect(receiver.score).toBe(version >= 6 ? 30 : 31);
614
+ expect(inner.score).toBe(version >= 6 ? 11 : 10);
615
+ expect(movie.root.outer.score).toBe(50);
616
+ }
617
+ });
618
+
619
+ it('resolves SetTarget2 from the defining clip of a captured With closure', () => {
620
+ const body = [
621
+ ...withPush(withString('child')), 0x20,
622
+ ...withSet('score', 9), ...withSet('targetOnly', 12),
623
+ ...withPush(withString('localOnly'), withNumber(13)), 0x3c,
624
+ ...withSet('localOnly', 17),
625
+ ...withGet('this'), ...withPush(withString('explicit'), withNumber(7)), 0x4f,
626
+ ...withGet('this'), ...withPush(withString('observed')), ...withGet('localOnly'), 0x4f,
627
+ ...withPush(withString('')), 0x20,
628
+ ...withSet('baseOnly', 15),
629
+ ];
630
+ const header = [0, 0, 0, body.length, 0];
631
+ for (const version of [5, 6, 7]) {
632
+ const movie = compileWithFrame([
633
+ ...withGet('scope'), ...withBlock([
634
+ ...withGet('this'), ...withPush(withString('callback')),
635
+ 0x9b, header.length, 0, ...header, ...body, 0x4f,
636
+ ]),
637
+ ], version);
638
+ const definingClip = movie.root.createEmptyMovieClip('defining', 2);
639
+ const receiver = movie.root.createEmptyMovieClip('receiver', 3);
640
+ const authoredChild = definingClip.createEmptyMovieClip('child', 1);
641
+ const receiverChild = receiver.createEmptyMovieClip('child', 1);
642
+ definingClip.scope = { score: 1 };
643
+ authoredChild.score = 2;
644
+ authoredChild.localOnly = 33;
645
+ receiverChild.score = 3;
646
+ movie.FRAME_SCRIPTS[0][0].call(definingClip);
647
+ definingClip.callback.call(receiver);
648
+ expect(receiver.explicit).toBe(7);
649
+ expect(receiver.observed).toBe(17);
650
+ expect(authoredChild.localOnly).toBe(33);
651
+ expect(definingClip.scope.score).toBe(version >= 6 ? 9 : 1);
652
+ expect(authoredChild.score).toBe(2);
653
+ expect(receiverChild.score).toBe(version >= 6 ? 3 : 9);
654
+ expect(authoredChild.targetOnly).toBe(version >= 6 ? 12 : undefined);
655
+ expect(receiverChild.targetOnly).toBe(version >= 6 ? undefined : 12);
656
+ expect(definingClip.baseOnly).toBe(version >= 6 ? 15 : undefined);
657
+ expect(receiver.baseOnly).toBe(version >= 6 ? undefined : 15);
658
+ }
659
+ });
660
+
661
+ it('keeps Number aliases, boxes, prototype methods and constants movie-local', () => {
662
+ const bytes = [
663
+ ...withPush(withString('Alias')), ...withGet('Number'), 0x1d,
664
+ ...withPush(withString('mixedCaseRead')), ...withGet('nUmBeR'), 0x1d,
665
+ ...withPush(withString('directResult'), [3], withNumber(1), withString('Number')), 0x3d, 0x1d,
666
+ ...withPush(withString('aliasResult'), [3], withNumber(1), withString('Alias')), 0x3d, 0x1d,
667
+ ...withPush(withString('defaultResult'), withNumber(0), withString('Alias')), 0x3d, 0x1d,
668
+ ...withPush(withString('boxed'), withString('077'), withNumber(1), withString('Number')), 0x40, 0x1d,
669
+ ...withPush(withString('aliasBox'), withString('077'), withNumber(1)), ...withGet('Alias'), 0x40, 0x1d,
670
+ ...withPush(withString('unboxed'), withNumber(0)), ...withGet('aliasBox'), ...withPush(withString('valueOf')), 0x52, 0x1d,
671
+ ...withPush(withString('formatted'), withNumber(0)), ...withGet('aliasBox'), ...withPush(withString('toString')), 0x52, 0x1d,
672
+ ...withPush(withString('constructorRead')), ...withGet('aliasBox'), ...withPush(withString('constructor')), 0x4e, 0x1d,
673
+ ...withPush(withString('instance')), ...withGet('boxed'), ...withGet('Alias'), 0x54, 0x1d,
674
+ ...withPush(withString('primitiveInstance'), withNumber(1)), ...withGet('Alias'), 0x54, 0x1d,
675
+ ...withPush(withString('cast')), ...withGet('Alias'), ...withGet('aliasBox'), 0x2b, 0x1d,
676
+ ...withPush(withString('constant')), ...withGet('Alias'), ...withPush(withString('MAX_VALUE')), 0x4e, 0x1d,
677
+ ];
678
+ const movies = [5, 6, 7].map(version => compileWithFrame(bytes, version));
679
+ for (const [index, movie] of movies.entries()) {
680
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
681
+ const expected = index === 0 ? 77 : 63;
682
+ expect(movie.root.Alias).toBe(movie.globals.Number);
683
+ expect(movie.root.mixedCaseRead).toBe(index < 2 ? movie.globals.Number : undefined);
684
+ expect(movie.root).toMatchObject({
685
+ directResult: index < 2 ? 0 : NaN, aliasResult: index < 2 ? 0 : NaN,
686
+ defaultResult: 0, unboxed: expected, formatted: String(expected), instance: true,
687
+ primitiveInstance: false, constant: Number.MAX_VALUE,
688
+ });
689
+ expect(movie.root.cast).toBe(movie.root.aliasBox);
690
+ expect(movie.root.constructorRead).toBe(movie.root.Alias);
691
+ expect(movie.root.aliasBox.constructor).toBe(movie.root.Alias);
692
+ expect(Object.getPrototypeOf(movie.root.boxed)).toBe(movie.root.Alias.prototype);
693
+ expect(Object.getOwnPropertyDescriptor(movie.root.Alias, 'MAX_VALUE')).toMatchObject({
694
+ writable: false, configurable: false, enumerable: false,
695
+ });
696
+ }
697
+ movies[0].root.Alias.prototype.movieOnly = 42;
698
+ expect(movies[0].root.aliasBox.movieOnly).toBe(42);
699
+ expect(movies[1].root.aliasBox.movieOnly).toBeUndefined();
700
+ });
701
+
702
+ it('resolves existing mixed-case clip, with-scope, member and local names only through SWF 6', () => {
703
+ const script = [
704
+ ...withSet('Score', 6), ...withSet('SCORE', 9),
705
+ ...withPush(withString('scoreValue')), ...withGet('score'), 0x1d,
706
+ ...withPush(withString('Count'), withNumber(3)), 0x3c,
707
+ ...withSet('COUNT', 4),
708
+ ...withPush(withString('localValue')), ...withGet('count'), 0x1d,
709
+ ...withGet('data'), ...withPush(withString('vaLue'), withNumber(8)), 0x4f,
710
+ ...withPush(withString('memberValue')), ...withGet('data'), ...withPush(withString('VALUE')), 0x4e, 0x1d,
711
+ ];
712
+ const scoped = [
713
+ ...withGet('data'),
714
+ ...withBlock([
715
+ ...withSet('CoNfIgUrEd', 7),
716
+ ...withPush(withString('scopeResult')), ...withGet('configured'), 0x1d,
717
+ ]),
718
+ ];
719
+ for (const version of [6, 7]) {
720
+ const movie = compileWithFrame(script, version);
721
+ movie.root.data = { Value: 1 };
722
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
723
+ const withMovie = compileWithFrame(scoped, version);
724
+ withMovie.root.data = { Configured: 1 };
725
+ withMovie.FRAME_SCRIPTS[0][0].call(withMovie.root);
726
+ if (version === 6) {
727
+ expect(movie.root).toMatchObject({ Score: 9, scoreValue: 9, localValue: 4, memberValue: 8 });
728
+ expect(movie.root.data).toEqual({ Value: 8 });
729
+ expect(withMovie.root.scopeResult).toBe(7);
730
+ expect(withMovie.root.data).toEqual({ Configured: 7 });
731
+ } else {
732
+ expect(movie.root).toMatchObject({ Score: 6, SCORE: 9, COUNT: 4 });
733
+ expect(movie.root.scoreValue).toBeUndefined();
734
+ expect(movie.root.localValue).toBeUndefined();
735
+ expect(movie.root.memberValue).toBeUndefined();
736
+ expect(movie.root.data).toEqual({ Value: 1, vaLue: 8 });
737
+ expect(withMovie.root.scopeResult).toBeUndefined();
738
+ expect(withMovie.root.data).toEqual({ Configured: 1 });
739
+ }
740
+ }
741
+ });
742
+
743
+ it('reuses the first statically known spelling across legacy frames without changing string values', () => {
744
+ const tags = [
745
+ { type: 'DoAction', bytecode: Uint8Array.from([...withSet('ghBVal', 162), 0]) },
746
+ { type: 'ShowFrame' },
747
+ { type: 'DoAction', bytecode: Uint8Array.from([
748
+ ...withGet('scope'),
749
+ ...withBlock([
750
+ ...withPush(withString('duration')), ...withGet('ghBval'), 0x1d,
751
+ ...withPush(withString('caption'), withString('ghBval')), 0x1d,
752
+ ]), 0,
753
+ ]) },
754
+ ];
755
+ for (const swfVersion of [6, 7]) {
756
+ const { fullJs } = transpileActionScript(tags, new DiagnosticsCollector(), { sharedAvm1Runtime: true, swfVersion });
757
+ expect(fullJs).toContain(`_avm1GetWithVariable(_avm1With, _avm1Locals, "${swfVersion === 6 ? 'ghBVal' : 'ghBval'}"`);
758
+ const movie = buildMovieFactory(fullJs)(1, swfVersion);
759
+ movie.root.scope = {};
760
+ for (const scripts of movie.FRAME_SCRIPTS) scripts?.forEach(script => script.call(movie.root));
761
+ expect(movie.root.caption).toBe('ghBval');
762
+ expect(movie.root.duration).toBe(swfVersion === 6 ? 162 : undefined);
763
+ }
764
+ });
765
+
766
+ it('deletes own AVM1 members by static and dynamic name with the movie’s casing rules', () => {
767
+ for (const version of [6, 7]) {
768
+ const movie = compileWithFrame([
769
+ ...withGet('data'), ...withPush(withString('Caption'), withNumber(1)), 0x4f,
770
+ ...withPush(withString('deletedStatic')), ...withGet('data'), ...withPush(withString('CAPTION')), 0x3a, 0x1d,
771
+ ...withPush(withString('deletedDynamic')), ...withGet('data'), ...withGet('key'), 0x3a, 0x1d,
772
+ ...withPush(withString('deletedInherited')), ...withGet('data'), ...withPush(withString('Inherited')), 0x3a, 0x1d,
773
+ ...withPush(withString('deletedFixed')), ...withGet('data'), ...withPush(withString('Hard')), 0x3a, 0x1d,
774
+ ], version);
775
+ const prototype = { Inherited: 1 };
776
+ movie.root.data = Object.assign(Object.create(prototype), { Value: 2 });
777
+ Object.defineProperty(movie.root.data, 'Hard', { value: 3, configurable: false });
778
+ movie.root.key = 'vAlUe';
779
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
780
+ const legacy = version === 6;
781
+ expect(movie.root).toMatchObject({ deletedStatic: legacy, deletedDynamic: legacy, deletedInherited: false, deletedFixed: false });
782
+ expect(Object.keys(movie.root.data)).toEqual(legacy ? [] : ['Value', 'Caption']);
783
+ expect(prototype.Inherited).toBe(1);
784
+ expect(movie.root.data.Hard).toBe(3);
785
+ }
786
+ });
787
+
788
+ it('consumes Delete2 results and searches With, local and timeline bindings in order', () => {
789
+ for (const version of [6, 7]) {
790
+ const movie = compileWithFrame([
791
+ ...withPush(withString('Local'), withNumber(4)), 0x3c,
792
+ ...withGet('scope'),
793
+ ...withBlock([
794
+ ...withPush(withString('deletedWith')), ...withGet('key'), 0x3b, 0x1d,
795
+ ]),
796
+ ...withPush(withString('deletedLocal'), withString('LOCAL')), 0x3b, 0x1d,
797
+ ...withPush(withString('currentLocal')), ...withGet('Local'), 0x1d,
798
+ ...withPush(withString('deletedClip'), withString('FLAG')), 0x3b, 0x1d,
799
+ ...withPush(withString('deletedAgain'), withString('FLAG')), 0x3b, 0x1d,
800
+ ], version);
801
+ movie.root.scope = { Flag: 3 };
802
+ movie.root.Flag = 1;
803
+ movie.root.Local = 9;
804
+ movie.root.key = 'fLaG';
805
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
806
+ const legacy = version === 6;
807
+ expect(movie.root).toMatchObject({
808
+ deletedWith: legacy, deletedLocal: legacy, currentLocal: legacy ? undefined : 4, deletedClip: legacy, deletedAgain: false,
809
+ });
810
+ expect(Object.keys(movie.root.scope)).toEqual(legacy ? [] : ['Flag']);
811
+ expect(movie.root.Flag).toBe(legacy ? undefined : 1);
812
+ }
813
+ });
814
+
815
+ it('deletes a persistent frame declaration without With', () => {
816
+ const movie = compileWithFrame([
817
+ ...withPush(withString('Local'), withNumber(4)), 0x3c,
818
+ ...withPush(withString('deleted'), withString('LOCAL')), 0x3b, 0x1d,
819
+ ...withPush(withString('value')), ...withGet('Local'), 0x1d,
820
+ ], 6);
821
+ movie.root.Local = 9;
822
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
823
+ expect(movie.root).toMatchObject({ deleted: true, value: undefined });
824
+ expect(Object.hasOwn(movie.root, 'Local')).toBe(false);
825
+ });
826
+
827
+ it('stores the evaluated operand once and leaves it on the AVM1 stack', () => {
828
+ const movie = compileWithFrame([
829
+ ...withPush(withString('stackValue')), ...withGet('sample'),
830
+ 0x87, 1, 0, 1, // StoreRegister 1
831
+ 0x1d, // SetVariable consumes the same value.
832
+ ...withPush(withString('registerValue'), [4, 1]), 0x1d,
833
+ ]);
834
+ let reads = 0;
835
+ Object.defineProperty(movie.root, 'sample', { get: () => ++reads });
836
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
837
+ expect(reads).toBe(1);
838
+ expect(movie.root).toMatchObject({ stackValue: 1, registerValue: 1 });
839
+ });
840
+
841
+ const extraOperand = withPush(withNumber(99));
842
+ const inconsistentJoin = [
843
+ ...withPush(withNumber(0), [5, 1]), // Push 0 and true.
844
+ 0x9d, 2, 0, extraOperand.length, 0, ...extraOperand, 0x17, // True skips the extra operand.
845
+ ];
846
+
847
+ it('constructs named and computed AVM1 classes through both operand stacks', () => {
848
+ const body = [
849
+ ...withGet('this'), ...withPush(withString('value')), ...withGet('seed'), 0x4f,
850
+ ...withPush(withString('discarded'), [5, 1], withNumber(1)), 0x43, 0x3e, // Return an object.
851
+ ];
852
+ const header = [...Buffer.from('Authored'), 0, 1, 0, ...Buffer.from('seed'), 0, body.length, 0];
853
+ const authored = [0x9b, header.length, 0, ...header, ...body];
854
+ const factoryBody = [
855
+ ...withPush(withString('parameter'), withNumber(15), withNumber(2), withString('factory')),
856
+ 0x40, 0x3e,
857
+ ];
858
+ const factoryHeader = [...Buffer.from('Build'), 0, 1, 0, ...Buffer.from('factory'), 0, factoryBody.length, 0];
859
+ for (const runtimeStack of [false, true]) {
860
+ const count = runtimeStack ? withGet('count') : withPush(withNumber(2));
861
+ const movie = compileWithFrame([
862
+ ...authored,
863
+ 0x9b, factoryHeader.length, 0, ...factoryHeader, ...factoryBody,
864
+ ...withPush(withString('LocalAlias')), ...withGet('Authored'), 0x3c,
865
+ ...withPush(withString('localResult'), withNumber(7), withNumber(1), withString('LocalAlias')), 0x40, 0x1d,
866
+ ...withPush(withString('NamedNumber')), ...withGet('Number'), 0x1d,
867
+ ...withPush(withString('aliasResult'), withString('077'), withNumber(1), withString('NamedNumber')), 0x40, 0x1d,
868
+ ...withPush(withString('targetResult'), withString('target'), withNumber(8)), ...count, ...withGet('className'), 0x40, 0x1d,
869
+ ...withPush(withString('movieResult'), withString('movie'), withNumber(9)), ...count, ...withPush(withString('MovieOnly')), 0x40, 0x1d,
870
+ ...withPush(withString('pkg')), ...withGet('library'), 0x3c,
871
+ ...withPush(withString('localPackage'), withString('local package'), withNumber(10)), ...count, ...withPush(withString('pkg.Native')), 0x40, 0x1d,
872
+ ...withPush(withString('globalPackage'), withString('global package'), withNumber(11)), ...count, ...withPush(withString('sdk.Tool')), 0x40, 0x1d,
873
+ ...withGet('scope'), ...withBlock([
874
+ ...withPush(withString('withResult'), withString('with'), withNumber(12)), ...count, ...withPush(withString('Scoped')), 0x40, 0x1d,
875
+ ]),
876
+ ...withPush(withString('methodResult'), withString('method'), withNumber(13)), ...count,
877
+ ...withGet('library'), ...withGet('methodName'), 0x53, 0x1d,
878
+ ...withPush(withString('methodAlias'), withNumber(14), withNumber(1)), ...withGet('Authored'),
879
+ ...withPush([3]), 0x53, 0x1d,
880
+ ...withPush(withString('missingObject'), withNumber(0), withString('MissingClass')), 0x40, 0x1d,
881
+ ...withPush(withString('missingReceiver'), withNumber(0), [3], withString('Native')), 0x53, 0x1d,
882
+ ...withSet('continued', 1),
883
+ ], 6);
884
+ const clip = movie.root.createEmptyMovieClip('scopeTest', 2);
885
+ function Native(value, label) { return { value, label }; }
886
+ clip.TargetCtor = Native;
887
+ clip.library = { Native };
888
+ clip.className = 'targetctor';
889
+ clip.methodName = 'nATIVE';
890
+ clip.count = 2;
891
+ clip.scope = { Scoped: Native };
892
+ movie.root.MovieOnly = Native;
893
+ movie.globals.sdk = { Tool: Native };
894
+ const script = movie.FRAME_SCRIPTS[0][0];
895
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
896
+ script.call(clip);
897
+ expect(clip.localResult).toBeInstanceOf(clip.Authored);
898
+ expect(clip.localResult).toEqual({ value: 7 });
899
+ expect(clip.Build(Native)).toEqual({ value: 15, label: 'parameter' });
900
+ expect(clip.aliasResult.valueOf()).toBe(63);
901
+ expect(clip).toMatchObject({
902
+ targetResult: { value: 8, label: 'target' }, movieResult: { value: 9, label: 'movie' },
903
+ localPackage: { value: 10, label: 'local package' }, globalPackage: { value: 11, label: 'global package' },
904
+ withResult: { value: 12, label: 'with' }, methodResult: { value: 13, label: 'method' },
905
+ methodAlias: { value: 14 }, missingObject: undefined, missingReceiver: undefined, continued: 1,
906
+ });
907
+ }
908
+ });
909
+
910
+ it.each([4, 5, 6, 7])('coerces AVM1 branch and logical operands in SWF %i', version => {
911
+ for (const runtimeStack of [false, true]) {
912
+ const fallthrough = [...withPush(withString('fellThrough'), [5, 1]), 0x1d];
913
+ const right = [...withPush(withNumber(0), withString('right')), 0x3d];
914
+ const movie = compileWithFrame([
915
+ ...(runtimeStack ? inconsistentJoin : []),
916
+ ...withGet('input'), 0x9d, 2, 0, fallthrough.length, 0, ...fallthrough,
917
+ ...withPush(withString('notResult')), ...withGet('input'), 0x12, 0x1d,
918
+ ...withPush(withString('andResult')), ...withGet('input'), ...right, 0x10, 0x1d,
919
+ ...withPush(withString('orResult')), ...withGet('input'), ...right, 0x11, 0x1d,
920
+ ], version);
921
+ const script = movie.FRAME_SCRIPTS[0][0];
922
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
923
+ let rightReads = 0;
924
+ movie.root.right = () => { rightReads++; return '2'; };
925
+ const values = [['0', false], ['text', false], ['2', true], ['', false], [null, false], [NaN, false], [{}, true]];
926
+ for (const [input, legacyTruthy] of values) {
927
+ movie.root.input = input;
928
+ movie.root.fellThrough = false;
929
+ script.call(movie.root);
930
+ const truthy = version >= 7 && typeof input === 'string' ? Boolean(input) : legacyTruthy;
931
+ expect(movie.root.fellThrough).toBe(!truthy);
932
+ expect(movie.root.notResult).toBe(version === 4 ? Number(!truthy) : !truthy);
933
+ expect(movie.root.andResult).toBe(version === 4 ? Number(truthy) : truthy);
934
+ expect(movie.root.orResult).toBe(version === 4 ? 1 : true);
935
+ }
936
+ expect(rightReads).toBe(values.length * 2);
937
+ }
938
+ });
939
+
940
+ it.each([5, 6, 7, 8, 9, 10])('coerces AVM1 bitwise operands in SWF %i', version => {
941
+ for (const runtimeStack of [false, true]) {
942
+ const movie = compileWithFrame([
943
+ ...(runtimeStack ? inconsistentJoin : []),
944
+ ...withPush(withString('integer')), ...withGet('word'), 0x18, 0x1d,
945
+ ...withPush(withString('mask')), ...withGet('left'), ...withGet('right'), 0x60, 0x1d,
946
+ ...withPush(withString('or')), ...withGet('word'), ...withPush(withNumber(1)), 0x61, 0x1d,
947
+ ...withPush(withString('xor')), ...withGet('word'), ...withPush(withNumber(1)), 0x62, 0x1d,
948
+ ...withPush(withString('shift')), ...withGet('word'), ...withPush(withNumber(33)), 0x63, 0x1d,
949
+ ...withPush(withString('signed')), ...withGet('negativeFive'), ...withPush(withNumber(1)), 0x64, 0x1d,
950
+ ...withPush(withString('unsigned')), ...withGet('negativeOne'), ...withPush(withNumber(32)), 0x65, 0x1d,
951
+ ], version);
952
+ const script = movie.FRAME_SCRIPTS[0][0];
953
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
954
+ const order = [];
955
+ Object.assign(movie.root, {
956
+ word: '010', negativeFive: -5, negativeOne: -1,
957
+ left: { valueOf() { order.push('left'); return 2 ** 32 + 7.9; } },
958
+ right: { valueOf() { order.push('right'); return '003'; } },
959
+ });
960
+ script.call(movie.root);
961
+ const integer = version === 5 ? 10 : 8;
962
+ expect(movie.root).toMatchObject({
963
+ integer, mask: 3, or: integer | 1, xor: integer ^ 1, shift: integer * 2,
964
+ signed: -3, unsigned: version === 8 || version === 9 ? -1 : 0xffffffff,
965
+ });
966
+ expect(order).toEqual(['right', 'left']);
967
+ }
968
+ });
969
+
970
+ it.each([false, true])('retains pending values through SetMember with runtime stack %s', runtimeStack => {
971
+ const fixedRead = [...withGet('this'), ...withPush(withString('score')), 0x4e];
972
+ const dynamicRead = [...withGet('record'), ...withGet('key'), 0x4e];
973
+ const movie = compileWithFrame([
974
+ ...(runtimeStack ? inconsistentJoin : []),
975
+ ...withPush(withString('beforeFixed')), ...fixedRead,
976
+ ...withGet('this'), ...withPush(withString('score'), withNumber(9)), 0x4f, 0x1d,
977
+ ...withPush(withString('beforeDynamic')), ...dynamicRead,
978
+ ...withGet('record'), ...withGet('key'), ...withPush(withNumber(15)), 0x4f, 0x1d,
979
+ ], 7);
980
+ const script = movie.FRAME_SCRIPTS[0][0];
981
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
982
+ let current = 5;
983
+ const events = [];
984
+ Object.assign(movie.root, {
985
+ score: 3, key: 'value',
986
+ record: {
987
+ get value() { events.push(['read', current]); return current; },
988
+ set value(value) { events.push(['write', value]); current = value; },
989
+ },
990
+ });
991
+ script.call(movie.root);
992
+ expect(movie.root).toMatchObject({ beforeFixed: 3, score: 9, beforeDynamic: 5 });
993
+ expect(events).toEqual([['read', 5], ['write', 15]]);
994
+ });
995
+
996
+ it.each([false, true])('resolves unqualified clip-action globals with runtime stack %s', runtimeStack => {
997
+ const bytecode = Uint8Array.from([
998
+ ...(runtimeStack ? inconsistentJoin : []),
999
+ ...withPush(withString('result'), withNumber(0)),
1000
+ ...withGet('RacePhysics'), ...withPush(withString('update')), 0x52, 0x1d,
1001
+ ...withPush(withString('computedResult'), withNumber(0)),
1002
+ ...withGet('helperName'), 0x1c, ...withPush(withString('update')), 0x52, 0x1d,
1003
+ 0,
1004
+ ]);
1005
+ const { fullJs } = transpileActionScript([{
1006
+ type: 'PlaceObject2', name: 'controller', characterId: 9,
1007
+ clipActions: [{ eventFlags: 2, bytecode }],
1008
+ }], new DiagnosticsCollector(), { sharedAvm1Runtime: true, swfVersion: 7 });
1009
+ expect(fullJs.includes('var _avm1Stack')).toBe(runtimeStack);
1010
+ const movie = buildMovieFactory(fullJs)(1, 7);
1011
+ const clip = movie.root.createEmptyMovieClip('controller', 3);
1012
+ const globalPhysics = { calls: 0, update() { return ++this.calls; } };
1013
+ movie.globals.RacePhysics = globalPhysics;
1014
+ clip.helperName = 'RacePhysics';
1015
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1016
+
1017
+ movie.dispatchClipEvent();
1018
+ expect([clip.result, clip.computedResult, globalPhysics.calls]).toEqual([1, 2, 2]);
1019
+
1020
+ clip.RacePhysics = { calls: 10, update: globalPhysics.update };
1021
+ movie.dispatchClipEvent();
1022
+ expect([clip.result, clip.computedResult, globalPhysics.calls]).toEqual([11, 12, 2]);
1023
+ });
1024
+
1025
+ it.each([5, 6, 7])('preserves pending SetProperty reads and rejects invalid slots in SWF %i', version => {
1026
+ for (const runtimeStack of [false, true]) {
1027
+ const write = (index, value) => [...withPush(withString(''), index, withNumber(value)), 0x23];
1028
+ const movie = compileWithFrame([
1029
+ ...(runtimeStack ? inconsistentJoin : []),
1030
+ ...withPush(withString('beforeX'), withString(''), withNumber(0)), 0x22,
1031
+ ...write(withNumber(0), 9), 0x1d,
1032
+ ...withPush(withString('_root')), ...withGet('computedSlot'), ...withPush(withNumber(15)), 0x23,
1033
+ ...write(withString('-0.5'), 11),
1034
+ ...write(withString('custom'), 77),
1035
+ ...write(withString('4294967296'), 78),
1036
+ ...write(withString('-1'), 79),
1037
+ ...write(withNumber(4), 99),
1038
+ ...write(withString('5.5'), 99),
1039
+ ...write(withString('021'), 1),
1040
+ ], version);
1041
+ const script = movie.FRAME_SCRIPTS[0][0];
1042
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
1043
+ const conversions = [];
1044
+ Object.assign(movie.root, {
1045
+ x: 3, y: 5, custom: 6, focusRect: false,
1046
+ currentFrame: 3, totalFrames: 5,
1047
+ computedSlot: { valueOf() { conversions.push('index'); return '1.9'; } },
1048
+ });
1049
+ script.call(movie.root);
1050
+ expect(movie.root).toMatchObject({ beforeX: 3, x: 11, y: 15, custom: 6, currentFrame: 3, totalFrames: 5 });
1051
+ expect(Boolean(movie.root.focusRect)).toBe(version >= 6);
1052
+ expect(conversions).toEqual(['index']);
1053
+ }
1054
+ });
1055
+
1056
+ it.each([4, 5, 6, 7])('reads only indexed display properties in SWF %i', version => {
1057
+ for (const runtimeStack of [false, true]) {
1058
+ const readSlot = (result, index, target = '') => [
1059
+ ...withPush(withString(result), withString(target)), ...index, 0x22, 0x1d,
1060
+ ];
1061
+ const literalSlot = index => withPush(withNumber(index));
1062
+ const textSlot = value => withPush(withString(value));
1063
+ const movie = compileWithFrame([
1064
+ ...(runtimeStack ? inconsistentJoin : []),
1065
+ ...readSlot('position', literalSlot(0)),
1066
+ ...readSlot('childPosition', textSlot('0.9'), 'child'),
1067
+ ...readSlot('fraction', textSlot('-0.5')),
1068
+ ...readSlot('coerced', withGet('computedSlot'), '_root'),
1069
+ ...readSlot('badNegative', textSlot('-1')),
1070
+ ...readSlot('badLarge', textSlot('4294967296')),
1071
+ ...readSlot('customResult', textSlot('custom')),
1072
+ ...readSlot('missingTarget', literalSlot(0), 'missing'),
1073
+ ...readSlot('qualityResult', literalSlot(19)),
1074
+ ...withPush(withString(''), withNumber(19), withString('HIGH')), 0x23,
1075
+ ...readSlot('updatedQuality', literalSlot(19)),
1076
+ ], version);
1077
+ const script = movie.FRAME_SCRIPTS[0][0];
1078
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
1079
+ const conversions = [];
1080
+ Object.assign(movie.root, {
1081
+ x: 3, y: 5, custom: 7, quality: 'LOW',
1082
+ computedSlot: { valueOf() { conversions.push('index'); return '1.9'; } },
1083
+ });
1084
+ const child = movie.root.createEmptyMovieClip('child', 1);
1085
+ child.x = 8;
1086
+ script.call(movie.root);
1087
+ expect(movie.root).toMatchObject({
1088
+ position: 3, childPosition: 8, fraction: 3, coerced: 5,
1089
+ badNegative: 0, badLarge: 0, customResult: version === 4 ? 3 : 0, missingTarget: 0,
1090
+ qualityResult: version === 4 ? 0 : 'LOW',
1091
+ updatedQuality: version === 4 ? 0 : 'HIGH',
1092
+ });
1093
+ expect(movie.root.quality).toBe(version === 4 ? 'LOW' : 'HIGH');
1094
+ expect(conversions).toEqual(['index']);
1095
+ }
1096
+ });
1097
+
1098
+ it('retains the shared RandomNumber call for a literal bound', () => {
1099
+ const movie = compileWithFrame([
1100
+ ...withPush(withString('randomValue'), withNumber(1)), 0x30, 0x1d,
1101
+ ], 6);
1102
+ const script = movie.FRAME_SCRIPTS[0][0];
1103
+ script.call(movie.root);
1104
+ expect(movie.root.randomValue).toBe(0);
1105
+ });
1106
+
1107
+ it('applies Flash numeric coercion to missing operands in both AVM1 stack paths', () => {
1108
+ for (const swfVersion of [6, 7]) {
1109
+ for (const runtimeStack of [false, true]) {
1110
+ const movie = compileWithFrame([
1111
+ ...(runtimeStack ? inconsistentJoin : []),
1112
+ ...withPush(withString('missingNumber')), ...withGet('unsetValue'), 0x4a, 0x1d, // ToNumber
1113
+ ...withPush(withString('nullNumber'), [2]), 0x4a, 0x1d,
1114
+ ...withPush(withString('incremented')), ...withGet('unsetValue'), 0x50, 0x1d, // Increment
1115
+ ...withPush(withString('decremented'), [2]), 0x51, 0x1d, // Decrement
1116
+ ...withPush(withString('subtracted'), withNumber(8)), ...withGet('unsetValue'), 0x0b, 0x1d,
1117
+ ...withPush(withString('multiplied'), [2], withNumber(8)), 0x0c, 0x1d,
1118
+ ...withPush(withString('divided'), [2], withNumber(8)), 0x0d, 0x1d,
1119
+ ...withPush(withString('remainder'), [2], withNumber(8)), 0x3f, 0x1d,
1120
+ ...withPush(withString('sum'), withString('12'), withString('3')), 0x0a, 0x1d, // ActionAdd
1121
+ ...withPush(withString('float'), [1, 0, 0, 0, 0xbf], withNumber(8)), 0x0c, 0x1d,
1122
+ ], swfVersion);
1123
+ const script = movie.FRAME_SCRIPTS[0][0];
1124
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
1125
+ script.call(movie.root);
1126
+ const results = ['missingNumber', 'nullNumber', 'incremented', 'decremented',
1127
+ 'subtracted', 'multiplied', 'divided', 'remainder'].map(key => movie.root[key]);
1128
+ expect(results).toEqual(swfVersion === 6 ? [0, 0, 1, -1, 8, 0, 0, 0] : Array(8).fill(NaN));
1129
+ expect(movie.root.sum).toBe(15);
1130
+ expect(movie.root.float).toBe(-4);
1131
+ }
1132
+ }
1133
+ });
1134
+
1135
+ it('selects the Add2 operation after AVM1 primitive conversion in both stack paths', () => {
1136
+ for (const swfVersion of [6, 7]) {
1137
+ for (const runtimeStack of [false, true]) {
1138
+ const movie = compileWithFrame([
1139
+ ...(runtimeStack ? inconsistentJoin : []),
1140
+ ...withPush(withString('missingSum'), withNumber(3), [3]), 0x47, 0x1d,
1141
+ ...withPush(withString('nullSum'), [2], withNumber(3)), 0x47, 0x1d,
1142
+ ...withPush(withString('booleanSum'), [5, 1], [5, 1]), 0x47, 0x1d,
1143
+ ...withPush(withString('missingText'), withString('MiXeD:'), [3]), 0x47, 0x1d,
1144
+ ...withPush(withString('nullText'), [2], withString(':UP')), 0x47, 0x1d,
1145
+ ...withPush(withString('boolText'), [5, 0], withString(':UP')), 0x47, 0x1d,
1146
+ ...withPush(withString('objectResult')), ...withGet('left'), ...withGet('right'), 0x47, 0x1d,
1147
+ ...withPush(withString('objectText'), withString('Case:')), ...withGet('fallback'), 0x47, 0x1d,
1148
+ ...withPush(withString('objectNumber'), withNumber(1)), ...withGet('fallback'), 0x47, 0x1d,
1149
+ ...withPush(withString('objectCase'), withNumber(1)), ...withGet('variant'), 0x47, 0x1d,
1150
+ ...withPush(withString('dateResult'), withString('Date:')), ...withGet('date'), 0x47, 0x1d,
1151
+ ], swfVersion);
1152
+ const conversions = [];
1153
+ movie.root.left = { valueOf() { conversions.push('left'); return 4; } };
1154
+ movie.root.right = { valueOf() { conversions.push('right'); return 'Answer'; } };
1155
+ movie.root.fallback = {
1156
+ valueOf() { return this; },
1157
+ toString() { conversions.push('string'); return 'HostCase'; },
1158
+ };
1159
+ movie.root.variant = { VALUEOF() { return 6; } };
1160
+ movie.root.date = new movie.globals.Date(0);
1161
+ movie.root.date.toString = () => 'Epoch';
1162
+ const script = movie.FRAME_SCRIPTS[0][0];
1163
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
1164
+ script.call(movie.root);
1165
+ const legacy = swfVersion === 6;
1166
+ expect(movie.root).toMatchObject({
1167
+ missingSum: legacy ? 3 : NaN, nullSum: legacy ? 3 : NaN, booleanSum: 2,
1168
+ missingText: legacy ? 'MiXeD:' : 'MiXeD:undefined', nullText: 'null:UP', boolText: 'false:UP',
1169
+ objectResult: '4Answer', objectText: 'Case:HostCase', objectNumber: NaN,
1170
+ objectCase: legacy ? 7 : NaN, dateResult: 'Date:Epoch',
1171
+ });
1172
+ expect(conversions).toEqual(['right', 'left', 'string']);
1173
+ }
1174
+ }
1175
+ });
1176
+
1177
+ it('preserves the distinct numeric and abstract comparison results in both AVM1 stack paths', () => {
1178
+ const nan = [1, 0, 0, 0xc0, 0x7f];
1179
+ const oneAndHalf = [1, 0, 0, 0xc0, 0x3f];
1180
+ for (const swfVersion of [6, 7]) {
1181
+ for (const runtimeStack of [false, true]) {
1182
+ const movie = compileWithFrame([
1183
+ ...(runtimeStack ? inconsistentJoin : []),
1184
+ ...withPush(withString('numericLess'), withString('2'), withString('10')), 0x0f, 0x1d,
1185
+ ...withPush(withString('textLess'), withString('2'), withString('10')), 0x48, 0x1d,
1186
+ ...withPush(withString('textGreater'), withString('2'), withString('10')), 0x67, 0x1d,
1187
+ ...withPush(withString('legacyEq'), withString('01'), withString('1')), 0x0e, 0x1d,
1188
+ ...withPush(withString('typedEq'), withString('01'), withString('1')), 0x49, 0x1d,
1189
+ ...withPush(withString('typedCase'), withString('Case'), withString('case')), 0x49, 0x1d,
1190
+ ...withPush(withString('floatEq'), oneAndHalf, withString('1.50')), 0x49, 0x1d,
1191
+ ...withPush(withString('floatGreater'), oneAndHalf, withNumber(1)), 0x67, 0x1d,
1192
+ ...withPush(withString('legacyNaN'), nan, nan), 0x0e, 0x1d,
1193
+ ...withPush(withString('typedNaN'), nan, nan), 0x49, 0x1d,
1194
+ ...withPush(withString('stringNaN'), nan, withString('NaN')), 0x49, 0x1d,
1195
+ ...withPush(withString('legacyUnordered'), nan, withNumber(1)), 0x0f, 0x1d,
1196
+ ...withPush(withString('lessUnordered'), nan, withNumber(1)), 0x48, 0x1d,
1197
+ ...withPush(withString('greaterUnordered'), withNumber(1), nan), 0x67, 0x1d,
1198
+ ...withPush(withString('legacyMissing'), [3], withNumber(0)), 0x0e, 0x1d,
1199
+ ...withPush(withString('typedMissing'), [3], withNumber(0)), 0x49, 0x1d,
1200
+ ...withPush(withString('missingLess'), [2], withNumber(1)), 0x48, 0x1d,
1201
+ ...withPush(withString('objectLess')), ...withGet('object'), ...withPush(withNumber(2)), 0x48, 0x1d,
1202
+ ], swfVersion);
1203
+ movie.root.object = { valueOf() { return this; }, toString() { return '1'; } };
1204
+ const script = movie.FRAME_SCRIPTS[0][0];
1205
+ expect(String(script).includes('var _avm1Stack')).toBe(runtimeStack);
1206
+ script.call(movie.root);
1207
+ expect(movie.root).toMatchObject({
1208
+ numericLess: true, textLess: false, textGreater: true, legacyEq: true, typedEq: false, typedCase: false,
1209
+ floatEq: true, floatGreater: true, legacyNaN: false, typedNaN: true, stringNaN: false,
1210
+ legacyUnordered: false, lessUnordered: undefined, greaterUnordered: undefined,
1211
+ legacyMissing: swfVersion === 6, typedMissing: false, missingLess: swfVersion === 6 ? true : undefined,
1212
+ objectLess: false,
1213
+ });
1214
+ }
1215
+ }
1216
+ });
1217
+
1218
+ it('consumes computed call argument counts from the AVM1 operand stack', () => {
1219
+ const movie = compileWithFrame([
1220
+ ...withPush(withString('methodResult'), withNumber(21)), ...withGet('oneArgument'),
1221
+ ...withPush(withNumber(7), withNumber(1)), ...withGet('package'),
1222
+ ...withPush(withString('TestValue')), 0x53, // NewMethod leaves the outer call's argument and count on the stack.
1223
+ ...withPush(withString('combine')), 0x52, 0x1d,
1224
+ ...withPush(withString('functionResult'), withString('second'), withString('first')),
1225
+ ...withGet('twoArguments'), ...withPush(withString('format')), 0x3d, 0x1d,
1226
+ ]);
1227
+ function TestValue(value) { this.value = value; }
1228
+ TestValue.prototype.combine = function (right) { return this.value + right; };
1229
+ movie.root.package = { TestValue };
1230
+ movie.root.oneArgument = 1;
1231
+ movie.root.twoArguments = 2;
1232
+ movie.root.format = function (first, second) { return `${first}/${second}`; };
1233
+
1234
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1235
+
1236
+ expect(movie.root).toMatchObject({ methodResult: 28, functionResult: 'first/second' });
1237
+ });
1238
+
1239
+ it('preserves a previously pushed register value after an overwrite', () => {
1240
+ const storeRegister = [0x87, 1, 0, 1];
1241
+ for (const dynamic of [false, true]) {
1242
+ const movie = compileWithFrame([
1243
+ // A computed argument count selects the runtime operand stack.
1244
+ ...(dynamic ? [...withGet('argumentCount'), ...withPush(withString('noop')), 0x3d, 0x17] : []),
1245
+ ...withPush(withNumber(3)), ...storeRegister, 0x17,
1246
+ ...withPush(withString('oldValue'), [4, 1], withNumber(9)), ...storeRegister, 0x17, 0x1d,
1247
+ ...withPush(withString('newValue'), [4, 1]), 0x1d,
1248
+ ]);
1249
+ movie.root.argumentCount = 0;
1250
+ movie.root.noop = () => {};
1251
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1252
+ expect(movie.root).toMatchObject({ oldValue: 3, newValue: 9 });
1253
+ }
1254
+ });
1255
+
1256
+ it('peeks at the current enumeration key without consuming it', () => {
1257
+ const movie = compileWithFrame([
1258
+ ...withGet('entries'), 0x55, // Enumerate2
1259
+ 0x87, 1, 0, 1, // StoreRegister 1 peeks at the key
1260
+ ...withPush(withString('peeked')), 0x4d, 0x1d, // StackSwap, SetVariable consumes the key
1261
+ ...withPush(withString('stored'), [4, 1]), 0x1d,
1262
+ ]);
1263
+ movie.root.entries = { item: 1 };
1264
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1265
+ expect(movie.root).toMatchObject({ stored: 'item', peeked: 'item' });
1266
+ });
1267
+
1268
+ it('uses nested with objects for reads, writes and calls without replacing this or locals', () => {
1269
+ const callback = [...withGet('local'), 0x3e];
1270
+ const movie = compileWithFrame([
1271
+ ...withPush(withString('local'), withNumber(7)), 0x3c, // DefineLocal
1272
+ ...withGet('outer'),
1273
+ ...withBlock([
1274
+ ...withPush(withString('called'), withNumber(0), withString('compute')), 0x3d, 0x1d,
1275
+ ...withPush(withString('local'), withNumber(8)), 0x3c, // Outer has its own local property
1276
+ ...withPush(withString('score')), 0x41, // DefineLocal2 keeps an existing property
1277
+ ...withPush(withString('callback')),
1278
+ 0x9b, 5, 0, 0, 0, 0, callback.length, 0, ...callback, 0x1d, // DefineFunction
1279
+
1280
+ ...withGet('inner'),
1281
+ ...withBlock([
1282
+ ...withSet('score', 15), // Found on outer
1283
+ ...withSet('innerValue', 12),
1284
+ ...withPush(withString('saved'), withNumber(30)), 0x3c,
1285
+ ...withGet('this'), ...withPush(withString('explicit'), withNumber(2)), 0x4f,
1286
+ ]),
1287
+ ]),
1288
+ ...withPush(withString('outsideLocal')), ...withGet('local'), 0x1d,
1289
+ ...withPush(withString('savedOutside')), ...withGet('saved'), 0x1d,
1290
+ ]);
1291
+ movie.root.score = 1;
1292
+ movie.root.outer = {
1293
+ score: 10, local: 20, called: 0, callback: null, inner: { innerValue: 0 },
1294
+ compute() { return this.score * 2; },
1295
+ };
1296
+
1297
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1298
+
1299
+ expect(movie.root.outer).toMatchObject({ score: 15, local: 8, called: 20, inner: { innerValue: 12 } });
1300
+ expect(movie.root.score).toBe(1);
1301
+ expect(movie.root.explicit).toBe(2);
1302
+ expect(movie.root.outsideLocal).toBe(7);
1303
+ expect(movie.root.outer.callback()).toBe(7);
1304
+ expect(movie.root.savedOutside).toBe(30);
1305
+ expect('saved' in movie.root.outer.inner).toBe(false);
1306
+ });
1307
+
1308
+ it('skips a null with body and unwinds the timeline on a forward branch out of the body', () => {
1309
+ const afterWith = withSet('continued', 17);
1310
+ const movie = compileWithFrame([
1311
+ ...withPush([2]), ...withBlock(withSet('skipped', 1)),
1312
+ 0x8b, 6, 0, ...Buffer.from('first'), 0,
1313
+ ...withGet('scope'),
1314
+ ...withBlock([
1315
+ 0x8b, 7, 0, ...Buffer.from('second'), 0,
1316
+ 0x81, 2, 0, 1, 0, // GotoFrame 2 on second.
1317
+ 0x99, 2, 0, afterWith.length + 1, 0, // Jump beyond the child body.
1318
+ 0x00, // The child continuation still begins immediately after this.
1319
+ ]),
1320
+ ...afterWith,
1321
+ 0x81, 2, 0, 2, 0, // GotoFrame 3 on restored first.
1322
+ ]);
1323
+ const gotos = [];
1324
+ movie.root.scope = {};
1325
+ movie.root.first = { gotoAndStop(frame) { gotos.push(['first', frame]); } };
1326
+ movie.root.second = { gotoAndStop(frame) { gotos.push(['second', frame]); } };
1327
+
1328
+ movie.FRAME_SCRIPTS[0][0].call(movie.root);
1329
+
1330
+ expect(gotos).toEqual([['second', 2], ['first', 3]]);
1331
+ expect(movie.root.first.continued).toBe(17);
1332
+ expect('skipped' in movie.root).toBe(false);
1333
+ });
1334
+
1335
+ it('propagates an explicit return from within a with body', () => {
1336
+ const movie = compileWithFrame([
1337
+ ...withGet('scope'),
1338
+ ...withBlock([...withPush(withNumber(42)), 0x3e]),
1339
+ ...withSet('fellThrough', 1),
1340
+ ]);
1341
+ movie.root.scope = {};
1342
+ expect(movie.FRAME_SCRIPTS[0][0].call(movie.root)).toBe(42);
1343
+ expect('fellThrough' in movie.root).toBe(false);
1344
+ });
1345
+
1346
+ it('lists only child clips and script variables when for…in enumerates a MovieClip', () => {
1347
+ // Pacman hides its ghosts with `for (g in Ghost) Ghost[g]._visible = false`; listing the
1348
+ // runtime's own members there hid the clip's internal child list, so the ghosts never came back.
1349
+ const body = [...withPush([4, 0], withNumber(1)), ...withGet('keys'), ...withPush(withString('push')), 0x52, 0x17];
1350
+ const test = [0x87, 1, 0, 0, ...withPush([2]), 0x49];
1351
+ const jumpBack = -(test.length + 5 + body.length + 5);
1352
+ const movie = compileWithFrame([
1353
+ ...withGet('holder'), 0x55, // Enumerate2
1354
+ ...test, 0x9d, 2, 0, body.length + 5, 0, // If the key is null, leave the loop
1355
+ ...body, 0x99, 2, 0, jumpBack & 0xff, (jumpBack >> 8) & 0xff,
1356
+ ]);
1357
+ const { root } = movie;
1358
+ root.holder = root.createEmptyMovieClip('holder', 1);
1359
+ root.holder.createEmptyMovieClip('ghost', 1);
1360
+ root.holder.score = 3;
1361
+ root.keys = [];
1362
+ movie.FRAME_SCRIPTS[0][0].call(root);
1363
+ expect([...root.keys].sort()).toEqual(['ghost', 'score']);
1364
+ });