Share functions between source files in multiple programming languages

1.2k Views Asked by At

I'm trying to figure out how to share functions between source files when the source files are written in several different programming languages. Is there any way to share functions written in three languages across three different source files, as shown below? I want functions written in each language to be accessible from the other languages.

(To clarify, all of the source files are in the same folder.)

Java file:

public class JavaFile{
    public static String generateStringFromRegex(String theRegex){
        //native Java function, implement this using xeger
    }
    public static String generateRandomString(String theString){
        //return the result from the corresponding Javascript function
    }
    public static int getFileText(String filename){
        //return the result from the corresponding C++ function
    }
}

Javascript file:

function getFileText(fileName){
    //call the corresponding C++ function and return the value
}

function generateRandomString(theString){
    //native Javascript function
}
function generateStringFromRegex(int1, int2){
    //call the corresponding Java function and return the value
}

C++ file:

#include<string>

int main(){

}

string generateRandomString(string theString){
    //call the corresponding Javascript function and return the value
}
string generateStringFromRegex(string theRegex){
    //call the corresponding Java function and return its value
}

string getFileText(string fileName){
    //native C++ function: get the text of a text file
}
1

There are 1 best solutions below

4
On

Let me explain how the function call mechanism works and then you will be able to get a clearer picture on what is possible and what is not.

What happens when you call a function? [At a very top level] You call a function with arguments and it possibly returns a value. The arguments may be passed by reference or by value. The return address has to be stored somewhere so that the called function knows where to jump back.

The arguments must be prepared: Either the reference is being passed so the address is reused or by value which means a copy is being created on a stack. While returning the return value has to be stored somewhere for the calling function to access. This is also generally on the stack [doesn't have to be, can be a register on the CPU depending on CPU architecture or compiler optimization]. Finally while returning the return address is being returned too.

For each language the specifics of the above are different and not compatible. This is where you need to have bindings across languages explicitly which translate from one language to another. This binding is wrapped across libraries [object code]. So Eg. C program does not call a Java function directly. It simply has an unresolved symbol which the binding wrapper resolves in a compatible fashion. This in turn calls the java code.

One cannot call Java functions from C unless one has a JNI interface in the middle for the very purposes mentioned above. One cannot even call C++ code from C code without explicitly adding the support while compiling your C++ library.

Additionally if you have interpreted langauges like javascript, I am not sure you can call compiled languages at all.

So my answer would be For an arbitary set of languages : It depends on the languages but the answer is mostly no. If you writing bindings to support your libraries [remember compiled languages] : Yes.

If something above does not make sense feel free to ask in the comments.

EDIT: HTTP usage:

Programs can communicate with each other over sockets when across machines or using files/pipes/shared memory when on the same machine. Ultimately HTTP based communication is nothing but sockets. Something like apache thrift is also the same. It will use sockets (across machines) or pipes (within the same machine) to send data out to another program. Apache thrift is then providing the binding for the other language on the other side. So you have to have multiple programs running communicating over sockets. This is different from a function call within a same language or using bindings to a object library that is "linked" into the running executable. Hope you understand the difference.

Can a program written in one language communicate with a program written in another language? Absolutely we do it all the time. Your entire internet is based on different lanaguages. Your operating systems and softwares within it are in tons of langauges and they all works seamlessly with each other because they all respect each others interface.

As a developer though you need to understand that to use functions across various languages both sides may have to use thrift and that there is a difference between communicating over sockets using a RPC abstraction and calling a function via a function call. Hope you get the picture.