@@ -95,6 +95,47 @@ const deserializerTypeError =
95
95
assert . strictEqual ( des . readValue ( ) . val , hostObject ) ;
96
96
}
97
97
98
+ // This test ensures that `v8.Serializer.writeRawBytes()` support
99
+ // `TypedArray` and `DataView`.
100
+ {
101
+ const text = 'hostObjectTag' ;
102
+ const data = Buffer . from ( text ) ;
103
+ const arrayBufferViews = common . getArrayBufferViews ( data ) ;
104
+
105
+ // `buf` is one of `TypedArray` or `DataView`.
106
+ function testWriteRawBytes ( buf ) {
107
+ let writeHostObjectCalled = false ;
108
+ const ser = new v8 . DefaultSerializer ( ) ;
109
+
110
+ ser . _writeHostObject = common . mustCall ( ( object ) => {
111
+ writeHostObjectCalled = true ;
112
+ ser . writeUint32 ( buf . byteLength ) ;
113
+ ser . writeRawBytes ( buf ) ;
114
+ } ) ;
115
+
116
+ ser . writeHeader ( ) ;
117
+ ser . writeValue ( { val : hostObject } ) ;
118
+
119
+ const des = new v8 . DefaultDeserializer ( ser . releaseBuffer ( ) ) ;
120
+ des . _readHostObject = common . mustCall ( ( ) => {
121
+ assert . strictEqual ( writeHostObjectCalled , true ) ;
122
+ const length = des . readUint32 ( ) ;
123
+ const buf = des . readRawBytes ( length ) ;
124
+ assert . strictEqual ( buf . toString ( ) , text ) ;
125
+
126
+ return hostObject ;
127
+ } ) ;
128
+
129
+ des . readHeader ( ) ;
130
+
131
+ assert . strictEqual ( des . readValue ( ) . val , hostObject ) ;
132
+ }
133
+
134
+ arrayBufferViews . forEach ( ( buf ) => {
135
+ testWriteRawBytes ( buf ) ;
136
+ } ) ;
137
+ }
138
+
98
139
{
99
140
const ser = new v8 . DefaultSerializer ( ) ;
100
141
ser . _writeHostObject = common . mustCall ( ( object ) => {
@@ -143,3 +184,46 @@ const deserializerTypeError =
143
184
assert . throws ( v8 . Serializer , serializerTypeError ) ;
144
185
assert . throws ( v8 . Deserializer , deserializerTypeError ) ;
145
186
}
187
+
188
+
189
+ // `v8.deserialize()` and `new v8.Deserializer()` should support both
190
+ // `TypedArray` and `DataView`.
191
+ {
192
+ for ( const obj of objects ) {
193
+ const buf = v8 . serialize ( obj ) ;
194
+
195
+ for ( const arrayBufferView of common . getArrayBufferViews ( buf ) ) {
196
+ assert . deepStrictEqual ( v8 . deserialize ( arrayBufferView ) , obj ) ;
197
+ }
198
+
199
+ for ( const arrayBufferView of common . getArrayBufferViews ( buf ) ) {
200
+ const deserializer = new v8 . DefaultDeserializer ( arrayBufferView ) ;
201
+ deserializer . readHeader ( ) ;
202
+ const value = deserializer . readValue ( ) ;
203
+ assert . deepStrictEqual ( value , obj ) ;
204
+
205
+ const serializer = new v8 . DefaultSerializer ( ) ;
206
+ serializer . writeHeader ( ) ;
207
+ serializer . writeValue ( value ) ;
208
+ assert . deepStrictEqual ( buf , serializer . releaseBuffer ( ) ) ;
209
+ }
210
+ }
211
+ }
212
+
213
+ {
214
+ const INVALID_SOURCE = 'INVALID_SOURCE_TYPE' ;
215
+ const serializer = new v8 . Serializer ( ) ;
216
+ serializer . writeHeader ( ) ;
217
+ assert . throws (
218
+ ( ) => serializer . writeRawBytes ( INVALID_SOURCE ) ,
219
+ / ^ T y p e E r r o r : s o u r c e m u s t b e a T y p e d A r r a y o r a D a t a V i e w $ / ,
220
+ ) ;
221
+ assert . throws (
222
+ ( ) => v8 . deserialize ( INVALID_SOURCE ) ,
223
+ / ^ T y p e E r r o r : b u f f e r m u s t b e a T y p e d A r r a y o r a D a t a V i e w $ / ,
224
+ ) ;
225
+ assert . throws (
226
+ ( ) => new v8 . Deserializer ( INVALID_SOURCE ) ,
227
+ / ^ T y p e E r r o r : b u f f e r m u s t b e a T y p e d A r r a y o r a D a t a V i e w $ / ,
228
+ ) ;
229
+ }
0 commit comments