42

2019 年新开源的 JavaScript 引擎 QuickJS

 5 years ago
source link: https://www.tuicool.com/articles/QBBvI3E
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 代码:

EZzEza3.jpg!web

源码也不太多:

QfYVzm6.jpg!web

如果你对 JS 解释器比较感兴趣,可以看看源码,学习下 JS 是如何被执行的。最近在学习前端,看到这个项目比较好,推荐给大家,可以关注“素燕”这个公众号,一起学前端。QuickJS 文档也值得一读,超越技术公众号后台回复: qj ,或者直接前往 https://bellard.org/quickjs/quickjs.pdf 获得官方文档。

ryEjMvU.jpg!web

参考:

https://github.com/horhof/quickjs

https://bellard.org/quickjs/

推荐阅读:

开启我的前端之路

图解数据结构和算法

bYnYjiA.png!web


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK