nv-buf-serde 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DIST/dist.js +25 -0
- package/README.md +382 -0
- package/TEST/common.js +435 -0
- package/TEST/hole-tst.js +31 -0
- package/TEST/nd-benchmark.js +17 -0
- package/TEST/r-1bstr.js +33 -0
- package/TEST/r-2bstr.js +33 -0
- package/TEST/r-ab-and-abvw.js +48 -0
- package/TEST/r-bi.js +33 -0
- package/TEST/r-date.js +28 -0
- package/TEST/r-double.js +33 -0
- package/TEST/r-int-not-smi.js +28 -0
- package/TEST/r-mp-st-circular.js +105 -0
- package/TEST/r-odd-ball.js +33 -0
- package/TEST/r-packed-double.js +38 -0
- package/TEST/r-packed-smi.js +35 -0
- package/TEST/r-packed-with-attr.js +40 -0
- package/TEST/r-prim-wrap.js +35 -0
- package/TEST/r-rgx.js +28 -0
- package/TEST/r-smi.js +28 -0
- package/TEST/read-bi-contents.js +20 -0
- package/TEST/run.js +43 -0
- package/TEST/run.sh +29 -0
- package/TEST/serde-benchmark.js +17 -0
- package/TEST/tst.json +114 -0
- package/TEST/tst.v8ser +0 -0
- package/TEST/w-1bstr.js +34 -0
- package/TEST/w-2bstr.js +34 -0
- package/TEST/w-ab-and-abvw.js +60 -0
- package/TEST/w-bi.js +33 -0
- package/TEST/w-date.js +33 -0
- package/TEST/w-double.js +35 -0
- package/TEST/w-int-not-smi.js +37 -0
- package/TEST/w-mp-st-circular.js +60 -0
- package/TEST/w-odd-ball.js +43 -0
- package/TEST/w-packed-double.js +40 -0
- package/TEST/w-packed-smi.js +38 -0
- package/TEST/w-packed-with-attr.js +41 -0
- package/TEST/w-prim-wrap.js +41 -0
- package/TEST/w-rgx.js +33 -0
- package/TEST/w-smi.js +36 -0
- package/build.sh +1 -0
- package/const.js +181 -0
- package/ctx.js +89 -0
- package/fixed-cfg.js +6 -0
- package/index.js +27 -0
- package/misc.js +112 -0
- package/package.json +22 -0
- package/r.js +646 -0
- package/restrict.js +48 -0
- package/w.js +510 -0
- package/zero-nid.js +21 -0
package/w.js
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
const {_u,_n,_t,_f,is_untf} = require("nv-facutil-untf");
|
|
2
|
+
|
|
3
|
+
const _typis = require("nv-facutil-typis");
|
|
4
|
+
const _istis = require("nv-facutil-istis");
|
|
5
|
+
const {
|
|
6
|
+
is_prms,
|
|
7
|
+
is_g,
|
|
8
|
+
is_iter
|
|
9
|
+
} = require("nv-facutil-ppgflike-is");
|
|
10
|
+
|
|
11
|
+
const _zigzag = require("nv-number-zigzag");
|
|
12
|
+
const _utf16 = require("nv-buf-jstr");
|
|
13
|
+
const _rgx_flag = require("nv-regexp-flags");
|
|
14
|
+
const _buf_bi = require("nv-buf-bi");
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const _misc = require("./misc");
|
|
18
|
+
const {
|
|
19
|
+
SerializationTag,
|
|
20
|
+
ErrorTag,
|
|
21
|
+
ArrayBufferViewTag,
|
|
22
|
+
IsNodeFastBuffer,
|
|
23
|
+
kNodeFastBufferSym,
|
|
24
|
+
ArrayBufferViewIndexToCtor,
|
|
25
|
+
CreatFakeCantBeSered,
|
|
26
|
+
arrayBufferViewTypeToIndex,
|
|
27
|
+
} = require("./const");
|
|
28
|
+
|
|
29
|
+
const _ctx = require("./ctx");
|
|
30
|
+
|
|
31
|
+
////----
|
|
32
|
+
|
|
33
|
+
const WriteHeader = (a,kLatestVersion=15,ctx) => {
|
|
34
|
+
a.push(SerializationTag.kVersion);
|
|
35
|
+
a.push(kLatestVersion);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const WriteTag = (a,tag,ctx) => { a.push(tag)}
|
|
39
|
+
|
|
40
|
+
const WriteVarint = (a,v,ctx) => {
|
|
41
|
+
if(_zigzag.is_smi_can_be_encded(v)){
|
|
42
|
+
let next_byte;
|
|
43
|
+
do {
|
|
44
|
+
next_byte = (v & 0x7F) | 0x80 ;
|
|
45
|
+
a.push(Number(next_byte)) ;
|
|
46
|
+
v = v>>7;
|
|
47
|
+
} while(v);
|
|
48
|
+
a[a.length-1] = a[a.length-1] & 0x7F
|
|
49
|
+
return(a)
|
|
50
|
+
} else {
|
|
51
|
+
v = BigInt(v);
|
|
52
|
+
let next_byte;
|
|
53
|
+
do {
|
|
54
|
+
next_byte = (v & 0x7Fn) | 0x80n ;
|
|
55
|
+
a.push(Number(next_byte)) ;
|
|
56
|
+
v = v>>7n;
|
|
57
|
+
} while(v);
|
|
58
|
+
a[a.length-1] = a[a.length-1] & 0x7F
|
|
59
|
+
return(a)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const WriteZigZag = (a,v,ctx,encode=_zigzag.encd) => {
|
|
64
|
+
v = encode(v);
|
|
65
|
+
WriteVarint(a,v);
|
|
66
|
+
return(a)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
const WriteDouble = (a,v,ctx,f64a) => {
|
|
71
|
+
f64a = f64a?? new Float64Array(1);
|
|
72
|
+
f64a[0] = v;
|
|
73
|
+
let u8a = new Uint8Array(f64a.buffer);
|
|
74
|
+
for(let i=0;i<8;++i) {
|
|
75
|
+
a.push(u8a[i])
|
|
76
|
+
}
|
|
77
|
+
return(a)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const WriteOneByteString = (a,s,ctx) => {
|
|
81
|
+
WriteVarint(a,s.length);
|
|
82
|
+
for(let i=0;i<s.length;++i) {
|
|
83
|
+
a.push(s.charCodeAt(i))
|
|
84
|
+
}
|
|
85
|
+
return(a)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const WriteTwoByteString = (a,s,ctx,mach) => {
|
|
89
|
+
mach = mach?? (new _utf16.encd.mach.Mach());
|
|
90
|
+
WriteVarint(a,s.length*2);
|
|
91
|
+
for(let i =0;i<s.length;++i) {
|
|
92
|
+
let cd = s.charCodeAt(i);
|
|
93
|
+
let [fst,snd] = mach.input(cd);
|
|
94
|
+
a.push(fst);
|
|
95
|
+
a.push(snd)
|
|
96
|
+
}
|
|
97
|
+
return(a)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
const WriteRawBytes = (a,byts,ctx) => {
|
|
102
|
+
for(let i=0;i<byts.length;++i) {
|
|
103
|
+
a.push(byts[i])
|
|
104
|
+
}
|
|
105
|
+
return(a)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const WriteOddball = (a,o,ctx)=> {
|
|
109
|
+
let tag = SerializationTag.kUndefined;
|
|
110
|
+
switch (o) {
|
|
111
|
+
case _u:
|
|
112
|
+
tag = SerializationTag.kUndefined;
|
|
113
|
+
break;
|
|
114
|
+
case _f:
|
|
115
|
+
tag = SerializationTag.kFalse;
|
|
116
|
+
break;
|
|
117
|
+
case _t:
|
|
118
|
+
tag = SerializationTag.kTrue;
|
|
119
|
+
break;
|
|
120
|
+
case _n:
|
|
121
|
+
tag = SerializationTag.kNull;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
WriteTag(a,tag);
|
|
125
|
+
return(a);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const WriteSmi = (a,smi,ctx)=> {
|
|
129
|
+
WriteTag(a,SerializationTag.kInt32,ctx);
|
|
130
|
+
WriteZigZag(a,smi,ctx);
|
|
131
|
+
return(a);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const WriteHeapNumber = (a,number,ctx) => {
|
|
135
|
+
WriteTag(a,SerializationTag.kDouble,ctx);
|
|
136
|
+
WriteDouble(a,number,ctx);
|
|
137
|
+
return(a);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
const WriteString = (a,s,ctx)=>{
|
|
142
|
+
s | 0; // String::Flattern
|
|
143
|
+
////-------------------------------------------------------------------
|
|
144
|
+
if (_misc.IsOneByte(s)) {
|
|
145
|
+
WriteTag(a,SerializationTag.kOneByteString,ctx);
|
|
146
|
+
WriteOneByteString(a,s,ctx);
|
|
147
|
+
} else {
|
|
148
|
+
let byte_length = s.length * 2;
|
|
149
|
+
let head_byte_len = _misc.BytesNeededForVarint(byte_length);
|
|
150
|
+
if((a.length + 1+ head_byte_len & 1)===0) {
|
|
151
|
+
//no need padding TAG | X0 X1 X2 | ....data
|
|
152
|
+
} else {
|
|
153
|
+
//need padding TAG | X0 X1 X2 X3| ...data
|
|
154
|
+
// PAD TAG | X0 X1 X2 X3|
|
|
155
|
+
//to make len(PAD TAG | X0 X1 X2 X3|) is even
|
|
156
|
+
WriteTag(a,SerializationTag.kPadding,ctx);
|
|
157
|
+
}
|
|
158
|
+
WriteTag(a,SerializationTag.kTwoByteString,ctx);
|
|
159
|
+
WriteTwoByteString(a,s,ctx);
|
|
160
|
+
}
|
|
161
|
+
return(a);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const WriteJSDate = (a,dt,ctx) => {
|
|
165
|
+
WriteTag(a,SerializationTag.kDate,ctx);
|
|
166
|
+
WriteDouble(a,dt.getTime(),ctx);
|
|
167
|
+
return(a);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const WriteJSRegExp = (a,rgx,ctx)=> {
|
|
171
|
+
WriteTag(a,SerializationTag.kRegExp,ctx);
|
|
172
|
+
WriteString(a,rgx.source,ctx);
|
|
173
|
+
let ui32_flags = _rgx_flag.s2n(rgx.flags);
|
|
174
|
+
WriteVarint(a,ui32_flags,ctx);
|
|
175
|
+
return(a);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const WriteBigIntContents = (a,obi,ctx) => {
|
|
179
|
+
let [sign,blen,bi] = _buf_bi._get_head(obi);
|
|
180
|
+
let encd_head = (sign | (blen<<1));
|
|
181
|
+
WriteVarint(a,encd_head,ctx);
|
|
182
|
+
let bui64a = new BigUint64Array(1);
|
|
183
|
+
let u8a = new Uint8Array(bui64a.buffer);
|
|
184
|
+
while(bi) {
|
|
185
|
+
bui64a[0] = bi & _buf_bi.MASK;
|
|
186
|
+
a.push(u8a[0],u8a[1],u8a[2],u8a[3],u8a[4],u8a[5],u8a[6],u8a[7])
|
|
187
|
+
bi = bi>>64n;
|
|
188
|
+
}
|
|
189
|
+
return(a);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const WriteBigInt = (a,bi,ctx)=> {
|
|
193
|
+
WriteTag(a,SerializationTag.kBigInt,ctx);
|
|
194
|
+
WriteBigIntContents(a,bi,ctx);
|
|
195
|
+
return(a);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const WriteByte = (a,u8,ctx) => { a.push(u8); return(a);}
|
|
199
|
+
const WriteUint32 = (a,u32,ctx) => { WriteVarint(a,u32,ctx) ; return(a);}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
const WriteJSPrimitiveWrapper = (a,v,ctx) => {
|
|
203
|
+
if (_istis.is_truo(v)) {
|
|
204
|
+
WriteTag(a,SerializationTag.kTrueObject,ctx);
|
|
205
|
+
return(a);
|
|
206
|
+
} else if (_istis.is_flso(v)) {
|
|
207
|
+
WriteTag(a,SerializationTag.kFalseObject,ctx);
|
|
208
|
+
return(a);
|
|
209
|
+
} else if (_istis.is_numo(v)) {
|
|
210
|
+
WriteTag(a,SerializationTag.kNumberObject,ctx);
|
|
211
|
+
WriteDouble(a,Number(v),ctx);
|
|
212
|
+
return(a);
|
|
213
|
+
} else if (_istis.is_stro(v)) {
|
|
214
|
+
WriteTag(a,SerializationTag.kStringObject,ctx);
|
|
215
|
+
WriteString(a,String(v),ctx);
|
|
216
|
+
return(a);
|
|
217
|
+
} else if (_istis.is_bio(v)) {
|
|
218
|
+
// this is impossible in js-layer
|
|
219
|
+
WriteTag(a,SerializationTag.kBigIntObject,ctx);
|
|
220
|
+
WriteBigIntContents(a,BigInt(v),ctx);
|
|
221
|
+
return(a);
|
|
222
|
+
} else {
|
|
223
|
+
return(_u); // this is a unmatch-flag
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
const WriteJSError = (a,e,ctx) => {
|
|
229
|
+
|
|
230
|
+
WriteTag(a,SerializationTag.kError,ctx);
|
|
231
|
+
let name = e.name;
|
|
232
|
+
switch(name) {
|
|
233
|
+
case("EvalError" ): {WriteVarint(a,ErrorTag.kEvalErrorPrototype,ctx ); break;}
|
|
234
|
+
case("RangeError"): {WriteVarint(a,ErrorTag.kRangeErrorPrototype,ctx ); break;}
|
|
235
|
+
case("ReferenceError"): {WriteVarint(a,ErrorTag.kReferenceErrorPrototype,ctx); break;}
|
|
236
|
+
case("SyntaxError"): {WriteVarint(a,ErrorTag.kSyntaxErrorPrototype,ctx ); break;}
|
|
237
|
+
case("TypeError"): {WriteVarint(a,ErrorTag.kTypeErrorPrototype,ctx ); break;}
|
|
238
|
+
case("URIError"): {WriteVarint(a,ErrorTag.kUriErrorPrototype,ctx ); break;}
|
|
239
|
+
default: {
|
|
240
|
+
//do nothing
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
let message = String(e.message);
|
|
245
|
+
WriteVarint(a,ErrorTag.kMessage,ctx);
|
|
246
|
+
WriteString(a,message,ctx);
|
|
247
|
+
|
|
248
|
+
let cause = e.cause;
|
|
249
|
+
if(cause) {
|
|
250
|
+
WriteVarint(a,ErrorTag.kCause,ctx);
|
|
251
|
+
WriteObject(a,cause,ctx);
|
|
252
|
+
} else {}
|
|
253
|
+
|
|
254
|
+
let stack = String(e.stack);
|
|
255
|
+
WriteVarint(a,ErrorTag.kStack,ctx);
|
|
256
|
+
WriteString(a,stack,ctx);
|
|
257
|
+
|
|
258
|
+
WriteVarint(a,ErrorTag.kEnd,ctx);
|
|
259
|
+
return(a);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
const WriteObject = (a,o,ctx) => {
|
|
264
|
+
if (_typis.is_num(o) && _misc.IsSmi(o)) {
|
|
265
|
+
WriteSmi(a,o,ctx);
|
|
266
|
+
} else if(o===_u) {
|
|
267
|
+
WriteTag(a,SerializationTag.kUndefined,ctx);
|
|
268
|
+
} else if(o===_n) {
|
|
269
|
+
WriteTag(a,SerializationTag.kNull,ctx);
|
|
270
|
+
} else if(o===_t) {
|
|
271
|
+
WriteTag(a,SerializationTag.kTrue,ctx);
|
|
272
|
+
} else if(o===_f) {
|
|
273
|
+
WriteTag(a,SerializationTag.kFalse,ctx);
|
|
274
|
+
} else if(_typis.is_num(o)) {
|
|
275
|
+
WriteHeapNumber(a,o,ctx);
|
|
276
|
+
} else if(_typis.is_bi(o)) {
|
|
277
|
+
WriteBigInt(a,o,ctx);
|
|
278
|
+
} else if(_typis.is_str(o)) {
|
|
279
|
+
WriteString(a,o,ctx);
|
|
280
|
+
} else {
|
|
281
|
+
WriteJSReceiver(a,o,ctx); //!id_map_.Find
|
|
282
|
+
}
|
|
283
|
+
return(a);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const WriteJSSet = (a,st,ctx) => {
|
|
287
|
+
WriteTag(a,SerializationTag.kBeginJSSet,ctx);
|
|
288
|
+
let sz = 0;
|
|
289
|
+
for(let e of st) {
|
|
290
|
+
WriteObject(a,e,ctx);
|
|
291
|
+
++sz;
|
|
292
|
+
}
|
|
293
|
+
WriteTag(a,SerializationTag.kEndJSSet,ctx);
|
|
294
|
+
WriteVarint(a,sz,ctx);
|
|
295
|
+
return(a);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const WriteJSMap = (a,mp,ctx) => {
|
|
299
|
+
WriteTag(a,SerializationTag.kBeginJSMap,ctx);
|
|
300
|
+
let sz = mp.size * 2;
|
|
301
|
+
for(let [k,v] of mp) {
|
|
302
|
+
WriteObject(a,k,ctx);
|
|
303
|
+
WriteObject(a,v,ctx);
|
|
304
|
+
}
|
|
305
|
+
WriteTag(a,SerializationTag.kEndJSMap,ctx);
|
|
306
|
+
WriteVarint(a,sz,ctx);
|
|
307
|
+
return(a);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
//compatible with node delegate
|
|
312
|
+
const WriteHostObject = (a,o,ctx) => {
|
|
313
|
+
let abvw_sub_type = arrayBufferViewTypeToIndex(o);
|
|
314
|
+
if(abvw_sub_type>-1) {
|
|
315
|
+
WriteTag(a,SerializationTag.kHostObject,ctx);
|
|
316
|
+
WriteUint32(a, abvw_sub_type, ctx);
|
|
317
|
+
WriteUint32(a, o.byteLength, ctx);
|
|
318
|
+
let u8a = new Uint8Array(o.buffer);
|
|
319
|
+
WriteRawBytes(a,u8a,ctx);
|
|
320
|
+
return(a);
|
|
321
|
+
} else {
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const WriteJSArrayBufferView = (a,o,ctx) => WriteHostObject(a,o,ctx);
|
|
326
|
+
|
|
327
|
+
const WriteJSArrayBuffer = (a,o,ctx) => {
|
|
328
|
+
let is_resizable = _f;
|
|
329
|
+
if(_istis.is_sab(o)) {
|
|
330
|
+
is_resizable = Boolean(o.growable); // compatible to nodejs version < 20
|
|
331
|
+
} else {
|
|
332
|
+
is_resizable = o.resizable
|
|
333
|
+
}
|
|
334
|
+
let max_byte_length = (is_resizable)?o.maxByteLength:o.byteLength;
|
|
335
|
+
let byte_length = o.byteLength;
|
|
336
|
+
if (is_resizable) {
|
|
337
|
+
WriteTag(a,SerializationTag.kResizableArrayBuffer,ctx);
|
|
338
|
+
WriteVarint(a,byte_length,ctx);
|
|
339
|
+
WriteVarint(a,max_byte_length,ctx);
|
|
340
|
+
WriteRawBytes(a,new Uint8Array(o),ctx);
|
|
341
|
+
} else {
|
|
342
|
+
WriteTag(a,SerializationTag.kArrayBuffer,ctx);
|
|
343
|
+
WriteVarint(a,byte_length,ctx);
|
|
344
|
+
WriteRawBytes(a,new Uint8Array(o),ctx);
|
|
345
|
+
}
|
|
346
|
+
return(a)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const WriteJSObject = (a,o,ctx) => {
|
|
350
|
+
WriteTag(a,SerializationTag.kBeginJSObject,ctx);
|
|
351
|
+
let properties_written = 0; // kv-pair length NOT entries length
|
|
352
|
+
for(let k in o) {
|
|
353
|
+
WriteObject(a,k,ctx);
|
|
354
|
+
WriteObject(a,o[k],ctx);
|
|
355
|
+
properties_written++;
|
|
356
|
+
}
|
|
357
|
+
WriteTag(a,SerializationTag.kEndJSObject,ctx);
|
|
358
|
+
WriteVarint(a,properties_written,ctx);
|
|
359
|
+
return(a)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
const WriteJSArray = (a,o,ctx) => {
|
|
364
|
+
let length = o.length;
|
|
365
|
+
let {is_packed_smi,is_packed_double} = _misc.GetArrayPackedInfo(o);
|
|
366
|
+
if(is_packed_smi) {
|
|
367
|
+
WriteTag(a,SerializationTag.kBeginDenseJSArray,ctx);
|
|
368
|
+
WriteVarint(a,length,ctx);
|
|
369
|
+
for(let i=0;i<o.length;++i) {
|
|
370
|
+
WriteSmi(a,o[i],ctx);
|
|
371
|
+
}
|
|
372
|
+
} else if(is_packed_double) {
|
|
373
|
+
WriteTag(a,SerializationTag.kBeginDenseJSArray,ctx);
|
|
374
|
+
WriteVarint(a,length,ctx);
|
|
375
|
+
if(length !==0) {
|
|
376
|
+
for(let i=0;i<o.length;++i) {
|
|
377
|
+
WriteTag(a,SerializationTag.kDouble,ctx);
|
|
378
|
+
WriteDouble(a,o[i],ctx);
|
|
379
|
+
}
|
|
380
|
+
} else {}
|
|
381
|
+
} else {
|
|
382
|
+
WriteTag(a,SerializationTag.kBeginDenseJSArray,ctx);
|
|
383
|
+
WriteVarint(a,length,ctx);
|
|
384
|
+
for(let i=0;i<o.length;++i) {
|
|
385
|
+
WriteObject(a,o[i],ctx);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
////-------must use this slow method
|
|
389
|
+
let ks = Object.keys(o);
|
|
390
|
+
let properties_written = 0;
|
|
391
|
+
for(let i=o.length;i<ks.length;++i) {
|
|
392
|
+
let k = ks[i];
|
|
393
|
+
WriteObject(a,k,ctx);
|
|
394
|
+
WriteObject(a,o[k],ctx);
|
|
395
|
+
properties_written++;
|
|
396
|
+
}
|
|
397
|
+
WriteTag(a,SerializationTag.kEndDenseJSArray,ctx);
|
|
398
|
+
WriteVarint(a,properties_written,ctx);
|
|
399
|
+
WriteVarint(a,length,ctx);
|
|
400
|
+
return(a);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
const WriteJSReceiver = (a,o,ctx) => {
|
|
405
|
+
if(is_untf(o)) {
|
|
406
|
+
WriteOddball(a,o,ctx);
|
|
407
|
+
} else if(_misc.IsSmi(o)) {
|
|
408
|
+
WriteSmi(a,o,ctx);
|
|
409
|
+
} else if(_typis.is_num(o)) {
|
|
410
|
+
WriteHeapNumber(a,o,ctx);
|
|
411
|
+
} else if(_typis.is_str(o)) {
|
|
412
|
+
WriteString(a,o,ctx);
|
|
413
|
+
} else if(_typis.is_bi(o)) {
|
|
414
|
+
WriteBigInt(a,o,ctx);
|
|
415
|
+
} else if(_typis.is_sym(o)) {
|
|
416
|
+
let phantom = CreatFakeCantBeSered("Symbol",o.description);
|
|
417
|
+
WriteJSObject(a,phantom,ctx);
|
|
418
|
+
} else {
|
|
419
|
+
let [already_exists,idx] = ctx.idmap.FindOrInsert(o);
|
|
420
|
+
if (already_exists) {
|
|
421
|
+
WriteTag(a,SerializationTag.kObjectReference,ctx);
|
|
422
|
+
WriteVarint(a,idx,ctx);
|
|
423
|
+
} else {
|
|
424
|
+
if(_istis.is_ary(o)) {
|
|
425
|
+
WriteJSArray(a,o,ctx);
|
|
426
|
+
} else if(_istis.is_st(o)) {
|
|
427
|
+
WriteJSSet(a,o,ctx);
|
|
428
|
+
} else if(_istis.is_mp(o)) {
|
|
429
|
+
WriteJSMap(a,o,ctx);
|
|
430
|
+
} else if(_istis.is_dt(o)) {
|
|
431
|
+
WriteJSDate(a,o,ctx);
|
|
432
|
+
} else if(_istis.is_rgx(o)) {
|
|
433
|
+
WriteJSRegExp(a,o,ctx);
|
|
434
|
+
} else if(_istis.is_ab(o) || _istis.is_sab(o)) {
|
|
435
|
+
WriteJSArrayBuffer(a,o,ctx)
|
|
436
|
+
} else if(o.buffer && _istis.is_ab(o.buffer)) {
|
|
437
|
+
//not correct ,but quick
|
|
438
|
+
let r = WriteJSArrayBufferView(a,o,ctx);
|
|
439
|
+
if(r!==_u) {
|
|
440
|
+
} else {
|
|
441
|
+
WriteJSObject(a,o,ctx);
|
|
442
|
+
}
|
|
443
|
+
} else if(_typis.istof(o,Error)) {
|
|
444
|
+
WriteJSError(a,o,ctx);
|
|
445
|
+
} else {
|
|
446
|
+
let r = WriteJSPrimitiveWrapper(a,o,ctx);
|
|
447
|
+
if(r!==_u) {
|
|
448
|
+
} else {
|
|
449
|
+
if(
|
|
450
|
+
_typis.is_func_like(o) ||
|
|
451
|
+
is_prms(o) ||
|
|
452
|
+
is_g(o) ||
|
|
453
|
+
is_iter(o)
|
|
454
|
+
) {
|
|
455
|
+
let phantom = CreatFakeCantBeSered(idx,String(o));
|
|
456
|
+
WriteJSObject(a,phantom,ctx);
|
|
457
|
+
} else {
|
|
458
|
+
WriteJSObject(a,o,ctx)
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return(a)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const encd = (o,kLatestVersion=15) => {
|
|
468
|
+
let a = [];
|
|
469
|
+
let ctx = _ctx.creat_wctx();
|
|
470
|
+
WriteHeader(a,kLatestVersion);
|
|
471
|
+
WriteJSReceiver(a,o,ctx);
|
|
472
|
+
let u8a = new Uint8Array(a);
|
|
473
|
+
return(u8a.buffer)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
module.exports = {
|
|
477
|
+
WriteHeader,
|
|
478
|
+
WriteTag,
|
|
479
|
+
WriteVarint,
|
|
480
|
+
WriteZigZag,
|
|
481
|
+
WriteDouble,
|
|
482
|
+
WriteOneByteString,
|
|
483
|
+
WriteTwoByteString,
|
|
484
|
+
WriteRawBytes,
|
|
485
|
+
WriteOddball,
|
|
486
|
+
WriteSmi,
|
|
487
|
+
WriteHeapNumber,
|
|
488
|
+
WriteString,
|
|
489
|
+
WriteJSDate,
|
|
490
|
+
WriteJSRegExp,
|
|
491
|
+
WriteBigIntContents,
|
|
492
|
+
WriteBigInt,
|
|
493
|
+
WriteByte,
|
|
494
|
+
WriteUint32,
|
|
495
|
+
WriteJSPrimitiveWrapper,
|
|
496
|
+
WriteJSError,
|
|
497
|
+
////
|
|
498
|
+
WriteObject,
|
|
499
|
+
WriteJSSet,
|
|
500
|
+
WriteJSMap,
|
|
501
|
+
WriteHostObject,
|
|
502
|
+
WriteJSArrayBufferView,
|
|
503
|
+
WriteJSArrayBuffer,
|
|
504
|
+
WriteJSObject,
|
|
505
|
+
WriteJSArray,
|
|
506
|
+
////
|
|
507
|
+
WriteJSReceiver,
|
|
508
|
+
////
|
|
509
|
+
encd
|
|
510
|
+
}
|
package/zero-nid.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const _A = Array;
|
|
2
|
+
|
|
3
|
+
const MAX_NID = 2**53-1;
|
|
4
|
+
|
|
5
|
+
class Nid extends Array {
|
|
6
|
+
constructor() {super();this[0]=0;}
|
|
7
|
+
get id() {return(this[0])}
|
|
8
|
+
next() {
|
|
9
|
+
let id = this[0]
|
|
10
|
+
let nid = this[0]+1;
|
|
11
|
+
if(nid-1 === MAX_NID) {nid = 0}
|
|
12
|
+
this[0] = nid;
|
|
13
|
+
return(id)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const creat_nid = () =>new Nid();
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
MAX_NID,creat_nid,
|
|
21
|
+
}
|