affinirum 1.5.0 → 1.5.1

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 (45) hide show
  1. package/README.md +21 -12
  2. package/dst/ParserState.js +23 -79
  3. package/dst/atom/ArrayAtom.js +4 -1
  4. package/dst/cjs/ParserState.js +22 -78
  5. package/dst/cjs/atom/ArrayAtom.js +4 -1
  6. package/dst/cjs/native/Aggregable.js +1 -1
  7. package/dst/cjs/native/Boolean.js +3 -1
  8. package/dst/cjs/native/Buffer.js +32 -3
  9. package/dst/cjs/native/Enumerable.js +5 -3
  10. package/dst/cjs/native/Float.js +17 -5
  11. package/dst/cjs/native/Integer.js +41 -13
  12. package/dst/cjs/native/Iterable.js +6 -4
  13. package/dst/cjs/native/Number.js +45 -1
  14. package/dst/cjs/native/String.js +68 -22
  15. package/dst/cjs/native/Timestamp.js +74 -17
  16. package/dst/cjs/native/Unknown.js +1 -1
  17. package/dst/cjs/native/notation/AN.js +152 -11
  18. package/dst/cjs/native/notation/IP.js +1 -1
  19. package/dst/cjs/native/notation/JSON.js +1 -1
  20. package/dst/cjs/node/BlockNode.js +5 -4
  21. package/dst/cjs/node/CallNode.js +7 -5
  22. package/dst/cjs/node/SwitchNode.js +7 -9
  23. package/dst/native/Aggregable.js +1 -1
  24. package/dst/native/Boolean.js +3 -1
  25. package/dst/native/Buffer.d.ts +2 -0
  26. package/dst/native/Buffer.js +29 -2
  27. package/dst/native/Enumerable.js +5 -3
  28. package/dst/native/Float.js +17 -5
  29. package/dst/native/Integer.js +41 -13
  30. package/dst/native/Iterable.js +6 -4
  31. package/dst/native/Number.d.ts +4 -0
  32. package/dst/native/Number.js +40 -0
  33. package/dst/native/String.d.ts +2 -7
  34. package/dst/native/String.js +65 -14
  35. package/dst/native/Timestamp.d.ts +4 -2
  36. package/dst/native/Timestamp.js +71 -16
  37. package/dst/native/Unknown.js +1 -1
  38. package/dst/native/notation/AN.d.ts +1 -0
  39. package/dst/native/notation/AN.js +152 -12
  40. package/dst/native/notation/IP.js +1 -1
  41. package/dst/native/notation/JSON.js +1 -1
  42. package/dst/node/BlockNode.js +5 -4
  43. package/dst/node/CallNode.js +7 -5
  44. package/dst/node/SwitchNode.js +7 -9
  45. package/package.json +1 -1
package/README.md CHANGED
@@ -23,7 +23,7 @@ Target: ES2022 [browser+Node.js][ESM+CJS].
23
23
  ## Specifications
24
24
 
25
25
  A script may contain multiple expressions separated by semicolons, as well as blocks of statements.
26
- The value of a block is defined by the value of its last expression.
26
+ The value of a block is defined by the value of its last expression. An empty block has type **void** and evaluates to JavaScript `undefined`.
27
27
  - Scientific notation is supported for floating point numbers, like *0.1281e+2*.
28
28
  - ISO Timestamps prefixed with **@**, like *@2025-05-11T19:09:21.320Z*.
