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)
Hi,
I'm running into the following error when I try running
pdfium.wasm
withwasmtime
. I'm running on MacOS M3. Any ideas what could be wrong?Steps to Reproduce
Install Deps
Download and install pdfium-wasm
Run python
test_wasm.py
in the same directorySet 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 main():
Path to your PDFium WASM file
if name == "main": main()