@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,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("./utils");
8
+ var _default = exports.default = {
9
+ /** { pipes: [[op]] } */
10
+ op(runtime, input, params, scope, next) {
11
+ const iter = async from => {
12
+ // 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.
13
+ await undefined;
14
+ scope.signal.checkHealth();
15
+ if (from >= (params.pipes?.length ?? 0)) return next(null, scope);
16
+ const pipe = params.pipes[from];
17
+ return runtime.executeInstructions(pipe ?? [], [input], scope, output => {
18
+ switch (runtime.type(output)) {
19
+ case 'null':
20
+ return iter(from + 1);
21
+ default:
22
+ return next(output, scope);
23
+ }
24
+ });
25
+ };
26
+ return iter(0);
27
+ },
28
+ /** { pipes: [function] } */
29
+ map(runtime, params) {
30
+ return {
31
+ pipes: runtime.muxOne([params.pipes], pipe => (0, _utils.call)(pipe))
32
+ };
33
+ }
34
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _default = exports.default = {
8
+ /** { number: number } */
9
+ op(runtime, input, params, scope, next) {
10
+ return next(params.number ?? 0, scope);
11
+ },
12
+ /** { number: number } */
13
+ map(runtime, params) {
14
+ return {
15
+ number: runtime.assertType(params.number, 'number')
16
+ };
17
+ }
18
+ };
@@ -0,0 +1,37 @@
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
+ /** { fields: [{ key: [op], value: [op], optional: boolean }] } */
11
+ async op(runtime, input, params, scope, next) {
12
+ const fields = await runtime.muxAsync([params.fields ?? []], async field => {
13
+ const [keys, values] = await Promise.all([runtime.executeInstructions(field.key ?? [], [input], scope), runtime.executeInstructions(field.value ?? [], [input], scope)]);
14
+ return runtime.muxAll([runtime.unwrapValues(keys), values], (key, value) => {
15
+ const t = runtime.type(key);
16
+ switch (t) {
17
+ case 'string':
18
+ return [[key, value]];
19
+ default:
20
+ }
21
+ if (field.optional) return [];
22
+ throw new _library.JPLTypeError('cannot use %s (%*<100v) as object key', t, key);
23
+ });
24
+ });
25
+ return runtime.muxAll(fields, (...entries) => next(Object.fromEntries(entries), scope));
26
+ },
27
+ /** { fields: [{ key: function, value: function, optional: boolean }] } */
28
+ map(runtime, params) {
29
+ return {
30
+ fields: runtime.muxOne([params.fields], entry => ({
31
+ key: (0, _utils.call)(entry.key),
32
+ value: (0, _utils.call)(entry.value),
33
+ optional: runtime.assertType(entry.optional, 'boolean')
34
+ }))
35
+ };
36
+ }
37
+ };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("./utils");
8
+ var _default = exports.default = {
9
+ /** { pipes: [[op]] } */
10
+ op(runtime, input, params, scope, next) {
11
+ const iter = async from => {
12
+ // 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.
13
+ await undefined;
14
+ scope.signal.checkHealth();
15
+ if (from >= (params.pipes?.length ?? 0)) return next(false, scope);
16
+ const pipe = params.pipes[from];
17
+ return runtime.executeInstructions(pipe ?? [], [input], scope, output => {
18
+ if (runtime.truthy(output)) return next(true, scope);
19
+ return iter(from + 1);
20
+ });
21
+ };
22
+ return iter(0);
23
+ },
24
+ /** { pipes: [function] } */
25
+ map(runtime, params) {
26
+ return {
27
+ pipes: runtime.muxOne([params.pipes], pipe => (0, _utils.call)(pipe))
28
+ };
29
+ }
30
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("./utils");
8
+ var _default = exports.default = {
9
+ /** { pipes: [[op]] } */
10
+ op(runtime, input, params, scope, next) {
11
+ return runtime.muxAll([params.pipes ?? []], pipe => runtime.executeInstructions(pipe ?? [], [input], scope, output => next(output, scope)));
12
+ },
13
+ /** { pipes: [function] } */
14
+ map(runtime, params) {
15
+ return {
16
+ pipes: runtime.muxOne([params.pipes], value => (0, _utils.call)(value))
17
+ };
18
+ }
19
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _default = exports.default = {
8
+ /** { string: string } */
9
+ op(runtime, input, params, scope, next) {
10
+ return next(params.string ?? '', scope);
11
+ },
12
+ /** { string: string } */
13
+ map(runtime, params) {
14
+ return {
15
+ string: runtime.assertType(params.string, 'string')
16
+ };
17
+ }
18
+ };
@@ -0,0 +1,37 @@
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
+ /** { try: [op], catch: [op] } */
11
+ async op(runtime, input, params, scope, next) {
12
+ const nextScope = scope.next({
13
+ signal: scope.signal.next()
14
+ });
15
+ try {
16
+ return await runtime.executeInstructions(params.try ?? [], [input], nextScope, async output => {
17
+ try {
18
+ return await next(output, scope);
19
+ } catch (err) {
20
+ throw new _library.JPLErrorEnclosure(err);
21
+ }
22
+ });
23
+ } catch (err) {
24
+ if (_library.JPLErrorEnclosure.is(err)) throw err.inner;
25
+ if (!_library.JPLExecutionError.is(err)) throw err;
26
+ nextScope.signal.exit();
27
+ return runtime.executeInstructions(params.catch ?? [], [err.value], scope, output => next(output, scope));
28
+ }
29
+ },
30
+ /** { try: function, catch: function } */
31
+ map(runtime, params) {
32
+ return {
33
+ try: (0, _utils.call)(params.try),
34
+ catch: (0, _utils.call)(params.catch)
35
+ };
36
+ }
37
+ };
@@ -0,0 +1,20 @@
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
+ /** { name: string } */
10
+ op(runtime, input, params, scope, next) {
11
+ if (!Object.hasOwn(scope.vars, params.name ?? '')) throw new _library.JPLReferenceError('%s is not defined', params.name ?? '');
12
+ return next(scope.vars[params.name ?? ''], scope);
13
+ },
14
+ /** { name: string } */
15
+ map(runtime, params) {
16
+ return {
17
+ name: runtime.assertType(params.name, 'string')
18
+ };
19
+ }
20
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _utils = require("./utils");
8
+ var _default = exports.default = {
9
+ /** { name: string, pipe: [op] } */
10
+ op(runtime, input, params, scope, next) {
11
+ return runtime.executeInstructions(params.pipe ?? [], [input], scope, output => next(input, scope.next({
12
+ vars: {
13
+ [params.name ?? '']: output
14
+ }
15
+ })));
16
+ },
17
+ /** { name: string, pipe: function } */
18
+ map(runtime, params) {
19
+ return {
20
+ name: runtime.assertType(params.name, 'string'),
21
+ pipe: (0, _utils.call)(params.pipe)
22
+ };
23
+ }
24
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _default = exports.default = {
8
+ /** {} */
9
+ op() {
10
+ return [];
11
+ },
12
+ /** {} */
13
+ map() {
14
+ return {};
15
+ }
16
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.call = call;
7
+ exports.constant = constant;
8
+ var _library = require("../../library");
9
+ /** Create OP_CONSTANT operation for the specified normalized value */
10
+ function constant(value) {
11
+ return [{
12
+ op: _library.OP_CONSTANT,
13
+ params: {
14
+ value: value ?? null
15
+ }
16
+ }];
17
+ }
18
+
19
+ /** Create OPA_FUNCTION operation for the specified function */
20
+ function call(value) {
21
+ return [{
22
+ op: _library.OP_ACCESS,
23
+ params: {
24
+ pipe: constant((0, _library.assertType)(value, 'function')),
25
+ selectors: [{
26
+ op: _library.OPA_FUNCTION,
27
+ params: {
28
+ args: [],
29
+ bound: false,
30
+ optional: false
31
+ }
32
+ }]
33
+ }
34
+ }];
35
+ }
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.applyRuntimeDefaults = applyRuntimeDefaults;
7
+ exports.default = void 0;
8
+ var _applyDefaults = _interopRequireDefault(require("../applyDefaults"));
9
+ var _library = require("../library");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ const defaultOptions = {
12
+ vars: {}
13
+ };
14
+ function applyRuntimeDefaults(options = {}, defaults = {}) {
15
+ return (0, _applyDefaults.default)(options, defaults, 'vars');
16
+ }
17
+
18
+ /** JPL runtime */
19
+ class JPLRuntime {
20
+ constructor(program, options) {
21
+ this._options = applyRuntimeDefaults(options?.runtime, defaultOptions);
22
+ this._program = program;
23
+ }
24
+
25
+ /** Return the runtime's options */
26
+ get options() {
27
+ return this._options;
28
+ }
29
+
30
+ /** Return the runtime's program */
31
+ get program() {
32
+ return this._program;
33
+ }
34
+
35
+ /** Create a new orphan scope */
36
+ createScope = presets => new _library.JPLRuntimeScope(presets);
37
+
38
+ /** Execute a new dedicated program */
39
+ execute = async inputs => {
40
+ const scope = this.createScope({
41
+ vars: Object.fromEntries(this.muxOne([Object.entries(this.options.vars)], ([name, value]) => [name, this.normalizeValue(value)]))
42
+ });
43
+ try {
44
+ return await this.executeInstructions(this.program.definition.instructions ?? [], inputs, scope, this.options.adjustResult);
45
+ } finally {
46
+ scope.signal.exit();
47
+ }
48
+ };
49
+
50
+ /** Execute the specified instructions */
51
+ executeInstructions = (instructions, inputs, scope, next = output => [output]) => {
52
+ const iter = async (from, input, currentScope) => {
53
+ // 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.
54
+ await undefined;
55
+ currentScope.signal.checkHealth();
56
+ if (from >= instructions.length) return next(input, currentScope);
57
+ const {
58
+ op,
59
+ params
60
+ } = instructions[from];
61
+ const operator = this.program.ops[op];
62
+ if (!operator) throw new _library.JPLFatalError(`invalid OP '${op}'`);
63
+ return operator.op(this, input, params ?? {}, currentScope, (output, nextScope) => iter(from + 1, output, nextScope));
64
+ };
65
+ return this.muxAll([inputs], input => iter(0, input, scope));
66
+ };
67
+
68
+ /** Execute the specified OP */
69
+ op(op, params, inputs, scope, next = output => [output]) {
70
+ const operator = this.program.ops[op];
71
+ if (!operator) throw new _library.JPLFatalError(`invalid OP '${op}'`);
72
+ const opParams = operator.map(this, params);
73
+ return this.muxAll([inputs], input => operator.op(this, input, opParams ?? {}, scope, next));
74
+ }
75
+
76
+ /** Normalize the specified external value */
77
+ normalizeValue = _library.normalize;
78
+
79
+ /** Normalize the specified array of external values */
80
+ normalizeValues = (values = [], name = 'values') => {
81
+ if (!Array.isArray(values)) throw new _library.JPLFatalError(`expected ${name} to be an array`);
82
+ return this.normalizeValue(values);
83
+ };
84
+
85
+ /** Unwrap the specified normalized value for usage in JPL operations */
86
+ unwrapValue = _library.unwrap;
87
+
88
+ /** Unwrap the specified array of normalized values for usage in JPL operations */
89
+ unwrapValues = (values = [], name = 'values') => {
90
+ if (!Array.isArray(values)) throw new _library.JPLFatalError(`expected ${name} to be an array`);
91
+ return this.muxOne([values], value => this.unwrapValue(value));
92
+ };
93
+
94
+ /** Strip the specified normalized value for usage in JPL operations */
95
+ stripValue = value => (0, _library.strip)(value, (k, v) => this.unwrapValue(v));
96
+
97
+ /** Strip the specified array of normalized values for usage in JPL operations */
98
+ stripValues = (values = [], name = 'values') => {
99
+ if (!Array.isArray(values)) throw new _library.JPLFatalError(`expected ${name} to be an array`);
100
+ return this.muxOne([values], value => this.stripValue(value));
101
+ };
102
+
103
+ /** Alter the specified normalized value using the specified updater */
104
+ alterValue = async (value, updater) => _library.JPLType.is(value) ? (0, _library.adaptErrorsAsync)(() => value.alter(updater)) : this.normalizeValue(await updater(value));
105
+
106
+ /** Resolve the type of the specified normalized value for JPL operations */
107
+ type = _library.typeOf;
108
+
109
+ /** Assert the type for the specified unwrapped value for JPL operations */
110
+ assertType = _library.assertType;
111
+
112
+ /** Determine whether the specified normalized value should be considered as truthy in JPL operations */
113
+ truthy = value => {
114
+ const raw = this.unwrapValue(value);
115
+ return raw !== null && raw !== false;
116
+ };
117
+
118
+ /** Compare the specified normalized values */
119
+ compare = compare.bind(this);
120
+
121
+ /** Compare the specified normalized strings based on their unicode code points */
122
+ compareStrings = (a, b) => {
123
+ const ta = this.type(a);
124
+ if (ta !== 'string') throw new _library.JPLFatalError(`unexpected type ${ta}`);
125
+ const tb = this.type(b);
126
+ if (tb !== 'string') throw new _library.JPLFatalError(`unexpected type ${tb}`);
127
+ return compareStrings(this.unwrapValue(a), this.unwrapValue(b));
128
+ };
129
+
130
+ /** Compare the specified normalized arrays based on their lexical order */
131
+ compareArrays = (a, b) => {
132
+ const ta = this.type(a);
133
+ if (ta !== 'array') throw new _library.JPLFatalError(`unexpected type ${ta}`);
134
+ const tb = this.type(b);
135
+ if (tb !== 'array') throw new _library.JPLFatalError(`unexpected type ${tb}`);
136
+ return compareArrays.call(this, this.unwrapValue(a), this.unwrapValue(b));
137
+ };
138
+
139
+ /** Compare the specified normalized objects */
140
+ compareObjects = (a, b) => {
141
+ const ta = this.type(a);
142
+ if (ta !== 'object') throw new _library.JPLFatalError(`unexpected type ${ta}`);
143
+ const tb = this.type(b);
144
+ if (tb !== 'object') throw new _library.JPLFatalError(`unexpected type ${tb}`);
145
+ return compareObjects.call(this, this.unwrapValue(a), this.unwrapValue(b));
146
+ };
147
+
148
+ /** Determine if the specified normalized values can be considered to be equal */
149
+ equals = (a, b) => this.compare(a, b) === 0;
150
+
151
+ /** Deep merge the specified normalized values */
152
+ merge = async (a, b) => {
153
+ // 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.
154
+ await undefined;
155
+ if (this.type(a) !== 'object' || this.type(b) !== 'object') return b;
156
+ return this.alterValue(a, async value => {
157
+ const changes = await Promise.all(Object.entries(this.unwrapValue(b)).map(async ([k, v]) => [k, await this.merge(value[k] ?? null, v)]));
158
+ return (0, _library.applyObject)(value, changes);
159
+ });
160
+ };
161
+
162
+ /** Stringify the specified normalized value for usage in program outputs */
163
+ stringifyJSON = (value, unescapeString) => (0, _library.stringify)(value, unescapeString);
164
+
165
+ /** Strip the specified normalized value for usage in program outputs */
166
+ stripJSON = value => (0, _library.strip)(value);
167
+
168
+ /**
169
+ * Multiplex the specified array of arguments by calling cb for all possible combinations of arguments.
170
+ *
171
+ * `mux([[1,2], [3,4]], cb)` for example yields:
172
+ * - `cb(1, 3)`
173
+ * - `cb(1, 4)`
174
+ * - `cb(2, 3)`
175
+ * - `cb(2, 4)`
176
+ */
177
+ mux = _library.mux;
178
+
179
+ /** Multiplex the specified array of arguments and return the results produced by the callbacks */
180
+ muxOne = _library.muxOne;
181
+
182
+ /** Multiplex the specified array of arguments asynchronously and return the results produced by the callbacks */
183
+ muxAsync = _library.muxAsync;
184
+
185
+ /** Multiplex the specified array of arguments asynchronously and return a single array of all merged result arrays produced by the callbacks */
186
+ muxAll = _library.muxAll;
187
+ }
188
+ var _default = exports.default = JPLRuntime;
189
+ /** Compare the specified normalized values */
190
+ function compare(a, b) {
191
+ const ta = this.type(a);
192
+ const tb = this.type(b);
193
+ if (ta !== tb) return _library.typeOrder.indexOf(ta) - _library.typeOrder.indexOf(tb);
194
+ const ua = this.unwrapValue(a);
195
+ const ub = this.unwrapValue(b);
196
+ switch (ta) {
197
+ case 'null':
198
+ case 'function':
199
+ return 0;
200
+ case 'boolean':
201
+ case 'number':
202
+ return +ua - +ub;
203
+ case 'string':
204
+ return compareStrings(ua, ub);
205
+ case 'array':
206
+ return compareArrays.call(this, ua, ub);
207
+ case 'object':
208
+ return compareObjects.call(this, ua, ub);
209
+ default:
210
+ throw new _library.JPLFatalError(`unexpected type ${ta}`);
211
+ }
212
+ }
213
+
214
+ /** Compare the specified normalized strings based on their unicode code points */
215
+ function compareStrings(a, b) {
216
+ const min = Math.min(a.length, b.length);
217
+ let i = 0;
218
+ // eslint-disable-next-line no-unused-vars
219
+ for (const _ of a) {
220
+ if (i >= min) {
221
+ break;
222
+ }
223
+ const cp1 = a.codePointAt(i);
224
+ const cp2 = b.codePointAt(i);
225
+ const order = cp1 - cp2;
226
+ if (order !== 0) {
227
+ return order;
228
+ }
229
+ i += 1;
230
+ if (cp1 > 0xffff) {
231
+ i += 1;
232
+ }
233
+ }
234
+ return a.length - b.length;
235
+ }
236
+
237
+ /** Compare the specified normalized arrays based on their lexical order */
238
+ function compareArrays(a, b) {
239
+ const min = Math.min(a.length, b.length);
240
+ for (let i = 0; i < min; i += 1) {
241
+ const c = compare.call(this, a[i], b[i]);
242
+ if (c !== 0) return c;
243
+ }
244
+ return a.length - b.length;
245
+ }
246
+
247
+ /** Compare the specified normalized objects */
248
+ function compareObjects(a, b) {
249
+ const aKeys = Object.keys(a).sort(compareStrings);
250
+ const bKeys = Object.keys(b).sort(compareStrings);
251
+ let order = compareArrays.call(this, aKeys, bKeys);
252
+ if (order !== 0) return order;
253
+ for (const key of aKeys) {
254
+ order = compare.call(this, a[key], b[key]);
255
+ if (order !== 0) return order;
256
+ }
257
+ return 0;
258
+ }
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@jplorg/jpl",
3
+ "version": "1.0.0",
4
+ "description": "JPL interpreter",
5
+ "main": "./lib/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./src/index.js",
9
+ "require": "./lib/index.js",
10
+ "default": "./lib/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "bin": {
15
+ "jpl-repl": "repl.js"
16
+ },
17
+ "files": [
18
+ "lib",
19
+ "src",
20
+ "repl.js"
21
+ ],
22
+ "scripts": {
23
+ "build:clean": "rimraf ./lib",
24
+ "prebuild": "npm run build:clean",
25
+ "build": "babel src -d lib",
26
+ "lint": "eslint src --report-unused-disable-directives",
27
+ "prepare": "npm run lint && npm run build",
28
+ "preversion": "git diff HEAD --name-only --exit-code || (echo -\\> unclean working directory && exit 1)",
29
+ "postversion": "cross-env git commit -am$npm_package_version",
30
+ "prettify": "prettier --write src/**",
31
+ "start": "npm run build >/dev/null && ./repl.js",
32
+ "repl": "./repl.js"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/jplorg/jpl"
37
+ },
38
+ "keywords": [
39
+ "jpl",
40
+ "json",
41
+ "cli"
42
+ ],
43
+ "author": "Aaron Burmeister",
44
+ "license": "MIT",
45
+ "bugs": {
46
+ "url": "https://github.com/jplorg/jpl/issues"
47
+ },
48
+ "homepage": "https://github.com/jplorg/jpl",
49
+ "devDependencies": {
50
+ "@babel/cli": "^7.22.15",
51
+ "@babel/core": "^7.24.4",
52
+ "@babel/eslint-parser": "^7.22.15",
53
+ "@babel/preset-env": "^7.24.4",
54
+ "@babel/preset-react": "^7.18.6",
55
+ "cross-env": "^7.0.3",
56
+ "eslint": "^8.53.0",
57
+ "eslint-config-prettier": "^9.0.0",
58
+ "eslint-plugin-import": "^2.29.0",
59
+ "eslint-plugin-prettier": "^5.0.1",
60
+ "eslint-plugin-react": "^7.33.2",
61
+ "eslint-plugin-react-hooks": "^4.6.0",
62
+ "eslint-plugin-unused-imports": "^3.0.0",
63
+ "prettier": "^3.2.5",
64
+ "rimraf": "^5.0.5"
65
+ },
66
+ "dependencies": {}
67
+ }