百道网
 您现在的位置:Fun书 > 深入浅出WebAssembly
深入浅出WebAssembly


深入浅出WebAssembly

作  者:于航

出 版 社:电子工业出版社

出版时间:2018年11月

定  价:128.00

I S B N :9787121352171

所属分类: 专业科技  >  计算机/网络  >  行业软件及应用    

标  签:

[查看微博评论]

分享到:

TOP好评推荐   [展开]

TOP内容简介

WebAssembly是一种新的二进制格式,它可以方便地将C/C++等静态语言的代码快速地“运行”在浏览器中,这一特性为前端密集计算场景提供了无限可能。不仅如此,通过WebAssembly技术,我们还可以将基于Unity等游戏引擎开发的大型游戏快速地移植到Web端。WebAssembly技术现在已经被计划设计成W3C的标准,众多浏览器厂商已经提供了对其MVP版本标准的支持。在Google I/O 2017大会上,Google首次针对WebAssembly技术进行了公开演讲和推广,其Post-MVP版本标准更是对诸如DOM操作、多线程和GC等特性提供了支持。WebAssembly所带来的Web技术变革势不可挡。

《深入浅出WebAssembly》力求从一些简单的实践入手,深入理论,到复杂的具有实际业务价值的综合实践,深入浅出地介绍Wasm技术发展至今,其背后所涉及的各种底层设计原理与实现、相关工具链以及未来发展方向等多方面内容。本书内容包括:WebAssembly技术的发展历程,从PNaCl到ASM.js再到WebAssembly,以及这些技术的基本应用方法与性能对比;WebAssembly的标准上层API、底层堆栈机的设计原理,以及对MVP标准理论的深入解读;与WebAssembly标准相关的进阶内容,如单指令多数据流(SIMD)、动态链接(DL)等;LLVM工具链与WAT可读文本格式的相关内容;基于Emscripten工具链开发WebAssembly应用的基本流程,以及工具链的一些基本常用功能和特性;基于Emscripten工具链实现C/C++语言动态关系绑定技术;Emscripten工具链所提供的一些如WebGL支持、虚拟文件系统、应用优化以及HTML 5事件系统等高级应用特性;构建一个具有实际业务价值的WebAssembly应用,现阶段Wasm生态的发展情况,以及在Post-MVP标准中制订的一些WebAssembly未来发展规划。

《深入浅出WebAssembly》的目标读者为Web前端开发人员、C/C++开发人员和对WebAssembly技术感兴趣的人员。

 

TOP作者简介

于航,现就职于阿里巴巴 / 饿了么事业部(BU),资深前端工程师,FreeCodeCamp(FCC) China 上海社区负责人,QCon(2017)上海前端专场讲师。2016 年开始研究 WebAssembly 技术,2017 年加入 WebAssembly 中国社区,同年加入官方WCG(W3C Community Group),定期参与 WCG组织的各种线上视频研讨会议,在跟进 WebAssembly 新发展的同时,也为 WebAssembly 的标准化提出自己的建议和意见。生活中喜欢弹钢琴、演讲与分享。主要技术研究方向为Web前端与基础设施架构、WebAssembly、LLVM以及编译器等相关领域。

TOP目录

第1章  漫谈WebAssembly发展史  1

1.1  JavaScript的发展和弊端  1

1.1.1  快速发展与基准测试  1

1.1.2  Web新时代与不断挑战  8

1.1.3  无法跨越的“阻碍”  11

1.1.4  Chrome V8引擎链路  17

1.2  曾经尝试——ASM.js与PNaCl  28

1.2.1  失落的ASM.js  28

1.2.2  古老的NaCl与PNaCl  42

1.3  新的可能——WebAssembly  57

1.3.1  改变与颠覆  57

1.3.2  一路向前,WCG与WWG  85

第2章  WebAssembly核心原理(基于MVP标准)  90

2.1  应用与标准Web接口  90

2.1.1  编译与初始化  90

