affinirum 1.4.2 → 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 (89) hide show
  1. package/README.md +226 -154
  2. package/dst/Affinirum.js +24 -15
  3. package/dst/Atom.d.ts +3 -1
  4. package/dst/Constant.js +1 -1
  5. package/dst/Functions.js +7 -4
  6. package/dst/Keywords.js +4 -4
  7. package/dst/ParserState.d.ts +2 -1
  8. package/dst/ParserState.js +43 -82
  9. package/dst/Type.d.ts +11 -7
  10. package/dst/Type.js +113 -38
  11. package/dst/Value.d.ts +1 -0
  12. package/dst/Value.js +19 -1
  13. package/dst/atom/ArrayAtom.d.ts +7 -3
  14. package/dst/atom/ArrayAtom.js +53 -8
  15. package/dst/atom/FunctionAtom.d.ts +4 -1
  16. package/dst/atom/FunctionAtom.js +65 -15
  17. package/dst/atom/ObjectAtom.d.ts +7 -5
  18. package/dst/atom/ObjectAtom.js +35 -13
  19. package/dst/atom/PrimitiveAtom.d.ts +3 -1
  20. package/dst/atom/PrimitiveAtom.js +10 -1
  21. package/dst/cjs/Affinirum.js +24 -15
  22. package/dst/cjs/Constant.js +1 -1
  23. package/dst/cjs/Functions.js +6 -3
  24. package/dst/cjs/Keywords.js +4 -4
  25. package/dst/cjs/ParserState.js +42 -81
  26. package/dst/cjs/Type.js +113 -38
  27. package/dst/cjs/Value.js +21 -0
  28. package/dst/cjs/atom/ArrayAtom.js +53 -8
  29. package/dst/cjs/atom/FunctionAtom.js +65 -15
  30. package/dst/cjs/atom/ObjectAtom.js +35 -13
  31. package/dst/cjs/atom/PrimitiveAtom.js +10 -1
  32. package/dst/cjs/native/Aggregable.js +3 -2
  33. package/dst/cjs/native/Array.js +10 -8
  34. package/dst/cjs/native/Boolean.js +8 -5
  35. package/dst/cjs/native/Buffer.js +32 -3
  36. package/dst/cjs/native/Enumerable.js +7 -4
  37. package/dst/cjs/native/Float.js +26 -13
  38. package/dst/cjs/native/Integer.js +64 -28
  39. package/dst/cjs/native/Iterable.js +8 -5
  40. package/dst/cjs/native/Number.js +62 -33
  41. package/dst/cjs/native/Object.js +2 -1
  42. package/dst/cjs/native/String.js +71 -24
  43. package/dst/cjs/native/Timestamp.js +76 -18
  44. package/dst/cjs/native/Unknown.js +1 -1
  45. package/dst/cjs/native/notation/AN.js +152 -11
  46. package/dst/cjs/native/notation/IP.js +4 -3
  47. package/dst/cjs/native/notation/JSON.js +1 -1
  48. package/dst/cjs/node/ArrayNode.js +6 -6
  49. package/dst/cjs/node/BlockNode.js +5 -4
  50. package/dst/cjs/node/CallNode.js +28 -22
  51. package/dst/cjs/node/CastNode.js +42 -0
  52. package/dst/cjs/node/ObjectNode.js +2 -4
  53. package/dst/cjs/node/SwitchNode.js +10 -10
  54. package/dst/cjs/node/VariableNode.js +3 -1
  55. package/dst/native/Aggregable.js +3 -2
  56. package/dst/native/Array.js +10 -8
  57. package/dst/native/Boolean.d.ts +1 -0
  58. package/dst/native/Boolean.js +7 -4
  59. package/dst/native/Buffer.d.ts +2 -0
  60. package/dst/native/Buffer.js +29 -2
  61. package/dst/native/Enumerable.js +7 -4
  62. package/dst/native/Float.d.ts +2 -0
  63. package/dst/native/Float.js +25 -12
  64. package/dst/native/Integer.d.ts +5 -1
  65. package/dst/native/Integer.js +61 -27
  66. package/dst/native/Iterable.js +8 -5
  67. package/dst/native/Number.d.ts +4 -3
  68. package/dst/native/Number.js +57 -32
  69. package/dst/native/Object.js +2 -1
  70. package/dst/native/String.d.ts +2 -7
  71. package/dst/native/String.js +68 -16
  72. package/dst/native/Timestamp.d.ts +4 -2
  73. package/dst/native/Timestamp.js +73 -17
  74. package/dst/native/Unknown.js +1 -1
  75. package/dst/native/notation/AN.d.ts +1 -0
  76. package/dst/native/notation/AN.js +152 -12
  77. package/dst/native/notation/IP.js +4 -3
  78. package/dst/native/notation/JSON.js +1 -1
  79. package/dst/node/ArrayNode.d.ts +1 -1
  80. package/dst/node/ArrayNode.js +6 -6
  81. package/dst/node/BlockNode.js +5 -4
  82. package/dst/node/CallNode.js +28 -22
  83. package/dst/node/CastNode.d.ts +15 -0
  84. package/dst/node/CastNode.js +38 -0
  85. package/dst/node/ObjectNode.d.ts +1 -1
  86. package/dst/node/ObjectNode.js +2 -4
  87. package/dst/node/SwitchNode.js +10 -10
  88. package/dst/node/VariableNode.js +3 -1
  89. package/package.json +5 -4
