@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.
Files changed (215) hide show
  1. package/README.md +34 -0
  2. package/lib/applyDefaults.js +34 -0
  3. package/lib/builtins/funcContains.js +25 -0
  4. package/lib/builtins/funcEndsWith.js +21 -0
  5. package/lib/builtins/funcError.js +11 -0
  6. package/lib/builtins/funcFromJSON.js +26 -0
  7. package/lib/builtins/funcHas.js +24 -0
  8. package/lib/builtins/funcIn.js +24 -0
  9. package/lib/builtins/funcKeys.js +20 -0
  10. package/lib/builtins/funcLength.js +24 -0
  11. package/lib/builtins/funcNow.js +10 -0
  12. package/lib/builtins/funcStartsWith.js +21 -0
  13. package/lib/builtins/funcToJSON.js +10 -0
  14. package/lib/builtins/funcToNumber.js +26 -0
  15. package/lib/builtins/funcToString.js +10 -0
  16. package/lib/builtins/funcTrim.js +17 -0
  17. package/lib/builtins/funcTrimEnd.js +17 -0
  18. package/lib/builtins/funcTrimStart.js +17 -0
  19. package/lib/builtins/funcType.js +10 -0
  20. package/lib/builtins/funcVoid.js +10 -0
  21. package/lib/builtins/index.js +158 -0
  22. package/lib/builtins/internals.js +8 -0
  23. package/lib/builtins/math.js +40 -0
  24. package/lib/builtins/native.js +164 -0
  25. package/lib/createSingleton.js +14 -0
  26. package/lib/index.js +80 -0
  27. package/lib/interpreter/index.js +57 -0
  28. package/lib/interpreter/parse.js +2377 -0
  29. package/lib/interpreter/parseFunction.js +24 -0
  30. package/lib/interpreter/system.js +10 -0
  31. package/lib/interpreter/util.js +349 -0
  32. package/lib/library/apply.js +142 -0
  33. package/lib/library/definition.js +9 -0
  34. package/lib/library/errors/adaptErrors.js +34 -0
  35. package/lib/library/errors/enclosure.js +16 -0
  36. package/lib/library/errors/error.js +18 -0
  37. package/lib/library/errors/execution.js +20 -0
  38. package/lib/library/errors/fatal.js +15 -0
  39. package/lib/library/errors/runtime.js +78 -0
  40. package/lib/library/errors/syntax.js +15 -0
  41. package/lib/library/functions.js +82 -0
  42. package/lib/library/index.js +336 -0
  43. package/lib/library/mux.js +106 -0
  44. package/lib/library/ops/index.js +60 -0
  45. package/lib/library/ops/op.js +180 -0
  46. package/lib/library/ops/opa.js +41 -0
  47. package/lib/library/ops/opc.js +59 -0
  48. package/lib/library/ops/opm.js +50 -0
  49. package/lib/library/ops/opu.js +77 -0
  50. package/lib/library/runtimeScope.js +38 -0
  51. package/lib/library/runtimeSignal.js +72 -0
  52. package/lib/library/types.js +305 -0
  53. package/lib/program/index.js +72 -0
  54. package/lib/program/ops/index.js +61 -0
  55. package/lib/program/ops/opAccess/index.js +55 -0
  56. package/lib/program/ops/opAccess/opaField.js +50 -0
  57. package/lib/program/ops/opAccess/opaFunction.js +50 -0
  58. package/lib/program/ops/opAccess/opaIter.js +31 -0
  59. package/lib/program/ops/opAccess/opaSlice.js +44 -0
  60. package/lib/program/ops/opAnd.js +30 -0
  61. package/lib/program/ops/opArrayConstructor.js +20 -0
  62. package/lib/program/ops/opAssignment/index.js +90 -0
  63. package/lib/program/ops/opAssignment/opaAssignField.js +125 -0
  64. package/lib/program/ops/opAssignment/opaAssignIter.js +69 -0
  65. package/lib/program/ops/opAssignment/opaAssignSlice.js +99 -0
  66. package/lib/program/ops/opAssignment/opuAddition.js +31 -0
  67. package/lib/program/ops/opAssignment/opuDivision.js +31 -0
  68. package/lib/program/ops/opAssignment/opuMultiplication.js +31 -0
  69. package/lib/program/ops/opAssignment/opuNullCoalescence.js +25 -0
  70. package/lib/program/ops/opAssignment/opuRemainder.js +31 -0
  71. package/lib/program/ops/opAssignment/opuSet.js +19 -0
  72. package/lib/program/ops/opAssignment/opuSubtraction.js +31 -0
  73. package/lib/program/ops/opAssignment/opuUpdate.js +19 -0
  74. package/lib/program/ops/opCalculation/index.js +57 -0
  75. package/lib/program/ops/opCalculation/opmAddition.js +49 -0
  76. package/lib/program/ops/opCalculation/opmDivision.js +42 -0
  77. package/lib/program/ops/opCalculation/opmMultiplication.js +40 -0
  78. package/lib/program/ops/opCalculation/opmRemainder.js +34 -0
  79. package/lib/program/ops/opCalculation/opmSubtraction.js +53 -0
  80. package/lib/program/ops/opComparison/index.js +59 -0
  81. package/lib/program/ops/opComparison/opcEqual.js +19 -0
  82. package/lib/program/ops/opComparison/opcGreater.js +19 -0
  83. package/lib/program/ops/opComparison/opcGreaterEqual.js +19 -0
  84. package/lib/program/ops/opComparison/opcLess.js +19 -0
  85. package/lib/program/ops/opComparison/opcLessEqual.js +19 -0
  86. package/lib/program/ops/opComparison/opcUnequal.js +19 -0
  87. package/lib/program/ops/opConstant.js +18 -0
  88. package/lib/program/ops/opConstantFalse.js +16 -0
  89. package/lib/program/ops/opConstantNull.js +16 -0
  90. package/lib/program/ops/opConstantTrue.js +16 -0
  91. package/lib/program/ops/opFunctionDefinition.js +21 -0
  92. package/lib/program/ops/opIf.js +38 -0
  93. package/lib/program/ops/opInterpolatedString.js +24 -0
  94. package/lib/program/ops/opNegation.js +25 -0
  95. package/lib/program/ops/opNot.js +16 -0
  96. package/lib/program/ops/opNullCoalescence.js +34 -0
  97. package/lib/program/ops/opNumber.js +18 -0
  98. package/lib/program/ops/opObjectConstructor.js +37 -0
  99. package/lib/program/ops/opOr.js +30 -0
  100. package/lib/program/ops/opOutputConcat.js +19 -0
  101. package/lib/program/ops/opString.js +18 -0
  102. package/lib/program/ops/opTry.js +37 -0
  103. package/lib/program/ops/opVariable.js +20 -0
  104. package/lib/program/ops/opVariableDefinition.js +24 -0
  105. package/lib/program/ops/opVoid.js +16 -0
  106. package/lib/program/ops/utils.js +35 -0
  107. package/lib/runtime/index.js +258 -0
  108. package/package.json +67 -0
  109. package/repl.js +212 -0
  110. package/src/applyDefaults.js +35 -0
  111. package/src/builtins/funcContains.js +27 -0
  112. package/src/builtins/funcEndsWith.js +21 -0
  113. package/src/builtins/funcError.js +7 -0
  114. package/src/builtins/funcFromJSON.js +24 -0
  115. package/src/builtins/funcHas.js +25 -0
  116. package/src/builtins/funcIn.js +25 -0
  117. package/src/builtins/funcKeys.js +20 -0
  118. package/src/builtins/funcLength.js +26 -0
  119. package/src/builtins/funcNow.js +5 -0
  120. package/src/builtins/funcStartsWith.js +21 -0
  121. package/src/builtins/funcToJSON.js +5 -0
  122. package/src/builtins/funcToNumber.js +25 -0
  123. package/src/builtins/funcToString.js +5 -0
  124. package/src/builtins/funcTrim.js +16 -0
  125. package/src/builtins/funcTrimEnd.js +16 -0
  126. package/src/builtins/funcTrimStart.js +16 -0
  127. package/src/builtins/funcType.js +5 -0
  128. package/src/builtins/funcVoid.js +5 -0
  129. package/src/builtins/index.js +145 -0
  130. package/src/builtins/internals.js +9 -0
  131. package/src/builtins/math.js +44 -0
  132. package/src/builtins/native.js +19 -0
  133. package/src/createSingleton.js +9 -0
  134. package/src/index.js +44 -0
  135. package/src/interpreter/index.js +48 -0
  136. package/src/interpreter/parse.js +1473 -0
  137. package/src/interpreter/parseFunction.js +20 -0
  138. package/src/interpreter/system.js +5 -0
  139. package/src/interpreter/util.js +237 -0
  140. package/src/library/apply.js +135 -0
  141. package/src/library/definition.js +3 -0
  142. package/src/library/errors/adaptErrors.js +28 -0
  143. package/src/library/errors/enclosure.js +11 -0
  144. package/src/library/errors/error.js +14 -0
  145. package/src/library/errors/execution.js +15 -0
  146. package/src/library/errors/fatal.js +10 -0
  147. package/src/library/errors/runtime.js +71 -0
  148. package/src/library/errors/syntax.js +10 -0
  149. package/src/library/functions.js +73 -0
  150. package/src/library/index.js +41 -0
  151. package/src/library/mux.js +98 -0
  152. package/src/library/ops/index.js +5 -0
  153. package/src/library/ops/op.js +174 -0
  154. package/src/library/ops/opa.js +35 -0
  155. package/src/library/ops/opc.js +53 -0
  156. package/src/library/ops/opm.js +44 -0
  157. package/src/library/ops/opu.js +71 -0
  158. package/src/library/runtimeScope.js +35 -0
  159. package/src/library/runtimeSignal.js +67 -0
  160. package/src/library/types.js +299 -0
  161. package/src/program/index.js +80 -0
  162. package/src/program/ops/index.js +82 -0
  163. package/src/program/ops/opAccess/index.js +55 -0
  164. package/src/program/ops/opAccess/opaField.js +58 -0
  165. package/src/program/ops/opAccess/opaFunction.js +52 -0
  166. package/src/program/ops/opAccess/opaIter.js +31 -0
  167. package/src/program/ops/opAccess/opaSlice.js +58 -0
  168. package/src/program/ops/opAnd.js +32 -0
  169. package/src/program/ops/opArrayConstructor.js +16 -0
  170. package/src/program/ops/opAssignment/index.js +102 -0
  171. package/src/program/ops/opAssignment/opaAssignField.js +156 -0
  172. package/src/program/ops/opAssignment/opaAssignIter.js +74 -0
  173. package/src/program/ops/opAssignment/opaAssignSlice.js +157 -0
  174. package/src/program/ops/opAssignment/opuAddition.js +29 -0
  175. package/src/program/ops/opAssignment/opuDivision.js +29 -0
  176. package/src/program/ops/opAssignment/opuMultiplication.js +29 -0
  177. package/src/program/ops/opAssignment/opuNullCoalescence.js +26 -0
  178. package/src/program/ops/opAssignment/opuRemainder.js +29 -0
  179. package/src/program/ops/opAssignment/opuSet.js +15 -0
  180. package/src/program/ops/opAssignment/opuSubtraction.js +29 -0
  181. package/src/program/ops/opAssignment/opuUpdate.js +15 -0
  182. package/src/program/ops/opCalculation/index.js +64 -0
  183. package/src/program/ops/opCalculation/opmAddition.js +63 -0
  184. package/src/program/ops/opCalculation/opmDivision.js +47 -0
  185. package/src/program/ops/opCalculation/opmMultiplication.js +52 -0
  186. package/src/program/ops/opCalculation/opmRemainder.js +49 -0
  187. package/src/program/ops/opCalculation/opmSubtraction.js +72 -0
  188. package/src/program/ops/opComparison/index.js +67 -0
  189. package/src/program/ops/opComparison/opcEqual.js +17 -0
  190. package/src/program/ops/opComparison/opcGreater.js +17 -0
  191. package/src/program/ops/opComparison/opcGreaterEqual.js +17 -0
  192. package/src/program/ops/opComparison/opcLess.js +17 -0
  193. package/src/program/ops/opComparison/opcLessEqual.js +17 -0
  194. package/src/program/ops/opComparison/opcUnequal.js +17 -0
  195. package/src/program/ops/opConstant.js +13 -0
  196. package/src/program/ops/opConstantFalse.js +11 -0
  197. package/src/program/ops/opConstantNull.js +11 -0
  198. package/src/program/ops/opConstantTrue.js +11 -0
  199. package/src/program/ops/opFunctionDefinition.js +17 -0
  200. package/src/program/ops/opIf.js +44 -0
  201. package/src/program/ops/opInterpolatedString.js +27 -0
  202. package/src/program/ops/opNegation.js +26 -0
  203. package/src/program/ops/opNot.js +11 -0
  204. package/src/program/ops/opNullCoalescence.js +36 -0
  205. package/src/program/ops/opNumber.js +13 -0
  206. package/src/program/ops/opObjectConstructor.js +40 -0
  207. package/src/program/ops/opOr.js +32 -0
  208. package/src/program/ops/opOutputConcat.js +17 -0
  209. package/src/program/ops/opString.js +13 -0
  210. package/src/program/ops/opTry.js +38 -0
  211. package/src/program/ops/opVariable.js +17 -0
  212. package/src/program/ops/opVariableDefinition.js +18 -0
  213. package/src/program/ops/opVoid.js +11 -0
  214. package/src/program/ops/utils.js +19 -0
  215. package/src/runtime/index.js +309 -0
