QuickJS 2019 年 7 月 9 日 Fabrice Bellard 开源了一款 JavaScript 引擎, 使用 MIT 协议. Fabrice Bellard 是一位计算机奇才, 著名的多媒体播放器 FFMPEG 就是由他发起, 一生中开源了很多项目, 比如 QEMU 等.
QuickJS 是一款小巧的可嵌入 JavaScript 引擎, 它支持 ES2019 规范, 包括模块, 异步生成器和代理器. 主要特征有:
- 1
- .Small and easily embeddable: just a few C files, no external dependency, 190 KiB of x86 code for a simple hello world program.
简单而方便地嵌入, 只需几个 C 文件即可搞定, 无需其它额外的依赖, 一个简单 hello world 程序, 运行在 x86 机器上只需 190 kiB;
2 .Fast interpreter with very low startup time: runs the 56000 tests of the ECMAScript Test Suite in about 100 seconds on a single core of a desktop PC. The complete life cycle of a runtime instance completes in Less than 300 microseconds.
启动速度极快的解释器.
3 .Almost complete ES2019 support including modules, asynchronous generators and full Annex B support (legacy web compatibility).
几乎支持 ES2019 中所有的特性, 包括模块, 异步生成器和完整的 Annex B 支持 (传统 Web 兼容性).
4
.Passes 100% of the ECMAScript Test Suite.
通过 100% 的 ECMAScript Test Suite.
5
.Can compile JavaScript sources to executables with no external dependency.
可将 JavaScript 源代码编译成无需外部依赖的可执行文件.
- 6
- .Garbage collection using reference counting (to reduce memory usage and have deterministic behavior) with cycle removal.
使用引用计数做垃圾回收和循环删除.
7
.Mathematical extensions: BigInt, BigFloat, operator overloading, bigint mode, math mode.
支持数学扩展运算, 比如大整数, 大浮点数, 运算符重载, bigint 模式和数学模式.
8
.Command line interpreter with contextual colorization implemented in JavaScript.
用 JavaScript 实现了带有上下文着色的命令行解释器.
- 9
- .Small built-in standard library with C library wrappers.
带有使用 C 语言库包装的小型内置标准库 .
使 用 QuickJS 执行一段 JS 代码 (代码保存在一个 hello.JS 文件中):
- var lefe = function log(){
- console.log("超越技术公众号, 关注一下吧, 数据结构和算法, 计算机通用技术")
- }
- lefe()
- function sum(a, b){
- return a + b
- }
- console.log(sum(3, 6))
用 QuickJS 执行这段代码需要安装 QuickJS, 在 https://github.com/horhof/quickjs 上下载代码, 执行 make install 后即可. 安装完后执行, 使用命令行执行上面的 JS 代码:
quickjs ./qjs /Users/lefe/Desktop/hello.JS
超越技术公众号, 关注一下吧, 数据结构和算法, 计算机通用技术
9
也可以在线上执行 JS 代码:
源码也不太多:
如果你对 JS 解释器比较感兴趣, 可以看看源码, 学习下 JS 是如何被执行的. 最近在学习前端, 看到这个项目比较好, 推荐给大家, 可以关注 "素燕" 这个公众号, 一起学前端. QuickJS 文档也值得一读, 超越技术公众号后台回复: qj , 或者直接前往 https://bellard.org/quickjs/quickjs.pdf 获得官方文档.
参考:
- https://github.com/horhof/quickjs
- https://bellard.org/quickjs/
来源: http://www.tuicool.com/articles/QBBvI3E