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
@@ -30,6 +30,10 @@ export function createAvm2Runtime(globalThis, swflib) {
30
30
  }
31
31
  if (_avm2UtilsPackage?.Proxy) _classes['flash.utils.Proxy'] = _avm2UtilsPackage.Proxy;
32
32
  if (_classes.Vector) _classes['__AS3__.vec.Vector'] = _classes.Vector;
33
+ // A leftover `flash.filters.GlowFilter` lookup is the same class as `swoof.filters.GlowFilter`.
34
+ // The qualified name is stored beside the short name so a bracket read of the original
35
+ // string still finds the catalog class.
36
+ _aliasSwoofPackages(swflib && swflib.packages, '');
33
37
  var _avm2Int = _classes.int || (_classes.int = function int(value) { return value | 0; });
34
38
  var _avm2Uint = _classes.uint || (_classes.uint = function uint(value) { return value >>> 0; });
35
39
  var _avm2ClassMarker = globalThis.Symbol.for('swoof.avm2Class');
@@ -907,6 +911,41 @@ export function createAvm2Runtime(globalThis, swflib) {
907
911
  return _avm2ArrayProperty(target, key);
908
912
  }
909
913
 
914
+ function _aliasSwoofPackages(node, prefix) {
915
+ if (!node || typeof node !== 'object') return;
916
+ for (var key in node) {
917
+ if (!Object.prototype.hasOwnProperty.call(node, key) || node[key] == null) continue;
918
+ var value = node[key];
919
+ var path = prefix ? prefix + '.' + key : key;
920
+ if (typeof value === 'function' || typeof value === 'object') _aliasSwoofName(path, value);
921
+ if (value && typeof value === 'object') _aliasSwoofPackages(value, path);
922
+ }
923
+ }
924
+
925
+ function _aliasSwoofName(path, value) {
926
+ var names = ['flash.' + path, 'swoof.' + path];
927
+ var dot = path.lastIndexOf('.');
928
+ if (dot > 0) {
929
+ var head = path.slice(0, dot), tail = path.slice(dot + 1);
930
+ names.push('flash.' + head + '::' + tail, 'swoof.' + head + '::' + tail);
931
+ }
932
+ for (var i = 0; i < names.length; i++) {
933
+ if (!Object.prototype.hasOwnProperty.call(_classes, names[i])) _classes[names[i]] = value;
934
+ }
935
+ }
936
+
937
+ function _readAliasedPackage(key) {
938
+ if (typeof key !== 'string') return undefined;
939
+ var normalized = key.replace(/::/g, '.');
940
+ var source = normalized.startsWith('flash.') ? normalized.slice(6)
941
+ : normalized.startsWith('swoof.') ? normalized.slice(6) : '';
942
+ if (!source) return undefined;
943
+ var pkg = swflib && swflib.packages;
944
+ var parts = source.split('.');
945
+ for (var i = 0; i < parts.length && pkg != null; i++) pkg = pkg[parts[i]];
946
+ return pkg == null ? undefined : pkg;
947
+ }
948
+
910
949
  function _avm2ReadProperty(target, key) {
911
950
  if (target === _avm2FontDescription && key === 'isFontCompatible' && _avm2MovieFontLookup) {
912
951
  return _avm2MovieFontLookup.isFontCompatible;
@@ -914,7 +953,10 @@ export function createAvm2Runtime(globalThis, swflib) {
914
953
  if (typeof target === 'function' && _isAvm2StaticProtectedKey(key) &&
915
954
  !Object.prototype.hasOwnProperty.call(target, key)) return undefined;
916
955
  var builtin = _avm2BuiltinProperty(target, key);
917
- return builtin !== undefined && !(key in target) ? builtin : target[key];
956
+ if (builtin !== undefined && !(key in target)) return builtin;
957
+ if (key in target) return target[key];
958
+ var aliased = _readAliasedPackage(key);
959
+ return aliased !== undefined ? aliased : target[key];
918
960
  }
919
961
 
920
962
  function _avm2PropertyExists(target, key) {
@@ -1134,6 +1176,14 @@ export function createAvm2Runtime(globalThis, swflib) {
1134
1176
  return n || name;
1135
1177
  }
1136
1178
 
1179
+ function _swoofPackage(read, redirect) {
1180
+ try {
1181
+ var resolved = read();
1182
+ if (resolved != null) return resolved;
1183
+ } catch (_) {}
1184
+ return redirect();
1185
+ }
1186
+
1137
1187
  function _getlex(name) {
1138
1188
  if (typeof name === 'string') {
1139
1189
  // AS3 undefined/null/NaN lex — never stub these as classes
@@ -1142,8 +1192,8 @@ export function createAvm2Runtime(globalThis, swflib) {
1142
1192
  if (name === 'NaN') return NaN;
1143
1193
  if (name === 'Infinity') return Infinity;
1144
1194
  if (name === 'TextField' && movieTextField && globalThis.TextField === _avm2PublicTextField) return _classes.TextField;
1145
- if (name.startsWith('flash.utils')) {
1146
- var timer = _avm2TimerFunctions[name.replace('::', '.')];
1195
+ if (name.startsWith('flash.utils') || name.startsWith('swoof.utils')) {
1196
+ var timer = _avm2TimerFunctions[name.replace('::', '.').replace(/^swoof\./, 'flash.')];
1147
1197
  if (timer) return timer;
1148
1198
  }
1149
1199
  // Timeline/instance props wrongly emitted as getlex — prefer document class / root
@@ -1170,7 +1220,7 @@ export function createAvm2Runtime(globalThis, swflib) {
1170
1220
  try {
1171
1221
  if (swflib && swflib.packages) {
1172
1222
  var parts = name.replace(/::/g, '.').split('.');
1173
- if (parts[0] === 'flash' && parts.length >= 2) {
1223
+ if ((parts[0] === 'flash' || parts[0] === 'swoof') && parts.length >= 2) {
1174
1224
  var pkg = swflib.packages;
1175
1225
  for (var i = 1; i < parts.length; i++) {
1176
1226
  if (!pkg) break;
@@ -1558,7 +1608,7 @@ export function createAvm2Runtime(globalThis, swflib) {
1558
1608
  _queueAvm2ClassInitializer, _queueAvm2ScriptInitializer, _runAvm2Initializers,
1559
1609
  _registerAvm2Interfaces, _registerAvm2ProtectedTraits, _avm2SuperKey, _avm2Call, _avm2CallProperty,
1560
1610
  _avm2MarkMethod, _avm2BindMethod, _avm2GetProperty, _avm2GetMethod, _avm2SetProperty, _avm2DeleteProperty, _findProp,
1561
- _applyType, _seedScriptTrait, _getlex, _getlexScoped, _getDefinition, _avm2NamespacedConstructor,
1611
+ _applyType, _seedScriptTrait, _swoofPackage, _getlex, _getlexScoped, _getDefinition, _avm2NamespacedConstructor,
1562
1612
  _avm2MethodEntry, _callStatic, _callMethodById, _callSuperById, _createAvm2ExecutionBudget,
1563
1613
  _registerAvm2ActivationSlots, _createAvm2Activation, _getSlot, _setSlot,
1564
1614
  _avm2HasNext, _avm2NextName, _avm2NextValue, _avm2In,
@@ -1575,7 +1625,7 @@ _createAvm2ScriptScope, _createAvm2ScriptScopes, _captureAvm2ClassScopes, _getAv
1575
1625
  _queueAvm2ClassInitializer, _queueAvm2ScriptInitializer, _runAvm2Initializers,
1576
1626
  _registerAvm2Interfaces, _registerAvm2ProtectedTraits, _avm2SuperKey, _avm2Call, _avm2CallProperty,
1577
1627
  _avm2MarkMethod, _avm2BindMethod, _avm2GetProperty, _avm2GetMethod, _avm2SetProperty, _avm2DeleteProperty, _findProp,
1578
- _applyType, _seedScriptTrait, _getlex, _getlexScoped, _getDefinition, _avm2NamespacedConstructor,
1628
+ _applyType, _seedScriptTrait, _swoofPackage, _getlex, _getlexScoped, _getDefinition, _avm2NamespacedConstructor,
1579
1629
  _avm2MethodEntry, _callStatic, _callMethodById, _callSuperById, _createAvm2ExecutionBudget,
1580
1630
  _registerAvm2ActivationSlots, _createAvm2Activation, _getSlot, _setSlot,
1581
1631
  _avm2HasNext, _avm2NextName, _avm2NextValue, _avm2In,
@@ -83,6 +83,10 @@ export function createEmbeddedMovieRuntime(hostRuntime, hostGlobal, environment,
83
83
  childGlobal.Timer = timers.Timer;
84
84
  Object.assign(childGlobal, timers.scheduling);
85
85
  childGlobal.flash = Object.assign({}, hostGlobal.flash, runtime.packages);
86
+ // The movie script rebinds `globalThis` to childGlobal. Its rewritten
87
+ // `globalThis.swoof.*` names must see this movie's Loader, Timer, Sound,
88
+ // TextBlock, and ApplicationDomain, not the host copies inherited from the page.
89
+ childGlobal.swoof = Object.assign({}, hostGlobal.swoof, runtime.packages);
86
90
 
87
91
  var canvas = hostGlobal.document.createElement('canvas');
88
92
  canvas.width = metadata.stageW;
@@ -126,7 +130,8 @@ export function createEmbeddedMovieRuntime(hostRuntime, hostGlobal, environment,
126
130
 
127
131
  var constructingDocumentClass = null;
128
132
  function bindConstructingDocument(instance) {
129
- if (!constructingDocumentClass || instance.constructor !== constructingDocumentClass) return false;
133
+ // Use instanceof so Proxy-wrapped constructors (MovieClip cast export) still match.
134
+ if (!constructingDocumentClass || !(instance instanceof constructingDocumentClass)) return false;
130
135
  instance.root = instance;
131
136
  instance.loaderInfo = environment.loaderInfo;
132
137
  instance.stage = environment.loader && environment.loader.stage || null;
@@ -2,15 +2,11 @@
2
2
  * @fileoverview Runtime sources inlined into browser bundles.
3
3
  */
4
4
 
5
- import { readFileSync } from 'fs';
6
- import { fileURLToPath } from 'url';
7
- import { dirname, join } from 'path';
8
5
  import { createAvm2Runtime } from './avm2-runtime.js';
9
-
10
- const __dirname = dirname(fileURLToPath(import.meta.url));
6
+ import { assembleSwflibSource } from './swflib/assemble.js';
11
7
 
12
8
  /** Shared SWF player API shim (geom, display, events, media, ui). */
13
- export const SWFLIB_BASE_SOURCE = readFileSync(join(__dirname, 'swflib.js'), 'utf8').trim();
9
+ export const SWFLIB_BASE_SOURCE = assembleSwflibSource().trim();
14
10
  export const SWFLIB_SOURCE = `${SWFLIB_BASE_SOURCE}
15
11
  globalThis.__swoof_swflib.createAvm2Runtime = ${createAvm2Runtime.toString()};`;
16
12
 
@@ -186,7 +186,8 @@ export function installMovieRuntime(runtime, global) {
186
186
 
187
187
  let host, lookupClass, constructingDocumentClass;
188
188
  function bindConstructingDocument(instance) {
189
- if (!constructingDocumentClass || !instance || instance.constructor !== constructingDocumentClass) return false;
189
+ // Use instanceof so Proxy-wrapped constructors (MovieClip cast export) still match.
190
+ if (!constructingDocumentClass || !instance || !(instance instanceof constructingDocumentClass)) return false;
190
191
  instance.root = instance;
191
192
  instance.stage = stage;
192
193
  instance.loaderInfo = stage.loaderInfo;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Bundle the player modules into the classic script emitted as swoof.js.
3
+ * The browser file has no import statements.
4
+ */
5
+ import esbuild from 'esbuild';
6
+ import { dirname } from 'path';
7
+ import { fileURLToPath } from 'url';
8
+
9
+ const dir = dirname(fileURLToPath(import.meta.url));
10
+
11
+ export const SWFLIB_ENTRY = 'builtins.js';
12
+
13
+ export function assembleSwflibSource() {
14
+ const result = esbuild.buildSync({
15
+ absWorkingDir: dir,
16
+ entryPoints: [SWFLIB_ENTRY],
17
+ bundle: true,
18
+ format: 'iife',
19
+ platform: 'browser',
20
+ target: 'es2020',
21
+ write: false,
22
+ legalComments: 'none',
23
+ banner: {
24
+ js: `/**
25
+ * swoof swflib — shared SWF player.
26
+ * Built from the ES modules in src/runtime/swflib.
27
+ * Loaded before bundle.js:
28
+ * <script src="swoof.js"></script>
29
+ * <script src="bundle.js"></script>
30
+ */`,
31
+ },
32
+ });
33
+ return result.outputFiles[0].text;
34
+ }
@@ -0,0 +1,4 @@
1
+ /** The canvas or image a BitmapData currently shows. No dependency on the renderer. */
2
+ export function bitmapDisplayImage(data) {
3
+ return data && (data._lockedDisplayImage || data._canvas || data._img);
4
+ }