chrome v8 引擎_了解Chrome V8引擎如何将JavaScript转换为机器代码

chrome v8 引擎

by Mayank Tripathi

通过Mayank Tripathi

了解Chrome V8引擎如何将JavaScript转换为机器代码 (Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code)

Before diving deep into the core of Chrome’s V8, first, let’s get our fundamentals down. All of our systems consist of microprocessors, the thing that is sitting inside your computer right now and allowing you to read this.

在深入探讨Chrome V8的核心之前,首先让我们了解一下基础知识。 我们所有的系统都由微处理器组成,它现在位于计算机内部,可以让您阅读。

Microprocessors are tiny machines that work with electrical signals and ultimately do the job. We give microprocessors the instructions. The instructions are in the language that microprocessors can interpret. Different microprocessors speak different languages. Some of the most common are IA-32, x86–64, MIPS, and ARM. These languages directly interact with the hardware so the code written in them is called machine code. Code that we write on our computers is converted or compiled into machine code.

微处理器是微型机器,可以处理电信号并最终完成工作。 我们给微处理器指令。 这些指令使用微处理器可以解释的语言。 不同的微处理器使用不同的语言。 最常见的是IA-32,x86-64,MIPS和ARM。 这些语言直接与硬件交互,因此用它们编写的代码称为机器代码。 我们在计算机上编写的代码将转换或编译为机器代码。

That’s what machine code looks like:

那就是机器代码的样子:

It consists of instructions that are performed at a particular piece of memory in your system at a low level. You must feel lucky for not having to write all this to run your program!

它由在系统中特定级别的内存上执行的低级指令组成。 您不必为编写所有程序来运行程序就感到幸运!

High-level languages are abstracted from machine language. In the level of abstraction below, you can see how far JavaScript is abstracted from the machine level. C/C++ are relatively much closer to the hardware and hence much faster than other high-level languages.

高级语言是从机器语言中抽象出来的。 在下面的抽象级别中,您可以查看JavaScript从计算机级别抽象的程度。 C / C ++相对更接近硬件,因此比其他高级语言要快得多。

Now back to the V8 engine: V8 is a powerful open source Javascript engine provided by Google. So what actually is a Javascript Engine? It is a program that converts Javascript code into lower level or machine code that microprocessors can understand.

现在回到V8引擎:V8是Google提供的功能强大的开源Javascript引擎。 那么,什么是Javascript引擎呢? 它是一个将Javascript代码转换为微处理器可以理解的较低级别或机器代码的程序。

There are different JavaScript engines including Rhino, JavaScriptCore, and SpiderMonkey. These engines follow the ECMAScript Standards. ECMAScript defines the standard for the scripting language. JavaScript is based on ECMAScript standards. These standards define how the language should work and what features it should have. You can learn more about ECMAScript here.

有不同JavaScript引擎,包括RhinoJavaScriptCoreSpiderMonkey 。 这些引擎遵循ECMAScript标准。 ECMAScript定义了脚本语言的标准。 JavaScript基于ECMAScript标准。 这些标准定义了语言应如何工作以及应具有的功能。 您可以在此处了解有关ECMAScript的更多信息。

The Chrome V8 engine :

Chrome V8引擎:

  • The V8 engine is written in C++ and used in Chrome and Nodejs.

    V8引擎是用C ++编写的,并在Chrome和Nodejs中使用。
  • It implements ECMAScript as specified in ECMA-262.

    它实现ECMA-262中指定的ECMAScript。
  • The V8 engine can run standalone we can embed it with our own C++ program.

    V8引擎可以独立运行,我们可以将其嵌入我们自己的C ++程序。

Let us understand the last point a little better. V8 can run standalone and at the same time we can add our own function implementation in C++ to add new features to JavaScript.

让我们更好地了解最后一点。 V8可以独立运行,同时我们可以在C ++中添加我们自己的函数实现,以向JavaScript添加新功能。

So for example: print('hello world')is not a valid statement in Node.js. It will give error if we compile it. But we can add our own implementation of the print function in C++ on top of the V8 which is open source at Github, thus making the print function work natively. This allows the JavaScript to understand more than what the ECMAScript standard specifies the JavaScript should understand.

因此,例如: print('hello world')在Node.js中不是有效的语句。 如果我们编译它会给出错误。 但是,我们可以在Github上开源的V8之上添加自己的C ++打印功能实现,从而使打印功能本地化。 这使JavaScript不仅可以理解ECMAScript标准指定JavaScript应该理解的内容。

This is a powerful feature since C++ has more features as a programming language as compared to JavaScript, as it is much closer to hardware like dealing with files and folders on the hard drive.

这是一项强大的功能,因为与JavaScript相比,C ++作为编程语言具有更多的功能,因为它更接近硬件,例如处理硬盘驱动器上的文件和文件夹。

Allowing us to write code in C++ and making it available to JavaScript makes it so we can add more features to JavaScript.

允许我们用C ++编写代码并将其提供给JavaScript使用,从而可以为JavaScript添加更多功能。

Node.js in itself is a C++ implementation of a V8 engine allowing server-side programming and networking applications.

Node.js本身是V8引擎的C ++实现,允许服务器端编程和联网应用程序。

Let’s now look at some of the open source code inside the engine. To do this, you need to go to the v8/samples/shell.cc folder.

现在让我们来看一下引擎内部的一些开源代码。 为此,您需要转到v8 / samples / shell.cc文件夹。

Here you can see the implementation of different functions such as Print and Read, which are natively not available in Node.js.

在这里,您可以看到不同功能的实现,例如“ Print和“ Read,这些功能本来在Node.js中不可用。

Below, you can see the implementation of the Print function. Whenever the print() function is invoked in Node.js, it will create a callback and the function will be executed.

在下面,您可以看到“ Print功能的实现。 每当在Node.js中调用print()函数时,它将创建一个回调并执行该函数。

Similarly, we can add our own implementation of different new functions in C++ inside V8 allowing it to be understood by Node.js.

同样,我们可以在V8中的C ++中添加我们自己的不同新功能的实现,以使Node.js能够理解它。

That is certainly too much to grab for a simple statement and that’s the amount of work V8 engine does under the hood.

对于一个简单的声明来说,肯定是太多了,这就是V8引擎在后台进行的工作量。

Now you must have a clear understanding of how Node.js works and what actually is the Chrome V8 engine.

现在,您必须清楚地了解Node.js的工作原理以及Chrome V8引擎的真正含义。

Thanks for reading this article. Let’s follow up on Twitter, Linkedin, Github, and Facebook.

感谢您阅读本文。 让我们跟进的TwitterLinkedInGithub上 Facebook的

翻译自: https://www.freecodecamp.org/news/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964/

chrome v8 引擎

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值