wasmer-pack 0.7.1

A code generator that lets you treat WebAssembly modules like native dependencies.
Documentation
---
source: crates/wasmer-pack/src/py/mod.rs
expression: "files[\"wasmer_pack/bindings/__init__.py\"].utf8_contents().unwrap()"
---
'''
Bindings to the  library.
'''

from pathlib import Path
from typing import Optional, Any

from wasmer import Store, Module, wasi # type: ignore
from .wasmer_pack.bindings import (
    WasmerPack as _WasmerPack,
    add_browser_to_imports as _wasmer_pack__add_browser_to_imports,
    Browser as _wasmer_pack__Browser,
)

class Bindings:
    """
    Instantiate bindings to the various libraries in this package.
    """

    def __init__(self, store: Store):
        self._store = store
        self._cache: dict[str, Module] = {}

    def _get_module(self, filename: str) -> Module:
        if filename in self._cache:
            return self._cache[filename]

        wasm = Path(__file__).parent.joinpath(filename).read_bytes()
        module = Module(self._store, wasm)
        self._cache[filename] = module
        return module

    def wasmer_pack(
        self,
        browser: _wasmer_pack__Browser,
        module: Optional[Module] = None,
    ) -> _WasmerPack:
        """
        Instantiate the "wasmer_pack" library.
        :param browser: An implementation of the "browser" interface.
        :param module: A user-specified WebAssembly module to use instead of the
                       one bundled with this package.
        """

        filename = "wasmer_pack/wasmer_pack_wasm.wasm"
        if not module:
            module = self._get_module(filename)

        imports: dict[str, Any] = {}

        wrapper = None

        def get_export(item_name: str):
            assert wrapper is not None
            return getattr(wrapper.instance.exports, item_name)
        _wasmer_pack__add_browser_to_imports(self._store, imports, browser, get_export)

        wrapper = _WasmerPack(self._store, imports, module)
        return wrapper