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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add esbuild-py #4696

Open
keller-mark opened this issue Apr 15, 2024 · 4 comments
Open

Add esbuild-py #4696

keller-mark opened this issue Apr 15, 2024 · 4 comments

Comments

@keller-mark
Copy link

馃悕 Package Request

Hi, I am interested in creating a pyodide package for https://github.com/keller-mark/esbuild-py to be used in jupyterlite. Esbuild-py contains bindings to the Golang-based esbuild.

I cannot simply run

GOOS=js GOARCH=wasm go build -o _esbuild.wasm

because cgo (which i am using to create the existing non-pyodide Python bindings) is not compatible with WASM. However there is already a WASM build of esbuild available https://github.com/evanw/esbuild/blob/70893e4d89bab1dbf2d512593f9e4d5bcb1f76bf/scripts/esbuild.js#L129

I am just unsure how to construct the meta.yaml file and subsequently how to modify/extend the ctypes-based bindings https://github.com/keller-mark/esbuild-py/blob/1d3aebd2cda143b84cca9349861f44cc8d1676d4/src/esbuild_py/esbuild.py#L32 to support WASM-based bindings which will presumably work differently.

@keller-mark
Copy link
Author

I think the question comes down to how to create a pyodide package that contains bindings for an existing .wasm binary

@ryanking13
Copy link
Member

That's an interesting problem.

If you can build esbuild WASM binaries with the Emscripten toolchain, I guess we could treat it like a normal Emscripten shared library and load it in Pyodide, but I doubt that WASM binaries built with go's build toolchain would be compatible.

@hoodmane
Copy link
Member

It only seems to expose a single function which takes a JavaScript string and returns a JavaScript string. So maybe just use the JS FFI?

@keller-mark
Copy link
Author

I doubt that WASM binaries built with go's build toolchain would be compatible

Is this due to a differing ABI between what Emscripten would produce and what the Go WASM implementation would produce? It seems that Go does not yet support exporting functions via wasm which presents another obstacle to this method. I was able to use GOOS=wasip1 GOARCH=wasm go build -o _esbuild.so but then I run into this

Python 3.11.3 (main, Mar 31 2024, 11:27:51) [Clang 18.0.0 (https://github.com/llvm/llvm-project 75501f53624de92aafce2f1da698 on emscripten
>>> so = ctypes.CDLL(lib_path)
>>> so.transform
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python311.zip/ctypes/__init__.py", line 389, in __getattr__
  File "/lib/python311.zip/ctypes/__init__.py", line 394, in __getitem__
AttributeError: Tried to lookup unknown symbol "transform" in dynamic lib: /Users/mkeller/research/dbmi/vitessce/esbuild-py/build/lib.emscripten_3_1_46_wasm32-cpython-311/_esbuild.cpython-311-wasm32-emscripten.so
>>> so.__dict__
{'_name': PosixPath('/Users/mkeller/research/dbmi/vitessce/esbuild-py/build/lib.emscripten_3_1_46_wasm32-cpython-311/_esbuild.cpython-311-wasm32-emscripten.so'), '_FuncPtr': <class 'ctypes.CDLL.__init__.<locals>._FuncPtr'>, '_handle': 23285320}

maybe just use the JS FFI?

Thanks very much for this suggestion, I will proceed with this method, calling the official esbuild WASM build that works in the browser using https://pyodide.org/en/stable/usage/api/python-api/code.html#pyodide.code.run_js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants