swf2js 0.1.0 → 0.1.2

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 (50) hide show
  1. package/README.md +9 -9
  2. package/package.json +3 -2
  3. package/src/cli.js +15 -6
  4. package/src/emitter/bundle-emitter.js +56 -16
  5. package/src/runtime/avm1-runtime.js +329 -84
  6. package/src/runtime/avm2-runtime.js +56 -6
  7. package/src/runtime/embedded-movie-runtime.js +6 -1
  8. package/src/runtime/index.js +2 -6
  9. package/src/runtime/movie-runtime.js +2 -1
  10. package/src/runtime/swflib/assemble.js +34 -0
  11. package/src/runtime/swflib/bitmap-image.js +4 -0
  12. package/src/runtime/swflib/bitmap.js +1004 -0
  13. package/src/runtime/swflib/builtins.js +156 -0
  14. package/src/runtime/swflib/bytearray.js +949 -0
  15. package/src/runtime/swflib/compiled-swf.js +3 -0
  16. package/src/runtime/swflib/context-menu.js +245 -0
  17. package/src/runtime/swflib/cursor-flag.js +12 -0
  18. package/src/runtime/swflib/dictionary.js +121 -0
  19. package/src/runtime/swflib/display.js +823 -0
  20. package/src/runtime/swflib/events.js +754 -0
  21. package/src/runtime/swflib/filters.js +298 -0
  22. package/src/runtime/swflib/flash.js +130 -0
  23. package/src/runtime/swflib/geom.js +759 -0
  24. package/src/runtime/swflib/glyph-metrics.js +14 -0
  25. package/src/runtime/swflib/graphics.js +735 -0
  26. package/src/runtime/swflib/host.js +2 -0
  27. package/src/runtime/swflib/inherit.js +22 -0
  28. package/src/runtime/swflib/input.js +1085 -0
  29. package/src/runtime/swflib/loader.js +687 -0
  30. package/src/runtime/swflib/marks.js +16 -0
  31. package/src/runtime/swflib/matrix3d.js +144 -0
  32. package/src/runtime/swflib/pointer-bridge.js +8 -0
  33. package/src/runtime/swflib/projection.js +158 -0
  34. package/src/runtime/swflib/render.js +1240 -0
  35. package/src/runtime/swflib/sound.js +1166 -0
  36. package/src/runtime/swflib/sprite-drag.js +116 -0
  37. package/src/runtime/swflib/stage-ref.js +9 -0
  38. package/src/runtime/swflib/stage.js +484 -0
  39. package/src/runtime/swflib/system.js +987 -0
  40. package/src/runtime/swflib/text-field.js +519 -0
  41. package/src/runtime/swflib/text-paint.js +797 -0
  42. package/src/runtime/swflib/text.js +1387 -0
  43. package/src/runtime/swflib/transform.js +133 -0
  44. package/src/runtime/swflib/xml.js +268 -0
  45. package/src/transpiler/avm1/avm1-ir.js +2 -1
  46. package/src/transpiler/avm1/avm1-rewrite.js +23 -2
  47. package/src/transpiler/avm1/avm1-transpiler.js +4 -1
  48. package/src/transpiler/avm2/avm2-transpiler.js +38 -7
  49. package/src/transpiler/timeline/timeline-codegen.js +366 -123
  50. package/src/runtime/swflib.js +0 -14050
@@ -282,7 +282,8 @@ export function hitTestAvm1ShapeGeometry(characters, characterId, point, dynamic
282
282
  (entry.name && instance ? instance[entry.name] : null);
283
283
  if (entry.characterId == null || entry.visible === false ||
284
284
  (entry.clipDepth != null && !child?._swoofTimelineMaskOverridden)) continue;
285
- if (child && child.visible === false) continue;
285
+ // Script hitTest still includes a child that hid itself. Pointer tests opt into visibleOnly.
286
+ if (visibleOnly && child && child.visible === false) continue;
286
287
  var effectiveDepth = child?._swoofSwappedDepth == null ? childDepth : child._swoofSwappedDepth + 16384;
287
288
  if (excludedByMask(effectiveDepth)) continue;
288
289
  var localMatrix = avm1HitLocalMatrix(child, entry.matrix);
@@ -328,6 +329,26 @@ export function dispatchAvm1ClipKeyEvent(rootClip, clips, eventName, keyCode, on
328
329
  invoke = (clip, handler) => handler.call(clip), seen = new Set()) {
329
330
  if (!rootClip || !clips || (eventName !== 'onKeyDown' && eventName !== 'onKeyUp')) return 0;
330
331
  var delivered = 0;
332
+ function avm1ClipDepth(clip) {
333
+ if (clip && typeof clip.getDepth === 'function') {
334
+ var depth = clip.getDepth();
335
+ if (Number.isFinite(depth)) return depth;
336
+ }
337
+ return 0;
338
+ }
339
+ function orderedAvm1Children(clip) {
340
+ var listed = new Set();
341
+ var children = [];
342
+ for (var group of [clip._swoofRootChildren, clip._swoofSpriteChildren, clip._swoofChildren]) {
343
+ for (var child of Object.values(group || {})) {
344
+ if (!child || child.parent !== clip || listed.has(child)) continue;
345
+ listed.add(child);
346
+ children.push(child);
347
+ }
348
+ }
349
+ children.sort(function (a, b) { return avm1ClipDepth(a) - avm1ClipDepth(b) || 0; });
350
+ return children;
351
+ }
331
352
  function visit(clip) {
332
353
  if (!clip || (typeof clip !== 'object' && typeof clip !== 'function') || clip._swoofDetached || seen.has(clip)) return;
333
354
  seen.add(clip);
@@ -354,9 +375,7 @@ export function dispatchAvm1ClipKeyEvent(rootClip, clips, eventName, keyCode, on
354
375
  });
355
376
  }
356
377
  }
357
- for (var children of [clip._swoofRootChildren, clip._swoofSpriteChildren, clip._swoofChildren]) {
358
- for (var child of Object.values(children || {})) if (child.parent === clip) visit(child);
359
- }
378
+ for (var child of orderedAvm1Children(clip)) visit(child);
360
379
  }
361
380
  visit(rootClip);
