@exodus/patch-broken-hermes-typed-arrays
修复损坏的 Hermes 引擎 Typed Array 执行
简而言之:
import '@exodus/patch-broken-hermes-typed-arrays'
这是什么?
这一问题背后的问题:
> Buffer.alloc(10).subarray(0).toString('hex')
'0,0,0,0,0,0,0,0,0,0'
// What?
你可能倾向于固定投出物的具体位置(例如:
Buffer.from但这是个错误.
最重要的是: 很难追踪到这些。 。 。 .
还有.., Buffer.from(arg) 是一份副本,是无效的,
Buffer.from(x.buffer, x.byteOffset, x.byteLength) 尴尬,容易犯人类错误,而且可以
启动安全检查 buffer.buffer 手动( 这是不安全的)
实践,例如错误类型 x.byteLegnth 可以通过曝光向其他用户泄露密码/秘密
无关的应用程序内存). 你不想在你的密码库里复制一吨的图案.
把这个修好 Buffer (例如用猴子来打它)也是不够的——你的密码库可以
包括捆绑的多个依赖关系 缓冲,还有所有
那些事件不会那样解决的!
此外,这影响到的不仅仅是 Buffer 超过 subarray - Hermes引擎没有执行
已键入阵列 对.
即赫尔姆斯 TypedArray 不遵循这些规格:
- ECMAScript 语言规格,
%TypedArray%.prototype.subarray - ECMAScript 语言规格,
%TypedArray%.prototype.map - ECMAScript 语言规格,
%TypedArray%.prototype.filter - ECMAScript 语言规格,
%TypedArray%.prototype.slice
修补
总的来说,这个模块仅仅是以下片段的被颂扬的版本(但带有 .map编号 :.filter
对Hermes的更新提供支持和保障,以发现情况是否发生变化.
TypedArray.prototype.subarray = function (...args) {
var arr = subarray.apply(this, args)
if (!this.constructor || arr.constructor === this.constructor) return arr
return new this.constructor(arr.buffer, arr.byteOffset, arr.length)
}
注:如果赫尔墨斯执行可调整范围,上述版本可能不符合规格 TypedArray编号
从ECMAScript 2024, 或例如开始支持 Symbol.Species
上面的片段也没有赫尔墨斯的检查,所以如果选中的话,它也会盲目补丁阵列 切换到 JavaScript Core
这就是为什么这个模块的存在——它处理所有
问题的细节
例如与 缓冲数字 :
// ... import Buffer polyfill from https://npmjs.com/package/buffer
// ... and a console.log polyfill
console.log(Buffer.alloc(2).subarray(0, 1).constructor.name)
console.log(Buffer.alloc(2).map(() => 1).constructor.name)
console.log(Buffer.alloc(2).filter(() => true).constructor.name)
% node buftest.0.js
Buffer
Buffer
Buffer
% jsc buftest.0.js
Buffer
Buffer
Buffer
% hermes buftest.0.js
Uint8Array
Uint8Array
Uint8Array
这不限于 buffer
// Let's assume this will get transpiled for a demo, Hermes has no `class` support
class TestArray extends Uint16Array {
static instances = 0
constructor(...args) {
super(...args)
// console.log('We can do something here!')
TestArray.instances++ // count constructor calls
return this
}
hello() {
return 'hi there'
}
}
var arr = new TestArray(10)
// TestArray.instances: 1
var mapped = arr.map((_, i) => i * 10)
// TestArray.instances: 2
console.log(TestArray.instances) // 2 everywhere, but 1 in Hermes
console.log(mapped.constructor.name) // 'TestArray' everywhere, but 'Uint16Array' in Hermes
console.log(mapped.hello()) // throws in Hermes