affinirum 1.5.0 → 1.5.2
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/README.md +30 -14
- package/dst/Affinirum.d.ts +33 -26
- package/dst/Affinirum.js +58 -44
- package/dst/ParserState.js +23 -79
- package/dst/Scope.d.ts +16 -0
- package/dst/Scope.js +49 -0
- package/dst/atom/ArrayAtom.js +4 -1
- package/dst/cjs/Affinirum.js +57 -43
- package/dst/cjs/ParserState.js +22 -78
- package/dst/cjs/Scope.js +53 -0
- package/dst/cjs/atom/ArrayAtom.js +4 -1
- package/dst/cjs/native/Aggregable.js +1 -1
- package/dst/cjs/native/Boolean.js +3 -1
- package/dst/cjs/native/Buffer.js +32 -3
- package/dst/cjs/native/Enumerable.js +5 -3
- package/dst/cjs/native/Float.js +17 -5
- package/dst/cjs/native/Integer.js +41 -13
- package/dst/cjs/native/Iterable.js +6 -4
- package/dst/cjs/native/Number.js +45 -1
- package/dst/cjs/native/String.js +68 -22
- package/dst/cjs/native/Timestamp.js +74 -17
- package/dst/cjs/native/Unknown.js +1 -1
- package/dst/cjs/native/notation/AN.js +152 -11
- package/dst/cjs/native/notation/IP.js +1 -1
- package/dst/cjs/native/notation/JSON.js +1 -1
- package/dst/cjs/node/BlockNode.js +5 -4
- package/dst/cjs/node/CallNode.js +7 -5
- package/dst/cjs/node/SwitchNode.js +7 -9
- package/dst/native/Aggregable.js +1 -1
- package/dst/native/Boolean.js +3 -1
- package/dst/native/Buffer.d.ts +2 -0
- package/dst/native/Buffer.js +29 -2
- package/dst/native/Enumerable.js +5 -3
- package/dst/native/Float.js +17 -5
- package/dst/native/Integer.js +41 -13
- package/dst/native/Iterable.js +6 -4
- package/dst/native/Number.d.ts +4 -0
- package/dst/native/Number.js +40 -0
- package/dst/native/String.d.ts +2 -7
- package/dst/native/String.js +65 -14
- package/dst/native/Timestamp.d.ts +4 -2
- package/dst/native/Timestamp.js +71 -16
- package/dst/native/Unknown.js +1 -1
- package/dst/native/notation/AN.d.ts +1 -0
- package/dst/native/notation/AN.js +152 -12
- package/dst/native/notation/IP.js +1 -1
- package/dst/native/notation/JSON.js +1 -1
- package/dst/node/BlockNode.js +5 -4
- package/dst/node/CallNode.js +7 -5
- package/dst/node/SwitchNode.js +7 -9
- package/package.json +1 -1
- package/dst/StaticScope.d.ts +0 -13
- package/dst/StaticScope.js +0 -44
- package/dst/cjs/StaticScope.js +0 -48
package/dst/cjs/Affinirum.js
CHANGED
|
@@ -25,18 +25,19 @@ const ObjectNode_js_1 = require("./node/ObjectNode.js");
|
|
|
25
25
|
const SwitchNode_js_1 = require("./node/SwitchNode.js");
|
|
26
26
|
const VariableNode_js_1 = require("./node/VariableNode.js");
|
|
27
27
|
const ParserState_js_1 = require("./ParserState.js");
|
|
28
|
-
const
|
|
28
|
+
const Scope_js_1 = require("./Scope.js");
|
|
29
29
|
const JumpException_js_1 = require("./JumpException.js");
|
|
30
30
|
class Affinirum {
|
|
31
31
|
static keywords = [...Keywords_js_1.Keywords, ...Constants_js_1.Constants.map((c) => c[0])];
|
|
32
32
|
_script;
|
|
33
33
|
_strict;
|
|
34
34
|
_root;
|
|
35
|
+
_vartypes = new Map();
|
|
35
36
|
_varframes = new Map();
|
|
36
37
|
_variables = new Map();
|
|
37
38
|
_constants = new Map(Constants_js_1.Constants);
|
|
38
39
|
_functions = new Map(Functions_js_1.Functions);
|
|
39
|
-
_scope = new
|
|
40
|
+
_scope = new Scope_js_1.Scope();
|
|
40
41
|
/**
|
|
41
42
|
Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
|
|
42
43
|
@param script Math expression to compile.
|
|
@@ -49,7 +50,7 @@ class Affinirum {
|
|
|
49
50
|
this._strict = config?.strict ?? false;
|
|
50
51
|
if (config?.variables) {
|
|
51
52
|
for (const [vname, vtype] of Object.entries(config.variables)) {
|
|
52
|
-
this.
|
|
53
|
+
this._vartypes.set(vname, vtype);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
const state = new ParserState_js_1.ParserState(this._script);
|
|
@@ -84,31 +85,29 @@ class Affinirum {
|
|
|
84
85
|
*/
|
|
85
86
|
variables() {
|
|
86
87
|
const types = Object.create(null);
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
types[name] = variables[name].type;
|
|
88
|
+
for (const [name, variable] of this._variables) {
|
|
89
|
+
types[name] = variable.type;
|
|
90
90
|
}
|
|
91
91
|
return types;
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
|
-
Evaluates compiled expression using provided
|
|
95
|
-
@param values
|
|
96
|
-
@returns
|
|
94
|
+
Evaluates the compiled expression using the provided input values.
|
|
95
|
+
@param values Values for the expression's input variables. Omitted when there are no input variables.
|
|
96
|
+
@returns The evaluated result.
|
|
97
|
+
@throws If an input value is missing or evaluation fails.
|
|
97
98
|
*/
|
|
98
99
|
evaluate(values) {
|
|
99
|
-
const variables = this._scope.variables();
|
|
100
|
-
for (const name in variables) {
|
|
101
|
-
if (!values || !Object.hasOwn(values, name)) {
|
|
102
|
-
this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
103
|
-
}
|
|
104
|
-
const variable = variables[name];
|
|
105
|
-
const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
|
|
106
|
-
if (!variable.type.acceptValue(value)) {
|
|
107
|
-
this._varframes.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
108
|
-
}
|
|
109
|
-
variable.value = value;
|
|
110
|
-
}
|
|
111
100
|
try {
|
|
101
|
+
for (const [name, variable] of this._variables) {
|
|
102
|
+
if (!values || !Object.hasOwn(values, name)) {
|
|
103
|
+
this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
104
|
+
}
|
|
105
|
+
const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
|
|
106
|
+
if (!variable.type.acceptValue(value)) {
|
|
107
|
+
this._varframes.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
108
|
+
}
|
|
109
|
+
variable.value = value;
|
|
110
|
+
}
|
|
112
111
|
return (0, Value_js_1.normalize)(this._root.evaluate() ?? undefined);
|
|
113
112
|
}
|
|
114
113
|
catch (e) {
|
|
@@ -122,12 +121,25 @@ class Affinirum {
|
|
|
122
121
|
}
|
|
123
122
|
throw e;
|
|
124
123
|
}
|
|
124
|
+
finally {
|
|
125
|
+
for (const variable of this._variables.values()) {
|
|
126
|
+
variable.value = undefined;
|
|
127
|
+
}
|
|
128
|
+
this._scope.clear();
|
|
129
|
+
}
|
|
125
130
|
}
|
|
126
|
-
/**
|
|
127
|
-
|
|
131
|
+
/** Parses an Affinirum Notation string into a JavaScript value.
|
|
132
|
+
@param value Affinirum Notation string to parse.
|
|
133
|
+
@returns The parsed value.
|
|
134
|
+
@throws If string is not a valid Affinirum Notation string.
|
|
135
|
+
*/
|
|
136
|
+
static parse(value) {
|
|
137
|
+
return (0, AN_js_1.parseAN)(value);
|
|
138
|
+
}
|
|
139
|
+
/** Formats a JavaScript value as an Affinirum Notation string.
|
|
128
140
|
@param value Value to format.
|
|
129
|
-
@param whitespace Optional
|
|
130
|
-
@returns
|
|
141
|
+
@param whitespace Optional whitespace used for each indentation level.
|
|
142
|
+
@returns Affinirum Notation formatted string.
|
|
131
143
|
*/
|
|
132
144
|
static format(value, whitespace) {
|
|
133
145
|
return (0, AN_js_1.formatAN)(value, whitespace);
|
|
@@ -330,20 +342,14 @@ class Affinirum {
|
|
|
330
342
|
state.next();
|
|
331
343
|
return new ConstantNode_js_1.ConstantNode(frame, constant);
|
|
332
344
|
}
|
|
333
|
-
let variable = scope.get(state.token);
|
|
345
|
+
let variable = scope.get(state.token) ?? this._variables.get(state.token);
|
|
334
346
|
if (variable == null) {
|
|
335
|
-
|
|
336
|
-
if (
|
|
337
|
-
|
|
338
|
-
state.throwError(`undefined variable ${state.token} in strict mode`);
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
variable = new Variable_js_1.Variable();
|
|
342
|
-
}
|
|
347
|
+
const type = this._vartypes.get(state.token);
|
|
348
|
+
if (type == null && this._strict) {
|
|
349
|
+
state.throwError(`undefined variable ${state.token} in strict mode`);
|
|
343
350
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
if (!this._varframes.has(state.token)) {
|
|
351
|
+
variable = new Variable_js_1.Variable(type);
|
|
352
|
+
this._variables.set(state.token, variable);
|
|
347
353
|
this._varframes.set(state.token, state.starts());
|
|
348
354
|
}
|
|
349
355
|
if (state.next().isAssignment) {
|
|
@@ -365,9 +371,10 @@ class Affinirum {
|
|
|
365
371
|
}
|
|
366
372
|
else if (state.isBracesOpen) { // code block
|
|
367
373
|
const frame = state.starts();
|
|
374
|
+
const subscope = scope.subscope();
|
|
368
375
|
const subnodes = [];
|
|
369
376
|
while (!state.next().isBracesClose) {
|
|
370
|
-
subnodes.push(this._unit(state,
|
|
377
|
+
subnodes.push(this._unit(state, subscope));
|
|
371
378
|
if (!state.isSemicolon) {
|
|
372
379
|
break;
|
|
373
380
|
}
|
|
@@ -434,7 +441,7 @@ class Affinirum {
|
|
|
434
441
|
type = this._type(state.next(), scope);
|
|
435
442
|
}
|
|
436
443
|
const variable = new Variable_js_1.Variable(type, assignable);
|
|
437
|
-
scope.
|
|
444
|
+
scope.set(token, variable);
|
|
438
445
|
if (state.isAssignment) {
|
|
439
446
|
if (state.assignment.operator) {
|
|
440
447
|
state.throwError(`illegal assignment operator with variable ${token}`);
|
|
@@ -483,7 +490,7 @@ class Affinirum {
|
|
|
483
490
|
state.throwError("missing function argument name");
|
|
484
491
|
}
|
|
485
492
|
const token = state.token;
|
|
486
|
-
if (scope.
|
|
493
|
+
if (scope.has(token)) {
|
|
487
494
|
state.throwError("variable redefinition");
|
|
488
495
|
}
|
|
489
496
|
let argType = Type_js_1.Type.Unknown;
|
|
@@ -511,10 +518,17 @@ class Affinirum {
|
|
|
511
518
|
const retType = state.isColon ? this._type(state.next(), scope) : Type_js_1.Type.Unknown;
|
|
512
519
|
frame.ends(state);
|
|
513
520
|
const args = Array.from(variables.values());
|
|
514
|
-
const
|
|
521
|
+
const subscope = scope.subscope(variables);
|
|
522
|
+
const subnode = this._unit(state, subscope);
|
|
515
523
|
const func = (...values) => {
|
|
516
|
-
|
|
517
|
-
|
|
524
|
+
const previous = subscope.enter();
|
|
525
|
+
try {
|
|
526
|
+
args.forEach((arg, ix) => arg.value = (0, Value_js_1.normalize)(values[ix]));
|
|
527
|
+
return (0, Value_js_1.normalize)(subnode.evaluate());
|
|
528
|
+
}
|
|
529
|
+
finally {
|
|
530
|
+
subscope.leave(previous);
|
|
531
|
+
}
|
|
518
532
|
};
|
|
519
533
|
const constant = new Constant_js_1.Constant(func, Type_js_1.Type.functionType(retType, args.map((v) => v.type), variadic));
|
|
520
534
|
return new ConstantNode_js_1.ConstantNode(frame, constant, subnode);
|
package/dst/cjs/ParserState.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
458
|
-
|
|
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
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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,
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
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}`);
|
package/dst/cjs/Scope.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Scope = void 0;
|
|
4
|
+
class Scope {
|
|
5
|
+
_subscopes = [];
|
|
6
|
+
_superscope;
|
|
7
|
+
_variables;
|
|
8
|
+
_boundary;
|
|
9
|
+
constructor(superscope, variables) {
|
|
10
|
+
superscope?._subscopes.push(this);
|
|
11
|
+
this._superscope = superscope;
|
|
12
|
+
this._variables = variables ?? new Map();
|
|
13
|
+
this._boundary = Boolean(variables);
|
|
14
|
+
}
|
|
15
|
+
has(name) {
|
|
16
|
+
return this._variables.has(name) || Boolean(this._superscope?.has(name));
|
|
17
|
+
}
|
|
18
|
+
get(name) {
|
|
19
|
+
return this._variables.get(name) ?? this._superscope?.get(name);
|
|
20
|
+
}
|
|
21
|
+
set(name, variable) {
|
|
22
|
+
this._variables.set(name, variable);
|
|
23
|
+
}
|
|
24
|
+
subscope(variables) {
|
|
25
|
+
return new Scope(this, variables);
|
|
26
|
+
}
|
|
27
|
+
enter(values = new Map()) {
|
|
28
|
+
for (const variable of this._variables.values()) {
|
|
29
|
+
values.set(variable, variable.value);
|
|
30
|
+
variable.value = undefined;
|
|
31
|
+
}
|
|
32
|
+
for (const subscope of this._subscopes) {
|
|
33
|
+
if (!subscope._boundary) {
|
|
34
|
+
subscope.enter(values);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return values;
|
|
38
|
+
}
|
|
39
|
+
leave(values) {
|
|
40
|
+
for (const [variable, value] of values) {
|
|
41
|
+
variable.value = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
clear() {
|
|
45
|
+
for (const variable of this._variables.values()) {
|
|
46
|
+
variable.value = undefined;
|
|
47
|
+
}
|
|
48
|
+
for (const subscope of this._subscopes) {
|
|
49
|
+
subscope.clear();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.Scope = Scope;
|
|
@@ -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
|
|
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
|
|
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) {
|
package/dst/cjs/native/Buffer.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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,
|
|
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,
|
|
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)));
|
package/dst/cjs/native/Float.js
CHANGED
|
@@ -34,12 +34,24 @@ const decodeFloat = (value, encoding = "f64", byteOffset) => {
|
|
|
34
34
|
if (value == null) {
|
|
35
35
|
return undefined;
|
|
36
36
|
}
|
|
37
|
-
const
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
case "
|
|
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
|
|
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
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
case "
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
case "
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
case "
|
|
88
|
-
|
|
89
|
-
|
|
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
|
|
31
|
-
|
|
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
|
|
42
|
-
|
|
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])));
|