362
381
  if (!rootClip._swoofRootChildren) {
@@ -406,6 +425,26 @@ var hitTestAvm1ShapeGeometry = ${hitTestAvm1ShapeGeometry.toString()};
406
425
  var hitTestAvm1ClipGeometry = ${hitTestAvm1ClipGeometry.toString()};
407
426
  var _dispatchAvm1ClipKeyEvent = ${dispatchAvm1ClipKeyEvent.toString()};
408
427
  var avm1ClipKeyPressCode = ${avm1KeyPressCode.toString()};
428
+ function avm1ClipDepth(clip) {
429
+ if (clip && typeof clip.getDepth === 'function') {
430
+ var depth = clip.getDepth();
431
+ if (Number.isFinite(depth)) return depth;
432
+ }
433
+ return 0;
434
+ }
435
+ function orderedAvm1Children(clip) {
436
+ var listed = new Set();
437
+ var children = [];
438
+ for (var group of [clip._swoofRootChildren, clip._swoofSpriteChildren, clip._swoofChildren]) {
439
+ for (var child of Object.values(group || {})) {
440
+ if (!child || child.parent !== clip || listed.has(child)) continue;
441
+ listed.add(child);
442
+ children.push(child);
443
+ }
444
+ }
445
+ children.sort(function (a, b) { return avm1ClipDepth(a) - avm1ClipDepth(b) || 0; });
446
+ return children;
447
+ }
409
448
  // swoof AVM1 clip runtime — player APIs come from swflib.js
410
449
  var Color = (typeof As2Color !== 'undefined') ? As2Color : (typeof Color !== 'undefined' ? Color : function () {});
411
450
  function ContextMenu(callback) {
@@ -890,7 +929,7 @@ var avm1FlashPackages = typeof __swoof_swflib !== 'undefined' && __swoof_swflib?
890
929
  var sharedAvm1BitmapData = avm1FlashPackages?.display?.BitmapData;
891
930
  var avm1BitmapRectangle = Symbol.for('swoof.avm1.BitmapData.rectangle');
892
931
  var Avm1BitmapData = sharedAvm1BitmapData && function BitmapData(width, height, transparent, fillColor) {
893
- return sharedAvm1BitmapData.call(this, width, height, transparent, fillColor);
932
+ return Reflect.construct(sharedAvm1BitmapData, [width, height, transparent, fillColor], new.target || BitmapData);
894
933
  };
895
934
  if (Avm1BitmapData) {
896
935
  Avm1BitmapData.prototype = sharedAvm1BitmapData.prototype;
@@ -903,15 +942,16 @@ if (Avm1BitmapData) {
903
942
  });
904
943
  }
905
944
  Avm1BitmapData.loadBitmap = function (linkageId) {
945
+ var movie = typeof _avm1Host !== 'undefined' ? _avm1Host : null;
906
946
  var name = String(linkageId);
907
- var symbols = typeof EXPORT_ASSETS !== 'undefined' ? EXPORT_ASSETS : null;
908
- var symbol = symbols?.find(function (asset) { return asset.name === name; });
909
- var info = symbol && typeof CHARACTERS !== 'undefined' ? CHARACTERS?.[symbol.id] : null;
947
+ var symbol = movie?.exportAssets?.find(function (asset) { return asset.name === name; });
948
+ var info = symbol ? movie.characters?.[symbol.id] : null;
910
949
  if (info?.type !== 'bitmap') return undefined;
911
- var assets = typeof _avm1Host !== 'undefined' ? _avm1Host.loaded : typeof loaded !== 'undefined' ? loaded : null;
912
- var image = assets?.[info.assetId ?? symbol.id];
913
- if (image) return __swoof_swflib.bitmapDataFromImage(image);
950
+ var image = movie.loaded?.[info.assetId ?? symbol.id];
951
+ var fromImage = movie.runtime?.bitmapDataFromImage;
952
+ if (image && fromImage) return fromImage(image);
914
953
  };
954
+ if (typeof _avm1Host !== 'undefined' && _avm1Host) _avm1Host.BitmapData = Avm1BitmapData;
915
955
  }
916
956
  // Keep ActionScript overrides local to this movie. Native rounding takes an
917
957
  // AVM1 Number, so a supplied argument uses the movie's conversion rules.
@@ -990,6 +1030,16 @@ if (typeof STAGE_WIDTH === 'number' && typeof STAGE_HEIGHT === 'number') {
990
1030
  }
991
1031
  }
992
1032
  }
