.. _wasm: WebAssembly Runtime =================== PytoRuntime can run WebAssembly programs through WebKit, via the ``wasm`` command or the Swift :swift:symbol:`WASM` class. .. note:: WebAssembly requires iOS / Mac Catalyst 15.2 or later because of SharedArrayBuffer. Host Environment ---------------- PytoRuntime provides implementations of the WASIX and WASI Preview 1 syscalls through JavaScript, which communicates with the host process that exposes its environment with no sandboxing features. A web server on port 20818 is automatically started when a WebAssembly module is being loaded so it can serve resources with the 'Cross-Origin-Opener-Policy' and 'Cross-Origin-Embedder-Policy' headers which are required for ``SharedArrayBuffer``. All modules are executed in the same ``WKWebView`` instance, with each thread running in its own Web Worker. The runtime also exports its own memory if the module is linked with ``--import-memory``. Dynamic Linking --------------- Programs can dynamically link modules in two ways: by declaring them at link time or by calling ``dlopen``. Needed libraries declared in the ``dylink.0`` section are loaded before the root module at startup. They can be full paths or names that will be searched first in the working directory, then in :swift:symbol:`WASM.librarySearchURLs` which defaults to: - ``/../lib`` - ``~/Documents/.local/lib`` - ``~/Documents/.local/lib/sysroot32-ehpic/lib/wasm32-wasip1`` - ``~/Documents/.local/share/clang32-ehpic/lib/wasm32-pyto-wasip1`` Every module, including the root one is re-instantiated for every new thread regardless of how it was loaded. Subprocesses ------------ Like Python, the WASIX layer provided by PytoRuntime can also emulate process spawning via *ios_system*. The ``wasm`` command assigns the *ios_system* current PID to :swift:symbol:`WASIState.pid` so programs can send signals to each other. .. warning:: The ``fork`` and ``exec`` functions are not supported. You can use the ``posix_spawn`` APIs or ``system``. Signals ------- The :swift:symbol:`Interpreter.kill(_:)` function sends a signal to the WASM process if there is one running on that interpreter, for example in a shell. Otherwise, there is :swift:symbol:`WASM.kill(_:)`. WASM processes can also send each other signals using their PID. The callbacks are executed before and after running a system call.