2.1.2  验证模块  106

2.1.3  遇到错误  106

2.1.4  内存分配  108

2.1.5  表  112

2.2  深入设计模型——堆栈机  118

2.2.1  堆栈式虚拟机  119

2.2.2  逆波兰表达式  125

2.2.3  Shunting-yard算法  126

2.2.4  标签与跳转  130

2.2.5  条件语句  135

2.2.6  子程序调用  137

2.2.7  变量  138

2.2.8  栈帧  139

2.2.9  堆  140

2.3  类型检查  141

2.3.1  数据指令类型  142

2.3.2  基本流程控制  144

2.3.3  基于表达式的控制流  149

2.3.4  类型堆栈的一致性  151

2.3.5  不可达代码  155

2.4  二进制编码  156

2.4.1  字节序——大端模式与小端模式  157

2.4.2  基于LEB-128的整数编码  161

2.4.3  基于IEEE-754—2008的浮点数编码  164

2.4.4  基于UTF-8的字符串编码  167

2.4.5  模块数据类型  168

2.4.6  虚拟指令与编码  169

2.4.7  类型构造符  174

2.5  模块  175

2.5.1  段  175

2.5.2  索引空间  185

2.5.3  二进制原型结构  186

2.6  内存结构  196

2.6.1  操作运算符  197

2.6.2  寻址  197

2.6.3  对齐  198

2.6.4  溢出与调整  202

第3章  动态链接与SIMD(基于MVP标准)  204

3.1  动态链接(Dynamic Linking)  204

3.1.1  ELF  206

3.1.2  重定向(Relocation)  212

3.1.3  GOT(Global Offset Table,全局偏移表)  225

3.1.4  PLT(Procedure Lookup Table,过程查询表)  229

3.1.5  基于表的Wasm模块动态链接  233

3.2  单指令多数据流(SIMD)  236

3.2.1  SIMD应用  238

3.2.2  并行与并发  243

3.2.3  费林分类法  244

3.2.4  SIMD.js & TC39  246

3.2.5  WebAssembly上的SIMD扩展  248

第4章  深入LLVM与WAT  250

4.1  LLVM——底层虚拟机  250

4.1.1  传统的编译器架构  251

4.1.2  LLVM中间表示层  252

4.1.3  基于LLVM的编译器架构  254

4.1.4  LLVM优化策略  256

4.1.5  LLVM命令行工具  261

4.1.6  WebAssembly与LLVM  267

4.2  基于LLVM开发编程语言  272

4.2.1  图灵完备与DSL  276

4.2.2  简易词法分析器  280

4.2.3  RDP与OPP算法  287

4.2.4  AST  295

4.2.5  简易语法分析器  296

4.2.6  生成LLVM-IR代码  303

4.2.7  链接优化器  307

4.2.8  编译到目标代码  308

4.2.9  整合I/O交互层  312

4.3  WAT  315

4.3.1  S-表达式  317

4.3.2  WAT/Wasm与Binary-AST  319

4.3.3  其他与设计原则  320

第5章  Emscripten基础应用  322

5.1  利器——Emscripten工具链  322

5.1.1  Emscripten发展历史  322

5.1.2  Emscripten组成结构  324

5.1.3  Emscripten下载、安装与配置  326

5.1.4  运行测试套件  330

5.1.5  编译到ASM.js  331

5.2  连接C/C++与WebAssembly  333

5.2.1  构建类型  333

5.2.2  Emscripten运行时环境  342

5.2.3  在JavaScript代码中调用C/C++函数  351

5.2.4  在C/C++代码中调用JavaScript函数  362

第6章  基于Emscripten的语言关系绑定  382

6.1  基于Embind实现关系绑定  384

6.1.1  简单类  389

6.1.2  数组与对象类型  391

6.1.3  高级类元素  393

6.1.4  重载函数  407

6.1.5  枚举类型  408

6.1.6  基本类型  409

6.1.7  容器类型  411

6.1.8  转译JavaScript代码  413

