Skip to content

Compile ahead of time

The swf2es command (packages/cli) writes the same modules the player would compile, a log for each, and a manifest.json of their keys:

Terminal window
swf2es movie.swf -o movie.swf2es --emit-libraries

against the libraries the player loads: builtin.abc and swf2es’s playerglobal, and AIR’s library too with --air, all shipped with the command. --lib names others in their place, in order, such as Flash Player’s playerglobal for a player given that one. In a checkout, node packages/cli/dist/main.js is the command.

The player reads them as a module cache. Chain an IndexedDB cache behind it to keep whatever the command did not compile:

import { createCodegen } from "@swf2es/codegen";
import { builtinLibrary, Player, playerglobalLibrary, Scripting } from "@swf2es/player";
import { indexedDbModuleCache } from "@swf2es/player-hosts/indexeddb";
import { chainCaches, precompiledModules } from "@swf2es/player-hosts/precompiled";
const bytes = async (url) => new Uint8Array(await (await fetch(url)).arrayBuffer());
const codegen = await createCodegen(await WebAssembly.compileStreaming(fetch("/codegen.wasm")));
const scripting = new Scripting(codegen, {
url: new URL("movie.swf", location.href).href,
moduleCache: chainCaches(
precompiledModules("/movie.swf2es/manifest.json", { importModules: true }),
indexedDbModuleCache(),
),
});
await scripting.loadLibraries([builtinLibrary, playerglobalLibrary]);
const player = new Player(await bytes("movie.swf"), scripting);
await player.start(); // nothing compiled: every module came from movie.swf2es

A module the command wrote is found only for the SWF in the position it was compiled for: loaded after the same libraries into a domain that sees nothing else, as the main movie or by a Loader into new ApplicationDomain(null). Other modules compile as usual. If importModules is on and the libraries were compiled with --emit-libraries, every module is imported rather than evaluated, so the page runs under script-src 'self' 'wasm-unsafe-eval', without 'unsafe-eval'. To draw the player, see RenderView (@swf2es/player/scene, packages/player/src/scene/view.ts) and how the element uses it (packages/web/src/playback.ts).