mol_vary 0.0.28 → 0.0.30

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/node.mjs CHANGED
@@ -251,15 +251,15 @@ var $;
251
251
  "use strict";
252
252
  var $;
253
253
  (function ($) {
254
- const catched = new WeakMap();
254
+ const catched = new WeakSet();
255
255
  function $mol_fail_catch(error) {
256
256
  if (typeof error !== 'object')
257
257
  return false;
258
258
  if ($mol_promise_like(error))
259
259
  $mol_fail_hidden(error);
260
- if (catched.get(error))
260
+ if (catched.has(error))
261
261
  return false;
262
- catched.set(error, true);
262
+ catched.add(error);
263
263
  return true;
264
264
  }
265
265
  $.$mol_fail_catch = $mol_fail_catch;
@@ -2361,22 +2361,18 @@ var $;
2361
2361
  const release = (size) => {
2362
2362
  capacity -= size;
2363
2363
  };
2364
- const dump_bint = (val) => {
2365
- const buf = $mol_bigint_encode(val);
2366
- acquire(buf.byteLength - 7);
2367
- if (buf.byteLength > 264)
2368
- $mol_fail(new Error('Number too high', { cause: { val } }));
2369
- buffer[pos++] = -$mol_vary_len.LA;
2370
- buffer[pos++] = buf.byteLength - 9;
2371
- buffer.set(buf, pos);
2372
- pos += buf.byteLength;
2373
- };
2374
2364
  const dump_unum = (tip, val) => {
2375
2365
  if (val < $mol_vary_len.L1) {
2376
2366
  buffer[pos++] = tip | Number(val);
2377
2367
  release(8);
2368
+ return;
2369
+ }
2370
+ if (tip == $mol_vary_tip.uint) {
2371
+ const offset = offsets.get(val);
2372
+ if (offset !== undefined)
2373
+ return dump_unum($mol_vary_tip.link, offset);
2378
2374
  }
2379
- else if (val < 2 ** 8) {
2375
+ if (val < 2 ** 8) {
2380
2376
  buffer[pos++] = tip | $mol_vary_len.L1;
2381
2377
  buffer[pos++] = Number(val);
2382
2378
  release(7);
@@ -2401,13 +2397,19 @@ var $;
2401
2397
  else {
2402
2398
  dump_bint(val);
2403
2399
  }
2400
+ if (tip == $mol_vary_tip.uint)
2401
+ offsets.set(val, offsets.size);
2404
2402
  };
2405
2403
  const dump_snum = (val) => {
2406
2404
  if (val > -$mol_vary_len.L1) {
2407
2405
  buffer[pos++] = Number(val);
2408
2406
  release(8);
2407
+ return;
2409
2408
  }
2410
- else if (val >= -(2 ** 7)) {
2409
+ const offset = offsets.get(val);
2410
+ if (offset !== undefined)
2411
+ return dump_unum($mol_vary_tip.link, offset);
2412
+ if (val >= -(2 ** 7)) {
2411
2413
  buffer[pos++] = -$mol_vary_len.L1;
2412
2414
  buffer[pos++] = Number(val);
2413
2415
  release(7);
@@ -2432,28 +2434,43 @@ var $;
2432
2434
  else {
2433
2435
  dump_bint(val);
2434
2436
  }
2437
+ offsets.set(val, offsets.size);
2438
+ };
2439
+ const dump_bint = (val) => {
2440
+ const buf = $mol_bigint_encode(val);
2441
+ if (buf.byteLength > 264)
2442
+ $mol_fail(new Error('Number too high', { cause: { val } }));
2443
+ acquire(buf.byteLength - 7);
2444
+ buffer[pos++] = -$mol_vary_len.LA;
2445
+ buffer[pos++] = buf.byteLength - 9;
2446
+ buffer.set(buf, pos);
2447
+ pos += buf.byteLength;
2448
+ };
2449
+ const dump_float = (val) => {
2450
+ const offset = offsets.get(val);
2451
+ if (offset !== undefined)
2452
+ return dump_unum($mol_vary_tip.link, offset);
2453
+ buffer[pos++] = $mol_vary_spec.fp64;
2454
+ pack.setFloat64(pos, val, true);
2455
+ pos += 8;
2456
+ offsets.set(val, offsets.size);
2435
2457
  };
2436
2458
  const dump_string = (val) => {
2437
- if (val.length) {
2438
- const offset = offsets.get(val);
2439
- if (offset !== undefined)
2440
- return dump_unum($mol_vary_tip.link, offset);
2441
- }
2459
+ const offset = offsets.get(val);
2460
+ if (offset !== undefined)
2461
+ return dump_unum($mol_vary_tip.link, offset);
2442
2462
  dump_unum($mol_vary_tip.text, val.length);
2443
2463
  acquire(val.length * 3);
2444
2464
  const len = $mol_charset_encode_to(val, buffer, pos);
2445
2465
  pos += len;
2446
2466
  release(val.length * 3 - len);
2447
- if (val.length)
2448
- offsets.set(val, offsets.size);
2467
+ offsets.set(val, offsets.size);
2449
2468
  return;
2450
2469
  };
2451
2470
  const dump_buffer = (val) => {
2452
- if (val.byteLength) {
2453
- const offset = offsets.get(val);
2454
- if (offset !== undefined)
2455
- return dump_unum($mol_vary_tip.link, offset);
2456
- }
2471
+ const offset = offsets.get(val);
2472
+ if (offset !== undefined)
2473
+ return dump_unum($mol_vary_tip.link, offset);
2457
2474
  dump_unum($mol_vary_tip.blob, val.byteLength);
2458
2475
  if (val instanceof Uint8Array)
2459
2476
  buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
@@ -2483,21 +2500,17 @@ var $;
2483
2500
  acquire(val.byteLength);
2484
2501
  buffer.set(src, pos);
2485
2502
  pos += val.byteLength;
2486
- if (val.byteLength)
2487
- offsets.set(val, offsets.size);
2503
+ offsets.set(val, offsets.size);
2488
2504
  };
2489
2505
  const dump_list = (val) => {
2490
- if (val.length) {
2491
- const offset = offsets.get(val);
2492
- if (offset !== undefined)
2493
- return dump_unum($mol_vary_tip.link, offset);
2494
- }
2506
+ const offset = offsets.get(val);
2507
+ if (offset !== undefined)
2508
+ return dump_unum($mol_vary_tip.link, offset);
2495
2509
  dump_unum($mol_vary_tip.list, val.length);
2496
2510
  acquire(val.length * 9);
2497
2511
  for (const item of val)
2498
2512
  dump(item);
2499
- if (val.length)
2500
- offsets.set(val, offsets.size);
2513
+ offsets.set(val, offsets.size);
2501
2514
  };
2502
2515
  const dump_object = (val) => {
2503
2516
  const offset = offsets.get(val);
@@ -2511,8 +2524,7 @@ var $;
2511
2524
  dump(item);
2512
2525
  for (const item of vals)
2513
2526
  dump(item);
2514
- if (vals.length)
2515
- offsets.set(val, offsets.size);
2527
+ offsets.set(val, offsets.size);
2516
2528
  };
2517
2529
  const dump = (val) => {
2518
2530
  switch (typeof val) {
@@ -2527,12 +2539,8 @@ var $;
2527
2539
  return;
2528
2540
  }
2529
2541
  case 'number': {
2530
- if (!Number.isInteger(val)) {
2531
- buffer[pos++] = $mol_vary_spec.fp64;
2532
- pack.setFloat64(pos, val, true);
2533
- pos += 8;
2534
- return;
2535
- }
2542
+ if (!Number.isInteger(val))
2543
+ return dump_float(val);
2536
2544
  }
2537
2545
  case 'bigint': {
2538
2546
  if (val < 0) {
@@ -2591,6 +2599,8 @@ var $;
2591
2599
  else {
2592
2600
  $mol_fail(new Error('Unsupported unum', { cause: { num } }));
2593
2601
  }
2602
+ if ((kind & 0b111_00000) === $mol_vary_tip.uint)
2603
+ stream.push(res);
2594
2604
  return res;
2595
2605
  };
2596
2606
  const read_snum = (kind) => {
@@ -2623,21 +2633,20 @@ var $;
2623
2633
  else {
2624
2634
  $mol_fail(new Error('Unsupported snum', { cause: { num } }));
2625
2635
  }
2636
+ stream.push(res);
2626
2637
  return res;
2627
2638
  };
2628
2639
  const read_text = (kind) => {
2629
2640
  const len = read_unum(kind);
2630
2641
  const [text, bytes] = $mol_charset_decode_from(buffer, pack.byteOffset + pos, len);
2631
2642
  pos += bytes;
2632
- if (text.length)
2633
- stream.push(text);
2643
+ stream.push(text);
2634
2644
  return text;
2635
2645
  };
2636
2646
  const read_buffer = (len, TypedArray) => {
2637
2647
  const bin = new TypedArray(buffer.slice(pos, pos + len).buffer);
2638
2648
  pos += len;
2639
- if (len)
2640
- stream.push(bin);
2649
+ stream.push(bin);
2641
2650
  return bin;
2642
2651
  };
2643
2652
  const read_blob = (kind) => {
@@ -2664,8 +2673,7 @@ var $;
2664
2673
  const list = new Array(len);
2665
2674
  for (let i = 0; i < len; ++i)
2666
2675
  list[i] = read_vary();
2667
- if (len)
2668
- stream.push(list);
2676
+ stream.push(list);
2669
2677
  return list;
2670
2678
  };
2671
2679
  const read_link = (kind) => {
@@ -2693,8 +2701,7 @@ var $;
2693
2701
  for (let i = 0; i < len; ++i)
2694
2702
  obj[keys[i]] = vals[i];
2695
2703
  }
2696
- if (vals.length)
2697
- stream.push(obj);
2704
+ stream.push(obj);
2698
2705
  return obj;
2699
2706
  };
2700
2707
  const read_spec = (kind) => {
@@ -2713,16 +2720,19 @@ var $;
2713
2720
  return undefined;
2714
2721
  case $mol_vary_spec.fp64: {
2715
2722
  const val = pack.getFloat64(++pos, true);
2723
+ stream.push(val);
2716
2724
  pos += 8;
2717
2725
  return val;
2718
2726
  }
2719
2727
  case $mol_vary_spec.fp32: {
2720
2728
  const val = pack.getFloat32(++pos, true);
2729
+ stream.push(val);
2721
2730
  pos += 4;
2722
2731
  return val;
2723
2732
  }
2724
2733
  case $mol_vary_spec.fp16: {
2725
2734
  const val = pack.getFloat16(++pos, true);
2735
+ stream.push(val);
2726
2736
  pos += 2;
2727
2737
  return val;
2728
2738
  }
package/node.test.js CHANGED
@@ -242,15 +242,15 @@ var $;
242
242
  "use strict";
243
243
  var $;
244
244
  (function ($) {
245
- const catched = new WeakMap();
245
+ const catched = new WeakSet();
246
246
  function $mol_fail_catch(error) {
247
247
  if (typeof error !== 'object')
248
248
  return false;
249
249
  if ($mol_promise_like(error))
250
250
  $mol_fail_hidden(error);
251
- if (catched.get(error))
251
+ if (catched.has(error))
252
252
  return false;
253
- catched.set(error, true);
253
+ catched.add(error);
254
254
  return true;
255
255
  }
256
256
  $.$mol_fail_catch = $mol_fail_catch;
@@ -2352,22 +2352,18 @@ var $;
2352
2352
  const release = (size) => {
2353
2353
  capacity -= size;
2354
2354
  };
2355
- const dump_bint = (val) => {
2356
- const buf = $mol_bigint_encode(val);
2357
- acquire(buf.byteLength - 7);
2358
- if (buf.byteLength > 264)
2359
- $mol_fail(new Error('Number too high', { cause: { val } }));
2360
- buffer[pos++] = -$mol_vary_len.LA;
2361
- buffer[pos++] = buf.byteLength - 9;
2362
- buffer.set(buf, pos);
2363
- pos += buf.byteLength;
2364
- };
2365
2355
  const dump_unum = (tip, val) => {
2366
2356
  if (val < $mol_vary_len.L1) {
2367
2357
  buffer[pos++] = tip | Number(val);
2368
2358
  release(8);
2359
+ return;
2360
+ }
2361
+ if (tip == $mol_vary_tip.uint) {
2362
+ const offset = offsets.get(val);
2363
+ if (offset !== undefined)
2364
+ return dump_unum($mol_vary_tip.link, offset);
2369
2365
  }
2370
- else if (val < 2 ** 8) {
2366
+ if (val < 2 ** 8) {
2371
2367
  buffer[pos++] = tip | $mol_vary_len.L1;
2372
2368
  buffer[pos++] = Number(val);
2373
2369
  release(7);
@@ -2392,13 +2388,19 @@ var $;
2392
2388
  else {
2393
2389
  dump_bint(val);
2394
2390
  }
2391
+ if (tip == $mol_vary_tip.uint)
2392
+ offsets.set(val, offsets.size);
2395
2393
  };
2396
2394
  const dump_snum = (val) => {
2397
2395
  if (val > -$mol_vary_len.L1) {
2398
2396
  buffer[pos++] = Number(val);
2399
2397
  release(8);
2398
+ return;
2400
2399
  }
2401
- else if (val >= -(2 ** 7)) {
2400
+ const offset = offsets.get(val);
2401
+ if (offset !== undefined)
2402
+ return dump_unum($mol_vary_tip.link, offset);
2403
+ if (val >= -(2 ** 7)) {
2402
2404
  buffer[pos++] = -$mol_vary_len.L1;
2403
2405
  buffer[pos++] = Number(val);
2404
2406
  release(7);
@@ -2423,28 +2425,43 @@ var $;
2423
2425
  else {
2424
2426
  dump_bint(val);
2425
2427
  }
2428
+ offsets.set(val, offsets.size);
2429
+ };
2430
+ const dump_bint = (val) => {
2431
+ const buf = $mol_bigint_encode(val);
2432
+ if (buf.byteLength > 264)
2433
+ $mol_fail(new Error('Number too high', { cause: { val } }));
2434
+ acquire(buf.byteLength - 7);
2435
+ buffer[pos++] = -$mol_vary_len.LA;
2436
+ buffer[pos++] = buf.byteLength - 9;
2437
+ buffer.set(buf, pos);
2438
+ pos += buf.byteLength;
2439
+ };
2440
+ const dump_float = (val) => {
2441
+ const offset = offsets.get(val);
2442
+ if (offset !== undefined)
2443
+ return dump_unum($mol_vary_tip.link, offset);
2444
+ buffer[pos++] = $mol_vary_spec.fp64;
2445
+ pack.setFloat64(pos, val, true);
2446
+ pos += 8;
2447
+ offsets.set(val, offsets.size);
2426
2448
  };
2427
2449
  const dump_string = (val) => {
2428
- if (val.length) {
2429
- const offset = offsets.get(val);
2430
- if (offset !== undefined)
2431
- return dump_unum($mol_vary_tip.link, offset);
2432
- }
2450
+ const offset = offsets.get(val);
2451
+ if (offset !== undefined)
2452
+ return dump_unum($mol_vary_tip.link, offset);
2433
2453
  dump_unum($mol_vary_tip.text, val.length);
2434
2454
  acquire(val.length * 3);
2435
2455
  const len = $mol_charset_encode_to(val, buffer, pos);
2436
2456
  pos += len;
2437
2457
  release(val.length * 3 - len);
2438
- if (val.length)
2439
- offsets.set(val, offsets.size);
2458
+ offsets.set(val, offsets.size);
2440
2459
  return;
2441
2460
  };
2442
2461
  const dump_buffer = (val) => {
2443
- if (val.byteLength) {
2444
- const offset = offsets.get(val);
2445
- if (offset !== undefined)
2446
- return dump_unum($mol_vary_tip.link, offset);
2447
- }
2462
+ const offset = offsets.get(val);
2463
+ if (offset !== undefined)
2464
+ return dump_unum($mol_vary_tip.link, offset);
2448
2465
  dump_unum($mol_vary_tip.blob, val.byteLength);
2449
2466
  if (val instanceof Uint8Array)
2450
2467
  buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
@@ -2474,21 +2491,17 @@ var $;
2474
2491
  acquire(val.byteLength);
2475
2492
  buffer.set(src, pos);
2476
2493
  pos += val.byteLength;
2477
- if (val.byteLength)
2478
- offsets.set(val, offsets.size);
2494
+ offsets.set(val, offsets.size);
2479
2495
  };
2480
2496
  const dump_list = (val) => {
2481
- if (val.length) {
2482
- const offset = offsets.get(val);
2483
- if (offset !== undefined)
2484
- return dump_unum($mol_vary_tip.link, offset);
2485
- }
2497
+ const offset = offsets.get(val);
2498
+ if (offset !== undefined)
2499
+ return dump_unum($mol_vary_tip.link, offset);
2486
2500
  dump_unum($mol_vary_tip.list, val.length);
2487
2501
  acquire(val.length * 9);
2488
2502
  for (const item of val)
2489
2503
  dump(item);
2490
- if (val.length)
2491
- offsets.set(val, offsets.size);
2504
+ offsets.set(val, offsets.size);
2492
2505
  };
2493
2506
  const dump_object = (val) => {
2494
2507
  const offset = offsets.get(val);
@@ -2502,8 +2515,7 @@ var $;
2502
2515
  dump(item);
2503
2516
  for (const item of vals)
2504
2517
  dump(item);
2505
- if (vals.length)
2506
- offsets.set(val, offsets.size);
2518
+ offsets.set(val, offsets.size);
2507
2519
  };
2508
2520
  const dump = (val) => {
2509
2521
  switch (typeof val) {
@@ -2518,12 +2530,8 @@ var $;
2518
2530
  return;
2519
2531
  }
2520
2532
  case 'number': {
2521
- if (!Number.isInteger(val)) {
2522
- buffer[pos++] = $mol_vary_spec.fp64;
2523
- pack.setFloat64(pos, val, true);
2524
- pos += 8;
2525
- return;
2526
- }
2533
+ if (!Number.isInteger(val))
2534
+ return dump_float(val);
2527
2535
  }
2528
2536
  case 'bigint': {
2529
2537
  if (val < 0) {
@@ -2582,6 +2590,8 @@ var $;
2582
2590
  else {
2583
2591
  $mol_fail(new Error('Unsupported unum', { cause: { num } }));
2584
2592
  }
2593
+ if ((kind & 0b111_00000) === $mol_vary_tip.uint)
2594
+ stream.push(res);
2585
2595
  return res;
2586
2596
  };
2587
2597
  const read_snum = (kind) => {
@@ -2614,21 +2624,20 @@ var $;
2614
2624
  else {
2615
2625
  $mol_fail(new Error('Unsupported snum', { cause: { num } }));
2616
2626
  }
2627
+ stream.push(res);
2617
2628
  return res;
2618
2629
  };
2619
2630
  const read_text = (kind) => {
2620
2631
  const len = read_unum(kind);
2621
2632
  const [text, bytes] = $mol_charset_decode_from(buffer, pack.byteOffset + pos, len);
2622
2633
  pos += bytes;
2623
- if (text.length)
2624
- stream.push(text);
2634
+ stream.push(text);
2625
2635
  return text;
2626
2636
  };
2627
2637
  const read_buffer = (len, TypedArray) => {
2628
2638
  const bin = new TypedArray(buffer.slice(pos, pos + len).buffer);
2629
2639
  pos += len;
2630
- if (len)
2631
- stream.push(bin);
2640
+ stream.push(bin);
2632
2641
  return bin;
2633
2642
  };
2634
2643
  const read_blob = (kind) => {
@@ -2655,8 +2664,7 @@ var $;
2655
2664
  const list = new Array(len);
2656
2665
  for (let i = 0; i < len; ++i)
2657
2666
  list[i] = read_vary();
2658
- if (len)
2659
- stream.push(list);
2667
+ stream.push(list);
2660
2668
  return list;
2661
2669
  };
2662
2670
  const read_link = (kind) => {
@@ -2684,8 +2692,7 @@ var $;
2684
2692
  for (let i = 0; i < len; ++i)
2685
2693
  obj[keys[i]] = vals[i];
2686
2694
  }
2687
- if (vals.length)
2688
- stream.push(obj);
2695
+ stream.push(obj);
2689
2696
  return obj;
2690
2697
  };
2691
2698
  const read_spec = (kind) => {
@@ -2704,16 +2711,19 @@ var $;
2704
2711
  return undefined;
2705
2712
  case $mol_vary_spec.fp64: {
2706
2713
  const val = pack.getFloat64(++pos, true);
2714
+ stream.push(val);
2707
2715
  pos += 8;
2708
2716
  return val;
2709
2717
  }
2710
2718
  case $mol_vary_spec.fp32: {
2711
2719
  const val = pack.getFloat32(++pos, true);
2720
+ stream.push(val);
2712
2721
  pos += 4;
2713
2722
  return val;
2714
2723
  }
2715
2724
  case $mol_vary_spec.fp16: {
2716
2725
  const val = pack.getFloat16(++pos, true);
2726
+ stream.push(val);
2717
2727
  pos += 2;
2718
2728
  return val;
2719
2729
  }
@@ -4921,6 +4931,13 @@ var $;
4921
4931
  const box = [seven];
4922
4932
  check([box, box, seven], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
4923
4933
  },
4934
+ "vary pack dedup uint"($) {
4935
+ check([28, 28], [list | 2, uint | L1, 28, link | 0]);
4936
+ check([2n ** 64n, 2n ** 64n], [list | 2, sint | -LA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, link | 0]);
4937
+ },
4938
+ "vary pack dedup float"($) {
4939
+ check([1.5, 1.5], [list | 2, fp64, ...new Uint8Array(new Float64Array([1.5]).buffer), link | 0]);
4940
+ },
4924
4941
  "vary pack text"($) {
4925
4942
  check('foo', [text | 3, ...str('foo')]);
4926
4943
  const long = 'abcdefghijklmnopqrstuvwxyzЖЫ';