6.1.9  内存视图  416

6.2  基于WebIDL实现关系绑定  417

6.2.1  指针、引用和值类型  420

6.2.2  类成员变量  422

6.2.3  常量“const”关键字  423

6.2.4  命名空间  424

6.2.5  运算符重载  425

6.2.6  枚举类型  426

6.2.7  接口类  429

6.2.8  原始指针、空指针与void指针  430

6.2.9  默认类型转换  433

第7章  探索Emscripten高级特性  436

7.1  加入优化流程  436

7.1.1  使用编译器代码优化策略  441

7.1.2  使用GCC压缩代码  443

7.1.3  使用IndexedDB缓存模块对象  445

7.1.4  其他优化参数  452

7.2  使用标准库与文件系统  453

7.2.1  使用基于musl和libc++的标准库  454

7.2.2  虚拟文件系统结构  457

7.2.3  打包初始化文件  458

7.2.4  基本文件系统操作  460

7.2.5  懒加载  469

7.2.6  Fetch API  472

7.3  处理浏览器事件  477

7.3.1  事件注册函数  479

7.3.2  事件回调函数  479

7.3.3  通用类型与返回值类型  481

7.3.4  常用事件  482

7.4  基于EGL、OpenGL、SDL和OpenAL的多媒体处理  485

7.4.1  使用EGL与OpenGL处理图形  486

7.4.2  使用SDL处理图形  492

7.4.3  使用OpenAL处理音频  495

7.5  调试WebAssembly应用  497

7.5.1  编译器的调试信息  498

7.5.2  使用调试模式  500

7.5.3  手动跟踪  501

7.5.4  其他常用编译器调试选项  503

第8章  WebAssembly综合实践、发展与未来  504

8.1  DIP综合实践应用  504

8.1.1  应用描述  504

8.1.2  滤镜与卷积  505

8.1.3  基本组件类型与架构  509

8.1.4  编写基本页面骨架(HTML与CSS)  510

8.1.5  编写核心卷积函数(C++)  511

8.1.6  编写主渲染循环与“胶水”代码(JavaScript)  513

8.1.7  使用Emscripten编译并运行应用  518

8.1.8  性能对比  519

8.2  WebAssembly常用工具集  520

8.2.1  Cheerp  520

8.2.2  Webpack 4  522

8.2.3  Go和Rust的WebAssembly实践  525

8.2.4  Binaryen  527

8.2.5  WasmFiddle  528

8.2.6  Wabt  529

8.2.7  AssemblyScript  529

8.3  WebAssembly未来草案  529

8.3.1  GC(垃圾回收)  530

8.3.2  Multi-Thread(多线程)与原子操作  530

8.3.3  异常处理  530

8.3.4  多返回值扩展  530

8.3.5  ES模块  530

8.3.6  尾递归  531

8.3.7  BigInts的双向支持  531

8.3.8  自定义注释语法  531

 

TOP书摘

序言(一)

I’m very excited to see this book, which covers in great detail a wide range of topics regarding WebAssembly. At this point in time WebAssembly is around one year old - if we count from when it shipped in all major browsers - so it’s still fairly young, and the industry is just starting to figure out how revolutionary it is going to be. The potential is there for huge impact, and good documentation is necessary for that.

Why is WebAssembly’s potential impact so large? For several reasons:

WebAssembly helps make the Web fast: WebAssembly is designed for small download size, fast startup, and predictably fast execution. The improvement compared to JavaScript can be very significant, over 2x in many cases, and especially in startup, where the speedup can be 10x.

WebAssembly makes the Web competitive with native: WebAssembly is designed as a compiler target for multiple languages. That includes C and C++, and in many areas of software the best implementations are in those languages, for example, game engines like Unity and Unreal, design software like AutoCAD, etc. It would take many years to write comparable products in JavaScript; instead, by compiling them to WebAssembly, the Web can be on par with native platforms today.