29
29
  - Hexadecimal buffer values are enclosed in backticks (**\`**), like *\`10ab0901\`*.
@@ -94,6 +94,12 @@ substitutability or prove that every accepted expression is safe for every possi
94
94
  function parameter may receive **null**, `undefined`, or be omitted.
95
95
  - An optional type such as **integer?** is the union **void | integer**. Optional parameters may receive **null**, `undefined`,
96
96
  or be omitted.
97
+ - A call with known argument types must match one complete function overload. Parameter matches from separate overloads
98
+ are not combined to accept a call.
99
+ - Object types are open structural constraints: required properties and their values are checked, while additional
100
+ properties are allowed.
101
+ - Tuple types are prefix constraints: the declared item types are checked in order, optional trailing items may be
102
+ omitted, and additional items are allowed.
97
103
  - Function compatibility is deliberately flexible. The accepted argument-count ranges only need to overlap;
98
104
  one function's complete arity range does not need to contain the other's.
99
105
  - A callback may declare fewer parameters than the caller provides. Additional arguments are ignored at runtime,
@@ -159,7 +165,7 @@ Casts can be followed by property access, index access, function calls, or anoth
159
165
  - Boolean conjunction (Logical AND) assignment: **&=**
160
166
  - Arithmetic addition assignment: **+=**
161
167
  - Arithmetic subtraction assignment: **-=**
162
- - Arithmetic multiplication assignment: ***=**
168
+ - Arithmetic multiplication assignment: **\*=**
163
169
  - Arithmetic division assignment: **/=**
164
170
  - Arithmetic remainder assignment: **%=**
165
171
 
@@ -184,7 +190,7 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
184
190
  #### Timestamp
185
191
  - **Timestamp.Now():timestamp** — Current date and time
186
192
  - **Timestamp.Epoch(value: float | integer, epoch: timestamp?):timestamp** — Create timestamp from milliseconds
187
- - **Timestamp.Decode(value: buffer?, encoding: string?, offset: integer?):timestamp?** — Decode a timestamp using `i64` (default) or `i64le`
193
+ - **Timestamp.Decode(value: buffer?, encoding: string?, offset: integer?):timestamp?** — Decode a timestamp using `i32` or `i32le` minutes, or `i64` (default) or `i64le` milliseconds since the Unix epoch
188
194
  - **Timestamp.Parse(value: string?):timestamp?** — Parse a string as a timestamp
189
195
 
190
196
  #### Float
@@ -225,7 +231,7 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
225
231
 
226
232
  #### Array
227
233
  - **Array.Join(values: ...array):array** — Join arrays of any depths into a single array
228
- - **Array.Range(inclusiveFrom: integer, exclusiveTo: integer):array** — New array filled with integers in range
234
+ - **Array.Range(a: integer, b: integer):array** — Integers from the lower endpoint (inclusive) to the higher endpoint (exclusive)
229
235
  - **Array.Unique(value: array):array** — Array of unique values
230
236
  - **Array.Intersection(a1: array, a2: array):array** — Common values from two arrays
231
237
  - **Array.Difference(a1: array, a2: array):array** — Symmetrical difference between arrays
@@ -252,11 +258,12 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
252
258
  - **??.Equal(value: ??):boolean** — Equals to
253
259
  - **??.Unequal(value: ??):boolean** — Not equals to
254
260
  - **(boolean | buffer | function).Encode():buffer** — Encode value to buffer
255
- - **(void | timestamp | float | integer | string | array | object).Encode(encoding: string?):buffer** — Encode a value; defaults are `i64` for timestamps and integers, `f64` for floats, and `utf8` for strings
261
+ - **(void | timestamp | float | integer | string | array | object).Encode(encoding: string?):buffer** — Encode a value; timestamps support `i32` and `i32le` minutes or `i64` and `i64le` milliseconds, while defaults are `i64` for timestamps and integers, `f64` for floats, and `utf8` for strings
256
262
  - **(boolean | string | function).Format():string** — Format value to string
257
263
  - **(void | timestamp | float | integer | buffer | array | object).Format(formatting: string?):string** — Format a value; buffers use `hex` by default and also support `base64`
258
264
 
259
265
  #### Aggregable Functions
266
+ - **float.Add(value: float):float** — Add a float
260
267
  - **float.Add(value: integer):float** — Add an integer
261
268
  - **integer.Add(value: float):float** — Add a float
262
269
  - **integer.Add(value: integer):integer** — Add an integer
@@ -287,12 +294,12 @@ Invalid non-void inputs are function-specific and may raise an evaluation error.
287
294
  - **buffer.Slice(start: integer?, end: integer?):buffer** — Slice section
288
295
  - **string.Slice(start: integer?, end: integer?):string** — Slice section
289
296
  - **array.Slice(start: integer?, end: integer?):array** — Slice section
290
- - **buffer.Splice(start: integer, remove: integer, inject: ...(buffer | string | array)):buffer** — Splice section
291
- - **string.Splice(start: integer, remove: integer, inject: ...(buffer | string | array)):string** — Splice section
292
- - **array.Splice(start: integer, remove: integer, inject: ...(buffer | string | array)):array** — Splice section
293
- - **buffer.Inject(start: integer, inject: ...(buffer | string | array)):buffer** — Inject section
294
- - **string.Inject(start: integer, inject: ...(buffer | string | array)):string** — Inject section
295
- - **array.Inject(start: integer, inject: ...(buffer | string | array)):array** — Inject section
297
+ - **buffer.Splice(start: integer, remove: integer, inject: ...buffer):buffer** — Splice section
298
+ - **string.Splice(start: integer, remove: integer, inject: ...string):string** — Splice section
299
+ - **array.Splice(start: integer, remove: integer, inject: ...array):array** — Splice section
300
+ - **buffer.Inject(start: integer, inject: ...buffer):buffer** — Inject section
301
+ - **string.Inject(start: integer, inject: ...string):string** — Inject section
302
+ - **array.Inject(start: integer, inject: ...array):array** — Inject section
296
303
 
297
304
  #### Iterable Functions
298
305
  - **(buffer | string | array | object).Length():integer** — Get length
@@ -399,7 +406,9 @@ a JavaScript value using Affinirum Notation.
399
406
 
400
407
  JavaScript `number` values map to **float**, while **integer** values map to `bigint`. Timestamps use `Date`, buffers use
401
408
  `ArrayBuffer`, and a **void** result is returned as `undefined`. Arrays, objects, and functions use their corresponding
402
- JavaScript values. Injected values are checked against the types inferred or declared during compilation.
409
+ JavaScript values. Injected non-function values are checked against the types inferred or declared during compilation.
410
+ Injected functions are checked to be callable; their JavaScript parameter behavior and return values are trusted according
411
+ to their declared Affinirum types.
403
412
 
404
413
  Use **val** for a local value that cannot be reassigned and **var** for a reassignable local variable. Statements are
405
414
  separated with semicolons.
@@ -1,11 +1,11 @@
1
1
  import { funcOr, funcAnd, funcNot } from "./native/Boolean.js";
2
- import { parseBuffer } from "./native/Buffer.js";
2
+ import { parseLiteralBuffer } from "./native/Buffer.js";
3
3
  import { funcAdd } from "./native/Aggregable.js";
4
- import { isSignSymbol, isTokenStartSymbol, isNumericSymbol, isTokenSymbol, isDateSymbol, isTimeSymbol, isDateTimeSeparatorSymbol, replaceWith } from "./native/String.js";
5
- import { Constant } from "./Constant.js";
6
- import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower } from "./native/Number.js";
4
+ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower, isNumericSymbol, parseLiteralNumber } from "./native/Number.js";
7
5
  import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
8
- import { boundInteger } from "./native/Integer.js";
6
+ import { isTokenStartSymbol, isTokenSymbol, parseLiteralString } from "./native/String.js";
7
+ import { parseLiteralTimestamp } from "./native/Timestamp.js";
8
+ import { Constant } from "./Constant.js";
9
9
  import { Type } from "./Type.js";
10
10
  import { ParserFrame } from "./ParserFrame.js";
11
11
  class Literal {
@@ -450,53 +450,25 @@ export class ParserState extends ParserFrame {
450
450
  break;
451
451
  }
452
452
  break;
453
- case "@":
454
- while (isDateSymbol(this._expr.charAt(this._end))) {
455
- ++this._end;
456
- }
457
- if (isDateTimeSeparatorSymbol(this._expr.charAt(this._end))) {
458
- ++this._end;
459
- while (isTimeSymbol(this._expr.charAt(this._end))) {
460
- ++this._end;
461
- }
462
- if (this._expr.charAt(this._end) === "."
463
- && isNumericSymbol(this._expr.charAt(this._end + 1))) {
464
- ++this._end;
465
- while (isNumericSymbol(this._expr.charAt(this._end))) {
466
- ++this._end;
467
- }
468
- if (this._expr.charAt(this._end) === "Z") {
469
- ++this._end;
470
- }
471
- }
472
- }
473
- this._fragment = new Literal(new Date(this._expr.substring(this._start + 1, this._end)));
453
+ case "@": {
454
+ const [ts, tsEnd] = parseLiteralTimestamp(this._expr, this._start);
455
+ this._fragment = new Literal(ts);
456
+ this._end = tsEnd;
474
457
  break;
475
- case "`":
476
- while (this._expr.charAt(this._end) !== ""
477
- && this._expr.charAt(this._end) !== c) {
478
- ++this._end;
479
- }
480
- if (this._end >= this._expr.length) {
481
- this._start = this._expr.length;
482
- throw new Error(`missing closing buffer quotation mark ${c}`);
483
- }
484
- this._fragment = new Literal(parseBuffer(replaceWith(this._expr.substring(this._start + 1, this._end), "", " ", "\t", "\n")));
485
- ++this._end;
458
+ }
459
+ case "`": {
460
+ const [buf, bufEnd] = parseLiteralBuffer(this._expr, this._start);
461
+ this._fragment = new Literal(buf);
462
+ this._end = bufEnd;
486
463
  break;
464
+ }
487
465
  case "\"":
