@jplorg/jpl 1.0.0
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 +34 -0
- package/lib/applyDefaults.js +34 -0
- package/lib/builtins/funcContains.js +25 -0
- package/lib/builtins/funcEndsWith.js +21 -0
- package/lib/builtins/funcError.js +11 -0
- package/lib/builtins/funcFromJSON.js +26 -0
- package/lib/builtins/funcHas.js +24 -0
- package/lib/builtins/funcIn.js +24 -0
- package/lib/builtins/funcKeys.js +20 -0
- package/lib/builtins/funcLength.js +24 -0
- package/lib/builtins/funcNow.js +10 -0
- package/lib/builtins/funcStartsWith.js +21 -0
- package/lib/builtins/funcToJSON.js +10 -0
- package/lib/builtins/funcToNumber.js +26 -0
- package/lib/builtins/funcToString.js +10 -0
- package/lib/builtins/funcTrim.js +17 -0
- package/lib/builtins/funcTrimEnd.js +17 -0
- package/lib/builtins/funcTrimStart.js +17 -0
- package/lib/builtins/funcType.js +10 -0
- package/lib/builtins/funcVoid.js +10 -0
- package/lib/builtins/index.js +158 -0
- package/lib/builtins/internals.js +8 -0
- package/lib/builtins/math.js +40 -0
- package/lib/builtins/native.js +164 -0
- package/lib/createSingleton.js +14 -0
- package/lib/index.js +80 -0
- package/lib/interpreter/index.js +57 -0
- package/lib/interpreter/parse.js +2377 -0
- package/lib/interpreter/parseFunction.js +24 -0
- package/lib/interpreter/system.js +10 -0
- package/lib/interpreter/util.js +349 -0
- package/lib/library/apply.js +142 -0
- package/lib/library/definition.js +9 -0
- package/lib/library/errors/adaptErrors.js +34 -0
- package/lib/library/errors/enclosure.js +16 -0
- package/lib/library/errors/error.js +18 -0
- package/lib/library/errors/execution.js +20 -0
- package/lib/library/errors/fatal.js +15 -0
- package/lib/library/errors/runtime.js +78 -0
- package/lib/library/errors/syntax.js +15 -0
- package/lib/library/functions.js +82 -0
- package/lib/library/index.js +336 -0
- package/lib/library/mux.js +106 -0
- package/lib/library/ops/index.js +60 -0
- package/lib/library/ops/op.js +180 -0
- package/lib/library/ops/opa.js +41 -0
- package/lib/library/ops/opc.js +59 -0
- package/lib/library/ops/opm.js +50 -0
- package/lib/library/ops/opu.js +77 -0
- package/lib/library/runtimeScope.js +38 -0
- package/lib/library/runtimeSignal.js +72 -0
- package/lib/library/types.js +305 -0
- package/lib/program/index.js +72 -0
- package/lib/program/ops/index.js +61 -0
- package/lib/program/ops/opAccess/index.js +55 -0
- package/lib/program/ops/opAccess/opaField.js +50 -0
- package/lib/program/ops/opAccess/opaFunction.js +50 -0
- package/lib/program/ops/opAccess/opaIter.js +31 -0
- package/lib/program/ops/opAccess/opaSlice.js +44 -0
- package/lib/program/ops/opAnd.js +30 -0
- package/lib/program/ops/opArrayConstructor.js +20 -0
- package/lib/program/ops/opAssignment/index.js +90 -0
- package/lib/program/ops/opAssignment/opaAssignField.js +125 -0
- package/lib/program/ops/opAssignment/opaAssignIter.js +69 -0
- package/lib/program/ops/opAssignment/opaAssignSlice.js +99 -0
- package/lib/program/ops/opAssignment/opuAddition.js +31 -0
- package/lib/program/ops/opAssignment/opuDivision.js +31 -0
- package/lib/program/ops/opAssignment/opuMultiplication.js +31 -0
- package/lib/program/ops/opAssignment/opuNullCoalescence.js +25 -0
- package/lib/program/ops/opAssignment/opuRemainder.js +31 -0
- package/lib/program/ops/opAssignment/opuSet.js +19 -0
- package/lib/program/ops/opAssignment/opuSubtraction.js +31 -0
- package/lib/program/ops/opAssignment/opuUpdate.js +19 -0
- package/lib/program/ops/opCalculation/index.js +57 -0
- package/lib/program/ops/opCalculation/opmAddition.js +49 -0
- package/lib/program/ops/opCalculation/opmDivision.js +42 -0
- package/lib/program/ops/opCalculation/opmMultiplication.js +40 -0
- package/lib/program/ops/opCalculation/opmRemainder.js +34 -0
- package/lib/program/ops/opCalculation/opmSubtraction.js +53 -0
- package/lib/program/ops/opComparison/index.js +59 -0
- package/lib/program/ops/opComparison/opcEqual.js +19 -0
- package/lib/program/ops/opComparison/opcGreater.js +19 -0
- package/lib/program/ops/opComparison/opcGreaterEqual.js +19 -0
- package/lib/program/ops/opComparison/opcLess.js +19 -0
- package/lib/program/ops/opComparison/opcLessEqual.js +19 -0
- package/lib/program/ops/opComparison/opcUnequal.js +19 -0
- package/lib/program/ops/opConstant.js +18 -0
- package/lib/program/ops/opConstantFalse.js +16 -0
- package/lib/program/ops/opConstantNull.js +16 -0
- package/lib/program/ops/opConstantTrue.js +16 -0
- package/lib/program/ops/opFunctionDefinition.js +21 -0
- package/lib/program/ops/opIf.js +38 -0
- package/lib/program/ops/opInterpolatedString.js +24 -0
- package/lib/program/ops/opNegation.js +25 -0
- package/lib/program/ops/opNot.js +16 -0
- package/lib/program/ops/opNullCoalescence.js +34 -0
- package/lib/program/ops/opNumber.js +18 -0
- package/lib/program/ops/opObjectConstructor.js +37 -0
- package/lib/program/ops/opOr.js +30 -0
- package/lib/program/ops/opOutputConcat.js +19 -0
- package/lib/program/ops/opString.js +18 -0
- package/lib/program/ops/opTry.js +37 -0
- package/lib/program/ops/opVariable.js +20 -0
- package/lib/program/ops/opVariableDefinition.js +24 -0
- package/lib/program/ops/opVoid.js +16 -0
- package/lib/program/ops/utils.js +35 -0
- package/lib/runtime/index.js +258 -0
- package/package.json +67 -0
- package/repl.js +212 -0
- package/src/applyDefaults.js +35 -0
- package/src/builtins/funcContains.js +27 -0
- package/src/builtins/funcEndsWith.js +21 -0
- package/src/builtins/funcError.js +7 -0
- package/src/builtins/funcFromJSON.js +24 -0
- package/src/builtins/funcHas.js +25 -0
- package/src/builtins/funcIn.js +25 -0
- package/src/builtins/funcKeys.js +20 -0
- package/src/builtins/funcLength.js +26 -0
- package/src/builtins/funcNow.js +5 -0
- package/src/builtins/funcStartsWith.js +21 -0
- package/src/builtins/funcToJSON.js +5 -0
- package/src/builtins/funcToNumber.js +25 -0
- package/src/builtins/funcToString.js +5 -0
- package/src/builtins/funcTrim.js +16 -0
- package/src/builtins/funcTrimEnd.js +16 -0
- package/src/builtins/funcTrimStart.js +16 -0
- package/src/builtins/funcType.js +5 -0
- package/src/builtins/funcVoid.js +5 -0
- package/src/builtins/index.js +145 -0
- package/src/builtins/internals.js +9 -0
- package/src/builtins/math.js +44 -0
- package/src/builtins/native.js +19 -0
- package/src/createSingleton.js +9 -0
- package/src/index.js +44 -0
- package/src/interpreter/index.js +48 -0
- package/src/interpreter/parse.js +1473 -0
- package/src/interpreter/parseFunction.js +20 -0
- package/src/interpreter/system.js +5 -0
- package/src/interpreter/util.js +237 -0
- package/src/library/apply.js +135 -0
- package/src/library/definition.js +3 -0
- package/src/library/errors/adaptErrors.js +28 -0
- package/src/library/errors/enclosure.js +11 -0
- package/src/library/errors/error.js +14 -0
- package/src/library/errors/execution.js +15 -0
- package/src/library/errors/fatal.js +10 -0
- package/src/library/errors/runtime.js +71 -0
- package/src/library/errors/syntax.js +10 -0
- package/src/library/functions.js +73 -0
- package/src/library/index.js +41 -0
- package/src/library/mux.js +98 -0
- package/src/library/ops/index.js +5 -0
- package/src/library/ops/op.js +174 -0
- package/src/library/ops/opa.js +35 -0
- package/src/library/ops/opc.js +53 -0
- package/src/library/ops/opm.js +44 -0
- package/src/library/ops/opu.js +71 -0
- package/src/library/runtimeScope.js +35 -0
- package/src/library/runtimeSignal.js +67 -0
- package/src/library/types.js +299 -0
- package/src/program/index.js +80 -0
- package/src/program/ops/index.js +82 -0
- package/src/program/ops/opAccess/index.js +55 -0
- package/src/program/ops/opAccess/opaField.js +58 -0
- package/src/program/ops/opAccess/opaFunction.js +52 -0
- package/src/program/ops/opAccess/opaIter.js +31 -0
- package/src/program/ops/opAccess/opaSlice.js +58 -0
- package/src/program/ops/opAnd.js +32 -0
- package/src/program/ops/opArrayConstructor.js +16 -0
- package/src/program/ops/opAssignment/index.js +102 -0
- package/src/program/ops/opAssignment/opaAssignField.js +156 -0
- package/src/program/ops/opAssignment/opaAssignIter.js +74 -0
- package/src/program/ops/opAssignment/opaAssignSlice.js +157 -0
- package/src/program/ops/opAssignment/opuAddition.js +29 -0
- package/src/program/ops/opAssignment/opuDivision.js +29 -0
- package/src/program/ops/opAssignment/opuMultiplication.js +29 -0
- package/src/program/ops/opAssignment/opuNullCoalescence.js +26 -0
- package/src/program/ops/opAssignment/opuRemainder.js +29 -0
- package/src/program/ops/opAssignment/opuSet.js +15 -0
- package/src/program/ops/opAssignment/opuSubtraction.js +29 -0
- package/src/program/ops/opAssignment/opuUpdate.js +15 -0
- package/src/program/ops/opCalculation/index.js +64 -0
- package/src/program/ops/opCalculation/opmAddition.js +63 -0
- package/src/program/ops/opCalculation/opmDivision.js +47 -0
- package/src/program/ops/opCalculation/opmMultiplication.js +52 -0
- package/src/program/ops/opCalculation/opmRemainder.js +49 -0
- package/src/program/ops/opCalculation/opmSubtraction.js +72 -0
- package/src/program/ops/opComparison/index.js +67 -0
- package/src/program/ops/opComparison/opcEqual.js +17 -0
- package/src/program/ops/opComparison/opcGreater.js +17 -0
- package/src/program/ops/opComparison/opcGreaterEqual.js +17 -0
- package/src/program/ops/opComparison/opcLess.js +17 -0
- package/src/program/ops/opComparison/opcLessEqual.js +17 -0
- package/src/program/ops/opComparison/opcUnequal.js +17 -0
- package/src/program/ops/opConstant.js +13 -0
- package/src/program/ops/opConstantFalse.js +11 -0
- package/src/program/ops/opConstantNull.js +11 -0
- package/src/program/ops/opConstantTrue.js +11 -0
- package/src/program/ops/opFunctionDefinition.js +17 -0
- package/src/program/ops/opIf.js +44 -0
- package/src/program/ops/opInterpolatedString.js +27 -0
- package/src/program/ops/opNegation.js +26 -0
- package/src/program/ops/opNot.js +11 -0
- package/src/program/ops/opNullCoalescence.js +36 -0
- package/src/program/ops/opNumber.js +13 -0
- package/src/program/ops/opObjectConstructor.js +40 -0
- package/src/program/ops/opOr.js +32 -0
- package/src/program/ops/opOutputConcat.js +17 -0
- package/src/program/ops/opString.js +13 -0
- package/src/program/ops/opTry.js +38 -0
- package/src/program/ops/opVariable.js +17 -0
- package/src/program/ops/opVariableDefinition.js +18 -0
- package/src/program/ops/opVoid.js +11 -0
- package/src/program/ops/utils.js +19 -0
- package/src/runtime/index.js +309 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.JPLType = void 0;
|
|
7
|
+
exports.assertType = assertType;
|
|
8
|
+
exports.displayValue = displayValue;
|
|
9
|
+
exports.jplJSONStripper = jplJSONStripper;
|
|
10
|
+
exports.jplStripper = jplStripper;
|
|
11
|
+
exports.jplTypedStripper = jplTypedStripper;
|
|
12
|
+
exports.jsonStripper = jsonStripper;
|
|
13
|
+
exports.normalize = normalize;
|
|
14
|
+
exports.rawStripper = rawStripper;
|
|
15
|
+
exports.strictDisplayValue = strictDisplayValue;
|
|
16
|
+
exports.stringify = stringify;
|
|
17
|
+
exports.strip = strip;
|
|
18
|
+
exports.template = template;
|
|
19
|
+
exports.typeOf = typeOf;
|
|
20
|
+
exports.typeOrder = void 0;
|
|
21
|
+
exports.unwrap = unwrap;
|
|
22
|
+
var _apply = require("./apply");
|
|
23
|
+
var _adaptErrors = _interopRequireWildcard(require("./errors/adaptErrors"));
|
|
24
|
+
var _fatal = _interopRequireDefault(require("./errors/fatal"));
|
|
25
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
26
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
27
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
28
|
+
/**
|
|
29
|
+
* Generic type for handling special formatting on values.
|
|
30
|
+
*
|
|
31
|
+
* The value accessor should be used in JPL expressions, where type safety may be considered important.
|
|
32
|
+
*
|
|
33
|
+
* The json accessor should be used in program outputs, where the format of the value should be applied and the original type may be ignored.
|
|
34
|
+
*
|
|
35
|
+
* The alter function is called when the value is being changed by an operation.
|
|
36
|
+
* The JPLType is responsible to decide whether the resulting value should be wrapped in a new JPLType or not.
|
|
37
|
+
* It may also return itself if the value did not change.
|
|
38
|
+
* Note that the resulting value may have another type than the original value.
|
|
39
|
+
* Commonly, if multiple values wrapped in JPLTypes are involved in an operation, the leftmost operand should be altered whereas all other operands should be unwrapped using their value accessors, effectively loosing their formatting.
|
|
40
|
+
*
|
|
41
|
+
* However, both accessors should represent the same essential value.
|
|
42
|
+
* For example, a JPLType that applies rounding to numbers with a fixed number of decimal digits, may return the rounded numeric value for its value accessor (e.g. `1`), whereas it may return a formatted string for its JSON accessor (e.g. `"1.00"`).
|
|
43
|
+
* This allows this JPLType to be processed in JPL operations like generic numbers but resolves to formatted strings in the program output.
|
|
44
|
+
*/
|
|
45
|
+
class JPLType {
|
|
46
|
+
/** Detect whether the provided value is an instance of this JPLType or one of its subclasses */
|
|
47
|
+
static is(value) {
|
|
48
|
+
return value instanceof this;
|
|
49
|
+
}
|
|
50
|
+
constructor(value) {
|
|
51
|
+
this._v = normalizeInternalValue(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Resolve the internal value for usage in JPL operations */
|
|
55
|
+
get value() {
|
|
56
|
+
return this._v;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Resolve the JSON value for usage in program outputs */
|
|
60
|
+
get json() {
|
|
61
|
+
return this._v;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Alter the internal value using the specified updater.
|
|
66
|
+
* The result may or may not be wrapped into a new JPLType if desired.
|
|
67
|
+
* The JPLType may return itself if the value did not change.
|
|
68
|
+
*/
|
|
69
|
+
async alter(updater) {
|
|
70
|
+
return normalize(await updater(this.value));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Support for `JSON.stringify` like procedures (see `JPLType.json`).
|
|
75
|
+
*
|
|
76
|
+
* It is generally recommended to override the `get json` member access function instead of this method.
|
|
77
|
+
*/
|
|
78
|
+
toJSON() {
|
|
79
|
+
try {
|
|
80
|
+
return this.json;
|
|
81
|
+
} catch (err) {
|
|
82
|
+
throw (0, _adaptErrors.adaptError)(err);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Normalize the specified external value to be used in a JPLType */
|
|
88
|
+
exports.JPLType = JPLType;
|
|
89
|
+
function normalizeInternalValue(value) {
|
|
90
|
+
if (JPLType.is(value)) return value.value;
|
|
91
|
+
return normalize(value);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Normalize the specified external value */
|
|
95
|
+
function normalize(value) {
|
|
96
|
+
return strip(value, undefined, jplTypedStripper);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Unwrap the specified normalized value */
|
|
100
|
+
function unwrap(value) {
|
|
101
|
+
if (JPLType.is(value)) return (0, _adaptErrors.default)(() => value.value);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Order which applies when comparing values with different types */
|
|
106
|
+
const typeOrder = exports.typeOrder = ['null', 'function', 'boolean', 'number', 'string', 'array', 'object'];
|
|
107
|
+
|
|
108
|
+
/** Resolve the type of the specified normalized value */
|
|
109
|
+
function typeOf(value) {
|
|
110
|
+
const v = unwrap(value);
|
|
111
|
+
if (v === null) return 'null';
|
|
112
|
+
if (Array.isArray(v)) return 'array';
|
|
113
|
+
const t = typeof v;
|
|
114
|
+
switch (t) {
|
|
115
|
+
case 'function':
|
|
116
|
+
case 'boolean':
|
|
117
|
+
case 'number':
|
|
118
|
+
case 'string':
|
|
119
|
+
case 'object':
|
|
120
|
+
return t;
|
|
121
|
+
default:
|
|
122
|
+
throw new _fatal.default(`invalid type ${t} (${value})`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Assert the type for the specified unwrapped value */
|
|
127
|
+
function assertType(value, type) {
|
|
128
|
+
if (JPLType.is(value)) {
|
|
129
|
+
throw new _fatal.default(template('unexpected type: JPLTypes (%*<100v) are not allowed here', value));
|
|
130
|
+
}
|
|
131
|
+
const t = typeOf(value);
|
|
132
|
+
if (t !== type) {
|
|
133
|
+
throw new _fatal.default(template('unexpected type: %s (%*<100v) cannot be used as %s', t, value, type));
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Stringify the specified normalized value */
|
|
139
|
+
function stringify(value, unescapeString, escapeFunctions) {
|
|
140
|
+
const rawValue = escapeFunctions ? strip(value, (k, v) => typeof unwrap(v) === 'function' ? '<function>' : v) : strip(value);
|
|
141
|
+
if (unescapeString && typeof rawValue === 'string') return rawValue;
|
|
142
|
+
return JSON.stringify(rawValue);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Format the specified template string.
|
|
147
|
+
* The general form of a format is a percent sign, followed by optional flags, an optional width and a verb.
|
|
148
|
+
*
|
|
149
|
+
* Examples:
|
|
150
|
+
* - `%10s`: Format the next replacement as a string and pads the result at the left with spaces to be at least 10 unicode codepoints long.
|
|
151
|
+
* - `%*<10v`: Format the next replacement as a JSON value and truncates it, if it is longer then 10 unicode codepoints.
|
|
152
|
+
*
|
|
153
|
+
* The width specifies the desired field width and defaults to whatever is necessary to display the full replacement.
|
|
154
|
+
* If the width is specified without any other corresponding flags, it is used for padding the field if necessary.
|
|
155
|
+
*
|
|
156
|
+
* Valid flags:
|
|
157
|
+
*
|
|
158
|
+
* - `*`: Do not pad the value even if it is shorter than the specified width
|
|
159
|
+
* - `-`: Pad the value at the right rather than the left
|
|
160
|
+
* - `<`: Truncate the value at the right if it is too long for the specified width
|
|
161
|
+
*
|
|
162
|
+
* Valid verbs:
|
|
163
|
+
*
|
|
164
|
+
* - `%`: Returns a literal `%`
|
|
165
|
+
* - `s`: Format the next replacement as a string (like JSON, but does not escape strings)
|
|
166
|
+
* - `v`: Format the next replacement as a JSON value
|
|
167
|
+
*/
|
|
168
|
+
function template(tmpl, ...replacements) {
|
|
169
|
+
let i = 0;
|
|
170
|
+
return displayValue(tmpl).replace(/%([*\-<]+)?([1-9][0-9]*)?(.)/g, (match, flags, width, verb) => {
|
|
171
|
+
// verbs without replacement
|
|
172
|
+
switch (verb) {
|
|
173
|
+
case '%':
|
|
174
|
+
return '%';
|
|
175
|
+
default:
|
|
176
|
+
}
|
|
177
|
+
// verbs with replacement
|
|
178
|
+
const value = replacements[i] ?? null;
|
|
179
|
+
i += 1;
|
|
180
|
+
let result;
|
|
181
|
+
switch (verb) {
|
|
182
|
+
case 's':
|
|
183
|
+
result = displayValue(value);
|
|
184
|
+
break;
|
|
185
|
+
case 'v':
|
|
186
|
+
result = strictDisplayValue(value);
|
|
187
|
+
break;
|
|
188
|
+
default:
|
|
189
|
+
throw new _fatal.default(`format ${match} has unknown verb ${verb}`);
|
|
190
|
+
}
|
|
191
|
+
let pad = true;
|
|
192
|
+
let padRight = false;
|
|
193
|
+
let trunc = false;
|
|
194
|
+
[...(flags ?? [])].forEach(flag => {
|
|
195
|
+
switch (flag) {
|
|
196
|
+
case '*':
|
|
197
|
+
pad = false;
|
|
198
|
+
break;
|
|
199
|
+
case '-':
|
|
200
|
+
padRight = true;
|
|
201
|
+
break;
|
|
202
|
+
case '<':
|
|
203
|
+
trunc = true;
|
|
204
|
+
break;
|
|
205
|
+
default:
|
|
206
|
+
throw new _fatal.default(`format ${match} has unknown flag ${flag}`);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
const w = +(width ?? 0);
|
|
210
|
+
if (w > 0) {
|
|
211
|
+
const rl = [...result].length;
|
|
212
|
+
if (pad && rl < w) {
|
|
213
|
+
const padding = ' '.repeat(w - rl);
|
|
214
|
+
result = padRight ? `${result}${padding}` : `${padding}${result}`;
|
|
215
|
+
} else if (trunc && rl > w) {
|
|
216
|
+
result = `${result.substring(0, w - 1)}…`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Format the specified normalized value as a string */
|
|
224
|
+
function displayValue(value) {
|
|
225
|
+
return stringify(value, true, true);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Format the specified normalized value as a string, without removing escaping */
|
|
229
|
+
function strictDisplayValue(value) {
|
|
230
|
+
return stringify(value, false, true);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Unwrap the specified value similar to `JSON.stringify`.
|
|
235
|
+
* However, unlike with `JSON.stringify`, object member functions `toJSON` are not supported as they could interfere with user defined members.
|
|
236
|
+
* Instead, JPLTypes are being unwrapped by default.
|
|
237
|
+
*
|
|
238
|
+
* A custom stripper can be provided to customize the behavior.
|
|
239
|
+
*/
|
|
240
|
+
function strip(value, replacer, stripper = jplJSONStripper) {
|
|
241
|
+
const iter = (k, v) => {
|
|
242
|
+
let r = v;
|
|
243
|
+
if (replacer) r = replacer(k ?? '', r);
|
|
244
|
+
return stripper(k, r, iter);
|
|
245
|
+
};
|
|
246
|
+
return iter(null, value);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Stripper that allows JPLTypes and normalized values */
|
|
250
|
+
function jplTypedStripper(k, v, iter) {
|
|
251
|
+
if (JPLType.is(v)) return v;
|
|
252
|
+
if (typeof v === 'function') return v;
|
|
253
|
+
return rawStripper(k, v, iter);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Stripper that only allows normalized values and unwraps JPLTypes */
|
|
257
|
+
function jplStripper(k, v, iter) {
|
|
258
|
+
let r = v;
|
|
259
|
+
if (JPLType.is(r)) r = r.toJSON();
|
|
260
|
+
if (typeof r === 'function') return r;
|
|
261
|
+
return rawStripper(k, r, iter);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Stripper that allows JSON like values and unwraps JPLTypes */
|
|
265
|
+
function jplJSONStripper(k, v, iter) {
|
|
266
|
+
let r = v;
|
|
267
|
+
if (JPLType.is(r)) r = r.toJSON();
|
|
268
|
+
return rawStripper(k, r, iter);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Stripper that allows JSON like values and calls `toJSON` similar to `JSON.stringify` */
|
|
272
|
+
function jsonStripper(k, v, iter) {
|
|
273
|
+
let r = v;
|
|
274
|
+
if (typeof r?.toJSON === 'function') r = r.toJSON();
|
|
275
|
+
return rawStripper(k, r, iter);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Stripper that allows JSON like values */
|
|
279
|
+
function rawStripper(k, v, iter) {
|
|
280
|
+
const top = k == null;
|
|
281
|
+
const t = typeof v;
|
|
282
|
+
switch (t) {
|
|
283
|
+
case 'string':
|
|
284
|
+
case 'boolean':
|
|
285
|
+
return v;
|
|
286
|
+
case 'number':
|
|
287
|
+
if (!Number.isFinite(v)) return null;
|
|
288
|
+
return v;
|
|
289
|
+
case 'undefined':
|
|
290
|
+
case 'function':
|
|
291
|
+
return top ? null : undefined;
|
|
292
|
+
case 'object':
|
|
293
|
+
if (v === null) return null;
|
|
294
|
+
if (Array.isArray(v)) {
|
|
295
|
+
return (0, _apply.applyArray)(v, Array.from(new Array(v.length), (_, i) => [i, iter(`${i}`, v[i]) ?? null]));
|
|
296
|
+
}
|
|
297
|
+
return (0, _apply.applyObject)(v, Object.entries(v).map(([i, s]) => {
|
|
298
|
+
const entry = iter(i, s);
|
|
299
|
+
if (entry === undefined) return [i];
|
|
300
|
+
return [i, entry];
|
|
301
|
+
}));
|
|
302
|
+
default:
|
|
303
|
+
throw new _fatal.default(`unexpected ${t}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.applyProgramDefaults = applyProgramDefaults;
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _applyDefaults = _interopRequireDefault(require("../applyDefaults"));
|
|
9
|
+
var _library = require("../library");
|
|
10
|
+
var _runtime = _interopRequireWildcard(require("../runtime"));
|
|
11
|
+
var _ops = _interopRequireDefault(require("./ops"));
|
|
12
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
13
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
const defaultOptions = {};
|
|
16
|
+
function applyProgramDefaults(options = {}, defaults = {}) {
|
|
17
|
+
return (0, _applyDefaults.default)(options, defaults);
|
|
18
|
+
}
|
|
19
|
+
function validateDefinition(programDefinition) {
|
|
20
|
+
const version = programDefinition?.version;
|
|
21
|
+
const [, major, minor] = typeof version === 'string' ? /^(\d+)\.(\d+)?$/g.exec(version) ?? [] : [];
|
|
22
|
+
if (!major || !minor || !Array.isArray(programDefinition.instructions)) {
|
|
23
|
+
throw new _library.JPLFatalError('invalid program definition');
|
|
24
|
+
}
|
|
25
|
+
if (+major !== _library.DEFINITION_VERSION_MAJOR || +minor > _library.DEFINITION_VERSION_MINOR) {
|
|
26
|
+
throw new _library.JPLFatalError(`unsupported program definition v${major}.${minor} - this version of JPL only supports v${_library.DEFINITION_VERSION_MAJOR} (up to v${_library.DEFINITION_VERSION})`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** JPL program */
|
|
31
|
+
class JPLProgram {
|
|
32
|
+
constructor(programDefinition, options) {
|
|
33
|
+
validateDefinition(programDefinition);
|
|
34
|
+
this._options = applyProgramDefaults(options?.program, defaultOptions);
|
|
35
|
+
this._runtimeOptions = options?.runtime;
|
|
36
|
+
this._definition = programDefinition;
|
|
37
|
+
this._ops = _ops.default;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Return the program's options */
|
|
41
|
+
get options() {
|
|
42
|
+
return this._options;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Return the program's definition.
|
|
47
|
+
* The definition can be serialized as JSON to be reused in other JPL implementations.
|
|
48
|
+
*/
|
|
49
|
+
get definition() {
|
|
50
|
+
return this._definition;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Return the program's OPs */
|
|
54
|
+
get ops() {
|
|
55
|
+
return this._ops;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Run the program with the provided inputs and runtime options.
|
|
60
|
+
* The program throws a JPLExecutionError (see `JPLExecutionError.is`) for runtime failures.
|
|
61
|
+
* Other errors may be thrown when execution fails.
|
|
62
|
+
*/
|
|
63
|
+
run = async (inputs, options) => {
|
|
64
|
+
const runtime = new _runtime.default(this, {
|
|
65
|
+
runtime: (0, _runtime.applyRuntimeDefaults)(options?.runtime, this._runtimeOptions)
|
|
66
|
+
});
|
|
67
|
+
const normalizedInputs = runtime.normalizeValues(inputs, 'program inputs');
|
|
68
|
+
const outputs = await runtime.execute(normalizedInputs);
|
|
69
|
+
return runtime.stripJSON(outputs);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
var _default = exports.default = JPLProgram;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../library");
|
|
8
|
+
var _opAccess = _interopRequireDefault(require("./opAccess"));
|
|
9
|
+
var _opAnd = _interopRequireDefault(require("./opAnd"));
|
|
10
|
+
var _opArrayConstructor = _interopRequireDefault(require("./opArrayConstructor"));
|
|
11
|
+
var _opAssignment = _interopRequireDefault(require("./opAssignment"));
|
|
12
|
+
var _opCalculation = _interopRequireDefault(require("./opCalculation"));
|
|
13
|
+
var _opComparison = _interopRequireDefault(require("./opComparison"));
|
|
14
|
+
var _opConstant = _interopRequireDefault(require("./opConstant"));
|
|
15
|
+
var _opConstantFalse = _interopRequireDefault(require("./opConstantFalse"));
|
|
16
|
+
var _opConstantNull = _interopRequireDefault(require("./opConstantNull"));
|
|
17
|
+
var _opConstantTrue = _interopRequireDefault(require("./opConstantTrue"));
|
|
18
|
+
var _opFunctionDefinition = _interopRequireDefault(require("./opFunctionDefinition"));
|
|
19
|
+
var _opIf = _interopRequireDefault(require("./opIf"));
|
|
20
|
+
var _opInterpolatedString = _interopRequireDefault(require("./opInterpolatedString"));
|
|
21
|
+
var _opNegation = _interopRequireDefault(require("./opNegation"));
|
|
22
|
+
var _opNot = _interopRequireDefault(require("./opNot"));
|
|
23
|
+
var _opNullCoalescence = _interopRequireDefault(require("./opNullCoalescence"));
|
|
24
|
+
var _opNumber = _interopRequireDefault(require("./opNumber"));
|
|
25
|
+
var _opObjectConstructor = _interopRequireDefault(require("./opObjectConstructor"));
|
|
26
|
+
var _opOr = _interopRequireDefault(require("./opOr"));
|
|
27
|
+
var _opOutputConcat = _interopRequireDefault(require("./opOutputConcat"));
|
|
28
|
+
var _opString = _interopRequireDefault(require("./opString"));
|
|
29
|
+
var _opTry = _interopRequireDefault(require("./opTry"));
|
|
30
|
+
var _opVariable = _interopRequireDefault(require("./opVariable"));
|
|
31
|
+
var _opVariableDefinition = _interopRequireDefault(require("./opVariableDefinition"));
|
|
32
|
+
var _opVoid = _interopRequireDefault(require("./opVoid"));
|
|
33
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
34
|
+
const ops = {
|
|
35
|
+
[_library.OP_ACCESS]: _opAccess.default,
|
|
36
|
+
[_library.OP_AND]: _opAnd.default,
|
|
37
|
+
[_library.OP_ARRAY_CONSTRUCTOR]: _opArrayConstructor.default,
|
|
38
|
+
[_library.OP_ASSIGNMENT]: _opAssignment.default,
|
|
39
|
+
[_library.OP_CALCULATION]: _opCalculation.default,
|
|
40
|
+
[_library.OP_COMPARISON]: _opComparison.default,
|
|
41
|
+
[_library.OP_CONSTANT]: _opConstant.default,
|
|
42
|
+
[_library.OP_CONSTANT_FALSE]: _opConstantFalse.default,
|
|
43
|
+
[_library.OP_CONSTANT_NULL]: _opConstantNull.default,
|
|
44
|
+
[_library.OP_CONSTANT_TRUE]: _opConstantTrue.default,
|
|
45
|
+
[_library.OP_FUNCTION_DEFINITION]: _opFunctionDefinition.default,
|
|
46
|
+
[_library.OP_IF]: _opIf.default,
|
|
47
|
+
[_library.OP_INTERPOLATED_STRING]: _opInterpolatedString.default,
|
|
48
|
+
[_library.OP_NEGATION]: _opNegation.default,
|
|
49
|
+
[_library.OP_NOT]: _opNot.default,
|
|
50
|
+
[_library.OP_NULL_COALESCENCE]: _opNullCoalescence.default,
|
|
51
|
+
[_library.OP_NUMBER]: _opNumber.default,
|
|
52
|
+
[_library.OP_OBJECT_CONSTRUCTOR]: _opObjectConstructor.default,
|
|
53
|
+
[_library.OP_OR]: _opOr.default,
|
|
54
|
+
[_library.OP_OUTPUT_CONCAT]: _opOutputConcat.default,
|
|
55
|
+
[_library.OP_STRING]: _opString.default,
|
|
56
|
+
[_library.OP_TRY]: _opTry.default,
|
|
57
|
+
[_library.OP_VARIABLE]: _opVariable.default,
|
|
58
|
+
[_library.OP_VARIABLE_DEFINITION]: _opVariableDefinition.default,
|
|
59
|
+
[_library.OP_VOID]: _opVoid.default
|
|
60
|
+
};
|
|
61
|
+
var _default = exports.default = ops;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../../library");
|
|
8
|
+
var _utils = require("../utils");
|
|
9
|
+
var _opaField = _interopRequireDefault(require("./opaField"));
|
|
10
|
+
var _opaFunction = _interopRequireDefault(require("./opaFunction"));
|
|
11
|
+
var _opaIter = _interopRequireDefault(require("./opaIter"));
|
|
12
|
+
var _opaSlice = _interopRequireDefault(require("./opaSlice"));
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
var _default = exports.default = {
|
|
15
|
+
/** { pipe: [op], selectors: [opa] } */
|
|
16
|
+
op(runtime, input, params, scope, next) {
|
|
17
|
+
const iter = async (from, value) => {
|
|
18
|
+
// Call stack decoupling - This is necessary as some browsers (i.e. Safari) have very limited call stack sizes which result in stack overflow exceptions in certain situations.
|
|
19
|
+
await undefined;
|
|
20
|
+
scope.signal.checkHealth();
|
|
21
|
+
if (from >= (params.selectors?.length ?? 0)) return next(value, scope);
|
|
22
|
+
const {
|
|
23
|
+
op,
|
|
24
|
+
params: opParams
|
|
25
|
+
} = params.selectors[from];
|
|
26
|
+
const operator = opas[op];
|
|
27
|
+
if (!operator) throw new _library.JPLFatalError(`invalid OPA '${op}'`);
|
|
28
|
+
return operator.op(runtime, input, value, opParams ?? {}, scope, output => iter(from + 1, output));
|
|
29
|
+
};
|
|
30
|
+
return runtime.executeInstructions(params.pipe ?? [], [input], scope, output => iter(0, output));
|
|
31
|
+
},
|
|
32
|
+
/** { pipe: function, selectors: [opa] } */
|
|
33
|
+
map(runtime, params) {
|
|
34
|
+
return {
|
|
35
|
+
pipe: (0, _utils.call)(params.pipe),
|
|
36
|
+
selectors: runtime.muxOne([params.selectors], ({
|
|
37
|
+
op,
|
|
38
|
+
params: opParams
|
|
39
|
+
}) => {
|
|
40
|
+
const operator = opas[op];
|
|
41
|
+
if (!operator) throw new _library.JPLFatalError(`invalid OPA '${op}'`);
|
|
42
|
+
return {
|
|
43
|
+
op: runtime.assertType(op, 'string'),
|
|
44
|
+
params: operator.map(runtime, opParams)
|
|
45
|
+
};
|
|
46
|
+
})
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const opas = {
|
|
51
|
+
[_library.OPA_FIELD]: _opaField.default,
|
|
52
|
+
[_library.OPA_FUNCTION]: _opaFunction.default,
|
|
53
|
+
[_library.OPA_ITER]: _opaIter.default,
|
|
54
|
+
[_library.OPA_SLICE]: _opaSlice.default
|
|
55
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../../library");
|
|
8
|
+
var _utils = require("../utils");
|
|
9
|
+
var _default = exports.default = {
|
|
10
|
+
/** { pipe: [op], optional: boolean } */
|
|
11
|
+
op(runtime, input, target, params, scope, next) {
|
|
12
|
+
const value = runtime.unwrapValue(target);
|
|
13
|
+
const tv = runtime.type(value);
|
|
14
|
+
return runtime.executeInstructions(params.pipe ?? [], [input], scope, output => {
|
|
15
|
+
const field = runtime.unwrapValue(output);
|
|
16
|
+
const tf = runtime.type(field);
|
|
17
|
+
switch (tv) {
|
|
18
|
+
case 'null':
|
|
19
|
+
if (['string', 'number'].includes(tf)) return next(null);
|
|
20
|
+
break;
|
|
21
|
+
case 'object':
|
|
22
|
+
if (tf === 'string') return next(value[field] ?? null);
|
|
23
|
+
break;
|
|
24
|
+
case 'array':
|
|
25
|
+
if (tf === 'number') {
|
|
26
|
+
const i = Math.trunc(field);
|
|
27
|
+
return next(value[i >= 0 ? i : value.length + i] ?? null);
|
|
28
|
+
}
|
|
29
|
+
break;
|
|
30
|
+
case 'string':
|
|
31
|
+
if (tf === 'number') {
|
|
32
|
+
const i = Math.trunc(field);
|
|
33
|
+
const chars = [...value];
|
|
34
|
+
return next(chars[i >= 0 ? i : chars.length + i] ?? null);
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
default:
|
|
38
|
+
}
|
|
39
|
+
if (params.optional) return [];
|
|
40
|
+
throw new _library.JPLTypeError('cannot access field of %s (%*<100v) with %s (%*<100v)', tv, value, tf, field);
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
/** { pipe: function, optional: boolean } */
|
|
44
|
+
map(runtime, params) {
|
|
45
|
+
return {
|
|
46
|
+
pipe: (0, _utils.call)(params.pipe),
|
|
47
|
+
optional: runtime.assertType(params.optional, 'boolean')
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../../library");
|
|
8
|
+
var _utils = require("../utils");
|
|
9
|
+
var _default = exports.default = {
|
|
10
|
+
/** { args: [[op]], bound: boolean, optional: boolean } */
|
|
11
|
+
async op(runtime, input, target, params, scope, next) {
|
|
12
|
+
const value = runtime.unwrapValue(target);
|
|
13
|
+
const t = runtime.type(value);
|
|
14
|
+
switch (t) {
|
|
15
|
+
case 'function':
|
|
16
|
+
{
|
|
17
|
+
const args = await runtime.muxAsync([params.args ?? []], arg => runtime.executeInstructions(arg ?? [], [input], scope));
|
|
18
|
+
return runtime.muxAll(args, async (...a) => {
|
|
19
|
+
try {
|
|
20
|
+
const fnNext = async output => {
|
|
21
|
+
try {
|
|
22
|
+
return await next(output);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw new _library.JPLErrorEnclosure(err);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
if (params.bound) {
|
|
28
|
+
return await value(runtime, scope.signal, fnNext, a[0] ?? null, ...a.slice(1));
|
|
29
|
+
}
|
|
30
|
+
return await value(runtime, scope.signal, fnNext, input, ...a);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
if (_library.JPLErrorEnclosure.is(err)) throw err.inner;
|
|
33
|
+
throw (0, _library.adaptError)(err);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
default:
|
|
38
|
+
}
|
|
39
|
+
if (params.optional) return [];
|
|
40
|
+
throw new _library.JPLTypeError('cannot execute %s (%*<100v)', t, value);
|
|
41
|
+
},
|
|
42
|
+
/** { args: [function], bound: boolean, optional: boolean } */
|
|
43
|
+
map(runtime, params) {
|
|
44
|
+
return {
|
|
45
|
+
args: runtime.muxOne([params.args], entry => (0, _utils.call)(entry)),
|
|
46
|
+
bound: runtime.assertType(params.bound, 'boolean'),
|
|
47
|
+
optional: runtime.assertType(params.optional, 'boolean')
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../../library");
|
|
8
|
+
var _default = exports.default = {
|
|
9
|
+
/** { optional: boolean } */
|
|
10
|
+
op(runtime, input, target, params, scope, next) {
|
|
11
|
+
const value = runtime.unwrapValue(target);
|
|
12
|
+
const t = runtime.type(value);
|
|
13
|
+
switch (t) {
|
|
14
|
+
case 'object':
|
|
15
|
+
return runtime.muxAll([Object.values(value)], next);
|
|
16
|
+
case 'array':
|
|
17
|
+
return runtime.muxAll([value], next);
|
|
18
|
+
case 'string':
|
|
19
|
+
return runtime.muxAll([[...value]], next);
|
|
20
|
+
default:
|
|
21
|
+
}
|
|
22
|
+
if (params.optional) return [];
|
|
23
|
+
throw new _library.JPLTypeError('cannot iterate over %s (%*<100v)', t, value);
|
|
24
|
+
},
|
|
25
|
+
/** { optional: boolean } */
|
|
26
|
+
map(runtime, params) {
|
|
27
|
+
return {
|
|
28
|
+
optional: runtime.assertType(params.optional, 'boolean')
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _library = require("../../../library");
|
|
8
|
+
var _utils = require("../utils");
|
|
9
|
+
var _default = exports.default = {
|
|
10
|
+
/** { from: [op], to: [op], optional: boolean } */
|
|
11
|
+
async op(runtime, input, target, params, scope, next) {
|
|
12
|
+
const value = runtime.unwrapValue(target);
|
|
13
|
+
const tv = runtime.type(value);
|
|
14
|
+
const [froms, tos] = await Promise.all([runtime.executeInstructions(params.from ?? [], [input], scope), runtime.executeInstructions(params.to ?? [], [input], scope)]);
|
|
15
|
+
return runtime.muxAll([runtime.unwrapValues(froms), runtime.unwrapValues(tos)], (from, to) => {
|
|
16
|
+
const tf = runtime.type(from);
|
|
17
|
+
const tt = runtime.type(to);
|
|
18
|
+
switch (tv) {
|
|
19
|
+
case 'array':
|
|
20
|
+
if (['number', 'null'].includes(tf) && ['number', 'null'].includes(tt)) {
|
|
21
|
+
return next(value.slice(Math.trunc(from ?? 0), Math.trunc(to ?? value.length)));
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
case 'string':
|
|
25
|
+
if (['number', 'null'].includes(tf) && ['number', 'null'].includes(tt)) {
|
|
26
|
+
const chars = [...value];
|
|
27
|
+
return next(chars.slice(Math.trunc(from ?? 0), Math.trunc(to ?? chars.length)).join(''));
|
|
28
|
+
}
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
}
|
|
32
|
+
if (params.optional) return [];
|
|
33
|
+
throw new _library.JPLTypeError('cannot slice %s (%*<100v) with %s (%*<100v) and %s (%*<100v)', tv, value, tf, from, tt, to);
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
/** { from: function, to: function, optional: boolean } */
|
|
37
|
+
map(runtime, params) {
|
|
38
|
+
return {
|
|
39
|
+
from: (0, _utils.call)(params.from),
|
|
40
|
+
to: (0, _utils.call)(params.to),
|
|
41
|
+
optional: runtime.assertType(params.optional, 'boolean')
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|