2

Im trying to understand how a binding (port) to another language works in general, but to help clarify my question I will use the direct example of a project called libsass (A C/C++ implementation of a Sass compiler).
There is another project node-sass which is Node.js bindings to libsass.

Im assuming this means node-sass is a javascript program which runs on nodejs and nodejs acts as a proxy forwarding instructions to the libsass C++ system level program.

enter image description here

My question is: how does the nodejs intepreter "talk" to the libsass C++ application? - is it using sockets?

sub question: If node-sass exposed an API in the node environment by initialising objects, functions etc that were available to your own node scripts - is this by definition -the "binding"?

user2864740
  • 60,010
  • 15
  • 145
  • 220
the_velour_fog
  • 2,094
  • 4
  • 17
  • 29
  • A 'program' would run as it's own process; a library is generally loaded into the same process. – user2864740 Aug 23 '15 at 22:48
  • Ah I see this particular project is a library - but generally I would have thought that typically a program being ported to - *was* a program or a service in its own right ? – the_velour_fog Aug 23 '15 at 22:52
  • There's really little C++ to this question. "C/C++" are two languages. Sure, C++ has a language binding to C, but as this question shows, so does Node.js. And we don't go talking about "C/Node.js" either. – MSalters Aug 24 '15 at 07:31

1 Answers1

3

The C++ library part is, given that it´s really a library and not some server program, not running by itself and not listening to some socket. If a C++ lib is used in a C++ program, it´s integrated in this programs process too and not running somewhere else.

Many languages have built-in possibilites to access native C language APIs, including Node.js (with C being the de-facto standard for language interoperabilty, eg. because every somewhat important OS consists mainly of C too.). About C++ vs C, it´s not hard to write something in C++ and provide a C interface too.

In such cases, a language binding often is nothing more than something to wrap the complicated native access part in something more easy to use in the target language.

To elaborate a bit further because of the comment:

The OS itself has functions (to be used in C programs) to load C libraries on the fly, get specific functions of them and call them, without the names of lib and functions being known when the C program is compiled (eg. you could make a C program which asks the user to enter a lib name which is then used...).

Independent of that, every language is either made in a way that programs are compiled to "real" programs containing CPU instructions etc., these programs can be executed directly (example: C), or the programs of the language are made is some other format, but a "real" program is needed for every start to help the OS/CPU understanding what should be done (example: Javascript, Java.... You can´t run a program alone without having helper software installed, like a browser or the JRE).

For this second type, the helper software can make use of the lib loading functions of the OS, and if the JS/Java program contains instructions to do so... (and for the first "real" type, a certain level of compatibilty with C libs is automatically given because they use the same binary format (yes, that´s simplified))

Community
  • 1
  • 1
deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • Ah, ok thanks, so C/C++ based Operating Systems provide a kind of higher level environment where C/C++ programs are able to interact using native calls? Does this also mean sockets be more suitable for programs and running services that dont share the same underlying language e.g. if a Java application needed to communicate to a C based program?. – the_velour_fog Aug 23 '15 at 23:16
  • @user4668401 See the edited answer. And no, you don´t need sockets in Java either; Java too has native access possibilities. – deviantfan Aug 23 '15 at 23:32