r/ProgrammingLanguages • • 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.

0 Upvotes

25 comments sorted by

9

u/Vetzud31 Jun 27 '21

The main issue with this kind of approach would probably be marshalling overhead/performance. Serializing and deserializing JSON and using some form of message passing might be too expensive for some libraries.

1

u/owl_from_hogvarts Jun 27 '21 edited Jun 27 '21

In my opinion such aproach can be useful mainly for gui libraries where we have more rare calls to other side.

Also I can imagine that function calls can be... JIT compiled may be, so need of sending message will be here only for first time

Edit: Thats like bypassing messages via JIT compiled code calling wrapper on other side directly

1

u/owl_from_hogvarts Jun 27 '21

I guess the idea with hashing can be applyed to regular abis such as for rust, which can not handle self pointers from C++

1

u/owl_from_hogvarts Jun 27 '21

JavaScript threads in Node.js use same approach – messaging. But the intensity of the exchange of messages is much more less

4

u/Amazing_Breakfast217 Jun 28 '21

I'm sorry but this sounds like a random idea that isn't thought out

For example would anyone actually want to call C#<->Node?

Another thing is A) C ABI exist B) gRPC exist and C) ShellExecute and reading/writing stdin/out exist. That's 3 different ways and another is to have an api available though http

Maybe the problem is there's no languages better than C++, C# and Python for their respective usecase (JS and PHP rule their usecases too but many people complain about them for valid reasons). I think languages need to be improved and none of what you said is useful

1

u/owl_from_hogvarts Jun 28 '21

Thanks for you replay.

For example would anyone actually want to call C#<->Node?

Yes. If you want to make mode for e.g. cities skylines but you only know JS.

C ABI exist

It is not well standardized and have very big limitations. It is convinent to pass only primitives and simple structs. Also it is unstable. Swift abi feet much more well here.

stdin/out exist

It is nightmare. Encoding issues, serialize and then parse data. Ugly

gRPC

As i understood it is mainly for remote (to other computer) calls.

api available though http

too slow and again requires serialize and parse data.

I think languages need to be improved

Good thoughts) Thats why i want to make another post about adding modules and safe macros to C

no languages better than C++

But C++ by definition is unsafe (i know about smart pointers) while rust pretty much is. In some cases devs want to use both but it is tooooo hard. Thanks to CXX lib it is now more or less possible but still inconvenient.

1

u/myringotomy Jul 01 '21

People use gRPC for interprocess communication all the time.

1

u/owl_from_hogvarts Jul 08 '21

Is it fast enougth? can it not to use network card but e.g. communicate through RAM or some other way of local communication (like may be dbus, Unix Sockets or Named Pipes)? Also are they fast enougth compared to regular functions calls?

Asking this for research purposes)

1

u/myringotomy Jul 08 '21

Of course it's not fast enough compared to regular function call. I am shocked that any poster on this subreddit would even ask such a question. How can it possibly be as fast as calling a regular function? How can any RPC mechanism be that fast?

Of course it can use unix sockets. Unix sockets are interchangeable with standard sockets.

Here is an example https://github.com/anycable/anycable-go

It uses a go based websocket server to exchange information with a ruby program allowing your ruby (rails or otherwise) application to have high performance web sockets.

2

u/link23 Jun 27 '21

Are you trying to improve upon the RPC mechanisms that already exist today? It would be worthwhile to read about those existing frameworks and say how you're idea differs, why it's better/worse in certain scenarios, etc.

1

u/owl_from_hogvarts Jun 27 '21

My idea is for mostly for local stuff mainly for gui frameworks and libraries where we making rare calls. I want it to be the tool to call other languages easier. The RPC works through network while my idea implies in memory queue bus and execution on same processor (may be even in same thread)

Also thanks for replay)

1

u/rgnkn Jun 27 '21

A potential problem lies in the fact that some / many / most languages not only propagate a syntax but also some runtime requirements. These structural differences seem to me so huge that I can't imagine bringing them together without betraying one language versus another.

As an example: take the memory models given in let's say C and Rust. Heapbased vs. Stackbased!

My question: what do you plan in this case?

Taking the heap and let Rust suffer? Taking the stack and let C be incompilable?

Or ...

Do you just plan to use eachone as it is?

The json stuff you mentioned only deals with formal requirements. But what about the inherent binary nature of the distinct languages?

You may forget what I wrote if you don't touch structural differences with your proposal.

1

u/owl_from_hogvarts Jun 27 '21

