We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

SharedArrayBuffer 对象用来表示一个通用的,固定长度的原始二进制数据缓冲区,类似于 ArrayBuffer 对象,它们都可以用来在共享内存(shared memory)上创建视图。与 ArrayBuffer 不同的是,SharedArrayBuffer 不能被分离。

请注意,由于能够轻易地利用《边信道(side-channel)读取未授权内存的攻击技术》中提到的Spectre漏洞——一种利用现代 CPU 使用的执行优化功能的新攻击技术, SharedArrayBuffer 功能将在 Chrome 和 FireFox 的新版本中禁用,并将逐渐被所有浏览器禁用。

语法

new SharedArrayBuffer(length)

参数

length
所创建的数组缓冲区的大小,以字节(byte)为单位。

返回值

一个大小指定的新 SharedArrayBuffer 对象。其内容被初始化为 0。

描述

分配及共享内存

为了将一个SharedArrayBuffer 对象从一个用户代理共享到另一个用户代理(另一个页面的主进程或者当前页面的一个 worker )从而实现共享内存,我们需要运用 postMessage 和结构化克隆算法( structured cloning )。

结构化克隆算法接收被映射到一个新的 SharedArrayBuffers 对象上的 SharedArrayBuffers 对象与 TypedArrays 对象。在这两种映射下,这个新的 SharedArrayBuffer 对象会被传递到目标用户代理的接收函数上,导致在目标用户代理产生了一个新的私有 SharedArrayBuffer 对象(正如 ArrayBuffer 一样)。然而,这两个 SharedArrayBuffer 对象指向的共享数据块其实是同一个,并且在某一代理中的一个块的副作用将最终导致另一个代理具有可见性。

let sab = new SharedArrayBuffer(1024);
worker.postMessage(sab);

在早期版本的特性中,结构化克隆期间 SharedArrayBuffers 需要被精确转化。然而,从HTML的意义上来讲,SharedArrayBuffers 并非一个 Transferable object。因此,如果一个 SharedArrayBuffer 对象位于转化队列,postMessage 将会抛出一个 DataCloneError 或 一个 DOMException

var sab = new SharedArrayBuffer(1024);
worker.postMessage(sab, [sab]); 
// Uncaught DOMException: Failed to execute 'postMessage' on 'Worker': A SharedArrayBuffer could not be cloned.

通过原子操作更新及同步来共享内存

共享内存能被同时创建和更新于工作者线程或主线程。依赖于系统(CPU,操作系统,浏览器),变化传递给环境需要一段时间。需要通过 atomic 操作来进行同步。

接受 SharedArrayBuffer 对象的API

需要 new 运算符来构造

SharedArrayBuffer 需要 new 运算符来构造一个构造函数. 作为函数来调用一个 SharedArrayBuffer 构造函数时,如果不用 new 运算符,将会抛出一个 TypeError 异常。

var sab = SharedArrayBuffer(1024);
// TypeError: calling a builtin SharedArrayBuffer constructor 
// 必须用 new 来构造
var sab = new SharedArrayBuffer(1024);

属性

SharedArrayBuffer.length
SharedArrayBuffer 构造函数的 length 属性值为1。 
SharedArrayBuffer.prototype
允许所有 SharedArrayBuffer 对象的附加属性。

SharedArrayBuffer 原型对象

所有 SharedArrayBuffer 实例继承自 SharedArrayBuffer.prototype

属性

SharedArrayBuffer.prototype.constructor
Specifies the function that creates an object's prototype. The initial value is the standard built-in SharedArrayBuffer constructor.
SharedArrayBuffer.prototype.byteLength 只读
The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.

方法

SharedArrayBuffer.prototype.slice(begin, end)
Returns a new SharedArrayBuffer whose contents are a copy of this SharedArrayBuffer's bytes from begin, inclusive, up to end, exclusive. If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

规范

规范 状态 备注
ECMAScript Latest Draft (ECMA-262)
SharedArrayBuffer
Draft Initial definition in ES2017.
ECMAScript 2017 (ECMA-262)
SharedArrayBuffer
Standard  

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
SharedArrayBuffer in DataView601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
prototype601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
byteLength601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
slice601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support601601 ?

573 4

55 — 57

46 — 555

No No No
SharedArrayBuffer in DataView601601 ?

573 4

55 — 57

46 — 555

No No No
prototype601601 ?

573 4

55 — 57

46 — 555

No No No
byteLength601601 ?

573 4

55 — 57

46 — 555

No No No
slice601601 ?

573 4

55 — 57

46 — 555

No No No

1. Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

2. Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

3. Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).

4. From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

5. From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

相关链接

文档标签和贡献者

此页面的贡献者: xyy94813, AnnAngela, szengtal, xgqfrms-GitHub, winjeysong
最后编辑者: xyy94813,