Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
170 lines (105 sloc) 7.44 KB

Polyglot is a collection of examples of how to interface pieces of code written in different languages, and/or execute code in a runtime environment that's not traditional for its language. Calling a C++ library from a Python program, or executing OCaml code in a web browser, are two examples amongst what Polyglot demonstrates.

For the time being, it focuses on the following runtime environments:

  • Native: code compiled for the native CPU
  • Interpreted: code run by an interpreter compiled for the native CPU
  • Web browser and Node.js (below, JS): code translated to JavaScript

And the following languages (with their traditional runtime environment):

Contributions are greatly appreciated.


Reasons to use several programming languages in the same program:

  • reuse: a new program calls an existing code base in another language
  • sharing: the same code runs on several environments
  • performance: a part of a program is compiled natively for execution speed while other parts are interpreted to ease development

Examples by increasing complexity level

The examples demonstrates some problems encountered when interfacing several languages and/or runtime environments, and some solutions! Sometimes, several solutions are presented.

"Hello World"s

"Hello World" examples are written in a single language, but executed in a runtime environment that's not traditional for this language. This is most often achieved by some sort of cross-compilation.

Running on JS environment

To run in node.js, a program can be translated to JavaScript.

CoffeScript maintains a list of languages that compile to JavaScript.

C and C++

Emscripten translates the LLVM bitcode to JavaScript. It can be used to translate any language with an LLVM-based compiler.


Js_of_ocaml translates the OCaml bytecode to JavaScript.

The latest commit in oBrowser dates from 2011. Ocamljs seems unmaintained as well (last commit in 2010).


Many translators need to be evaluated.

Guest language calls

Show how to call code written in another language (the guest language), from code executed in its traditional runtime environment (the host language).

The guest language can either be compiled as usual and executed via a foreign call (e.g. a Python extension written in C), or it can be translated to the host language's runtime environment (e.g. using Js_of_ocaml).

C and C++

C and C++ have very similar tool chains, and C is mostly a subset of C++, but they are not directly binary-compatible. In particular, they have different calling convention and name mangling. This is referred globally as linkage.

Probably the simplest strategy to make C and C++ codes interact is to (re-)compile everything using a C++ compiler.

The next thing is to use extern "C", which changes the linkage of a portion of the program. This allows calling a pre-compiled library.

Python host

ctypes implements dynamic loading of C libraries. It allows calling a pre-compiled C library purely in Python.

Python can also be extended in C/C++. The Boost.Python library can be helpful.

OCaml host

ctypes allows calling a pre-compiled C library purely in OCaml.

OCaml can also be extended in C.

JavaScript host

Some languages can be translated to JavaScript. This is the only way to run guest code in a web browser.

OCaml guest

Python guest

In node.js however, JavaScript can be extended:

C++ guest

Host language callbacks

Show how the code written in guest language can, in turn, call code written in the host language.

You can’t perform that action at this time.