Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How compile function written in python to JavaScript (emscripten)?

I have a simple function written in python which I want to port to javascript.

I have compiled python 2.7 into a .so library, so thats not the issue.

The problem I'm having is that after I compile my program with cython, the function names get all scrambled up, which means I don't know how to preserve the functions when i run emcc.

Does anybody have any experience compiling python programs to js with emscripten?

Any information would be appreciated.

Note: I want to preserve the exact functionality to that of python, I don't want something that translates a python program into javascript.

like image 757
Firas Dib Avatar asked Jul 03 '26 13:07

Firas Dib


1 Answers

This other question, with an accepted answer, complains about the same issue: Cython mangling function names and making it difficult to access from C++: Embed python function in C++

The accepted answer states that Cython isn't meant for this sort of thing at all, suggesting you can't do what you want in this fashion:

You're not going to be able to get the interoperation you want that way. If you open and inspect hello.c you won't find "static int say_hello" anywhere in there. Cython is designed for letting Python use C libraries, not letting C libraries use python.

The not-accepted next answer suggest that specifying public will not mangle the function name, although he also mentions linking problems.

# (in the generated C file hello.c)
__PYX_EXTERN_C DL_IMPORT(...) say_hello(...);

Worth a shot, but please consider the other options in the comments if it fails.

like image 83
Vitorio Avatar answered Jul 05 '26 02:07

Vitorio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!