Hello, my name is Lucas Wasilewski and just like I put in the project description on my github since I started programming with NodeJS (at the beginning of 2021) I've always wanted to write something that looked like the tool, this only increased after I watched the documentary about the project and I was amazed at how the open source world can have several twists and turns and be very welcoming when it wants to. And after a week of a lot of head-scratching, I decided to write this article so that future crazy programmers who want this challenge don't make the same mistakes I did.
Javascript Runtimes
This term can easily mislead anyone who doesn't understand much about the subject and so a good definition is needed:
A Javascript runtime is a tool that allows you to run the language outside the browser
Nowadays there are 3 popular runtimes: NodeJS, Deno (Node Killer) and Bun (Deno Killer), but they basically do the same thing: they allow you to use javascript away from the browser and use other libraries to create features new ones, and that's very good, since you can use any of them to build a server, create libraries and even mobile or terminal applications.
Both Node and Deno were created by the same person: Ryan Dahl, and back in 2009 he created the tool to enable developers to create "async IO" applications, that is, that did not block the main thread but still continued to respond to requests, with this in mind he created Libuv, a library that does just that. Until then the project was just a big pile of C and if he wanted more people to use the tool he needed some language that was easier to understand and use, coincidentally, at the same time Google launched V8, which in general is a ultra-fast javascript compiler, this made him combine the two and thus create Node.
Some time later (9 years more specifically), Ryan left the project and went to work on other things that he considered more interesting, this made him realize several errors that could be fixed in Node, but the community was already very large and having to take a big step back was impossible, so, determined to do a better job, he creates Deno, another IO runtime that promises to be much superior to Node, as of today (2024) Deno is in version 2.0 and is very stable for projects and the community.
This whole story made more people join the runtime community and this also led us to the creation of Bun, and much better, mine and your runtime! Now let's get down to business.
Compiling V8
As previously stated, V8 is the Node engine, so we will have to actually download it and compile it manually to have access to its libraries and headers. As it is a Google project, they have their own methods for downloading and compiling, so for this we will have to follow their manual: link, just copying and pasting will take you to the final commands.
However, here I made a mistake that took 3 days for me to realize that I was doing everything wrong. After generating the build configuration files with:
tools/dev/v8gen.py x64.release
You need to be very careful with the args.gn file inside the out.gn/x64.release/ folder because it contains the build configuration that the ninja (compilation tool) will use to generate the library files, some Old tutorials use the v8_monolithic = true parameter, but in recent versions this is no longer used. According to this StackOverflow comment, we now need to use the is_component_build = true parameter to generate the right files and modify the flags when compiling the file, something very silly that if you don't pay attention to can waste precious time.
After placing the rest of the flags correctly, we just need to run the command to compile the project
ninja -C out.gn/x64.release
In the meantime, go eat something, because V8 is a very extensive project with countless tests, depending on your machine this process can easily take 1 hour or more, so leave it running and continue reading.
Where is console.log?
After compiling you can take a look at v8/samples/hello-world.cc and start to get an idea of how to compile javascript, but specifically these lines:
v8::Local<:string> source = v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'"); // Compile the source code. v8::Local<:script> script = v8::Script::Compile(context, source).ToLocalChecked(); </:script></:string>
Go ahead and play with the string that contains "Hello World", create functions, loops, conditionals and go crazy when you realize that if you include the classic console.log() you will receive an undefined, that firstly it perplexed me, I always thought that the console object was part of V8 itself, but in fact Node itself includes it and browsers include it as part of the DOM (post from 2012 saying that console.log is probably not supported by browsers), meaning we'll have to create it ourselves.
Hello World!
To be able to create our own functions, we first need to understand that V8 works with several scopes, one of them is context, where it is through this that the runtime knows where and how to execute the script individually, within it there may be a global object that is shared among all the others, and it is within it that we will insert our custom functions.
tools/dev/v8gen.py x64.release
With these lines we were able to create an object called global, we inserted a "print" function template that when executed calls the Print function.
v8::Local<:string> source = v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'"); // Compile the source code. v8::Local<:script> script = v8::Script::Compile(context, source).ToLocalChecked(); </:script></:string>
The Print function receives this crazy parameter that contains information about the function call within javascript and it is through it that we iterate over all the items within it, transform them into a C string and print them on the screen, very direct, very simple and it fulfills its role, that's enough to put it in a file, read it and play it on the V8 (I leave that one in your hands).
v8::Local<:objecttemplate> global = v8::ObjectTemplate::New(GetIsolate()); global->Set(GetIsolate(), "print", v8::FunctionTemplate::New(GetIsolate(), this->Print)); v8::Local<:context> context = v8::Context::New(GetIsolate(), nullptr, global); </:context></:objecttemplate>
Libuv
Well, I hope that by now you've been able to follow along and have even stopped reading to make some unique implementations for your homemade Node, but V8 will only take us so far, so that we can get closer to a professional runtime we need to make javascript able to perform more operations, to do this we will use Libuv, which was created precisely for this.
You can find the tutorial to install and compile here. The important thing to note here is that it gives us the freedom to do asynchronous operations, that is, without blocking the main thread, thus allowing the program to continue executing while doing heavier work (like opening a file or waiting for requests in a server socket).
It itself is already built-in with the functionality to create an http server, so we just need to synchronize it with the V8 calls. Make no mistake, this is not an easy task, because the interface of the two libraries differs a lot so it is difficult to connect both, but there is always a way and the node source code is open so make sure to steal some ideas from there
Conclusions
We have reached the end of another article and with it we look at some details that I noticed during the implementation. The first is definitely the complexity, of course, it's not a simple project, but once you understand how to interact with the V8 interface things go very quickly.
This project made me understand Node much better too. The fact that the runtime is just a conglomerate of libraries communicating makes it very easy to understand how more complex things (like the "event-loop") work.
If you want to see what I did right, or probably very wrong, please take a look at the project on github: done
Talk is cheap, show me the code - Linus Torvalds
## References
https://github.com/libuv/libuv
https://v8.dev/docs
https://stackoverflow.com/questions/71213580/cant-get-v8-monolith-to-genorate
https://github.com/ErickWendel/myownnode
https://github.com/WasixXD/done
The above is the detailed content of Creating a Runtime. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
