-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
v8: serialize BigInt64Array and BigUint64Array #43571
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,8 @@ | |
|
||
const { | ||
Array, | ||
ArrayBuffer, | ||
ArrayPrototypeForEach, | ||
ArrayPrototypePush, | ||
BigInt64Array, | ||
BigUint64Array, | ||
DataView, | ||
Error, | ||
Float32Array, | ||
|
@@ -27,7 +26,6 @@ const { | |
Int32Array, | ||
Int8Array, | ||
ObjectPrototypeToString, | ||
SafeMap, | ||
Uint16Array, | ||
Uint32Array, | ||
Uint8Array, | ||
|
@@ -247,29 +245,40 @@ Deserializer.prototype.readRawBytes = function readRawBytes(length) { | |
length); | ||
}; | ||
|
||
/* Keep track of how to handle different ArrayBufferViews. | ||
* The default Serializer for Node does not use the V8 methods for serializing | ||
* those objects because Node's `Buffer` objects use pooled allocation in many | ||
* cases, and their underlying `ArrayBuffer`s would show up in the | ||
* serialization. Because a) those may contain sensitive data and the user | ||
* may not be aware of that and b) they are often much larger than the `Buffer` | ||
* itself, custom serialization is applied. */ | ||
const arrayBufferViewTypes = [Int8Array, Uint8Array, Uint8ClampedArray, | ||
Int16Array, Uint16Array, Int32Array, Uint32Array, | ||
Float32Array, Float64Array, DataView]; | ||
|
||
const arrayBufferViewTypeToIndex = new SafeMap(); | ||
|
||
{ | ||
const dummy = new ArrayBuffer(); | ||
ArrayPrototypeForEach(arrayBufferViewTypes, (ctor, i) => { | ||
const tag = ObjectPrototypeToString(new ctor(dummy)); | ||
arrayBufferViewTypeToIndex.set(tag, i); | ||
}); | ||
function arrayBufferViewTypeToIndex(abView) { | ||
const type = ObjectPrototypeToString(abView); | ||
if (type === '[object Int8Array]') return 0; | ||
if (type === '[object Uint8Array]') return 1; | ||
if (type === '[object Uint8ClampedArray]') return 2; | ||
if (type === '[object Int16Array]') return 3; | ||
if (type === '[object Uint16Array]') return 4; | ||
if (type === '[object Int32Array]') return 5; | ||
if (type === '[object Uint32Array]') return 6; | ||
if (type === '[object Float32Array]') return 7; | ||
if (type === '[object Float64Array]') return 8; | ||
if (type === '[object DataView]') return 9; | ||
// Index 10 is FastBuffer. | ||
if (type === '[object BigInt64Array]') return 11; | ||
if (type === '[object BigUint64Array]') return 12; | ||
return -1; | ||
} | ||
|
||
const bufferConstructorIndex = | ||
ArrayPrototypePush(arrayBufferViewTypes, FastBuffer) - 1; | ||
Comment on lines
-271
to
-272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line made it kind of ugly and complex to simply extend the array literal on line 259. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following would work and be simpler (even though a bit hacky): // Add "Array" as entry to arrayBufferViewTypes[10] above. Other constructors also work.
arrayBufferViewTypeToIndex.delete('[object Array]');
arrayBufferViewTypes[10] = FastBuffer;
const bufferConstructorIndex = 10; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially did something like that to keep the diff small but I made the judgment call the open-coded approach is going to be easier to understand and update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is actually a even simpler solution: --- a/lib/v8.js
+++ b/lib/v8.js
@@ -256,20 +256,23 @@ Deserializer.prototype.readRawBytes = function readRawBytes(length) {
* itself, custom serialization is applied. */
const arrayBufferViewTypes = [Int8Array, Uint8Array, Uint8ClampedArray,
Int16Array, Uint16Array, Int32Array, Uint32Array,
- Float32Array, Float64Array, DataView];
+ Float32Array, Float64Array, DataView,
+ FastBuffer, BigInt64Array, BigUint64Array];
const arrayBufferViewTypeToIndex = new SafeMap();
{
const dummy = new ArrayBuffer();
ArrayPrototypeForEach(arrayBufferViewTypes, (ctor, i) => {
+ if (ctor === FastBuffer) {
+ return;
+ }
const tag = ObjectPrototypeToString(new ctor(dummy));
arrayBufferViewTypeToIndex.set(tag, i);
});
}
-const bufferConstructorIndex =
- ArrayPrototypePush(arrayBufferViewTypes, FastBuffer) - 1;
+const bufferConstructorIndex = 10; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I never replied. That would work but... I don't get warm fuzzies from do-this-except-when-it's-that logic. It's shorter but IMO the virtue of the open-coded approach is it's glaringly obvious what it does because it's so stupidly simple. |
||
function arrayBufferViewIndexToType(index) { | ||
if (index === 0) return Int8Array; | ||
if (index === 1) return Uint8Array; | ||
if (index === 2) return Uint8ClampedArray; | ||
if (index === 3) return Int16Array; | ||
if (index === 4) return Uint16Array; | ||
if (index === 5) return Int32Array; | ||
if (index === 6) return Uint32Array; | ||
if (index === 7) return Float32Array; | ||
if (index === 8) return Float64Array; | ||
if (index === 9) return DataView; | ||
if (index === 10) return FastBuffer; | ||
if (index === 11) return BigInt64Array; | ||
if (index === 12) return BigUint64Array; | ||
return undefined; | ||
} | ||
|
||
class DefaultSerializer extends Serializer { | ||
constructor() { | ||
|
@@ -285,14 +294,17 @@ class DefaultSerializer extends Serializer { | |
* @returns {void} | ||
*/ | ||
_writeHostObject(abView) { | ||
let i = 0; | ||
if (abView.constructor === Buffer) { | ||
i = bufferConstructorIndex; | ||
} else { | ||
const tag = ObjectPrototypeToString(abView); | ||
i = arrayBufferViewTypeToIndex.get(tag); | ||
|
||
if (i === undefined) { | ||
// Keep track of how to handle different ArrayBufferViews. The default | ||
// Serializer for Node does not use the V8 methods for serializing those | ||
// objects because Node's `Buffer` objects use pooled allocation in many | ||
// cases, and their underlying `ArrayBuffer`s would show up in the | ||
// serialization. Because a) those may contain sensitive data and the user | ||
// may not be aware of that and b) they are often much larger than the | ||
// `Buffer` itself, custom serialization is applied. | ||
let i = 10; // FastBuffer | ||
if (abView.constructor !== Buffer) { | ||
BridgeAR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
i = arrayBufferViewTypeToIndex(abView); | ||
if (i === -1) { | ||
throw new this._getDataCloneError( | ||
`Unserializable host object: ${inspect(abView)}`); | ||
} | ||
|
@@ -313,7 +325,7 @@ class DefaultDeserializer extends Deserializer { | |
*/ | ||
_readHostObject() { | ||
const typeIndex = this.readUint32(); | ||
const ctor = arrayBufferViewTypes[typeIndex]; | ||
const ctor = arrayBufferViewIndexToType(typeIndex); | ||
const byteLength = this.readUint32(); | ||
const byteOffset = this._readRawBytes(byteLength); | ||
const BYTES_PER_ELEMENT = ctor.BYTES_PER_ELEMENT || 1; | ||
|
Uh oh!
There was an error while loading. Please reload this page.