@swf2es/player-hosts 0.0.0-stage → 0.1.1

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 (405) hide show
  1. package/LICENSE +202 -0
  2. package/LICENSE.third-party +759 -0
  3. package/README.md +43 -2
  4. package/dist/indexeddb.d.ts +27 -0
  5. package/dist/indexeddb.d.ts.map +1 -0
  6. package/dist/indexeddb.js +170 -0
  7. package/dist/indexeddb.js.map +1 -0
  8. package/dist/node-worker.d.ts +2 -0
  9. package/dist/node-worker.d.ts.map +1 -0
  10. package/dist/node-worker.js +12 -0
  11. package/dist/node-worker.js.map +1 -0
  12. package/dist/node.d.ts +8 -0
  13. package/dist/node.d.ts.map +1 -0
  14. package/dist/node.js +43 -0
  15. package/dist/node.js.map +1 -0
  16. package/dist/precompiled.d.ts +31 -0
  17. package/dist/precompiled.d.ts.map +1 -0
  18. package/dist/precompiled.js +125 -0
  19. package/dist/precompiled.js.map +1 -0
  20. package/dist/stage3d.d.ts +13 -0
  21. package/dist/stage3d.d.ts.map +1 -0
  22. package/dist/stage3d.js +61 -0
  23. package/dist/stage3d.js.map +1 -0
  24. package/dist/thread/codegen/codegen.js +308 -0
  25. package/dist/thread/codegen/index.js +405 -0
  26. package/dist/thread/format/abc-names.js +126 -0
  27. package/dist/thread/format/actions.js +222 -0
  28. package/dist/thread/format/air-descriptor.js +63 -0
  29. package/dist/thread/format/api-versions.js +22 -0
  30. package/dist/thread/format/bitmap.js +199 -0
  31. package/dist/thread/format/button.js +60 -0
  32. package/dist/thread/format/compression.js +200 -0
  33. package/dist/thread/format/display.js +305 -0
  34. package/dist/thread/format/filters.js +136 -0
  35. package/dist/thread/format/font.js +168 -0
  36. package/dist/thread/format/index.js +26 -0
  37. package/dist/thread/format/pbj.js +484 -0
  38. package/dist/thread/format/shape.js +411 -0
  39. package/dist/thread/format/sound.js +91 -0
  40. package/dist/thread/format/swf.js +163 -0
  41. package/dist/thread/format/tags.js +58 -0
  42. package/dist/thread/format/text.js +135 -0
  43. package/dist/thread/format/zip.js +91 -0
  44. package/dist/thread/native/audio/binary.js +3 -0
  45. package/dist/thread/native/audio/index.js +72 -0
  46. package/dist/thread/native/base64.js +9 -0
  47. package/dist/thread/native/compress/binary.js +3 -0
  48. package/dist/thread/native/compress/index.js +145 -0
  49. package/dist/thread/native/instance.js +49 -0
  50. package/dist/thread/native/mp3/binary.js +3 -0
  51. package/dist/thread/native/mp3/index.js +48 -0
  52. package/dist/thread/native/render/binary.js +3 -0
  53. package/dist/thread/native/render/bitmap-kernel.js +591 -0
  54. package/dist/thread/native/render/draw-kernel.js +204 -0
  55. package/dist/thread/native/render/dxt-encoder.js +94 -0
  56. package/dist/thread/native/render/index.js +10 -0
  57. package/dist/thread/native/render/lines.js +122 -0
  58. package/dist/thread/native/render/module.js +26 -0
  59. package/dist/thread/native/render/pixel-ops.js +235 -0
  60. package/dist/thread/native/render/tessellator.js +250 -0
  61. package/dist/thread/player/avm1/amf.js +507 -0
  62. package/dist/thread/player/avm1/avm1.js +1304 -0
  63. package/dist/thread/player/avm1/bindings.js +214 -0
  64. package/dist/thread/player/avm1/classes.js +75 -0
  65. package/dist/thread/player/avm1/clips.js +229 -0
  66. package/dist/thread/player/avm1/compile.js +140 -0
  67. package/dist/thread/player/avm1/external.js +79 -0
  68. package/dist/thread/player/avm1/focus.js +278 -0
  69. package/dist/thread/player/avm1/globals/BitmapData.js +542 -0
  70. package/dist/thread/player/avm1/globals/Button.js +31 -0
  71. package/dist/thread/player/avm1/globals/Color.js +115 -0
  72. package/dist/thread/player/avm1/globals/ExternalInterface.js +310 -0
  73. package/dist/thread/player/avm1/globals/FileReference.js +217 -0
  74. package/dist/thread/player/avm1/globals/Key.js +60 -0
  75. package/dist/thread/player/avm1/globals/LoadVars.js +208 -0
  76. package/dist/thread/player/avm1/globals/LocalConnection.js +92 -0
  77. package/dist/thread/player/avm1/globals/MovieClip.js +1070 -0
  78. package/dist/thread/player/avm1/globals/MovieClipLoader.js +90 -0
  79. package/dist/thread/player/avm1/globals/NetConnection.js +261 -0
  80. package/dist/thread/player/avm1/globals/PrintJob.js +37 -0
  81. package/dist/thread/player/avm1/globals/Selection.js +62 -0
  82. package/dist/thread/player/avm1/globals/SharedObject.js +245 -0
  83. package/dist/thread/player/avm1/globals/Sound.js +238 -0
  84. package/dist/thread/player/avm1/globals/Stage.js +12 -0
  85. package/dist/thread/player/avm1/globals/StyleSheet.js +205 -0
  86. package/dist/thread/player/avm1/globals/TextField.js +396 -0
  87. package/dist/thread/player/avm1/globals/TextFormat.js +194 -0
  88. package/dist/thread/player/avm1/globals/TextSnapshot.js +88 -0
  89. package/dist/thread/player/avm1/globals/XML.js +396 -0
  90. package/dist/thread/player/avm1/globals/XMLNode.js +413 -0
  91. package/dist/thread/player/avm1/globals/XMLSocket.js +205 -0
  92. package/dist/thread/player/avm1/globals/drawing.js +202 -0
  93. package/dist/thread/player/avm1/globals/filters.js +635 -0
  94. package/dist/thread/player/avm1/globals/geom.js +600 -0
  95. package/dist/thread/player/avm1/input.js +483 -0
  96. package/dist/thread/player/avm1/keyboard.js +113 -0
  97. package/dist/thread/player/avm1/loads.js +352 -0
  98. package/dist/thread/player/avm1/net.js +219 -0
  99. package/dist/thread/player/avm1/paths.js +104 -0
  100. package/dist/thread/player/avm1/pointer.js +512 -0
  101. package/dist/thread/player/avm1/queue.js +303 -0
  102. package/dist/thread/player/avm1/sound.js +106 -0
  103. package/dist/thread/player/avm1/swf.js +141 -0
  104. package/dist/thread/player/avm1/timers.js +113 -0
  105. package/dist/thread/player/bitmap/bitmap.js +627 -0
  106. package/dist/thread/player/bitmap/draw.js +68 -0
  107. package/dist/thread/player/bitmap/dxt.js +11 -0
  108. package/dist/thread/player/bitmap/filters.js +656 -0
  109. package/dist/thread/player/bitmap/images.js +240 -0
  110. package/dist/thread/player/bitmap/kernel.js +65 -0
  111. package/dist/thread/player/bitmap/ops.js +1239 -0
  112. package/dist/thread/player/bitmap/pixel-bender.js +911 -0
  113. package/dist/thread/player/bitmap/png.js +139 -0
  114. package/dist/thread/player/bitmap/turbulence.js +89 -0
  115. package/dist/thread/player/clock.js +182 -0
  116. package/dist/thread/player/display/bitmapobject.js +25 -0
  117. package/dist/thread/player/display/bounds.js +520 -0
  118. package/dist/thread/player/display/buttonobject.js +305 -0
  119. package/dist/thread/player/display/color.js +40 -0
  120. package/dist/thread/player/display/container.js +150 -0
  121. package/dist/thread/player/display/display.js +14 -0
  122. package/dist/thread/player/display/displayobject.js +664 -0
  123. package/dist/thread/player/display/drawing.js +613 -0
  124. package/dist/thread/player/display/filters.js +285 -0
  125. package/dist/thread/player/display/geometry.js +189 -0
  126. package/dist/thread/player/display/gradients.js +117 -0
  127. package/dist/thread/player/display/graphicsdata.js +592 -0
  128. package/dist/thread/player/display/instantiate.js +71 -0
  129. package/dist/thread/player/display/matrix3d.js +373 -0
  130. package/dist/thread/player/display/morph.js +226 -0
  131. package/dist/thread/player/display/movieclip.js +680 -0
  132. package/dist/thread/player/display/perspective.js +193 -0
  133. package/dist/thread/player/display/scale9.js +236 -0
  134. package/dist/thread/player/display/shapeobject.js +131 -0
  135. package/dist/thread/player/display/shapes.js +407 -0
  136. package/dist/thread/player/display/textobjects.js +222 -0
  137. package/dist/thread/player/display/timeline.js +471 -0
  138. package/dist/thread/player/display/value-lists.js +40 -0
  139. package/dist/thread/player/display/videoobject.js +6 -0
  140. package/dist/thread/player/display/walk.js +127 -0
  141. package/dist/thread/player/files.js +334 -0
  142. package/dist/thread/player/hosts.js +207 -0
  143. package/dist/thread/player/input/avm1.js +82 -0
  144. package/dist/thread/player/input/context-menu.js +63 -0
  145. package/dist/thread/player/input/keyboard.js +458 -0
  146. package/dist/thread/player/input/pointer.js +863 -0
  147. package/dist/thread/player/input/text-editor.js +319 -0
  148. package/dist/thread/player/input/touch.js +209 -0
  149. package/dist/thread/player/localconnection.js +90 -0
  150. package/dist/thread/player/media/audio.js +376 -0
  151. package/dist/thread/player/media/dynamic-sound.js +90 -0
  152. package/dist/thread/player/media/extract.js +135 -0
  153. package/dist/thread/player/media/mp3.js +152 -0
  154. package/dist/thread/player/media/sounds.js +507 -0
  155. package/dist/thread/player/media/streaming-audio.js +133 -0
  156. package/dist/thread/player/player.js +616 -0
  157. package/dist/thread/player/playerglobal/air/net/WebSocket.js +250 -0
  158. package/dist/thread/player/playerglobal/air-library.js +8 -0
  159. package/dist/thread/player/playerglobal/flash/accessibility/Accessibility.js +21 -0
  160. package/dist/thread/player/playerglobal/flash/crypto/generateRandomBytes.js +32 -0
  161. package/dist/thread/player/playerglobal/flash/data/EncryptedLocalStore.js +61 -0
  162. package/dist/thread/player/playerglobal/flash/desktop/Clipboard.js +173 -0
  163. package/dist/thread/player/playerglobal/flash/desktop/NativeApplication.js +179 -0
  164. package/dist/thread/player/playerglobal/flash/display/AVM1Movie.js +19 -0
  165. package/dist/thread/player/playerglobal/flash/display/Bitmap.js +58 -0
  166. package/dist/thread/player/playerglobal/flash/display/BitmapData.js +633 -0
  167. package/dist/thread/player/playerglobal/flash/display/DisplayObject.js +614 -0
  168. package/dist/thread/player/playerglobal/flash/display/DisplayObjectContainer.js +224 -0
  169. package/dist/thread/player/playerglobal/flash/display/Graphics.js +345 -0
  170. package/dist/thread/player/playerglobal/flash/display/InteractiveObject.js +87 -0
  171. package/dist/thread/player/playerglobal/flash/display/Loader.js +79 -0
  172. package/dist/thread/player/playerglobal/flash/display/LoaderInfo.js +128 -0
  173. package/dist/thread/player/playerglobal/flash/display/MovieClip.js +194 -0
  174. package/dist/thread/player/playerglobal/flash/display/NativeWindow.js +245 -0
  175. package/dist/thread/player/playerglobal/flash/display/Shader.js +26 -0
  176. package/dist/thread/player/playerglobal/flash/display/ShaderData.js +85 -0
  177. package/dist/thread/player/playerglobal/flash/display/ShaderInput.js +64 -0
  178. package/dist/thread/player/playerglobal/flash/display/ShaderJob.js +181 -0
  179. package/dist/thread/player/playerglobal/flash/display/ShaderParameter.js +23 -0
  180. package/dist/thread/player/playerglobal/flash/display/Shape.js +13 -0
  181. package/dist/thread/player/playerglobal/flash/display/SimpleButton.js +103 -0
  182. package/dist/thread/player/playerglobal/flash/display/Sprite.js +84 -0
  183. package/dist/thread/player/playerglobal/flash/display/Stage.js +231 -0
  184. package/dist/thread/player/playerglobal/flash/display/Stage3D.js +54 -0
  185. package/dist/thread/player/playerglobal/flash/display/shaders.js +228 -0
  186. package/dist/thread/player/playerglobal/flash/events/Event.js +58 -0
  187. package/dist/thread/player/playerglobal/flash/events/EventDispatcher.js +103 -0
  188. package/dist/thread/player/playerglobal/flash/events/GestureEvent.js +34 -0
  189. package/dist/thread/player/playerglobal/flash/events/HTTPStatusEvent.js +8 -0
  190. package/dist/thread/player/playerglobal/flash/events/KeyboardEvent.js +51 -0
  191. package/dist/thread/player/playerglobal/flash/events/MouseEvent.js +59 -0
  192. package/dist/thread/player/playerglobal/flash/events/PressAndTapGestureEvent.js +30 -0
  193. package/dist/thread/player/playerglobal/flash/events/TimerEvent.js +16 -0
  194. package/dist/thread/player/playerglobal/flash/events/TouchEvent.js +45 -0
  195. package/dist/thread/player/playerglobal/flash/external/ExternalInterface.js +73 -0
  196. package/dist/thread/player/playerglobal/flash/filesystem/File.js +340 -0
  197. package/dist/thread/player/playerglobal/flash/filesystem/FileStream.js +453 -0
  198. package/dist/thread/player/playerglobal/flash/filesystem/paths.js +156 -0
  199. package/dist/thread/player/playerglobal/flash/filesystem/writes.js +143 -0
  200. package/dist/thread/player/playerglobal/flash/filters/BevelFilter.js +4 -0
  201. package/dist/thread/player/playerglobal/flash/filters/BitmapFilter.js +205 -0
  202. package/dist/thread/player/playerglobal/flash/filters/BlurFilter.js +4 -0
  203. package/dist/thread/player/playerglobal/flash/filters/ColorMatrixFilter.js +11 -0
  204. package/dist/thread/player/playerglobal/flash/filters/ConvolutionFilter.js +26 -0
  205. package/dist/thread/player/playerglobal/flash/filters/DisplacementMapFilter.js +51 -0
  206. package/dist/thread/player/playerglobal/flash/filters/DropShadowFilter.js +4 -0
  207. package/dist/thread/player/playerglobal/flash/filters/GlowFilter.js +4 -0
  208. package/dist/thread/player/playerglobal/flash/filters/GradientBevelFilter.js +4 -0
  209. package/dist/thread/player/playerglobal/flash/filters/GradientGlowFilter.js +4 -0
  210. package/dist/thread/player/playerglobal/flash/filters/ShaderFilter.js +53 -0
  211. package/dist/thread/player/playerglobal/flash/geom/Matrix3D.js +361 -0
  212. package/dist/thread/player/playerglobal/flash/geom/PerspectiveProjection.js +122 -0
  213. package/dist/thread/player/playerglobal/flash/geom/Transform.js +245 -0
  214. package/dist/thread/player/playerglobal/flash/geom/Utils3D.js +85 -0
  215. package/dist/thread/player/playerglobal/flash/globalization/Collator.js +149 -0
  216. package/dist/thread/player/playerglobal/flash/globalization/CurrencyFormatter.js +272 -0
  217. package/dist/thread/player/playerglobal/flash/globalization/CurrencyParseResult.js +21 -0
  218. package/dist/thread/player/playerglobal/flash/globalization/DateTimeFormatter.js +463 -0
  219. package/dist/thread/player/playerglobal/flash/globalization/LocaleID.js +144 -0
  220. package/dist/thread/player/playerglobal/flash/globalization/NumberFormatter.js +444 -0
  221. package/dist/thread/player/playerglobal/flash/globalization/NumberParseResult.js +22 -0
  222. package/dist/thread/player/playerglobal/flash/globalization/StringTools.js +59 -0
  223. package/dist/thread/player/playerglobal/flash/globalization/locale.js +190 -0
  224. package/dist/thread/player/playerglobal/flash/media/Camera.js +89 -0
  225. package/dist/thread/player/playerglobal/flash/media/Microphone.js +126 -0
  226. package/dist/thread/player/playerglobal/flash/media/Sound.js +543 -0
  227. package/dist/thread/player/playerglobal/flash/media/SoundMixer.js +74 -0
  228. package/dist/thread/player/playerglobal/flash/media/SoundTransform.js +40 -0
  229. package/dist/thread/player/playerglobal/flash/media/Video.js +43 -0
  230. package/dist/thread/player/playerglobal/flash/net/FileFilter.js +27 -0
  231. package/dist/thread/player/playerglobal/flash/net/FileReference.js +197 -0
  232. package/dist/thread/player/playerglobal/flash/net/LocalConnection.js +167 -0
  233. package/dist/thread/player/playerglobal/flash/net/NetConnection.js +155 -0
  234. package/dist/thread/player/playerglobal/flash/net/SharedObject.js +299 -0
  235. package/dist/thread/player/playerglobal/flash/net/Socket.js +282 -0
  236. package/dist/thread/player/playerglobal/flash/net/URLRequest.js +93 -0
  237. package/dist/thread/player/playerglobal/flash/net/URLStream.js +146 -0
  238. package/dist/thread/player/playerglobal/flash/net/XMLSocket.js +14 -0
  239. package/dist/thread/player/playerglobal/flash/net/local-modern.js +142 -0
  240. package/dist/thread/player/playerglobal/flash/net/local-values.js +217 -0
  241. package/dist/thread/player/playerglobal/flash/net/navigateToURL.js +29 -0
  242. package/dist/thread/player/playerglobal/flash/profiler/Telemetry.js +25 -0
  243. package/dist/thread/player/playerglobal/flash/sensors/Accelerometer.js +12 -0
  244. package/dist/thread/player/playerglobal/flash/system/ApplicationDomain.js +69 -0
  245. package/dist/thread/player/playerglobal/flash/system/Capabilities.js +163 -0
  246. package/dist/thread/player/playerglobal/flash/system/FSCommandHost.js +17 -0
  247. package/dist/thread/player/playerglobal/flash/system/Security.js +46 -0
  248. package/dist/thread/player/playerglobal/flash/system/SecurityDomain.js +21 -0
  249. package/dist/thread/player/playerglobal/flash/system/System.js +73 -0
  250. package/dist/thread/player/playerglobal/flash/system/Worker.js +112 -0
  251. package/dist/thread/player/playerglobal/flash/text/Font.js +95 -0
  252. package/dist/thread/player/playerglobal/flash/text/StaticText.js +16 -0
  253. package/dist/thread/player/playerglobal/flash/text/StyleSheet.js +65 -0
  254. package/dist/thread/player/playerglobal/flash/text/TextField.js +707 -0
  255. package/dist/thread/player/playerglobal/flash/text/TextRenderer.js +77 -0
  256. package/dist/thread/player/playerglobal/flash/text/engine/FontDescription.js +141 -0
  257. package/dist/thread/player/playerglobal/flash/text/engine/TabStop.js +46 -0
  258. package/dist/thread/player/playerglobal/flash/trace/Trace.js +17 -0
  259. package/dist/thread/player/playerglobal/flash/ui/ContextMenu.js +17 -0
  260. package/dist/thread/player/playerglobal/flash/ui/GameInput.js +19 -0
  261. package/dist/thread/player/playerglobal/flash/ui/Keyboard.js +27 -0
  262. package/dist/thread/player/playerglobal/flash/ui/Mouse.js +127 -0
  263. package/dist/thread/player/playerglobal/flash/ui/Multitouch.js +46 -0
  264. package/dist/thread/player/playerglobal/flash/utils/ByteArray.js +155 -0
  265. package/dist/thread/player/playerglobal/flash/utils/Timer.js +25 -0
  266. package/dist/thread/player/playerglobal/flash/xml/XMLDocument.js +74 -0
  267. package/dist/thread/player/playerglobal/index.js +224 -0
  268. package/dist/thread/player/playerglobal/libraries.js +10 -0
  269. package/dist/thread/player/playerglobal/playerglobal-library.js +22 -0
  270. package/dist/thread/player/playerglobal/toplevel.js +122 -0
  271. package/dist/thread/player/scripting/clipboard.js +146 -0
  272. package/dist/thread/player/scripting/code.js +900 -0
  273. package/dist/thread/player/scripting/evaluate.js +144 -0
  274. package/dist/thread/player/scripting/events.js +172 -0
  275. package/dist/thread/player/scripting/handover.js +182 -0
  276. package/dist/thread/player/scripting/lazy.js +38 -0
  277. package/dist/thread/player/scripting/lifecycle.js +272 -0
  278. package/dist/thread/player/scripting/loads.js +860 -0
  279. package/dist/thread/player/scripting/sha256.js +95 -0
  280. package/dist/thread/player/scripting/symbols.js +348 -0
  281. package/dist/thread/player/scripting/timers.js +434 -0
  282. package/dist/thread/player/scripting/workers.js +923 -0
  283. package/dist/thread/player/scripting.js +1018 -0
  284. package/dist/thread/player/stage.js +4 -0
  285. package/dist/thread/player/text/csm.js +113 -0
  286. package/dist/thread/player/text/css.js +167 -0
  287. package/dist/thread/player/text/fonts.js +97 -0
  288. package/dist/thread/player/text/layout.js +393 -0
  289. package/dist/thread/player/text/static.js +109 -0
  290. package/dist/thread/player/text/text.js +431 -0
  291. package/dist/thread/player/workers/thread.js +159 -0
  292. package/dist/thread/player/workers/wire.js +198 -0
  293. package/dist/thread/player-hosts/web-worker.js +19 -0
  294. package/dist/thread/runtime/avm1/activation.js +89 -0
  295. package/dist/thread/runtime/avm1/case.js +249 -0
  296. package/dist/thread/runtime/avm1/clips.js +334 -0
  297. package/dist/thread/runtime/avm1/control.js +280 -0
  298. package/dist/thread/runtime/avm1/errors.js +50 -0
  299. package/dist/thread/runtime/avm1/functions.js +249 -0
  300. package/dist/thread/runtime/avm1/globals/array.js +549 -0
  301. package/dist/thread/runtime/avm1/globals/broadcaster.js +131 -0
  302. package/dist/thread/runtime/avm1/globals/date.js +436 -0
  303. package/dist/thread/runtime/avm1/globals/global.js +344 -0
  304. package/dist/thread/runtime/avm1/globals/math.js +139 -0
  305. package/dist/thread/runtime/avm1/globals/number.js +93 -0
  306. package/dist/thread/runtime/avm1/globals/string.js +201 -0
  307. package/dist/thread/runtime/avm1/globals/timers.js +129 -0
  308. package/dist/thread/runtime/avm1/host.js +5 -0
  309. package/dist/thread/runtime/avm1/index.js +14 -0
  310. package/dist/thread/runtime/avm1/inheritance.js +132 -0
  311. package/dist/thread/runtime/avm1/numbers.js +298 -0
  312. package/dist/thread/runtime/avm1/objects.js +896 -0
  313. package/dist/thread/runtime/avm1/operators.js +170 -0
  314. package/dist/thread/runtime/avm1/realm.js +529 -0
  315. package/dist/thread/runtime/avm1/runtime.js +882 -0
  316. package/dist/thread/runtime/avm1/scope.js +385 -0
  317. package/dist/thread/runtime/avm1/urls.js +180 -0
  318. package/dist/thread/runtime/avm1/values.js +97 -0
  319. package/dist/thread/runtime/avm2/amf-classes.js +66 -0
  320. package/dist/thread/runtime/avm2/amf.js +801 -0
  321. package/dist/thread/runtime/avm2/array-conversion.js +100 -0
  322. package/dist/thread/runtime/avm2/builtin-library.js +21 -0
  323. package/dist/thread/runtime/avm2/cache-fill.js +86 -0
  324. package/dist/thread/runtime/avm2/classes.js +211 -0
  325. package/dist/thread/runtime/avm2/definitions.js +69 -0
  326. package/dist/thread/runtime/avm2/descriptors.js +5 -0
  327. package/dist/thread/runtime/avm2/dictionary-keys.js +295 -0
  328. package/dist/thread/runtime/avm2/domain-memory.js +75 -0
  329. package/dist/thread/runtime/avm2/domain.js +280 -0
  330. package/dist/thread/runtime/avm2/dynamic-properties.js +182 -0
  331. package/dist/thread/runtime/avm2/enumeration.js +393 -0
  332. package/dist/thread/runtime/avm2/errors.js +45 -0
  333. package/dist/thread/runtime/avm2/float-library.js +66 -0
  334. package/dist/thread/runtime/avm2/float.js +26 -0
  335. package/dist/thread/runtime/avm2/hooks.js +19 -0
  336. package/dist/thread/runtime/avm2/index.js +38 -0
  337. package/dist/thread/runtime/avm2/messages.js +177 -0
  338. package/dist/thread/runtime/avm2/method-factories.js +95 -0
  339. package/dist/thread/runtime/avm2/modules.js +105 -0
  340. package/dist/thread/runtime/avm2/names.js +248 -0
  341. package/dist/thread/runtime/avm2/natives/aliases.js +47 -0
  342. package/dist/thread/runtime/avm2/natives/array.js +390 -0
  343. package/dist/thread/runtime/avm2/natives/bytearray.js +1227 -0
  344. package/dist/thread/runtime/avm2/natives/case.js +88 -0
  345. package/dist/thread/runtime/avm2/natives/concurrent.js +199 -0
  346. package/dist/thread/runtime/avm2/natives/date.js +581 -0
  347. package/dist/thread/runtime/avm2/natives/define.js +130 -0
  348. package/dist/thread/runtime/avm2/natives/describe.js +351 -0
  349. package/dist/thread/runtime/avm2/natives/dictionary.js +12 -0
  350. package/dist/thread/runtime/avm2/natives/float-vector.js +81 -0
  351. package/dist/thread/runtime/avm2/natives/float.js +96 -0
  352. package/dist/thread/runtime/avm2/natives/index.js +64 -0
  353. package/dist/thread/runtime/avm2/natives/json.js +514 -0
  354. package/dist/thread/runtime/avm2/natives/number.js +162 -0
  355. package/dist/thread/runtime/avm2/natives/object.js +237 -0
  356. package/dist/thread/runtime/avm2/natives/proxy.js +75 -0
  357. package/dist/thread/runtime/avm2/natives/regexp.js +485 -0
  358. package/dist/thread/runtime/avm2/natives/shell-workers.js +404 -0
  359. package/dist/thread/runtime/avm2/natives/shell.js +557 -0
  360. package/dist/thread/runtime/avm2/natives/sort.js +377 -0
  361. package/dist/thread/runtime/avm2/natives/string.js +132 -0
  362. package/dist/thread/runtime/avm2/natives/sync.js +821 -0
  363. package/dist/thread/runtime/avm2/natives/toplevel.js +139 -0
  364. package/dist/thread/runtime/avm2/natives/vector-elements.js +46 -0
  365. package/dist/thread/runtime/avm2/natives/vector.js +406 -0
  366. package/dist/thread/runtime/avm2/natives/xml/access.js +633 -0
  367. package/dist/thread/runtime/avm2/natives/xml/build.js +291 -0
  368. package/dist/thread/runtime/avm2/natives/xml/chars.js +112 -0
  369. package/dist/thread/runtime/avm2/natives/xml/escape.js +38 -0
  370. package/dist/thread/runtime/avm2/natives/xml/names.js +204 -0
  371. package/dist/thread/runtime/avm2/natives/xml/node.js +272 -0
  372. package/dist/thread/runtime/avm2/natives/xml/operators.js +69 -0
  373. package/dist/thread/runtime/avm2/natives/xml/parser.js +356 -0
  374. package/dist/thread/runtime/avm2/natives/xml/strings.js +191 -0
  375. package/dist/thread/runtime/avm2/natives/xml/values.js +122 -0
  376. package/dist/thread/runtime/avm2/natives/xml/xml.js +645 -0
  377. package/dist/thread/runtime/avm2/number-strings.js +110 -0
  378. package/dist/thread/runtime/avm2/numbers.js +558 -0
  379. package/dist/thread/runtime/avm2/options.js +16 -0
  380. package/dist/thread/runtime/avm2/player-messages.js +550 -0
  381. package/dist/thread/runtime/avm2/property-cache.js +135 -0
  382. package/dist/thread/runtime/avm2/runtime.js +2644 -0
  383. package/dist/thread/runtime/avm2/traits.js +509 -0
  384. package/dist/thread/runtime/index.js +7 -0
  385. package/dist/web-worker.d.ts +2 -0
  386. package/dist/web-worker.d.ts.map +1 -0
  387. package/dist/web-worker.js +20 -0
  388. package/dist/web-worker.js.map +1 -0
  389. package/dist/websocket.d.ts +4 -0
  390. package/dist/websocket.d.ts.map +1 -0
  391. package/dist/websocket.js +27 -0
  392. package/dist/websocket.js.map +1 -0
  393. package/dist/workers.d.ts +35 -0
  394. package/dist/workers.d.ts.map +1 -0
  395. package/dist/workers.js +171 -0
  396. package/dist/workers.js.map +1 -0
  397. package/package.json +62 -3
  398. package/src/indexeddb.ts +227 -0
  399. package/src/node-worker.ts +13 -0
  400. package/src/node.ts +49 -0
  401. package/src/precompiled.ts +166 -0
  402. package/src/stage3d.ts +76 -0
  403. package/src/web-worker.ts +24 -0
  404. package/src/websocket.ts +32 -0
  405. package/src/workers.ts +255 -0