488
- case "'":
489
- while (this._expr.charAt(this._end) !== ""
490
- && this._expr.charAt(this._end) !== c) {
491
- ++this._end;
492
- }
493
- if (this._end >= this._expr.length) {
494
- this._start = this._expr.length;
495
- throw new Error(`missing closing string quotation mark ${c}`);
496
- }
497
- this._fragment = new Literal(replaceWith(this._expr.substring(this._start + 1, this._end), "", "\\\n"));
498
- ++this._end;
466
+ case "'": {
467
+ const [str, strEnd] = parseLiteralString(this._expr, this._start);
468
+ this._fragment = new Literal(str);
469
+ this._end = strEnd;
499
470
  break;
471
+ }
500
472
  default:
501
473
  if (isTokenStartSymbol(c)) {
502
474
  while (isTokenSymbol(this._expr.charAt(this._end))) {
@@ -574,37 +546,9 @@ export class ParserState extends ParserFrame {
574
546
  }
575
547
  }
576
548
  else if (isNumericSymbol(c)) {
577
- let integer = true;
578
- while (isNumericSymbol(this._expr.charAt(this._end))) {
579
- ++this._end;
580
- }
581
- if (this._expr.charAt(this._end) === ".") {
582
- ++this._end;
583
- if (isNumericSymbol(this._expr.charAt(this._end))) {
584
- ++this._end;
585
- while (isNumericSymbol(this._expr.charAt(this._end))) {
586
- ++this._end;
587
- }
588
- integer = false;
589
- }
590
- else {
591
- --this._end;
592
- }
593
- }
594
- if (this._expr.charAt(this._end) === "e") {
595
- ++this._end;
596
- if (isNumericSymbol(this._expr.charAt(this._end))
597
- || isSignSymbol(this._expr.charAt(this._end))) {
598
- ++this._end;
599
- while (isNumericSymbol(this._expr.charAt(this._end))) {
600
- ++this._end;
601
- }
602
- }
603
- integer = false;
604
- }
605
- this._fragment = integer
606
- ? new Literal(boundInteger(BigInt(this._expr.substring(this._start, this._end))))
607
- : new Literal(parseFloat(this._expr.substring(this._start, this._end)));
549
+ const [num, numEnd] = parseLiteralNumber(this._expr, this._start);
550
+ this._fragment = new Literal(num);
551
+ this._end = numEnd;
608
552
  }
609
553
  else {
610
554
  throw new Error(`unknown symbol ${c}`);
@@ -1,3 +1,4 @@
1
+ import { Type } from "../Type.js";
1
2
  export class ArrayAtom {
2
3
  _valTypes;
3
4
  constructor(_valTypes = []) {
@@ -30,7 +31,9 @@ export class ArrayAtom {
30
31
  }
31
32
  if (this.isTuple) {
32
33
  for (let i = 0; i < this._valTypes.length; ++i) {
33
- const vtype = atom.isTuple ? atom.valType(i) : atom.valType(0);
34
+ const vtype = atom.isTuple
35
+ ? atom.valType(i) ?? Type.Void
36
+ : atom.valType(0);
34
37
  if (!vtype || !this._valTypes[i].accept(vtype)) {
35
38
  return false;
36
39
  }
@@ -4,11 +4,11 @@ exports.ParserState = void 0;
4
4
  const Boolean_js_1 = require("./native/Boolean.js");
5
5
  const Buffer_js_1 = require("./native/Buffer.js");
6
6
  const Aggregable_js_1 = require("./native/Aggregable.js");
7
- const String_js_1 = require("./native/String.js");
8
- const Constant_js_1 = require("./Constant.js");
9
7
  const Number_js_1 = require("./native/Number.js");
10
8
  const Unknown_js_1 = require("./native/Unknown.js");
11
- const Integer_js_1 = require("./native/Integer.js");
9
+ const String_js_1 = require("./native/String.js");
10
+ const Timestamp_js_1 = require("./native/Timestamp.js");
11
+ const Constant_js_1 = require("./Constant.js");
12
12
  const Type_js_1 = require("./Type.js");
13
13
  const ParserFrame_js_1 = require("./ParserFrame.js");
14
14
  class Literal {
@@ -453,53 +453,25 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
453
453
  break;
454
454
  }
455
455
  break;
456
- case "@":
457
- while ((0, String_js_1.isDateSymbol)(this._expr.charAt(this._end))) {
458
- ++this._end;
459
- }
460
- if ((0, String_js_1.isDateTimeSeparatorSymbol)(this._expr.charAt(this._end))) {
461
- ++this._end;
462
- while ((0, String_js_1.isTimeSymbol)(this._expr.charAt(this._end))) {
463
- ++this._end;
464
- }
465
- if (this._expr.charAt(this._end) === "."
466
- && (0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end + 1))) {
467
- ++this._end;
468
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
469
- ++this._end;
470
- }
471
- if (this._expr.charAt(this._end) === "Z") {
472
- ++this._end;
473
- }
474
- }
475
- }
476
- this._fragment = new Literal(new Date(this._expr.substring(this._start + 1, this._end)));
456
+ case "@": {
457
+ const [ts, tsEnd] = (0, Timestamp_js_1.parseLiteralTimestamp)(this._expr, this._start);
458
+ this._fragment = new Literal(ts);
459
+ this._end = tsEnd;
477
460
  break;
478
- case "`":
479
- while (this._expr.charAt(this._end) !== ""
480
- && this._expr.charAt(this._end) !== c) {
481
- ++this._end;
482
- }
483
- if (this._end >= this._expr.length) {
484
- this._start = this._expr.length;
485
- throw new Error(`missing closing buffer quotation mark ${c}`);
486
- }
487
- this._fragment = new Literal((0, Buffer_js_1.parseBuffer)((0, String_js_1.replaceWith)(this._expr.substring(this._start + 1, this._end), "", " ", "\t", "\n")));
488
- ++this._end;
461
+ }
462
+ case "`": {
463
+ const [buf, bufEnd] = (0, Buffer_js_1.parseLiteralBuffer)(this._expr, this._start);
464
+ this._fragment = new Literal(buf);
465
+ this._end = bufEnd;
489
466
  break;
467
+ }
490
468
  case "\"":
491
- case "'":
492
- while (this._expr.charAt(this._end) !== ""
493
- && this._expr.charAt(this._end) !== c) {
494
- ++this._end;
495
- }
496
- if (this._end >= this._expr.length) {
497
- this._start = this._expr.length;
498
- throw new Error(`missing closing string quotation mark ${c}`);
499
- }
500
- this._fragment = new Literal((0, String_js_1.replaceWith)(this._expr.substring(this._start + 1, this._end), "", "\\\n"));
501
- ++this._end;
469
+ case "'": {
470
+ const [str, strEnd] = (0, String_js_1.parseLiteralString)(this._expr, this._start);
471
+ this._fragment = new Literal(str);
472
+ this._end = strEnd;
502
473
  break;
474
+ }
503
475
  default:
504
476
  if ((0, String_js_1.isTokenStartSymbol)(c)) {
505
477
  while ((0, String_js_1.isTokenSymbol)(this._expr.charAt(this._end))) {
@@ -576,38 +548,10 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
576
548
  break;
577
549
  }
578
550
  }
579
- else if ((0, String_js_1.isNumericSymbol)(c)) {
580
- let integer = true;
581
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
582
- ++this._end;
583
- }
584
- if (this._expr.charAt(this._end) === ".") {
585
- ++this._end;
586
- if ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
587
- ++this._end;
588
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
589
- ++this._end;
590
- }
591
- integer = false;
592
- }
593
- else {
594
- --this._end;
595
- }
596
- }
597
- if (this._expr.charAt(this._end) === "e") {
598
- ++this._end;
599
- if ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))
600
- || (0, String_js_1.isSignSymbol)(this._expr.charAt(this._end))) {
601
- ++this._end;
602
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
603
- ++this._end;
604
- }
605
- }
606
- integer = false;
607
- }
608
- this._fragment = integer
609
- ? new Literal((0, Integer_js_1.boundInteger)(BigInt(this._expr.substring(this._start, this._end))))
610
- : new Literal(parseFloat(this._expr.substring(this._start, this._end)));
551
+ else if ((0, Number_js_1.isNumericSymbol)(c)) {
552
+ const [num, numEnd] = (0, Number_js_1.parseLiteralNumber)(this._expr, this._start);
553
+ this._fragment = new Literal(num);
554
+ this._end = numEnd;
611
555
  }