@@ -0,0 +1,58 @@
1
+ import { JPLTypeError } from '../../../library';
2
+ import { call } from '../utils';
3
+
4
+ export default {
5
+ /** { from: [op], to: [op], optional: boolean } */
6
+ async op(runtime, input, target, params, scope, next) {
7
+ const value = runtime.unwrapValue(target);
8
+ const tv = runtime.type(value);
9
+
10
+ const [froms, tos] = await Promise.all([
11
+ runtime.executeInstructions(params.from ?? [], [input], scope),
12
+ runtime.executeInstructions(params.to ?? [], [input], scope),
13
+ ]);
14
+
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
+
25
+ case 'string':
26
+ if (['number', 'null'].includes(tf) && ['number', 'null'].includes(tt)) {
27
+ const chars = [...value];
28
+ return next(
29
+ chars.slice(Math.trunc(from ?? 0), Math.trunc(to ?? chars.length)).join(''),
30
+ );
31
+ }
32
+ break;
33
+
34
+ default:
35
+ }
36
+
37
+ if (params.optional) return [];
38
+ throw new JPLTypeError(
39
+ 'cannot slice %s (%*<100v) with %s (%*<100v) and %s (%*<100v)',
40
+ tv,
41
+ value,
42
+ tf,
43
+ from,
44
+ tt,
45
+ to,
46
+ );
47
+ });
48
+ },
49
+
50
+ /** { from: function, to: function, optional: boolean } */
51
+ map(runtime, params) {
52
+ return {
53
+ from: call(params.from),
54
+ to: call(params.to),
55
+ optional: runtime.assertType(params.optional, 'boolean'),
56
+ };
57
+ },
58
+ };
@@ -0,0 +1,32 @@
1
+ import { call } from './utils';
2
+
3
+ export default {
4
+ /** { pipes: [[op]] } */
5
+ op(runtime, input, params, scope, next) {
6
+ const iter = async (from) => {
7
+ // 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.
8
+ await undefined;
9
+
10
+ scope.signal.checkHealth();
11
+
12
+ if (from >= (params.pipes?.length ?? 0)) return next(true, scope);
13
+
14
+ const pipe = params.pipes[from];
15
+
16
+ return runtime.executeInstructions(pipe ?? [], [input], scope, (output) => {
17
+ if (!runtime.truthy(output)) return next(false, scope);
18
+
19
+ return iter(from + 1);
20
+ });
21
+ };
22
+
23
+ return iter(0);
24
+ },
25
+
26
+ /** { pipes: [function] } */
27
+ map(runtime, params) {
28
+ return {
29
+ pipes: runtime.muxOne([params.pipes], (pipe) => call(pipe)),
30
+ };
31
+ },
32
+ };
@@ -0,0 +1,16 @@
1
+ import { call } from './utils';
2
+
3
+ export default {
4
+ /** { pipe: [op] } */
5
+ async op(runtime, input, params, scope, next) {
6
+ const outputs = await runtime.executeInstructions(params.pipe ?? [], [input], scope);
7
+ return next(outputs, scope);
8
+ },
9
+
10
+ /** { pipe: function } */
11
+ map(runtime, params) {
12
+ return {
13
+ pipe: call(params.pipe),
14
+ };
15
+ },
16
+ };
@@ -0,0 +1,102 @@
1
+ import {
2
+ JPLFatalError,
3
+ OPA_FIELD,
4
+ OPA_ITER,
5
+ OPA_SLICE,
6
+ OPU_ADDITION,
7
+ OPU_DIVISION,
8
+ OPU_MULTIPLICATION,
9
+ OPU_NULL_COALESCENCE,
10
+ OPU_REMAINDER,
11
+ OPU_SET,
12
+ OPU_SUBTRACTION,
13
+ OPU_UPDATE,
14
+ } from '../../../library';
15
+ import { call } from '../utils';
16
+ import opaAssignField from './opaAssignField';
17
+ import opaAssignIter from './opaAssignIter';
18
+ import opaAssignSlice from './opaAssignSlice';
19
+ import opuAddition from './opuAddition';
20
+ import opuDivision from './opuDivision';
21
+ import opuMultiplication from './opuMultiplication';
22
+ import opuNullCoalescence from './opuNullCoalescence';
23
+ import opuRemainder from './opuRemainder';
24
+ import opuSet from './opuSet';
25
+ import opuSubtraction from './opuSubtraction';
26
+ import opuUpdate from './opuUpdate';
27
+
28
+ export default {
29
+ /** { pipe: [op], selectors: [opa], assignment: [opu] } */
30
+ op(runtime, input, params, scope, next) {
31
+ const iter = async (from, value) => {
32
+ // 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.
33
+ await undefined;
34
+
35
+ scope.signal.checkHealth();
36
+
37
+ if (from >= (params.selectors?.length ?? 0)) {
38
+ const { op, params: opParams } = params.assignment ?? {};
39
+ const operator = opus[op];
40
+ if (!operator) throw new JPLFatalError(`invalid OPU '${op}'`);
41
+
42
+ return operator.op(runtime, input, value, opParams ?? {}, scope, (output) => [output]);
43
+ }
44
+
45
+ const { op, params: opParams } = params.selectors[from];
46
+ const operator = opasAssign[op];
47
+ if (!operator) throw new JPLFatalError(`invalid OPA '${op}' (assignment)`);
48
+
49
+ return operator.op(runtime, input, value, opParams ?? {}, scope, (output) =>
50
+ iter(from + 1, output),
51
+ );
52
+ };
53
+
54
+ return runtime.executeInstructions(params.pipe ?? [], [input], scope, async (output) =>
55
+ runtime.muxAll([await iter(0, output)], (result) =>
56
+ next(result === undefined ? output : result, scope),
57
+ ),
58
+ );
59
+ },
60
+
61
+ /** { pipe: function, selectors: [opa], assignment: opu } */
62
+ map(runtime, params) {
63
+ return {
64
+ pipe: call(params.pipe),
65
+ selectors: runtime.muxOne([params.selectors], ({ op, params: opParams }) => {
66
+ const operator = opasAssign[op];
67
+ if (!operator) throw new JPLFatalError(`invalid OPA '${op}' (assignment)`);
68
+
69
+ return {
70
+ op: runtime.assertType(op, 'string'),
71
+ params: operator.map(runtime, opParams),
72
+ };
73
+ }),
74
+ assignment: (({ op, params: opParams }) => {
75
+ const operator = opus[op];
76
+ if (!operator) throw new JPLFatalError(`invalid OPU '${op}'`);
77
+
78
+ return {
79
+ op: runtime.assertType(op, 'string'),
80
+ params: operator.map(runtime, opParams),
81
+ };
82
+ })(params.assignment),
83
+ };
84
+ },
85
+ };
86
+
87
+ const opasAssign = {
88
+ [OPA_FIELD]: opaAssignField,
89
+ [OPA_ITER]: opaAssignIter,
90
+ [OPA_SLICE]: opaAssignSlice,
91
+ };
92
+
93
+ const opus = {
94
+ [OPU_ADDITION]: opuAddition,
95
+ [OPU_DIVISION]: opuDivision,
96
+ [OPU_MULTIPLICATION]: opuMultiplication,
97
+ [OPU_NULL_COALESCENCE]: opuNullCoalescence,
98
+ [OPU_REMAINDER]: opuRemainder,
99
+ [OPU_SET]: opuSet,
100
+ [OPU_SUBTRACTION]: opuSubtraction,
101
+ [OPU_UPDATE]: opuUpdate,
102
+ };
@@ -0,0 +1,156 @@
1
+ import { JPLTypeError, applyArray, applyObject } from '../../../library';
2
+ import { call } from '../utils';
3
+
4
+ export default {
5
+ /** { pipe: [op], optional: boolean } */
6
+ async op(runtime, input, target, params, scope, next) {
7
+ let items;
8
+ const vt = runtime.unwrapValue(target);
9
+ const tt = runtime.type(target);
10
+ switch (tt) {
11
+ case 'null':
12
+ case 'object':
13
+ case 'array':
14
+ items = vt;
15
+ break;
16
+
17
+ case 'string':
18
+ items = [...vt];
19
+ break;
20
+
21
+ default:
22
+ if (params.optional) return [undefined];
23
+ throw new JPLTypeError('cannot access fields of %s (%*<100v) (assignment)', tt, vt);
24
+ }
25
+
26
+ const fields = await runtime.executeInstructions(
27
+ params.pipe ?? [],
28
+ [input],
29
+ scope,
30
+ (output) => [output],
31
+ );
32
+
33
+ const iter = async (from, source) => {
34
+ // 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.
35
+ await undefined;
36
+
37
+ scope.signal.checkHealth();
38
+
39
+ if (from >= fields.length) return [source];
40
+
41
+ const t = tt === 'string' ? tt : runtime.type(source);
42
+ const field = runtime.unwrapValue(fields[from]);
43
+ const tf = runtime.type(field);
44
+ switch (t) {
45
+ case 'null': {
46
+ switch (tf) {
47
+ case 'string':
48
+ return runtime.muxAll([await next(null)], (output) => {
49
+ if (output === undefined) return iter(from + 1, source);
50
+ return iter(from + 1, { [field]: output });
51
+ });
52
+
53
+ case 'number': {
54
+ const i = Math.trunc(field);
55
+ return runtime.muxAll([await next(null)], (output) => {
56
+ if (output === undefined) return iter(from + 1, source);
57
+ return iter(from + 1, applyArray([], [[i, output]]));
58
+ });
59
+ }
60
+
61
+ default:
62
+ }
63
+
64
+ break;
65
+ }
66
+
67
+ case 'object':
68
+ if (tf === 'string') {
69
+ const item = source[field];
70
+ return runtime.muxAll([await next(item ?? null)], (output) => {
71
+ if (output === undefined) return iter(from + 1, source);
72
+ return iter(from + 1, applyObject(source, [[field, output]]));
73
+ });
74
+ }
75
+ break;
76
+
77
+ case 'array':
78
+ if (tf === 'number') {
79
+ const i = Math.trunc(field);
80
+ const item = source[i >= 0 ? i : source.length + i];
81
+ return runtime.muxAll([await next(item ?? null)], (output) => {
82
+ if (output === undefined) return iter(from + 1, source);
83
+ return iter(from + 1, applyArray(source, [[i, output]]));
84
+ });
85
+ }
86
+ break;
87
+
88
+ case 'string':
89
+ if (tf === 'number') {
90
+ const i = Math.trunc(field);
91
+ const item = source[i >= 0 ? i : source.length + i];
92
+ return runtime.muxAll([await next(item ?? null)], (output) => {
93
+ if (output === undefined) return iter(from + 1, source);
94
+ const r = runtime.unwrapValue(output);
95
+ const tr = runtime.type(r);
96
+ switch (tr) {
97
+ case 'null':
98
+ case 'string':
99
+ return iter(from + 1, applyArray(source, [[i, output ?? ' ']], ' '));
100
+
101
+ default:
102
+ }
103
+
104
+ throw new JPLTypeError(
105
+ 'cannot assign %s (%*<100v) to string (%*<100v)',
106
+ tr,
107
+ r,
108
+ source.join(''),
109
+ );
110
+ });
111
+ }
112
+ break;
113
+
114
+ default:
115
+ }
116
+
117
+ if (params.optional) return iter(from + 1, source);
118
+ throw new JPLTypeError(
119
+ 'cannot access field of %s (%*<100v) with %s (%*<100v) (assignment)',
120
+ t,
121
+ t === 'string' ? source.join('') : source,
122
+ tf,
123
+ field,
124
+ );
125
+ };
126
+
127
+ return runtime.muxAsync([await iter(0, items)], (results) => {
128
+ if (items === results) return target;
129
+
130
+ const t = tt === 'string' ? tt : runtime.type(results);
131
+ switch (t) {
132
+ case 'null':
133
+ return undefined;
134
+
135
+ case 'object':
136
+ case 'array':
137
+ return runtime.alterValue(target, () => results);
138
+
139
+ case 'string':
140
+ return runtime.alterValue(target, () => results.join(''));
141
+
142
+ default:
143
+ }
144
+
145
+ return undefined;
146
+ });
147
+ },
148
+
149
+ /** { pipe: function, optional: boolean } */
150
+ map(runtime, params) {
151
+ return {
152
+ pipe: call(params.pipe),
153
+ optional: runtime.assertType(params.optional, 'boolean'),
154
+ };
155
+ },
156
+ };
@@ -0,0 +1,74 @@
1
+ import { JPLTypeError, applyCombinations } from '../../../library';
2
+
3
+ export default {
4
+ /** { optional: boolean } */
5
+ async op(runtime, input, target, params, scope, next) {
6
+ const vt = runtime.unwrapValue(target);
7
+ const tt = runtime.type(vt);
8
+ switch (tt) {
9
+ case 'object': {
10
+ const items = Object.entries(vt);
11
+ const outputs = await runtime.muxAsync([items], async (item) =>
12
+ runtime.muxAsync([await next(item[1] ?? null)], (output) => {
13
+ if (output === undefined) return item;
14
+ return [item[0], output];
15
+ }),
16
+ );
17
+ return runtime.muxAsync([applyCombinations(items, outputs)], (results) => {
18
+ if (items === results) return target;
19
+ return runtime.alterValue(target, () => Object.fromEntries(results));
20
+ });
21
+ }
22
+
23
+ case 'array': {
24
+ const items = vt;
25
+ const outputs = await runtime.muxAsync([items], async (item) =>
26
+ runtime.muxAsync([await next(item ?? null)], (output) => {
27
+ if (output === undefined) return item;
28
+ return output;
29
+ }),
30
+ );
31
+ return runtime.muxAsync([applyCombinations(items, outputs)], (results) => {
32
+ if (items === results) return target;
33
+ return runtime.alterValue(target, () => results);
34
+ });
35
+ }
36
+
37
+ case 'string': {
38
+ const items = [...vt];
39
+ const outputs = await runtime.muxAsync([items], async (item) =>
40
+ runtime.muxAsync([await next(item)], (output) => {
41
+ if (output === undefined) return item;
42
+ const r = runtime.unwrapValue(output);
43
+ const tr = runtime.type(r);
44
+ switch (tr) {
45
+ case 'null':
46
+ case 'string':
47
+ return r ?? ' ';
48
+
49
+ default:
50
+ }
51
+
52
+ throw new JPLTypeError('cannot assign %s (%*<100v) to string (%*<100v)', tr, r, vt);
53
+ }),
54
+ );
55
+ return runtime.muxAsync([applyCombinations(items, outputs)], (results) => {
56
+ if (items === results) return target;
57
+ return runtime.alterValue(target, () => results.join(''));
58
+ });
59
+ }
60
+
61
+ default:
62
+ }
63
+
64
+ if (params.optional) return [undefined];
65
+ throw new JPLTypeError('cannot iterate over %s (%*<100v) (assignment)', tt, vt);
66
+ },
67
+
68
+ /** { optional: boolean } */
69
+ map(runtime, params) {
70
+ return {
71
+ optional: runtime.assertType(params.optional, 'boolean'),
72
+ };
73
+ },
74
+ };
@@ -0,0 +1,157 @@
1
+ import { JPLTypeError } from '../../../library';
2
+ import { call } from '../utils';
3
+
4
+ function shallowCompareArrays(a, b) {
5
+ if (a.length !== b.length) return false;
6
+ return a.every((v, i) => v === b[i]);
7
+ }
8
+
9
+ export default {
10
+ /** { from: [op], to: [op], optional: boolean } */
11
+ async op(runtime, input, target, params, scope, next) {
12
+ const [froms, tos] = await Promise.all([
13
+ runtime.executeInstructions(params.from ?? [], [input], scope),
14
+ runtime.executeInstructions(params.to ?? [], [input], scope),
15
+ ]);
16
+
17
+ const ranges = runtime.muxOne(
18
+ [runtime.unwrapValues(froms), runtime.unwrapValues(tos)],
19
+ (from, to) => [from, to],
20
+ );
21
+
22
+ const iter = async (from, source) => {
23
+ // 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.
24
+ await undefined;
25
+
26
+ scope.signal.checkHealth();
27
+
28
+ if (from >= ranges.length) return [source];
29
+
30
+ const v = runtime.unwrapValue(source);
31
+ const tv = runtime.type(source);
32
+ const [start, end] = ranges[from];
33
+ const ts = runtime.type(start);
34
+ const te = runtime.type(end);
35
+ switch (tv) {
36
+ case 'array':
37
+ if (['number', 'null'].includes(ts) && ['number', 'null'].includes(te)) {
38
+ return runtime.muxAll(
39
+ [await next(v.slice(Math.trunc(start ?? 0), Math.trunc(end ?? v.length)))],
40
+ async (output) => {
41
+ if (output === undefined) return iter(from + 1, source);
42
+ return iter(
43
+ from + 1,
44
+ await runtime.alterValue(source, (value) => {
45
+ const result = runtime.unwrapValue(output);
46
+ const tr = runtime.type(result);
47
+ switch (tr) {
48
+ case 'null':
49
+ case 'array': {
50
+ let s = Math.trunc(start ?? 0);
51
+ let e = Math.trunc(end ?? value.length);
52
+ s = s >= 0 ? Math.min(value.length, s) : Math.max(0, value.length + s);
53
+ e = Math.max(
54
+ s,
55
+ e >= 0 ? Math.min(value.length, e) : Math.max(0, value.length + e),
56
+ );
57
+ if (shallowCompareArrays(value.slice(s, e), result ?? [])) return value;
58
+ const c = [...value];
59
+ c.splice(s, e - s, ...(result ?? []));
60
+ return c;
61
+ }
62
+
63
+ default:
64
+ }
65
+
66
+ throw new JPLTypeError(
67
+ 'cannot assign %s (%*<100v) to slice of %s (%*<100v)',
68
+ tr,
69
+ result,
70
+ tv,
71
+ value,
72
+ );
73
+ }),
74
+ );
75
+ },
76
+ );
77
+ }
78
+ break;
79
+
80
+ case 'string':
81
+ if (['number', 'null'].includes(ts) && ['number', 'null'].includes(te)) {
82
+ const chars = [...v];
83
+ return runtime.muxAll(
84
+ [
85
+ await next(
86
+ chars.slice(Math.trunc(start ?? 0), Math.trunc(end ?? chars.length)).join(''),
87
+ ),
88
+ ],
89
+ async (output) => {
90
+ if (output === undefined) return iter(from + 1, source);
91
+ return iter(
92
+ from + 1,
93
+ await runtime.alterValue(source, (value) => {
94
+ const result = runtime.unwrapValue(output);
95
+ const tr = runtime.type(result);
96
+ switch (tr) {
97
+ case 'null':
98
+ case 'string': {
99
+ const chars = [...value];
100
+ let s = Math.trunc(start ?? 0);
101
+ let e = Math.trunc(end ?? chars.length);
102
+ s = s >= 0 ? Math.min(chars.length, s) : Math.max(0, chars.length + s);
103
+ e = Math.max(
104
+ s,
105
+ e >= 0 ? Math.min(chars.length, e) : Math.max(0, chars.length + e),
106
+ );
107
+ if (shallowCompareArrays(chars.slice(s, e), result ?? [])) return value;
108
+ chars.splice(s, e - s, ...(result ?? []));
109
+ return chars.join('');
110
+ }
111
+
112
+ default:
113
+ }
114
+
115
+ throw new JPLTypeError(
116
+ 'cannot assign %s (%*<100v) to slice of %s (%*<100v)',
117
+ tr,
118
+ result,
119
+ tv,
120
+ value,
121
+ );
122
+ }),
123
+ );
124
+ },
125
+ );
126
+ }
127
+ break;
128
+
129
+ default:
130
+ }
131
+
132
+ if (params.optional) return iter(from + 1, source);
133
+ throw new JPLTypeError(
134
+ 'cannot slice %s (%*<100v) with %s (%*<100v) and %s (%*<100v) (assignment)',
135
+ tv,
136
+ v,
137
+ ts,
138
+ start,
139
+ te,
140
+ end,
141
+ );
142
+ };
143
+
144
+ return runtime.muxOne([await iter(0, target)], (output) =>
145
+ runtime.type(output) === 'null' ? undefined : output,
146
+ );
147
+ },
148
+
149
+ /** { from: function, to: function, optional: boolean } */
150
+ map(runtime, params) {
151
+ return {
152
+ from: call(params.from),
153
+ to: call(params.to),
154
+ optional: runtime.assertType(params.optional, 'boolean'),
155
+ };
156
+ },
157
+ };
@@ -0,0 +1,29 @@
1
+ import { OPM_ADDITION, OP_CALCULATION } from '../../../library';
2
+ import { call, constant } from '../utils';
3
+
4
+ export default {
5
+ /** { pipe: [op] } */
6
+ op(runtime, input, target, params, scope, next) {
7
+ return runtime.executeInstructions(
8
+ [
9
+ {
10
+ op: OP_CALCULATION,
11
+ params: {
12
+ pipe: constant(target),
13
+ operations: [{ op: OPM_ADDITION, params: { by: params.pipe ?? [] } }],
14
+ },
15
+ },
16
+ ],
17
+ [input],
18
+ scope,
19
+ next,
20
+ );
21
+ },
22
+
23
+ /** { pipe: function } */
24
+ map(runtime, params) {
25
+ return {
26
+ pipe: call(params.pipe),
27
+ };
28
+ },
29
+ };
@@ -0,0 +1,29 @@
1
+ import { OPM_DIVISION, OP_CALCULATION } from '../../../library';
2
+ import { call, constant } from '../utils';
3
+
4
+ export default {
5
+ /** { pipe: [op] } */
6
+ op(runtime, input, target, params, scope, next) {
7
+ return runtime.executeInstructions(
8
+ [
9
+ {
10
+ op: OP_CALCULATION,
11
+ params: {
12
+ pipe: constant(target),
13
+ operations: [{ op: OPM_DIVISION, params: { by: params.pipe ?? [] } }],
14
+ },
15
+ },
16
+ ],
17
+ [input],
18
+ scope,
19
+ next,
20
+ );
21
+ },
22
+
23
+ /** { pipe: function } */
24
+ map(runtime, params) {
25
+ return {
26
+ pipe: call(params.pipe),
27
+ };
28
+ },
29
+ };
@@ -0,0 +1,29 @@
1
+ import { OPM_MULTIPLICATION, OP_CALCULATION } from '../../../library';
2
+ import { call, constant } from '../utils';
3
+
4
+ export default {
5
+ /** { pipe: [op] } */
6
+ op(runtime, input, target, params, scope, next) {
7
+ return runtime.executeInstructions(
8
+ [
9
+ {
10
+ op: OP_CALCULATION,
11
+ params: {
12
+ pipe: constant(target),
13
+ operations: [{ op: OPM_MULTIPLICATION, params: { by: params.pipe ?? [] } }],
14
+ },
15
+ },
16
+ ],
17
+ [input],
18
+ scope,
19
+ next,
20
+ );
21
+ },
22
+
23
+ /** { pipe: function } */
24
+ map(runtime, params) {
25
+ return {
26
+ pipe: call(params.pipe),
27
+ };
28
+ },
29
+ };