@@ -135,7 +135,7 @@ function decodeIP(value) {
135
135
  }
136
136
  return prefix == null ? address : `${address}/${prefix}`;
137
137
  }
138
- throw new Error(`Invalid IP buffer length: ${dv.byteLength}`);
138
+ throw new RangeError(`Invalid IP buffer length: ${dv.byteLength}`);
139
139
  }
140
140
  class IPSet {
141
141
  _ipv4Sets = Array(33).fill(undefined);
@@ -195,7 +195,8 @@ class IPSet {
195
195
  }
196
196
  }
197
197
  }
198
- const funcMatchIP = new Constant_js_1.Constant((values, ...search) => {
198
+ const typeStringOrArray = Type_js_1.Type.union(Type_js_1.Type.String, Type_js_1.Type.arrayType([Type_js_1.Type.String]));
199
+ const funcMatchIP = new Constant_js_1.Constant((values, search) => {
199
200
  try {
200
201
  const ipset = new IPSet().add(values);
201
202
  for (const ip of search.flat()) {
@@ -209,7 +210,7 @@ const funcMatchIP = new Constant_js_1.Constant((values, ...search) => {
209
210
  }
210
211
  catch { }
211
212
  return false;
212
- }, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Array, Type_js_1.Type.union(Type_js_1.Type.String, Type_js_1.Type.Array)], true));
213
+ }, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.arrayType([Type_js_1.Type.String]), Type_js_1.Type.arrayType([typeStringOrArray])], true));
213
214
  const funcEncodeIP = new Constant_js_1.Constant((value) => {
214
215
  if (value == null) {
215
216
  return Buffer_js_1.EmptyBuffer;
@@ -4,7 +4,7 @@ exports.constJSON = exports.formatJSON = void 0;
4
4
  const Constant_js_1 = require("../../Constant.js");
5
5
  const Type_js_1 = require("../../Type.js");
6
6
  const typeJson = Type_js_1.Type.union(Type_js_1.Type.Void, Type_js_1.Type.Boolean, Type_js_1.Type.Timestamp, Type_js_1.Type.Float, Type_js_1.Type.Integer, Type_js_1.Type.String, Type_js_1.Type.Array, Type_js_1.Type.Object);
7
- const formatJSON = (value, whitespace) => value == null ? "" : JSON.stringify(value, (_key, value) => typeof value === "bigint" ? `"${value.toString()}"` : value, whitespace);
7
+ const formatJSON = (value, whitespace) => value == null ? "" : JSON.stringify(value, (_key, value) => typeof value === "bigint" ? value.toString() : value, whitespace);
8
8
  exports.formatJSON = formatJSON;
9
9
  const funcFormatJSON = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatJSON)(value, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [typeJson, Type_js_1.Type.OptionalString]));
10
10
  const funcParseJSON = new Constant_js_1.Constant((value) => value == null ? undefined : JSON.parse(value), Type_js_1.Type.functionType(typeJson, [Type_js_1.Type.OptionalString]));
@@ -8,23 +8,23 @@ const Type_js_1 = require("../Type.js");
8
8
  class ArrayNode extends Node_js_1.Node {
9
9
  _subnodes;
10
10
  _type;
11
- constructor(frame, _subnodes, generic = false) {
11
+ constructor(frame, _subnodes) {
12
12
  super(frame);
13
13
  this._subnodes = _subnodes;
14
- this._type = generic
15
- ? Type_js_1.Type.Array
16
- : Type_js_1.Type.arrayType(_subnodes.map((i) => i.type));
14
+ this._type = Type_js_1.Type.arrayType(_subnodes.map((i) => i.type));
17
15
  }
18
16
  get type() {
19
17
  return this._type;
20
18
  }
21
19
  compile(type) {
22
- this._type = this.reduceType(type);
23
20
  let constant = true;
24
21
  for (let i = 0; i < this._subnodes.length; ++i) {
25
- this._subnodes[i] = this._subnodes[i].compile(Type_js_1.Type.Unknown);
22
+ const valType = type.arrayAtomValType(i);
23
+ this._subnodes[i] = this._subnodes[i].compile(valType);
26
24
  constant &&= this._subnodes[i].constant;
27
25
  }
26
+ this._type = Type_js_1.Type.arrayType(this._subnodes.map((node) => node.type));
27
+ this._type = this.reduceType(type);
28
28
  if (constant) {
29
29
  return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
30
30
  }
@@ -12,9 +12,13 @@ class BlockNode extends Node_js_1.Node {
12
12
  this._subnodes = _subnodes;
13
13
  }
14
14
  get type() {
15
- return this._subnodes[this._subnodes.length - 1].type;
15
+ return this._subnodes.length ? this._subnodes[this._subnodes.length - 1].type : Type_js_1.Type.Void;
16
16
  }
17
17
  compile(type) {
18
+ if (this._subnodes.length === 0) {
19
+ this.reduceType(type);
20
+ return new ConstantNode_js_1.ConstantNode(this, Constant_js_1.Constant.Null);
21
+ }
18
22
  let constant = true;
19
23
  for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) {
20
24
  this._subnodes[i] = this._subnodes[i].compile(i < last ? Type_js_1.Type.Unknown : type);
@@ -23,9 +27,6 @@ class BlockNode extends Node_js_1.Node {
23
27
  if (constant) {
24
28
  return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
25
29
  }
26
- if (this._subnodes.length === 0) {
27
- return new ConstantNode_js_1.ConstantNode(this, Constant_js_1.Constant.Null);
28
- }
29
30
  if (this._subnodes.length === 1) {
30
31
  return this._subnodes[0];
31
32
  }
@@ -5,6 +5,7 @@ const Node_js_1 = require("../Node.js");
5
5
  const Constant_js_1 = require("../Constant.js");
6
6
  const ConstantNode_js_1 = require("./ConstantNode.js");
7
7
  const ArrayNode_js_1 = require("./ArrayNode.js");
8
+ const Value_js_1 = require("../Value.js");
8
9
  const Type_js_1 = require("../Type.js");
9
10
  const JumpException_js_1 = require("../JumpException.js");
10
11
  class CallNode extends Node_js_1.Node {
@@ -17,7 +18,7 @@ class CallNode extends Node_js_1.Node {
17
18
  this._subnodes = _subnodes;
18
19
  const functionAtoms = this._fnode.type.functionAtoms(Type_js_1.Type.Unknown, this._subnodes.length);
19
20
  if (!functionAtoms.length) {
20
- this.throwError(`function ${this._fnode.type} does not take ${this._subnodes.length} arguments)`);
21
+ this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s)`);
21
22
  }
22
23
  this._type = Type_js_1.Type.union(...functionAtoms.map((i) => i.retType));
23
24
  }
@@ -26,29 +27,34 @@ class CallNode extends Node_js_1.Node {
26
27
  }
27
28
  compile(type) {
28
29
  this._fnode = this._fnode.compile(this._fnode.type);
29
- const functionAtoms = this._fnode.type.functionAtoms(type, this._subnodes.length);
30
+ const functionAtoms_ = this._fnode.type.functionAtoms(type, this._subnodes.length);
31
+ let functionAtoms = functionAtoms_.filter((atom) => !atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type_js_1.Type.Unknown)));
30
32
  if (!functionAtoms.length) {
31
- this.throwError(`function ${this._fnode.type} does not take ${this._subnodes.length} arguments returning ${type})`);
33
+ functionAtoms = functionAtoms_.filter((atom) => atom.isVariadic && this._subnodes.every((node, index) => node.type.intersect(atom.paramType(index) ?? Type_js_1.Type.Unknown)));
34
+ }
35
+ if (!functionAtoms.length) {
36
+ this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s) and return a type compatible with ${type}`);
32
37
  }
33
38
  this._type = Type_js_1.Type.union(...functionAtoms.map((i) => i.retType));
34
- if (this._fnode.constant) {
35
- let constant = true;
36
- for (let i = 0; i < this._subnodes.length; ++i) {
37
- const argTypes = functionAtoms.map((a) => a.argType(i));
38
- const argType = Type_js_1.Type.union(...argTypes.filter((t) => t != null));
39
- this._subnodes[i] = this._subnodes[i].compile(argType);
40
- constant &&= this._subnodes[i].constant;
41
- }
42
- if (constant) {
43
- return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
44
- }
39
+ const arity = functionAtoms[0].isVariadic
40
+ ? Math.max(...functionAtoms.map((atom) => atom.arity))
41
+ : 0;
42
+ if (arity > 0) {
43
+ const frame = this._subnodes[arity - 1].starts();
44
+ const vnodes = this._subnodes.splice(arity - 1);
45
+ this._subnodes.push(new ArrayNode_js_1.ArrayNode(frame.ends(vnodes[vnodes.length - 1]), vnodes));
45
46
  }
46
- else {
47
- const arity = functionAtoms.filter((i) => i.isVariadic).map((i) => i.arity).reduce((acc, val) => Math.max(acc, val), 0);
48
- if (arity > 0) {
49
- const frame = this._subnodes[arity - 1].starts();
50
- this._subnodes = this._subnodes.slice(0, arity - 1).concat(new ArrayNode_js_1.ArrayNode(frame.ends(this._subnodes[this._subnodes.length - 1]), this._subnodes.slice(arity - 1)));
51
- }
47
+ let constant = this._fnode.constant;
48
+ for (let i = 0; i < this._subnodes.length; ++i) {
49
+ const argTypes = functionAtoms.map((a) => a.argType(i));
50
+ const argType = arity > 0 && i === arity - 1
51
+ ? Type_js_1.Type.arrayType([Type_js_1.Type.union(...functionAtoms.map((a) => a.paramType(i) ?? Type_js_1.Type.Unknown))])
52
+ : Type_js_1.Type.union(...argTypes.filter((t) => t != null));
53
+ this._subnodes[i] = this._subnodes[i].compile(argType);
54
+ constant &&= this._subnodes[i].constant;
55
+ }
56
+ if (constant) {
57
+ return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
52
58
  }
53
59
  return this;
54
60
  }
@@ -58,12 +64,12 @@ class CallNode extends Node_js_1.Node {
58
64
  this.throwError(`function expected not ${Type_js_1.Type.of(func)}`);
59
65
  }
60
66
  try {
61
- return func(...this._subnodes.map((node) => node.evaluate()));
67
+ return (0, Value_js_1.normalize)(func(...this._subnodes.map((node) => node.evaluate())));
62
68
  }
63
69
  catch (e) {
64
70
  if (e instanceof JumpException_js_1.JumpException) {
65
71
  if (e.jump === "return") {
66
- return e.value;
72
+ return (0, Value_js_1.normalize)(e.value);
67
73
  }
68
74
  if (e.jump === "stop" || e.jump === "next") {
69
75
  throw this.throwError(`unexpected ${e.jump} jump`);
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CastNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Type_js_1 = require("../Type.js");
6
+ class CastNode extends Node_js_1.Node {
7
+ _subnode;
8
+ _target;
9
+ _type;
10
+ constructor(frame, _subnode, _target) {
11
+ super(frame);
12
+ this._subnode = _subnode;
13
+ this._target = _target;
14
+ this._type = _target;
15
+ }
16
+ get type() {
17
+ return this._type;
18
+ }
19
+ compile(type) {
20
+ this._subnode = this._subnode.compile(Type_js_1.Type.Unknown);
21
+ if (!this._subnode.type.intersect(this._target)) {
22
+ this.throwError(`cannot cast type ${this._subnode.type} to ${this._target}`);
23
+ }
24
+ this._type = this._target;
25
+ this._type = this.reduceType(type);
26
+ return this;
27
+ }
28
+ evaluate() {
29
+ const value = this._subnode.evaluate();
30
+ if (!this._type.acceptValue(value)) {
31
+ this.throwError(`cannot cast value of type ${Type_js_1.Type.of(value)} to ${this._type}`);
32
+ }
33
+ return value;
34
+ }
35
+ get constant() {
36
+ return this._subnode.constant;
37
+ }
38
+ toString(ident = 0) {
39
+ return `${super.toString(ident)} cast node target <${this._target}>:\n${this._subnode.toString(ident + 1)}`;
40
+ }
41
+ }
42
+ exports.CastNode = CastNode;
@@ -8,12 +8,10 @@ const Type_js_1 = require("../Type.js");
8
8
  class ObjectNode extends Node_js_1.Node {
9
9
  _subnodes;
10
10
  _type;
11
- constructor(frame, _subnodes, generic = false) {
11
+ constructor(frame, _subnodes) {
12
12
  super(frame);
13
13
  this._subnodes = _subnodes;
14
- this._type = generic
15
- ? Type_js_1.Type.Object
16
- : Type_js_1.Type.objectType(Object.fromEntries(_subnodes.filter(([i]) => i instanceof ConstantNode_js_1.ConstantNode && i.constant).map(([k, v]) => [k.evaluate(), v.type])));
14
+ this._type = Type_js_1.Type.objectType(_subnodes.filter(([i]) => i instanceof ConstantNode_js_1.ConstantNode && i.constant).map(([k, v]) => [k.evaluate(), v.type]));
17
15
  }
18
16
  get type() {
19
17
  return this._type;
@@ -24,20 +24,20 @@ class SwitchNode extends Node_js_1.Node {
24
24
  compile(type) {
25
25
  this._cnode = this._cnode.compile(Type_js_1.Type.Boolean);
26
26
  this._type = type;
27
- if (this._cnode.constant) {
28
- let constant = true;
29
- for (let i = 0; i < this._subnodes.length; ++i) {
30
- this._subnodes[i] = this._subnodes[i].compile(type);
31
- constant &&= this._subnodes[i].constant;
32
- }
33
- if (constant) {
34
- return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
35
- }
27
+ let constant = this._cnode.constant;
28
+ for (let i = 0; i < this._subnodes.length; ++i) {
29
+ this._subnodes[i] = this._subnodes[i].compile(type);
30
+ constant &&= this._subnodes[i].constant;
31
+ }
32
+ if (constant) {
33
+ return new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type));
36
34
  }
37
35
  return this;
38
36
  }
39
37
  evaluate() {
40
- return this._cnode.evaluate() ? this._subnodes[0].evaluate() : this._subnodes[1].evaluate();
38
+ return this._cnode.evaluate()
39
+ ? this._subnodes[0].evaluate()
40
+ : this._subnodes[1].evaluate();
41
41
  }
42
42
  toString(ident = 0) {
43
43
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -19,7 +19,9 @@ class VariableNode extends Node_js_1.Node {
19
19
  return this;
20
20
  }
21
21
  evaluate() {
22
- return this._subnode ? this._variable.value = this._subnode.evaluate() : this._variable.value;
22
+ return this._subnode
23
+ ? this._variable.value = this._subnode.evaluate()
24
+ : this._variable.value;
23
25
  }
24
26
  toString(ident = 0) {
25
27
  return this._subnode
@@ -1,6 +1,7 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { concatBuffers } from "./Buffer.js";
4
+ import { boundInteger } from "./Integer.js";
4
5
  export const aggregate = (value1, value2) => {
5
6
  if (value1 == null || value2 == null) {
6
7
  return undefined;
@@ -15,11 +16,11 @@ export const aggregate = (value1, value2) => {
15
16
  };
16
17
  const add = (value1, value2) => {
17
18
  if (typeof value1 === "bigint" && typeof value2 === "bigint") {
18
- return BigInt.asIntN(64, value1 + value2);
19
+ return boundInteger(value1 + value2);
19
20
  }
20
21
  if ((typeof value1 === "number" || typeof value1 === "bigint") && (typeof value2 === "number" || typeof value2 === "bigint")) {
21
22
  return Number(value1) + Number(value2);
22
23
  }
23
24
  return aggregate(value1, value2);
24
25
  };
25
- export const funcAdd = new Constant((value1, value2) => add(value1, value2), Type.union(Type.functionType(Type.Float, [Type.Float, Type.Integer]), Type.functionType(Type.Float, [Type.Integer, Type.Float]), Type.functionType(Type.Integer, [Type.Integer, Type.Integer]), Type.functionType(Type.Buffer, [Type.Buffer, Type.Buffer]), Type.functionType(Type.String, [Type.String, Type.String]), Type.functionType(Type.Array, [Type.Array, Type.Array])));
26
+ export const funcAdd = new Constant((value1, value2) => add(value1, value2), Type.union(Type.functionType(Type.Float, [Type.Float, Type.Float]), Type.functionType(Type.Float, [Type.Float, Type.Integer]), Type.functionType(Type.Float, [Type.Integer, Type.Float]), Type.functionType(Type.Integer, [Type.Integer, Type.Integer]), Type.functionType(Type.Buffer, [Type.Buffer, Type.Buffer]), Type.functionType(Type.String, [Type.String, Type.String]), Type.functionType(Type.Array, [Type.Array, Type.Array])));
@@ -1,11 +1,12 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
+ import { boundInteger } from "./Integer.js";
3
4
  import { equate } from "./Unknown.js";
4
5
  const typePredicate = Type.functionType(Type.Boolean, [Type.Unknown, Type.OptionalInteger, Type.OptionalArray]);
5
6
  const typeItemFinder = Type.functionType(Type.Unknown, [Type.Array, typePredicate]);
6
7
  const typeIndexFinder = Type.functionType(Type.OptionalInteger, [Type.Array, typePredicate]);
7
8
  const typeConditionSet = Type.functionType(Type.Boolean, [Type.Array, typePredicate]);
8
- const typeVariadicInsert = Type.functionType(Type.Array, [Type.Array, Type.Unknown], true);
9
+ const typeVariadicInsert = Type.functionType(Type.Array, [Type.Array, Type.Array], true);
9
10
  const typeArrayOperator = Type.functionType(Type.Array, [Type.Array, Type.Array]);
10
11
  export const funcFirst = new Constant((value, predicate) => value?.find((v, i, a) => predicate(v, BigInt(i), a)), typeItemFinder);
11
12
  export const funcLast = new Constant((value, predicate) => {
@@ -24,7 +25,7 @@ export const funcFirstIndex = new Constant((value, predicate) => {
24
25
  return undefined;
25
26
  }
26
27
  const ix = value.findIndex((v, i, a) => predicate(v, BigInt(i), a));
27
- return ix < 0 ? undefined : BigInt.asIntN(64, BigInt(ix));
28
+ return ix < 0 ? undefined : boundInteger(BigInt(ix));
28
29
  }, typeIndexFinder);
29
30
  export const funcLastIndex = new Constant((value, predicate) => {
30
31
  if (value == null) {
@@ -32,7 +33,7 @@ export const funcLastIndex = new Constant((value, predicate) => {
32
33
  }
33
34
  for (let i = value.length - 1; i >= 0; i--) {
34
35
  if (predicate(value[i], BigInt(i), value)) {
35
- return BigInt.asIntN(64, BigInt(i));
36
+ return boundInteger(BigInt(i));
36
37
  }
37
38
  }
38
39
  return undefined;
@@ -57,18 +58,19 @@ export const funcCompose = new Constant((value, callback) => {
57
58
  }
58
59
  return obj;
59
60
  }, Type.functionType(Type.Object, [Type.Array, Type.functionType(Type.Unknown, [Type.Object, Type.String, Type.OptionalInteger, Type.OptionalArray])]));
60
- export const funcPrepend = new Constant((value, ...items) => {
61
+ export const funcPrepend = new Constant((value, items) => {
61
62
  value?.unshift(items);
62
63
  return value;
63
64
  }, typeVariadicInsert);
64
- export const funcAppend = new Constant((value, ...items) => {
65
+ export const funcAppend = new Constant((value, items) => {
65
66
  value?.push(items);
66
67
  return value;
67
68
  }, typeVariadicInsert);
68
- const funcJoin = new Constant((...values) => values.flat(Infinity), Type.functionType(Type.Array, [Type.Array], true));
69
+ const funcJoin = new Constant((values) => values.flat(Infinity), Type.functionType(Type.Array, [Type.arrayType([Type.Array])], true));
69
70
  const funcRange = new Constant((value1, value2) => {
70
- const min = value1 < value2 ? value1 : value2;
71
- const max = value1 > value2 ? value1 : value2;
71
+ const v1 = boundInteger(value1);
72
+ const v2 = boundInteger(value2);
73
+ const [min, max] = v1 < v2 ? [v1, v2] : [v2, v1];
72
74
  return [...Array(Number(max - min)).keys()].map((i) => BigInt(i) + min);
73
75
  }, Type.functionType(Type.Array, [Type.Integer, Type.Integer]));
74
76
  const funcUnique = new Constant((value) => {
@@ -2,4 +2,5 @@ import { Constant } from "../Constant.js";
2
2
  export declare const funcOr: Constant;
3
3
  export declare const funcAnd: Constant;
4
4
  export declare const funcNot: Constant;
5
+ export declare const funcBoolean: Constant;
5
6
  export declare const constBoolean: any;
@@ -1,13 +1,15 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  const typeBooleanOrArray = Type.union(Type.Boolean, Type.arrayType([Type.Boolean]));
4
- const typeBooleanLogic = Type.functionType(Type.Boolean, [typeBooleanOrArray], true);
5
- export const funcOr = new Constant((...values) => values.flat().some((v) => v), typeBooleanLogic);
6
- export const funcAnd = new Constant((...values) => values.flat().every((v) => v), typeBooleanLogic);
4
+ const typeBooleanLogic = Type.functionType(Type.Boolean, [Type.arrayType([typeBooleanOrArray])], true);
5
+ export const funcOr = new Constant((values) => values.flat().some((v) => v), typeBooleanLogic);
6
+ export const funcAnd = new Constant((values) => values.flat().every((v) => v), typeBooleanLogic);
7
7
  export const funcNot = new Constant((value) => !value, Type.functionType(Type.Boolean, [Type.Boolean]));
8
8
  const funcDecodeBoolean = new Constant((value, byteOffset) => {
9
9
  const offset = byteOffset == null ? 0 : Number(byteOffset);
10
- return value && offset >= 0 && value.byteLength > offset ? Boolean(new Uint8Array(value)[offset]) : undefined;
10
+ return !value || offset < 0 || value.byteLength < offset + 1
11
+ ? undefined
12
+ : Boolean(new Uint8Array(value)[offset]);
11
13
  }, Type.functionType(Type.OptionalBoolean, [Type.OptionalBuffer, Type.OptionalInteger]));
12
14
  const funcParseBoolean = new Constant((value) => {
13
15
  if (value == null) {
@@ -16,6 +18,7 @@ const funcParseBoolean = new Constant((value) => {
16
18
  const v = value.toLowerCase();
17
19
  return v === "true" ? true : v === "false" ? false : undefined;
18
20
  }, Type.functionType(Type.OptionalBoolean, [Type.OptionalString]));
21
+ export const funcBoolean = new Constant((value) => typeof value === "bigint" ? value !== 0n : Boolean(Number(value)), Type.union(Type.functionType(Type.Boolean, [Type.Timestamp]), Type.functionType(Type.Boolean, [Type.Float]), Type.functionType(Type.Boolean, [Type.Integer])));
19
22
  export const constBoolean = Object.assign(Object.create(null), {
20
23
  Or: funcOr,
21
24
  And: funcAnd,
@@ -1,6 +1,8 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  export type BufferFormatting = "hex" | "base64";
3
3
  export declare const EmptyBuffer: ArrayBuffer;
4
+ export declare const formatLiteralBuffer: (value: ArrayBuffer) => string;
5
+ export declare const parseLiteralBuffer: (value: string, offset?: number) => readonly [ArrayBuffer, number];
4
6
  export declare const parseBoundedInteger: (input: string, base: 10 | 16, min: number, max: number) => number;
5
7
  export declare const equateBuffers: (value1: ArrayBuffer, value2: ArrayBuffer) => boolean;
6
8
  export declare const containsBuffer: (value: ArrayBuffer, search: ArrayBuffer, startPos?: number) => boolean;
@@ -4,6 +4,33 @@ export const EmptyBuffer = new ArrayBuffer(0);
4
4
  const ZERO = "0".charCodeAt(0), NINE = "9".charCodeAt(0);
5
5
  const UPPER_A = "A".charCodeAt(0), UPPER_F = "F".charCodeAt(0);
6
6
  const LOWER_A = "a".charCodeAt(0), LOWER_F = "f".charCodeAt(0);
7
+ export const formatLiteralBuffer = (value) => `\`${formatBuffer(value)}\``;
8
+ export const parseLiteralBuffer = (value, offset = 0) => {
9
+ const q = value.charCodeAt(offset);
10
+ for (let i = offset + 1, bytes = [], digits = [0, 0], ix = 0; i < value.length; ++i) {
11
+ const code = value.charCodeAt(i);
12
+ if (code === q) {
13
+ if (ix === 1) {
14
+ bytes.push(digits[0]);
15
+ }
16
+ return [new Uint8Array(bytes).buffer, i + 1];
17
+ }
18
+ if (code >= ZERO && code <= NINE) {
19
+ digits[ix++] = code - ZERO;
20
+ }
21
+ else if (code >= UPPER_A && code <= UPPER_F) {
22
+ digits[ix++] = code - UPPER_A + 10;
23
+ }
24
+ else if (code >= LOWER_A && code <= LOWER_F) {
25
+ digits[ix++] = code - LOWER_A + 10;
26
+ }
27
+ if (ix > 1) {
28
+ bytes.push(digits[0] << 4 | digits[1]);
29
+ ix = 0;
30
+ }
31
+ }
32
+ throw new Error(`missing closing buffer quotation mark ${String.fromCharCode(q)}`);
33
+ };
7
34
  export const parseBoundedInteger = (input, base, min, max) => {
8
35
  if (input.length === 0) {
9
36
  throw new Error(`Invalid IP component: ${input}`);
@@ -22,12 +49,12 @@ export const parseBoundedInteger = (input, base, min, max) => {
22
49
  digit = code - LOWER_A + 10;
23
50
  }
24
51
  else {
25
- throw new Error(`Invalid IP component: ${input}`);
52
+ throw new Error(`Invalid character: ${input.charAt(i)}`);
26
53
  }
27
54
  value = value * base + digit;
28
55
  }
29
56
  if (value < min || value > max) {
30
- throw new Error(`Invalid IP component: ${input}`);
57
+ throw new Error(`Invalid integer: ${value}`);
31
58
  }
32
59
  return value;
33
60
  };
@@ -1,12 +1,15 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  import { aggregate } from "./Aggregable.js";
4
+ const typeBufferArray = Type.arrayType([Type.Buffer]);
5
+ const typeStringArray = Type.arrayType([Type.String]);
6
+ const typeArrayArray = Type.arrayType([Type.Array]);
4
7
  export const funcSlice = new Constant((value, start = 0n, end) => value == null
5
8
  ? undefined
6
9
  : value.slice(Number(start), end == null ? undefined : Number(end)), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.OptionalInteger, Type.OptionalInteger]), Type.functionType(Type.String, [Type.String, Type.OptionalInteger, Type.OptionalInteger]), Type.functionType(Type.Array, [Type.Array, Type.OptionalInteger, Type.OptionalInteger])));
7
- export const funcSplice = new Constant((value, start, remove, ...inject) => value == null
10
+ export const funcSplice = new Constant((value, start, remove, inject) => value == null
8
11
  ? undefined
9
- : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, Type.Integer, Type.Enumerable], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Integer, Type.Enumerable], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Integer, Type.Enumerable], true)));
10
- export const funcInject = new Constant((value, start, ...inject) => value == null
12
+ : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start + remove))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, Type.Integer, typeBufferArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Integer, typeStringArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Integer, typeArrayArray], true)));
13
+ export const funcInject = new Constant((value, start, inject) => value == null
11
14
  ? undefined
12
- : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, Type.Enumerable], true), Type.functionType(Type.String, [Type.String, Type.Integer, Type.Enumerable], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, Type.Enumerable], true)));
15
+ : aggregate(inject.reduce((acc, val) => aggregate(acc, val), value.slice(0, Number(start))), value.slice(Number(start))), Type.union(Type.functionType(Type.Buffer, [Type.Buffer, Type.Integer, typeBufferArray], true), Type.functionType(Type.String, [Type.String, Type.Integer, typeStringArray], true), Type.functionType(Type.Array, [Type.Array, Type.Integer, typeArrayArray], true)));
@@ -1,3 +1,5 @@
1
+ import { Constant } from "../Constant.js";
1
2
  export type FloatEncoding = "f32" | "f32le" | "f64" | "f64le";
2
3
  export declare const encodeFloat: (value: number, encoding?: FloatEncoding) => ArrayBuffer;
4
+ export declare const funcFloat: Constant;
3
5
  export declare const constFloat: any;
@@ -30,21 +30,33 @@ const decodeFloat = (value, encoding = "f64", byteOffset) => {
30
30
  if (value == null) {
31
31
  return undefined;
32
32
  }
33
- const dv = new DataView(value, byteOffset);
33
+ const offset = byteOffset ?? 0;
34
+ if (offset < 0) {
35
+ return undefined;
36
+ }
37
+ const dv = new DataView(value);
34
38
  switch (encoding) {
35
- case "f32": return dv.getFloat32(0);
36
- case "f32le": return dv.getFloat32(0, true);
37
- case "f64": return dv.getFloat64(0);
38
- case "f64le": return dv.getFloat64(0, true);
39
+ case "f32": return value.byteLength < offset + 4
40
+ ? undefined
41
+ : dv.getFloat32(offset);
42
+ case "f32le": return value.byteLength < offset + 4
43
+ ? undefined
44
+ : dv.getFloat32(offset, true);
45
+ case "f64": return value.byteLength < offset + 8
46
+ ? undefined
47
+ : dv.getFloat64(offset);
48
+ case "f64le": return value.byteLength < offset + 8
49
+ ? undefined
50
+ : dv.getFloat64(offset, true);
39
51
  default: throw new Error(`${encoding} encoding not supported`);
40
52
  }
41
53
  };
42
- const typeNumberOrArray = Type.union(Type.Float, Type.arrayType([Type.Float]));
43
- const typeAggregator = Type.functionType(Type.Float, [typeNumberOrArray], true);
44
- const typeTransform = Type.functionType(Type.Float, [Type.Float]);
45
- const funcSum = new Constant((...values) => values.flat().reduce((acc, val) => acc + Number(val), 0), typeAggregator);
46
- const funcMin = new Constant((...values) => Math.min(Number.POSITIVE_INFINITY, ...values.flat().map((i) => Number(i))), typeAggregator);
47
- const funcMax = new Constant((...values) => Math.max(Number.NEGATIVE_INFINITY, ...values.flat().map((i) => Number(i))), typeAggregator);
54
+ const typeNumberOrArray = Type.union(Type.Number, Type.arrayType([Type.Number]));
55
+ const typeAggregator = Type.functionType(Type.Float, [Type.arrayType([typeNumberOrArray])], true);
56
+ const typeTransform = Type.functionType(Type.Float, [Type.Number]);
57
+ const funcSum = new Constant((values) => values.flat().reduce((acc, val) => acc + Number(val), 0), typeAggregator);
58
+ const funcMin = new Constant((values) => Math.min(Number.POSITIVE_INFINITY, ...values.flat().map((i) => Number(i))), typeAggregator);
59
+ const funcMax = new Constant((values) => Math.max(Number.NEGATIVE_INFINITY, ...values.flat().map((i) => Number(i))), typeAggregator);
48
60
  const funcExponent = new Constant((value) => Math.exp(Number(value)), typeTransform);
49
61
  const funcLogarithm = new Constant((value) => Math.log(Number(value)), typeTransform);
50
62
  const funcAbs = new Constant((value) => Math.abs(Number(value)), typeTransform);
@@ -52,9 +64,10 @@ const funcCeil = new Constant((value) => Math.ceil(Number(value)), typeTransform
52
64
  const funcFloor = new Constant((value) => Math.floor(Number(value)), typeTransform);
53
65
  const funcRound = new Constant((value) => Math.round(Number(value)), typeTransform);
54
66
  const funcTruncate = new Constant((value) => Math.trunc(Number(value)), typeTransform);
55
- const funcRandomFloat = new Constant((value) => value == null ? undefined : Math.random() * Number(value), Type.functionType(Type.Float, [Type.Float]), false);
67
+ const funcRandomFloat = new Constant((value) => value == null ? undefined : Math.random() * Number(value), typeTransform, false);
56
68
  const funcDecodeFloat = new Constant((value, encoding = "f64", byteOffset) => decodeFloat(value, encoding, byteOffset == null ? undefined : Number(byteOffset)), Type.functionType(Type.OptionalFloat, [Type.OptionalBuffer, Type.OptionalString, Type.OptionalInteger]));
57
69
  const funcParseFloat = new Constant((value) => value ? Number.parseFloat(value) : undefined, Type.functionType(Type.OptionalFloat, [Type.OptionalString]));
70
+ export const funcFloat = new Constant((value) => Number(value), Type.union(Type.functionType(Type.Float, [Type.Boolean]), Type.functionType(Type.Float, [Type.Timestamp]), Type.functionType(Type.Float, [Type.Integer])));
58
71
  export const constFloat = Object.assign(Object.create(null), {
59
72
  NAN: new Constant(Number.NaN),
60
73
  PositiveInfinity: new Constant(Number.POSITIVE_INFINITY),
@@ -1,3 +1,7 @@
1
- export type IntegerEncoding = "i8" | "i16" | "i16le" | "i32" | "i32le" | "i64" | "i64le" | "n8" | "n16" | "n16le" | "n32" | "n32le" | "n64" | "n64le";
1
+ import { Constant } from "../Constant.js";
2
+ export type IntegerEncoding = "i8" | "i16" | "i16le" | "i32" | "i32le" | "i64" | "i64le" | "n8" | "n16" | "n16le" | "n32" | "n32le";
3
+ export declare const boundInteger: (value: bigint) => bigint;
4
+ export declare const createInteger: (value?: number) => bigint | undefined;
2
5
  export declare const encodeInteger: (value: bigint, encoding?: IntegerEncoding) => ArrayBuffer;
6
+ export declare const funcInteger: Constant;
3
7
  export declare const constInteger: any;