612
556
  else {
613
557
  throw new Error(`unknown symbol ${c}`);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArrayAtom = void 0;
4
+ const Type_js_1 = require("../Type.js");
4
5
  class ArrayAtom {
5
6
  _valTypes;
6
7
  constructor(_valTypes = []) {
@@ -33,7 +34,9 @@ class ArrayAtom {
33
34
  }
34
35
  if (this.isTuple) {
35
36
  for (let i = 0; i < this._valTypes.length; ++i) {
36
- const vtype = atom.isTuple ? atom.valType(i) : atom.valType(0);
37
+ const vtype = atom.isTuple
38
+ ? atom.valType(i) ?? Type_js_1.Type.Void
39
+ : atom.valType(0);
37
40
  if (!vtype || !this._valTypes[i].accept(vtype)) {
38
41
  return false;
39
42
  }
@@ -27,4 +27,4 @@ const add = (value1, value2) => {
27
27
  }
28
28
  return (0, exports.aggregate)(value1, value2);
29
29
  };
30
- exports.funcAdd = new Constant_js_1.Constant((value1, value2) => add(value1, value2), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Float, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Integer, Type_js_1.Type.Float]), Type_js_1.Type.functionType(Type_js_1.Type.Integer, [Type_js_1.Type.Integer, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Buffer]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.String]), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Array])));
30
+ exports.funcAdd = new Constant_js_1.Constant((value1, value2) => add(value1, value2), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Float, Type_js_1.Type.Float]), Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Float, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Float, [Type_js_1.Type.Integer, Type_js_1.Type.Float]), Type_js_1.Type.functionType(Type_js_1.Type.Integer, [Type_js_1.Type.Integer, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Buffer]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.String]), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Array])));
@@ -10,7 +10,9 @@ exports.funcAnd = new Constant_js_1.Constant((values) => values.flat().every((v)
10
10
  exports.funcNot = new Constant_js_1.Constant((value) => !value, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Boolean]));