WebAssembly also fills an industry need outside the Web: WebAssembly is fast, portable, sandboxed, has multiple excellent open source implementations, and just like the Web itself it is an industry standard expected to be supported for the long term. As a result, it’s not surprising that WebAssembly is starting to be used outside of browsers, for example in the blockchain and content delivery network (CDN) spaces.

Looking back, it’s remarkable that our industry has gotten to this point. Just a few years ago, there was no cross-browser collaboration on getting native code to run on the Web. Instead, there were multiple options, including Native Client, Adobe Alchemy, and ASM.js, each with its own advantages and disadvantages. I believe it was the momentum of ASM.js that got the industry to focus on fixing things: ASM.js started out in Firefox, and by virtue of being a subset of JavaScript it immediately ran in all browsers - just not as efficiently. That led top companies in the video game industry and elsewhere to adopt ASM.js, together with Emscripten, the open source compiler to JavaScript that I started in 2010, and which could emit ASM.js. That adoption led to ASM.js support in Edge and later Chrome, at which point there was consensus that the industry should produce a proper standard in this space, which turned into WebAssembly. As the spec was designed and implementations started to appear, we added WebAssembly support to Emscripten, which allowed people to compile to both ASM.js and WebAssembly by just flipping a switch, making it easy for people to use the new technology. Finally, as of May 2018 Emscripten emits WebAssembly by default, and today WebAssembly has robust and stable support both in all major browsers and in the toolchain projects that emit it.

It’s been a complicated path to get here, but the future looks bright. It is especially worth noting that WebAssembly is expected to add features like multithreading, SIMD, GC, and others, which will open up even more interesting opportunities.

Alon Zakai

Alon is a researcher at Mozilla, where he works on compile-to-Web technologies. Alon co-created WebAssembly and ASM.js, and created the Emscripten and Binaryen open source projects which are part of the primary WebAssembly compiler toolchain.

译文:

我很高兴能够看到这本书的出版,作者在书中详细地介绍了有关WebAssembly的各种主题。在这本书即将出版之际,WebAssembly差不多一岁了——如果从所有主流的Web浏览器开始支持WebAssembly算起,那么这项技术仍然相当年轻,业界也才刚刚开始意识到它将多么具有革命性。WebAssembly所拥有的潜力将会在未来对IT行业产生巨大的影响,但在此之前,我们需要有优秀的文档。

为什么WebAssembly的潜在影响力会如此之大?有以下几个原因。

WebAssembly让Web应用运行更快。WebAssembly是一种新的格式,文件体积更小,启动速度更快,运行速度也更快。与使用JavaScript构建的Web应用相比,性能提升非常明显。在大部分情况下,运行速度提升两倍以上,特别是在启动速度方面,速度提升可以达到10倍。

WebAssembly让Web应用能够与原生应用展开竞争。WebAssembly是多种编程语言的编译器目标,包括C和C++。基于这些编程语言实现的优秀软件,如游戏引擎Unity、Unreal,设计软件AutoCAD等,如果使用JavaScript开发在功能上与这些软件旗鼓相当的产品可能需要很多年时间。但如果将它们编译成WebAssembly,这些原生应用就可以直接运行在Web平台上。因此,Web能够与原生平台相提并论。

WebAssembly还在Web领域之外为行业带来了其他可能性。WebAssembly运行速度快、可移植,提供了沙箱机制,并拥有众多优秀的开源实现,就像Web本身一样,它将会是一个被长期支持的行业标准。因此,WebAssembly开始被应用在Web浏览器之外的领域也就不足为奇了,例如区块链和内容分发网络(CDN)。