@@ -0,0 +1,1227 @@
1
+ // flash.utils.ByteArray and domain memory, as avmplus' ByteArray and
2
+ // DataInput/DataOutput implement them. The buffer keeps avmplus' capacity:
3
+ // it grows as avmplus' Grower grows it, zeroing what a reallocation adds,
4
+ // so bytes a shorter length left behind come back when the length grows
5
+ // within the capacity, as they do in avmplus. UTF-8 is avmplus' own: an
6
+ // invalid byte reads as the character of its value.
7
+ //
8
+ // Translated from avmplus' core/ByteArrayGlue.cpp, core/DataIO.cpp and
9
+ // core/UnicodeUtils.cpp, this file is subject to the Mozilla Public
10
+ // License, v. 2.0: http://mozilla.org/MPL/2.0/.
11
+ import { CompressedDataError, CompressedDataTooLargeError, deflateCompress, LZMA_HEADER, lzmaByteArrayCompress, lzmaByteArrayUncompress, zlibCompress, zlibUncompress, } from "../../../format/index.js";
12
+ import { registerNativeClass } from "./define.js";
13
+ const kGrowthIncr = 4096;
14
+ const kHugeGrowthThreshold = 24 * 1024 * 1024;
15
+ const kHugeGrowthIncr = 24 * 1024 * 1024;
16
+ /**
17
+ * The most a ByteArray holds: what a 32-bit avmshell, as the oracle's, can
18
+ * allocate, beyond which it fails with MemoryError 1000, and less than a
19
+ * browser's ArrayBuffer may be refused at.
20
+ */
21
+ const kMaxCapacity = 0x80000000;
22
+ /** DomainEnv::GLOBAL_MEMORY_MIN_SIZE: domain memory's least length. */
23
+ export const GLOBAL_MEMORY_MIN_SIZE = 1024;
24
+ const kAMF0 = 0;
25
+ const kAMF3 = 3;
26
+ /** The ways a browser serves a page cross-origin isolated, for the error a page that is not gets. */
27
+ const ISOLATION = "a cross-origin isolated page (served with Cross-Origin-Opener-Policy: same-origin and " +
28
+ "Cross-Origin-Embedder-Policy: credentialless or require-corp)";
29
+ /**
30
+ * Whether this thread can share memory with others: SharedArrayBuffer is
31
+ * there, as node always has it, and a browser only for a page that is
32
+ * cross-origin isolated.
33
+ */
34
+ export function canShareMemory() {
35
+ const g = globalThis;
36
+ return typeof SharedArrayBuffer === "function" && g.crossOriginIsolated !== false;
37
+ }
38
+ /** What a page that is not cross-origin isolated gets for sharing `what` with another worker. */
39
+ export function notIsolated(what) {
40
+ return new Error(`swf2es: sharing ${what} with another worker needs shared memory, which a browser gives only ${ISOLATION}`);
41
+ }
42
+ /** Each runtime's ByteArray for each shared memory, by its id. */
43
+ const sharedObjects = new WeakMap();
44
+ function sharedObjectsOf(rt) {
45
+ let objects = sharedObjects.get(rt);
46
+ if (!objects) {
47
+ objects = new Map();
48
+ sharedObjects.set(rt, objects);
49
+ }
50
+ return objects;
51
+ }
52
+ /** The capacity of each runtime's ByteArrays, live or not yet collected. */
53
+ const runtimeCapacity = new WeakMap();
54
+ /** A ByteArray collected: its capacity no longer counted, by its runtime's count and its own. */
55
+ const collected = new FinalizationRegistry(({ runtime, own }) => {
56
+ runtime.bytes -= own.bytes;
57
+ });
58
+ /** The capacity of `rt`'s ByteArrays, which avmshell's System counts in its memory. */
59
+ export function byteArrayCapacity(rt) {
60
+ return runtimeCapacity.get(rt)?.bytes ?? 0;
61
+ }
62
+ /**
63
+ * A ByteArray's state: its buffer, as long as its capacity, and its length
64
+ * and position. A read or write takes its offset first: growing replaces
65
+ * the buffer and its view.
66
+ *
67
+ * A length or position is stored through `>>> 0`, as a uint32: an int32
68
+ * while below 2^31, and a double only from there.
69
+ * JavaScriptCore keeps a number its JIT once made a double a double, and
70
+ * an addition that has seen one makes doubles from then on: a double
71
+ * offset fails the int32 check of a DataView or typed array access, and
72
+ * after enough failures JavaScriptCore calls DataView's methods instead
73
+ * of inlining them, a quarter slower on every read and write. V8 makes
74
+ * small integers of them itself.
75
+ */
76
+ export class Bytes {
77
+ rt;
78
+ owner;
79
+ buffer = new Uint8Array(0);
80
+ view = new DataView(this.buffer.buffer);
81
+ length = 0;
82
+ position = 0;
83
+ littleEndian = false;
84
+ /** As ByteArray's constructor: ByteArray.defaultObjectEncoding when it is made. */
85
+ objectEncoding;
86
+ /** Whether it is the domain memory, which it then tells when its buffer or length changes. */
87
+ subscribed = false;
88
+ /**
89
+ * Whether `shareable` was set true, which cannot be undone: the
90
+ * ByteArray then crosses to another worker as its memory, not a copy.
91
+ */
92
+ shareable = false;
93
+ /**
94
+ * Its memory once it has crossed to another worker (share), which its
95
+ * buffer and view are then over, tracking its length as it grows; null
96
+ * until then, when it is a ByteArray like any other.
97
+ */
98
+ shared = null;
99
+ /** The shared memory's words, the length first, and its bytes as words for the atomics. */
100
+ sharedWords = null;
101
+ sharedInts = null;
102
+ /** Its runtime's capacity count, and its own part of it. */
103
+ capacity;
104
+ constructor(rt, owner) {
105
+ this.rt = rt;
106
+ this.owner = owner;
107
+ this.objectEncoding = rt.defaultObjectEncoding;
108
+ let runtime = runtimeCapacity.get(rt);
109
+ if (!runtime) {
110
+ runtime = { bytes: 0 };
111
+ runtimeCapacity.set(rt, runtime);
112
+ }
113
+ this.capacity = { runtime, own: { bytes: 0 } };
114
+ collected.register(this, this.capacity);
115
+ }
116
+ /** Its buffer replaced by `next`, its runtime's capacity count with it. */
117
+ setBuffer(next) {
118
+ const { runtime, own } = this.capacity;
119
+ runtime.bytes += next.length - own.bytes;
120
+ own.bytes = next.length;
121
+ this.buffer = next;
122
+ this.view = new DataView(next.buffer);
123
+ }
124
+ get available() {
125
+ return this.length > this.position ? this.length - this.position : 0;
126
+ }
127
+ /** As Grower::ReallocBackingStore: a new buffer, the old length's bytes copied and the rest zero. */
128
+ realloc(capacityIn, minimum, fromSetter) {
129
+ let capacity = capacityIn;
130
+ if (capacity < minimum) {
131
+ capacity = minimum;
132
+ }
133
+ if (!(this.buffer.length === 0 && fromSetter) && capacity < kGrowthIncr) {
134
+ capacity = kGrowthIncr;
135
+ }
136
+ if (minimum > kMaxCapacity) {
137
+ throw this.rt.error("flash.errors::MemoryError", 1000);
138
+ }
139
+ capacity = Math.min(capacity, kMaxCapacity);
140
+ if (capacity === this.buffer.length) {
141
+ return;
142
+ }
143
+ if (this.shared !== null) {
144
+ this.reshare(capacity);
145
+ return;
146
+ }
147
+ let next;
148
+ try {
149
+ next = new Uint8Array(capacity);
150
+ }
151
+ catch {
152
+ // The host would not give it the memory, as the system would not give avmplus.
153
+ throw this.rt.error("flash.errors::MemoryError", 1000);
154
+ }
155
+ next.set(this.buffer.subarray(0, Math.min(capacity, this.length)));
156
+ this.setBuffer(next);
157
+ }
158
+ /** As Grower::EnsureWritableCapacity: double, at least `minimum`, and 4096 unless the setter sizes an empty one. */
159
+ ensure(minimum, fromSetter) {
160
+ if (minimum > 0xffffffff - 2 * 4096) {
161
+ throw this.rt.error("flash.errors::MemoryError", 1000);
162
+ }
163
+ if (minimum > this.buffer.length) {
164
+ this.realloc(this.buffer.length * 2, minimum, fromSetter);
165
+ }
166
+ }
167
+ /** As ByteArray::SetLengthCommon. */
168
+ setLength(length, fromSetter = false) {
169
+ if (this.subscribed && length < GLOBAL_MEMORY_MIN_SIZE) {
170
+ throw this.rt.error("RangeError", 1506);
171
+ }
172
+ this.reserve(length, fromSetter);
173
+ this.length = length >>> 0;
174
+ if (this.position > length) {
175
+ this.position = length >>> 0;
176
+ }
177
+ this.notify();
178
+ }
179
+ /**
180
+ * The length made at least `length`, as a write past the end makes it:
181
+ * shared, by a compare and swap that only ever raises it, so that
182
+ * workers appending at once never lower what another made it.
183
+ */
184
+ extend(length) {
185
+ if (this.shared === null) {
186
+ this.setLength(length);
187
+ return;
188
+ }
189
+ this.reserve(length, false);
190
+ this.raise(length);
191
+ this.notify();
192
+ }
193
+ /** A shared length raised to `length` unless another worker made it as long. */
194
+ raise(length) {
195
+ const words = this.sharedWords;
196
+ for (;;) {
197
+ const now = Atomics.load(words, 0) >>> 0;
198
+ if (now >= length || Atomics.compareExchange(words, 0, now | 0, length | 0) >>> 0 === now) {
199
+ return;
200
+ }
201
+ }
202
+ }
203
+ /** As Grower::SetLengthCommon, before the length is set: the capacity for `length`. */
204
+ reserve(length, fromSetter) {
205
+ if (!fromSetter || (length < kHugeGrowthThreshold && this.length < kHugeGrowthThreshold)) {
206
+ if (length > this.buffer.length) {
207
+ this.ensure(length, fromSetter);
208
+ }
209
+ }
210
+ else {
211
+ const rounded = Math.ceil(length / kHugeGrowthIncr) * kHugeGrowthIncr;
212
+ const capacity = rounded <= 0xffffffff - 2 * 4096 ? rounded : length;
213
+ if (capacity !== this.buffer.length) {
214
+ this.realloc(capacity, length, true);
215
+ }
216
+ }
217
+ }
218
+ /** Tell the domain memory, if this is it, where the bytes are now: a new view only if they moved or their length changed. */
219
+ notify() {
220
+ const rt = this.rt;
221
+ if (this.subscribed &&
222
+ (rt.memory.buffer !== this.buffer.buffer || rt.memoryLength !== this.length)) {
223
+ rt.memory = new DataView(this.buffer.buffer, 0, this.length);
224
+ }
225
+ }
226
+ /** As ByteArray::Clear: no buffer at all; shared, every worker's ByteArray for it empty. */
227
+ clear() {
228
+ if (this.subscribed) {
229
+ throw this.rt.error("RangeError", 1506);
230
+ }
231
+ this.position = 0;
232
+ if (this.shared !== null) {
233
+ // The memory cannot shrink: what it held is zeroed, as a new buffer
234
+ // would be once it grows again, before the length is told, so that a
235
+ // worker that grows it once it is empty writes into zeroes already.
236
+ this.buffer.fill(0);
237
+ this.length = 0;
238
+ return;
239
+ }
240
+ this.setBuffer(new Uint8Array(0));
241
+ this.length = 0;
242
+ }
243
+ /**
244
+ * Its memory, shared from now on: a growable SharedArrayBuffer its bytes
245
+ * move to once, and that this ByteArray, and every worker's ByteArray for
246
+ * it, uses from then on. Where memory cannot be shared, the error that
247
+ * names cross-origin isolation.
248
+ */
249
+ share() {
250
+ if (this.shared !== null) {
251
+ return this.shared;
252
+ }
253
+ if (!canShareMemory()) {
254
+ throw notIsolated("a shareable ByteArray");
255
+ }
256
+ const capacity = Math.max(this.buffer.length, this.length);
257
+ const Shared = SharedArrayBuffer;
258
+ let data = null;
259
+ // As much room to grow as a ByteArray has, or as much as the host reserves.
260
+ for (let max = kMaxCapacity; data === null; max /= 2) {
261
+ try {
262
+ data = new Shared(capacity, { maxByteLength: Math.max(max, capacity) });
263
+ }
264
+ catch {
265
+ if (max <= capacity) {
266
+ throw this.rt.error("flash.errors::MemoryError", 1000);
267
+ }
268
+ }
269
+ }
270
+ new Uint8Array(data).set(this.buffer);
271
+ const memory = {
272
+ id: Math.floor(Math.random() * 2 ** 52),
273
+ meta: new SharedArrayBuffer(8),
274
+ data,
275
+ };
276
+ new Int32Array(memory.meta)[0] = this.length | 0;
277
+ this.adopt(memory);
278
+ sharedObjectsOf(this.rt).set(memory.id, new WeakRef(this.owner));
279
+ return memory;
280
+ }
281
+ /**
282
+ * Use `memory`, shared, as its bytes and length from now on. Its length
283
+ * becomes an accessor of the memory's word on this object alone, so that
284
+ * every read sees another worker's change and every write is seen, and
285
+ * a ByteArray that shares nothing reads a plain field as before.
286
+ */
287
+ adopt(memory) {
288
+ this.shared = memory;
289
+ this.shareable = true;
290
+ const words = new Int32Array(memory.meta);
291
+ this.sharedWords = words;
292
+ Object.defineProperty(this, "length", {
293
+ get: () => Atomics.load(words, 0) >>> 0,
294
+ set: (length) => Atomics.store(words, 0, length | 0),
295
+ configurable: true,
296
+ });
297
+ // Views that track the memory's length as any worker grows it.
298
+ this.buffer = new Uint8Array(memory.data);
299
+ this.view = new DataView(memory.data);
300
+ this.sharedInts = new Int32Array(memory.data);
301
+ const { runtime, own } = this.capacity;
302
+ runtime.bytes += memory.data.byteLength - own.bytes;
303
+ own.bytes = memory.data.byteLength;
304
+ if (this.subscribed) {
305
+ this.rt.refreshMemory = () => this.notify();
306
+ }
307
+ this.notify();
308
+ }
309
+ /**
310
+ * A shared memory's capacity made at least `capacity`: it grows in place,
311
+ * which every worker sees, and never shrinks, as another worker may have
312
+ * grown it past what this one asks for since. Nor is what a smaller
313
+ * buffer would drop zeroed: without avmplus' safepoint, another worker
314
+ * may have grown the length again and written there meanwhile, so bytes
315
+ * past a lowered length may come back when it grows again.
316
+ */
317
+ reshare(capacity) {
318
+ const data = this.shared.data;
319
+ if (capacity <= data.byteLength) {
320
+ return;
321
+ }
322
+ if (capacity > data.maxByteLength) {
323
+ throw this.rt.error("flash.errors::MemoryError", 1000);
324
+ }
325
+ try {
326
+ data.grow(capacity);
327
+ }
328
+ catch {
329
+ // Another worker grew it meanwhile, to as much or more, or the host refused.
330
+ if (data.byteLength < capacity) {
331
+ throw this.rt.error("flash.errors::MemoryError", 1000);
332
+ }
333
+ }
334
+ const { runtime, own } = this.capacity;
335
+ runtime.bytes += data.byteLength - own.bytes;
336
+ own.bytes = data.byteLength;
337
+ }
338
+ /** As ByteArray::CAS, once the index is checked: atomic where the memory is shared. */
339
+ compareAndSwapInt(at, expected, next) {
340
+ if (this.sharedInts !== null) {
341
+ return Atomics.compareExchange(this.sharedInts, at >> 2, expected, next);
342
+ }
343
+ const previous = this.view.getInt32(at, true);
344
+ if (previous === expected) {
345
+ this.view.setInt32(at, next, true);
346
+ }
347
+ return previous;
348
+ }
349
+ /**
350
+ * As UnprotectedAtomicCompareAndSwapLength: the length set to `next`, as
351
+ * its setter sets it, if it is `expected`; the length before. Shared,
352
+ * the capacity is made first and the length swapped atomically, so that
353
+ * of workers growing it at once one wins.
354
+ */
355
+ compareAndSwapLength(expected, next) {
356
+ if (this.shared === null) {
357
+ const previous = this.length;
358
+ if (previous === expected) {
359
+ this.setLength(next, true);
360
+ }
361
+ return previous;
362
+ }
363
+ // Read once: another worker may change it between two reads.
364
+ const now = this.length;
365
+ if (now !== expected) {
366
+ return now;
367
+ }
368
+ // Only grows the memory, so it does nothing another worker would undo if the swap fails.
369
+ this.reserve(next, true);
370
+ const previous = Atomics.compareExchange(this.sharedWords, 0, expected | 0, next | 0) >>> 0;
371
+ if (previous === expected && this.position > next) {
372
+ this.position = next;
373
+ }
374
+ this.notify();
375
+ return previous;
376
+ }
377
+ /** As requestBytesForShortRead: the offset of `n` bytes to read, or EOFError. */
378
+ shortRead(n) {
379
+ const at = this.position;
380
+ if (at >= this.length || at + n > this.length) {
381
+ throw this.rt.error("flash.errors::EOFError", 2030);
382
+ }
383
+ this.position = (at + n) >>> 0;
384
+ return at;
385
+ }
386
+ /** As requestBytesForShortWrite: the offset of `n` bytes to write, the length grown to hold them. */
387
+ shortWrite(n) {
388
+ const at = this.position;
389
+ if (at >= this.length || at + n > this.length) {
390
+ if (at + n > 0xffffffff) {
391
+ throw this.rt.error("flash.errors::MemoryError", 1000);
392
+ }
393
+ this.extend(at + n);
394
+ }
395
+ this.position = (at + n) >>> 0;
396
+ return at;
397
+ }
398
+ /** As DataInput::CheckEOF. */
399
+ checkEOF(count) {
400
+ if (this.available < count) {
401
+ throw this.rt.error("flash.errors::EOFError", 2030);
402
+ }
403
+ }
404
+ /** As ByteArray::Read: `count` bytes from the position. */
405
+ read(count) {
406
+ this.checkEOF(count);
407
+ const bytes = this.buffer.slice(this.position, this.position + count);
408
+ this.position = (this.position + count) >>> 0;
409
+ return bytes;
410
+ }
411
+ /** As read, but the bytes themselves, not a copy: for a caller that only reads them, at once. */
412
+ readView(count) {
413
+ this.checkEOF(count);
414
+ const bytes = this.buffer.subarray(this.position, this.position + count);
415
+ this.position = (this.position + count) >>> 0;
416
+ return bytes;
417
+ }
418
+ /**
419
+ * As ByteArray::Write: `count` bytes of `source` from `offset`, all of
420
+ * it by default, at the position; the length grows to the position after.
421
+ */
422
+ write(source, offset = 0, count = source.length) {
423
+ if (count > 0xffffffff - this.position) {
424
+ throw this.rt.error("flash.errors::MemoryError", 1000);
425
+ }
426
+ const end = (this.position + count) >>> 0;
427
+ if (end > this.buffer.length) {
428
+ this.ensure(end, false);
429
+ }
430
+ copyBytes(this.buffer, this.position, source, offset, count);
431
+ this.position = end;
432
+ if (this.length < end) {
433
+ if (this.shared === null) {
434
+ this.length = end;
435
+ }
436
+ else {
437
+ this.raise(end);
438
+ }
439
+ }
440
+ this.notify();
441
+ }
442
+ /** As ByteArray's operator[] for a write: the length grows to hold `index`. */
443
+ setByte(index, value) {
444
+ if (index >= this.length) {
445
+ if (index === 0xffffffff) {
446
+ throw this.rt.error("flash.errors::MemoryError", 1000);
447
+ }
448
+ this.extend(index + 1);
449
+ }
450
+ this.buffer[index] = value;
451
+ }
452
+ }
453
+ /** Below this many bytes, copyBytes copies one at a time. */
454
+ const COPY_LOOP_LENGTH = 32;
455
+ /**
456
+ * `count` bytes of `source` from `offset` copied into `target` at `at`,
457
+ * as memmove copies them where the two are one array. Part of `source`,
458
+ * fewer than COPY_LOOP_LENGTH bytes, is copied one at a time: the
459
+ * subarray set() would take costs more than they do, in JavaScriptCore up
460
+ * to about 64 bytes and in V8 up to about 32. All of it set() copies
461
+ * faster than a loop in V8, and as fast in JavaScriptCore.
462
+ */
463
+ function copyBytes(target, at, source, offset, count) {
464
+ if (source === target) {
465
+ target.copyWithin(at, offset, offset + count);
466
+ }
467
+ else if (offset === 0 && count === source.length) {
468
+ target.set(source, at);
469
+ }
470
+ else if (count < COPY_LOOP_LENGTH) {
471
+ for (let i = 0; i < count; i++) {
472
+ target[at + i] = source[offset + i];
473
+ }
474
+ }
475
+ else {
476
+ target.set(source.subarray(offset, offset + count), at);
477
+ }
478
+ }
479
+ /** How a ByteArray's elements are indexed: its bytes; past its length undefined, and a write grows it. */
480
+ export const byteArrayHook = {
481
+ // Its state from the start: it takes defaultObjectEncoding as it is then.
482
+ create: (traits, rt) => {
483
+ const o = Object.create(traits.proto);
484
+ o.$bytes = new Bytes(rt, o);
485
+ return o;
486
+ },
487
+ getIndex: (o, i) => {
488
+ const b = o.$bytes;
489
+ return b && i < b.length ? b.buffer[i] : undefined;
490
+ },
491
+ setIndex: (o, i, v, rt) => {
492
+ bytesOf(rt, o).setByte(i, rt.toInt(v) & 0xff);
493
+ },
494
+ hasIndex: (o, i) => {
495
+ const b = o.$bytes;
496
+ return b !== undefined && i < b.length;
497
+ },
498
+ };
499
+ /** A ByteArray's state, made the first time a native needs it. */
500
+ export function bytesOf(rt, o) {
501
+ if (!o.$bytes) {
502
+ o.$bytes = new Bytes(rt, o);
503
+ }
504
+ return o.$bytes;
505
+ }
506
+ /**
507
+ * A shareable ByteArray's memory, shared from now on, as it crosses to
508
+ * another worker; where memory cannot be shared, the error that names
509
+ * cross-origin isolation.
510
+ */
511
+ export function shareBytes(rt, o) {
512
+ return bytesOf(rt, o).share();
513
+ }
514
+ /** This VM's one ByteArray for a shared memory, as avmplus interns one for its Buffer: shareable, at position 0. */
515
+ export function sharedByteArray(rt, memory) {
516
+ const objects = sharedObjectsOf(rt);
517
+ const known = objects.get(memory.id)?.deref();
518
+ if (known) {
519
+ return known;
520
+ }
521
+ const o = rt.construct(rt.classNamed("flash.utils::ByteArray"));
522
+ bytesOf(rt, o).adopt(memory);
523
+ objects.set(memory.id, new WeakRef(o));
524
+ return o;
525
+ }
526
+ /** This VM's ByteArray for the shared memory `id`, if it has one. */
527
+ export function knownSharedByteArray(rt, id) {
528
+ return sharedObjectsOf(rt).get(id)?.deref();
529
+ }
530
+ /** The host's UTF-8 encoder, which writes a lone surrogate as U+FFFD, as utf8() does. */
531
+ const encoder = new TextEncoder();
532
+ /**
533
+ * Below this length, a string encodes faster here than through
534
+ * TextEncoder, whose call costs more than the loop saves (about 90
535
+ * characters in V8).
536
+ */
537
+ const ENCODER_LENGTH = 96;
538
+ /**
539
+ * As UnicodeUtils::Utf16ToUtf8: a surrogate pair as four bytes, a lone
540
+ * surrogate as U+FFFD. avmshell's writeUTFBytes loses characters around a
541
+ * lone one instead; this keeps them, and the UTF-8 valid.
542
+ */
543
+ export function utf8(s) {
544
+ if (s.length >= ENCODER_LENGTH) {
545
+ return encoder.encode(s);
546
+ }
547
+ // ASCII, byte for byte.
548
+ let ascii = true;
549
+ for (let i = 0; i < s.length && ascii; i++) {
550
+ ascii = s.charCodeAt(i) < 0x80;
551
+ }
552
+ if (ascii) {
553
+ const out = new Uint8Array(s.length);
554
+ for (let i = 0; i < s.length; i++) {
555
+ out[i] = s.charCodeAt(i);
556
+ }
557
+ return out;
558
+ }
559
+ const out = new Uint8Array(s.length * 3);
560
+ let n = 0;
561
+ for (let i = 0; i < s.length; i++) {
562
+ let ch = s.charCodeAt(i);
563
+ if (ch < 0x80) {
564
+ out[n++] = ch;
565
+ continue;
566
+ }
567
+ if (ch < 0x800) {
568
+ out[n++] = 0xc0 | ((ch >> 6) & 0x1f);
569
+ out[n++] = 0x80 | (ch & 0x3f);
570
+ continue;
571
+ }
572
+ if (ch >= 0xd800 && ch <= 0xdbff) {
573
+ const ch2 = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
574
+ if (ch2 >= 0xdc00 && ch2 <= 0xdfff) {
575
+ i++;
576
+ const ucs4 = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
577
+ out[n++] = 0xf0 | ((ucs4 >> 18) & 0x07);
578
+ out[n++] = 0x80 | ((ucs4 >> 12) & 0x3f);
579
+ out[n++] = 0x80 | ((ucs4 >> 6) & 0x3f);
580
+ out[n++] = 0x80 | (ucs4 & 0x3f);
581
+ continue;
582
+ }
583
+ ch = 0xfffd;
584
+ }
585
+ else if (ch >= 0xdc00 && ch <= 0xdfff) {
586
+ ch = 0xfffd;
587
+ }
588
+ out[n++] = 0xe0 | ((ch >> 12) & 0x0f);
589
+ out[n++] = 0x80 | ((ch >> 6) & 0x3f);
590
+ out[n++] = 0x80 | (ch & 0x3f);
591
+ }
592
+ return out.subarray(0, n);
593
+ }
594
+ /**
595
+ * As UnicodeUtils::Utf8ToUtf16, not strict: a byte that does not start a
596
+ * valid sequence is the character of its value, and a four-byte sequence
597
+ * may start with 0xF8 to 0xFF, as Flash Player 9 and 10 read them.
598
+ */
599
+ /** The length from which fromUtf8 first looks for ASCII. */
600
+ const ASCII_LENGTH = 16;
601
+ export function fromUtf8(bytes) {
602
+ const n = bytes.length;
603
+ // ASCII, as most text is: its bytes are its characters. Below
604
+ // ASCII_LENGTH bytes, a character at a time, until one is not.
605
+ if (n < ASCII_LENGTH) {
606
+ let s = "";
607
+ let k = 0;
608
+ for (; k < n && bytes[k] < 0x80; k++) {
609
+ s += String.fromCharCode(bytes[k]);
610
+ }
611
+ if (k === n) {
612
+ return s;
613
+ }
614
+ }
615
+ else {
616
+ let ascii = 0;
617
+ while (ascii < n && bytes[ascii] < 0x80) {
618
+ ascii++;
619
+ }
620
+ if (ascii === n) {
621
+ let s = "";
622
+ for (let k = 0; k < n; k += 8192) {
623
+ s += String.fromCharCode.apply(null, bytes.subarray(k, k + 8192));
624
+ }
625
+ return s;
626
+ }
627
+ }
628
+ const out = [];
629
+ let i = 0;
630
+ while (i < n) {
631
+ const c = bytes[i];
632
+ const left = n - i;
633
+ switch (c >> 4) {
634
+ case 12:
635
+ case 13: {
636
+ if (left >= 2 && (bytes[i + 1] & 0xc0) === 0x80) {
637
+ const ch = ((c << 6) & 0x7c0) | (bytes[i + 1] & 0x3f);
638
+ if (ch >= 0x80) {
639
+ out.push(ch);
640
+ i += 2;
641
+ continue;
642
+ }
643
+ }
644
+ break;
645
+ }
646
+ case 14: {
647
+ if (left >= 3 && (bytes[i + 1] & 0xc0) === 0x80 && (bytes[i + 2] & 0xc0) === 0x80) {
648
+ const ch = ((c << 12) & 0xf000) | ((bytes[i + 1] << 6) & 0xfc0) | (bytes[i + 2] & 0x3f);
649
+ if (ch >= 0x800) {
650
+ out.push(ch);
651
+ i += 3;
652
+ continue;
653
+ }
654
+ }
655
+ break;
656
+ }
657
+ case 15: {
658
+ if (left >= 4 &&
659
+ (bytes[i + 1] & 0xc0) === 0x80 &&
660
+ (bytes[i + 2] & 0xc0) === 0x80 &&
661
+ (bytes[i + 3] & 0xc0) === 0x80) {
662
+ const ch = ((c << 18) & 0x1c0000) |
663
+ ((bytes[i + 1] << 12) & 0x3f000) |
664
+ ((bytes[i + 2] << 6) & 0xfc0) |
665
+ (bytes[i + 3] & 0x3f);
666
+ if (ch >= 0x10000) {
667
+ out.push((((ch - 0x10000) >> 10) & 0x3ff) + 0xd800, ((ch - 0x10000) & 0x3ff) + 0xdc00);
668
+ i += 4;
669
+ continue;
670
+ }
671
+ }
672
+ break;
673
+ }
674
+ default:
675
+ break;
676
+ }
677
+ // Not a sequence: the byte itself.
678
+ out.push(c);
679
+ i++;
680
+ }
681
+ return fromCodes(out);
682
+ }
683
+ /** A string of UTF-16 code units, in chunks, as String.fromCharCode takes only so many arguments. */
684
+ function fromCodes(units) {
685
+ let s = "";
686
+ for (let k = 0; k < units.length; k += 8192) {
687
+ s += String.fromCharCode(...units.slice(k, k + 8192));
688
+ }
689
+ return s;
690
+ }
691
+ /** The bytes up to the first NUL, as avmplus reads a C string. */
692
+ function toNul(bytes) {
693
+ const nul = bytes.indexOf(0);
694
+ return nul < 0 ? bytes : bytes.subarray(0, nul);
695
+ }
696
+ const BOM = (b) => b.length >= 3 && b[0] === 0xef && b[1] === 0xbb && b[2] === 0xbf;
697
+ /** As ByteArrayObject::algorithmToEnum: zlib, deflate or lzma; null a TypeError, any other an IOError. */
698
+ function algorithmOf(rt, algorithm) {
699
+ if (algorithm === null || algorithm === undefined) {
700
+ throw rt.error("TypeError", 2007, "algorithm");
701
+ }
702
+ const name = rt.toString(algorithm);
703
+ if (name === "zlib" || name === "deflate" || name === "lzma") {
704
+ return name;
705
+ }
706
+ throw rt.error("flash.errors::IOError", 2058);
707
+ }
708
+ /** A ByteArray's bytes, all of them, replaced by a copy of `bytes`, its position `position`; the domain memory told, if it is. */
709
+ function replaceBytes(b, bytes, position) {
710
+ if (b.shared !== null) {
711
+ // In place, for every worker, as avmplus swaps the shared Buffer's array.
712
+ const previous = b.length;
713
+ b.position = 0;
714
+ b.write(bytes);
715
+ if (previous > bytes.length) {
716
+ b.buffer.fill(0, bytes.length, previous);
717
+ }
718
+ b.length = bytes.length;
719
+ b.position = position >>> 0;
720
+ b.notify();
721
+ return;
722
+ }
723
+ b.setBuffer(new Uint8Array(bytes));
724
+ b.length = bytes.length;
725
+ b.position = position >>> 0;
726
+ b.notify();
727
+ }
728
+ /** ByteArray's natives, by the names the compiler gives them. */
729
+ /** ByteArray's natives, for `rt`: written as a class, each running with the ByteArray object as `this`. */
730
+ export function byteArrayNatives(rt) {
731
+ const own = "flash.utils:ByteArray";
732
+ const natives = {};
733
+ const nonNull = (rt, v, name) => {
734
+ if (v === null || v === undefined) {
735
+ throw rt.error("TypeError", 2007, name);
736
+ }
737
+ };
738
+ // As ByteArrayObject::readUTFBytes: a BOM skipped, and a NUL ends the string.
739
+ const readUTFBytes = (rt, b, n) => {
740
+ if (b.available < n) {
741
+ throw rt.error("flash.errors::EOFError", 2030);
742
+ }
743
+ let bytes = b.buffer.subarray(b.position, b.position + n);
744
+ if (BOM(bytes)) {
745
+ bytes = bytes.subarray(3);
746
+ }
747
+ const s = fromUtf8(toNul(bytes));
748
+ b.position = (b.position + n) >>> 0;
749
+ return s;
750
+ };
751
+ class ByteArrayNatives {
752
+ static get defaultObjectEncoding() {
753
+ return rt.defaultObjectEncoding;
754
+ }
755
+ static set defaultObjectEncoding(v) {
756
+ const e = rt.toUint(v);
757
+ if (e !== kAMF0 && e !== kAMF3) {
758
+ throw rt.error("ArgumentError", 2008, "objectEncoding");
759
+ }
760
+ rt.defaultObjectEncoding = e;
761
+ }
762
+ get length() {
763
+ const b = bytesOf(rt, this);
764
+ return b.length;
765
+ }
766
+ set length(v) {
767
+ const b = bytesOf(rt, this);
768
+ b.setLength(rt.toUint(v), true);
769
+ }
770
+ get position() {
771
+ const b = bytesOf(rt, this);
772
+ return b.position;
773
+ }
774
+ set position(v) {
775
+ const b = bytesOf(rt, this);
776
+ // Not redundant: toUint's own `>>> 0` serves every caller, and once
777
+ // one passes it 2^31 or more, JavaScriptCore makes doubles there for
778
+ // all of them (see Bytes).
779
+ b.position = rt.toUint(v) >>> 0;
780
+ }
781
+ get bytesAvailable() {
782
+ const b = bytesOf(rt, this);
783
+ return b.available;
784
+ }
785
+ get endian() {
786
+ const b = bytesOf(rt, this);
787
+ return b.littleEndian ? "littleEndian" : "bigEndian";
788
+ }
789
+ set endian(v) {
790
+ const b = bytesOf(rt, this);
791
+ nonNull(rt, v, "endian");
792
+ const type = rt.toString(v);
793
+ if (type === "bigEndian" || type === "littleEndian") {
794
+ b.littleEndian = type === "littleEndian";
795
+ }
796
+ else {
797
+ throw rt.error("ArgumentError", 2008, "type");
798
+ }
799
+ }
800
+ get objectEncoding() {
801
+ const b = bytesOf(rt, this);
802
+ return b.objectEncoding;
803
+ }
804
+ set objectEncoding(v) {
805
+ const b = bytesOf(rt, this);
806
+ const e = rt.toUint(v);
807
+ if (e !== kAMF0 && e !== kAMF3) {
808
+ throw rt.error("ArgumentError", 2008, "objectEncoding");
809
+ }
810
+ b.objectEncoding = e;
811
+ }
812
+ get shareable() {
813
+ return bytesOf(rt, this).shareable;
814
+ }
815
+ // As ByteArray::setShareable: once shareable, always; its memory is
816
+ // shared when it first crosses to another worker.
817
+ set shareable(v) {
818
+ if (rt.toBoolean(v)) {
819
+ bytesOf(rt, this).shareable = true;
820
+ }
821
+ }
822
+ clear() {
823
+ const b = bytesOf(rt, this);
824
+ b.clear();
825
+ }
826
+ // As ByteArray::CAS: the int at a word-aligned index within the bytes,
827
+ // replaced by `next` if it is `expected`; the int there before.
828
+ atomicCompareAndSwapIntAt(index, expected, next) {
829
+ const b = bytesOf(rt, this);
830
+ const at = rt.toInt(index) >>> 0;
831
+ if (b.length < 4 || at > b.length - 4 || at % 4 !== 0) {
832
+ throw rt.error("RangeError", 1506);
833
+ }
834
+ return b.compareAndSwapInt(at, rt.toInt(expected), rt.toInt(next));
835
+ }
836
+ // As ByteArrayObject::atomicCompareAndSwapLength: the length set to
837
+ // `next` if it is `expected`, as its setter sets it; the length before.
838
+ // The domain memory refuses a length below its least, as later avmplus
839
+ // does; the oracle's avmshell sets it.
840
+ atomicCompareAndSwapLength(expected, next) {
841
+ const b = bytesOf(rt, this);
842
+ const length = rt.toInt(next);
843
+ if (b.subscribed && length < GLOBAL_MEMORY_MIN_SIZE) {
844
+ throw rt.error("RangeError", 1506);
845
+ }
846
+ return b.compareAndSwapLength(rt.toInt(expected), length >>> 0);
847
+ }
848
+ // Reads.
849
+ readBoolean() {
850
+ const b = bytesOf(rt, this);
851
+ const at = b.shortRead(1);
852
+ return b.buffer[at] !== 0;
853
+ }
854
+ readByte() {
855
+ const b = bytesOf(rt, this);
856
+ const at = b.shortRead(1);
857
+ return b.view.getInt8(at);
858
+ }
859
+ readUnsignedByte() {
860
+ const b = bytesOf(rt, this);
861
+ const at = b.shortRead(1);
862
+ return b.buffer[at];
863
+ }
864
+ readShort() {
865
+ const b = bytesOf(rt, this);
866
+ const at = b.shortRead(2);
867
+ return b.view.getInt16(at, b.littleEndian);
868
+ }
869
+ readUnsignedShort() {
870
+ const b = bytesOf(rt, this);
871
+ const at = b.shortRead(2);
872
+ return b.view.getUint16(at, b.littleEndian);
873
+ }
874
+ readInt() {
875
+ const b = bytesOf(rt, this);
876
+ const at = b.shortRead(4);
877
+ return b.view.getInt32(at, b.littleEndian);
878
+ }
879
+ readUnsignedInt() {
880
+ const b = bytesOf(rt, this);
881
+ const at = b.shortRead(4);
882
+ return b.view.getUint32(at, b.littleEndian);
883
+ }
884
+ readFloat() {
885
+ const b = bytesOf(rt, this);
886
+ const at = b.shortRead(4);
887
+ return b.view.getFloat32(at, b.littleEndian);
888
+ }
889
+ readDouble() {
890
+ const b = bytesOf(rt, this);
891
+ const at = b.shortRead(8);
892
+ return b.view.getFloat64(at, b.littleEndian);
893
+ }
894
+ readUTFBytes(n) {
895
+ const b = bytesOf(rt, this);
896
+ return readUTFBytes(rt, b, rt.toUint(n));
897
+ }
898
+ readUTF() {
899
+ const b = bytesOf(rt, this);
900
+ const at = b.shortRead(2);
901
+ return readUTFBytes(rt, b, b.view.getUint16(at, b.littleEndian));
902
+ }
903
+ // avmshell converts no other charset: the bytes are checked, and nothing read.
904
+ readMultiByte(n, charSet) {
905
+ const b = bytesOf(rt, this);
906
+ nonNull(rt, charSet, "charSet");
907
+ b.checkEOF(rt.toUint(n));
908
+ return "";
909
+ }
910
+ // As DataInput::ReadByteArray: into `bytes` at `offset`, growing it to hold them.
911
+ readBytes(bytes, offsetIn = 0, lengthIn = 0) {
912
+ const b = bytesOf(rt, this);
913
+ nonNull(rt, bytes, "bytes");
914
+ const offset = rt.toUint(offsetIn);
915
+ let count = rt.toUint(lengthIn);
916
+ const available = b.available;
917
+ if (count === 0) {
918
+ count = available;
919
+ }
920
+ if (count > available) {
921
+ throw rt.error("flash.errors::EOFError", 2030);
922
+ }
923
+ if (offset + count > 0xffffffff) {
924
+ throw rt.error("RangeError", 2006);
925
+ }
926
+ // From the buffer as it is now: a target that grows, this ByteArray
927
+ // too, gets a new buffer, and the bytes come from the old one. As in
928
+ // DataInput::ReadByteArray, the target is sized first: one that
929
+ // cannot grow leaves the position where it was.
930
+ const to = bytesOf(rt, bytes);
931
+ const source = b.buffer;
932
+ const from = b.position;
933
+ if (offset + count >= to.length) {
934
+ to.extend(offset + count);
935
+ }
936
+ b.position = (from + count) >>> 0;
937
+ copyBytes(to.buffer, offset, source, from, count);
938
+ }
939
+ // Writes.
940
+ writeBoolean(v) {
941
+ const b = bytesOf(rt, this);
942
+ const at = b.shortWrite(1);
943
+ b.buffer[at] = v ? 1 : 0;
944
+ }
945
+ writeByte(v) {
946
+ const b = bytesOf(rt, this);
947
+ const at = b.shortWrite(1);
948
+ b.buffer[at] = rt.toInt(v) & 0xff;
949
+ }
950
+ writeShort(v) {
951
+ const b = bytesOf(rt, this);
952
+ const at = b.shortWrite(2);
953
+ b.view.setInt16(at, rt.toInt(v), b.littleEndian);
954
+ }
955
+ writeInt(v) {
956
+ const b = bytesOf(rt, this);
957
+ const at = b.shortWrite(4);
958
+ b.view.setInt32(at, rt.toInt(v), b.littleEndian);
959
+ }
960
+ writeUnsignedInt(v) {
961
+ const b = bytesOf(rt, this);
962
+ const at = b.shortWrite(4);
963
+ b.view.setUint32(at, rt.toUint(v), b.littleEndian);
964
+ }
965
+ writeFloat(v) {
966
+ const b = bytesOf(rt, this);
967
+ const at = b.shortWrite(4);
968
+ b.view.setFloat32(at, rt.toNumber(v), b.littleEndian);
969
+ }
970
+ writeDouble(v) {
971
+ const b = bytesOf(rt, this);
972
+ const at = b.shortWrite(8);
973
+ b.view.setFloat64(at, rt.toNumber(v), b.littleEndian);
974
+ }
975
+ writeUTFBytes(v) {
976
+ const b = bytesOf(rt, this);
977
+ nonNull(rt, v, "value");
978
+ b.write(utf8(rt.toString(v)));
979
+ }
980
+ writeUTF(v) {
981
+ const b = bytesOf(rt, this);
982
+ nonNull(rt, v, "value");
983
+ const bytes = utf8(rt.toString(v));
984
+ if (bytes.length > 65535) {
985
+ throw rt.error("RangeError", 2006);
986
+ }
987
+ const at = b.shortWrite(2);
988
+ b.view.setUint16(at, bytes.length, b.littleEndian);
989
+ b.write(bytes);
990
+ }
991
+ writeMultiByte(value, charSet) {
992
+ nonNull(rt, value, "value");
993
+ nonNull(rt, charSet, "charSet");
994
+ }
995
+ // As ByteArrayObject::writeBytes and DataOutput::WriteByteArray.
996
+ writeBytes(bytes, offsetIn = 0, lengthIn = 0) {
997
+ const b = bytesOf(rt, this);
998
+ nonNull(rt, bytes, "bytes");
999
+ const from = bytesOf(rt, bytes);
1000
+ let offset = rt.toUint(offsetIn);
1001
+ let count = rt.toUint(lengthIn);
1002
+ if (count === 0) {
1003
+ count = (from.length - offset) >>> 0;
1004
+ }
1005
+ const length = from.length;
1006
+ if (offset > length) {
1007
+ offset = length;
1008
+ }
1009
+ if (count === 0) {
1010
+ count = length - offset;
1011
+ }
1012
+ if (count > length - offset) {
1013
+ throw rt.error("RangeError", 2006);
1014
+ }
1015
+ if (count > 0) {
1016
+ b.write(from.buffer, offset, count);
1017
+ }
1018
+ }
1019
+ // As ByteArrayObject::_compress and _uncompress, which compress(),
1020
+ // uncompress(), deflate() and inflate() call: the algorithm checked
1021
+ // first, and an empty ByteArray left as it is. The domain memory is
1022
+ // compressed too, as avmshell's is: its Domain does not subscribe to it
1023
+ // as the player refuses a subscribed one (3735). Compressing leaves the position at the end, uncompressing at 0;
1024
+ // data that does not uncompress leaves the ByteArray as it was.
1025
+ [`${own}::_compress`](algorithm) {
1026
+ const kind = algorithmOf(rt, algorithm);
1027
+ const b = bytesOf(rt, this);
1028
+ if (b.length === 0) {
1029
+ return;
1030
+ }
1031
+ const data = b.buffer.subarray(0, b.length);
1032
+ const out = kind === "zlib"
1033
+ ? zlibCompress(data)
1034
+ : kind === "deflate"
1035
+ ? deflateCompress(data)
1036
+ : lzmaByteArrayCompress(data);
1037
+ replaceBytes(b, out, out.length);
1038
+ }
1039
+ [`${own}::_uncompress`](algorithm) {
1040
+ const kind = algorithmOf(rt, algorithm);
1041
+ const b = bytesOf(rt, this);
1042
+ if (b.length === 0) {
1043
+ return;
1044
+ }
1045
+ const data = b.buffer.subarray(0, b.length);
1046
+ let out;
1047
+ if (kind === "lzma") {
1048
+ // As UncompressViaLzma: too short for its header is left as it is, and
1049
+ // a length past 32 bits a MemoryError before anything is read.
1050
+ if (b.length < LZMA_HEADER) {
1051
+ return;
1052
+ }
1053
+ if (data[9] | data[10] | data[11] | data[12]) {
1054
+ throw rt.error("flash.errors::MemoryError", 1000);
1055
+ }
1056
+ }
1057
+ try {
1058
+ out =
1059
+ kind === "lzma"
1060
+ ? lzmaByteArrayUncompress(data)
1061
+ : zlibUncompress(data, kind === "deflate");
1062
+ }
1063
+ catch (e) {
1064
+ // As avmplus: an output it cannot allocate is a MemoryError, corrupt data an IOError.
1065
+ if (e instanceof CompressedDataTooLargeError) {
1066
+ throw rt.error("flash.errors::MemoryError", 1000);
1067
+ }
1068
+ if (e instanceof CompressedDataError) {
1069
+ throw rt.error("flash.errors::IOError", 2058);
1070
+ }
1071
+ throw e;
1072
+ }
1073
+ replaceBytes(b, out, 0);
1074
+ }
1075
+ // As ByteArrayObject::_toString: by its BOM, UTF-8 or UTF-16 of either
1076
+ // order, else UTF-8; all of its length, NULs too.
1077
+ [`${own}::_toString`]() {
1078
+ const b = bytesOf(rt, this);
1079
+ const bytes = b.buffer.subarray(0, b.length);
1080
+ if (bytes.length >= 3) {
1081
+ if (BOM(bytes)) {
1082
+ return fromUtf8(bytes.subarray(3));
1083
+ }
1084
+ if ((bytes[0] === 0xfe && bytes[1] === 0xff) || (bytes[0] === 0xff && bytes[1] === 0xfe)) {
1085
+ const little = bytes[0] === 0xff;
1086
+ const units = [];
1087
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
1088
+ for (let i = 2; i + 1 < bytes.length; i += 2) {
1089
+ units.push(view.getUint16(i, little));
1090
+ }
1091
+ return fromCodes(units);
1092
+ }
1093
+ }
1094
+ return fromUtf8(bytes);
1095
+ }
1096
+ }
1097
+ registerNativeClass(natives, "flash.utils::ByteArray", ByteArrayNatives);
1098
+ // flash.utils' ObjectOutput and ObjectInput, what writeExternal and
1099
+ // readExternal are given: the ByteArray's own writes and reads, in the
1100
+ // stream's byte order, and its objects through the stream's tables.
1101
+ const delegated = (target, names) => {
1102
+ const from = ByteArrayNatives.prototype;
1103
+ for (const name of names) {
1104
+ const f = from[name];
1105
+ Object.defineProperty(target, name, {
1106
+ value(...args) {
1107
+ const stream = this.$amf;
1108
+ const b = stream.bytes;
1109
+ const order = b.littleEndian;
1110
+ b.littleEndian = stream.littleEndian;
1111
+ try {
1112
+ return f.apply(b.owner, args);
1113
+ }
1114
+ finally {
1115
+ b.littleEndian = order;
1116
+ }
1117
+ },
1118
+ });
1119
+ }
1120
+ };
1121
+ const streamState = {
1122
+ get endian() {
1123
+ return this.$amf.littleEndian ? "littleEndian" : "bigEndian";
1124
+ },
1125
+ set endian(v) {
1126
+ nonNull(rt, v, "endian");
1127
+ if (v !== "bigEndian" && v !== "littleEndian") {
1128
+ throw rt.error("ArgumentError", 2008, "type");
1129
+ }
1130
+ this.$amf.littleEndian = v === "littleEndian";
1131
+ },
1132
+ get objectEncoding() {
1133
+ return this.$amf.objectEncoding;
1134
+ },
1135
+ set objectEncoding(v) {
1136
+ const encoding = rt.toUint(v);
1137
+ if (encoding !== 0 && encoding !== 3) {
1138
+ throw rt.error("ArgumentError", 2008, "objectEncoding");
1139
+ }
1140
+ this.$amf.objectEncoding = encoding;
1141
+ },
1142
+ };
1143
+ class ObjectOutputNatives {
1144
+ writeObject(v) {
1145
+ const stream = this.$amf;
1146
+ if (stream.objectEncoding !== 3) {
1147
+ throw rt.unsupported("AMF0");
1148
+ }
1149
+ stream.value(v);
1150
+ }
1151
+ }
1152
+ class ObjectInputNatives {
1153
+ readObject() {
1154
+ const stream = this.$amf;
1155
+ if (stream.objectEncoding !== 3) {
1156
+ throw rt.unsupported("AMF0");
1157
+ }
1158
+ return stream.value();
1159
+ }
1160
+ get bytesAvailable() {
1161
+ return this.$amf.bytes.available;
1162
+ }
1163
+ }
1164
+ for (const Class of [ObjectOutputNatives, ObjectInputNatives]) {
1165
+ Object.defineProperties(Class.prototype, Object.getOwnPropertyDescriptors(streamState));
1166
+ }
1167
+ delegated(ObjectOutputNatives.prototype, [
1168
+ "writeBytes",
1169
+ "writeBoolean",
1170
+ "writeByte",
1171
+ "writeShort",
1172
+ "writeInt",
1173
+ "writeUnsignedInt",
1174
+ "writeFloat",
1175
+ "writeDouble",
1176
+ "writeMultiByte",
1177
+ "writeUTF",
1178
+ "writeUTFBytes",
1179
+ ]);
1180
+ delegated(ObjectInputNatives.prototype, [
1181
+ "readBytes",
1182
+ "readBoolean",
1183
+ "readByte",
1184
+ "readUnsignedByte",
1185
+ "readShort",
1186
+ "readUnsignedShort",
1187
+ "readInt",
1188
+ "readUnsignedInt",
1189
+ "readFloat",
1190
+ "readDouble",
1191
+ "readMultiByte",
1192
+ "readUTF",
1193
+ "readUTFBytes",
1194
+ ]);
1195
+ registerNativeClass(natives, "flash.utils::ObjectOutput", ObjectOutputNatives);
1196
+ registerNativeClass(natives, "flash.utils::ObjectInput", ObjectInputNatives);
1197
+ return natives;
1198
+ }
1199
+ /**
1200
+ * As DomainEnv::set_globalMemory: the domain memory set to ByteArray `v`, or
1201
+ * to the scratch memory for null; a ByteArray shorter than the least length
1202
+ * fails. avmshell's Domain and the player's ApplicationDomain both set it.
1203
+ */
1204
+ export function setDomainMemory(rt, v) {
1205
+ const previous = rt.memoryProvider;
1206
+ if (v === null || v === undefined) {
1207
+ if (previous) {
1208
+ bytesOf(rt, previous).subscribed = false;
1209
+ }
1210
+ rt.memoryProvider = null;
1211
+ rt.refreshMemory = null;
1212
+ rt.memory = rt.scratchMemory;
1213
+ return;
1214
+ }
1215
+ const b = bytesOf(rt, v);
1216
+ if (b.length < GLOBAL_MEMORY_MIN_SIZE) {
1217
+ throw rt.error("Error", 1504);
1218
+ }
1219
+ b.subscribed = true;
1220
+ b.notify();
1221
+ if (previous && previous !== v) {
1222
+ bytesOf(rt, previous).subscribed = false;
1223
+ }
1224
+ rt.memoryProvider = v;
1225
+ // Shared, its length as the other workers left it, its view made again if it changed.
1226
+ rt.refreshMemory = b.shared !== null ? () => b.notify() : null;
1227
+ }