extism / python-pdk

Write Extism plugins in Python
https://extism.org
BSD 3-Clause "New" or "Revised" License
12 stars 1 forks source link

Experiment: Dynamic Linking of core #8

Open bhelx opened 2 months ago

bhelx commented 2 months ago

See my EIP for more details on this

How this experiment works

The way this compiler currently works is it builds a shim.wasm and a core.wasm. The shim had both the export and the import shims in it. This works fine when using wasm-merge, but creates a circular dependency when trying to dynamically load the modules. So i moved the imports into their own module. They could maybe be wasm-merged with core.wasm though?

When compiling count-vowels.wasm e.g., we get 3 modules in our build directory (hard coded to my home directory ~/py-out):

$ ls
core.wasm  import_shim.wasm  main.wasm

core.wasm exports the __invoke:

$ wasm-objdump core.wasm --section=Export -x

core.wasm:  file format wasm 0x1

Section Details:

Export[3]:
 - memory[0] -> "memory"
 - func[11050] <__invoke> -> "__invoke"
 - func[11052] <PyInit_extism_ffi> -> "PyInit_extism_ffi"

import_shim.wasm just has the __invokeHostFunc* exports.

main.wasm has the final export:

$ wasm-objdump main.wasm --section=Export -x

main.wasm:  file format wasm 0x1

Section Details:

Export[1]:
 - func[1] <count_vowels> -> "count_vowels"

These all get wasm-merged together in the end. But i left the build directory so i could try dynamically linking them. And it works:

$ extism call main.wasm count_vowels --input="Hello World" --link import_shim=import_shim.wasm --link core=./core.wasm  --wasi
{"count": 3}

Note that the way this is being wizened, i don't this this core is reusable. Discussed this in the EIP