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

add support for wasm standalone #140

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ jobs:
- name: Generate
run: python3 make.py generate-${{ matrix.config.target }}

- name: Test WasmTime
run: python3 make.py test-${{ matrix.config.target }}time

- name: Archive
run: python3 make.py archive-${{ matrix.config.target }}

Expand Down
5 changes: 5 additions & 0 deletions make.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- build-wasm
- install-wasm
- test-wasm
- test-wasmtime
- generate-wasm
- publish-wasm
- publish-to-web-wasm
Expand Down Expand Up @@ -211,6 +212,10 @@ def main(options):
elif task == "test-wasm":
wasm.run_task_test()

# test - wasmtime
elif task == "test-wasmtime":
wasm.run_task_test_wasmtime()

# generate - wasm
elif task == "generate-wasm":
wasm.run_task_generate()
Expand Down
83 changes: 80 additions & 3 deletions modules/wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,70 @@ def run_task_test():
l.ok()


# -----------------------------------------------------------------------------
def run_task_test_wasmtime():
import wasmtime
from wasmtime import Engine, Func, Instance, Linker, Module, Store
from wasmtime.loader import WasiConfig

l.colored("Testing with wasmtime...", l.YELLOW)

current_dir = f.current_dir()

for target in c.targets_wasm:
# paths
relative_dir = os.path.join(
"build",
target["target_os"],
target["target_cpu"],
)

root_dir = os.path.join(current_dir, relative_dir)
gen_dir = os.path.join(root_dir, "gen")
gen_out_dir = os.path.join(gen_dir, "out")
wasm_file = os.path.join(gen_out_dir, "pdfium.std.wasm")

# create the engine and store
engine = Engine()
store = Store(engine)

# load the WebAssembly module
module = Module.from_file(engine, wasm_file)

# create a linker to provide the necessary imports
linker = Linker(engine)

# WASI support using wasmtime.loader.WasiConfig
wasi_config = WasiConfig()
wasi_config.inherit_stdin()
wasi_config.inherit_stdout()
wasi_config.inherit_stderr()
store.set_wasi(wasi_config)

# instantiate the WebAssembly module with linker
instance = linker.instantiate(store, module)

# now we can invoke exported functions from the WebAssembly module
try:
init_library = instance.exports(store)["FPDF_InitLibrary"]
init_library(store)
l.i("FPDF_InitLibrary successfully called.")
except KeyError:
l.e("Function 'FPDF_InitLibrary' not found.")

# invoke 'FPDF_GetLastError'
try:
get_last_error = instance.exports(store)["FPDF_GetLastError"]
result = get_last_error(store)
l.i(f"Result from FPDF_GetLastError: {result}")
except KeyError:
l.e("Function 'FPDF_GetLastError' not found.")

l.i(f"Result: {result}")

l.ok()


# -----------------------------------------------------------------------------
def run_task_generate():
l.colored("Generating...", l.YELLOW)
Expand Down Expand Up @@ -660,21 +724,34 @@ def run_task_generate():
*base_command,
"-o",
os.path.join(gen_out_dir, "pdfium.js"),

]
r.run(" ".join(umd_command), cwd=gen_utils_dir, shell=True)

# Generate ES6 module, only .js will be generated (no .wasm)
l.colored("Compiling ES6 module with emscripten...", l.YELLOW)
es6_command = [
*base_command,
"-s"
"-s",
"EXPORT_ES6=1",
"-o",
os.path.join(gen_out_dir, "pdfium.esm.js"),
]
r.run(" ".join(es6_command), cwd=gen_utils_dir, shell=True)

# Generate STANDALONE module, only .js will be generated (no .wasm)
l.colored("Compiling STANDALONE module with emscripten...", l.YELLOW)
std_command = [
*base_command,
"-s" "STANDALONE_WASM=1",
"-sSTANDALONE_WASM=1",
"-sWASM_ASYNC_COMPILATION=0",
"-sWARN_ON_UNDEFINED_SYMBOLS=1",
"-sERROR_ON_UNDEFINED_SYMBOLS=0",
"-o",
os.path.join(gen_out_dir, "pdfium.std.js"),
]
r.run(" ".join(std_command), cwd=gen_utils_dir, shell=True)

# copy files
l.colored("Copying compiled files...", l.YELLOW)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docopt
black
pygemstones>=0.0.13
wasmtime
Loading