Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重学js —— 结构化数据之SharedArrayBuffer对象 #123

Open
lizhongzhen11 opened this issue Jun 28, 2020 · 0 comments
Open

重学js —— 结构化数据之SharedArrayBuffer对象 #123

lizhongzhen11 opened this issue Jun 28, 2020 · 0 comments
Labels
js基础 Good for newcomers 重学js 重学js系列 规范+MDN

Comments

@lizhongzhen11
Copy link
Owner

lizhongzhen11 commented Jun 28, 2020

SharedArrayBuffer 对象

SharedArrayBuffer 构造器

  • 即固有对象 %SharedArrayBuffer%
  • 全局对象 "SharedArrayBuffer" 属性的初始值
  • ...

SharedArrayBuffer ( [ length ] )

  1. 如果 NewTargetundefined,抛 TypeError 异常
  2. 定义 byteLength? ToIndex(length)
  3. 返回 ? AllocateSharedArrayBuffer(NewTarget, byteLength)

SharedArrayBuffer 构造器上的属性

SharedArrayBuffer 原型对象上的属性

  • 即固有对象 %SharedArrayBufferPrototype%
  • [[Prototype]] 内置插槽其值为 %Object.prototype%
  • 属于 普通对象
  • 没有 [[ArrayBufferData]][[ArrayBufferByteLength]] 内置插槽

get SharedArrayBuffer.prototype.byteLength

  1. 定义 Othis
  2. 执行 ? RequireInternalSlot(O, [[ArrayBufferData]])
  3. 如果 O 不是 SharedArrayBuffer,抛 TypeError 异常
  4. 定义 lengthO.[[ArrayBufferByteLength]]
  5. 返回 length

SharedArrayBuffer.prototype.constructor

初始值为 %SharedArrayBuffer%

SharedArrayBuffer.prototype.slice ( start, end )

整体步骤与 ArrayBuffer.prototype.slice ( start, end ) 相差不大。

  1. 定义 Othis
  2. 执行 ? RequireInternalSlot(O, [[ArrayBufferData]])
  3. 如果 O 不是 SharedArrayBuffer,抛 TypeError 异常
  4. 定义 lenO.[[ArrayBufferByteLength]]
  5. 定义 relativeStart? ToInteger(start)
  6. 如果 relativeStart < 0,定义 firstmax((len + relativeStart), 0);否则,定义 firstmin(relativeStart, len)
  7. 如果 endundefined,定义 relativeEndlen;否则,定义 relativeEnd? ToInteger(end)
  8. 如果 relativeEnd < 0,定义 finalmax((len + relativeEnd), 0);否则,定义 finalmin(relativeEnd, len)
  9. 定义 newLenmax(final - first, 0)
  10. 定义 ctor? SpeciesConstructor(O, %SharedArrayBuffer%)
  11. 定义 new? Construct(ctor, « newLen »)
  12. 执行 ? RequireInternalSlot(new, [[ArrayBufferData]])
  13. 如果 new 不是 SharedArrayBuffer,抛 TypeError 异常
  14. 如果 new.[[ArrayBufferData]]O.[[ArrayBufferData]] 是相同的 Shared Data Block 值,抛 TypeError 异常
  15. 如果 new.[[ArrayBufferByteLength]] < newLen,抛 TypeError 异常
  16. 定义 fromBufO.[[ArrayBufferData]]
  17. 定义 toBufnew.[[ArrayBufferData]]
  18. 执行 CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen)
  19. 返回 new

和 ArrayBuffer 有什么区别?

根据MDN所说,SharedArrayBuffer 不能被分离。

这块我并不懂,可以看 「译」图解 ArrayBuffers 和 SharedArrayBuffers 了解下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js基础 Good for newcomers 重学js 重学js系列 规范+MDN
Projects
None yet
Development

No branches or pull requests

1 participant