A few days ago, it was the final examination phase, for college students’ examination, DDDD. However, when reviewing the principle of computer composition, I saw this sentence
Compilers are fully compiled and then run programs, such as C++,C, etc. An interpreter is a program that is translated sentence by sentence and executed as it is translated, such as JavaScript, Python, etc.
JavaScript is an interpreted language, and I’ve known that since I started learning JavaScript. But the latter part of the sentence made me confused, because some time ago I saw part of teacher Li Bing’s browser course, the teacher explained that the implementation mechanism of JavaScript is to compile first and then execute. This?
To the group of small partners expressed doubts, said that this is related to the underlying principle, some complex, curiosity driven, online surfing to find information, brave comb, if there is a mistake, hope to guide!
1. Computer system hierarchy
1.1 Language Level
Computer programming languages can be divided into three levels:
- Machine language: Due to hardware reasons, for example, a light bulb can only have two states: on and off. A computer can understand a complex system composed of many light bulbs, so the computer can only understand two states: 0 and 1. Machine instructions are represented by a string of binary numbers, for example
00011101
An instruction corresponds to a command that a computer can execute correctly. Machine language is a collection of machine instructions. - Assembly language: is a low-level symbolic language, the body of assembly is assembly instructions such as ADC(with carry addition),SUB(subtraction), assembly instructions can be considered as machine instructions mnemonic.
- High-level languages, such as JavaScript, Java, C, C++ and so on, are easy for people to understand. As soon as you see a key word in a language, you can roughly know what aspect it plays a role
Machine language is the only language that computers can directly recognize and execute. Every high-level language has to be translated into machine language before it can be executed by a computer, but there are two kinds of translations.
1.2 Compilation and Interpretation
Compilation is to translate the high-level language into object code (machine language) at once, and then execute it directly by the CPU. Each time the program is executed, only the object code is executed. As long as the source program remains unchanged, there is no need to recompile. Low cross-platform and high efficiency
Interpretation is to translate a high-level statement of the source program into object code (machine language), execute it immediately, and then translate the next statement and execute it. It is cross-platform and inefficient.
2. The JavaScript engine
2.1 The role of JavaScript engines
Early JavaScript engines directly interpreted JavaScript source code as unoptimized binary machine code, which caused two problems:
- The explanation takes a long time,
- Binary code takes up more memory
Modern JavaScript engines have come a long way as user experience requirements have increased and technology has improved.
2.2 Modern JavaScript engines
The Bytecode scheme of the Java language is well recognized in the industry, and other languages are well used, including our JavaScript.
2.2.1 Java bytecode
Java uses virtual machine technology to support cross-platform features, a set of code can run on multiple systems.
2.2.2 JavaScriptCore engine
The JavaScriptCore engine is the default engine in WebKit, using the two main WebKit browsers Sfari and Chromium (Chorme open source projects).
2.2.3 v8 engine
V8, written in c++ for Chrome and node.js, compiles/executes JavaScript code.
JavaScript engine related knowledge, a little complex, later have the opportunity to learn summary.
Each engine optimizes JavaScript slightly differently, but most now incorporate bytecode and JIT technology
When a method or block of Code is found to be running particularly frequently, it is considered “Hot Spot Code.” The JIT then compiles and optimizes some of the “hot code” to local machine-specific machine code, and caches the compiled machine code for future use. Improve execution speed
So what does a JavaScript engine do? The code is optimized to make interpretation execution more efficient, and ultimately converted to machine code, which is executed by the CPU.
3 Problem Solving
Having said a lot (and probably a lot), what exactly does JavaScript execution look like? Compile before execute?
When a piece of JavaScript code runs, it can be roughly divided into three steps:
-
Lexical analysis and syntax analysis (in this case, generate AST abstract syntax tree)
-
Precompile (the precompile here is li Bing teacher said the first compile after running in the compile, here will be promoted by variables, the execution context. Lexical environment and other related knowledge, summarized in the previous blog)
-
Interpreted execution (To stick with the idea that high-level languages, no matter what they go through, eventually translate into machine language, which is executed by the CPU, so JavaScript is classified as interpreted because it is precompiled to be interpreted as it is executed)
Lexical analysis: Divide a statement into various types of code blocks (lexical units). For example, var a = 1 can be broken down into var, a, =, 1. The scattered lexical units form a lexical unit stream.
Parsing: Lexical unit streams are parsed into an AST(abstract syntax tree), as follows
{
"type": "Program"."body": [{"type": "VariableDeclaration"."declarations": [{"type": "VariableDeclarator"."id": {
"type": "Identifier"."name": "a"
},
"init": {
"type": "Literal"."value": 1."raw": "1"}}]."kind": "var"}]."sourceType": "script"
}
Copy the code
Reference:
Understand Javascript execution