3

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.

4
  • 1
    There are more direct ways of turning Python into JavaScript, which may be more appropriate for your situation. The projects listed in the Python section here: altjs.org/#ports and here: github.com/jashkenas/coffee-script/wiki/… may all be viable options for you. Commented Nov 7, 2013 at 4:20
  • 1
    You may also find this project interesting: github.com/replit/jsrepl. It's a multi-language scripting engine in javascript that you can run in a browser. Supports Python (which I think was ported to it using emscripten). So instead of compiling Python to js you can just send Python to the browser. Commented Nov 7, 2013 at 4:49
  • 1
    Given the SO question I found and posted below, I think @slebetman is right, that doing the sort of thing that jsrepl does to run arbitrary Python code against its emscripten-compiled CPython interpreter is going to be your best bet if you want to preserve the original Python code. github.com/replit/empythoned is their Python-specific example. Commented Nov 11, 2013 at 8:51
  • You can also run Python scripts in JavaScript using Skulpt: skulpt.org Commented Jan 12, 2014 at 18:09

1 Answer 1

6
+150

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, but my function names after going through cython get scrambled up, to "my_func" is called "<bunch of weird stuff>_my_func" now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.