回首过去,我们的行业能够取得如此的成就已经很了不起了。几年前,还没有人去进行这种跨浏览器协作,以便让原生代码运行在Web平台上。不过有很多不同的项目,如Native Client、Adobe Alchemy和ASM.js,它们都在尝试做同样的事情,只是每个项目都有各自的优缺点。而我认为,是ASM.js的出现让业界开始专注于解决这个问题——ASM.js最初出现在Firefox中,由于它是JavaScript的一个子集,因此可以无缝地运行在所有浏览器中,但运行效率不高。视频游戏等行业的一些顶级的公司开始尝试使用ASM.js和Emscripten(我在2010年开源的编译器工具链,可以将代码编译成ASM.js)。由于在这些领域的广泛应用,Edge以及后来的Chrome均开始支持ASM.js。此时,人们一致认为这个领域需要一个行业标准,于是WebAssembly出现了。

随着规范设计和实现的不断演进,我们在Emscripten中加入了WebAssembly支持——只需要在编译命令中加入一个“开关”,便可选择性地将编译目标设置为ASM.js或WebAssembly,从而可以更轻松地使用这项新技术。截至2018年5月,Emscripten已经将默认的编译目标类型改为WebAssembly。今天,WebAssembly已经在所有主流浏览器和工具链项目中得到了强大而稳定的支持。

一路走来历经坎坷,但未来是光明的。特别值得注意的是,WebAssembly将会在未来添加多线程、SIMD、GC等功能,而这些新特性将会为我们带来更多有趣的可能性。

——Alon Zakai(Alon是Mozilla的研究员,从事与“编译到Web平台”相关的研究工作。Alon参与制定了WebAssembly和ASM.js标准,并创建了Emscripten和Binaryen等开源项目,这些项目都是WebAssembly编译器工具链的重要组成部分)

 

序言(二)

前端的可玩性变得越来越高,也越来越开放了。现如今,我们不仅仅能够使用HTML、CSS及Javascript来编写各种跨端的应用程序,WebAssembly的出现还让我们能够以极小的成本来复用其他领域已存在的成果,以此来弥补JavaScript在其性能与功能上的不足。

我第一次了解到WebAssembly是在2017年年初,当时沉迷于想自己制作一个基于Node.js环境和树莓派的语音助手。可惜对于语音处理这个领域来说,JavaScript还是一个“新人”,大量成熟的实现成果主要集中在C/C++领域。因此,对于当时对Node.js扩展及C/C++了解甚少的我来说,这是难度颇大的一个门槛。后来通过Twitter我了解到WebAssembly的前身是ASM.js,于是我立即尝试使用Emscripten将Google Assistant的Linux SDK编译为ASM.js,并顺利地在Node.js环境中进行了调用,那份喜悦我记忆犹新,同时这也极大地提升了我对这项技术的信心和好感。之后在全民直播的技术提升项目中,我与另一位研发人员有幸一起对最核心的播放器组件编解码和弹幕协议加密部分进行WebAssembly化,并成功上线且获得了极大的性能提升。在此之后,我坚信WebAseembly在未来一定会大有可为。

由于WebAssembly是一项极新的技术,因此在最初学习WebAssembly的过程中常常觉得知识零碎且不成体系,经常会出现浮沙驻高塔的情形,感觉入门十分困难。好在本书的出版,让这种情形不再复现。这本书的好处就是它系统详细地讲述了WebAssembly的方方面面,由浅入深地构建了整个WebAssembly的知识体系。不管你是刚接触WebAssembly的新人,还是已经在工作场景中使用WebAssembly的“老鸟”,通过阅读这本书都能够得到极大的提升。总之,如果你想了解WebAssembly,或者想补足相关的知识体系,它都是一本不可多得的案头好书。风雨欲来,如果现在还不进行WebAssembly的技术储备,更待何时?

最后,我要感谢于航让我第一时间读到如此精彩的作品,同时也感谢他对WebAssembly在国内的布道普及所做的工作,我相信WebAssembly的未来一定会更加美好,Web的未来也会更加开放和美好。

——赵洋(赵洋是“全民直播”的前端研发经理,曾经主导全民直播播放器编解码核心模块及弹幕协议加密过程WebAssembly化)

TOP 其它信息

装  帧:平装

页  数:548

开  本:16

加载页面用时:145.9843