Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does code written in one language get called from another language

This is a question that I've always wanted to know the answer, but never really asked.

How does code written by one language, particularly an interpreted language, get called by code written by a compiled language.

For example, say I'm writing a game in C++ and I outsource some of the AI behavior to be written in Scheme. How does the code written in Scheme get to a point that is usable by the compiled C++ code? How is it used by the C++ source code, and how is it used by the C++ compiled code? Is there a difference in the way it's used?

Related

How do multiple-languages interact in one project?

like image 905
Hooray Im Helping Avatar asked May 08 '09 17:05

Hooray Im Helping


People also ask

How do I translate a code from one language to another?

Compilers convert one programming language into another. Usually, compilers are used to convert code so the machine can understand it. If we want it to be human-readable, we need a subset of compilers called transpilers. Transpilers also convert code however the output is generally understandable by a human.

How does code get translated?

Compilers are used to translate a program written in a high-level language into machine code (object code). Once compiled (all in one go), the translated program file can then be directly used by the computer and is independently executable.

Can code be written in other languages?

Over a third of programming language were developed in English speaking countries. But some of the well-known, highly-used coding languages were developed in non-English speaking countries e.g. Switzerland (PASCAL), Denmark (PHP), Japan (Ruby), Brazil (Lua), and The Netherlands (Python).

How do different coding languages interact?

Multiple languages can interact with: Piped input/output (ANY language can do this because input and output must by necessity be implemented in every non-toy language) Having code in one language compile to a native library while the other supports calling native code. Communicating over a loopback network connection.


2 Answers

There is no single answer to the question that works everywhere. In general, the answer is that the two languages must agree on "something" -- a set or rules or a "calling protocol".

In a high level, any protocol needs to specify three things:

  • "discovery": how to find about each other.
  • "linking": How to make the connection (after they know about each other).
  • "Invocation": How to actually make requests to each other.

The details depend heavily on the protocol itself.

Sometimes the two languages conspire to work together. Sometimes the two languages agree to support some outside-defined protocol. These days, the OS or the "runtime environment" (.NET and Java) is often involved as well. Sometimes the ability only goes one way ("A" can call "B", but "B" cannot call "A").

Notice that this is the same problem that any language faces when communicating with the OS. The Linux kernel is not written in Scheme, you know!

Let's see some typical answers from the world of Windows:

  • C with C++: C++ uses a contorted ("mangled") variation of the "C protocol". C++ can call into C, and C can call into C++ (although the names can be quite messy sometimes and it might need external help translating the names). This is not just Windows; it's generally true in all platforms that support both. Most popular OS's use a "C protocol" as well.

  • VB6 vs. most languages: VB6's preferred method is the "COM protocol". Other languages must be able to write COM objects to be usable from VB6. VB6 can produce COM objects too (although not every possible variation of COM objects).

    VB6 can also talk a very limited variation of the "C protocol", and then only to make calls outside: it cannot create objects that can be talked to directly via the "C protocol".

  • .NET languages: All .NET languages communicate compile to the same low-level language (IL). The runtime manages the communication and from that point of view, they all look like the same language.

  • VBScript vs. other languages: VBScript can only talk a subset of the COM protocol.

One more note: SOAP "Web Services" is really a "calling protocol" as well, like many other web-based protocol that are becoming popular. After all, it's all about talking to code written in a different language (and running in a different box at that!)

like image 57
Euro Micelli Avatar answered Sep 28 '22 03:09

Euro Micelli


Typically the C++ code will invoke an interpreter for the scripting language. The degree of interaction between the compiled and scripting code is dependent on the interpreter but there's always a way to pass data between the two. Depending on the interpreter, it may be possible to manipulate the objects on one side from the other side, such as a C++ function calling a method on a Ruby object. There may even be a way to control execution of one from the other.

like image 41
Pesto Avatar answered Sep 28 '22 03:09

Pesto