服务器之家:专注于服务器技术及软件下载分享
分类导航

服务器资讯|IT/互联网|云计算|区块链|软件资讯|操作系统|手机数码|百科知识|免费资源|头条新闻|

服务器之家 - 新闻资讯 - 软件资讯 - JavaScript 引擎 V8 发布 8.5 版本

JavaScript 引擎 V8 发布 8.5 版本

2020-07-24 21:46开源中国 软件资讯

JavaScript 引擎 V8 发布了 8.5 版本(测试阶段), 正式版本将在之后随 Chrome 85 一起推出 。8.5 版本带来了一些面向开发人员的特性,主要亮点包括: JavaScript Promise.any和AggregateError: Promise.any是一个 Promise 组合器,一旦输入的一个

JavaScript 引擎 V8 发布了 8.5 版本(测试阶段),正式版本将在之后随 Chrome 85 一起推出。8.5 版本带来了一些面向开发人员的特性,主要亮点包括:

JavaScript

 

  • Promise.any 和 AggregateError:

Promise.any 是一个 Promise 组合器,一旦输入的一个 Promise 满足,它就会解决所产生的 Promise。

  1. const promises = [ 
  2.   fetch('/endpoint-a').then(() => 'a'), 
  3.   fetch('/endpoint-b').then(() => 'b'), 
  4.   fetch('/endpoint-c').then(() => 'c'), 
  5. ]; 
  6. try { 
  7.   const first = await Promise.any(promises); 
  8.   // Any of the promises was fulfilled. 
  9.   console.log(first); 
  10.   // → e.g. 'b' 
  11. catch (error) { 
  12.   // All of the promises were rejected. 
  13.   console.assert(error instanceof AggregateError); 
  14.   // Log the rejection values: 
  15.   console.log(error.errors); 

如果所有输入的 Promise 都被拒绝,则所产生的 Promise 将被 AggregateError 对象拒绝,该对象包含一个 error 属性,该属性保存一个拒绝值数组。

  • String.prototype.replaceAll:

String.prototype.replaceAll 提供了一种简单的方法来替换所有出现的子字符串,而无需创建全局 RegExp。

  1. const queryString = 'q=query+string+parameters'
  2.  
  3. // Works, but requires escaping inside regular expressions. 
  4. queryString.replace(/+/g, ' '); 
  5. // → 'q=query string parameters' 
  6.  
  7. // Simpler! 
  8. queryString.replaceAll('+'' '); 
  9. // → 'q=query string parameters' 
  • Logical assignment operators(逻辑赋值运算符)

逻辑赋值运算符是新的复合赋值运算符,它将逻辑运算符 &&、 || 或 ?? 与任务组合在一起

  1. x &&= y; 
  2. // Roughly equivalent to x && (x = y) 
  3. x ||= y; 
  4. // Roughly equivalent to x || (x = y) 
  5. x ??= y; 
  6. // Roughly equivalent to x ?? (x = y) 

与数学和按位复合赋值运算符不同,逻辑赋值运算符仅有条件地执行赋值。

WebAssembly

 

  • 在所有平台上都提供了 Liftoff

从 V8 v6.9 开始,Liftoff 一直用作 Intel 平台上 WebAssembly 的基准编译器(Chrome 69 在桌面系统上启用了它)。由于担心内存的增加(基线编译器会生成更多代码),因此到目前为止一直将其保留在移动系统中。

经过最近几个月的试验,v8 团队发现大多数情况下的内存增加可以忽略不计,因此最终在所有架构上默认都启用了 Liftoff,从而提高了编译速度,尤其是在 ARM 设备(32 位和 64 位)上。

提供多值(Multi-value)支持

现在,WebAssembly 对多值代码块和函数返回的支持已可以普遍使用。这反映了该提案最近在正式的 WebAssembly 标准中的合并,并得到所有编译层的支持。

例如,现在这是一个有效的 WebAssembly 函数:

  1. (func $swap (param i32 i32) (result i32 i32) 
  2.   (local.get 1) (local.get 0) 

如果导出了该函数,则也可以从 JavaScript 对其进行调用,并返回一个数组:

  1. instance.exports.swap(1, 2); 
  2. // → [2, 1] 

相反,如果 JavaScript 函数返回数组(或任何迭代器),则可以将其导入并在 WebAssembly 模块内作为多返回函数调用:

  1. new WebAssembly.Instance(module, { 
  2.   imports: { 
  3.     swap: (x, y) => [y, x], 
  4.   }, 
  5. });(func $main (result i32 i32) 
  6.   i32.const 0 
  7.   i32.const 1 
  8.   call $swap 

更重要的是,工具链现在可以使用此功能在 WebAssembly 模块中生成更紧凑、更快的代码。

  • 支持 JS BigInts

已提供 WebAssembly 支持,用于将 WebAssembly I64 值与 JavaScript BigInts 相互转换,并且根据官方标准的最新更改,该支持可用于一般用途。

现在可以从 JavaScript 调用具有 i64 参数和返回值的 WebAssembly 函数,而不会造成精度损失:

  1. (module 
  2.   (func $add (param $x i64) (param $y i64) (result i64) 
  3.     local.get $x 
  4.     local.get $y 
  5.     i64.add) 
  6.   (export "add" (func $add))) 

从 JavaScript,只能将 BigInts 作为 I64 参数传递:

  1. WebAssembly.instantiateStreaming(fetch('i64.wasm')) 
  2.   .then(({ module, instance }) => { 
  3.     instance.exports.add(12n, 30n); 
  4.     // → 42n 
  5.     instance.exports.add(12, 30); 
  6.     // → TypeError: parameters are not of type BigInt 
  7.   }); 

V8 API

 

可使用 git log branch-heads/8.4..branch-heads/8.5 include/v8.h 来获取 API 更改列表。

更新说明:https://v8.dev/blog/v8-release-85

延伸 · 阅读

精彩推荐