1033
+ function avm1MoviePackages() {
1034
+ var library = avm1FlashPackages && typeof avm1FlashPackages === 'object' ? avm1FlashPackages : null;
1035
+ var swoof = library ? Object.create(library) : {};
1036
+ if (typeof Avm1BitmapData === 'function') {
1037
+ var display = library?.display && typeof library.display === 'object' ? Object.create(library.display) : {};
1038
+ display.BitmapData = Avm1BitmapData;
1039
+ swoof.display = display;
1040
+ }
1041
+ return swoof;
1042
+ }
993
1043
  var globals = {
994
1044
  Math: avm1Math, String: String, Array: Array, Object: Avm1Object, Number: Avm1Number, Boolean: Boolean, Date: Date,
995
1045
  isNaN: function (value) {
@@ -1020,12 +1070,7 @@ var globals = {
1020
1070
  Selection: Selection,
1021
1071
  ASSetPropFlags: ASSetPropFlags,
1022
1072
  mx: {},
1023
- flash: {
1024
- display: { BitmapData: Avm1BitmapData },
1025
- geom: Object.assign({}, avm1FlashPackages?.geom),
1026
- filters: Object.assign({}, avm1FlashPackages?.filters),
1027
- external: { ExternalInterface: avm1FlashPackages?.external?.ExternalInterface },
1028
- },
1073
+ swoof: avm1MoviePackages(),
1029
1074
  Sound: typeof Sound !== 'undefined' ? Sound : function () {
1030
1075
  this._id = null;
1031
1076
  this.attachSound = function (id) { this._id = id; };
@@ -1039,6 +1084,8 @@ var globals = {
1039
1084
  this.play = this.start;
1040
1085
  },
1041
1086
  };
1087
+ // Movies compiled against the flash package still look that name up dynamically.
1088
+ globals.flash = globals.swoof;
1042
1089
  // Do not clobber swflib's AS3 MovieClip on globalThis — AVM1 uses globals.MovieClip.
1043
1090
  try { if (typeof globalThis !== 'undefined') globalThis.ASSetPropFlags = ASSetPropFlags; } catch (_) {}
1044
1091
  var _clips = Object.create(null);
@@ -2095,6 +2142,12 @@ function _avm1MarkBytecodeFunction(fn) {
2095
2142
  avm1BytecodeFunctions.set(fn, _avm1MovieClips.has(definingClip) ? definingClip : null);
2096
2143
  return fn;
2097
2144
  }
2145
+ /** DefineFunction2 preload_arguments — array copy that keeps Flash arguments.callee. */
2146
+ function _avm1PreloadArguments(args, callee) {
2147
+ var copy = Array.prototype.slice.call(args);
2148
+ copy.callee = callee;
2149
+ return copy;
2150
+ }
2098
2151
  function _avm1Construct(ctor, args) {
2099
2152
  if (typeof ctor !== 'function') return undefined;
2100
2153
  if (!avm1BytecodeFunctions.has(ctor)) return Reflect.construct(ctor, args);
@@ -2169,11 +2222,20 @@ var avm1QualityProperties = {
2169
2222
  };
2170
2223
  Object.defineProperties(Avm1MovieClip.prototype, avm1QualityProperties);
2171
2224
 
2225
+ function _avm1PathSegment(clip) {
2226
+ var name = _avm1InstanceName(clip);
2227
+ if (name) return name;
2228
+ // Unnamed timeline/dynamic clips keep a stable depth segment so Selection.getFocus
2229
+ // and relative paths still round-trip after scene changes.
2230
+ var depth = typeof clip.getDepth === 'function' ? clip.getDepth()
2231
+ : (clip._swoofSwappedDepth ?? ((clip._swoofDepth | 0) - (clip._swoofAuthoredAttached ? 16384 : 0)));
2232
+ return Number.isFinite(depth) ? '#' + (depth | 0) : null;
2233
+ }
2172
2234
  function _avm1TargetParts(clip) {
2173
2235
  if (!clip || !(clip === root || clip instanceof Avm1MovieClip || clip._swoofAvm1TextField || typeof clip.gotoAndStop === 'function')) return null;
2174
2236
  var names = [], seen = new Set();
2175
2237
  while (clip !== root) {
2176
- var name = _avm1InstanceName(clip);
2238
+ var name = _avm1PathSegment(clip);
2177
2239
  if (!clip || clip._swoofDetached || !clip.parent || !name || seen.has(clip)) return null;
2178
2240
  seen.add(clip);
2179
2241
  names.push(name);
@@ -2229,6 +2291,7 @@ function _avm1ResolvePath(base, path) {
2229
2291
  var keyword = _avm1PathKeyword(name);
2230
2292
  if (keyword === '_root' || name === 'root' || keyword === '_level0') object = root;
2231
2293
  else if (parentStep || keyword === '_parent' || name === 'parent') object = object.parent;
2294
+ else if (name && name.charAt(0) === '#') object = _avm1ClipAtDepth(object, name.slice(1) | 0) || null;
2232
2295
  else if (name && keyword !== 'this') object = _avm1PathMember(object, name, slash);
2233
2296
  if (separator === '/') slash = true;
2234
2297
  }
@@ -2739,7 +2802,17 @@ function _globalToLocalImpl(mc, pt) {
2739
2802
  return { x: x, y: y };
2740
2803
  }
2741
2804
 
2742
- function _hitTestImpl(mc, a, b, c) {
2805
+ function _avm1RootPointToStage(x, y) {
2806
+ // hitTest(x, y) is in the main timeline's coordinates. Moving _root does not move those points.
2807
+ if (typeof root === 'undefined' || !root) return { x: x, y: y };
2808
+ var m = _clipMatrix(root);
2809
+ return {
2810
+ x: m.a * x + m.c * y + (m.tx || 0) / 20,
2811
+ y: m.b * x + m.d * y + (m.ty || 0) / 20,
2812
+ };
2813
+ }
2814
+
2815
+ function _hitTestImpl(mc, a, b, c, visibleOnly, pointIsStage) {
2743
2816
  // Flash overloads:
2744
2817
  // hitTest(x, y, shapeFlag?)
2745
2818
  // hitTest(target)
@@ -2754,10 +2827,15 @@ function _hitTestImpl(mc, a, b, c) {
2754
2827
  // (flag, y, x) legacy reversed form
2755
2828
  if (typeof a === 'boolean') { shapeFlag = a; y = b; x = c; }
2756
2829
  }
2830
+ if (!pointIsStage && Number.isFinite(+x) && Number.isFinite(+y)) {
2831
+ var stagePoint = _avm1RootPointToStage(+x, +y);
2832
+ x = stagePoint.x;
2833
+ y = stagePoint.y;
2834
+ }
2757
2835
  if (shapeFlag && typeof hitTestAvm1ClipGeometry === 'function') {
2758
2836
  var shapeHit = hitTestAvm1ClipGeometry(
2759
2837
  typeof CHARACTERS !== 'undefined' ? CHARACTERS : null,
2760
- mc, x, y, typeof spritePlay !== 'undefined' ? spritePlay : null
2838
+ mc, x, y, typeof spritePlay !== 'undefined' ? spritePlay : null, null, visibleOnly
2761
2839
  );
2762
2840
  if (shapeHit != null) return shapeHit;
2763
2841
  }
@@ -3011,7 +3089,9 @@ function _attachMovieImpl(parent, id, newName, depth, initObj, cloneSource) {
3011
3089
  }
3012
3090
  if (cloneSource && !child._swoofDetached) _inheritAvm1ClipActions(cloneSource, child);
3013
3091
  if (symbol?.type === 'sprite' && _avm1DisplayEntry(child) && typeof runSpriteFrameScripts === 'function') {
3014
- runSpriteFrameScripts(charId, 0, child);
3092
+ // The caller often assigns id and other fields after attachMovie returns.
3093
+ // Running this frame now would read those names before they exist.
3094
+ _scheduleAttachedSpriteFrame(charId, child);
3015
3095
  }
3016
3096
  if (initialize && symbol?.type === 'sprite' && !child._swoofDetached && typeof _member(child, 'onLoad') === 'function') {
3017
3097
  avm1PendingDynamicLoads.add(child);
@@ -3073,19 +3153,41 @@ function _avm1CloneSprite(base, source, name, depth) {
3073
3153
  }
3074
3154
  }
3075
3155
 
3076
- function _registerAvm1DisplayEntry(mc, entry) {
3077
- if (typeof displayList === 'undefined' || !displayList) return;
3156
+ var avm1PendingDisplay = [];
3157
+ function _avm1ChainReachesRoot(mc) {
3078
3158
  var seen = new Set();
3079
3159
  for (var owner = mc; owner && owner !== root; owner = owner.parent) {
3080
- if (owner._swoofDetached || seen.has(owner)) return;
3160
+ if (owner._swoofDetached || seen.has(owner)) return false;
3081
3161
  seen.add(owner);
3082
3162
  }
3083
- if (owner !== root) return;
3163
+ return owner === root;
3164
+ }
3165
+ function _commitAvm1DisplayEntry(mc, entry) {
3166
+ if (mc._swoofDisplayDepth != null && displayList[mc._swoofDisplayDepth]?._clip === mc) return;
3084
3167
  var key = mc.parent === root ? _avm1RootDisplayKey(mc._swoofDepth) : 'avm1:' + (++nextAvm1DisplayKey);
3085
3168
  mc._swoofDisplayDepth = key;
3086
3169
  entry._avm1Dynamic = true;
3087
3170
  displayList[key] = entry;
3088
3171
  }
3172
+ function _flushPendingDisplay() {
3173
+ var pending = avm1PendingDisplay;
3174
+ avm1PendingDisplay = [];
3175
+ for (var i = 0; i < pending.length; i++) {
3176
+ var item = pending[i];
3177
+ if (item.mc._swoofDetached) continue;
3178
+ if (_avm1ChainReachesRoot(item.mc)) _commitAvm1DisplayEntry(item.mc, item.entry);
3179
+ else avm1PendingDisplay.push(item);
3180
+ }
3181
+ }
3182
+ function _registerAvm1DisplayEntry(mc, entry) {
3183
+ if (typeof displayList === 'undefined' || !displayList) return;
3184
+ if (!_avm1ChainReachesRoot(mc)) {
3185
+ avm1PendingDisplay.push({ mc: mc, entry: entry });
3186
+ return;
3187
+ }
3188
+ _commitAvm1DisplayEntry(mc, entry);
3189
+ _flushPendingDisplay();
3190
+ }
3089
3191
 
3090
3192
  function _avm1RootDisplayKey(depth) {
3091
3193
  return displayList[depth] && !displayList[depth]._avm1Dynamic ? 'avm1:root:' + depth : depth;
@@ -3626,7 +3728,7 @@ function _mcRemoveMovieClip() {
3626
3728
 
3627
3729
  function _avm1RetainSwappedClip(clip, placement) {
3628
3730
  var depth = clip._swoofSwappedDepth;
3629
- if (!clip.parent || !placement || depth == null || depth < 0 || clip._swoofDetached) return false;
3731
+ if (!clip.parent || !placement || depth == null || depth < -16384 || depth > 0x7effbffc || clip._swoofDetached) return false;
3630
3732
  var parent = clip.parent;
3631
3733
  var children = parent._swoofChildren || (parent._swoofChildren = Object.create(null));
3632
3734
  if (children[depth] && children[depth] !== clip) return false;
@@ -3680,6 +3782,7 @@ function _swapClipDepths(mc, other) {
3680
3782
  move(mc, newDepth, entry);
3681
3783
  if (sibling) move(sibling, oldDepth, target);
3682
3784
  if (mc._swoofAuthoredAttached) mc._swoofScriptDepthTransform = true;
3785
+ if (sibling && sibling._swoofAuthoredAttached) sibling._swoofScriptDepthTransform = true;
3683
3786
  }
3684
3787
 
3685
3788
  function _clipByteLength() {
@@ -3736,7 +3839,7 @@ function _mcCreateTextField(name, depth, x, y, width, height) {
3736
3839
  field.y = +y || 0;
3737
3840
  field.width = Math.max(0, +width || 0);
3738
3841
  field.height = Math.max(0, +height || 0);
3739
- __swoof_swflib.bindAvm1TextFieldSize?.(field);
3842
+ __swoof_swflib.bindTextFieldSize?.(field);
3740
3843
  __swoof_swflib.bindAvm1TextFieldScroll?.(field, invokeAvm1TextFieldListener);
3741
3844
  field.defaultTextFormat.font = 'Times New Roman';
3742
3845
  field._swoofAvm1TextField = true;
@@ -4046,7 +4149,7 @@ function _ensureNamedClip(name, characterId, owner, newPlacement) {
4046
4149
  field.width = info.bounds.w;
4047
4150
  field.height = info.bounds.h;
4048
4151
  }
4049
- __swoof_swflib.bindAvm1TextFieldSize?.(field);
4152
+ __swoof_swflib.bindTextFieldSize?.(field);
4050
4153
  __swoof_swflib.bindAvm1TextFieldScroll?.(field, invokeAvm1TextFieldListener);
4051
4154
  __swoof_swflib.applyCsmTextSettings?.(field, info.csmTextSettings);
4052
4155
  var format = field.defaultTextFormat;
@@ -4094,14 +4197,22 @@ function _ensureNamedClip(name, characterId, owner, newPlacement) {
4094
4197
 
4095
4198
  /** Update root/stage mouse + fire onPress/onRelease on named hit targets. */
4096
4199
  function _bindAvm1Pointer() {
4097
- if (typeof document === 'undefined') return;
4098
- var canvas = document.getElementById('stage');
4200
+ var stageCanvas = typeof document !== 'undefined' && document.getElementById ? document.getElementById('stage') : null;
4201
+ var movieCanvas = typeof _avm1Host !== 'undefined' ? _avm1Host.canvas : null;
4202
+ var canvas = movieCanvas || stageCanvas;
4203
+ // A loaded movie paints on its own canvas. Listening on the host stage would
4204
+ // either steal the host's input or miss the movie entirely.
4205
+ var embeddedMovie = !!(movieCanvas && movieCanvas !== stageCanvas);
4099
4206
  if (!canvas || canvas._swoofAvm1Pointer) return;
4100
4207
  canvas._swoofAvm1Pointer = true;
4101
- canvas.style.touchAction = 'none';
4102
- canvas.tabIndex = 0;
4208
+ if (canvas.style) canvas.style.touchAction = 'none';
4209
+ if (!embeddedMovie) canvas.tabIndex = 0;
4210
+ function listen(target, type, handler, capture) {
4211
+ if (!embeddedMovie && target && target.addEventListener) target.addEventListener(type, handler, capture);
4212
+ }
4103
4213
 
4104
4214
  function stageCoords(e) {
4215
+ if (e && e._swoofLocalPoint) return e._swoofLocalPoint;
4105
4216
  var rect = canvas.getBoundingClientRect();
4106
4217
  var sx = canvas.width / Math.max(1, rect.width);
4107
4218
  var sy = canvas.height / Math.max(1, rect.height);
@@ -4147,9 +4258,11 @@ function _bindAvm1Pointer() {
4147
4258
  }
4148
4259
 
4149
4260
  function hitNamed(x, y) {
4150
- // Prefer display-list named places (accurate matrix). Fall back to clip x/y/width/height.
4261
+ // Prefer display-list named places with vector/sprite geometry, then AABB fallback.
4151
4262
  var best = null;
4152
4263
  var bestPath = null;
4264
+ var characters = typeof CHARACTERS !== 'undefined' ? CHARACTERS : null;
4265
+ var heads = typeof spritePlay !== 'undefined' ? spritePlay : null;
4153
4266
  if (typeof displayList !== 'undefined' && displayList) {
4154
4267
  for (var d in displayList) {
4155
4268
  if (!Object.prototype.hasOwnProperty.call(displayList, d)) continue;
@@ -4162,23 +4275,26 @@ function _bindAvm1Pointer() {
4162
4275
  typeof e._clip.onPress !== 'function' && typeof e._clip.onRelease !== 'function')) continue;
4163
4276
  var areaHit = _avm1HitAreaContains(clip, x, y);
4164
4277
  if (areaHit === false) continue;
4165
- var m = e._avm1Dynamic ? _clipWorldMatrix(e._clip) : e.matrix;
4166
- if (!m) m = { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
4167
- var px = (m.tx || 0) / 20;
4168
- var py = (m.ty || 0) / 20;
4169
- var info = (typeof CHARACTERS !== 'undefined') ? CHARACTERS[String(e.characterId)] : null;
4170
- // Sprites often lack bounds — use a generous hit box so menu buttons work.
4171
- var b = (info && info.bounds) || { x: 0, y: 0, w: 160, h: 60 };
4172
- var w = Math.max(40, (b.w || 160) * Math.abs(m.a || 1));
4173
- var h = Math.max(24, (b.h || 60) * Math.abs(m.d || 1));
4174
- var left = px + (b.x || 0);
4175
- var top = py + (b.y || 0);
4176
- if (areaHit === true || (x >= left && x <= left + w && y >= top && y <= top + h)) {
4177
- var path = e._avm1Dynamic || e._clip?._swoofSwappedDepth != null ? _clipDepthPath(e._clip) : [Number(d) - 16384];
4178
- if (!bestPath || compareDisplayDepthPaths(path, bestPath) >= 0) {
4179
- bestPath = path;
4180
- best = e;
4181
- }
4278
+ var geometryHit = areaHit === true ||
4279
+ (clip && hitTestAvm1ClipGeometry(characters, clip, x, y, heads) === true);
4280
+ if (!geometryHit) {
4281
+ var m = e._avm1Dynamic ? _clipWorldMatrix(e._clip) : e.matrix;
4282
+ if (!m) m = { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
4283
+ var px = (m.tx || 0) / 20;
4284
+ var py = (m.ty || 0) / 20;
4285
+ var info = characters ? characters[String(e.characterId)] : null;
4286
+ // Sprites without measurable fills still need a generous menu hit box.
4287
+ var b = (info && info.bounds) || { x: 0, y: 0, w: 160, h: 60 };
4288
+ var w = Math.max(40, (b.w || 160) * Math.abs(m.a || 1));
4289
+ var h = Math.max(24, (b.h || 60) * Math.abs(m.d || 1));
4290
+ var left = px + (b.x || 0);
4291
+ var top = py + (b.y || 0);
4292
+ if (!(x >= left && x <= left + w && y >= top && y <= top + h)) continue;
4293
+ }
4294
+ var path = e._avm1Dynamic || e._clip?._swoofSwappedDepth != null ? _clipDepthPath(e._clip) : [Number(d) - 16384];
4295
+ if (!bestPath || compareDisplayDepthPaths(path, bestPath) >= 0) {
4296
+ bestPath = path;
4297
+ best = e;
4182
4298
  }
4183
4299
  }
4184
4300
  }
@@ -4194,6 +4310,7 @@ function _bindAvm1Pointer() {
4194
4310
  var areaHit = _avm1HitAreaContains(clip, x, y);
4195
4311
  if (areaHit === true) return clip;
4196
4312
  if (areaHit === false) continue;
4313
+ if (hitTestAvm1ClipGeometry(characters, clip, x, y, heads) === true) return clip;
4197
4314
  var cx = +clip.x || 0, cy = +clip.y || 0;
4198
4315
  var cw = +clip.width || 120, ch = +clip.height || 40;
4199
4316
  // Flash scaleX is often 100-based on these stubs
@@ -4205,6 +4322,11 @@ function _bindAvm1Pointer() {
4205
4322
  }
4206
4323
 
4207
4324
  var _pressed = null;
4325
+ var touchHitDelivered = false;
4326
+ function isTouchMouse(event) {
4327
+ var type = event && event.pointerType;
4328
+ return type === 'touch' || type === 'pen';
4329
+ }
4208
4330
  function selectWheelText(point) {
4209
4331
  avm1CanvasWheelText = _avm1FocusTimeline()?.pickAvm1CanvasText?.(point.x, point.y) || null;
4210
4332
  }
@@ -4242,10 +4364,29 @@ function _bindAvm1Pointer() {
4242
4364
  }
4243
4365
  if (outside || cancelled) _pressed = null;
4244
4366
  _enterFrameAll('onMouseUp');
4367
+ if (isTouchMouse(event)) {
4368
+ if (!outside && !cancelled && touchHitDelivered) onMouseUp(event);
4369
+ touchHitDelivered = false;
4370
+ }
4371
+ }
4372
+ listen(canvas, 'pointermove', mouseMove, true);
4373
+ if (!pointerEvents) listen(canvas, 'mousemove', mouseMove, true);
4374
+ // Flash keeps startDrag tracking while the pointer leaves the stage; without
4375
+ // Pointer Event capture (or after a scripted startDrag), canvas-only moves stop.
4376
+ if (keyTarget !== canvas) {
4377
+ listen(keyTarget, pointerEvents ? 'pointermove' : 'mousemove', function (event) {
4378
+ if (!avm1Drag || event.isPrimary === false) return;
4379
+ if (event.target === canvas) return;
4380
+ mouseMove(event);
4381
+ }, true);
4382
+ if (!pointerEvents) {
4383
+ listen(keyTarget, 'mouseup', function (event) {
4384
+ if (!avm1Drag && !_pressed) return;
4385
+ onMouseUp(event);
4386
+ }, true);
4387
+ }
4245
4388
  }
4246
- canvas.addEventListener('pointermove', mouseMove, true);
4247
- if (!pointerEvents) canvas.addEventListener('mousemove', mouseMove, true);
4248
- canvas.addEventListener('pointerdown', function (event) {
4389
+ function onPointerDown(event) {
4249
4390
  var point = pointerPosition(event);
4250
4391
  if (pointerEvents && point && event.button === 0) {
4251
4392
  var id = event.pointerId ?? 0;
@@ -4253,27 +4394,40 @@ function _bindAvm1Pointer() {
4253
4394
  heldPointers.set(id, event.pointerType);
4254
4395
  selectWheelText(point);
4255
4396
  _enterFrameAll('onMouseDown');
4397
+ // Capture keeps the release when it lands outside the browser window.
4398
+ try { canvas.setPointerCapture?.(event.pointerId); } catch (_) {}
4256
4399
  }
4257
- }, true);
4400
+ }
4401
+ listen(canvas, 'pointerdown', onPointerDown, true);
4402
+ // Bubble runs after the timeline's capture listener, which marks a button it already pressed.
4403
+ listen(canvas, 'pointerdown', function (event) {
4404
+ if (!isTouchMouse(event) || event._swoofTimelinePress || event.button !== 0 || event.isPrimary === false) return;
4405
+ touchHitDelivered = false;
4406
+ onMouseDown(event);
4407
+ try { canvas.setPointerCapture?.(event.pointerId); } catch (_) {}
4408
+ event.preventDefault?.();
4409
+ }, false);
4258
4410
  if (pointerEvents) {
4259
- canvas.addEventListener('pointerup', function (event) { finishPointer(event, false, false); }, true);
4260
- canvas.addEventListener('pointercancel', function (event) { finishPointer(event, false, true); }, true);
4261
- canvas.addEventListener('lostpointercapture', function (event) { finishPointer(event, false, true, false); }, true);
4411
+ listen(canvas, 'pointerup', function (event) { finishPointer(event, false, false); }, true);
4412
+ listen(canvas, 'pointercancel', function (event) { finishPointer(event, false, true); }, true);
4413
+ listen(canvas, 'lostpointercapture', function (event) { finishPointer(event, false, true, false); }, true);
4262
4414
  if (keyTarget !== canvas) {
4263
- keyTarget.addEventListener('pointerup', function (event) {
4415
+ listen(keyTarget, 'pointerup', function (event) {
4264
4416
  if (event.target !== canvas) finishPointer(event, true, false);
4265
4417
  }, true);
4266
- keyTarget.addEventListener('pointercancel', function (event) {
4418
+ listen(keyTarget, 'pointercancel', function (event) {
4267
4419
  if (event.target !== canvas) finishPointer(event, true, true);
4268
4420
  }, true);
4269
4421
  }
4270
4422
  }
4271
4423
  function dispatchKeyEvent(name, keyPress, onKeyPress) {
4272
4424
  var visited = new Set();
4425
+ // Snapshot before clip events so a listener added by one of them waits for the next key.
4426
+ var registered = Array.from(avm1KeyListeners);
4273
4427
  _dispatchAvm1ClipKeyEvent(root, _clips, name, keyPress, onKeyPress, _avm1ReportScriptError, callAvm1ClipScript, visited);
4274
- _broadcastAvm1Listeners(avm1KeyListeners, name, [], visited);
4428
+ _broadcastAvm1Listeners(avm1KeyListeners, name, [], visited, registered);
4275
4429
  }
4276
- keyTarget.addEventListener('keydown', function (e) {
4430
+ function onKeyDown(e) {
4277
4431
  var code = _KeyImpl.getCode();
4278
4432
  if (code > 0) {
4279
4433
  blurredKeys.delete(code);
@@ -4282,7 +4436,13 @@ function _bindAvm1Pointer() {
4282
4436
  var keyPress = avm1ClipKeyPressCode(e), caught = false;
4283
4437
  dispatchKeyEvent('onKeyDown', keyPress, function () { caught = true; });
4284
4438
  var overlay = avm1Focus?._swoofOverlayElement;
4285
- if (e.defaultPrevented || (e.target !== canvas && !(overlay && e.target === overlay) && document.activeElement !== canvas)) return;
4439
+ // Title screens often receive Space/Tab before the canvas is focused; treat the
4440
+ // idle document (body/html/null) as the game keyboard target.
4441
+ var active = typeof document !== 'undefined' ? document.activeElement : null;
4442
+ var canvasFocused = e.target === canvas || active === canvas ||
4443
+ (overlay && (e.target === overlay || active === overlay)) ||
4444
+ !active || active === document.body || active === document.documentElement;
4445
+ if (e.defaultPrevented || !canvasFocused) return;
4286
4446
  if ((keyPress === 18 || keyPress === 13 || keyPress === 32) &&
4287
4447
  (caught || _avm1FocusTimeline()?.hasAvm1ButtonKeyPress?.(keyPress))) { e.preventDefault?.(); return; }
4288
4448
  if (keyPress === 18 && !e.altKey && !e.ctrlKey && !e.metaKey) {
@@ -4303,14 +4463,16 @@ function _bindAvm1Pointer() {
4303
4463
  e.preventDefault?.();
4304
4464
  }
4305
4465
  }
4306
- }, true);
4307
- keyTarget.addEventListener('keyup', function () {
4466
+ }
4467
+ listen(keyTarget, 'keydown', onKeyDown, true);
4468
+ function onKeyUp() {
4308
4469
  var code = _KeyImpl.getCode();
4309
4470
  if (blurredKeys.delete(code)) return;
4310
4471
  heldKeys.delete(code);
4311
4472
  dispatchKeyEvent('onKeyUp', 0);
4312
- }, true);
4313
- if (keyTarget !== canvas) keyTarget.addEventListener('blur', function () {
4473
+ }
4474
+ listen(keyTarget, 'keyup', onKeyUp, true);
4475
+ if (keyTarget !== canvas) listen(keyTarget, 'blur', function () {
4314
4476
  if (heldPointers.size) {
4315
4477
  for (var [id, type] of heldPointers) cancelledPointers.set(type, id);
4316
4478
  heldPointers.clear();
@@ -4349,8 +4511,9 @@ function _bindAvm1Pointer() {
4349
4511
  }
4350
4512
  }
4351
4513
 
4352
- canvas.addEventListener('mousedown', function (e) {
4514
+ function onMouseDown(e) {
4353
4515
  if (e.button != null && e.button !== 0) return;
4516
+ if (touchHitDelivered && isTouchMouse(e)) return;
4354
4517
  var p = stageCoords(e);
4355
4518
  setMouse(p.x, p.y);
4356
4519
  canvas.focus();
@@ -4365,9 +4528,12 @@ function _bindAvm1Pointer() {
4365
4528
  _avm1ReportScriptError('swoof: onPress', err, 'message');
4366
4529
  }
4367
4530
  }
4368
- }, true);
4369
- canvas.addEventListener('mouseup', function (e) {
4531
+ if (isTouchMouse(e)) touchHitDelivered = true;
4532
+ }
4533
+ listen(canvas, 'mousedown', onMouseDown, true);
4534
+ function onMouseUp(e) {
4370
4535
  if (e.button != null && e.button !== 0) return;
4536
+ if (isTouchMouse(e) && !touchHitDelivered) return;
4371
4537
  var p = stageCoords(e);
4372
4538
  setMouse(p.x, p.y);
4373
4539
  if (!pointerEvents) _enterFrameAll('onMouseUp');
@@ -4387,13 +4553,14 @@ function _bindAvm1Pointer() {
4387
4553
  }
4388
4554
  }
4389
4555
  _pressed = null;
4390
- }, true);
4556
+ }
4557
+ listen(canvas, 'mouseup', onMouseUp, true);
4391
4558
  function topmostClip(p, selectableOnly) {
4392
4559
  var target = root, bestPath = [];
4393
4560
  function visit(clip) {
4394
4561
  if (clip.visible === false || clip._swoofDetached || !_avm1PassesMasks(clip, p.x, p.y)) return;
4395
4562
  if (clip !== root && (!selectableOnly || !clip._swoofAvm1TextField || clip.selectable !== false) &&
4396
- _hitTestImpl(clip, p.x, p.y, true)) {
4563
+ _hitTestImpl(clip, p.x, p.y, true, true, true)) {
4397
4564
  var path = _clipDepthPath(clip);
4398
4565
  if (compareDisplayDepthPaths(path, bestPath) >= 0) { target = clip; bestPath = path; }
4399
4566
  }
@@ -4404,7 +4571,7 @@ function _bindAvm1Pointer() {
4404
4571
  visit(root);
4405
4572
  return target;
4406
4573
  }
4407
- if (typeof __swoof_swflib !== 'undefined' && __swoof_swflib.stage) {
4574
+ if (embeddedMovie || typeof __swoof_swflib !== 'undefined' && __swoof_swflib.stage) {
4408
4575
  canvas._swoofAvm1Wheel = function (event, delta) {
4409
4576
  if (!avm1MouseListeners.size && !avm1CanvasWheelText) return;
4410
4577
  var p = stageCoords(event);
@@ -4418,7 +4585,7 @@ function _bindAvm1Pointer() {
4418
4585
  if (avm1MouseListeners.size) _broadcastAvm1Listeners(avm1MouseListeners, 'onMouseWheel', [delta, topmostClip(p, false)]);
4419
4586
  };
4420
4587
  }
4421
- canvas.addEventListener('contextmenu', function (e) {
4588
+ listen(canvas, 'contextmenu', function (e) {
4422
4589
  var runtime = typeof __swoof_swflib !== 'undefined' && __swoof_swflib;
4423
4590
  if (!runtime?.openContextMenu) return;
4424
4591
  var p = stageCoords(e);
@@ -4431,6 +4598,40 @@ function _bindAvm1Pointer() {
4431
4598
  }
4432
4599
  }
4433
4600
  }, true);
4601
+ if (embeddedMovie) {
4602
+ function localPointer(x, y, event) {
4603
+ return {
4604
+ _swoofLocalPoint: { x: x, y: y },
4605
+ isPrimary: !event || event.isPrimary !== false,
4606
+ button: event && event.button != null ? event.button : 0,
4607
+ pointerId: event && event.pointerId,
4608
+ pointerType: event && event.pointerType,
4609
+ target: canvas,
4610
+ preventDefault: function () { if (event && event.preventDefault) event.preventDefault(); },
4611
+ get defaultPrevented() { return !!(event && event.defaultPrevented); },
4612
+ };
4613
+ }
4614
+ canvas._swoofAvm1Embed = {
4615
+ move: function (x, y, event) { mouseMove(localPointer(x, y, event)); },
4616
+ down: function (x, y, event) {
4617
+ var e = localPointer(x, y, event);
4618
+ onPointerDown(e);
4619
+ onMouseDown(e);
4620
+ },
4621
+ up: function (x, y, event, cancelled) {
4622
+ var e = localPointer(x, y, event);
4623
+ finishPointer(e, false, !!cancelled);
4624
+ onMouseUp(e);
4625
+ },
4626
+ wheel: function (x, y, delta, event) {
4627
+ if (canvas._swoofAvm1Wheel) canvas._swoofAvm1Wheel(localPointer(x, y, event), delta);
4628
+ },
4629
+ // Host continues window moves while a scripted startDrag is active.
4630
+ isDragging: function () { return !!avm1Drag; },
4631
+ keyDown: onKeyDown,
4632
+ keyUp: onKeyUp,
4633
+ };
4634
+ }
4434
4635
  }
4435
4636
  if (typeof document !== 'undefined') {
4436
4637
  if (document.readyState === 'loading') {
@@ -4505,9 +4706,31 @@ function _avm1ApplyCallback(handler, receiver, args, useDefiningClip = true, exi
4505
4706
  }
4506
4707
  }
4507
4708
 
4709
+ var avm1ScriptDepth = 0;
4710
+ var avm1PendingSpriteFrames = [];
4711
+ function _scheduleAttachedSpriteFrame(charId, child) {
4712
+ if (typeof runSpriteFrameScripts !== 'function') return;
4713
+ if (avm1ScriptDepth > 0) {
4714
+ runSpriteFrameScripts(charId, 0, child, 'playhead');
4715
+ avm1PendingSpriteFrames.push([charId, child]);
4716
+ } else runSpriteFrameScripts(charId, 0, child);
4717
+ }
4718
+ function _flushAttachedSpriteFrames() {
4719
+ var guard = 0;
4720
+ while (avm1PendingSpriteFrames.length && guard++ < 64) {
4721
+ var batch = avm1PendingSpriteFrames;
4722
+ avm1PendingSpriteFrames = [];
4723
+ for (var i = 0; i < batch.length; i++) {
4724
+ avm1ScriptDepth++;
4725
+ try { runSpriteFrameScripts(batch[i][0], 0, batch[i][1], 'scripts'); }
4726
+ finally { avm1ScriptDepth--; }
4727
+ }
4728
+ }
4729
+ }
4508
4730
  function callAvm1ClipScript(mc, fn, isControlInput) {
4509
4731
  var previous = avm1ScriptClip;
4510
4732
  avm1ScriptClip = mc || root;
4733
+ avm1ScriptDepth++;
4511
4734
  try {
4512
4735
  if (_avm1AuthoredButton(avm1ScriptClip) ||
4513
4736
  (isControlInput && avm1ScriptClip._swoofAuthoredAttached && _avm1MovieClips.has(avm1ScriptClip))) {
@@ -4516,6 +4739,8 @@ function callAvm1ClipScript(mc, fn, isControlInput) {
4516
4739
  return fn.call(avm1ScriptClip);
4517
4740
  } finally {
4518
4741
  avm1ScriptClip = previous;
4742
+ avm1ScriptDepth--;
4743
+ if (avm1ScriptDepth === 0) _flushAttachedSpriteFrames();
4519
4744
  }
4520
4745
  }
4521
4746
 
@@ -4530,8 +4755,8 @@ function runClipScript(mc, fn, reportErrors = true) {
4530
4755
  }
4531
4756
  }
4532
4757
 
4533
- function _broadcastAvm1Listeners(listeners, event, args, dispatched) {
4534
- for (var [listener, clip] of Array.from(listeners)) {
4758
+ function _broadcastAvm1Listeners(listeners, event, args, dispatched, registered) {
4759
+ for (var [listener, clip] of registered || Array.from(listeners)) {
4535
4760
  if (dispatched?.has(listener) || listener._swoofDetached || !listeners.has(listener)) continue;
4536
4761
  runClipScript(clip, function () {
4537
4762
  var handler = _member(listener, event);
@@ -4568,6 +4793,7 @@ function _enterFrameAll(eventName) {
4568
4793
  _avm1AdvanceCompanions();
4569
4794
  }
4570
4795
  var seen = new Set();
4796
+ var registeredMouseListeners = mouseEvent ? Array.from(avm1MouseListeners) : null;
4571
4797
  function visit(clip) {
4572
4798
  if (!clip || typeof clip !== 'object' || clip._swoofDetached || seen.has(clip)) return;
4573
4799
  seen.add(clip);
@@ -4581,8 +4807,13 @@ function _enterFrameAll(eventName) {
4581
4807
  _enterFrameErrorCount++;
4582
4808
  }
4583
4809
  }
4584
- for (var children of [clip._swoofRootChildren, clip._swoofSpriteChildren, clip._swoofChildren]) {
4585
- for (var child of Object.values(children || {})) if (child.parent === clip) visit(child);
4810
+ var children = mouseEvent ? orderedAvm1Children(clip) : null;
4811
+ if (children) {
4812
+ for (var child of children) visit(child);
4813
+ } else {
4814
+ for (var group of [clip._swoofRootChildren, clip._swoofSpriteChildren, clip._swoofChildren]) {
4815
+ for (var nested of Object.values(group || {})) if (nested.parent === clip) visit(nested);
4816
+ }
4586
4817
  }
4587
4818
  }
4588
4819
  visit(root);
@@ -4594,7 +4825,7 @@ function _enterFrameAll(eventName) {
4594
4825
  if (mouseEvent && !(target instanceof Avm1MovieClip) && !target?._swoofAvm1TextField) continue;
4595
4826
  visit(target);
4596
4827
  }
4597
- if (mouseEvent) _broadcastAvm1Listeners(avm1MouseListeners, eventName, [], seen);
4828
+ if (mouseEvent) _broadcastAvm1Listeners(avm1MouseListeners, eventName, [], seen, registeredMouseListeners);
4598
4829
  }
4599
4830
 
4600
4831
  function _own(obj, name) {
@@ -4778,7 +5009,7 @@ const avm1RuntimeBindings = [
4778
5009
  '_avm1LegacyCompare', '_avm1Less2', '_avm1Equals2', '_avm1ToBoolean', '_avm1Logical',
4779
5010
  '_avm1Enumerate', '_avm1Substring', '_avm1RandomNumber', '_avm1CharToCode', '_avm1CodeToChar',
4780
5011
  '_avm1FrameLoaded', '_avm1ActionArgs', '_avm1Implements', '_avm1InstanceOf', '_avm1Cast',
4781
- '_avm1MarkBytecodeFunction', '_avm1NewObject', '_avm1NewMethod', '_avm1Extend', '_avm1Super',
5012
+ '_avm1MarkBytecodeFunction', '_avm1PreloadArguments', '_avm1NewObject', '_avm1NewMethod', '_avm1Extend', '_avm1Super',
4782
5013
  '_avm1ToggleQuality', '_avm1FSCommand2',
4783
5014
  '_avm1GetURL',
4784
5015
  '_avm1CallFrame',
@@ -4806,10 +5037,24 @@ export const AVM1_RUNTIME_FACTORY_SOURCE = `function createAvm1Runtime(_avm1Host
4806
5037
  var HostSound = globalThis.Sound;
4807
5038
  var activeAvm1Sounds = new Map();
4808
5039
  var Sound = typeof HostSound === 'function' ? function Sound(target) {
4809
- HostSound.call(this);
4810
- this._swoofSoundHost = _avm1Host;
4811
- this._avm1SoundTarget = target == null ? null : _avm1ResolveTarget(root, target);
4812
- this._avm1SoundInvalidTarget = target != null && this._avm1SoundTarget == null;
5040
+ // new Sound() builds a host instance. AS2 subclass super() applies into the
5041
+ // existing receiver (ES class constructors cannot be .call'd onto it).
5042
+ var self;
5043
+ if (new.target) {
5044
+ self = Reflect.construct(HostSound, [], new.target);
5045
+ } else {
5046
+ self = this;
5047
+ if (typeof __swoof_swflib.installSoundInstance === 'function') {
5048
+ __swoof_swflib.installSoundInstance(self);
5049
+ } else if (!(self._channels instanceof Set)) {
5050
+ self._channels = new Set();
5051
+ if (!self._listeners) self._listeners = Object.create(null);
5052
+ }
5053
+ }
5054
+ self._swoofSoundHost = _avm1Host;
5055
+ self._avm1SoundTarget = target == null ? null : _avm1ResolveTarget(root, target);
5056
+ self._avm1SoundInvalidTarget = target != null && self._avm1SoundTarget == null;
5057
+ return self;
4813
5058
  } : undefined;
4814
5059
  if (Sound) {
4815
5060
  Sound.prototype = Object.create(HostSound.prototype, { constructor: { value: Sound } });
@@ -4894,7 +5139,7 @@ export const AVM1_RUNTIME_BOOTSTRAP = `var { ${avm1RuntimeBindings} } = __swoof_
4894
5139
  frameRate: typeof FRAME_RATE === 'undefined' ? undefined : FRAME_RATE,
4895
5140
  stageWidth: typeof STAGE_WIDTH === 'undefined' ? undefined : STAGE_WIDTH,
4896
5141
  stageHeight: typeof STAGE_HEIGHT === 'undefined' ? undefined : STAGE_HEIGHT,
4897
- root: __swoof_root, runtime: __swoof_swflib, exportAssets: EXPORT_ASSETS,
5142
+ root: __swoof_root, runtime: __swoof_swflib, canvas: typeof ctx !== 'undefined' && ctx ? ctx.canvas : undefined, exportAssets: EXPORT_ASSETS,
4898
5143
  assets: typeof ASSETS === 'undefined' ? undefined : ASSETS,
4899
5144
  loaded: typeof loaded === 'undefined' ? undefined : loaded,
4900
5145
  characters: CHARACTERS, displayList: displayList, spritePlay: spritePlay,