mol_vary 0.0.29 → 0.0.31

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/web.mjs CHANGED
@@ -287,22 +287,18 @@ var $;
287
287
  const release = (size) => {
288
288
  capacity -= size;
289
289
  };
290
- const dump_bint = (val) => {
291
- const buf = $mol_bigint_encode(val);
292
- acquire(buf.byteLength - 7);
293
- if (buf.byteLength > 264)
294
- $mol_fail(new Error('Number too high', { cause: { val } }));
295
- buffer[pos++] = -$mol_vary_len.LA;
296
- buffer[pos++] = buf.byteLength - 9;
297
- buffer.set(buf, pos);
298
- pos += buf.byteLength;
299
- };
300
290
  const dump_unum = (tip, val) => {
301
291
  if (val < $mol_vary_len.L1) {
302
292
  buffer[pos++] = tip | Number(val);
303
293
  release(8);
294
+ return;
295
+ }
296
+ if (tip == $mol_vary_tip.uint) {
297
+ const offset = offsets.get(val);
298
+ if (offset !== undefined)
299
+ return dump_unum($mol_vary_tip.link, offset);
304
300
  }
305
- else if (val < 2 ** 8) {
301
+ if (val < 2 ** 8) {
306
302
  buffer[pos++] = tip | $mol_vary_len.L1;
307
303
  buffer[pos++] = Number(val);
308
304
  release(7);
@@ -327,13 +323,19 @@ var $;
327
323
  else {
328
324
  dump_bint(val);
329
325
  }
326
+ if (tip == $mol_vary_tip.uint)
327
+ offsets.set(val, offsets.size);
330
328
  };
331
329
  const dump_snum = (val) => {
332
330
  if (val > -$mol_vary_len.L1) {
333
331
  buffer[pos++] = Number(val);
334
332
  release(8);
333
+ return;
335
334
  }
336
- else if (val >= -(2 ** 7)) {
335
+ const offset = offsets.get(val);
336
+ if (offset !== undefined)
337
+ return dump_unum($mol_vary_tip.link, offset);
338
+ if (val >= -(2 ** 7)) {
337
339
  buffer[pos++] = -$mol_vary_len.L1;
338
340
  buffer[pos++] = Number(val);
339
341
  release(7);
@@ -358,28 +360,43 @@ var $;
358
360
  else {
359
361
  dump_bint(val);
360
362
  }
363
+ offsets.set(val, offsets.size);
364
+ };
365
+ const dump_bint = (val) => {
366
+ const buf = $mol_bigint_encode(val);
367
+ if (buf.byteLength > 264)
368
+ $mol_fail(new Error('Number too high', { cause: { val } }));
369
+ acquire(buf.byteLength - 7);
370
+ buffer[pos++] = -$mol_vary_len.LA;
371
+ buffer[pos++] = buf.byteLength - 9;
372
+ buffer.set(buf, pos);
373
+ pos += buf.byteLength;
374
+ };
375
+ const dump_float = (val) => {
376
+ const offset = offsets.get(val);
377
+ if (offset !== undefined)
378
+ return dump_unum($mol_vary_tip.link, offset);
379
+ buffer[pos++] = $mol_vary_spec.fp64;
380
+ pack.setFloat64(pos, val, true);
381
+ pos += 8;
382
+ offsets.set(val, offsets.size);
361
383
  };
362
384
  const dump_string = (val) => {
363
- if (val.length) {
364
- const offset = offsets.get(val);
365
- if (offset !== undefined)
366
- return dump_unum($mol_vary_tip.link, offset);
367
- }
385
+ const offset = offsets.get(val);
386
+ if (offset !== undefined)
387
+ return dump_unum($mol_vary_tip.link, offset);
368
388
  dump_unum($mol_vary_tip.text, val.length);
369
389
  acquire(val.length * 3);
370
390
  const len = $mol_charset_encode_to(val, buffer, pos);
371
391
  pos += len;
372
392
  release(val.length * 3 - len);
373
- if (val.length)
374
- offsets.set(val, offsets.size);
393
+ offsets.set(val, offsets.size);
375
394
  return;
376
395
  };
377
396
  const dump_buffer = (val) => {
378
- if (val.byteLength) {
379
- const offset = offsets.get(val);
380
- if (offset !== undefined)
381
- return dump_unum($mol_vary_tip.link, offset);
382
- }
397
+ const offset = offsets.get(val);
398
+ if (offset !== undefined)
399
+ return dump_unum($mol_vary_tip.link, offset);
383
400
  dump_unum($mol_vary_tip.blob, val.byteLength);
384
401
  if (val instanceof Uint8Array)
385
402
  buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
@@ -409,21 +426,17 @@ var $;
409
426
  acquire(val.byteLength);
410
427
  buffer.set(src, pos);
411
428
  pos += val.byteLength;
412
- if (val.byteLength)
413
- offsets.set(val, offsets.size);
429
+ offsets.set(val, offsets.size);
414
430
  };
415
431
  const dump_list = (val) => {
416
- if (val.length) {
417
- const offset = offsets.get(val);
418
- if (offset !== undefined)
419
- return dump_unum($mol_vary_tip.link, offset);
420
- }
432
+ const offset = offsets.get(val);
433
+ if (offset !== undefined)
434
+ return dump_unum($mol_vary_tip.link, offset);
421
435
  dump_unum($mol_vary_tip.list, val.length);
422
436
  acquire(val.length * 9);
423
437
  for (const item of val)
424
438
  dump(item);
425
- if (val.length)
426
- offsets.set(val, offsets.size);
439
+ offsets.set(val, offsets.size);
427
440
  };
428
441
  const dump_object = (val) => {
429
442
  const offset = offsets.get(val);
@@ -437,8 +450,7 @@ var $;
437
450
  dump(item);
438
451
  for (const item of vals)
439
452
  dump(item);
440
- if (vals.length)
441
- offsets.set(val, offsets.size);
453
+ offsets.set(val, offsets.size);
442
454
  };
443
455
  const dump = (val) => {
444
456
  switch (typeof val) {
@@ -453,12 +465,8 @@ var $;
453
465
  return;
454
466
  }
455
467
  case 'number': {
456
- if (!Number.isInteger(val)) {
457
- buffer[pos++] = $mol_vary_spec.fp64;
458
- pack.setFloat64(pos, val, true);
459
- pos += 8;
460
- return;
461
- }
468
+ if (!Number.isInteger(val))
469
+ return dump_float(val);
462
470
  }
463
471
  case 'bigint': {
464
472
  if (val < 0) {
@@ -517,6 +525,8 @@ var $;
517
525
  else {
518
526
  $mol_fail(new Error('Unsupported unum', { cause: { num } }));
519
527
  }
528
+ if ((kind & 0b111_00000) === $mol_vary_tip.uint)
529
+ stream.push(res);
520
530
  return res;
521
531
  };
522
532
  const read_snum = (kind) => {
@@ -549,21 +559,20 @@ var $;
549
559
  else {
550
560
  $mol_fail(new Error('Unsupported snum', { cause: { num } }));
551
561
  }
562
+ stream.push(res);
552
563
  return res;
553
564
  };
554
565
  const read_text = (kind) => {
555
566
  const len = read_unum(kind);
556
567
  const [text, bytes] = $mol_charset_decode_from(buffer, pack.byteOffset + pos, len);
557
568
  pos += bytes;
558
- if (text.length)
559
- stream.push(text);
569
+ stream.push(text);
560
570
  return text;
561
571
  };
562
572
  const read_buffer = (len, TypedArray) => {
563
573
  const bin = new TypedArray(buffer.slice(pos, pos + len).buffer);
564
574
  pos += len;
565
- if (len)
566
- stream.push(bin);
575
+ stream.push(bin);
567
576
  return bin;
568
577
  };
569
578
  const read_blob = (kind) => {
@@ -590,8 +599,7 @@ var $;
590
599
  const list = new Array(len);
591
600
  for (let i = 0; i < len; ++i)
592
601
  list[i] = read_vary();
593
- if (len)
594
- stream.push(list);
602
+ stream.push(list);
595
603
  return list;
596
604
  };
597
605
  const read_link = (kind) => {
@@ -619,8 +627,7 @@ var $;
619
627
  for (let i = 0; i < len; ++i)
620
628
  obj[keys[i]] = vals[i];
621
629
  }
622
- if (vals.length)
623
- stream.push(obj);
630
+ stream.push(obj);
624
631
  return obj;
625
632
  };
626
633
  const read_spec = (kind) => {
@@ -639,16 +646,19 @@ var $;
639
646
  return undefined;
640
647
  case $mol_vary_spec.fp64: {
641
648
  const val = pack.getFloat64(++pos, true);
649
+ stream.push(val);
642
650
  pos += 8;
643
651
  return val;
644
652
  }
645
653
  case $mol_vary_spec.fp32: {
646
654
  const val = pack.getFloat32(++pos, true);
655
+ stream.push(val);
647
656
  pos += 4;
648
657
  return val;
649
658
  }
650
659
  case $mol_vary_spec.fp16: {
651
660
  const val = pack.getFloat16(++pos, true);
661
+ stream.push(val);
652
662
  pos += 2;
653
663
  return val;
654
664
  }
package/web.test.js CHANGED
@@ -1682,6 +1682,13 @@ var $;
1682
1682
  const box = [seven];
1683
1683
  check([box, box, seven], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
1684
1684
  },
1685
+ "vary pack dedup uint"($) {
1686
+ check([28, 28], [list | 2, uint | L1, 28, link | 0]);
1687
+ check([2n ** 64n, 2n ** 64n], [list | 2, sint | -LA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, link | 0]);
1688
+ },
1689
+ "vary pack dedup float"($) {
1690
+ check([1.5, 1.5], [list | 2, fp64, ...new Uint8Array(new Float64Array([1.5]).buffer), link | 0]);
1691
+ },
1685
1692
  "vary pack text"($) {
1686
1693
  check('foo', [text | 3, ...str('foo')]);
1687
1694
  const long = 'abcdefghijklmnopqrstuvwxyzЖЫ';