

Github GitHub - Jack-Works/proposal-readonly-arraybuffer
source link: https://github.com/Jack-Works/proposal-readonly-arraybuffer/
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.

Freeze ArrayBuffer and Readonly view to ArrayBuffer
Status
Champion(s): Jack Works
Author(s): Jack Works
Stage: 0
Related proposal: Fixed view of ArrayBuffer
Presentations
Motivation
Security
We can have frozen objects (via Object.freeze
) but not for binary data today.
Performance
- Developers might copy the whole ArrayBuffer if they want to prevent external code to modify that ArrayBuffer. The copy brings the performance lost.
- Engines can safely share the memory across different Realms/processes if the ArrayBuffer is read-only.
Target
- Add a new way to freeze the ArrayBuffer.
- One-way. Once it froze, there is no way back.
- All views to the frozen ArrayBuffer are read-only too.
- If it is sent across Realms/processes, it is still frozen.
- Add a new way to create a read-only view to a read-write ArrayBuffer.
- Cannot construct the read-write view from
readOnlyView.buffer
- Cannot construct the read-write view from
Possible API design
// Mutate this, not copy & return a new one. ArrayBuffer.prototype.freeze(): ArrayBuffer ArrayBuffer.prototype.isFrozen(): boolean // One of the following: new TypedArray(buffer, { readonly: true }) new TypedArray(arrayBuffer.frozenView) TypedArray.prototype.freeze(), TypedArray.prototype.isFrozen()
Freeze ArrayBuffer
const buffer = new ArrayBuffer(8) const view = new Int32Array(buffer) view[0] = 42 // OK buffer.freeze() view[0] = 42 // TypeError buffer.isFrozen() // true
Readonly view to ArrayBuffer
const buffer = new ArrayBuffer(8) // This one? function createROInt32Array(buffer) { return new Int32Array(buffer, { readonly: true }) } // This one? function createROInt32Array(buffer) { const view = new Int32Array(buffer) view.freeze() return view } // Or this one? function createROInt32Array(buffer) { const view = new Int32Array(buffer.frozenView) // view.buffer === buffer.frozenView // view.buffer.buffer === view.buffer return view } const roView = createROInt32Array(buffer) const rwView = new Int32Array(buffer) roView[0] = 42 // TypeError rwView[0] = 42 // OK const tryEscape = roView.buffer const newView = new Int32Array(tryEscape) newView[0] = 42 // still TypeError roView.buffer !== rwView
Integrate with Tuple & Records?
const buffer = new ArrayBuffer(8) fillBuffer(buffer) buffer.freeze() const data = #{ binary: buffer // yay! }
Recommend
-
24
MS15-106 JScript ArrayBuffer.slice 任意地址读漏洞分析 小叮当...
-
36
During our trip aroundmanaged pointers andstructs, we get the last topic to discuss – readonly semantics . Thus, we touch today topics like readonly structs and readonly paremeters. Readon...
-
35
With the Roslyn update, this was the first time that C# received minor versions instead of only major ones. Roslyn helped the designers of the...
-
42
Do you want static or runtime immutability, or both? In this article I will show you how to combine Object.freeze with Readonly to get the best of both worlds. JavaScript’s Object.freeze JavaScript’s
-
6
聊聊JS的二进制家族:Blob、ArrayBuffer和Buffer广发证券 技术工程师前端较少涉及对二进制数据的处理,但即便如此,我们偶尔总能在角落里看见它们的身影,今天我们就来聊一聊JS的...
-
6
Convert base64 string to ArrayBuffer Join Stack Overflow to learn, share knowledge, and build your career.
-
6
This repo is merged with Freeze ArrayBuffer. Fixed view of ArrayBuffer Status Champion(s): Jack Wor...
-
16
by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=3725
-
8
V8 堆外内存 ArrayBuffer 垃圾回收的实现更新日期: 2022-04-29阅读量: 41标签: V8分...
-
7
Js二进制家族:文件base64、File、Blob、ArrayBuffer互转更新日期: 2022-12-09阅读: 45标签:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK