Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function signature mismatch with pdfium.wasm i32 vs. i64 #177

Open
mrboxtobox opened this issue Sep 13, 2024 · 1 comment
Open

Function signature mismatch with pdfium.wasm i32 vs. i64 #177

mrboxtobox opened this issue Sep 13, 2024 · 1 comment

Comments

@mrboxtobox
Copy link

mrboxtobox commented Sep 13, 2024

Hi,

I'm running into the following error when I try running pdfium.wasm with wasmtime. I'm running on MacOS M3. Any ideas what could be wrong?

    raise WasmtimeError._from_ptr(error)
wasmtime._error.WasmtimeError: incompatible import type for `wasi_snapshot_preview1::fd_seek`

Caused by:
    types incompatible: expected type `(func (param i32 i32 i32 i32 i32) (result i32))`, found type `(func (param i32 i64 i32 i32) (result i32))`

Steps to Reproduce

  1. Install Deps
pip install wasmtime
  1. Download and install pdfium-wasm
curl -L -O https://github.com/bblanchon/pdfium-binaries/releases/latest/download/pdfium-wasm.tgz
tar -xzvf pdfium-wasm.tgz
  1. Run python test_wasm.py in the same directory
# test_wasm.py
from wasmtime import Engine, Store, Module, Linker, WasiConfig, FuncType, ValType
import ctypes
import os
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def define_unknown_imports_as_default_values(linker: Linker, module: Module) -> None:
    for imp in module.imports:
        if imp.module != "env":
            continue
        if isinstance(imp.type, FuncType):
            func_ty = imp.type
            result_tys = func_ty.results

            def default_func(*args):
                # XXX: For some reason _emscripten_throw_longjmp is called with different numbers of
                # arguments, and sometimes expects a return value and sometimes not. This feels like
                # a bug, but I don't know if it's in emscripten or wasmtime.
                if imp.name == "_emscripten_throw_longjmp":
                    if len(args) in (3, 5):
                        return 0
                    return None
                results = []
                for ty in result_tys:
                    match ty:
                        case ValType.i32():
                            results.append(0)
                        case ValType.i64():
                            results.append(0)
                        case ValType.f32():
                            results.append(0.0)
                        case ValType.f64():
                            results.append(0.0)
                return results

            linker.define_func(imp.module, imp.name, func_ty, default_func)


def main():
    # Path to your PDFium WASM file
    wasm_file = "lib/pdfium.wasm"

    import sys
    # Check if files exist
    if not os.path.exists(wasm_file):
        logger.error(f"WASM file not found: {wasm_file}")
        return


    # Create a store and engine
    engine = Engine()
    module = Module.from_file(engine, wasm_file)
    store = Store(engine)
    linker = Linker(engine)
    linker.define_wasi()
    wasi_config = WasiConfig()
    wasi_config.inherit_stdout()

    store.set_wasi(wasi_config)

    # Compile and instantiate the WASM module
    logger.info("Loading WASM module...")
    define_unknown_imports_as_default_values(linker, module)

    instance = linker.instantiate(store, module)

if __name__ == "__main__":
    main()
@mrboxtobox mrboxtobox changed the title Function signature mismatch with pdfium.wasm on M3 Function signature mismatch with pdfium.wasm i32 vs. i64 Sep 13, 2024
@bblanchon
Copy link
Owner

@jerbob92, do we need another build for WASM 64?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@bblanchon @mrboxtobox and others