I like to think that since all languages are just meanings to talk to machines and that's all they do in the end it shouldn't be hard |
If all you want is to pass byte arrays (which is ultimately what the underlying machine deals with) back and forth then the problem becomes somewhat easier. You just create different processes that each runs whatever language you need and connect them via network sockets or pipes. You will still need to decide on a common data interchange format with well-defined semantics so that information can be de/serialized.
couldn't find a way for python |
In Python this is called "embedding". The Python code becomes a slave to the other language, which must call the library's relevant functions to initialize its data structures and load code. The master can then instantiate classes and call functions.
https://docs.python.org/3/extending/embedding.html
In the case of Java there's the JNI, which is, IMO, fairly cumbersome but not much more so than I would expect. Not quite as easy as .NET's P/Invoke, though.
Is there a reason for that? |
The reason is that C has stupidly simple functions and types. Take a look at what a C interface for FFI looks like: it's always very narrowly defined and all that gets passed around is integers, raw buffers, function pointers, and opaque pointers. It rarely strays very far from that, because it quickly becomes unusable.
Conversely, try instantiating a C++ class from C and then calling one of its polymorphic members, or calling a variadic template function instantiation. Let me know how that goes, because I've never even bothered trying.
Generally, the more complex the language's type system, the more difficult it's going to be to do FFI.