11
11
  const funcDecodeBoolean = new Constant_js_1.Constant((value, byteOffset) => {
12
12
  const offset = byteOffset == null ? 0 : Number(byteOffset);
13
- return value && offset >= 0 && value.byteLength > offset ? Boolean(new Uint8Array(value)[offset]) : undefined;
13
+ return !value || offset < 0 || value.byteLength < offset + 1
14
+ ? undefined
15
+ : Boolean(new Uint8Array(value)[offset]);
14
16
  }, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalInteger]));
15
17
  const funcParseBoolean = new Constant_js_1.Constant((value) => {
16
18
  if (value == null) {
@@ -1,12 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.constBuffer = exports.funcByte = exports.parseBuffer = exports.formatBuffer = exports.concatBuffers = exports.containsBuffer = exports.equateBuffers = exports.parseBoundedInteger = exports.EmptyBuffer = void 0;
3
+ exports.constBuffer = exports.funcByte = exports.parseBuffer = exports.formatBuffer = exports.concatBuffers = exports.containsBuffer = exports.equateBuffers = exports.parseBoundedInteger = exports.parseLiteralBuffer = exports.formatLiteralBuffer = exports.EmptyBuffer = void 0;
4
4
  const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  exports.EmptyBuffer = new ArrayBuffer(0);
7
7
  const ZERO = "0".charCodeAt(0), NINE = "9".charCodeAt(0);
8
8
  const UPPER_A = "A".charCodeAt(0), UPPER_F = "F".charCodeAt(0);
9
9
  const LOWER_A = "a".charCodeAt(0), LOWER_F = "f".charCodeAt(0);
10
+ const formatLiteralBuffer = (value) => `\`${(0, exports.formatBuffer)(value)}\``;
11
+ exports.formatLiteralBuffer = formatLiteralBuffer;
12
+ const parseLiteralBuffer = (value, offset = 0) => {
13
+ const q = value.charCodeAt(offset);
14
+ for (let i = offset + 1, bytes = [], digits = [0, 0], ix = 0; i < value.length; ++i) {
15
+ const code = value.charCodeAt(i);
16
+ if (code === q) {
17
+ if (ix === 1) {
18
+ bytes.push(digits[0]);
19
+ }
20
+ return [new Uint8Array(bytes).buffer, i + 1];
21
+ }
22
+ if (code >= ZERO && code <= NINE) {
23
+ digits[ix++] = code - ZERO;
24
+ }
25
+ else if (code >= UPPER_A && code <= UPPER_F) {
26
+ digits[ix++] = code - UPPER_A + 10;
27
+ }
28
+ else if (code >= LOWER_A && code <= LOWER_F) {
29
+ digits[ix++] = code - LOWER_A + 10;
30
+ }
31
+ if (ix > 1) {
32
+ bytes.push(digits[0] << 4 | digits[1]);
33
+ ix = 0;
34
+ }
35
+ }
36
+ throw new Error(`missing closing buffer quotation mark ${String.fromCharCode(q)}`);
37
+ };
38
+ exports.parseLiteralBuffer = parseLiteralBuffer;
10
39
  const parseBoundedInteger = (input, base, min, max) => {
11
40
  if (input.length === 0) {
12
41
  throw new Error(`Invalid IP component: ${input}`);
@@ -25,12 +54,12 @@ const parseBoundedInteger = (input, base, min, max) => {
25
54
  digit = code - LOWER_A + 10;
26
55
  }
27
56
  else {
28
- throw new Error(`Invalid IP component: ${input}`);
57
+ throw new Error(`Invalid character: ${input.charAt(i)}`);
29
58
  }
30
59
  value = value * base + digit;
31
60
  }
32
61
  if (value < min || value > max) {
33
- throw new Error(`Invalid IP component: ${input}`);
62
+ throw new Error(`Invalid integer: ${value}`);
34
63
  }
35
64
  return value;
36
65
  };
@@ -4,13 +4,15 @@ exports.funcInject = exports.funcSplice = exports.funcSlice = void 0;
4
4
  const Constant_js_1 = require("../Constant.js");
5
5
  const Type_js_1 = require("../Type.js");
6
6
  const Aggregable_js_1 = require("./Aggregable.js");
7
- const typeEnumerableArray = Type_js_1.Type.arrayType([Type_js_1.Type.Enumerable]);
7
+ const typeBufferArray = Type_js_1.Type.arrayType([Type_js_1.Type.Buffer]);
8
+ const typeStringArray = Type_js_1.Type.arrayType([Type_js_1.Type.String]);
9
+ const typeArrayArray = Type_js_1.Type.arrayType([Type_js_1.Type.Array]);
8
10
  exports.funcSlice = new Constant_js_1.Constant((value, start = 0n, end) => value == null
9
11
  ? undefined
10
12
  : value.slice(Number(start), end == null ? undefined : Number(end)), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger])));
