-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add WASM standalone builds #137
Comments
Hi, What is the steps to i understand how to build it. |
I was able to get it to build with this: diff --git a/modules/wasm.py b/modules/wasm.py
--- a/modules/wasm.py
+++ b/modules/wasm.py
@@ -657,6 +657,11 @@ def run_task_generate():
"ASSERTIONS=1",
"-s",
"ALLOW_MEMORY_GROWTH=1",
+ "-sSTANDALONE_WASM=1",
+ "-sWASM_ASYNC_COMPILATION=0",
+ "-sWARN_ON_UNDEFINED_SYMBOLS=1",
+ "-sERROR_ON_UNDEFINED_SYMBOLS=0",
"-sMODULARIZE",
"-sEXPORT_NAME=PDFiumModule",
"-std=c++11", But I'm not sure if it's better to patch the undefined symbols instead of ignoring them like that. |
And more two questions:
|
The only undefined symbol is If you want to call some functions in the library to test, here's an example that uses wasmtime with rust: use wasmtime::*;
use wasmtime_wasi::*;
fn main() -> Result<(), wasmtime::Error> {
let engine = Engine::default();
let module = Module::from_file(&engine, "pdfium.wasm")?;
let mut linker = Linker::new(&engine);
wasmtime_wasi::preview1::add_to_linker_sync(&mut linker, |s| s)?;
linker.define_unknown_imports_as_default_values(&module)?;
let pre = linker.instantiate_pre(&module)?;
let wasi_ctx = WasiCtxBuilder::new().build_p1();
let mut store = Store::new(&engine, wasi_ctx);
let instance = pre.instantiate(&mut store)?;
let init_library = instance.get_typed_func::<(), ()>(&mut store, "FPDF_InitLibrary")?;
init_library.call(&mut store, ())?;
let get_last_error = instance.get_typed_func::<(), i32>(&mut store, "FPDF_GetLastError")?;
println!("result={}", get_last_error.call(&mut store, ())?);
Ok(())
} wasmtime also has python bindings that should work the same way |
Hi! Just wanted to follow up on this. I think it would be really helpful. Thanks for your work on this library! |
Hi, I have added but get error: Error:
Do you know what can be wrong? Thanks. |
Thank you for looking into this! Emscripten adds some functions like that to the compiled WASM file. I'm not sure if it's possible to In the rust example I posted, that's what The python bindings don't look like they have a similar function, but we can do it manually with def define_unknown_imports_as_default_values(linker: Linker, module: Module) -> None:
for imp in module.imports:
if imp.module != "env" or not imp.name or not isinstance(imp.type, wasmtime.FuncType):
continue
func_ty = imp.type
result_tys = func_ty.results
ret = None
if result_tys:
ty = result_tys[0]
if ty in (ValType.f32(), ValType.f64()):
ret = 0.0
else:
ret = 0
linker.define_func(imp.module, imp.name, func_ty, lambda *_, r=ret: r)
engine = Engine()
module = Module.from_file(engine, "pdfium.wasi.wasm")
store = Store(engine)
linker = Linker(engine)
linker.define_wasi()
wasi_config = WasiConfig()
wasi_config.inherit_stdout()
store.set_wasi(wasi_config)
_define_unknown_imports_as_default_values(linker, module)
instance = linker.instantiate(store, module)
instance.exports(store)["FPDF_InitLibrary"](store) That's kind of hacky, but it seems to work. |
Nice. Do you have the commands to test the rust code? Thanks. |
To run that rust code, you'd need a [package]
name = "rust_wasm_test"
version = "0.1.0"
edition = "2021"
[dependencies]
wasmtime = "=24.0.0"
wasmtime-wasi = "=24.0.0" Put the Then you'd run |
Thanks for your work on this project!
Would you consider adding building a new wasm-wasi artifact that's compiled with emscripten's standalone mode?
I'm trying to use pdfium in a wasm engine like wasmtime or wasmer which can't use the JS shims that the current wasm build requires.
The text was updated successfully, but these errors were encountered: