JavaScript – How to Combine C/Fortran with JavaScript?

cfortraninteroperabilityjavascript

I'm working on a project where I need heavy numerical calculations to be real-time visualized with something flexible like D3.js. Are there frameworks out there that would let me painlessly achieve this? Or do I need to use some sort of "glue" code written in, say, Python?

Best Answer

What you're asking for is interoperation between C/Fortran and Javascript, interopation can be done in a great many ways, the most common two that come to mind however are:

Foreign Function Interface (FFI) bindings and Inter-Process Communication (IPC).

FFI is where a standard agreed upon protocol is built into the assembly at compile time which allows access to the assemblies facilities by foreign languages. There are a variety of FFI standard interfaces, but I am familiar with none that allows an interpreted language to interface with a compiled language or vice versa; the concept doesn't fit with using interpreters as their scripts are plain text with no assemblies to interface.

That said, the interpreter may have facilities for FFI access. Node.JS comes to mind as a javascript interpreter that likely has facilities for FFI access. Alternatively you could use your C program to host a javascript interpreter like Chrome's V8 engine to have your C execute the javascript directly if this would help you, though I don't suspect it would.

Inter-Process Communication (IPC) is where you run the process in one language, and the other process in the other language, and then use a form of IPC to communicate between the two. Commonly this is done via shared memory space where both processes can put data into that memory space and read from it to send messages back and forth. The same technique is used with files sometimes though this is a high-overhead technique due to the IO costs, it does however afford a durability in the event of either or both processes crashing, or even the machine losing power.

The shared memory technique for IPC often comes in a couple flavors:

  • A parent process calling a child process and using "pipes" to send messages directly to the child process. I believe Node.JS suggests this technique for it to interoperate with other languages
  • Named pipes which are system-wide shared memory spaces registered with the kernel and accessible by an identifier

Other than shared memory or disk, the other largely used IPC technique is network communication. Whenever your browser accesses a website it is communicating with another process, so this is IPC, and in this way web services have become a hugely common way of accomplishing IPC. Direct TCP techniques are still used as well as even UDP for certain tasks, but the point is the network may act as a communication layer between processes just as a memory space or disk space may.

All of this said, my suggestion is you use web services for IPC as your language interoperation technique, because you wish for visualization on a browser.

That's all. I hope you learned something, carry on :)

Related Topic