eliscript 0.1.3 → 0.1.4
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/COPYING.RUNTIME +93 -0
- package/README.md +15 -3
- package/bin/eliscript-test +29 -0
- package/bootstrap/backends/README.md +5 -4
- package/bootstrap/backends/binary.eli +89 -22
- package/bootstrap/backends/capabilities.eli +9 -4
- package/bootstrap/backends/managed-layout.eli +93 -81
- package/bootstrap/backends/managed.eli +651 -125
- package/bootstrap/backends/native.eli +1791 -324
- package/bootstrap/backends/numeric.eli +71 -7
- package/bootstrap/backends/sha256.eli +165 -68
- package/bootstrap/backends/wasm.eli +461 -52
- package/bootstrap/compiler/analyzer.eli +692 -2
- package/bootstrap/compiler/compiler.eli +16 -3
- package/bootstrap/compiler/emitter.eli +80 -35
- package/bootstrap/compiler/project.eli +175 -1
- package/bootstrap/compiler/transient-analysis.eli +2 -2
- package/bootstrap/host/build.eli +1 -0
- package/bootstrap/host/build.mjs +1 -1
- package/bootstrap/host/build.mjs.map +1 -1
- package/bootstrap/host/bun.mjs +7 -9
- package/bootstrap/host/bun.mjs.map +1 -1
- package/bootstrap/host/check-cli-ground.eli +47 -5
- package/bootstrap/host/check-cli-ground.mjs +60 -19
- package/bootstrap/host/check-cli-ground.mjs.map +1 -1
- package/bootstrap/host/check-cli.mjs +41 -1
- package/bootstrap/host/check-cli.mjs.map +1 -1
- package/bootstrap/host/compiler-session.eli +136 -22
- package/bootstrap/host/eliscript-cli.mjs +1 -1
- package/bootstrap/host/eliscript-cli.mjs.map +1 -1
- package/bootstrap/host/evaluation-cli.eli +33 -1
- package/bootstrap/host/evaluation-cli.mjs +65 -28
- package/bootstrap/host/evaluation-cli.mjs.map +1 -1
- package/bootstrap/host/evaluation.eli +651 -42
- package/bootstrap/host/evaluation.mjs +884 -72
- package/bootstrap/host/evaluation.mjs.map +1 -1
- package/bootstrap/host/project-cli-ground.eli +6 -1
- package/bootstrap/host/project-cli-ground.mjs +33 -32
- package/bootstrap/host/project-cli-ground.mjs.map +1 -1
- package/bootstrap/host/project-cli.mjs +1 -1
- package/bootstrap/host/project-cli.mjs.map +1 -1
- package/bootstrap/host/project-ground.eli +266 -45
- package/bootstrap/host/project-ground.mjs +429 -159
- package/bootstrap/host/project-ground.mjs.map +1 -1
- package/bootstrap/host/project.mjs +30 -40
- package/bootstrap/host/project.mjs.map +1 -1
- package/bootstrap/host/test-cli.eli +379 -0
- package/bootstrap/host/test-cli.mjs +494 -0
- package/bootstrap/host/test-cli.mjs.map +1 -0
- package/browser/README.md +7 -0
- package/browser/host.mjs +21 -29
- package/browser/host.mjs.map +1 -1
- package/browser/worker.mjs +7 -9
- package/browser/worker.mjs.map +1 -1
- package/compiler/eliscript-analyzer.el +694 -0
- package/compiler/eliscript-cli.el +66 -22
- package/compiler/eliscript-emitter.el +67 -43
- package/compiler/eliscript-ir-emitter.el +71 -36
- package/compiler/eliscript-project.el +360 -80
- package/compiler/eliscript-transient-analysis.el +5 -5
- package/compiler/eliscript.el +6 -0
- package/dist/backends/binary.mjs +148 -26
- package/dist/backends/binary.mjs.map +1 -1
- package/dist/backends/capabilities.mjs +11 -4
- package/dist/backends/capabilities.mjs.map +1 -1
- package/dist/backends/elf.mjs +4 -4
- package/dist/backends/elf.mjs.map +1 -1
- package/dist/backends/managed-layout.mjs +53 -56
- package/dist/backends/managed-layout.mjs.map +1 -1
- package/dist/backends/managed.mjs +1116 -375
- package/dist/backends/managed.mjs.map +1 -1
- package/dist/backends/memory.mjs +24 -26
- package/dist/backends/memory.mjs.map +1 -1
- package/dist/backends/native.mjs +2529 -434
- package/dist/backends/native.mjs.map +1 -1
- package/dist/backends/numeric.mjs +140 -33
- package/dist/backends/numeric.mjs.map +1 -1
- package/dist/backends/optimize.mjs +2 -2
- package/dist/backends/optimize.mjs.map +1 -1
- package/dist/backends/regexp.mjs +33 -35
- package/dist/backends/regexp.mjs.map +1 -1
- package/dist/backends/sha256.mjs +159 -59
- package/dist/backends/sha256.mjs.map +1 -1
- package/dist/backends/wasm.mjs +609 -73
- package/dist/backends/wasm.mjs.map +1 -1
- package/dist/bootstrap/analyzer.mjs +1018 -2
- package/dist/bootstrap/analyzer.mjs.map +1 -1
- package/dist/bootstrap/compiler.mjs +30 -23
- package/dist/bootstrap/compiler.mjs.map +1 -1
- package/dist/bootstrap/emitter.mjs +206 -161
- package/dist/bootstrap/emitter.mjs.macro-origins.json +16 -16
- package/dist/bootstrap/emitter.mjs.map +1 -1
- package/dist/bootstrap/expander.mjs +27 -31
- package/dist/bootstrap/expander.mjs.map +1 -1
- package/dist/bootstrap/ir.mjs +22 -24
- package/dist/bootstrap/ir.mjs.map +1 -1
- package/dist/bootstrap/project.mjs +246 -1
- package/dist/bootstrap/project.mjs.map +1 -1
- package/dist/bootstrap/reader.mjs +2 -2
- package/dist/bootstrap/reader.mjs.map +1 -1
- package/dist/bootstrap/source-map.mjs +34 -48
- package/dist/bootstrap/source-map.mjs.map +1 -1
- package/dist/bootstrap/transient-analysis.mjs +2 -2
- package/dist/bootstrap/transient-analysis.mjs.map +1 -1
- package/dist/browser/compiler.js +106 -106
- package/dist/browser/host.mjs +21 -29
- package/dist/browser/loader.js +1 -1
- package/dist/browser/worker.mjs +95 -95
- package/dist/compiler-artifact.json +1 -1
- package/dist/host/compiler-session.mjs +405 -228
- package/dist/host/compiler-session.mjs.map +1 -1
- package/dist/host/compiler-tools.mjs +44 -46
- package/dist/host/compiler-tools.mjs.map +1 -1
- package/dist/literal-sources/runtime/core/array-map.eli +45 -0
- package/dist/literal-sources/runtime/core/collection-internals.eli +22 -2
- package/dist/literal-sources/runtime/core/collection.eli +282 -23
- package/dist/literal-sources/runtime/core/data-text.eli +196 -13
- package/dist/literal-sources/runtime/core/data.eli +1 -1
- package/dist/literal-sources/runtime/core/identifier.eli +1 -1
- package/dist/literal-sources/runtime/core/lazy-sequence.eli +822 -44
- package/dist/literal-sources/runtime/core/list.eli +1 -1
- package/dist/literal-sources/runtime/core/map.eli +1 -1
- package/dist/literal-sources/runtime/core/metadata-internals.eli +2 -2
- package/dist/literal-sources/runtime/core/metadata.eli +2 -2
- package/dist/literal-sources/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
- package/dist/literal-sources/runtime/core/object.eli +1 -1
- package/dist/literal-sources/runtime/core/order.eli +3 -3
- package/dist/literal-sources/runtime/core/persistent-kind.eli +1 -1
- package/dist/literal-sources/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
- package/dist/literal-sources/runtime/core/protocol.eli +5 -3
- package/dist/literal-sources/runtime/core/queue.eli +1 -1
- package/dist/literal-sources/runtime/core/record.eli +3 -3
- package/dist/literal-sources/runtime/core/regex.eli +2 -2
- package/dist/literal-sources/runtime/core/sequence.eli +1 -1
- package/dist/literal-sources/runtime/core/set-algebra.eli +1 -1
- package/dist/literal-sources/runtime/core/set.eli +1 -1
- package/dist/literal-sources/runtime/core/sorted.eli +4 -4
- package/dist/literal-sources/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
- package/dist/literal-sources/runtime/core/text.eli +1 -1
- package/dist/literal-sources/runtime/core/transducer.eli +24 -2
- package/dist/literal-sources/runtime/core/transient.eli +7 -5
- package/dist/literal-sources/runtime/core/type.eli +1 -1
- package/dist/literal-sources/runtime/core/value-internals.eli +3 -3
- package/dist/literal-sources/runtime/core/value.eli +2 -2
- package/dist/literal-sources/runtime/core/vector.eli +5 -2
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-literals.eli +406 -81
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-queue-state.eli +1 -1
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-sorted-state.eli +1 -1
- package/dist/literal-sources/runtime/managed/managed-transient-state.eli +1206 -0
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-value-state.eli +30 -6
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
- package/dist/literal-sources/runtime/wire/http-rpc.eli +1 -1
- package/dist/literal-sources/runtime/wire/markdown.eli +1 -1
- package/dist/literal-sources/runtime/wire/schema.eli +1 -1
- package/dist/literal-sources/stdlib/bit.eli +89 -1
- package/dist/literal-sources/stdlib/data.eli +522 -0
- package/dist/literal-sources/stdlib/interop/object.eli +39 -0
- package/dist/literal-sources/stdlib/persistent-list.eli +60 -21
- package/dist/literal-sources/stdlib/persistent-map.eli +335 -324
- package/dist/literal-sources/stdlib/persistent-set.eli +8 -6
- package/dist/literal-sources/stdlib/persistent-vector.eli +18 -18
- package/dist/literal-sources/stdlib/protocol.eli +63 -0
- package/{stdlib/core → dist/literal-sources/stdlib}/set.eli +8 -8
- package/dist/literal-sources/stdlib/string.eli +362 -0
- package/dist/runtime/lisp/async-runtime.mjs +55 -59
- package/dist/runtime/lisp/async-runtime.mjs.map +1 -1
- package/dist/runtime/lisp/builtins.mjs +232 -280
- package/dist/runtime/lisp/builtins.mjs.map +1 -1
- package/dist/runtime/lisp/cli.mjs +159 -167
- package/dist/runtime/lisp/cli.mjs.map +1 -1
- package/dist/runtime/lisp/compiler.mjs +359 -419
- package/dist/runtime/lisp/compiler.mjs.map +1 -1
- package/dist/runtime/lisp/core.mjs +24 -24
- package/dist/runtime/lisp/core.mjs.map +1 -1
- package/dist/runtime/lisp/evaluator.mjs +315 -385
- package/dist/runtime/lisp/evaluator.mjs.map +1 -1
- package/dist/runtime/lisp/extensions.mjs +85 -103
- package/dist/runtime/lisp/extensions.mjs.map +1 -1
- package/dist/runtime/lisp/host-io.mjs +106 -122
- package/dist/runtime/lisp/host-io.mjs.map +1 -1
- package/dist/runtime/lisp/io-worker.mjs +117 -135
- package/dist/runtime/lisp/io-worker.mjs.map +1 -1
- package/dist/runtime/lisp/keymap.mjs +5 -5
- package/dist/runtime/lisp/keymap.mjs.map +1 -1
- package/dist/runtime/lisp/macros-forms.mjs +49 -51
- package/dist/runtime/lisp/macros-forms.mjs.map +1 -1
- package/dist/runtime/lisp/reader-format.mjs +31 -31
- package/dist/runtime/lisp/reader-format.mjs.map +1 -1
- package/dist/runtime/lisp/regexp-buffer.mjs +9 -9
- package/dist/runtime/lisp/regexp-buffer.mjs.map +1 -1
- package/dist/runtime/math.mjs +32 -38
- package/dist/runtime/math.mjs.map +1 -1
- package/dist/runtime/number-radix.mjs +5 -7
- package/dist/runtime/number-radix.mjs.map +1 -1
- package/dist/runtime/primitives.mjs +81 -46
- package/dist/runtime/primitives.mjs.macro-origins.json +294 -44
- package/dist/runtime/primitives.mjs.map +1 -1
- package/dist/runtime/unicode-case.mjs +6 -6
- package/dist/runtime/unicode-case.mjs.map +1 -1
- package/dist/runtime.eli +3068 -1882
- package/dist/stage3-release.json +1 -1
- package/dist/stdlib/async.mjs +1838 -0
- package/dist/stdlib/bit.mjs +116 -0
- package/dist/stdlib/collection.mjs +193 -0
- package/dist/stdlib/core.mjs +28 -0
- package/dist/stdlib/data-text.mjs +54 -0
- package/dist/stdlib/data.mjs +601 -0
- package/dist/stdlib/devtools.mjs +565 -0
- package/dist/stdlib/diff.mjs +180 -0
- package/dist/stdlib/fn.mjs +164 -0
- package/dist/stdlib/hierarchy.mjs +189 -0
- package/dist/stdlib/identifier.mjs +33 -0
- package/dist/stdlib/interop/js.mjs +706 -0
- package/dist/stdlib/interop/object.mjs +40 -0
- package/dist/stdlib/json.mjs +843 -0
- package/dist/stdlib/lazy-sequence.mjs +117 -0
- package/dist/stdlib/math.mjs +386 -0
- package/dist/stdlib/meta.mjs +21 -0
- package/dist/stdlib/multimethod.mjs +345 -0
- package/dist/stdlib/numeric.mjs +327 -0
- package/dist/stdlib/order.mjs +194 -0
- package/dist/stdlib/persistent-list.mjs +171 -0
- package/dist/stdlib/persistent-map.mjs +781 -0
- package/dist/stdlib/persistent-queue.mjs +44 -0
- package/dist/stdlib/persistent-set.mjs +225 -0
- package/dist/stdlib/persistent-vector.mjs +316 -0
- package/dist/stdlib/portable/data-text.mjs +1094 -0
- package/dist/stdlib/portable/data.mjs +62 -0
- package/dist/stdlib/portable/identifier.mjs +179 -0
- package/dist/stdlib/portable/metadata.mjs +63 -0
- package/dist/stdlib/portable/object.mjs +140 -0
- package/dist/stdlib/portable/sequence.mjs +142 -0
- package/dist/stdlib/portable/text.mjs +111 -0
- package/dist/stdlib/pprint.mjs +807 -0
- package/dist/stdlib/protocol.mjs +61 -0
- package/dist/stdlib/record.mjs +25 -0
- package/dist/stdlib/regex.mjs +115 -0
- package/dist/stdlib/result.mjs +198 -0
- package/dist/stdlib/schema.mjs +2828 -0
- package/dist/stdlib/seq.mjs +913 -0
- package/dist/stdlib/set.mjs +317 -0
- package/dist/stdlib/sorted.mjs +56 -0
- package/dist/stdlib/state.mjs +451 -0
- package/dist/stdlib/string.mjs +494 -0
- package/dist/stdlib/tap.mjs +187 -0
- package/dist/stdlib/test.mjs +669 -0
- package/dist/stdlib/time.mjs +522 -0
- package/dist/stdlib/transducer.mjs +85 -0
- package/dist/stdlib/transient.mjs +32 -0
- package/dist/stdlib/type.mjs +28 -0
- package/dist/stdlib/uuid.mjs +377 -0
- package/dist/stdlib/value.mjs +334 -0
- package/dist/stdlib/walk.mjs +243 -0
- package/dist/stdlib/wire/gepo.mjs +28 -0
- package/dist/stdlib/wire/mdp.mjs +13 -0
- package/dist/stdlib/wire/mds.mjs +10 -0
- package/dist/stdlib/zipper.mjs +1025 -0
- package/editor/README.md +15 -0
- package/editor/eliscript-data.el +221 -8
- package/editor/eliscript-mode.el +207 -48
- package/editor/eliscript-runtime.el +6 -3
- package/package.json +49 -15
- package/platform/README.md +6 -0
- package/platform/browser.mjs +10 -12
- package/platform/browser.mjs.map +1 -1
- package/runtime/README.md +12 -5
- package/runtime/core/array-map.eli +538 -0
- package/runtime/core/array-map.mjs +651 -0
- package/runtime/core/array-map.mjs.map +1 -0
- package/runtime/core/collection-internals.eli +85 -33
- package/runtime/core/collection-internals.mjs +245 -193
- package/runtime/core/collection-internals.mjs.map +1 -1
- package/runtime/core/collection.eli +105 -3
- package/runtime/core/collection.mjs +190 -53
- package/runtime/core/collection.mjs.map +1 -1
- package/runtime/core/data-text.eli +224 -16
- package/runtime/core/data-text.mjs +345 -83
- package/runtime/core/data-text.mjs.map +1 -1
- package/runtime/core/data.eli +50 -33
- package/runtime/core/data.mjs +72 -36
- package/runtime/core/data.mjs.map +1 -1
- package/runtime/core/identifier.mjs +15 -19
- package/runtime/core/identifier.mjs.map +1 -1
- package/runtime/core/lazy-sequence.eli +885 -15
- package/runtime/core/lazy-sequence.mjs +1265 -38
- package/runtime/core/lazy-sequence.mjs.map +1 -1
- package/runtime/core/map-internals.eli +209 -1
- package/runtime/core/map-internals.mjs +359 -71
- package/runtime/core/map-internals.mjs.map +1 -1
- package/runtime/core/map.eli +75 -7
- package/runtime/core/map.mjs +122 -47
- package/runtime/core/map.mjs.map +1 -1
- package/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
- package/runtime/core/object-impl.mjs +5 -5
- package/runtime/core/object-impl.mjs.map +1 -1
- package/runtime/core/order.eli +15 -3
- package/runtime/core/order.mjs +23 -8
- package/runtime/core/order.mjs.map +1 -1
- package/runtime/core/persistent-kind.mjs +9 -13
- package/runtime/core/persistent-kind.mjs.map +1 -1
- package/runtime/core/protocol-error.mjs +9 -11
- package/runtime/core/protocol-error.mjs.map +1 -1
- package/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
- package/runtime/core/protocol-impl.mjs +10 -10
- package/runtime/core/protocol-impl.mjs.map +1 -1
- package/runtime/core/queue.mjs +14 -16
- package/runtime/core/queue.mjs.map +1 -1
- package/runtime/core/record.eli +2 -2
- package/runtime/core/record.mjs +13 -17
- package/runtime/core/record.mjs.map +1 -1
- package/runtime/core/regex.mjs +18 -22
- package/runtime/core/regex.mjs.map +1 -1
- package/runtime/core/sequence.eli +33 -31
- package/runtime/core/sequence.mjs +60 -93
- package/runtime/core/sequence.mjs.map +1 -1
- package/runtime/core/set.eli +53 -27
- package/runtime/core/set.mjs +113 -82
- package/runtime/core/set.mjs.map +1 -1
- package/runtime/core/sorted.mjs +91 -108
- package/runtime/core/sorted.mjs.map +1 -1
- package/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
- package/runtime/core/text-impl.mjs +1 -1
- package/runtime/core/text-impl.mjs.map +1 -1
- package/runtime/core/transducer.eli +24 -0
- package/runtime/core/transducer.mjs +27 -1
- package/runtime/core/transducer.mjs.map +1 -1
- package/runtime/core/transient-internals.eli +12 -4
- package/runtime/core/transient-internals.mjs +19 -6
- package/runtime/core/transient-internals.mjs.map +1 -1
- package/runtime/core/transient.eli +31 -0
- package/runtime/core/transient.mjs +33 -2
- package/runtime/core/transient.mjs.map +1 -1
- package/runtime/core/value-internals.mjs +29 -35
- package/runtime/core/value-internals.mjs.map +1 -1
- package/runtime/core/vector.eli +73 -20
- package/runtime/core/vector.mjs +159 -156
- package/runtime/core/vector.mjs.map +1 -1
- package/runtime/literals.eli +16 -9
- package/runtime/literals.mjs +25 -9
- package/runtime/literals.mjs.map +1 -1
- package/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
- package/{stdlib → runtime/managed}/managed-literals.eli +406 -81
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-queue-state.eli +1 -1
- package/runtime/managed/managed-runtime-array-map.eli +45 -0
- package/{stdlib → runtime/managed}/managed-runtime-collection-internals.eli +22 -2
- package/{stdlib → runtime/managed}/managed-runtime-collection.eli +282 -23
- package/{stdlib → runtime/managed}/managed-runtime-data-text.eli +196 -13
- package/{stdlib → runtime/managed}/managed-runtime-data.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-identifier.eli +1 -1
- package/runtime/managed/managed-runtime-lazy-sequence.eli +935 -0
- package/{stdlib → runtime/managed}/managed-runtime-list.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-map.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-metadata-internals.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-metadata.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-object.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-order.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-persistent-kind.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-protocol.eli +5 -3
- package/{stdlib → runtime/managed}/managed-runtime-queue.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-record.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-regex.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-sequence.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-set-algebra.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-set.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-sorted.eli +4 -4
- package/{stdlib → runtime/managed}/managed-runtime-text.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-transducer.eli +24 -2
- package/{stdlib → runtime/managed}/managed-runtime-transient.eli +7 -5
- package/{stdlib → runtime/managed}/managed-runtime-type.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-value-internals.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-value.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-vector.eli +5 -2
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-sorted-state.eli +1 -1
- package/runtime/managed/managed-transient-state.eli +1206 -0
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-value-state.eli +30 -6
- package/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-http-rpc.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-markdown.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-schema.eli +1 -1
- package/runtime/math.mjs +32 -38
- package/runtime/math.mjs.map +1 -1
- package/runtime/number-radix.mjs +5 -7
- package/runtime/number-radix.mjs.map +1 -1
- package/runtime/primitives.eli +19 -6
- package/runtime/primitives.mjs +81 -46
- package/runtime/primitives.mjs.map +1 -1
- package/runtime/source-mapping.mjs +17 -23
- package/runtime/source-mapping.mjs.map +1 -1
- package/runtime/testing/map.eli +70 -41
- package/runtime/testing/map.mjs +57 -34
- package/runtime/testing/map.mjs.map +1 -1
- package/runtime/unicode-case.mjs +6 -6
- package/runtime/unicode-case.mjs.map +1 -1
- package/runtime/wire/http-rpc.mjs +12 -18
- package/runtime/wire/http-rpc.mjs.map +1 -1
- package/runtime/wire/markdown.mjs +42 -42
- package/runtime/wire/markdown.mjs.map +1 -1
- package/runtime/wire/schema.mjs +13 -13
- package/runtime/wire/schema.mjs.map +1 -1
- package/runtime/worker-value-codec.mjs +5 -7
- package/runtime/worker-value-codec.mjs.map +1 -1
- package/runtime/worker-value-stream.mjs +128 -135
- package/runtime/worker-value-stream.mjs.map +1 -1
- package/runtime/worker.mjs +75 -89
- package/runtime/worker.mjs.map +1 -1
- package/stdlib/README.md +25 -18
- package/stdlib/async.eli +1245 -0
- package/stdlib/bit.eli +89 -1
- package/stdlib/bit.mjs +89 -2
- package/stdlib/bit.mjs.map +1 -1
- package/stdlib/{core/collection.eli → collection.eli} +39 -4
- package/stdlib/core.eli +956 -0
- package/stdlib/data-text.eli +62 -1091
- package/stdlib/data.eli +517 -37
- package/stdlib/devtools.eli +399 -0
- package/stdlib/diff.eli +134 -0
- package/stdlib/{function.eli → fn.eli} +1 -1
- package/stdlib/identifier.eli +21 -136
- package/stdlib/interop/object.eli +39 -0
- package/stdlib/{core/lazy-sequence.eli → lazy-sequence.eli} +28 -2
- package/stdlib/math.eli +267 -0
- package/stdlib/{core/metadata.eli → meta.eli} +2 -2
- package/stdlib/{core/order.eli → order.eli} +6 -6
- package/stdlib/persistent-list.eli +60 -21
- package/stdlib/persistent-map.eli +335 -324
- package/stdlib/persistent-set.eli +8 -6
- package/stdlib/persistent-vector.eli +18 -18
- package/stdlib/portable/data-text.eli +1092 -0
- package/stdlib/portable/data.eli +42 -0
- package/stdlib/portable/identifier.eli +147 -0
- package/stdlib/{metadata.eli → portable/metadata.eli} +5 -5
- package/stdlib/{object.eli → portable/object.eli} +1 -1
- package/stdlib/{sequence.eli → portable/sequence.eli} +1 -1
- package/stdlib/{text.eli → portable/text.eli} +1 -1
- package/stdlib/pprint.eli +588 -0
- package/stdlib/protocol.eli +63 -0
- package/stdlib/{core/record.eli → record.eli} +2 -2
- package/stdlib/regex.eli +70 -0
- package/stdlib/result.eli +53 -44
- package/stdlib/schema.eli +1886 -0
- package/stdlib/{core/seq.eli → seq.eli} +97 -90
- package/{dist/literal-sources/stdlib/core → stdlib}/set.eli +8 -8
- package/stdlib/{core/sorted.eli → sorted.eli} +2 -2
- package/stdlib/{state/atom.eli → state.eli} +137 -7
- package/stdlib/string.eli +362 -0
- package/stdlib/tap.eli +135 -0
- package/stdlib/test.eli +566 -0
- package/stdlib/time.eli +419 -0
- package/stdlib/{core/transducer.eli → transducer.eli} +4 -2
- package/stdlib/{core/transient.eli → transient.eli} +11 -5
- package/stdlib/{core/type.eli → type.eli} +2 -2
- package/stdlib/uuid.eli +288 -0
- package/stdlib/{core/walk.eli → walk.eli} +6 -6
- package/stdlib/{gepo.eli → wire/gepo.eli} +2 -2
- package/stdlib/{mdp.eli → wire/mdp.eli} +2 -2
- package/stdlib/{mds.eli → wire/mds.eli} +2 -2
- package/stdlib/{core/zipper.eli → zipper.eli} +179 -138
- package/tests/helpers/wasm-managed-math-node.mjs +495 -582
- package/tools/acceptance/check-ground.mjs +106 -132
- package/tools/acceptance/check.mjs +16 -16
- package/tools/acceptance/retained-performance-snapshot.mjs +265 -149
- package/tools/acceptance/retained-soak.mjs +5 -7
- package/tools/acceptance/retained-transient.mjs +5 -7
- package/tools/backends/build.mjs +373 -95
- package/tools/backends/compiler-bootstrap.mjs +271 -317
- package/tools/backends/compiler-browser.mjs +7 -9
- package/tools/backends/compiler-build.mjs +77 -97
- package/tools/backends/compiler-cli-ground.mjs +47 -47
- package/tools/backends/compiler-data-transport.mjs +27 -31
- package/tools/backends/compiler-host-iteration.mjs +13 -21
- package/tools/backends/compiler-host-record.mjs +28 -34
- package/tools/backends/compiler-metadata.mjs +145 -53
- package/tools/backends/compiler-package-ground.mjs +39 -47
- package/tools/backends/compiler-package-loader.mjs +164 -206
- package/tools/backends/compiler-session.mjs +432 -324
- package/tools/backends/compiler-worker.mjs +79 -89
- package/tools/backends/dist/bootstrap-js-stages.mjs +17 -19
- package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
- package/tools/backends/dist/release-cli.mjs +151 -22
- package/tools/backends/dist/release-cli.mjs.map +1 -1
- package/tools/backends/dist/run-artifact.mjs +54 -76
- package/tools/backends/dist/run-artifact.mjs.map +1 -1
- package/tools/backends/managed-foreign-binding.mjs +370 -114
- package/tools/backends/managed-host-ground.mjs +14 -17
- package/tools/backends/managed-host-lifecycle.mjs +42 -53
- package/tools/backends/managed-host-object-realm.mjs +121 -111
- package/tools/backends/managed-host-proxy.mjs +5 -7
- package/tools/backends/managed-host-runtime.mjs +6295 -4350
- package/tools/backends/managed-host.mjs +234 -188
- package/tools/backends/managed-layout.mjs +53 -56
- package/tools/backends/managed-module-graph-ground.mjs +488 -568
- package/tools/backends/managed-module-graph.mjs +62 -76
- package/tools/backends/managed-runtime-host-ground.mjs +352 -400
- package/tools/backends/managed-runtime.mjs +227 -266
- package/tools/backends/managed-source-view-ground.mjs +165 -161
- package/tools/backends/managed-source-view.mjs +415 -154
- package/tools/backends/native-managed-runtime.mjs +96 -138
- package/tools/backends/native-memory.mjs +1122 -742
- package/tools/backends/native-node-ffi.mjs +2439 -0
- package/tools/backends/native-node-memory.mjs +793 -383
- package/tools/backends/native-node-runner.mjs +1458 -579
- package/tools/backends/native-node-watchdog.mjs +12 -14
- package/tools/backends/native-node-worker.mjs +46 -50
- package/tools/backends/stage3-targets.mjs +2 -2
- package/tools/bootstrap-runtime.el +4 -4
- package/tools/collections/transient-builder-benchmark.mjs +17 -17
- package/tools/project/check-cli.eli +27 -3
- package/tools/project/project-cli.eli +1 -1
- package/tools/testing/maintained-suite-source.mjs +5 -7
- package/tools/testing/maintained-suite-source.mjs.map +1 -1
- package/dist/literal-sources/stdlib/core/data.eli +0 -306
- package/dist/literal-sources/stdlib/managed-transient-state.eli +0 -353
- package/stdlib/core/data-text.eli +0 -16
- package/stdlib/core/data.eli +0 -306
- package/stdlib/core/identifier.eli +0 -32
- package/stdlib/core/regex.eli +0 -17
- package/stdlib/deferred.eli +0 -70
- package/stdlib/managed-runtime-lazy-sequence.eli +0 -157
- package/stdlib/managed-transient-state.eli +0 -353
- /package/{stdlib → runtime/managed}/managed-protocol-error.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-runtime-literals.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-runtime-truth.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-testing-value.eli +0 -0
|
@@ -0,0 +1,1886 @@
|
|
|
1
|
+
(module eliscript.schema
|
|
2
|
+
(import "../runtime/core/collection.mjs" :as Collection)
|
|
3
|
+
(import "../runtime/core/data-text.mjs" printValue)
|
|
4
|
+
(import "../runtime/core/identifier.mjs"
|
|
5
|
+
eliscriptSymbol isEliscriptSymbol isIdentifier isKeyword keyword qualifiedIdentifierName)
|
|
6
|
+
(import "../runtime/core/list.mjs" persistentList)
|
|
7
|
+
(import "../runtime/core/map.mjs" EMPTY_MAP)
|
|
8
|
+
(import "../runtime/core/set.mjs" persistentHashSet)
|
|
9
|
+
(import "../runtime/core/value.mjs" equalValues)
|
|
10
|
+
(import "../runtime/core/vector.mjs" EMPTY_VECTOR PersistentVector persistentVector)
|
|
11
|
+
|
|
12
|
+
(defconst INVALID (keyword "eliscript.schema" "invalid"))
|
|
13
|
+
(defconst KW_SCHEMA (keyword "eliscript.schema" "schema"))
|
|
14
|
+
(defconst KW_VALUE (keyword "eliscript.schema" "value"))
|
|
15
|
+
(defconst KW_PROBLEMS (keyword "eliscript.schema" "problems"))
|
|
16
|
+
(defconst KW_PATH (keyword "eliscript.schema" "path"))
|
|
17
|
+
(defconst KW_PRED (keyword "eliscript.schema" "pred"))
|
|
18
|
+
(defconst KW_VAL (keyword "eliscript.schema" "val"))
|
|
19
|
+
(defconst KW_VIA (keyword "eliscript.schema" "via"))
|
|
20
|
+
(defconst KW_IN (keyword "eliscript.schema" "in"))
|
|
21
|
+
(defconst KW_REASON (keyword "eliscript.schema" "reason"))
|
|
22
|
+
(defconst KW_OK (keyword "ok?"))
|
|
23
|
+
(defconst KW_SEED (keyword "seed"))
|
|
24
|
+
(defconst KW_NUM_TESTS (keyword "num-tests"))
|
|
25
|
+
(defconst KW_FAIL (keyword "fail"))
|
|
26
|
+
(defconst KW_SHRUNK (keyword "shrunk"))
|
|
27
|
+
(defconst KW_SMALLEST (keyword "smallest"))
|
|
28
|
+
(defconst KW_STEPS (keyword "steps"))
|
|
29
|
+
(defconst KW_DEPTH (keyword "depth"))
|
|
30
|
+
(defconst KW_ERROR (keyword "error"))
|
|
31
|
+
(defconst MISSING (js-object))
|
|
32
|
+
(defconst REGISTRY (new globalThis.Map))
|
|
33
|
+
|
|
34
|
+
(defconst Schema
|
|
35
|
+
(js* "(class Schema { constructor(kind, name, config, customGen, meta) { this.kind = kind; this.name = name; this.config = config; this.customGen = customGen || null; this.meta = meta || null; Object.freeze(this); } })"))
|
|
36
|
+
|
|
37
|
+
(defconst SchemaGen
|
|
38
|
+
(js* "(class SchemaGen { constructor(name, sampleFn) { this.name = name; this.sampleFn = sampleFn; Object.freeze(this); } })"))
|
|
39
|
+
|
|
40
|
+
(defun schema? (value)
|
|
41
|
+
(js* "value instanceof Schema"))
|
|
42
|
+
|
|
43
|
+
(defun gen? (value)
|
|
44
|
+
(js* "value instanceof SchemaGen"))
|
|
45
|
+
|
|
46
|
+
(defun invalid? (value)
|
|
47
|
+
(equalValues value INVALID))
|
|
48
|
+
|
|
49
|
+
(defun kv-map (&rest kvs)
|
|
50
|
+
(let ((out EMPTY_MAP)
|
|
51
|
+
(i 0)
|
|
52
|
+
(len (js-length kvs)))
|
|
53
|
+
(while (< (1+ i) len)
|
|
54
|
+
(setq out (Collection/assoc out (aref kvs i) (aref kvs (1+ i))))
|
|
55
|
+
(setq i (+ i 2)))
|
|
56
|
+
out))
|
|
57
|
+
|
|
58
|
+
(defun truthy? (value)
|
|
59
|
+
(not (or (nil? value) (undefined? value) (eq value false))))
|
|
60
|
+
|
|
61
|
+
(defun make-schema (kind name config &optional custom-gen meta)
|
|
62
|
+
(new Schema kind name config
|
|
63
|
+
(if (nullish? custom-gen) nil custom-gen)
|
|
64
|
+
(if (nullish? meta) nil meta)))
|
|
65
|
+
|
|
66
|
+
(defun make-gen (name sample-fn)
|
|
67
|
+
(new SchemaGen name sample-fn))
|
|
68
|
+
|
|
69
|
+
(defun read-opt (options k1 &optional k2 k3 fallback)
|
|
70
|
+
(cond
|
|
71
|
+
((nullish? options) fallback)
|
|
72
|
+
((Collection/isMap options)
|
|
73
|
+
(let ((v1 (Collection/get options (keyword k1) MISSING)))
|
|
74
|
+
(if (not (eq v1 MISSING))
|
|
75
|
+
v1
|
|
76
|
+
(let ((s1 (Collection/get options k1 MISSING)))
|
|
77
|
+
(if (not (eq s1 MISSING))
|
|
78
|
+
s1
|
|
79
|
+
(if (not (nullish? k2))
|
|
80
|
+
(let ((v2 (Collection/get options (keyword k2) MISSING)))
|
|
81
|
+
(if (not (eq v2 MISSING))
|
|
82
|
+
v2
|
|
83
|
+
(let ((s2 (Collection/get options k2 MISSING)))
|
|
84
|
+
(if (not (eq s2 MISSING))
|
|
85
|
+
s2
|
|
86
|
+
(if (not (nullish? k3))
|
|
87
|
+
(let ((v3 (Collection/get options (keyword k3) MISSING)))
|
|
88
|
+
(if (not (eq v3 MISSING))
|
|
89
|
+
v3
|
|
90
|
+
(Collection/get options k3 fallback)))
|
|
91
|
+
fallback)))))
|
|
92
|
+
fallback))))))
|
|
93
|
+
((= (value-type options) "object")
|
|
94
|
+
(cond
|
|
95
|
+
((Object/hasOwn options k1) (aref options k1))
|
|
96
|
+
((and (not (nullish? k2)) (Object/hasOwn options k2)) (aref options k2))
|
|
97
|
+
((and (not (nullish? k3)) (Object/hasOwn options k3)) (aref options k3))
|
|
98
|
+
(t fallback)))
|
|
99
|
+
(t fallback)))
|
|
100
|
+
|
|
101
|
+
(defun def-spec (key schema)
|
|
102
|
+
(unless (or (isKeyword key) (isEliscriptSymbol key))
|
|
103
|
+
(throw (new globalThis/TypeError "def-spec expects a keyword or symbol key")))
|
|
104
|
+
(let ((resolved (resolve-spec schema)))
|
|
105
|
+
(js-call REGISTRY :set (hash-key-for-id key) resolved)
|
|
106
|
+
resolved))
|
|
107
|
+
|
|
108
|
+
(defun hash-key-for-id (id)
|
|
109
|
+
(str (if (isKeyword id) "k:" "s:") (qualifiedIdentifierName id)))
|
|
110
|
+
|
|
111
|
+
(defun get-spec (key)
|
|
112
|
+
(if (or (isKeyword key) (isEliscriptSymbol key))
|
|
113
|
+
(let ((found (js-call REGISTRY :get (hash-key-for-id key))))
|
|
114
|
+
(if (undefined? found) nil found))
|
|
115
|
+
nil))
|
|
116
|
+
|
|
117
|
+
(defun resolve-spec (spec)
|
|
118
|
+
(cond
|
|
119
|
+
((schema? spec) spec)
|
|
120
|
+
((= (value-type spec) "function")
|
|
121
|
+
(spec-pred spec (js-object :name (let ((n (get spec :name))) (if (and n (> (js-length n) 0)) n "predicate")))))
|
|
122
|
+
((Collection/isSet spec)
|
|
123
|
+
(spec-enum spec))
|
|
124
|
+
((or (isKeyword spec) (isEliscriptSymbol spec))
|
|
125
|
+
(let ((found (get-spec spec)))
|
|
126
|
+
(when (nil? found)
|
|
127
|
+
(throw (new globalThis/Error (str "unknown registered schema: " (qualifiedIdentifierName spec)))))
|
|
128
|
+
found))
|
|
129
|
+
(t
|
|
130
|
+
(throw (new globalThis/TypeError (str "expected schema or predicate, got " (printValue spec)))))))
|
|
131
|
+
|
|
132
|
+
;; ============================================================================
|
|
133
|
+
;; Deterministic Mulberry32 PRNG
|
|
134
|
+
;; ============================================================================
|
|
135
|
+
|
|
136
|
+
(defun make-rng (seed)
|
|
137
|
+
(let ((normalized (if (nullish? seed) 42 (uint32 seed))))
|
|
138
|
+
(js-object :state normalized)))
|
|
139
|
+
|
|
140
|
+
(defun rng-next-u32 (rng)
|
|
141
|
+
(let* ((s (uint32 (+ (get rng :state) 1831565813)))
|
|
142
|
+
(_w (put rng :state s))
|
|
143
|
+
(t1 (imul32 (bit-xor s (unsigned-bit-shift-right s 15)) (bit-or s 1)))
|
|
144
|
+
(t2 (uint32 (+ t1 (imul32 (bit-xor t1 (unsigned-bit-shift-right t1 7)) (bit-or t1 61))))))
|
|
145
|
+
(uint32 (bit-xor t2 (unsigned-bit-shift-right t2 14)))))
|
|
146
|
+
|
|
147
|
+
(defun rng-next-float (rng)
|
|
148
|
+
(/ (rng-next-u32 rng) 4294967296))
|
|
149
|
+
|
|
150
|
+
(defun rng-next-int (rng min-val max-val)
|
|
151
|
+
(let ((lo (Math/ceil min-val))
|
|
152
|
+
(hi (Math/floor max-val)))
|
|
153
|
+
(when (> lo hi)
|
|
154
|
+
(throw (new globalThis/RangeError (str "invalid integer range [" min-val ", " max-val "]"))))
|
|
155
|
+
(if (= lo hi)
|
|
156
|
+
lo
|
|
157
|
+
(let ((span (+ (- hi lo) 1)))
|
|
158
|
+
(+ lo (Math/floor (* (rng-next-float rng) span)))))))
|
|
159
|
+
|
|
160
|
+
;; ============================================================================
|
|
161
|
+
;; Built-in Generators
|
|
162
|
+
;; ============================================================================
|
|
163
|
+
|
|
164
|
+
(defun gen-return (value)
|
|
165
|
+
(make-gen "return" (lambda (_rng _size) value)))
|
|
166
|
+
|
|
167
|
+
(defun gen-fmap (f gen)
|
|
168
|
+
(unless (= (value-type f) "function")
|
|
169
|
+
(throw (new globalThis/TypeError "gen-fmap expects a function")))
|
|
170
|
+
(let ((g (ensure-gen gen)))
|
|
171
|
+
(make-gen (str "fmap:" (get g :name))
|
|
172
|
+
(lambda (rng size)
|
|
173
|
+
(funcall f (js-call g :sampleFn rng size))))))
|
|
174
|
+
|
|
175
|
+
(defun gen-bind (gen f)
|
|
176
|
+
(unless (= (value-type f) "function")
|
|
177
|
+
(throw (new globalThis/TypeError "gen-bind expects a function")))
|
|
178
|
+
(let ((g (ensure-gen gen)))
|
|
179
|
+
(make-gen (str "bind:" (get g :name))
|
|
180
|
+
(lambda (rng size)
|
|
181
|
+
(let ((inner (ensure-gen (funcall f (js-call g :sampleFn rng size)))))
|
|
182
|
+
(js-call inner :sampleFn rng size))))))
|
|
183
|
+
|
|
184
|
+
(defun gen-one-of (gens)
|
|
185
|
+
(let ((arr (to-js-array gens)))
|
|
186
|
+
(when (= (js-length arr) 0)
|
|
187
|
+
(throw (new globalThis/RangeError "gen-one-of requires at least one generator")))
|
|
188
|
+
(let ((resolved (js-call arr :map ensure-gen)))
|
|
189
|
+
(make-gen "one-of"
|
|
190
|
+
(lambda (rng size)
|
|
191
|
+
(let ((idx (rng-next-int rng 0 (1- (js-length resolved)))))
|
|
192
|
+
(js-call (aref resolved idx) :sampleFn rng size)))))))
|
|
193
|
+
|
|
194
|
+
(defun gen-elements (values)
|
|
195
|
+
(let ((arr (to-js-array values)))
|
|
196
|
+
(when (= (js-length arr) 0)
|
|
197
|
+
(throw (new globalThis/RangeError "gen-elements requires at least one value")))
|
|
198
|
+
(make-gen "elements"
|
|
199
|
+
(lambda (rng _size)
|
|
200
|
+
(aref arr (rng-next-int rng 0 (1- (js-length arr))))))))
|
|
201
|
+
|
|
202
|
+
(defun gen-such-that (pred gen &optional max-tries)
|
|
203
|
+
(let ((g (ensure-gen gen))
|
|
204
|
+
(tries-limit (if (nullish? max-tries) 200 max-tries)))
|
|
205
|
+
(make-gen (str "such-that:" (get g :name))
|
|
206
|
+
(lambda (rng size)
|
|
207
|
+
(let ((attempt 0)
|
|
208
|
+
(found MISSING))
|
|
209
|
+
(while (and (eq found MISSING) (< attempt tries-limit))
|
|
210
|
+
(let ((candidate (js-call g :sampleFn rng (+ size (Math/floor (/ attempt 4))))))
|
|
211
|
+
(when (truthy? (funcall pred candidate))
|
|
212
|
+
(setq found candidate)))
|
|
213
|
+
(setq attempt (1+ attempt)))
|
|
214
|
+
(when (eq found MISSING)
|
|
215
|
+
(throw (new globalThis/Error (str "gen-such-that exhausted after " tries-limit " attempts"))))
|
|
216
|
+
found)))))
|
|
217
|
+
|
|
218
|
+
(defconst gen-boolean
|
|
219
|
+
(make-gen "boolean"
|
|
220
|
+
(lambda (rng _size)
|
|
221
|
+
(= (rng-next-int rng 0 1) 1))))
|
|
222
|
+
|
|
223
|
+
(defun gen-int (&optional min-val max-val)
|
|
224
|
+
(if (and (not (nullish? min-val)) (not (nullish? max-val)))
|
|
225
|
+
(make-gen "int-range"
|
|
226
|
+
(lambda (rng _size) (rng-next-int rng min-val max-val)))
|
|
227
|
+
(make-gen "int"
|
|
228
|
+
(lambda (rng size)
|
|
229
|
+
(let* ((bound (Math/max 1 (Math/floor size)))
|
|
230
|
+
(r (rng-next-int rng (- bound) bound)))
|
|
231
|
+
r)))))
|
|
232
|
+
|
|
233
|
+
(defun gen-double (&optional options)
|
|
234
|
+
(let ((min-v (read-opt options "min" nil nil -1000.0))
|
|
235
|
+
(max-v (read-opt options "max" nil nil 1000.0))
|
|
236
|
+
(allow-nan (truthy? (read-opt options "NaN?" "nan?" nil false)))
|
|
237
|
+
(allow-inf (truthy? (read-opt options "infinite?" "infinite" nil false))))
|
|
238
|
+
(make-gen "double"
|
|
239
|
+
(lambda (rng size)
|
|
240
|
+
(let ((tag (rng-next-int rng 0 19)))
|
|
241
|
+
(cond
|
|
242
|
+
((and allow-nan (= tag 0)) (get globalThis/Number :NaN))
|
|
243
|
+
((and allow-inf (= tag 1)) (get globalThis/Number :POSITIVE_INFINITY))
|
|
244
|
+
((and allow-inf (= tag 2)) (get globalThis/Number :NEGATIVE_INFINITY))
|
|
245
|
+
(t
|
|
246
|
+
(let* ((span (- max-v min-v))
|
|
247
|
+
(scaled (+ min-v (* (rng-next-float rng) (if (Number/isFinite span) span (Math/max 1 size))))))
|
|
248
|
+
(/ (Math/round (* scaled 10000)) 10000)))))))))
|
|
249
|
+
|
|
250
|
+
(defconst PRINTABLE_ASCII "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_- ")
|
|
251
|
+
|
|
252
|
+
(defun gen-string (&optional min-len max-len)
|
|
253
|
+
(let ((lo (if (nullish? min-len) 0 min-len))
|
|
254
|
+
(hi (if (nullish? max-len) nil max-len)))
|
|
255
|
+
(make-gen "string"
|
|
256
|
+
(lambda (rng size)
|
|
257
|
+
(let* ((cap (if (nullish? hi) (Math/max lo (Math/min 32 (Math/floor size))) hi))
|
|
258
|
+
(len (rng-next-int rng lo cap))
|
|
259
|
+
(chars (js-array))
|
|
260
|
+
(i 0))
|
|
261
|
+
(while (< i len)
|
|
262
|
+
(let ((cidx (rng-next-int rng 0 (1- (js-length PRINTABLE_ASCII)))))
|
|
263
|
+
(js-call chars :push (aref PRINTABLE_ASCII cidx)))
|
|
264
|
+
(setq i (1+ i)))
|
|
265
|
+
(js-call chars :join ""))))))
|
|
266
|
+
|
|
267
|
+
(defconst gen-keyword
|
|
268
|
+
(make-gen "keyword"
|
|
269
|
+
(lambda (rng size)
|
|
270
|
+
(let* ((len (rng-next-int rng 1 (Math/max 1 (Math/min 12 (Math/floor (+ size 1))))))
|
|
271
|
+
(chars (js-array))
|
|
272
|
+
(i 0))
|
|
273
|
+
(while (< i len)
|
|
274
|
+
(js-call chars :push (aref "abcdefghijkmnopqrstuvwxyz" (rng-next-int rng 0 24)))
|
|
275
|
+
(setq i (1+ i)))
|
|
276
|
+
(keyword (js-call chars :join ""))))))
|
|
277
|
+
|
|
278
|
+
(defconst gen-symbol
|
|
279
|
+
(make-gen "symbol"
|
|
280
|
+
(lambda (rng size)
|
|
281
|
+
(let ((kw (js-call gen-keyword :sampleFn rng size)))
|
|
282
|
+
(eliscriptSymbol (qualifiedIdentifierName kw))))))
|
|
283
|
+
|
|
284
|
+
(defun gen-tuple (&rest gens)
|
|
285
|
+
(let ((resolved (js-call (Array/from gens) :map ensure-gen)))
|
|
286
|
+
(make-gen "tuple"
|
|
287
|
+
(lambda (rng size)
|
|
288
|
+
(let ((items (js-array))
|
|
289
|
+
(i 0))
|
|
290
|
+
(while (< i (js-length resolved))
|
|
291
|
+
(js-call items :push (js-call (aref resolved i) :sampleFn rng size))
|
|
292
|
+
(setq i (1+ i)))
|
|
293
|
+
(js-call PersistentVector :from items))))))
|
|
294
|
+
|
|
295
|
+
(defun gen-vector (item-gen &optional min-count max-count distinct?)
|
|
296
|
+
(let ((g (ensure-gen item-gen))
|
|
297
|
+
(lo (if (nullish? min-count) 0 min-count))
|
|
298
|
+
(hi (if (nullish? max-count) nil max-count))
|
|
299
|
+
(uniq (truthy? distinct?)))
|
|
300
|
+
(make-gen "vector"
|
|
301
|
+
(lambda (rng size)
|
|
302
|
+
(let* ((cap (if (nullish? hi) (Math/max lo (Math/min 16 (Math/floor size))) hi))
|
|
303
|
+
(target-len (rng-next-int rng lo cap))
|
|
304
|
+
(items (js-array))
|
|
305
|
+
(attempts 0)
|
|
306
|
+
(limit (* (Math/max 1 target-len) 20)))
|
|
307
|
+
(while (and (< (js-length items) target-len) (< attempts limit))
|
|
308
|
+
(let ((candidate (js-call g :sampleFn rng (Math/max 1 (Math/floor (/ size 2))))))
|
|
309
|
+
(if (or (not uniq) (not (array-contains-equal? items candidate)))
|
|
310
|
+
(js-call items :push candidate)))
|
|
311
|
+
(setq attempts (1+ attempts)))
|
|
312
|
+
(when (< (js-length items) lo)
|
|
313
|
+
(throw (new globalThis/Error "gen-vector failed to produce minimum distinct elements")))
|
|
314
|
+
(js-call PersistentVector :from items))))))
|
|
315
|
+
|
|
316
|
+
(defun gen-map (key-gen val-gen &optional min-count max-count)
|
|
317
|
+
(let ((kg (ensure-gen key-gen))
|
|
318
|
+
(vg (ensure-gen val-gen))
|
|
319
|
+
(lo (if (nullish? min-count) 0 min-count))
|
|
320
|
+
(hi (if (nullish? max-count) nil max-count)))
|
|
321
|
+
(make-gen "map"
|
|
322
|
+
(lambda (rng size)
|
|
323
|
+
(let* ((cap (if (nullish? hi) (Math/max lo (Math/min 12 (Math/floor size))) hi))
|
|
324
|
+
(target-len (rng-next-int rng lo cap))
|
|
325
|
+
(pairs (js-array))
|
|
326
|
+
(seen-keys (js-array))
|
|
327
|
+
(attempts 0)
|
|
328
|
+
(limit (* (Math/max 1 target-len) 20)))
|
|
329
|
+
(while (and (< (js-length seen-keys) target-len) (< attempts limit))
|
|
330
|
+
(let ((k (js-call kg :sampleFn rng (Math/max 1 (Math/floor (/ size 2))))))
|
|
331
|
+
(unless (array-contains-equal? seen-keys k)
|
|
332
|
+
(let ((v (js-call vg :sampleFn rng (Math/max 1 (Math/floor (/ size 2))))))
|
|
333
|
+
(js-call seen-keys :push k)
|
|
334
|
+
(js-call pairs :push k)
|
|
335
|
+
(js-call pairs :push v))))
|
|
336
|
+
(setq attempts (1+ attempts)))
|
|
337
|
+
(when (< (js-length seen-keys) lo)
|
|
338
|
+
(throw (new globalThis/Error "gen-map failed to produce minimum distinct keys")))
|
|
339
|
+
(apply kv-map pairs))))))
|
|
340
|
+
|
|
341
|
+
(defun ensure-gen (value)
|
|
342
|
+
(cond
|
|
343
|
+
((gen? value) value)
|
|
344
|
+
((schema? value) (gen-from-spec value))
|
|
345
|
+
(t (throw (new globalThis/TypeError "expected a generator or schema")))))
|
|
346
|
+
|
|
347
|
+
(defun with-gen (schema gen)
|
|
348
|
+
(let ((s (resolve-spec schema))
|
|
349
|
+
(g (if (= (value-type gen) "function")
|
|
350
|
+
(funcall gen)
|
|
351
|
+
(ensure-gen gen))))
|
|
352
|
+
(make-schema (get s :kind) (get s :name) (get s :config) (ensure-gen g) (get s :meta))))
|
|
353
|
+
|
|
354
|
+
;; ============================================================================
|
|
355
|
+
;; Schema Combinators
|
|
356
|
+
;; ============================================================================
|
|
357
|
+
|
|
358
|
+
(defun spec-pred (pred &optional options)
|
|
359
|
+
(unless (= (value-type pred) "function")
|
|
360
|
+
(throw (new globalThis/TypeError "spec-pred expects a predicate function")))
|
|
361
|
+
(let ((name (read-opt options "name" nil nil
|
|
362
|
+
(let ((pn (get pred :name))) (if (and pn (> (js-length pn) 0)) pn "predicate"))))
|
|
363
|
+
(gen (read-opt options "gen" nil nil nil)))
|
|
364
|
+
(make-schema "pred" name (js-object :pred pred) (if (nullish? gen) nil (ensure-gen gen)))))
|
|
365
|
+
|
|
366
|
+
(defconst spec-any
|
|
367
|
+
(make-schema "any" "any?" (js-object)
|
|
368
|
+
(gen-one-of (js-array (gen-int) gen-boolean (gen-string) gen-keyword (gen-return nil)))))
|
|
369
|
+
|
|
370
|
+
(defconst spec-nil
|
|
371
|
+
(make-schema "pred" "nil?" (js-object :pred (lambda (x) (nil? x))) (gen-return nil)))
|
|
372
|
+
|
|
373
|
+
(defconst spec-boolean
|
|
374
|
+
(make-schema "pred" "boolean?" (js-object :pred (lambda (x) (= (value-type x) "boolean"))) gen-boolean))
|
|
375
|
+
|
|
376
|
+
(defconst spec-number
|
|
377
|
+
(make-schema "pred" "number?"
|
|
378
|
+
(js-object :pred (lambda (x) (and (= (value-type x) "number") (not (Number/isNaN x)))))
|
|
379
|
+
(gen-double (js-object :min -100 :max 100))))
|
|
380
|
+
|
|
381
|
+
(defconst spec-int
|
|
382
|
+
(make-schema "pred" "int?"
|
|
383
|
+
(js-object :pred (lambda (x) (Number/isSafeInteger x)))
|
|
384
|
+
(gen-int)))
|
|
385
|
+
|
|
386
|
+
(defconst spec-double
|
|
387
|
+
(make-schema "pred" "double?"
|
|
388
|
+
(js-object :pred (lambda (x) (= (value-type x) "number")))
|
|
389
|
+
(gen-double)))
|
|
390
|
+
|
|
391
|
+
(defconst spec-string
|
|
392
|
+
(make-schema "pred" "string?" (js-object :pred (lambda (x) (= (value-type x) "string"))) (gen-string)))
|
|
393
|
+
|
|
394
|
+
(defconst spec-keyword
|
|
395
|
+
(make-schema "pred" "keyword?" (js-object :pred isKeyword) gen-keyword))
|
|
396
|
+
|
|
397
|
+
(defconst spec-symbol
|
|
398
|
+
(make-schema "pred" "symbol?" (js-object :pred isEliscriptSymbol) gen-symbol))
|
|
399
|
+
|
|
400
|
+
(defconst spec-vector
|
|
401
|
+
(make-schema "pred" "vector?" (js-object :pred Collection/isVector) (gen-vector spec-any 0 8)))
|
|
402
|
+
|
|
403
|
+
(defconst spec-map
|
|
404
|
+
(make-schema "pred" "map?" (js-object :pred Collection/isMap) (gen-map gen-keyword spec-any 0 8)))
|
|
405
|
+
|
|
406
|
+
(defconst spec-set
|
|
407
|
+
(make-schema "pred" "set?" (js-object :pred Collection/isSet)
|
|
408
|
+
(gen-fmap (lambda (v) (apply persistentHashSet (to-js-array v)))
|
|
409
|
+
(gen-vector spec-any 0 6))))
|
|
410
|
+
|
|
411
|
+
(defconst spec-list
|
|
412
|
+
(make-schema "pred" "list?" (js-object :pred Collection/isList)
|
|
413
|
+
(gen-fmap (lambda (v) (apply persistentList (to-js-array v)))
|
|
414
|
+
(gen-vector spec-any 0 8))))
|
|
415
|
+
|
|
416
|
+
(defun spec-int-in (min-val max-val)
|
|
417
|
+
(unless (and (Number/isSafeInteger min-val) (Number/isSafeInteger max-val) (<= min-val max-val))
|
|
418
|
+
(throw (new globalThis/RangeError "spec-int-in requires safe integers min <= max")))
|
|
419
|
+
(make-schema "int-in" (str "(int-in " min-val " " max-val ")")
|
|
420
|
+
(js-object :min min-val :max max-val)
|
|
421
|
+
(gen-int min-val max-val)))
|
|
422
|
+
|
|
423
|
+
(defun spec-double-in (&optional options)
|
|
424
|
+
(let ((min-v (read-opt options "min" nil nil (get globalThis/Number :NEGATIVE_INFINITY)))
|
|
425
|
+
(max-v (read-opt options "max" nil nil (get globalThis/Number :POSITIVE_INFINITY)))
|
|
426
|
+
(allow-inf (truthy? (read-opt options "infinite?" "infinite" nil false)))
|
|
427
|
+
(allow-nan (truthy? (read-opt options "NaN?" "nan?" nil false))))
|
|
428
|
+
(make-schema "double-in" "double-in"
|
|
429
|
+
(js-object :min min-v :max max-v :infinite? allow-inf :NaN? allow-nan)
|
|
430
|
+
(gen-double (js-object
|
|
431
|
+
:min (if (Number/isFinite min-v) min-v -1000)
|
|
432
|
+
:max (if (Number/isFinite max-v) max-v 1000)
|
|
433
|
+
:infinite? allow-inf
|
|
434
|
+
:NaN? allow-nan)))))
|
|
435
|
+
|
|
436
|
+
(defun spec-string-len (min-len &optional max-len)
|
|
437
|
+
(let ((lo (if (nullish? min-len) 0 min-len))
|
|
438
|
+
(hi (if (nullish? max-len) nil max-len)))
|
|
439
|
+
(unless (and (Number/isSafeInteger lo) (>= lo 0)
|
|
440
|
+
(or (nullish? hi) (and (Number/isSafeInteger hi) (>= hi lo))))
|
|
441
|
+
(throw (new globalThis/RangeError "spec-string-len requires non-negative safe integers min <= max")))
|
|
442
|
+
(make-schema "string-len" (str "(string-len " lo (if (nullish? hi) ")" (str " " hi ")")))
|
|
443
|
+
(js-object :min lo :max hi)
|
|
444
|
+
(gen-string lo hi))))
|
|
445
|
+
|
|
446
|
+
(defun spec-nilable (spec)
|
|
447
|
+
(let ((inner (resolve-spec spec)))
|
|
448
|
+
(make-schema "nilable" (str "(nilable " (get inner :name) ")") (js-object :inner inner))))
|
|
449
|
+
|
|
450
|
+
(defun spec-enum (values)
|
|
451
|
+
(let ((arr (to-js-array values)))
|
|
452
|
+
(when (= (js-length arr) 0)
|
|
453
|
+
(throw (new globalThis/RangeError "spec-enum requires a non-empty collection or array")))
|
|
454
|
+
(make-schema "enum" "enum" (js-object :values arr) (gen-elements arr))))
|
|
455
|
+
|
|
456
|
+
(defun spec-and (&rest specs)
|
|
457
|
+
(when (= (js-length specs) 0)
|
|
458
|
+
(throw (new globalThis/RangeError "spec-and requires at least one spec")))
|
|
459
|
+
(let ((resolved (js-call (Array/from specs) :map resolve-spec)))
|
|
460
|
+
(make-schema "and" "and" (js-object :specs resolved))))
|
|
461
|
+
|
|
462
|
+
(defun normalize-tagged-pairs (args label)
|
|
463
|
+
(let ((count (js-length args)))
|
|
464
|
+
(when (or (= count 0) (not (= (% count 2) 0)))
|
|
465
|
+
(throw (new globalThis/Error (str label " expects alternating tag and schema pairs"))))
|
|
466
|
+
(let ((clauses (js-array))
|
|
467
|
+
(i 0))
|
|
468
|
+
(while (< i count)
|
|
469
|
+
(js-call clauses :push
|
|
470
|
+
(js-object :tag (aref args i) :spec (resolve-spec (aref args (+ i 1)))))
|
|
471
|
+
(setq i (+ i 2)))
|
|
472
|
+
clauses)))
|
|
473
|
+
|
|
474
|
+
(defun spec-or (&rest tag-spec-pairs)
|
|
475
|
+
(let ((clauses (normalize-tagged-pairs tag-spec-pairs "spec-or")))
|
|
476
|
+
(make-schema "or" "or" (js-object :clauses clauses))))
|
|
477
|
+
|
|
478
|
+
(defun normalize-key-entries (raw)
|
|
479
|
+
(cond
|
|
480
|
+
((nullish? raw) (js-array))
|
|
481
|
+
((Collection/isMap raw)
|
|
482
|
+
(let ((entries (js-array)))
|
|
483
|
+
(Collection/reduceKV raw
|
|
484
|
+
(lambda (_acc k v)
|
|
485
|
+
(js-call entries :push (js-object :key k :spec (resolve-spec v)))
|
|
486
|
+
nil)
|
|
487
|
+
nil)
|
|
488
|
+
entries))
|
|
489
|
+
((or (Array/isArray raw) (Collection/isSequential raw))
|
|
490
|
+
(let ((arr (to-js-array raw))
|
|
491
|
+
(entries (js-array))
|
|
492
|
+
(i 0))
|
|
493
|
+
(while (< i (js-length arr))
|
|
494
|
+
(let ((item (aref arr i)))
|
|
495
|
+
(cond
|
|
496
|
+
((or (Array/isArray item) (Collection/isVector item))
|
|
497
|
+
(let ((pair (to-js-array item)))
|
|
498
|
+
(unless (= (js-length pair) 2)
|
|
499
|
+
(throw (new globalThis/TypeError "spec-keys entry pair must have 2 elements")))
|
|
500
|
+
(js-call entries :push (js-object :key (aref pair 0) :spec (resolve-spec (aref pair 1))))))
|
|
501
|
+
((isKeyword item)
|
|
502
|
+
(let ((s (get-spec item)))
|
|
503
|
+
(if (nil? s)
|
|
504
|
+
(throw (new globalThis/Error (str "spec-keys unknown key spec: " (qualifiedIdentifierName item))))
|
|
505
|
+
(js-call entries :push (js-object :key item :spec s)))))
|
|
506
|
+
(t
|
|
507
|
+
(throw (new globalThis/TypeError "spec-keys entry must be [key schema] or registered keyword")))))
|
|
508
|
+
(setq i (1+ i)))
|
|
509
|
+
entries))
|
|
510
|
+
((= (value-type raw) "object")
|
|
511
|
+
(let ((keys (Object/keys raw))
|
|
512
|
+
(entries (js-array))
|
|
513
|
+
(i 0))
|
|
514
|
+
(while (< i (js-length keys))
|
|
515
|
+
(let ((k (aref keys i)))
|
|
516
|
+
(js-call entries :push (js-object :key k :spec (resolve-spec (aref raw k)))))
|
|
517
|
+
(setq i (1+ i)))
|
|
518
|
+
entries))
|
|
519
|
+
(t
|
|
520
|
+
(throw (new globalThis/TypeError "spec-keys entries must be a map or list of [key spec] pairs")))))
|
|
521
|
+
|
|
522
|
+
(defun unqualified-key (k)
|
|
523
|
+
(cond
|
|
524
|
+
((isKeyword k) (keyword (identifierName-safe k)))
|
|
525
|
+
((isEliscriptSymbol k) (eliscriptSymbol (identifierName-safe k)))
|
|
526
|
+
(t k)))
|
|
527
|
+
|
|
528
|
+
(defun identifierName-safe (id)
|
|
529
|
+
(if (isIdentifier id)
|
|
530
|
+
(let* ((q (qualifiedIdentifierName id))
|
|
531
|
+
(idx (js-call q :indexOf "/")))
|
|
532
|
+
(if (< idx 0) q (js-call q :slice (1+ idx))))
|
|
533
|
+
id))
|
|
534
|
+
|
|
535
|
+
(defun normalize-unqualified-entries (entries)
|
|
536
|
+
(js-call entries :map
|
|
537
|
+
(lambda (entry)
|
|
538
|
+
(js-object :key (unqualified-key (get entry :key))
|
|
539
|
+
:originalKey (get entry :key)
|
|
540
|
+
:spec (get entry :spec)))))
|
|
541
|
+
|
|
542
|
+
(defun spec-keys (options)
|
|
543
|
+
(let* ((req (normalize-key-entries (read-opt options "req" nil nil nil)))
|
|
544
|
+
(opt (normalize-key-entries (read-opt options "opt" nil nil nil)))
|
|
545
|
+
(req-un (normalize-unqualified-entries (normalize-key-entries (read-opt options "req-un" "reqUn" nil nil))))
|
|
546
|
+
(opt-un (normalize-unqualified-entries (normalize-key-entries (read-opt options "opt-un" "optUn" nil nil))))
|
|
547
|
+
(closed (truthy? (read-opt options "closed?" "closed" nil false)))
|
|
548
|
+
(all-req (js-call req :concat req-un))
|
|
549
|
+
(all-opt (js-call opt :concat opt-un)))
|
|
550
|
+
(make-schema "keys" "keys"
|
|
551
|
+
(js-object :req all-req :opt all-opt :closed closed))))
|
|
552
|
+
|
|
553
|
+
(defun spec-coll-of (item-spec &optional options)
|
|
554
|
+
(let ((inner (resolve-spec item-spec))
|
|
555
|
+
(kind (read-opt options "kind" nil nil nil))
|
|
556
|
+
(min-c (read-opt options "min-count" "minCount" nil 0))
|
|
557
|
+
(max-c (read-opt options "max-count" "maxCount" nil nil))
|
|
558
|
+
(distinct? (truthy? (read-opt options "distinct" "distinct?" nil false))))
|
|
559
|
+
(make-schema "coll-of" "coll-of"
|
|
560
|
+
(js-object :item inner :kind kind :minCount min-c :maxCount max-c :distinct distinct?))))
|
|
561
|
+
|
|
562
|
+
(defun spec-map-of (key-spec val-spec &optional options)
|
|
563
|
+
(let ((ks (resolve-spec key-spec))
|
|
564
|
+
(vs (resolve-spec val-spec))
|
|
565
|
+
(min-c (read-opt options "min-count" "minCount" nil 0))
|
|
566
|
+
(max-c (read-opt options "max-count" "maxCount" nil nil)))
|
|
567
|
+
(make-schema "map-of" "map-of"
|
|
568
|
+
(js-object :key ks :val vs :minCount min-c :maxCount max-c))))
|
|
569
|
+
|
|
570
|
+
(defun spec-tuple (&rest item-specs)
|
|
571
|
+
(let ((items (js-call (Array/from item-specs) :map resolve-spec)))
|
|
572
|
+
(make-schema "tuple" "tuple" (js-object :items items))))
|
|
573
|
+
|
|
574
|
+
(defun spec-cat (&rest tag-spec-pairs)
|
|
575
|
+
(let ((clauses (normalize-tagged-pairs tag-spec-pairs "spec-cat")))
|
|
576
|
+
(make-schema "cat" "cat" (js-object :clauses clauses))))
|
|
577
|
+
|
|
578
|
+
(defun spec-alt (&rest tag-spec-pairs)
|
|
579
|
+
(let ((clauses (normalize-tagged-pairs tag-spec-pairs "spec-alt")))
|
|
580
|
+
(make-schema "alt" "alt" (js-object :clauses clauses))))
|
|
581
|
+
|
|
582
|
+
(defun spec-rep* (spec)
|
|
583
|
+
(make-schema "rep*" "rep*" (js-object :inner (resolve-spec spec))))
|
|
584
|
+
|
|
585
|
+
(defun spec-rep+ (spec)
|
|
586
|
+
(make-schema "rep+" "rep+" (js-object :inner (resolve-spec spec))))
|
|
587
|
+
|
|
588
|
+
(defun spec-opt? (spec)
|
|
589
|
+
(make-schema "opt?" "opt?" (js-object :inner (resolve-spec spec))))
|
|
590
|
+
|
|
591
|
+
(defun spec-fn (options)
|
|
592
|
+
(let ((args-spec (read-opt options "args" nil nil nil))
|
|
593
|
+
(ret-spec (read-opt options "ret" nil nil nil))
|
|
594
|
+
(fn-spec (read-opt options "fn" nil nil nil)))
|
|
595
|
+
(make-schema "fn" "fspec"
|
|
596
|
+
(js-object
|
|
597
|
+
:args (if (nullish? args-spec) nil (resolve-spec args-spec))
|
|
598
|
+
:ret (if (nullish? ret-spec) nil (resolve-spec ret-spec))
|
|
599
|
+
:fn (if (nullish? fn-spec) nil (resolve-spec fn-spec))))))
|
|
600
|
+
|
|
601
|
+
(defun fdef (fn-value fn-spec)
|
|
602
|
+
(let ((s (resolve-spec fn-spec)))
|
|
603
|
+
(unless (= (get s :kind) "fn")
|
|
604
|
+
(throw (new globalThis/TypeError "fdef requires a spec-fn schema")))
|
|
605
|
+
(unless (= (value-type fn-value) "function")
|
|
606
|
+
(throw (new globalThis/TypeError "fdef expects a function")))
|
|
607
|
+
(js-call REGISTRY :set fn-value s)
|
|
608
|
+
(instrument fn-value s)))
|
|
609
|
+
|
|
610
|
+
(defun instrument (fn-value &optional explicit-spec)
|
|
611
|
+
(let ((s (if (nullish? explicit-spec) (js-call REGISTRY :get fn-value) (resolve-spec explicit-spec))))
|
|
612
|
+
(when (nullish? s)
|
|
613
|
+
(throw (new globalThis/Error "instrument: function has no registered spec-fn")))
|
|
614
|
+
(let* ((cfg (get s :config))
|
|
615
|
+
(args-spec (get cfg :args))
|
|
616
|
+
(ret-spec (get cfg :ret))
|
|
617
|
+
(fn-rel-spec (get cfg :fn))
|
|
618
|
+
(wrapped
|
|
619
|
+
(lambda (&rest call-args)
|
|
620
|
+
(let ((arg-vec (js-call PersistentVector :from call-args))
|
|
621
|
+
(conformed-args nil))
|
|
622
|
+
(when args-spec
|
|
623
|
+
(setq conformed-args (conform args-spec arg-vec))
|
|
624
|
+
(when (invalid? conformed-args)
|
|
625
|
+
(throw (new globalThis/Error (str "Call args violated spec:\n" (explain-str args-spec arg-vec))))))
|
|
626
|
+
(let ((ret (apply fn-value call-args))
|
|
627
|
+
(conformed-ret nil))
|
|
628
|
+
(when ret-spec
|
|
629
|
+
(setq conformed-ret (conform ret-spec ret))
|
|
630
|
+
(when (invalid? conformed-ret)
|
|
631
|
+
(throw (new globalThis/Error (str "Return value violated spec:\n" (explain-str ret-spec ret))))))
|
|
632
|
+
(when fn-rel-spec
|
|
633
|
+
(let ((rel-input (kv-map
|
|
634
|
+
(keyword "args") (if args-spec conformed-args arg-vec)
|
|
635
|
+
(keyword "ret") (if ret-spec conformed-ret ret))))
|
|
636
|
+
(unless (valid? fn-rel-spec rel-input)
|
|
637
|
+
(throw (new globalThis/Error (str "Function :fn invariant violated:\n" (explain-str fn-rel-spec rel-input)))))))
|
|
638
|
+
ret)))))
|
|
639
|
+
(js-call REGISTRY :set wrapped s)
|
|
640
|
+
wrapped)))
|
|
641
|
+
|
|
642
|
+
;; ============================================================================
|
|
643
|
+
;; Collection Helpers
|
|
644
|
+
;; ============================================================================
|
|
645
|
+
|
|
646
|
+
(defun to-js-array (coll)
|
|
647
|
+
(cond
|
|
648
|
+
((nullish? coll) (js-array))
|
|
649
|
+
((Array/isArray coll) coll)
|
|
650
|
+
((Collection/isSeqable coll)
|
|
651
|
+
(let ((items (js-array)))
|
|
652
|
+
(Collection/reduce coll
|
|
653
|
+
(lambda (_acc item) (js-call items :push item) nil)
|
|
654
|
+
nil)
|
|
655
|
+
items))
|
|
656
|
+
(t
|
|
657
|
+
(throw (new globalThis/TypeError (str "expected sequential or iterable collection, got " (printValue coll)))))))
|
|
658
|
+
|
|
659
|
+
(defun array-contains-equal? (items target)
|
|
660
|
+
(let ((i 0)
|
|
661
|
+
(found false))
|
|
662
|
+
(while (and (not found) (< i (js-length items)))
|
|
663
|
+
(when (equalValues (aref items i) target)
|
|
664
|
+
(setq found t))
|
|
665
|
+
(setq i (1+ i)))
|
|
666
|
+
found))
|
|
667
|
+
|
|
668
|
+
(defun regex-schema? (schema)
|
|
669
|
+
(let ((k (get schema :kind)))
|
|
670
|
+
(or (= k "cat") (= k "alt") (= k "rep*") (= k "rep+") (= k "opt?"))))
|
|
671
|
+
|
|
672
|
+
(defun push-problem (problems path pred val via in-path reason)
|
|
673
|
+
(js-call problems :push
|
|
674
|
+
(kv-map
|
|
675
|
+
KW_PATH (js-call PersistentVector :from path)
|
|
676
|
+
KW_PRED pred
|
|
677
|
+
KW_VAL val
|
|
678
|
+
KW_VIA (js-call PersistentVector :from via)
|
|
679
|
+
KW_IN (js-call PersistentVector :from in-path)
|
|
680
|
+
KW_REASON reason)))
|
|
681
|
+
|
|
682
|
+
;; ============================================================================
|
|
683
|
+
;; Regex Sequence Matching (Backtracking)
|
|
684
|
+
;; ============================================================================
|
|
685
|
+
|
|
686
|
+
(defun regex-match (schema items pos)
|
|
687
|
+
(let ((kind (get schema :kind))
|
|
688
|
+
(cfg (get schema :config)))
|
|
689
|
+
(cond
|
|
690
|
+
((= kind "cat")
|
|
691
|
+
(regex-match-cat (get cfg :clauses) 0 items pos EMPTY_MAP))
|
|
692
|
+
((= kind "alt")
|
|
693
|
+
(let ((clauses (get cfg :clauses))
|
|
694
|
+
(i 0)
|
|
695
|
+
(res nil))
|
|
696
|
+
(while (and (nil? res) (< i (js-length clauses)))
|
|
697
|
+
(let* ((clause (aref clauses i))
|
|
698
|
+
(tag (get clause :tag))
|
|
699
|
+
(sub (get clause :spec))
|
|
700
|
+
(m (regex-match sub items pos)))
|
|
701
|
+
(when (get m :ok)
|
|
702
|
+
(setq res (js-object :ok t :pos (get m :pos)
|
|
703
|
+
:value (persistentVector tag (get m :value))))))
|
|
704
|
+
(setq i (1+ i)))
|
|
705
|
+
(if (nil? res)
|
|
706
|
+
(js-object :ok false :pos pos :reason "no spec-alt branch matched" :schema schema)
|
|
707
|
+
res)))
|
|
708
|
+
((= kind "opt?")
|
|
709
|
+
(let ((inner (get cfg :inner)))
|
|
710
|
+
(let ((m (regex-match inner items pos)))
|
|
711
|
+
(if (get m :ok)
|
|
712
|
+
m
|
|
713
|
+
(js-object :ok t :pos pos :value nil)))))
|
|
714
|
+
((or (= kind "rep*") (= kind "rep+"))
|
|
715
|
+
(let ((inner (get cfg :inner))
|
|
716
|
+
(cursor pos)
|
|
717
|
+
(acc (js-array))
|
|
718
|
+
(keep-going t))
|
|
719
|
+
(while (and keep-going (< cursor (js-length items)))
|
|
720
|
+
(let ((m (regex-match inner items cursor)))
|
|
721
|
+
(if (and (get m :ok) (> (get m :pos) cursor))
|
|
722
|
+
(progn
|
|
723
|
+
(js-call acc :push (get m :value))
|
|
724
|
+
(setq cursor (get m :pos)))
|
|
725
|
+
(setq keep-going false))))
|
|
726
|
+
(if (and (= kind "rep+") (= (js-length acc) 0))
|
|
727
|
+
(js-object :ok false :pos pos :reason "expected at least one element for spec-rep+" :schema inner)
|
|
728
|
+
(js-object :ok t :pos cursor :value (js-call PersistentVector :from acc)))))
|
|
729
|
+
(t
|
|
730
|
+
;; Element schema consumes exactly 1 item
|
|
731
|
+
(if (>= pos (js-length items))
|
|
732
|
+
(js-object :ok false :pos pos :reason "unexpected end of sequence" :schema schema)
|
|
733
|
+
(let* ((item (aref items pos))
|
|
734
|
+
(c (conform schema item)))
|
|
735
|
+
(if (invalid? c)
|
|
736
|
+
(js-object :ok false :pos pos :reason "element failed schema" :schema schema :item item)
|
|
737
|
+
(js-object :ok t :pos (1+ pos) :value c))))))))
|
|
738
|
+
|
|
739
|
+
(defun regex-match-cat (clauses idx items pos acc-map)
|
|
740
|
+
(if (>= idx (js-length clauses))
|
|
741
|
+
(js-object :ok t :pos pos :value acc-map)
|
|
742
|
+
(let* ((clause (aref clauses idx))
|
|
743
|
+
(tag (get clause :tag))
|
|
744
|
+
(sub (get clause :spec))
|
|
745
|
+
(sub-kind (get sub :kind)))
|
|
746
|
+
(if (or (= sub-kind "rep*") (= sub-kind "rep+"))
|
|
747
|
+
;; Backtracking repetition inside cat
|
|
748
|
+
(let* ((inner (get (get sub :config) :inner))
|
|
749
|
+
(min-c (if (= sub-kind "rep+") 1 0))
|
|
750
|
+
(matches (js-array))
|
|
751
|
+
(cursor pos)
|
|
752
|
+
(keep-going t))
|
|
753
|
+
(while (and keep-going (< cursor (js-length items)))
|
|
754
|
+
(let ((m (regex-match inner items cursor)))
|
|
755
|
+
(if (and (get m :ok) (> (get m :pos) cursor))
|
|
756
|
+
(progn
|
|
757
|
+
(js-call matches :push (js-object :val (get m :value) :next (get m :pos)))
|
|
758
|
+
(setq cursor (get m :pos)))
|
|
759
|
+
(setq keep-going false))))
|
|
760
|
+
(let ((n (js-length matches))
|
|
761
|
+
(found nil))
|
|
762
|
+
(if (< n min-c)
|
|
763
|
+
(js-object :ok false :pos cursor :reason "insufficient elements for repetition" :schema sub :clauseTag tag)
|
|
764
|
+
(progn
|
|
765
|
+
(while (and (nil? found) (>= n min-c))
|
|
766
|
+
(let* ((end-pos (if (= n 0) pos (get (aref matches (1- n)) :next)))
|
|
767
|
+
(slice-vals (js-array))
|
|
768
|
+
(k 0))
|
|
769
|
+
(while (< k n)
|
|
770
|
+
(js-call slice-vals :push (get (aref matches k) :val))
|
|
771
|
+
(setq k (1+ k)))
|
|
772
|
+
(let* ((next-acc (Collection/assoc acc-map tag (js-call PersistentVector :from slice-vals)))
|
|
773
|
+
(tail-res (regex-match-cat clauses (1+ idx) items end-pos next-acc)))
|
|
774
|
+
(when (get tail-res :ok)
|
|
775
|
+
(setq found tail-res))))
|
|
776
|
+
(setq n (1- n)))
|
|
777
|
+
(if (nil? found)
|
|
778
|
+
(js-object :ok false :pos pos :reason "cat continuation failed" :schema sub :clauseTag tag)
|
|
779
|
+
found)))))
|
|
780
|
+
(if (= sub-kind "opt?")
|
|
781
|
+
;; Try present first, then omitted
|
|
782
|
+
(let* ((inner (get (get sub :config) :inner))
|
|
783
|
+
(present (regex-match inner items pos))
|
|
784
|
+
(res (if (get present :ok)
|
|
785
|
+
(let ((next-acc (Collection/assoc acc-map tag (get present :value))))
|
|
786
|
+
(regex-match-cat clauses (1+ idx) items (get present :pos) next-acc))
|
|
787
|
+
(js-object :ok false))))
|
|
788
|
+
(if (get res :ok)
|
|
789
|
+
res
|
|
790
|
+
(regex-match-cat clauses (1+ idx) items pos acc-map)))
|
|
791
|
+
(let ((m (regex-match sub items pos)))
|
|
792
|
+
(if (not (get m :ok))
|
|
793
|
+
(js-object :ok false :pos (get m :pos) :reason (get m :reason)
|
|
794
|
+
:schema (get m :schema) :clauseTag tag :item (get m :item))
|
|
795
|
+
(regex-match-cat clauses (1+ idx) items (get m :pos)
|
|
796
|
+
(Collection/assoc acc-map tag (get m :value))))))))))
|
|
797
|
+
|
|
798
|
+
(defun regex-conform (schema value)
|
|
799
|
+
(if (not (or (Collection/isSequential value) (Collection/isList value) (Collection/isVector value) (Array/isArray value)))
|
|
800
|
+
INVALID
|
|
801
|
+
(let* ((items (to-js-array value))
|
|
802
|
+
(m (regex-match schema items 0)))
|
|
803
|
+
(if (and (get m :ok) (= (get m :pos) (js-length items)))
|
|
804
|
+
(get m :value)
|
|
805
|
+
INVALID))))
|
|
806
|
+
|
|
807
|
+
;; ============================================================================
|
|
808
|
+
;; Core Conform / Valid? / Unform / Explain
|
|
809
|
+
;; ============================================================================
|
|
810
|
+
|
|
811
|
+
(defun conform (schema value)
|
|
812
|
+
(let* ((s (resolve-spec schema))
|
|
813
|
+
(kind (get s :kind))
|
|
814
|
+
(cfg (get s :config)))
|
|
815
|
+
(cond
|
|
816
|
+
((= kind "any") value)
|
|
817
|
+
((= kind "pred")
|
|
818
|
+
(if (truthy? (funcall (get cfg :pred) value)) value INVALID))
|
|
819
|
+
((= kind "int-in")
|
|
820
|
+
(if (and (Number/isSafeInteger value)
|
|
821
|
+
(>= value (get cfg :min))
|
|
822
|
+
(<= value (get cfg :max)))
|
|
823
|
+
value
|
|
824
|
+
INVALID))
|
|
825
|
+
((= kind "double-in")
|
|
826
|
+
(if (not (= (value-type value) "number"))
|
|
827
|
+
INVALID
|
|
828
|
+
(if (Number/isNaN value)
|
|
829
|
+
(if (get cfg :NaN?) value INVALID)
|
|
830
|
+
(if (not (Number/isFinite value))
|
|
831
|
+
(if (get cfg :infinite?) value INVALID)
|
|
832
|
+
(if (and (>= value (get cfg :min)) (<= value (get cfg :max))) value INVALID)))))
|
|
833
|
+
((= kind "string-len")
|
|
834
|
+
(if (not (= (value-type value) "string"))
|
|
835
|
+
INVALID
|
|
836
|
+
(let ((len (js-length value))
|
|
837
|
+
(hi (get cfg :max)))
|
|
838
|
+
(if (and (>= len (get cfg :min)) (or (nullish? hi) (<= len hi)))
|
|
839
|
+
value
|
|
840
|
+
INVALID))))
|
|
841
|
+
((= kind "nilable")
|
|
842
|
+
(if (nil? value) nil (conform (get cfg :inner) value)))
|
|
843
|
+
((= kind "enum")
|
|
844
|
+
(if (array-contains-equal? (get cfg :values) value) value INVALID))
|
|
845
|
+
((= kind "and")
|
|
846
|
+
(let ((specs (get cfg :specs))
|
|
847
|
+
(cur value)
|
|
848
|
+
(i 0)
|
|
849
|
+
(failed false))
|
|
850
|
+
(while (and (not failed) (< i (js-length specs)))
|
|
851
|
+
(setq cur (conform (aref specs i) cur))
|
|
852
|
+
(when (invalid? cur) (setq failed t))
|
|
853
|
+
(setq i (1+ i)))
|
|
854
|
+
(if failed INVALID cur)))
|
|
855
|
+
((= kind "or")
|
|
856
|
+
(let ((clauses (get cfg :clauses))
|
|
857
|
+
(i 0)
|
|
858
|
+
(res INVALID))
|
|
859
|
+
(while (and (invalid? res) (< i (js-length clauses)))
|
|
860
|
+
(let* ((clause (aref clauses i))
|
|
861
|
+
(c (conform (get clause :spec) value)))
|
|
862
|
+
(unless (invalid? c)
|
|
863
|
+
(setq res (persistentVector (get clause :tag) c))))
|
|
864
|
+
(setq i (1+ i)))
|
|
865
|
+
res))
|
|
866
|
+
((= kind "keys")
|
|
867
|
+
(if (not (Collection/isMap value))
|
|
868
|
+
INVALID
|
|
869
|
+
(let ((req (get cfg :req))
|
|
870
|
+
(opt (get cfg :opt))
|
|
871
|
+
(closed (get cfg :closed))
|
|
872
|
+
(out value)
|
|
873
|
+
(failed false)
|
|
874
|
+
(i 0))
|
|
875
|
+
(while (and (not failed) (< i (js-length req)))
|
|
876
|
+
(let* ((entry (aref req i))
|
|
877
|
+
(k (get entry :key))
|
|
878
|
+
(v (Collection/get value k MISSING)))
|
|
879
|
+
(if (eq v MISSING)
|
|
880
|
+
(setq failed t)
|
|
881
|
+
(let ((cv (conform (get entry :spec) v)))
|
|
882
|
+
(if (invalid? cv)
|
|
883
|
+
(setq failed t)
|
|
884
|
+
(unless (equalValues cv v)
|
|
885
|
+
(setq out (Collection/assoc out k cv)))))))
|
|
886
|
+
(setq i (1+ i)))
|
|
887
|
+
(setq i 0)
|
|
888
|
+
(while (and (not failed) (< i (js-length opt)))
|
|
889
|
+
(let* ((entry (aref opt i))
|
|
890
|
+
(k (get entry :key))
|
|
891
|
+
(v (Collection/get value k MISSING)))
|
|
892
|
+
(unless (eq v MISSING)
|
|
893
|
+
(let ((cv (conform (get entry :spec) v)))
|
|
894
|
+
(if (invalid? cv)
|
|
895
|
+
(setq failed t)
|
|
896
|
+
(unless (equalValues cv v)
|
|
897
|
+
(setq out (Collection/assoc out k cv)))))))
|
|
898
|
+
(setq i (1+ i)))
|
|
899
|
+
(when (and (not failed) closed)
|
|
900
|
+
(let ((ks (js-call req :concat opt)))
|
|
901
|
+
(Collection/reduceKV value
|
|
902
|
+
(lambda (_acc k _v)
|
|
903
|
+
(let ((found false)
|
|
904
|
+
(j 0))
|
|
905
|
+
(while (and (not found) (< j (js-length ks)))
|
|
906
|
+
(when (equalValues k (get (aref ks j) :key))
|
|
907
|
+
(setq found t))
|
|
908
|
+
(setq j (1+ j)))
|
|
909
|
+
(unless found (setq failed t)))
|
|
910
|
+
nil)
|
|
911
|
+
nil)))
|
|
912
|
+
(if failed INVALID out))))
|
|
913
|
+
((= kind "coll-of")
|
|
914
|
+
(if (not (or (Collection/isVector value)
|
|
915
|
+
(Collection/isList value)
|
|
916
|
+
(Collection/isSet value)
|
|
917
|
+
(Collection/isQueue value)
|
|
918
|
+
(Collection/isSequence value)
|
|
919
|
+
(Array/isArray value)))
|
|
920
|
+
INVALID
|
|
921
|
+
(let* ((expected-kind (get cfg :kind))
|
|
922
|
+
(kind-ok (cond
|
|
923
|
+
((nullish? expected-kind) t)
|
|
924
|
+
((= expected-kind "vector") (Collection/isVector value))
|
|
925
|
+
((= expected-kind "list") (Collection/isList value))
|
|
926
|
+
((= expected-kind "set") (Collection/isSet value))
|
|
927
|
+
((= (value-type expected-kind) "function") (truthy? (funcall expected-kind value)))
|
|
928
|
+
(t t))))
|
|
929
|
+
(if (not kind-ok)
|
|
930
|
+
INVALID
|
|
931
|
+
(let ((items (to-js-array value))
|
|
932
|
+
(min-c (get cfg :minCount))
|
|
933
|
+
(max-c (get cfg :maxCount))
|
|
934
|
+
(uniq (get cfg :distinct))
|
|
935
|
+
(item-spec (get cfg :item))
|
|
936
|
+
(conformed (js-array))
|
|
937
|
+
(seen (js-array))
|
|
938
|
+
(failed false)
|
|
939
|
+
(i 0))
|
|
940
|
+
(when (or (< (js-length items) min-c)
|
|
941
|
+
(and (not (nullish? max-c)) (> (js-length items) max-c)))
|
|
942
|
+
(setq failed t))
|
|
943
|
+
(while (and (not failed) (< i (js-length items)))
|
|
944
|
+
(let* ((el (aref items i))
|
|
945
|
+
(ce (conform item-spec el)))
|
|
946
|
+
(if (invalid? ce)
|
|
947
|
+
(setq failed t)
|
|
948
|
+
(if (and uniq (array-contains-equal? seen el))
|
|
949
|
+
(setq failed t)
|
|
950
|
+
(progn
|
|
951
|
+
(when uniq (js-call seen :push el))
|
|
952
|
+
(js-call conformed :push ce)))))
|
|
953
|
+
(setq i (1+ i)))
|
|
954
|
+
(if failed
|
|
955
|
+
INVALID
|
|
956
|
+
(cond
|
|
957
|
+
((Collection/isVector value) (js-call PersistentVector :from conformed))
|
|
958
|
+
((Collection/isSet value) (apply persistentHashSet conformed))
|
|
959
|
+
((Collection/isList value) (apply persistentList conformed))
|
|
960
|
+
(t (js-call PersistentVector :from conformed)))))))))
|
|
961
|
+
((= kind "map-of")
|
|
962
|
+
(if (not (Collection/isMap value))
|
|
963
|
+
INVALID
|
|
964
|
+
(let ((min-c (get cfg :minCount))
|
|
965
|
+
(max-c (get cfg :maxCount))
|
|
966
|
+
(cnt (Collection/count value))
|
|
967
|
+
(ks (get cfg :key))
|
|
968
|
+
(vs (get cfg :val))
|
|
969
|
+
(entries (js-array)))
|
|
970
|
+
(if (or (< cnt min-c) (and (not (nullish? max-c)) (> cnt max-c)))
|
|
971
|
+
INVALID
|
|
972
|
+
(progn
|
|
973
|
+
(Collection/reduceKV value
|
|
974
|
+
(lambda (_acc k v)
|
|
975
|
+
(js-call entries :push (js-array k v))
|
|
976
|
+
nil)
|
|
977
|
+
nil)
|
|
978
|
+
(let ((pairs (js-array))
|
|
979
|
+
(failed false)
|
|
980
|
+
(i 0))
|
|
981
|
+
(while (and (not failed) (< i (js-length entries)))
|
|
982
|
+
(let* ((pair (aref entries i))
|
|
983
|
+
(ck (conform ks (aref pair 0)))
|
|
984
|
+
(cv (conform vs (aref pair 1))))
|
|
985
|
+
(if (or (invalid? ck) (invalid? cv))
|
|
986
|
+
(setq failed t)
|
|
987
|
+
(progn
|
|
988
|
+
(js-call pairs :push ck)
|
|
989
|
+
(js-call pairs :push cv))))
|
|
990
|
+
(setq i (1+ i)))
|
|
991
|
+
(if failed INVALID (apply kv-map pairs))))))))
|
|
992
|
+
((= kind "tuple")
|
|
993
|
+
(if (not (or (Collection/isVector value) (Array/isArray value)))
|
|
994
|
+
INVALID
|
|
995
|
+
(let ((items (to-js-array value))
|
|
996
|
+
(item-specs (get cfg :items)))
|
|
997
|
+
(if (not (= (js-length items) (js-length item-specs)))
|
|
998
|
+
INVALID
|
|
999
|
+
(let ((conformed (js-array))
|
|
1000
|
+
(failed false)
|
|
1001
|
+
(i 0))
|
|
1002
|
+
(while (and (not failed) (< i (js-length item-specs)))
|
|
1003
|
+
(let ((ce (conform (aref item-specs i) (aref items i))))
|
|
1004
|
+
(if (invalid? ce)
|
|
1005
|
+
(setq failed t)
|
|
1006
|
+
(js-call conformed :push ce)))
|
|
1007
|
+
(setq i (1+ i)))
|
|
1008
|
+
(if failed INVALID (js-call PersistentVector :from conformed)))))))
|
|
1009
|
+
((regex-schema? s)
|
|
1010
|
+
(regex-conform s value))
|
|
1011
|
+
((= kind "fn")
|
|
1012
|
+
(if (= (value-type value) "function") value INVALID))
|
|
1013
|
+
(t INVALID))))
|
|
1014
|
+
|
|
1015
|
+
(defun valid? (schema value)
|
|
1016
|
+
(not (invalid? (conform schema value))))
|
|
1017
|
+
|
|
1018
|
+
(defun unform (schema conformed)
|
|
1019
|
+
(let* ((s (resolve-spec schema))
|
|
1020
|
+
(kind (get s :kind))
|
|
1021
|
+
(cfg (get s :config)))
|
|
1022
|
+
(cond
|
|
1023
|
+
((= kind "or")
|
|
1024
|
+
(let* ((pair (to-js-array conformed))
|
|
1025
|
+
(tag (aref pair 0))
|
|
1026
|
+
(val (aref pair 1))
|
|
1027
|
+
(clauses (get cfg :clauses))
|
|
1028
|
+
(i 0)
|
|
1029
|
+
(found nil))
|
|
1030
|
+
(while (and (nil? found) (< i (js-length clauses)))
|
|
1031
|
+
(let ((c (aref clauses i)))
|
|
1032
|
+
(when (equalValues (get c :tag) tag)
|
|
1033
|
+
(setq found (unform (get c :spec) val))))
|
|
1034
|
+
(setq i (1+ i)))
|
|
1035
|
+
(if (nil? found) val found)))
|
|
1036
|
+
((= kind "alt")
|
|
1037
|
+
(let* ((pair (to-js-array conformed))
|
|
1038
|
+
(tag (aref pair 0))
|
|
1039
|
+
(val (aref pair 1))
|
|
1040
|
+
(clauses (get cfg :clauses))
|
|
1041
|
+
(i 0)
|
|
1042
|
+
(found nil))
|
|
1043
|
+
(while (and (nil? found) (< i (js-length clauses)))
|
|
1044
|
+
(let ((c (aref clauses i)))
|
|
1045
|
+
(when (equalValues (get c :tag) tag)
|
|
1046
|
+
(setq found (unform (get c :spec) val))))
|
|
1047
|
+
(setq i (1+ i)))
|
|
1048
|
+
(persistentVector (if (nil? found) val found))))
|
|
1049
|
+
((= kind "cat")
|
|
1050
|
+
(let ((clauses (get cfg :clauses))
|
|
1051
|
+
(items (js-array))
|
|
1052
|
+
(i 0))
|
|
1053
|
+
(while (< i (js-length clauses))
|
|
1054
|
+
(let* ((c (aref clauses i))
|
|
1055
|
+
(tag (get c :tag))
|
|
1056
|
+
(sub (get c :spec)))
|
|
1057
|
+
(when (Collection/contains conformed tag)
|
|
1058
|
+
(let ((v (Collection/get conformed tag)))
|
|
1059
|
+
(if (or (= (get sub :kind) "rep*") (= (get sub :kind) "rep+"))
|
|
1060
|
+
(let ((inner (get (get sub :config) :inner))
|
|
1061
|
+
(arr (to-js-array v))
|
|
1062
|
+
(j 0))
|
|
1063
|
+
(while (< j (js-length arr))
|
|
1064
|
+
(let ((u (unform inner (aref arr j))))
|
|
1065
|
+
(if (regex-schema? inner)
|
|
1066
|
+
(let ((ua (to-js-array u)) (k 0))
|
|
1067
|
+
(while (< k (js-length ua))
|
|
1068
|
+
(js-call items :push (aref ua k))
|
|
1069
|
+
(setq k (1+ k))))
|
|
1070
|
+
(js-call items :push u)))
|
|
1071
|
+
(setq j (1+ j))))
|
|
1072
|
+
(let ((u (unform sub v)))
|
|
1073
|
+
(if (regex-schema? sub)
|
|
1074
|
+
(let ((ua (to-js-array u)) (k 0))
|
|
1075
|
+
(while (< k (js-length ua))
|
|
1076
|
+
(js-call items :push (aref ua k))
|
|
1077
|
+
(setq k (1+ k))))
|
|
1078
|
+
(js-call items :push u)))))))
|
|
1079
|
+
(setq i (1+ i)))
|
|
1080
|
+
(js-call PersistentVector :from items)))
|
|
1081
|
+
((or (= kind "rep*") (= kind "rep+"))
|
|
1082
|
+
(let ((inner (get cfg :inner))
|
|
1083
|
+
(arr (to-js-array conformed))
|
|
1084
|
+
(items (js-array))
|
|
1085
|
+
(i 0))
|
|
1086
|
+
(while (< i (js-length arr))
|
|
1087
|
+
(let ((u (unform inner (aref arr i))))
|
|
1088
|
+
(if (regex-schema? inner)
|
|
1089
|
+
(let ((ua (to-js-array u)) (k 0))
|
|
1090
|
+
(while (< k (js-length ua))
|
|
1091
|
+
(js-call items :push (aref ua k))
|
|
1092
|
+
(setq k (1+ k))))
|
|
1093
|
+
(js-call items :push u)))
|
|
1094
|
+
(setq i (1+ i)))
|
|
1095
|
+
(js-call PersistentVector :from items)))
|
|
1096
|
+
((= kind "opt?")
|
|
1097
|
+
(if (nil? conformed)
|
|
1098
|
+
EMPTY_VECTOR
|
|
1099
|
+
(let* ((inner (get cfg :inner))
|
|
1100
|
+
(u (unform inner conformed)))
|
|
1101
|
+
(if (regex-schema? inner) u (persistentVector u)))))
|
|
1102
|
+
((= kind "keys")
|
|
1103
|
+
(let ((out conformed)
|
|
1104
|
+
(all-entries (js-call (get cfg :req) :concat (get cfg :opt)))
|
|
1105
|
+
(i 0))
|
|
1106
|
+
(while (< i (js-length all-entries))
|
|
1107
|
+
(let* ((e (aref all-entries i))
|
|
1108
|
+
(k (get e :key)))
|
|
1109
|
+
(when (Collection/contains out k)
|
|
1110
|
+
(setq out (Collection/assoc out k (unform (get e :spec) (Collection/get out k))))))
|
|
1111
|
+
(setq i (1+ i)))
|
|
1112
|
+
out))
|
|
1113
|
+
((= kind "coll-of")
|
|
1114
|
+
(let ((item-spec (get cfg :item))
|
|
1115
|
+
(arr (to-js-array conformed)))
|
|
1116
|
+
(let ((out (js-call arr :map (lambda (el) (unform item-spec el)))))
|
|
1117
|
+
(cond
|
|
1118
|
+
((Collection/isSet conformed) (apply persistentHashSet out))
|
|
1119
|
+
((Collection/isList conformed) (apply persistentList out))
|
|
1120
|
+
(t (js-call PersistentVector :from out))))))
|
|
1121
|
+
((= kind "tuple")
|
|
1122
|
+
(let ((specs (get cfg :items))
|
|
1123
|
+
(arr (to-js-array conformed))
|
|
1124
|
+
(out (js-array))
|
|
1125
|
+
(i 0))
|
|
1126
|
+
(while (< i (js-length specs))
|
|
1127
|
+
(js-call out :push (unform (aref specs i) (aref arr i)))
|
|
1128
|
+
(setq i (1+ i)))
|
|
1129
|
+
(js-call PersistentVector :from out)))
|
|
1130
|
+
((= kind "and")
|
|
1131
|
+
(let ((specs (get cfg :specs))
|
|
1132
|
+
(cur conformed)
|
|
1133
|
+
(i (1- (js-length (get cfg :specs)))))
|
|
1134
|
+
(while (>= i 0)
|
|
1135
|
+
(setq cur (unform (aref specs i) cur))
|
|
1136
|
+
(setq i (1- i)))
|
|
1137
|
+
cur))
|
|
1138
|
+
((= kind "nilable")
|
|
1139
|
+
(if (nil? conformed) nil (unform (get cfg :inner) conformed)))
|
|
1140
|
+
(t conformed))))
|
|
1141
|
+
|
|
1142
|
+
(defun explain-into (schema value path via in-path problems)
|
|
1143
|
+
(let* ((s (resolve-spec schema))
|
|
1144
|
+
(kind (get s :kind))
|
|
1145
|
+
(name (get s :name))
|
|
1146
|
+
(cfg (get s :config))
|
|
1147
|
+
(next-via (js-call via :concat (js-array name))))
|
|
1148
|
+
(cond
|
|
1149
|
+
((= kind "any") nil)
|
|
1150
|
+
((= kind "pred")
|
|
1151
|
+
(unless (truthy? (funcall (get cfg :pred) value))
|
|
1152
|
+
(push-problem problems path name value via in-path "predicate failed")))
|
|
1153
|
+
((= kind "int-in")
|
|
1154
|
+
(unless (and (Number/isSafeInteger value)
|
|
1155
|
+
(>= value (get cfg :min))
|
|
1156
|
+
(<= value (get cfg :max)))
|
|
1157
|
+
(push-problem problems path name value via in-path
|
|
1158
|
+
(str "expected integer in [" (get cfg :min) ", " (get cfg :max) "]"))))
|
|
1159
|
+
((= kind "double-in")
|
|
1160
|
+
(when (invalid? (conform s value))
|
|
1161
|
+
(push-problem problems path name value via in-path "number out of bounds or invalid float")))
|
|
1162
|
+
((= kind "string-len")
|
|
1163
|
+
(when (invalid? (conform s value))
|
|
1164
|
+
(push-problem problems path name value via in-path "expected string within length bounds")))
|
|
1165
|
+
((= kind "nilable")
|
|
1166
|
+
(unless (nil? value)
|
|
1167
|
+
(explain-into (get cfg :inner) value path next-via in-path problems)))
|
|
1168
|
+
((= kind "enum")
|
|
1169
|
+
(unless (array-contains-equal? (get cfg :values) value)
|
|
1170
|
+
(push-problem problems path "enum" value via in-path "value not in allowed enum set")))
|
|
1171
|
+
((= kind "and")
|
|
1172
|
+
(let ((specs (get cfg :specs))
|
|
1173
|
+
(cur value)
|
|
1174
|
+
(i 0)
|
|
1175
|
+
(failed false))
|
|
1176
|
+
(while (and (not failed) (< i (js-length specs)))
|
|
1177
|
+
(let ((sub (aref specs i)))
|
|
1178
|
+
(let ((c (conform sub cur)))
|
|
1179
|
+
(if (invalid? c)
|
|
1180
|
+
(progn
|
|
1181
|
+
(explain-into sub cur (js-call path :concat (js-array i)) next-via in-path problems)
|
|
1182
|
+
(setq failed t))
|
|
1183
|
+
(setq cur c))))
|
|
1184
|
+
(setq i (1+ i)))))
|
|
1185
|
+
((= kind "or")
|
|
1186
|
+
(when (invalid? (conform s value))
|
|
1187
|
+
(let ((clauses (get cfg :clauses))
|
|
1188
|
+
(i 0))
|
|
1189
|
+
(while (< i (js-length clauses))
|
|
1190
|
+
(let ((clause (aref clauses i)))
|
|
1191
|
+
(explain-into (get clause :spec) value
|
|
1192
|
+
(js-call path :concat (js-array (get clause :tag)))
|
|
1193
|
+
next-via in-path problems))
|
|
1194
|
+
(setq i (1+ i))))))
|
|
1195
|
+
((= kind "keys")
|
|
1196
|
+
(if (not (Collection/isMap value))
|
|
1197
|
+
(push-problem problems path "map?" value via in-path "expected a map")
|
|
1198
|
+
(let ((req (get cfg :req))
|
|
1199
|
+
(opt (get cfg :opt))
|
|
1200
|
+
(closed (get cfg :closed))
|
|
1201
|
+
(i 0))
|
|
1202
|
+
(while (< i (js-length req))
|
|
1203
|
+
(let* ((entry (aref req i))
|
|
1204
|
+
(k (get entry :key))
|
|
1205
|
+
(v (Collection/get value k MISSING)))
|
|
1206
|
+
(if (eq v MISSING)
|
|
1207
|
+
(push-problem problems (js-call path :concat (js-array k))
|
|
1208
|
+
(str "contains? " (printValue k)) value next-via in-path
|
|
1209
|
+
(str "missing required key " (printValue k)))
|
|
1210
|
+
(unless (valid? (get entry :spec) v)
|
|
1211
|
+
(explain-into (get entry :spec) v
|
|
1212
|
+
(js-call path :concat (js-array k))
|
|
1213
|
+
next-via (js-call in-path :concat (js-array k)) problems))))
|
|
1214
|
+
(setq i (1+ i)))
|
|
1215
|
+
(setq i 0)
|
|
1216
|
+
(while (< i (js-length opt))
|
|
1217
|
+
(let* ((entry (aref opt i))
|
|
1218
|
+
(k (get entry :key))
|
|
1219
|
+
(v (Collection/get value k MISSING)))
|
|
1220
|
+
(when (and (not (eq v MISSING)) (not (valid? (get entry :spec) v)))
|
|
1221
|
+
(explain-into (get entry :spec) v
|
|
1222
|
+
(js-call path :concat (js-array k))
|
|
1223
|
+
next-via (js-call in-path :concat (js-array k)) problems)))
|
|
1224
|
+
(setq i (1+ i)))
|
|
1225
|
+
(when closed
|
|
1226
|
+
(let ((all-ks (js-call req :concat opt)))
|
|
1227
|
+
(Collection/reduceKV value
|
|
1228
|
+
(lambda (_acc k v)
|
|
1229
|
+
(let ((found false)
|
|
1230
|
+
(j 0))
|
|
1231
|
+
(while (and (not found) (< j (js-length all-ks)))
|
|
1232
|
+
(when (equalValues k (get (aref all-ks j) :key))
|
|
1233
|
+
(setq found t))
|
|
1234
|
+
(setq j (1+ j)))
|
|
1235
|
+
(unless found
|
|
1236
|
+
(push-problem problems path "closed-keys" v next-via
|
|
1237
|
+
(js-call in-path :concat (js-array k))
|
|
1238
|
+
(str "unexpected extra key " (printValue k)))))
|
|
1239
|
+
nil)
|
|
1240
|
+
nil))))))
|
|
1241
|
+
((= kind "coll-of")
|
|
1242
|
+
(if (not (or (Collection/isVector value)
|
|
1243
|
+
(Collection/isList value)
|
|
1244
|
+
(Collection/isSet value)
|
|
1245
|
+
(Collection/isQueue value)
|
|
1246
|
+
(Collection/isSequence value)
|
|
1247
|
+
(Array/isArray value)))
|
|
1248
|
+
(push-problem problems path "coll?" value via in-path "expected collection")
|
|
1249
|
+
(let* ((expected-kind (get cfg :kind))
|
|
1250
|
+
(kind-ok (cond
|
|
1251
|
+
((nullish? expected-kind) t)
|
|
1252
|
+
((= expected-kind "vector") (Collection/isVector value))
|
|
1253
|
+
((= expected-kind "list") (Collection/isList value))
|
|
1254
|
+
((= expected-kind "set") (Collection/isSet value))
|
|
1255
|
+
((= (value-type expected-kind) "function") (truthy? (funcall expected-kind value)))
|
|
1256
|
+
(t t))))
|
|
1257
|
+
(if (not kind-ok)
|
|
1258
|
+
(push-problem problems path (str "kind:" expected-kind) value via in-path "collection kind mismatch")
|
|
1259
|
+
(let ((items (to-js-array value))
|
|
1260
|
+
(min-c (get cfg :minCount))
|
|
1261
|
+
(max-c (get cfg :maxCount))
|
|
1262
|
+
(uniq (get cfg :distinct))
|
|
1263
|
+
(item-spec (get cfg :item))
|
|
1264
|
+
(seen (js-array))
|
|
1265
|
+
(i 0))
|
|
1266
|
+
(when (< (js-length items) min-c)
|
|
1267
|
+
(push-problem problems path (str "min-count>=" min-c) value next-via in-path
|
|
1268
|
+
(str "expected at least " min-c " elements, got " (js-length items))))
|
|
1269
|
+
(when (and (not (nullish? max-c)) (> (js-length items) max-c))
|
|
1270
|
+
(push-problem problems path (str "max-count<=" max-c) value next-via in-path
|
|
1271
|
+
(str "expected at most " max-c " elements, got " (js-length items))))
|
|
1272
|
+
(while (< i (js-length items))
|
|
1273
|
+
(let ((el (aref items i)))
|
|
1274
|
+
(when (and uniq (array-contains-equal? seen el))
|
|
1275
|
+
(push-problem problems path "distinct" el next-via
|
|
1276
|
+
(js-call in-path :concat (js-array i)) "duplicate collection element"))
|
|
1277
|
+
(when uniq (js-call seen :push el))
|
|
1278
|
+
(unless (valid? item-spec el)
|
|
1279
|
+
(explain-into item-spec el path next-via
|
|
1280
|
+
(js-call in-path :concat (js-array i)) problems)))
|
|
1281
|
+
(setq i (1+ i))))))))
|
|
1282
|
+
((= kind "map-of")
|
|
1283
|
+
(if (not (Collection/isMap value))
|
|
1284
|
+
(push-problem problems path "map?" value via in-path "expected a map")
|
|
1285
|
+
(let ((cnt (Collection/count value))
|
|
1286
|
+
(min-c (get cfg :minCount))
|
|
1287
|
+
(max-c (get cfg :maxCount))
|
|
1288
|
+
(ks (get cfg :key))
|
|
1289
|
+
(vs (get cfg :val))
|
|
1290
|
+
(entries (js-array)))
|
|
1291
|
+
(when (< cnt min-c)
|
|
1292
|
+
(push-problem problems path (str "min-count>=" min-c) value next-via in-path
|
|
1293
|
+
(str "expected at least " min-c " map entries")))
|
|
1294
|
+
(when (and (not (nullish? max-c)) (> cnt max-c))
|
|
1295
|
+
(push-problem problems path (str "max-count<=" max-c) value next-via in-path
|
|
1296
|
+
(str "expected at most " max-c " map entries")))
|
|
1297
|
+
(Collection/reduceKV value
|
|
1298
|
+
(lambda (_acc k v) (js-call entries :push (js-array k v)) nil)
|
|
1299
|
+
nil)
|
|
1300
|
+
(let ((i 0))
|
|
1301
|
+
(while (< i (js-length entries))
|
|
1302
|
+
(let* ((pair (aref entries i))
|
|
1303
|
+
(k (aref pair 0))
|
|
1304
|
+
(v (aref pair 1)))
|
|
1305
|
+
(unless (valid? ks k)
|
|
1306
|
+
(explain-into ks k (js-call path :concat (js-array (keyword "key")))
|
|
1307
|
+
next-via (js-call in-path :concat (js-array k)) problems))
|
|
1308
|
+
(unless (valid? vs v)
|
|
1309
|
+
(explain-into vs v (js-call path :concat (js-array (keyword "val")))
|
|
1310
|
+
next-via (js-call in-path :concat (js-array k)) problems)))
|
|
1311
|
+
(setq i (1+ i)))))))
|
|
1312
|
+
((= kind "tuple")
|
|
1313
|
+
(if (not (or (Collection/isVector value) (Array/isArray value)))
|
|
1314
|
+
(push-problem problems path "vector?" value via in-path "expected tuple vector")
|
|
1315
|
+
(let ((items (to-js-array value))
|
|
1316
|
+
(specs (get cfg :items)))
|
|
1317
|
+
(if (not (= (js-length items) (js-length specs)))
|
|
1318
|
+
(push-problem problems path (str "count=" (js-length specs)) value via in-path
|
|
1319
|
+
(str "expected tuple of length " (js-length specs) ", got " (js-length items)))
|
|
1320
|
+
(let ((i 0))
|
|
1321
|
+
(while (< i (js-length specs))
|
|
1322
|
+
(unless (valid? (aref specs i) (aref items i))
|
|
1323
|
+
(explain-into (aref specs i) (aref items i)
|
|
1324
|
+
(js-call path :concat (js-array i))
|
|
1325
|
+
next-via (js-call in-path :concat (js-array i)) problems))
|
|
1326
|
+
(setq i (1+ i))))))))
|
|
1327
|
+
((regex-schema? s)
|
|
1328
|
+
(if (not (or (Collection/isSequential value) (Collection/isList value) (Collection/isVector value) (Array/isArray value)))
|
|
1329
|
+
(push-problem problems path "sequential?" value via in-path "expected sequence")
|
|
1330
|
+
(let* ((items (to-js-array value))
|
|
1331
|
+
(m (regex-match s items 0)))
|
|
1332
|
+
(cond
|
|
1333
|
+
((not (get m :ok))
|
|
1334
|
+
(let ((bad-pos (get m :pos))
|
|
1335
|
+
(tag (get m :clauseTag)))
|
|
1336
|
+
(push-problem problems
|
|
1337
|
+
(if (nullish? tag) path (js-call path :concat (js-array tag)))
|
|
1338
|
+
(if (get m :schema) (get (get m :schema) :name) name)
|
|
1339
|
+
(if (< bad-pos (js-length items)) (aref items bad-pos) nil)
|
|
1340
|
+
next-via
|
|
1341
|
+
(js-call in-path :concat (js-array bad-pos))
|
|
1342
|
+
(get m :reason))))
|
|
1343
|
+
((< (get m :pos) (js-length items))
|
|
1344
|
+
(push-problem problems path name (aref items (get m :pos))
|
|
1345
|
+
next-via (js-call in-path :concat (js-array (get m :pos)))
|
|
1346
|
+
"unexpected trailing element in sequence"))))))
|
|
1347
|
+
(t
|
|
1348
|
+
(when (invalid? (conform s value))
|
|
1349
|
+
(push-problem problems path name value via in-path "schema check failed"))))))
|
|
1350
|
+
|
|
1351
|
+
(defun explain-data (schema value)
|
|
1352
|
+
(let ((s (resolve-spec schema)))
|
|
1353
|
+
(if (valid? s value)
|
|
1354
|
+
nil
|
|
1355
|
+
(let ((problems (js-array)))
|
|
1356
|
+
(explain-into s value (js-array) (js-array) (js-array) problems)
|
|
1357
|
+
(when (= (js-length problems) 0)
|
|
1358
|
+
(push-problem problems (js-array) (get s :name) value (js-array) (js-array) "validation failed"))
|
|
1359
|
+
(kv-map
|
|
1360
|
+
KW_SCHEMA s
|
|
1361
|
+
KW_VALUE value
|
|
1362
|
+
KW_PROBLEMS (js-call PersistentVector :from problems))))))
|
|
1363
|
+
|
|
1364
|
+
(defun explain-str (schema value)
|
|
1365
|
+
(let ((data (explain-data schema value)))
|
|
1366
|
+
(if (nil? data)
|
|
1367
|
+
"Success!\n"
|
|
1368
|
+
(let* ((problems (Collection/get data KW_PROBLEMS EMPTY_VECTOR))
|
|
1369
|
+
(lines (js-array))
|
|
1370
|
+
(i 0)
|
|
1371
|
+
(count (Collection/count problems)))
|
|
1372
|
+
(while (< i count)
|
|
1373
|
+
(let* ((p (js-call problems :nth i))
|
|
1374
|
+
(val-str (printValue (Collection/get p KW_VAL)))
|
|
1375
|
+
(pred-str (Collection/get p KW_PRED))
|
|
1376
|
+
(in-vec (Collection/get p KW_IN EMPTY_VECTOR))
|
|
1377
|
+
(path-vec (Collection/get p KW_PATH EMPTY_VECTOR))
|
|
1378
|
+
(reason (Collection/get p KW_REASON "")))
|
|
1379
|
+
(js-call lines :push
|
|
1380
|
+
(str val-str " - failed: " pred-str
|
|
1381
|
+
(if (> (Collection/count in-vec) 0) (str " in: " (printValue in-vec)) "")
|
|
1382
|
+
(if (> (Collection/count path-vec) 0) (str " at: " (printValue path-vec)) "")
|
|
1383
|
+
(if (> (js-length reason) 0) (str " (" reason ")") ""))))
|
|
1384
|
+
(setq i (1+ i)))
|
|
1385
|
+
(str (js-call lines :join "\n") "\n")))))
|
|
1386
|
+
|
|
1387
|
+
(defun explain (schema value)
|
|
1388
|
+
(console/log (js-call (explain-str schema value) :trimEnd))
|
|
1389
|
+
nil)
|
|
1390
|
+
|
|
1391
|
+
;; ============================================================================
|
|
1392
|
+
;; Schema-to-Generator Synthesis & Seeded Sampling
|
|
1393
|
+
;; ============================================================================
|
|
1394
|
+
|
|
1395
|
+
(defun gen-from-spec (schema)
|
|
1396
|
+
(let* ((s (resolve-spec schema))
|
|
1397
|
+
(custom (get s :customGen))
|
|
1398
|
+
(kind (get s :kind))
|
|
1399
|
+
(cfg (get s :config)))
|
|
1400
|
+
(if (not (nullish? custom))
|
|
1401
|
+
custom
|
|
1402
|
+
(cond
|
|
1403
|
+
((or (= kind "any") (= kind "pred") (= kind "int-in")
|
|
1404
|
+
(= kind "double-in") (= kind "string-len") (= kind "enum"))
|
|
1405
|
+
(throw (new globalThis/Error (str "no generator available for predicate schema " (get s :name)))))
|
|
1406
|
+
((= kind "nilable")
|
|
1407
|
+
(let ((inner-gen (gen-from-spec (get cfg :inner))))
|
|
1408
|
+
(make-gen "nilable"
|
|
1409
|
+
(lambda (rng size)
|
|
1410
|
+
(if (= (rng-next-int rng 0 7) 0)
|
|
1411
|
+
nil
|
|
1412
|
+
(js-call inner-gen :sampleFn rng size))))))
|
|
1413
|
+
((= kind "and")
|
|
1414
|
+
(let* ((specs (get cfg :specs))
|
|
1415
|
+
(base (gen-from-spec (aref specs 0))))
|
|
1416
|
+
(if (= (js-length specs) 1)
|
|
1417
|
+
base
|
|
1418
|
+
(gen-such-that (lambda (v) (valid? s v)) base))))
|
|
1419
|
+
((= kind "or")
|
|
1420
|
+
(let ((clauses (get cfg :clauses)))
|
|
1421
|
+
(gen-one-of (js-call clauses :map (lambda (c) (gen-from-spec (get c :spec)))))))
|
|
1422
|
+
((= kind "keys")
|
|
1423
|
+
(let ((req (get cfg :req))
|
|
1424
|
+
(opt (get cfg :opt)))
|
|
1425
|
+
(let ((req-gens (js-call req :map (lambda (e) (js-object :key (get e :key) :gen (gen-from-spec (get e :spec))))))
|
|
1426
|
+
(opt-gens (js-call opt :map (lambda (e) (js-object :key (get e :key) :gen (gen-from-spec (get e :spec)))))))
|
|
1427
|
+
(make-gen "keys"
|
|
1428
|
+
(lambda (rng size)
|
|
1429
|
+
(let ((pairs (js-array))
|
|
1430
|
+
(i 0))
|
|
1431
|
+
(while (< i (js-length req-gens))
|
|
1432
|
+
(let ((entry (aref req-gens i)))
|
|
1433
|
+
(js-call pairs :push (get entry :key))
|
|
1434
|
+
(js-call pairs :push (js-call (get entry :gen) :sampleFn rng size)))
|
|
1435
|
+
(setq i (1+ i)))
|
|
1436
|
+
(setq i 0)
|
|
1437
|
+
(while (< i (js-length opt-gens))
|
|
1438
|
+
(when (= (rng-next-int rng 0 1) 1)
|
|
1439
|
+
(let ((entry (aref opt-gens i)))
|
|
1440
|
+
(js-call pairs :push (get entry :key))
|
|
1441
|
+
(js-call pairs :push (js-call (get entry :gen) :sampleFn rng size))))
|
|
1442
|
+
(setq i (1+ i)))
|
|
1443
|
+
(apply kv-map pairs)))))))
|
|
1444
|
+
((= kind "coll-of")
|
|
1445
|
+
(let* ((ig (gen-from-spec (get cfg :item)))
|
|
1446
|
+
(expected-kind (get cfg :kind))
|
|
1447
|
+
(vg (gen-vector ig (get cfg :minCount) (get cfg :maxCount) (get cfg :distinct))))
|
|
1448
|
+
(cond
|
|
1449
|
+
((= expected-kind "set") (gen-fmap (lambda (v) (apply persistentHashSet (to-js-array v))) vg))
|
|
1450
|
+
((= expected-kind "list") (gen-fmap (lambda (v) (apply persistentList (to-js-array v))) vg))
|
|
1451
|
+
(t vg))))
|
|
1452
|
+
((= kind "map-of")
|
|
1453
|
+
(gen-map (gen-from-spec (get cfg :key))
|
|
1454
|
+
(gen-from-spec (get cfg :val))
|
|
1455
|
+
(get cfg :minCount)
|
|
1456
|
+
(get cfg :maxCount)))
|
|
1457
|
+
((= kind "tuple")
|
|
1458
|
+
(apply gen-tuple (js-call (get cfg :items) :map gen-from-spec)))
|
|
1459
|
+
((= kind "cat")
|
|
1460
|
+
(let ((clauses (get cfg :clauses)))
|
|
1461
|
+
(make-gen "cat"
|
|
1462
|
+
(lambda (rng size)
|
|
1463
|
+
(let ((out (js-array))
|
|
1464
|
+
(i 0))
|
|
1465
|
+
(while (< i (js-length clauses))
|
|
1466
|
+
(let* ((sub (get (aref clauses i) :spec))
|
|
1467
|
+
(sg (gen-from-spec sub))
|
|
1468
|
+
(part (js-call sg :sampleFn rng size)))
|
|
1469
|
+
(if (regex-schema? sub)
|
|
1470
|
+
(let ((pa (to-js-array part)) (j 0))
|
|
1471
|
+
(while (< j (js-length pa))
|
|
1472
|
+
(js-call out :push (aref pa j))
|
|
1473
|
+
(setq j (1+ j))))
|
|
1474
|
+
(js-call out :push part)))
|
|
1475
|
+
(setq i (1+ i)))
|
|
1476
|
+
(js-call PersistentVector :from out))))))
|
|
1477
|
+
((= kind "alt")
|
|
1478
|
+
(let ((clauses (get cfg :clauses)))
|
|
1479
|
+
(gen-one-of (js-call clauses :map (lambda (c) (gen-from-spec (get c :spec)))))))
|
|
1480
|
+
((= kind "rep*")
|
|
1481
|
+
(gen-vector (gen-from-spec (get cfg :inner)) 0 8))
|
|
1482
|
+
((= kind "rep+")
|
|
1483
|
+
(gen-vector (gen-from-spec (get cfg :inner)) 1 8))
|
|
1484
|
+
((= kind "opt?")
|
|
1485
|
+
(let ((inner-g (gen-from-spec (get cfg :inner))))
|
|
1486
|
+
(make-gen "opt?"
|
|
1487
|
+
(lambda (rng size)
|
|
1488
|
+
(if (= (rng-next-int rng 0 1) 0)
|
|
1489
|
+
EMPTY_VECTOR
|
|
1490
|
+
(let ((v (js-call inner-g :sampleFn rng size)))
|
|
1491
|
+
(if (regex-schema? (get cfg :inner)) v (persistentVector v))))))))
|
|
1492
|
+
(t
|
|
1493
|
+
(throw (new globalThis/Error (str "cannot synthesize generator for schema kind " kind))))))))
|
|
1494
|
+
|
|
1495
|
+
(defun gen-generate (schema-or-gen &optional options)
|
|
1496
|
+
(let* ((g (ensure-gen schema-or-gen))
|
|
1497
|
+
(seed (read-opt options "seed" nil nil 42))
|
|
1498
|
+
(size (read-opt options "size" nil nil 20))
|
|
1499
|
+
(rng (make-rng seed)))
|
|
1500
|
+
(js-call g :sampleFn rng size)))
|
|
1501
|
+
|
|
1502
|
+
(defun gen-sample (schema-or-gen &optional options)
|
|
1503
|
+
(let* ((g (ensure-gen schema-or-gen))
|
|
1504
|
+
(count (read-opt options "count" nil nil 10))
|
|
1505
|
+
(seed (read-opt options "seed" nil nil 42))
|
|
1506
|
+
(size (read-opt options "size" nil nil 20))
|
|
1507
|
+
(rng (make-rng seed))
|
|
1508
|
+
(items (js-array))
|
|
1509
|
+
(i 0))
|
|
1510
|
+
(while (< i count)
|
|
1511
|
+
(let ((step-size (Math/min size (+ 1 (Math/floor (/ (* i size) (Math/max 1 count)))))))
|
|
1512
|
+
(js-call items :push (js-call g :sampleFn rng step-size)))
|
|
1513
|
+
(setq i (1+ i)))
|
|
1514
|
+
(js-call PersistentVector :from items)))
|
|
1515
|
+
|
|
1516
|
+
;; ============================================================================
|
|
1517
|
+
;; Structural Counterexample Shrinking & Property Check
|
|
1518
|
+
;; ============================================================================
|
|
1519
|
+
|
|
1520
|
+
(defun shrink-int-toward (val target)
|
|
1521
|
+
(let ((candidates (js-array)))
|
|
1522
|
+
(unless (= val target)
|
|
1523
|
+
(js-call candidates :push target)
|
|
1524
|
+
(let ((diff (- val target)))
|
|
1525
|
+
(let ((step (if (> diff 0) (Math/floor (/ diff 2)) (Math/ceil (/ diff 2)))))
|
|
1526
|
+
(while (not (= step 0))
|
|
1527
|
+
(let ((cand (+ target step)))
|
|
1528
|
+
(unless (or (= cand val) (= cand target))
|
|
1529
|
+
(js-call candidates :push cand)))
|
|
1530
|
+
(setq step (if (> step 0) (Math/floor (/ step 2)) (Math/ceil (/ step 2)))))))
|
|
1531
|
+
(let ((one-closer (if (> val target) (1- val) (1+ val))))
|
|
1532
|
+
(unless (= one-closer target)
|
|
1533
|
+
(js-call candidates :push one-closer))))
|
|
1534
|
+
candidates))
|
|
1535
|
+
|
|
1536
|
+
(defun shrink-string-value (text min-len)
|
|
1537
|
+
(let ((candidates (js-array))
|
|
1538
|
+
(len (js-length text)))
|
|
1539
|
+
(when (> len min-len)
|
|
1540
|
+
(js-call candidates :push (js-call text :slice 0 min-len))
|
|
1541
|
+
(let ((half (Math/max min-len (Math/floor (/ (+ min-len len) 2)))))
|
|
1542
|
+
(when (and (> half min-len) (< half len))
|
|
1543
|
+
(js-call candidates :push (js-call text :slice 0 half))))
|
|
1544
|
+
(when (> len (1+ min-len))
|
|
1545
|
+
(js-call candidates :push (js-call text :slice 0 (1- len)))
|
|
1546
|
+
(js-call candidates :push (js-call text :slice 1))))
|
|
1547
|
+
candidates))
|
|
1548
|
+
|
|
1549
|
+
(defun wrap-sequential-like (original arr)
|
|
1550
|
+
(cond
|
|
1551
|
+
((Collection/isSet original) (apply persistentHashSet arr))
|
|
1552
|
+
((Collection/isList original) (apply persistentList arr))
|
|
1553
|
+
(t (js-call PersistentVector :from arr))))
|
|
1554
|
+
|
|
1555
|
+
(defun shrink-sequence-items (original arr item-spec min-c)
|
|
1556
|
+
(let ((candidates (js-array))
|
|
1557
|
+
(n (js-length arr)))
|
|
1558
|
+
(unless (= n 0)
|
|
1559
|
+
;; 1. Drop to minimum count first
|
|
1560
|
+
(when (> n min-c)
|
|
1561
|
+
(js-call candidates :push (wrap-sequential-like original (js-call arr :slice 0 min-c)))
|
|
1562
|
+
;; Try dropping half chunks
|
|
1563
|
+
(let ((half (Math/max min-c (Math/floor (/ n 2)))))
|
|
1564
|
+
(when (< half n)
|
|
1565
|
+
(js-call candidates :push (wrap-sequential-like original (js-call arr :slice 0 half)))
|
|
1566
|
+
(let ((second-half (js-call arr :slice (- n half))))
|
|
1567
|
+
(when (>= (js-length second-half) min-c)
|
|
1568
|
+
(js-call candidates :push (wrap-sequential-like original second-half))))))
|
|
1569
|
+
;; Try removing individual elements
|
|
1570
|
+
(let ((i 0))
|
|
1571
|
+
(while (< i n)
|
|
1572
|
+
(let ((without (js-array))
|
|
1573
|
+
(j 0))
|
|
1574
|
+
(while (< j n)
|
|
1575
|
+
(unless (= j i) (js-call without :push (aref arr j)))
|
|
1576
|
+
(setq j (1+ j)))
|
|
1577
|
+
(when (>= (js-length without) min-c)
|
|
1578
|
+
(js-call candidates :push (wrap-sequential-like original without))))
|
|
1579
|
+
(setq i (1+ i)))))
|
|
1580
|
+
;; 2. Shrink each element in-place
|
|
1581
|
+
(when item-spec
|
|
1582
|
+
(let ((i 0))
|
|
1583
|
+
(while (< i n)
|
|
1584
|
+
(let ((el-shrinks (shrink-value item-spec (aref arr i)))
|
|
1585
|
+
(si 0))
|
|
1586
|
+
(while (< si (js-length el-shrinks))
|
|
1587
|
+
(let ((copy (js-call arr :slice)))
|
|
1588
|
+
(put copy i (aref el-shrinks si))
|
|
1589
|
+
(js-call candidates :push (wrap-sequential-like original copy)))
|
|
1590
|
+
(setq si (1+ si))))
|
|
1591
|
+
(setq i (1+ i))))))
|
|
1592
|
+
candidates))
|
|
1593
|
+
|
|
1594
|
+
(defun shrink-value (schema value)
|
|
1595
|
+
(let* ((s (resolve-spec schema))
|
|
1596
|
+
(kind (get s :kind))
|
|
1597
|
+
(cfg (get s :config))
|
|
1598
|
+
(candidates (js-array)))
|
|
1599
|
+
(cond
|
|
1600
|
+
((= kind "nilable")
|
|
1601
|
+
(unless (nil? value)
|
|
1602
|
+
(js-call candidates :push nil)
|
|
1603
|
+
(let ((sub (shrink-value (get cfg :inner) value)) (i 0))
|
|
1604
|
+
(while (< i (js-length sub))
|
|
1605
|
+
(js-call candidates :push (aref sub i))
|
|
1606
|
+
(setq i (1+ i))))))
|
|
1607
|
+
((= kind "int-in")
|
|
1608
|
+
(let* ((lo (get cfg :min))
|
|
1609
|
+
(hi (get cfg :max))
|
|
1610
|
+
(target (if (<= lo 0) (if (>= hi 0) 0 hi) lo)))
|
|
1611
|
+
(setq candidates (shrink-int-toward value target))))
|
|
1612
|
+
((= kind "double-in")
|
|
1613
|
+
(when (and (= (value-type value) "number") (Number/isFinite value))
|
|
1614
|
+
(let* ((lo (get cfg :min))
|
|
1615
|
+
(hi (get cfg :max))
|
|
1616
|
+
(target (if (<= lo 0) (if (>= hi 0) 0 hi) lo)))
|
|
1617
|
+
(unless (= value target)
|
|
1618
|
+
(js-call candidates :push target)
|
|
1619
|
+
(let ((tr (Math/trunc value)))
|
|
1620
|
+
(when (and (not (= tr value)) (>= tr lo) (<= tr hi))
|
|
1621
|
+
(js-call candidates :push tr)))
|
|
1622
|
+
(let ((mid (/ (+ value target) 2)))
|
|
1623
|
+
(when (and (not (= mid value)) (>= mid lo) (<= mid hi))
|
|
1624
|
+
(js-call candidates :push mid)))))))
|
|
1625
|
+
((= kind "string-len")
|
|
1626
|
+
(when (= (value-type value) "string")
|
|
1627
|
+
(setq candidates (shrink-string-value value (get cfg :min)))))
|
|
1628
|
+
((= kind "pred")
|
|
1629
|
+
(let ((name (get s :name)))
|
|
1630
|
+
(cond
|
|
1631
|
+
((Number/isSafeInteger value)
|
|
1632
|
+
(setq candidates (shrink-int-toward value 0)))
|
|
1633
|
+
((= (value-type value) "string")
|
|
1634
|
+
(setq candidates (shrink-string-value value 0)))
|
|
1635
|
+
((= name "boolean?")
|
|
1636
|
+
(when (eq value t) (js-call candidates :push false))))))
|
|
1637
|
+
((= kind "coll-of")
|
|
1638
|
+
(setq candidates (shrink-sequence-items value (to-js-array value) (get cfg :item) (get cfg :minCount))))
|
|
1639
|
+
((or (= kind "rep*") (= kind "rep+"))
|
|
1640
|
+
(setq candidates (shrink-sequence-items value (to-js-array value) (get cfg :inner) (if (= kind "rep+") 1 0))))
|
|
1641
|
+
((= kind "tuple")
|
|
1642
|
+
(let ((items (to-js-array value))
|
|
1643
|
+
(specs (get cfg :items))
|
|
1644
|
+
(i 0))
|
|
1645
|
+
(while (< i (js-length specs))
|
|
1646
|
+
(let ((sub (shrink-value (aref specs i) (aref items i))) (j 0))
|
|
1647
|
+
(while (< j (js-length sub))
|
|
1648
|
+
(let ((copy (js-call items :slice)))
|
|
1649
|
+
(put copy i (aref sub j))
|
|
1650
|
+
(js-call candidates :push (js-call PersistentVector :from copy)))
|
|
1651
|
+
(setq j (1+ j))))
|
|
1652
|
+
(setq i (1+ i)))))
|
|
1653
|
+
((= kind "keys")
|
|
1654
|
+
(when (Collection/isMap value)
|
|
1655
|
+
(let ((req (get cfg :req))
|
|
1656
|
+
(opt (get cfg :opt))
|
|
1657
|
+
(i 0))
|
|
1658
|
+
;; Try dropping optional keys first
|
|
1659
|
+
(while (< i (js-length opt))
|
|
1660
|
+
(let ((k (get (aref opt i) :key)))
|
|
1661
|
+
(when (Collection/contains value k)
|
|
1662
|
+
(js-call candidates :push (Collection/dissoc value k))))
|
|
1663
|
+
(setq i (1+ i)))
|
|
1664
|
+
;; Try shrinking each declared key's value
|
|
1665
|
+
(let ((all-e (js-call req :concat opt)))
|
|
1666
|
+
(setq i 0)
|
|
1667
|
+
(while (< i (js-length all-e))
|
|
1668
|
+
(let* ((e (aref all-e i))
|
|
1669
|
+
(k (get e :key)))
|
|
1670
|
+
(when (Collection/contains value k)
|
|
1671
|
+
(let ((sub (shrink-value (get e :spec) (Collection/get value k)))
|
|
1672
|
+
(j 0))
|
|
1673
|
+
(while (< j (js-length sub))
|
|
1674
|
+
(js-call candidates :push (Collection/assoc value k (aref sub j)))
|
|
1675
|
+
(setq j (1+ j))))))
|
|
1676
|
+
(setq i (1+ i)))))))
|
|
1677
|
+
((= kind "map-of")
|
|
1678
|
+
(when (Collection/isMap value)
|
|
1679
|
+
(let ((min-c (get cfg :minCount))
|
|
1680
|
+
(ks (get cfg :key))
|
|
1681
|
+
(vs (get cfg :val))
|
|
1682
|
+
(entries (js-array)))
|
|
1683
|
+
(Collection/reduceKV value (lambda (_acc k v) (js-call entries :push (js-array k v)) nil) nil)
|
|
1684
|
+
(when (> (Collection/count value) min-c)
|
|
1685
|
+
(let ((i 0))
|
|
1686
|
+
(while (< i (js-length entries))
|
|
1687
|
+
(js-call candidates :push (Collection/dissoc value (aref (aref entries i) 0)))
|
|
1688
|
+
(setq i (1+ i)))))
|
|
1689
|
+
(let ((i 0))
|
|
1690
|
+
(while (< i (js-length entries))
|
|
1691
|
+
(let* ((pair (aref entries i))
|
|
1692
|
+
(k (aref pair 0))
|
|
1693
|
+
(v (aref pair 1))
|
|
1694
|
+
(v-shrinks (shrink-value vs v))
|
|
1695
|
+
(j 0))
|
|
1696
|
+
(while (< j (js-length v-shrinks))
|
|
1697
|
+
(js-call candidates :push (Collection/assoc value k (aref v-shrinks j)))
|
|
1698
|
+
(setq j (1+ j)))
|
|
1699
|
+
(let ((k-shrinks (shrink-value ks k)) (kj 0))
|
|
1700
|
+
(while (< kj (js-length k-shrinks))
|
|
1701
|
+
(let ((nk (aref k-shrinks kj)))
|
|
1702
|
+
(unless (Collection/contains value nk)
|
|
1703
|
+
(js-call candidates :push (Collection/assoc (Collection/dissoc value k) nk v))))
|
|
1704
|
+
(setq kj (1+ kj)))))
|
|
1705
|
+
(setq i (1+ i)))))))
|
|
1706
|
+
((= kind "and")
|
|
1707
|
+
(let ((specs (get cfg :specs)))
|
|
1708
|
+
(when (> (js-length specs) 0)
|
|
1709
|
+
(let ((sub (shrink-value (aref specs 0) value)) (i 0))
|
|
1710
|
+
(while (< i (js-length sub))
|
|
1711
|
+
(let ((c (aref sub i)))
|
|
1712
|
+
(when (valid? s c) (js-call candidates :push c)))
|
|
1713
|
+
(setq i (1+ i)))))))
|
|
1714
|
+
((= kind "or")
|
|
1715
|
+
(let ((clauses (get cfg :clauses))
|
|
1716
|
+
(i 0))
|
|
1717
|
+
(while (< i (js-length clauses))
|
|
1718
|
+
(let ((sub-spec (get (aref clauses i) :spec)))
|
|
1719
|
+
(when (valid? sub-spec value)
|
|
1720
|
+
(let ((sub (shrink-value sub-spec value)) (j 0))
|
|
1721
|
+
(while (< j (js-length sub))
|
|
1722
|
+
(js-call candidates :push (aref sub j))
|
|
1723
|
+
(setq j (1+ j))))))
|
|
1724
|
+
(setq i (1+ i)))))
|
|
1725
|
+
((= kind "cat")
|
|
1726
|
+
(let ((arr (to-js-array value))
|
|
1727
|
+
(i 0))
|
|
1728
|
+
(while (< i (js-length arr))
|
|
1729
|
+
(let ((el (aref arr i)))
|
|
1730
|
+
(cond
|
|
1731
|
+
((Number/isSafeInteger el)
|
|
1732
|
+
(let ((sub (shrink-int-toward el 0)) (j 0))
|
|
1733
|
+
(while (< j (js-length sub))
|
|
1734
|
+
(let ((copy (js-call arr :slice)))
|
|
1735
|
+
(put copy i (aref sub j))
|
|
1736
|
+
(js-call candidates :push (js-call PersistentVector :from copy)))
|
|
1737
|
+
(setq j (1+ j)))))
|
|
1738
|
+
((= (value-type el) "string")
|
|
1739
|
+
(let ((sub (shrink-string-value el 0)) (j 0))
|
|
1740
|
+
(while (< j (js-length sub))
|
|
1741
|
+
(let ((copy (js-call arr :slice)))
|
|
1742
|
+
(put copy i (aref sub j))
|
|
1743
|
+
(js-call candidates :push (js-call PersistentVector :from copy)))
|
|
1744
|
+
(setq j (1+ j)))))))
|
|
1745
|
+
(setq i (1+ i)))))
|
|
1746
|
+
(t nil))
|
|
1747
|
+
candidates))
|
|
1748
|
+
|
|
1749
|
+
(defun eval-property (prop-fn candidate)
|
|
1750
|
+
(try
|
|
1751
|
+
(let ((res (funcall prop-fn candidate)))
|
|
1752
|
+
(js-object :passed (truthy? res) :error nil))
|
|
1753
|
+
(catch err
|
|
1754
|
+
(js-object :passed false :error (get err :message)))))
|
|
1755
|
+
|
|
1756
|
+
(defun shrink-counterexample (schema initial-fail initial-error prop-fn max-shrinks)
|
|
1757
|
+
(let ((current initial-fail)
|
|
1758
|
+
(current-error initial-error)
|
|
1759
|
+
(steps 0)
|
|
1760
|
+
(depth 0)
|
|
1761
|
+
(improved t))
|
|
1762
|
+
(while (and improved (< steps max-shrinks))
|
|
1763
|
+
(setq improved false)
|
|
1764
|
+
(let ((candidates (shrink-value schema current))
|
|
1765
|
+
(i 0))
|
|
1766
|
+
(while (and (not improved) (< i (js-length candidates)) (< steps max-shrinks))
|
|
1767
|
+
(let ((cand (aref candidates i)))
|
|
1768
|
+
(when (and (not (equalValues cand current)) (valid? schema cand))
|
|
1769
|
+
(setq steps (1+ steps))
|
|
1770
|
+
(let ((ev (eval-property prop-fn cand)))
|
|
1771
|
+
(unless (get ev :passed)
|
|
1772
|
+
(setq current cand)
|
|
1773
|
+
(setq current-error (get ev :error))
|
|
1774
|
+
(setq depth (1+ depth))
|
|
1775
|
+
(setq improved t)))))
|
|
1776
|
+
(setq i (1+ i)))))
|
|
1777
|
+
(kv-map
|
|
1778
|
+
KW_SMALLEST current
|
|
1779
|
+
KW_STEPS steps
|
|
1780
|
+
KW_DEPTH depth
|
|
1781
|
+
KW_ERROR current-error)))
|
|
1782
|
+
|
|
1783
|
+
(defun check-property (schema prop-fn &optional options)
|
|
1784
|
+
(unless (= (value-type prop-fn) "function")
|
|
1785
|
+
(throw (new globalThis/TypeError "check-property expects a property function")))
|
|
1786
|
+
(let* ((s (resolve-spec schema))
|
|
1787
|
+
(g (gen-from-spec s))
|
|
1788
|
+
(num-tests (read-opt options "num-tests" "numTests" nil 100))
|
|
1789
|
+
(raw-seed (read-opt options "seed" nil nil nil))
|
|
1790
|
+
(seed (if (nullish? raw-seed)
|
|
1791
|
+
(uint32 (+ (* (Date/now) 2654435761) (Math/floor (* (Math/random) 100000))))
|
|
1792
|
+
(uint32 raw-seed)))
|
|
1793
|
+
(max-size (read-opt options "max-size" "maxSize" nil 30))
|
|
1794
|
+
(max-shrinks (read-opt options "max-shrinks" "maxShrinks" nil 200))
|
|
1795
|
+
(rng (make-rng seed))
|
|
1796
|
+
(i 0)
|
|
1797
|
+
(failure nil))
|
|
1798
|
+
(while (and (nil? failure) (< i num-tests))
|
|
1799
|
+
(let* ((size (Math/min max-size (+ 1 (Math/floor (/ (* i max-size) (Math/max 1 num-tests))))))
|
|
1800
|
+
(sample (js-call g :sampleFn rng size))
|
|
1801
|
+
(ev (eval-property prop-fn sample)))
|
|
1802
|
+
(unless (get ev :passed)
|
|
1803
|
+
(setq failure (js-object :index (1+ i) :sample sample :error (get ev :error)))))
|
|
1804
|
+
(setq i (1+ i)))
|
|
1805
|
+
(if (nil? failure)
|
|
1806
|
+
(kv-map
|
|
1807
|
+
KW_OK t
|
|
1808
|
+
KW_SEED seed
|
|
1809
|
+
KW_NUM_TESTS num-tests)
|
|
1810
|
+
(let ((shrunk (shrink-counterexample s (get failure :sample) (get failure :error) prop-fn max-shrinks)))
|
|
1811
|
+
(kv-map
|
|
1812
|
+
KW_OK false
|
|
1813
|
+
KW_SEED seed
|
|
1814
|
+
KW_NUM_TESTS (get failure :index)
|
|
1815
|
+
KW_FAIL (get failure :sample)
|
|
1816
|
+
KW_SHRUNK shrunk)))))
|
|
1817
|
+
|
|
1818
|
+
(export
|
|
1819
|
+
INVALID
|
|
1820
|
+
Schema
|
|
1821
|
+
SchemaGen
|
|
1822
|
+
check-property
|
|
1823
|
+
conform
|
|
1824
|
+
def-spec
|
|
1825
|
+
explain
|
|
1826
|
+
explain-data
|
|
1827
|
+
explain-str
|
|
1828
|
+
fdef
|
|
1829
|
+
gen-bind
|
|
1830
|
+
gen-boolean
|
|
1831
|
+
gen-double
|
|
1832
|
+
gen-elements
|
|
1833
|
+
gen-fmap
|
|
1834
|
+
gen-from-spec
|
|
1835
|
+
gen-generate
|
|
1836
|
+
gen-int
|
|
1837
|
+
gen-keyword
|
|
1838
|
+
gen-map
|
|
1839
|
+
gen-one-of
|
|
1840
|
+
gen-return
|
|
1841
|
+
gen-sample
|
|
1842
|
+
gen-string
|
|
1843
|
+
gen-such-that
|
|
1844
|
+
gen-symbol
|
|
1845
|
+
gen-tuple
|
|
1846
|
+
gen-vector
|
|
1847
|
+
gen?
|
|
1848
|
+
get-spec
|
|
1849
|
+
instrument
|
|
1850
|
+
invalid?
|
|
1851
|
+
schema?
|
|
1852
|
+
shrink-value
|
|
1853
|
+
spec-alt
|
|
1854
|
+
spec-and
|
|
1855
|
+
spec-any
|
|
1856
|
+
spec-boolean
|
|
1857
|
+
spec-cat
|
|
1858
|
+
spec-coll-of
|
|
1859
|
+
spec-double
|
|
1860
|
+
spec-double-in
|
|
1861
|
+
spec-enum
|
|
1862
|
+
spec-fn
|
|
1863
|
+
spec-int
|
|
1864
|
+
spec-int-in
|
|
1865
|
+
spec-keys
|
|
1866
|
+
spec-keyword
|
|
1867
|
+
spec-list
|
|
1868
|
+
spec-map
|
|
1869
|
+
spec-map-of
|
|
1870
|
+
spec-nil
|
|
1871
|
+
spec-nilable
|
|
1872
|
+
spec-number
|
|
1873
|
+
spec-opt?
|
|
1874
|
+
spec-or
|
|
1875
|
+
spec-pred
|
|
1876
|
+
spec-rep+
|
|
1877
|
+
spec-rep*
|
|
1878
|
+
spec-set
|
|
1879
|
+
spec-string
|
|
1880
|
+
spec-string-len
|
|
1881
|
+
spec-symbol
|
|
1882
|
+
spec-tuple
|
|
1883
|
+
spec-vector
|
|
1884
|
+
unform
|
|
1885
|
+
valid?
|
|
1886
|
+
with-gen))
|