11
13
  exports.funcSplice = new Constant_js_1.Constant((value, start, remove, inject) => value == null
12
14
  ? undefined
13
- : (0, Aggregable_js_1.aggregate)(inject.reduce((acc, val) => (0, Aggregable_js_1.aggregate)(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeEnumerableArray], true), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeEnumerableArray], true), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeEnumerableArray], true)));
15
+ : (0, Aggregable_js_1.aggregate)(inject.reduce((acc, val) => (0, Aggregable_js_1.aggregate)(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeBufferArray], true), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeStringArray], true), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Integer, Type_js_1.Type.Integer, typeArrayArray], true)));
14
16
  exports.funcInject = new Constant_js_1.Constant((value, start, inject) => value == null
15
17
  ? undefined
16
- : (0, Aggregable_js_1.aggregate)(inject.reduce((acc, val) => (0, Aggregable_js_1.aggregate)(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer, typeEnumerableArray], true), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.Integer, typeEnumerableArray], true), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Integer, typeEnumerableArray], true)));
18
+ : (0, Aggregable_js_1.aggregate)(inject.reduce((acc, val) => (0, Aggregable_js_1.aggregate)(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer, typeBufferArray], true), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.String, Type_js_1.Type.Integer, typeStringArray], true), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Integer, typeArrayArray], true)));
@@ -34,12 +34,24 @@ const decodeFloat = (value, encoding = "f64", byteOffset) => {
34
34
  if (value == null) {
35
35
  return undefined;
36
36
  }
37
- const dv = new DataView(value, byteOffset);
37
+ const offset = byteOffset ?? 0;
38
+ if (offset < 0) {
39
+ return undefined;
40
+ }
41
+ const dv = new DataView(value);
38
42
  switch (encoding) {
39
- case "f32": return dv.getFloat32(0);
40
- case "f32le": return dv.getFloat32(0, true);
41
- case "f64": return dv.getFloat64(0);
42
- case "f64le": return dv.getFloat64(0, true);
43
+ case "f32": return value.byteLength < offset + 4
44
+ ? undefined
45
+ : dv.getFloat32(offset);
46
+ case "f32le": return value.byteLength < offset + 4
47
+ ? undefined
48
+ : dv.getFloat32(offset, true);
49
+ case "f64": return value.byteLength < offset + 8
50
+ ? undefined
51
+ : dv.getFloat64(offset);
52
+ case "f64le": return value.byteLength < offset + 8
53
+ ? undefined
54
+ : dv.getFloat64(offset, true);
43
55
  default: throw new Error(`${encoding} encoding not supported`);
44
56
  }
45
57
  };
