bblanchon / pdfium-binaries

📰 Binary distribution of PDFium
885 stars 190 forks source link

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

Open mrboxtobox opened 1 month ago

mrboxtobox commented 1 month ago

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
  2. 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
  3. 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()

bblanchon commented 2 days ago

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