Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a language "binding" communicate with the existing library?

Tags:

c++

node.js

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"?

like image 720
the_velour_fog Avatar asked Oct 17 '25 18:10

the_velour_fog


1 Answers

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))

like image 110
deviantfan Avatar answered Oct 19 '25 09:10

deviantfan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!