What preparations do I need to write a JavaScript engine?

Why you want to write a JavaScript engine

As a programmer, the most romantic thing I can think of is to design a programming language and be used by other developers to solve practical problems.

Just like my idol Anders Hejlsberg, he is a great programmer and has designed many excellent programming languages, such as C# and TypeScript. In college, I used C# to explore the computing world, and now, I use TypeScript to do my work. Every time I use the languages ​​he designed, I can feel their elegance, power, efficiency and rigor, and they never disappoint!

Anders Hejlsberg

Implementing a programming language is a very difficult task with many challenges and difficulties to face.

Realizing a programming language that can be widely accepted by others needs to face more challenges and difficulties. It needs to consider the ease of use, performance, reliability, security, etc. of the language, as well as the needs and usage scenarios of users.

In addition, the realization of a programming language also needs to consider the construction and development of the language ecosystem, such as the development and maintenance of standard libraries, frameworks, tool chains, etc. This requires cooperation and communication with other developers and the community, and continuous improvement and improvement of the language ecosystem.

In general, implementing a programming language that is widely accepted by others takes a lot of effort and time, and requires extensive technical knowledge and development experience. But at the same time, it also requires a certain amount of luck and opportunity. For example, the choice of timing and market demand will have an impact on the success of the language.

As a front-end developer with 5 years of experience, implementing (or participating in implementing) a JavaScript engine is a good starting point, which can help you gain a deep understanding of how to design and implement a programming language. Gradually learn JavaScript language specifications and standards, as well as the implementation principles and optimization techniques of JavaScript engines. Familiar with related technologies such as compilers, interpreters, and garbage collectors.

What you need to know about writing a JavaScript engine

Syntax, Semantics, and Behavior of the JavaScript Language

Learn about JavaScript's basic syntax, concepts such as data types, control flow, and functions, as well as their semantics and behavior.

ECMAScript 是 JavaScript 语言的标准化规范,JavaScript 引擎需要遵循该规范来解析和执行 JavaScript 代码。因此,开发者需要深入理解 ECMAScript 规范,并熟悉其中的语法和特性。

ECMAScript 在这里:www.ecma-international.org/publication…

编译器和解释器的原理和技术

JavaScript 引擎可以通过编译器和解释器来执行 JavaScript 代码。因此,需要了解编译器和解释器的原理和技术,包括词法分析、语法分析、语义分析、代码生成等方面。

可以阅读编译原理三大经典《编译原理技术和工具》(龙书)、《现代编译原理-C语言描述》(虎书)、高级编译器设计与实现(鲸书)。

Compilation Principles Techniques and Tools

语言运行时和内存管理

JavaScript 引擎需要管理内存、处理异常等任务。因此,需要了解语言运行时和内存管理的原理和技术,包括堆、栈等方面。

JavaScript 是一种动态语言,它需要在运行时动态创建和销毁对象。因此,JavaScript 引擎需要实现一个垃圾回收器,用于自动回收不再使用的对象,防止内存泄漏和内存溢出。

可以阅读《垃圾回收的算法与实现》。

Algorithm and Implementation of Garbage Collection

性能优化和代码优化技术

JavaScript 引擎需要保证代码的执行效率,尤其是对于大规模、高并发的应用场景。因此,需要了解各种性能优化和代码优化技术,例如 JIT 编译、代码优化、并发处理等。

可以阅读《虚拟机》。

virtual machine

并发编程和线程安全

JavaScript 引擎需要支持并发编程,并保证线程安全。因此,需要了解并发编程和线程安全的原理和技术,包括锁、原子操作、内存模型等方面。

操作系统和硬件知识

JavaScript 引擎需要运行在操作系统和硬件之上,因此需要了解操作系统和硬件的知识,包括进程、线程、文件系统、网络等方面。

测试和调试技术

编写 JavaScript 引擎需要进行测试和调试,以确保它的正确性和可靠性。因此,需要了解各种测试和调试技术,例如单元测试、集成测试、性能测试、调试器等。

Test262 是一个完整的 ECMAScript 规范测试套件,由 ECMAScript 规范委员会维护。它包含了超过 20,000 个测试用例,覆盖了 ECMAScript 规范中的所有语言特性和场景。Test262 的测试用例是基于 ECMAScript 规范文档编写的,每个测试用例都包含了预期的结果和实际的结果,方便测试人员进行比对。

Test262 在这里:github.com/tc39/test26…

市面上已有的 JavaScript 引擎

  1. V8:V8 是由 Google 开发的 JavaScript 引擎,用于 Google Chrome 浏览器和 Node.js 运行时。V8 引擎使用了许多高效的技术,例如 JIT 编译、内联缓存、垃圾回收器等,具有出色的性能和稳定性。
  2. SpiderMonkey:SpiderMonkey 是由 Mozilla 开发的 JavaScript 引擎,用于 Firefox 浏览器和 Rhino 运行时。SpiderMonkey 引擎使用了许多高效的技术,例如 JIT 编译、分层解释器、垃圾回收器等,具有出色的性能和稳定性。
  3. JavaScriptCore:JavaScriptCore 是由苹果公司开发的 JavaScript 引擎,用于 Safari 浏览器和 WebKit 渲染引擎。JavaScriptCore 引擎使用了许多高效的技术,例如 JIT 编译、LLInt 解释器、垃圾回收器等,具有出色的性能和稳定性。
  4. Chakra:Chakra 是由微软公司开发的 JavaScript 引擎,用于 Edge 浏览器和 Node.js 运行时。Chakra 引擎使用了许多高效的技术,例如 JIT 编译、分层解释器、垃圾回收器等,具有出色的性能和稳定性。
  5. Rhino: Rhino is a Java-based JavaScript engine developed by Mozilla. The Rhino engine uses an interpreted execution method, has good cross-platform and flexibility, and can be used in embedded systems and Java applications.
  6. Duktape: Duktape is a lightweight JavaScript engine with fast startup and low memory footprint. The Duktape engine uses a stack machine, has flexible embedded features, and can be used in embedded systems and IoT devices.
  7. QuickJS: QuickJS is a small, efficient JavaScript engine developed by Fabrice Bellard. It is fast, lightweight, and embeddable, and is widely used in embedded systems and server-side applications.
  8. Boa: Boa is a Rust-based JavaScript engine developed by Mozilla. It uses JIT technology similar to V8, which can compile JavaScript code into fast native code, and supports multi-threaded concurrent compilation, which improves the overall performance.

Guess you like

Origin juejin.im/post/7255936307100123197