From native to JavaScript in Electron
How do Electron's features written in C++ or Objective-C get to JavaScript so they're available to an end-user?
Segundo plano
Electron is a JavaScript platform whose primary purpose is to lower the barrier to entry for developers to build robust desktop apps without worrying about platform-specific implementations. However, at its core, Electron itself still needs platform-specific functionality to be written in a given system language.
In reality, Electron handles the native code for you so that you can focus on a single JavaScript API.
How does that work, though? How do Electron's features written in C++ or Objective-C get to JavaScript so they're available to an end-user?
To trace this pathway, let's start with the app
module.
By opening the app.ts
file inside our lib/
directory, you'll find the following line of code towards the top:
const binding = process.electronBinding('app');
This line points directly to Electron's mechanism for binding its C++/Objective-C modules to JavaScript for use by developers. This function is created by the header and implementation file for the ElectronBindings
class.