nv-facutil-slct-ta 1.1.3 → 1.1.5

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.
Files changed (2) hide show
  1. package/index.js +158 -45
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,6 +8,80 @@ const tcate = (o)=> {
8
8
  return 'other'
9
9
  }
10
10
 
11
+ // 判断是否为有符号整数类型(包括大整数)
12
+ const is_signed_int_cls = (TypedArrayClass) => {
13
+ return (
14
+ TypedArrayClass === Int8Array ||
15
+ TypedArrayClass === Int16Array ||
16
+ TypedArrayClass === Int32Array ||
17
+ TypedArrayClass === BigInt64Array // 有符号大整数(64位)
18
+ );
19
+ };
20
+
21
+ // 判断是否为无符号整数类型(包括大整数和 clamped 类型)
22
+ const is_unsigned_int_cls = (TypedArrayClass) => {
23
+ return (
24
+ TypedArrayClass === Uint8Array || // 无符号8位整数
25
+ TypedArrayClass === Uint8ClampedArray || // 无符号8位钳位整数(特殊无符号)
26
+ TypedArrayClass === Uint16Array || // 无符号16位整数
27
+ TypedArrayClass === Uint32Array || // 无符号32位整数
28
+ TypedArrayClass === BigUint64Array // 无符号大整数(64位)
29
+ );
30
+ };
31
+
32
+ function get_display_name_prefix(TypedArrayClass) {
33
+ const byteSize = TypedArrayClass.BYTES_PER_ELEMENT;
34
+ // 判断是否为有符号类型(需要添加"Half"前缀)
35
+
36
+
37
+ // 基础名称映射
38
+ let baseName;
39
+ if (byteSize === 1) {
40
+ baseName = "Tiny";
41
+ } else if (byteSize === 2) {
42
+ baseName = "Small";
43
+ } else if (byteSize === 4) {
44
+ baseName = "Normal";
45
+ } else {
46
+ baseName = "Large";
47
+ }
48
+
49
+ // 有符号类型添加"Half"前缀
50
+ return is_signed_int_cls(TypedArrayClass) ? `Half${baseName}` : baseName;
51
+ }
52
+
53
+ const DUMP_ONE_BYTE_TYPE_MP = {
54
+ 0: Uint8Array,
55
+ 1: Uint16Array,
56
+ 2: Uint32Array,
57
+ 3: BigUint64Array, // js version NOT supported
58
+ //-----------------------------------------------------
59
+ 4: Int8Array,
60
+ 5: Int16Array,
61
+ 6: Int32Array,
62
+ 7: BigInt64Array, // js version NOT supported
63
+ 255: Float64Array,
64
+ }
65
+
66
+ const CLS2BYT = new Map([
67
+ [Uint8Array,0],
68
+ [Uint16Array,1],
69
+ [Uint32Array,2],
70
+ [BigUint64Array,3],
71
+ //-----------------------------------------------------------
72
+ [Int8Array,4],
73
+ [Int16Array,5],
74
+ [Int32Array,6],
75
+ [BigInt64Array,7],
76
+ [Float64Array,255]
77
+ ]);
78
+
79
+
80
+
81
+
82
+
83
+
84
+
11
85
 
12
86
 
13
87
  ////