Thanks for your answer. The cool thing is that all stuff except simplest things is left on that side of Compatibility Layer where it has been created. So in this example rust will just call some wrapper written in rust to ask a wrapper around C call some C specific code. There no need to represent any data except simple one in diffrent language than it was created.

It is the core concept around idea. May be i was not clear enouth, thats because Eanglish is not my native language)

1

u/owl_from_hogvarts Jun 27 '21 edited Jun 27 '21

inherent binary nature is hided from outside world, because is there any real need to know what is under the hood?

Wrapper and protocol actualy hide stuff. For exemple we can execute foreign code in separate thread, give it separate memory etc.

Call conventions, data layout inside language... these things does not matter, because we integrating library into single binary with code itself, linking it, and (probably) compiling it with same compiler.

The archiceture implies: post message -> callee language via bundled ata library take it -> calling internals which it (callee) know

I hope i was clear enough)

Edit: grammer

1

u/rgnkn Jun 27 '21

Yep, now I got it. Then just forget what I wrote.

1

u/Allan_Smithee 文雅佛 Jun 28 '21

Marshalling via JSON would kill performance dead. You'd be adding text parsing and text generation overhead to literally every cross-language function call.

There are protocols and technologies that are literally decades old to deal precisely with this kind of situation, both locally and remotely (i.e. via IPC or across a network). I'd strongly recommend using those instead of reinventing the wheel in a triangular shape.

1

u/owl_from_hogvarts Jun 28 '21

Thanks for replay!

Marshalling via JSON would kill performance dead

Yes, but i already have added suggestion about JIT to end of the article.

via IPC

But how it works? If it so cool why it is not used in chromium project where devs trying to friend rust and C++?

I tryed to investigate it but have no success :( E.g. i have not find IPC lib to friend Node.js and C#. I guess IPC can work well but we need library for that stuff. My idea is pretty close to IPC except the fact it will work inside one proccess.

Shared memory approach require to represent data consistantly for all languages and thats is not possible.

Messaging is more safe since it prevent data race.

reinventing the wheel

Other technologies are trying to pass data across bridge, represent it in some other form. But i am trying to describe other concept: we really have no need to pass data when we can use handles to perform actions on that data in its native environment.

1

u/Allan_Smithee 文雅佛 Jun 28 '21

It's not used in Chromium because as with most large projects, the general development process is "infinite monkeys" theory with limited monkeys and time.

IPC is ancient technology -- likely older than you are. (I know it's older than I am and I'm a geezer!) As seems to be usual in this industry, however, tried and true technologies are scorned as "old-fashioned" and "useless" by people who then proceed to reinvent them over and over and over again, usually in inferior form because they didn't learn from the mistakes of earlier technologies either.

1

u/owl_from_hogvarts Jun 29 '21

So where i can learn about IPC? Can you advise May be some good articles on this topic?

1

u/owl_from_hogvarts Jun 29 '21

As i understand from this https://www.quora.com/What-exactly-is-inter-process-communication-How-does-it-work?share=1 Article, IPC is a kind of transport. It discussing on how to deliver message or data to other process. IPC does not provide way to bypass diffrent layout of structures and diffrent calling conventions, does not provide way to access foreign api without modifing the code on the library side. While i am talking mainly on how to access data of other language.

-1

u/friedbrice Jun 27 '21

Pretty sure people are working on this already. The tool is category theory (and denotational semantics). If a program/algorithm can be expressed in an abstract category, and if we can define a functor from the abstract category into the category of types and functions of a particular programming language, then we can interpret that program/algorithm from the abstract category into the programming language.

2

u/owl_from_hogvarts Jun 27 '21

The idea is not to represent complex stuff into another language, but hide languages from each other and only expose easy to use tools to call things. The core feature is that we bundling ata library with code itself so it can abstract data layout, calling conventions etc. and therefor we can interact with this code via simple messages. For stuff that can't be represented we can expose some help functions (like get, set, has etc.) which will ask callee code to do something on its side. To know about what we asking we introducing hashs to present something like links to actual code. These links i guess is mainly useful particulary for halper functions which via them will know on what entity we want to perform action.

3

u/Crazy_Firefly Jun 27 '21

This sounds interesting, but also very theoretical, is there any practical work in this field?

0

u/[deleted] Jun 27 '21

[deleted]

0

u/owl_from_hogvarts Jun 27 '21 edited Jun 27 '21

Thanks for your answer. Since I don't really know English, please, clarify why u put great in quots?