All right, let's see some demos. Here, I'm going to show you the execution pipeline and how Node.js is working behind the scenes. So, first of all, I just have a JavaScript file here, and in just the JavaScript file, I'm calling a program, a C++ program, but don't think about C++ here, we're just seeing how the steps by step is doing.
Well, just to get started, we are reading a file. Remember I told you, JavaScript is a lie, because we are reading a string and saying it to V8, and V8 is doing all the magic behind the scenes. There's no such a thing as JavaScript. So, we are reading the file, getting all the context. Let's see. And then we compile. This compilation is just transforming all the JavaScript code into C++ instances, so we can call it right away. It's pretty nice. And then we can use run, which is like evolve, just execute it, just run this code. And we're going to have our results, and wait for events. Remember the event loop, remember the while true. The while true is going to be executed after we run our code, right? Because it's going to be executed in the future.
And just so you, just a matter of curiosity, here I'm reading file just as a C++ side, just copy paste, the easiest way to read a file, not using the LibuV in this case, but we're going to see that it's not that magic, right? It's not that hard. It's hard compiling and managing all the environment, but it's pretty cool. Here I had to gather all the libraries, so I've spent at least a month, my god, my job was all delayed trying to do this thing, I was crying at home, it was really hard. But you have to download V8 from the source, you have to compile, generate a binary, and here we're going to copy all the inputs, just C++ header, just references of how we're going to call this binary. We can think of this binary just as a web API, right? For libuvi, the same thing, I compile everything, and when I start C, I'm not a C++ developer, right? I'm a Node.js developer, and I know NodeMod. So NodeMod, we can watch for files and just execute another instruction. So I just implemented my own live reload here. But let's see these commands just working without any script. So I'm using make. So make is going to read that make file, gather all the binaries, and then it's going to generate a binary for us. And remember, Node.js is just a binary, right? Node.js, when you type node and the name of the file, Node.js is just reading the file, exactly the same way we are doing here. So I'm executing and I can see the hello world in the end. Pretty straightforward for now. But what if we can call console.log? When we are working with JavaScript, anything that relies on the environment, when you have to call something from the operating system, where you depend on the environment, like a console.log, or printing stuff, they are not in JavaScript. See this example how nice is it? If I run console.log, I don't see any error, which for me is weird, because console.log should be undefined, right? But in this case it's not undefined, it's just not printing anything.
Comments