@@ -137,24 +211,44 @@ function to_u8avw(ta) {
137
211
  function cp_rtrn_u8a(ta) {return to_u8avw(ta).slice(0)}
138
212
 
139
213
 
140
-
141
-
142
- function calc_max_incr_cnt(Cls,curr_cnt) {
143
- if(Cls === Float64Array) {
144
- return 2**53 - curr_cnt;
145
- } else if(Cls === BigUint64Array) { //JS version NOT supported
146
- var limit = 2n** (8n* BigInt(Cls.BYTES_PER_ELEMENT));
147
- return limit - BigInt(curr_cnt);
148
- } else {
149
- var limit = 2** (8*Cls.BYTES_PER_ELEMENT);
150
- return limit - curr_cnt;
214
+ function calc_max_incr_cnt(Cls, curr_cnt) {
215
+ // 处理 Float64Array
216
+ if (Cls === Float64Array) {
217
+ return Number.MAX_SAFE_INTEGER - curr_cnt; // 2^53-1 是最大安全整数
218
+ }
219
+ // 处理 BigUint64Array
220
+ else if (Cls === BigUint64Array) {
221
+ const n = 8n * BigInt(Cls.BYTES_PER_ELEMENT); // 64
222
+ const limit = 2n ** n; // 2^64
223
+ return limit - 1n- BigInt(curr_cnt);
224
+ }
225
+ // 处理有符号整数类型
226
+ else if (Cls === Int8Array) {
227
+ return 127 - curr_cnt; // 2^7 - 1
228
+ }
229
+ else if (Cls === Int16Array) {
230
+ return 32767 - curr_cnt; // 2^15 - 1
231
+ }
232
+ else if (Cls === Int32Array) {
233
+ return 2147483647 - curr_cnt; // 2^31 - 1
234
+ }
235
+ // 处理无符号整数类型(使用原逻辑)
236
+ else {
237
+ // 计算无符号整数最大值:2^(8*字节数) - 1
238
+ const n = 8 * Cls.BYTES_PER_ELEMENT;
239
+ const limit = 2 ** n; // 2^8, 2^16, 2^32
240
+ return limit -1 - curr_cnt;
151
241
  }
152
242
  }
153
243
 
244
+
154
245
  function get_ta_cls_with_max_cnt(cnt,using_u64_when_gt_safe_f64_int=false) {
155
246
  cnt = BigInt(cnt);
156
- if(cnt<=255n) { return Uint8Array;
247
+ if(cnt <= 127n) { return Int8Array;
248
+ } else if(cnt<=255n) { return Uint8Array;
249
+ } else if(cnt<=32767n) { return Int16Array;
157
250
  } else if(cnt<=65535n) { return Uint16Array;
251
+ } else if(cnt<=2147483647n) { return Int32Array;
158
252
  } else if(cnt<=(2n**32n-1n)) { return Uint32Array;
159
253
  } else if(cnt<=(2n**53n-1n)) {
160
254
  return using_u64_when_gt_safe_f64_int?BigUint64Array:Float64Array;
@@ -163,38 +257,21 @@ function get_ta_cls_with_max_cnt(cnt,using_u64_when_gt_safe_f64_int=false) {
163
257
  }
164
258
  }
165
259
 
166
- const DUMP_ONE_BYTE_TYPE_MP = {
167
- 0: Uint8Array,
168
- 1: Uint16Array,
169
- 2: Uint32Array,
170
- 3: BigUint64Array, // js version NOT supported
171
- 255: Float64Array,
172
- }
173
-
174
- const CLS2BYT = new Map([
175
- [Uint8Array,0],
176
- [Uint16Array,1],
177
- [Uint32Array,2],
178
- [BigUint64Array,3],
179
- [Float64Array,255]
180
- ]);
181
260
 
182
261
  function get_byt_from_cls(Cls) {
183
262
  return CLS2BYT.get(Cls)
184
263
  }
185
-
186
- function get_cls_from_dump_fst_byte(byt) {
264
+ function get_cls_from_byt(byt) {
187
265
  return DUMP_ONE_BYTE_TYPE_MP[byt]??Float64Array
188
266
  }
189
-
190
- function get_fst_byte_for_dump(Cls) {
191
- if(Cls === Uint8Array) return 0;
192
- if(Cls === Uint16Array) return 1;
193
- if(Cls === Uint32Array) return 2;
194
- if(Cls === BigUint64Array) return 3; // js version NOT supported
195
- return 255;
267
+ function get_fst_byte_for_dump(Cls) { //旧代码兼容
268
+ return CLS2BYT.get(Cls)
269
+ }
270
+ function get_cls_from_dump_fst_byte(byt) { //旧代码兼容
271
+ return DUMP_ONE_BYTE_TYPE_MP[byt]??Float64Array
196
272
  }
197
273
 
274
+
198
275
  function set_fst_byte_for_dump(ta) {
199
276
  var u8a = new Uint8Array(ta.buffer,ta.byteOffset,1);
200
277
  u8a[0] = get_fst_byte_for_dump(ta.constructor);
@@ -211,14 +288,6 @@ function get_nth_byte(ta,I) {
211
288
  }
212
289
 
213
290
 
214
- function get_display_name_prefix(TypedArrayClass) {
215
- if(TypedArrayClass.BYTES_PER_ELEMENT === 1) {return "Tiny"}
216
- if(TypedArrayClass.BYTES_PER_ELEMENT === 2) {return "Small"}
217
- if(TypedArrayClass.BYTES_PER_ELEMENT === 4) {return "Normal"}
218
- return "Large";
219
- }
220
-
221
-
222
291
  //------------------------------------------------------------------------------
223
292
 
224
293
 
@@ -484,8 +553,48 @@ const write_dv_with_cls = (dv, Cls, offset, value, little_endian = true) => {
484
553
  };
485
554
 
486
555
 
556
+ const fmt_underlying = (underlying)=>{
557
+ if(underlying === null || underlying === undefined) {
558
+ return {
559
+ src:null,
560
+ offset:0
561
+ }
562
+ } else if(is_ablike(underlying)) {
563
+ return {
564
+ src:underlying,
565
+ offset:0,
566
+ }
567
+ } else {
568
+ return {
569
+ src:underlying.src??null,
570
+ offset:underlying.offset??0
571
+ }
572
+ }
573
+ }
574
+
575
+ //inline 为true 时,会自动忽略 .ptr 此时ptr自动计算
576
+ const fmt_data = (data)=>{
577
+ if(data === null || data === true || data === undefined || Boolean(data?.inline) === true) {
578
+ return {
579
+ ptr: undefined, //自动计算
580
+ inline:true,
581
+ }
582
+ } else if(typeof(data) === "number" || typeof(data)==="bigint") { //只填写 ptr 后视作非inline
583
+ return {
584
+ ptr: parseInt(Number(data)),
585
+ inline:false,
586
+ }
587
+ } else {
588
+ return {
589
+ ptr: parseInt(Number(data.ptr??0)),
590
+ inline: Boolean(data.inline)
591
+ }
592
+ }
593
+ }
594
+
595
+
487
596
  module.exports = {
488
- tcate,
597
+ tcate, is_signed_int_cls , is_unsigned_int_cls,
489
598
  ////
490
599
  check_sz_and_algn,
491
600
  calc_algn_si,
@@ -493,6 +602,7 @@ module.exports = {
493
602
  calc_max_incr_cnt,
494
603
  get_ta_cls_with_max_cnt,
495
604
  get_byt_from_cls,
605
+ get_cls_from_byt,
496
606
  get_cls_from_dump_fst_byte,
497
607
  get_fst_byte_for_dump,
498
608
  set_fst_byte_for_dump,
@@ -539,4 +649,7 @@ module.exports = {
539
649
  min_tacls,max_tacls,
540
650
  read_dv_with_cls,
541
651
  write_dv_with_cls,
652
+ ////
653
+ fmt_underlying,
654
+ fmt_data,
542
655
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-facutil-slct-ta",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"