@@ -73,20 +73,48 @@ const decodeInteger = (value, encoding = "i64", byteOffset) => {
73
73
  if (value == null) {
74
74
  return undefined;
75
75
  }
76
- const dv = new DataView(value, byteOffset == null ? undefined : Number(byteOffset));
76
+ const offset = byteOffset ?? 0;
77
+ if (offset < 0) {
78
+ return undefined;
79
+ }
80
+ const dv = new DataView(value);
77
81
  switch (encoding) {
78
- case "i8": return BigInt(dv.getInt8(0));
79
- case "i16": return BigInt(dv.getInt16(0));
80
- case "i16le": return BigInt(dv.getInt16(0, true));
81
- case "i32": return BigInt(dv.getInt32(0));
82
- case "i32le": return BigInt(dv.getInt32(0, true));
83
- case "i64": return dv.getBigInt64(0);
84
- case "i64le": return dv.getBigInt64(0, true);
85
- case "n8": return BigInt(dv.getUint8(0));
86
- case "n16": return BigInt(dv.getUint16(0));
87
- case "n16le": return BigInt(dv.getUint16(0, true));
88
- case "n32": return BigInt(dv.getUint32(0));
89
- case "n32le": return BigInt(dv.getUint32(0, true));
82
+ case "i8": return value.byteLength < offset + 1
83
+ ? undefined
84
+ : BigInt(dv.getInt8(offset));
85
+ case "i16": return value.byteLength < offset + 2
86
+ ? undefined
87
+ : BigInt(dv.getInt16(offset));
88
+ case "i16le": return value.byteLength < offset + 2
89
+ ? undefined
90
+ : BigInt(dv.getInt16(offset, true));
91
+ case "i32": return value.byteLength < offset + 4
92
+ ? undefined
93
+ : BigInt(dv.getInt32(offset));
94
+ case "i32le": return value.byteLength < offset + 4
95
+ ? undefined
96
+ : BigInt(dv.getInt32(offset, true));
97
+ case "i64": return value.byteLength < offset + 8
98
+ ? undefined
99
+ : dv.getBigInt64(offset);
100
+ case "i64le": return value.byteLength < offset + 8
101
+ ? undefined
102
+ : dv.getBigInt64(offset, true);
103
+ case "n8": return value.byteLength < offset + 1
104
+ ? undefined
105
+ : BigInt(dv.getUint8(offset));
106
+ case "n16": return value.byteLength < offset + 2
107
+ ? undefined
108
+ : BigInt(dv.getUint16(offset));
109
+ case "n16le": return value.byteLength < offset + 2
110
+ ? undefined
111
+ : BigInt(dv.getUint16(offset, true));
112
+ case "n32": return value.byteLength < offset + 4
113
+ ? undefined
114
+ : BigInt(dv.getUint32(offset));
115
+ case "n32le": return value.byteLength < offset + 4
116
+ ? undefined
117
+ : BigInt(dv.getUint32(offset, true));
90
118
  default: throw new Error(`${encoding} encoding not supported`);
91
119
  }
92
120
  };
@@ -27,8 +27,9 @@ exports.funcAt = new Constant_js_1.Constant((value, index) => {
27
27
  return undefined;
28
28
  }
29
29
  else if (Array.isArray(value)) {
30
- const ix = Number(index);
31
- return value[ix < 0 ? value.length + ix : ix];
30
+ const num = Number(index);
31
+ const ix = num < 0 ? value.length + num : num;
32
+ return ix >= 0 && ix < value.length ? value[ix] : undefined;
32
33
  }
33
34
  const key = String(index);
34
35
  return Object.hasOwn(value, key) ? value[key] : undefined;
@@ -38,8 +39,9 @@ exports.funcHas = new Constant_js_1.Constant((value, index) => {
38
39
  return false;
39
40
  }
40
41
  else if (Array.isArray(value)) {
41
- const ix = Number(index);
42
- return ix < value.length;
42
+ const num = Number(index);
43
+ const ix = num < 0 ? value.length + num : num;
44
+ return ix >= 0 && ix < value.length;
43
45
  }
44
46
  return Object.hasOwn(value, String(index));
45
47
  }, Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Array, Type_js_1.Type.Integer]), Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Object, Type_js_1.Type.String])));