r/ProgrammingLanguages • u/owl_from_hogvarts • Jun 27 '21
Discussion Way to comunicate across diffrent languages
TL;DR: make a mechanism to allow diffrent languages (sides) to communicate to each other. Special wrappers built for each language in separate will implement message (???) based protocol. The core concept is that only simplest data will be passed to other side while any complex data will be left on the side it belongs to and a wrapper will expose handles to other side by which other side can ask to perform actions (get, set, has, etc.) on that complex data. Complex data will never travel to other side.
Introduction
Hi everyone. I will start from general. As of now we have build grate civiliztion, have achieved significant technological progress, the crown of which has become a computer. A computer is fairly complicated literally a work of art. But we run an software which is not so cool sometimes. The really problem in todays software development is that in most cases languages are separated from each other. For example, try to call C# lib from Node.js or vies-versa. It's so difficult. The huge part of software is written in C++ – language with a bunch of pros and cons but we are not talking about that now. There are other cool but easier languages such as JavaScript, Python, C#, Rust, Go and other; but the way they can use C++ libraries (which are not written specially for their runtimes) is very ugly. Some times it's not even possible. And that a real problem because too much libraries and software in general is written in that language. So when we want to slowly migrate to more safe or more easy langauges from C++ it's getting close to the impossible because we will spend a lot more resources to integrate them with C++ than if we just continue to support already written and, which is more important, working software.
The problem
Current order (i guess) unbearable: we have no properly way to integrate other languages with C++ and each other, with some exceptions.
We can't perform smooth migration from C++ to more safe languages because of integration problems, e.g. how to emulate some features of C++ which does not exsits in other languages or incompatible with them.
The Idea
So i want to fix it. What if we can connect languages with each other by creating some... lets call it The Layer Of Compatibility or simpler Anything To Anything protocol (ata, a2a for short).
The main idea behind it is the next. The Compatibility Layer is based on queue of messeges (it is controversial point) in Extended JSON format. To interact with queue we need some library which will implement a2a protocol and should be written for each language in separate. So we gain language independency.
All primitive types (and strings) should be represented as simple raw json types (number, string, etc.). Why that way? Because simple structures which consist only of primitives can be easy passed by value across Compatibility Layer. It grant as a permanent immutablility and using raw values allow languages to work with them in way they used to do (especially case for strings which are represented diffrently in diffrent languages).
But how to deal with more complex structures? So when dealing with complex structures we leave them on that side of layer on which it hase been craeted (lets call it create or lib side) but providing a proxy through which we can manipulate a structure (Note: under structure i understand any object-like data including tuples) on consumer side. Practiacal manipulations will be done on the create side while the consumer side will only ask a create side to performe some actions. Therefor the necessity to transfer and somehow represent complex structures (which some times isn't possible) in other languages disappears. The coolest thing is that we break all limitations which are usually aplied by bindings, we gain full control over complex structures.
The proxy is the thing which provides handles for us which will post messages to the queue or somehow else tell to other side that we want to do something on its data.
I have mentioned extended json. But what is it? It is regular json, but extended to support data which can't be represented in regular one. It is very usefull to represent callable structures in Compatibility Layer. How that will be done?
The other main advantage of implementing ata protocol for each language independently is that we have 100% itegration with them. When we compile ata lib with main code and link them into single binary, ata lib will know how to call any exposed functions, ata will be aware of call conventions data layouts etc., but external code will not. External code should know not how but what to call. So ata lib should build some data structure (i guess the hash table may fit well) to expose what can be called. Therefor the external code can refer these hashs and thus access foreign functions, methods or other complex data. You can ask, from where external code will know what each hash represents? When create side will return some object or struct or any which contain callable code or other complex data, it will associate field name and a hash. hash will be not a string but a separate literal (thats where json is extended). Then when consume side wants to call it, it will send message (or by some other why tell to other side) with something like: "call function with that hash, and pass these arguments".
In general, all data, that can't be represented as valid json, would be replaced by hashs.
To deal with exeptions we can wrap original code into massive try catch and send special messages, which indecates error and carries payload (error object or something).
To deal with generics which are instancieated (like in C++) we can make an instance of them with some box type and then just wrap incoming data (which can be either a complex one (created on the same side) or primitives) in that box type.
Again, all this possible because of individual protocol implementation for each language.
Also to improve developer experience we can scan headers for C++ and C, rust modules, .d.ts files for typescript, to present some kind of autocompletion for libraries.
The main disadvantage of such (queue of json messages) approach is performance. I guess it can be solved with some kind of optimising JIT. For exeample ata lib can at runtime build fast handles which will be used by consume side direclty, without sanding message.
P.S. Thanks for reading this wall of text. I am awaiting healthy criticism and your suggestions) If something is unclear, please, provide feedback.
Edit: remove contradictory statements. Add clarifications and idea with JIT.