@johnhenry/mallory-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
// The `bytes` helper's core: byte strings (Uint8Array) in pane code and in Node. Pure: no DOM, no
|
|
2
|
+
// device, no imports, so the Worker can load it as one virtual module (src/bytes/index.js adds the
|
|
3
|
+
// serial framers and gives Node the same object).
|
|
4
|
+
//
|
|
5
|
+
// concat, slice, equals, compare byte strings as values (copies; inputs are never changed)
|
|
6
|
+
// toHex / fromHex, toBase64 / fromBase64, toUtf8 / fromUtf8 text both ways
|
|
7
|
+
// struct("u8 id, u16le len, f32le x[3]") a record codec: encode(object) and decode(bytes)
|
|
8
|
+
// bits("mode:3, flag:1, level:4") bit fields packed into 1 to 4 bytes
|
|
9
|
+
// crc8, crc16, crc32 (and crc) checksums, with the common variants by name
|
|
10
|
+
//
|
|
11
|
+
// A "byte string" is a Uint8Array; every function also takes an ArrayBuffer, any typed array or
|
|
12
|
+
// DataView (its bytes) or an array of numbers 0 to 255.
|
|
13
|
+
|
|
14
|
+
export const version = 1;
|
|
15
|
+
|
|
16
|
+
/** Anything byte-like as a Uint8Array (a Uint8Array itself is returned as is, not copied). Throws a sentence for anything else. */
|
|
17
|
+
export function asBytes(value, what = "bytes") {
|
|
18
|
+
if (value instanceof Uint8Array) return value;
|
|
19
|
+
if (value instanceof ArrayBuffer || (typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer)) return new Uint8Array(value);
|
|
20
|
+
if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
for (const n of value) if (!Number.isInteger(n) || n < 0 || n > 255) throw new TypeError(`${what}: an array of bytes holds whole numbers from 0 to 255, not ${JSON.stringify(n)}.`);
|
|
23
|
+
return Uint8Array.from(value);
|
|
24
|
+
}
|
|
25
|
+
throw new TypeError(`${what}: expected a Uint8Array, an ArrayBuffer or an array of numbers 0 to 255.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ── values ─────────────────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
/** One new Uint8Array of the parts, in order. */
|
|
31
|
+
export function concat(...parts) {
|
|
32
|
+
const list = (parts.length === 1 && Array.isArray(parts[0]) && parts[0].some((p) => typeof p !== "number") ? parts[0] : parts).map((p) => asBytes(p, "concat"));
|
|
33
|
+
const out = new Uint8Array(list.reduce((n, p) => n + p.length, 0));
|
|
34
|
+
let at = 0;
|
|
35
|
+
for (const p of list) {
|
|
36
|
+
out.set(p, at);
|
|
37
|
+
at += p.length;
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A copy of the bytes from `start` up to `end` (negative counts from the end), like Array slice. */
|
|
43
|
+
export function slice(value, start = 0, end) {
|
|
44
|
+
return asBytes(value, "slice").slice(start, end);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Do the two hold the same bytes? */
|
|
48
|
+
export function equals(a, b) {
|
|
49
|
+
const x = asBytes(a, "equals");
|
|
50
|
+
const y = asBytes(b, "equals");
|
|
51
|
+
if (x.length !== y.length) return false;
|
|
52
|
+
for (let i = 0; i < x.length; i++) if (x[i] !== y[i]) return false;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** -1, 0 or 1: the first differing byte decides, then the shorter one is first. */
|
|
57
|
+
export function compare(a, b) {
|
|
58
|
+
const x = asBytes(a, "compare");
|
|
59
|
+
const y = asBytes(b, "compare");
|
|
60
|
+
const n = Math.min(x.length, y.length);
|
|
61
|
+
for (let i = 0; i < n; i++) if (x[i] !== y[i]) return x[i] < y[i] ? -1 : 1;
|
|
62
|
+
return x.length === y.length ? 0 : x.length < y.length ? -1 : 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── text ───────────────────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
/** "0d0a" -> Uint8Array. Spaces, commas, colons, dashes and 0x prefixes between the pairs are fine. */
|
|
68
|
+
export function fromHex(text) {
|
|
69
|
+
const s = String(text ?? "").replace(/0x/gi, "").replace(/[\s,:-]/g, "");
|
|
70
|
+
if (!/^[0-9a-f]*$/i.test(s) || s.length % 2) throw new Error("hex is pairs of digits, like 0d0a");
|
|
71
|
+
const out = new Uint8Array(s.length / 2);
|
|
72
|
+
for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Lower-case hex; `{ separator: " " }` goes between the bytes, `{ upper: true }` upper-cases. */
|
|
77
|
+
export function toHex(value, { separator = "", upper = false } = {}) {
|
|
78
|
+
const text = [...asBytes(value, "toHex")].map((b) => b.toString(16).padStart(2, "0")).join(separator);
|
|
79
|
+
return upper ? text.toUpperCase() : text;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
83
|
+
const B64_INDEX = Object.fromEntries([...B64].map((c, i) => [c, i]));
|
|
84
|
+
|
|
85
|
+
/** Base64 (RFC 4648); `{ url: true }` is the URL-safe alphabet, `{ pad: false }` leaves off the "=". */
|
|
86
|
+
export function toBase64(value, { url = false, pad = true } = {}) {
|
|
87
|
+
const b = asBytes(value, "toBase64");
|
|
88
|
+
let out = "";
|
|
89
|
+
for (let i = 0; i < b.length; i += 3) {
|
|
90
|
+
const n = (b[i] << 16) | ((b[i + 1] ?? 0) << 8) | (b[i + 2] ?? 0);
|
|
91
|
+
out += B64[(n >> 18) & 63] + B64[(n >> 12) & 63] + (i + 1 < b.length ? B64[(n >> 6) & 63] : "=") + (i + 2 < b.length ? B64[n & 63] : "=");
|
|
92
|
+
}
|
|
93
|
+
if (url) out = out.replace(/\+/g, "-").replace(/\//g, "_");
|
|
94
|
+
return pad ? out : out.replace(/=+$/, "");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Base64 or base64url, with or without "=" and with whitespace anywhere, to bytes. */
|
|
98
|
+
export function fromBase64(text) {
|
|
99
|
+
const s = String(text ?? "").replace(/\s/g, "").replace(/-/g, "+").replace(/_/g, "/").replace(/=+$/, "");
|
|
100
|
+
if (/[^A-Za-z0-9+/]/.test(s)) throw new Error("base64 uses the letters, digits, + and / (or - and _)");
|
|
101
|
+
if (s.length % 4 === 1) throw new Error("that is not complete base64 (a stray character at the end)");
|
|
102
|
+
const out = new Uint8Array(Math.floor((s.length * 3) / 4));
|
|
103
|
+
let at = 0;
|
|
104
|
+
for (let i = 0; i < s.length; i += 4) {
|
|
105
|
+
const n = (B64_INDEX[s[i]] << 18) | (B64_INDEX[s[i + 1]] << 12) | ((B64_INDEX[s[i + 2]] ?? 0) << 6) | (B64_INDEX[s[i + 3]] ?? 0);
|
|
106
|
+
out[at++] = (n >> 16) & 255;
|
|
107
|
+
if (i + 2 < s.length) out[at++] = (n >> 8) & 255;
|
|
108
|
+
if (i + 3 < s.length) out[at++] = n & 255;
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A string as UTF-8 bytes. */
|
|
114
|
+
export function fromUtf8(text) {
|
|
115
|
+
return new TextEncoder().encode(String(text ?? ""));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** UTF-8 bytes as a string; bytes that are not UTF-8 become U+FFFD, or throw with `{ fatal: true }`. */
|
|
119
|
+
export function toUtf8(value, { fatal = false } = {}) {
|
|
120
|
+
return new TextDecoder("utf-8", { fatal }).decode(asBytes(value, "toUtf8"));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ── struct ─────────────────────────────────────────────────────────────────────
|
|
124
|
+
|
|
125
|
+
const TYPES = {
|
|
126
|
+
u8: { size: 1, get: (v, o) => v.getUint8(o), set: (v, o, x) => v.setUint8(o, x), min: 0, max: 255 },
|
|
127
|
+
i8: { size: 1, get: (v, o) => v.getInt8(o), set: (v, o, x) => v.setInt8(o, x), min: -128, max: 127 },
|
|
128
|
+
u16: { size: 2, get: (v, o, le) => v.getUint16(o, le), set: (v, o, x, le) => v.setUint16(o, x, le), min: 0, max: 65535, endian: true },
|
|
129
|
+
i16: { size: 2, get: (v, o, le) => v.getInt16(o, le), set: (v, o, x, le) => v.setInt16(o, x, le), min: -32768, max: 32767, endian: true },
|
|
130
|
+
u32: { size: 4, get: (v, o, le) => v.getUint32(o, le), set: (v, o, x, le) => v.setUint32(o, x, le), min: 0, max: 4294967295, endian: true },
|
|
131
|
+
i32: { size: 4, get: (v, o, le) => v.getInt32(o, le), set: (v, o, x, le) => v.setInt32(o, x, le), min: -2147483648, max: 2147483647, endian: true },
|
|
132
|
+
u64: { size: 8, get: (v, o, le) => v.getBigUint64(o, le), set: (v, o, x, le) => v.setBigUint64(o, x, le), big: true, min: 0n, max: 2n ** 64n - 1n, endian: true },
|
|
133
|
+
i64: { size: 8, get: (v, o, le) => v.getBigInt64(o, le), set: (v, o, x, le) => v.setBigInt64(o, x, le), big: true, min: -(2n ** 63n), max: 2n ** 63n - 1n, endian: true },
|
|
134
|
+
f32: { size: 4, get: (v, o, le) => v.getFloat32(o, le), set: (v, o, x, le) => v.setFloat32(o, x, le), float: true, endian: true },
|
|
135
|
+
f64: { size: 8, get: (v, o, le) => v.getFloat64(o, le), set: (v, o, x, le) => v.setFloat64(o, x, le), float: true, endian: true },
|
|
136
|
+
};
|
|
137
|
+
export const STRUCT_TYPES = Object.freeze(Object.keys(TYPES).flatMap((t) => (TYPES[t].endian ? [`${t}le`, `${t}be`] : [t])));
|
|
138
|
+
const MAX_ARRAY = 65536;
|
|
139
|
+
|
|
140
|
+
function parseStruct(spec) {
|
|
141
|
+
if (typeof spec !== "string" || !spec.trim()) throw new Error('struct: give the fields as text, like "u8 id, u16le len, f32le x[3]".');
|
|
142
|
+
const fields = [];
|
|
143
|
+
const seen = new Set();
|
|
144
|
+
let offset = 0;
|
|
145
|
+
for (const part of spec.split(",")) {
|
|
146
|
+
const text = part.trim();
|
|
147
|
+
if (!text) throw new Error(`struct: an empty field in "${spec}" (a stray comma?).`);
|
|
148
|
+
const m = /^([a-z0-9]+)\s+([A-Za-z_$][\w$]*)\s*(?:\[\s*(\d+)\s*\])?$/.exec(text);
|
|
149
|
+
if (!m) throw new Error(`struct: can't read "${text}"; a field is a type and a name, like "u16le len" or "f32le x[3]".`);
|
|
150
|
+
const [, typeName, name, count] = m;
|
|
151
|
+
const em = /^(.+?)(le|be)?$/.exec(typeName);
|
|
152
|
+
const type = TYPES[typeName] ?? TYPES[em[1]];
|
|
153
|
+
const known = TYPES[typeName] ? !type.endian : Boolean(type?.endian && em[2]);
|
|
154
|
+
if (!type || !known) {
|
|
155
|
+
const hint = type?.endian && !em[2] ? ` (${typeName} needs an order: ${typeName}le or ${typeName}be)` : `; the types are ${STRUCT_TYPES.join(", ")}`;
|
|
156
|
+
throw new Error(`struct: "${typeName}" isn't a field type${hint}.`);
|
|
157
|
+
}
|
|
158
|
+
if (seen.has(name)) throw new Error(`struct: the name "${name}" is used twice.`);
|
|
159
|
+
seen.add(name);
|
|
160
|
+
const length = count === undefined ? undefined : Number(count);
|
|
161
|
+
if (length !== undefined && (length < 1 || length > MAX_ARRAY)) throw new Error(`struct: ${name}[${count}] should hold 1 to ${MAX_ARRAY} items.`);
|
|
162
|
+
fields.push({ name, type: typeName, spec: type, little: em[2] === "le", length, offset });
|
|
163
|
+
offset += type.size * (length ?? 1);
|
|
164
|
+
}
|
|
165
|
+
return { fields, size: offset };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function checkValue(field, x) {
|
|
169
|
+
const { spec, name } = field;
|
|
170
|
+
if (spec.big) {
|
|
171
|
+
let v = x;
|
|
172
|
+
if (typeof v === "number" && Number.isSafeInteger(v)) v = BigInt(v);
|
|
173
|
+
if (typeof v !== "bigint") throw new TypeError(`struct: ${name} (${field.type}) takes a BigInt or a whole number, not ${typeof x}.`);
|
|
174
|
+
if (v < spec.min || v > spec.max) throw new RangeError(`struct: ${name} is ${v}, outside ${field.type} (${spec.min} to ${spec.max}).`);
|
|
175
|
+
return v;
|
|
176
|
+
}
|
|
177
|
+
if (typeof x !== "number" || Number.isNaN(x) && !spec.float) throw new TypeError(`struct: ${name} (${field.type}) takes a number, not ${typeof x === "number" ? "NaN" : typeof x}.`);
|
|
178
|
+
if (!spec.float) {
|
|
179
|
+
if (!Number.isInteger(x)) throw new RangeError(`struct: ${name} is ${x}, and ${field.type} holds whole numbers.`);
|
|
180
|
+
if (x < spec.min || x > spec.max) throw new RangeError(`struct: ${name} is ${x}, outside ${field.type} (${spec.min} to ${spec.max}).`);
|
|
181
|
+
}
|
|
182
|
+
return x;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A codec for a record of fixed-size fields, laid out in order with no padding:
|
|
187
|
+
* `struct("u8 id, u16le len, f32le x[3]")`. Types: u8 i8, u16 i16 u32 i32 u64 i64 f32 f64 (the
|
|
188
|
+
* ones over a byte end in `le` or `be`), `name[n]` for an array. 64-bit fields are BigInts.
|
|
189
|
+
* `size` is the bytes a record takes; `encode(object)` -> Uint8Array; `decode(bytes, offset = 0)`
|
|
190
|
+
* -> object (extra bytes after the record are left alone; too few throw).
|
|
191
|
+
*/
|
|
192
|
+
export function struct(spec) {
|
|
193
|
+
const { fields, size } = parseStruct(spec);
|
|
194
|
+
const names = fields.map((f) => f.name);
|
|
195
|
+
return Object.freeze({
|
|
196
|
+
spec: String(spec),
|
|
197
|
+
size,
|
|
198
|
+
fields: Object.freeze(fields.map((f) => ({ name: f.name, type: f.type, ...(f.length ? { length: f.length } : {}), offset: f.offset }))),
|
|
199
|
+
encode(object) {
|
|
200
|
+
if (!object || typeof object !== "object") throw new TypeError("struct: encode takes an object with a value for each field.");
|
|
201
|
+
const out = new Uint8Array(size);
|
|
202
|
+
const view = new DataView(out.buffer);
|
|
203
|
+
for (const f of fields) {
|
|
204
|
+
if (!(f.name in object)) throw new TypeError(`struct: the object has no "${f.name}" (the fields are ${names.join(", ")}).`);
|
|
205
|
+
const value = object[f.name];
|
|
206
|
+
if (f.length === undefined) f.spec.set(view, f.offset, checkValue(f, value), f.little);
|
|
207
|
+
else {
|
|
208
|
+
if (value == null || typeof value.length !== "number" || value.length !== f.length) throw new TypeError(`struct: ${f.name} takes ${f.length} values${value?.length !== undefined ? `, not ${value.length}` : ""}.`);
|
|
209
|
+
for (let i = 0; i < f.length; i++) f.spec.set(view, f.offset + i * f.spec.size, checkValue({ ...f, name: `${f.name}[${i}]` }, value[i]), f.little);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
},
|
|
214
|
+
decode(value, offset = 0) {
|
|
215
|
+
const b = asBytes(value, "struct.decode");
|
|
216
|
+
if (!Number.isInteger(offset) || offset < 0) throw new RangeError("struct: offset is a whole number from 0.");
|
|
217
|
+
if (b.length - offset < size) {
|
|
218
|
+
const have = Math.max(0, b.length - offset);
|
|
219
|
+
const f = fields.find((x) => have >= x.offset && have < x.offset + x.spec.size * (x.length ?? 1));
|
|
220
|
+
const n = f?.length ? Math.floor((have - f.offset) / f.spec.size) : 0;
|
|
221
|
+
const inside = f ? ` It ends inside ${f.name}${f.length ? `[${n}]` : ""} (bytes ${f.offset + n * f.spec.size} to ${f.offset + (n + 1) * f.spec.size - 1}).` : "";
|
|
222
|
+
throw new RangeError(`struct: a record is ${size} bytes and only ${have} are there.${inside}`);
|
|
223
|
+
}
|
|
224
|
+
const view = new DataView(b.buffer, b.byteOffset + offset, size);
|
|
225
|
+
const out = {};
|
|
226
|
+
for (const f of fields) {
|
|
227
|
+
if (f.length === undefined) out[f.name] = f.spec.get(view, f.offset, f.little);
|
|
228
|
+
else out[f.name] = Array.from({ length: f.length }, (_, i) => f.spec.get(view, f.offset + i * f.spec.size, f.little));
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ── bit fields ─────────────────────────────────────────────────────────────────
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Bit fields in 1 to 32 bits: `bits("mode:3, flag:1, level:4")` lists the fields from the top bit
|
|
239
|
+
* down (the first is the most significant); `{ order: "lsb" }` starts from the bottom bit. A name
|
|
240
|
+
* of `_` is a gap. `pack(object)` -> number, `unpack(number)` -> object; `encode(object)` ->
|
|
241
|
+
* Uint8Array of `size` bytes (big endian, or little with `{ little: true }`) and `decode(bytes)`.
|
|
242
|
+
*/
|
|
243
|
+
export function bits(spec, { order = "msb", little = false } = {}) {
|
|
244
|
+
if (typeof spec !== "string" || !spec.trim()) throw new Error('bits: give the fields as text, like "mode:3, flag:1, level:4".');
|
|
245
|
+
if (order !== "msb" && order !== "lsb") throw new Error('bits: order is "msb" or "lsb".');
|
|
246
|
+
const list = [];
|
|
247
|
+
const seen = new Set();
|
|
248
|
+
let total = 0;
|
|
249
|
+
for (const part of spec.split(",")) {
|
|
250
|
+
const m = /^\s*([A-Za-z_$][\w$]*)\s*:\s*(\d+)\s*$/.exec(part);
|
|
251
|
+
if (!m) throw new Error(`bits: can't read "${part.trim()}"; a field is a name, a colon and a width, like "mode:3".`);
|
|
252
|
+
const width = Number(m[2]);
|
|
253
|
+
if (width < 1 || width > 32) throw new Error(`bits: ${m[1]} is ${width} bits wide; fields are 1 to 32.`);
|
|
254
|
+
if (m[1] !== "_" && seen.has(m[1])) throw new Error(`bits: the name "${m[1]}" is used twice.`);
|
|
255
|
+
seen.add(m[1]);
|
|
256
|
+
list.push({ name: m[1], width });
|
|
257
|
+
total += width;
|
|
258
|
+
}
|
|
259
|
+
if (total > 32) throw new Error(`bits: the fields take ${total} bits; the most is 32.`);
|
|
260
|
+
const size = Math.ceil(total / 8);
|
|
261
|
+
const span = size * 8; // the fields sit at the top of the bytes (msb) or the bottom (lsb)
|
|
262
|
+
let at = order === "msb" ? span : 0;
|
|
263
|
+
const placed = list.map((f) => {
|
|
264
|
+
if (order === "msb") at -= f.width;
|
|
265
|
+
const shift = at;
|
|
266
|
+
if (order === "lsb") at += f.width;
|
|
267
|
+
return { ...f, shift };
|
|
268
|
+
});
|
|
269
|
+
const mask = (w) => (w === 32 ? 0xffffffff : (1 << w) - 1);
|
|
270
|
+
const pack = (object) => {
|
|
271
|
+
let n = 0;
|
|
272
|
+
for (const f of placed) {
|
|
273
|
+
if (f.name === "_") continue;
|
|
274
|
+
const v = object?.[f.name] ?? 0;
|
|
275
|
+
if (typeof v === "boolean") n = (n | ((v ? 1 : 0) << f.shift)) >>> 0;
|
|
276
|
+
else {
|
|
277
|
+
if (!Number.isInteger(v) || v < 0 || v > mask(f.width)) throw new RangeError(`bits: ${f.name} is ${v}, and ${f.width} bits hold 0 to ${mask(f.width)}.`);
|
|
278
|
+
n = (n | (v * 2 ** f.shift)) >>> 0;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return n >>> 0;
|
|
282
|
+
};
|
|
283
|
+
const unpack = (number) => {
|
|
284
|
+
if (!Number.isInteger(number) || number < 0 || number > 0xffffffff) throw new RangeError("bits: unpack takes a whole number from 0 to 4294967295.");
|
|
285
|
+
const out = {};
|
|
286
|
+
for (const f of placed) if (f.name !== "_") out[f.name] = Math.floor(number / 2 ** f.shift) % 2 ** f.width;
|
|
287
|
+
return out;
|
|
288
|
+
};
|
|
289
|
+
return Object.freeze({
|
|
290
|
+
spec: String(spec),
|
|
291
|
+
bits: total,
|
|
292
|
+
size,
|
|
293
|
+
fields: Object.freeze(placed.filter((f) => f.name !== "_").map((f) => ({ name: f.name, width: f.width, shift: f.shift }))),
|
|
294
|
+
pack,
|
|
295
|
+
unpack,
|
|
296
|
+
encode(object) {
|
|
297
|
+
const n = pack(object);
|
|
298
|
+
const out = new Uint8Array(size);
|
|
299
|
+
for (let i = 0; i < size; i++) out[little ? i : size - 1 - i] = Math.floor(n / 2 ** (8 * i)) % 256;
|
|
300
|
+
return out;
|
|
301
|
+
},
|
|
302
|
+
decode(value) {
|
|
303
|
+
const b = asBytes(value, "bits.decode");
|
|
304
|
+
if (b.length < size) throw new RangeError(`bits: these fields are ${size} byte${size === 1 ? "" : "s"} and only ${b.length} are there.`);
|
|
305
|
+
let n = 0;
|
|
306
|
+
for (let i = 0; i < size; i++) n += b[little ? i : size - 1 - i] * 2 ** (8 * i);
|
|
307
|
+
return unpack(n);
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Bit `index` (0 is the lowest bit) of a number or of a byte string read as one big-endian number: 0 or 1. */
|
|
313
|
+
export function getBit(value, index) {
|
|
314
|
+
if (typeof value === "number") return Math.floor(value / 2 ** index) % 2;
|
|
315
|
+
const b = asBytes(value, "getBit");
|
|
316
|
+
return (b[b.length - 1 - (index >> 3)] >> (index & 7)) & 1;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ── checksums ──────────────────────────────────────────────────────────────────
|
|
320
|
+
|
|
321
|
+
// The usual CRC variants by name (the Rocksoft model: width, poly, init, reflect in and out, xor out).
|
|
322
|
+
// check = the CRC of the ASCII text "123456789", which the tests hold each one to.
|
|
323
|
+
export const CRC_ALGORITHMS = Object.freeze({
|
|
324
|
+
crc8: Object.freeze({ default: "smbus", smbus: { width: 8, poly: 0x07, init: 0x00, refIn: false, refOut: false, xorOut: 0x00, check: 0xf4 }, maxim: { width: 8, poly: 0x31, init: 0x00, refIn: true, refOut: true, xorOut: 0x00, check: 0xa1 }, cdma2000: { width: 8, poly: 0x9b, init: 0xff, refIn: false, refOut: false, xorOut: 0x00, check: 0xda } }),
|
|
325
|
+
crc16: Object.freeze({
|
|
326
|
+
default: "ccitt-false",
|
|
327
|
+
"ccitt-false": { width: 16, poly: 0x1021, init: 0xffff, refIn: false, refOut: false, xorOut: 0x0000, check: 0x29b1 },
|
|
328
|
+
xmodem: { width: 16, poly: 0x1021, init: 0x0000, refIn: false, refOut: false, xorOut: 0x0000, check: 0x31c3 },
|
|
329
|
+
kermit: { width: 16, poly: 0x1021, init: 0x0000, refIn: true, refOut: true, xorOut: 0x0000, check: 0x2189 },
|
|
330
|
+
modbus: { width: 16, poly: 0x8005, init: 0xffff, refIn: true, refOut: true, xorOut: 0x0000, check: 0x4b37 },
|
|
331
|
+
arc: { width: 16, poly: 0x8005, init: 0x0000, refIn: true, refOut: true, xorOut: 0x0000, check: 0xbb3d },
|
|
332
|
+
}),
|
|
333
|
+
crc32: Object.freeze({
|
|
334
|
+
default: "iso-hdlc",
|
|
335
|
+
"iso-hdlc": { width: 32, poly: 0x04c11db7, init: 0xffffffff, refIn: true, refOut: true, xorOut: 0xffffffff, check: 0xcbf43926 },
|
|
336
|
+
c: { width: 32, poly: 0x1edc6f41, init: 0xffffffff, refIn: true, refOut: true, xorOut: 0xffffffff, check: 0xe3069283 },
|
|
337
|
+
bzip2: { width: 32, poly: 0x04c11db7, init: 0xffffffff, refIn: false, refOut: false, xorOut: 0xffffffff, check: 0xfc891918 },
|
|
338
|
+
mpeg2: { width: 32, poly: 0x04c11db7, init: 0xffffffff, refIn: false, refOut: false, xorOut: 0x00000000, check: 0x0376e6e7 },
|
|
339
|
+
}),
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
const reflect = (n, width) => {
|
|
343
|
+
let r = 0;
|
|
344
|
+
for (let i = 0; i < width; i++) if (n & (1 << i)) r |= 1 << (width - 1 - i);
|
|
345
|
+
return r >>> 0;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** The CRC of `data` for explicit parameters `{ width (8, 16 or 32), poly, init, refIn, refOut, xorOut }`: an unsigned number. */
|
|
349
|
+
export function crc(data, params) {
|
|
350
|
+
const { width, poly, init = 0, refIn = false, refOut = false, xorOut = 0 } = params ?? {};
|
|
351
|
+
if (width !== 8 && width !== 16 && width !== 32) throw new Error("crc: width is 8, 16 or 32.");
|
|
352
|
+
const b = asBytes(data, "crc");
|
|
353
|
+
const top = width === 32 ? 0x80000000 : 1 << (width - 1);
|
|
354
|
+
const mask = width === 32 ? 0xffffffff : (1 << width) - 1;
|
|
355
|
+
let r = init >>> 0;
|
|
356
|
+
for (let i = 0; i < b.length; i++) {
|
|
357
|
+
r = (r ^ ((refIn ? reflect(b[i], 8) : b[i]) << (width - 8))) >>> 0;
|
|
358
|
+
for (let k = 0; k < 8; k++) r = ((r & top ? (r << 1) ^ poly : r << 1) & mask) >>> 0;
|
|
359
|
+
}
|
|
360
|
+
if (refOut) r = reflect(r, width);
|
|
361
|
+
return (r ^ xorOut) >>> 0;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function crcOf(family, data, algorithm) {
|
|
365
|
+
const table = CRC_ALGORITHMS[family];
|
|
366
|
+
const name = algorithm ?? table.default;
|
|
367
|
+
if (typeof name === "object" && name) return crc(data, { width: Number(family.slice(3)), ...name });
|
|
368
|
+
const params = name !== "default" && Object.hasOwn(table, name) ? table[name] : undefined;
|
|
369
|
+
if (!params) throw new Error(`${family}: no variant "${name}"; the variants are ${Object.keys(table).filter((k) => k !== "default").join(", ")}.`);
|
|
370
|
+
return crc(data, params);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** CRC-8 (default variant "smbus", polynomial 0x07); also "maxim", "cdma2000", or your own `{ poly, init, refIn, refOut, xorOut }`. */
|
|
374
|
+
export const crc8 = (data, algorithm) => crcOf("crc8", data, algorithm);
|
|
375
|
+
/** CRC-16 (default "ccitt-false"); also "xmodem", "kermit", "modbus", "arc", or your own parameters. */
|
|
376
|
+
export const crc16 = (data, algorithm) => crcOf("crc16", data, algorithm);
|
|
377
|
+
/** CRC-32 (default "iso-hdlc": zlib, PNG, Ethernet); also "c" (Castagnoli), "bzip2", "mpeg2", or your own parameters. */
|
|
378
|
+
export const crc32 = (data, algorithm) => crcOf("crc32", data, algorithm);
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The lines of a calc pane (src/panes/calc.js), read as text and nothing else: which are blank, comments, assignments, function
|
|
2
|
+
// definitions or plain expressions, and which names they assign. Pure and dependency-free: the page reads it for the pane's output
|
|
3
|
+
// ports (the names its lines assign) and the Worker's evaluator (src/calc/source.js) reads it to know what a line is.
|
|
4
|
+
//
|
|
5
|
+
// x = 3 a variable
|
|
6
|
+
// f(t) = t^2 + x a function (late binding: x is read when f is called)
|
|
7
|
+
// f(2) an expression, shown with its result
|
|
8
|
+
// simplify((x+1)^2 - x^2) a command (COMMANDS) on an expression
|
|
9
|
+
// # a comment and // a comment
|
|
10
|
+
// 3*4 -> k a store: the same as k = 3*4 (and k <- 3*4 )
|
|
11
|
+
|
|
12
|
+
export const LIMITS = Object.freeze({
|
|
13
|
+
lines: 64, // lines per pane
|
|
14
|
+
line: 500, // characters per line
|
|
15
|
+
params: 8, // parameters of a function
|
|
16
|
+
depth: 24, // nested calls of user functions in one line
|
|
17
|
+
span: 100_000, // the terms a sum or a product may add up
|
|
18
|
+
exponent: 4096, // the largest exact power
|
|
19
|
+
deadlineMs: 2_000, // the time one run may take
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/** The arithmetic modes of a pane: what each one does, as the pane's own words (the page shows `help`). */
|
|
23
|
+
export const MODES = Object.freeze({
|
|
24
|
+
float: { label: "float", help: "Ordinary decimal arithmetic, rounded to 12 digits so 0.1 + 0.2 shows 0.3. Names, functions and commands (simplify, diff, integrate, solve…) work here. The exact switch turns the numbers into fractions." },
|
|
25
|
+
units: { label: "units", help: "Numbers carry units. Write 5 km + 300 m, 5 m / 2 s, or 5 km in m to convert. Spaces separate the number, the unit and the operator; there are no parentheses. A name holds a plain number (the magnitude of what was stored)." },
|
|
26
|
+
interval: { label: "interval", help: "Every result is a range [low, high] that is guaranteed to hold the true value, rounded outward. sqrt(2) shows the two floats on either side of it. Division by a range holding zero is refused. Powers take a whole number 0 or more; sin, cos, tan, exp, ln, sqrt and abs work. A stored name holds the middle of the range." },
|
|
27
|
+
complex: { label: "complex", help: "Complex numbers with i as the imaginary unit: (3+4i)*(1-2i), sqrt(-4), exp(i*pi). A result with an imaginary part is shown but not stored in a name; a real one is." },
|
|
28
|
+
modulus: { label: "Z/nZ", help: "Whole-number arithmetic modulo n (set n below). 3 - 5 is 5 in Z/7Z, and 1/3 is the inverse of 3, when there is one. Powers take a literal whole exponent such as 2^10." },
|
|
29
|
+
});
|
|
30
|
+
export const MODE_NAMES = Object.freeze(Object.keys(MODES));
|
|
31
|
+
export const ANGLES = Object.freeze({ rad: "radians", deg: "degrees" });
|
|
32
|
+
/** The largest modulus: a product of two residues stays an exact double. */
|
|
33
|
+
export const MAX_MODULUS = 67_108_864;
|
|
34
|
+
|
|
35
|
+
/** The commands: a line (or the right side of an assignment) that is `command(expression, arguments…)`. */
|
|
36
|
+
export const COMMANDS = Object.freeze(["simplify", "expand", "factor", "diff", "integrate", "solve", "taylor", "limit"]);
|
|
37
|
+
|
|
38
|
+
// @johnhenry/math's FUNCTION_NAMES plus the keywords of its parser (test/calc-syntax.test.mjs fails if the library grows a name this misses).
|
|
39
|
+
const MATH_NAMES = "sin,cos,tan,exp,ln,sqrt,asin,acos,atan,sinh,cosh,tanh,cot,sec,csc,asinh,acosh,atanh,coth,sech,csch,acot,asec,acsc,acoth,asech,acsch,abs,log10,log2,cbrt,floor,ceil,round,sign,trunc,expm1,log1p,sigmoid,erf,relu,arcsin,arccos,arctan,arcsinh,arccosh,arctanh,arccot,arcsec,arccsc,arccoth,arcsech,arccsch,sgn,logistic,atan2,hypot,min,max,gcd,lcm,log,clamp,piecewise,sum,product".split(",");
|
|
40
|
+
export const RESERVED = Object.freeze(new Set(["pi", "e", ...MATH_NAMES, ...COMMANDS, "__proto__", "constructor", "prototype", "toString", "valueOf", "hasOwnProperty"]));
|
|
41
|
+
export const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
42
|
+
|
|
43
|
+
const RIGHT_STORE = /^(.+?)\s*->\s*([A-Za-z_][A-Za-z0-9_]*)\s*$/s;
|
|
44
|
+
const LEFT_STORE = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*<-\s*(.+)$/s;
|
|
45
|
+
const ASSIGN = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?:\(([^()]*)\))?\s*=(?!=)\s*(.*)$/s;
|
|
46
|
+
|
|
47
|
+
/** Why a name can't be assigned, or null. */
|
|
48
|
+
export function nameProblem(name) {
|
|
49
|
+
if (!NAME.test(name)) return `"${String(name).slice(0, 40)}" isn't a name: use letters, digits and _`;
|
|
50
|
+
if (RESERVED.has(name)) return `"${name}" is already something (a constant, a function or a command)`;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What a line is: { kind: "blank" | "comment" | "assign" | "function" | "expr", text, name?, params?, rhs?, problem? }.
|
|
56
|
+
* `problem` says why an assignment can't be made (a reserved name, a bad parameter list); the evaluator turns it into the line's error.
|
|
57
|
+
*/
|
|
58
|
+
export function classify(line) {
|
|
59
|
+
const text = typeof line === "string" ? line : "";
|
|
60
|
+
const trimmed = text.trim();
|
|
61
|
+
if (!trimmed) return { kind: "blank", text };
|
|
62
|
+
if (trimmed.startsWith("#") || trimmed.startsWith("//")) return { kind: "comment", text, comment: trimmed.replace(/^(#|\/\/)\s?/, "") };
|
|
63
|
+
const store = RIGHT_STORE.exec(text) ?? LEFT_STORE.exec(text);
|
|
64
|
+
if (store) {
|
|
65
|
+
const right = RIGHT_STORE.test(text);
|
|
66
|
+
const [name, rhs] = right ? [store[2], store[1]] : [store[1], store[2]];
|
|
67
|
+
const out = { kind: "assign", text, name, rhs: rhs.trim(), store: right ? "right" : "left" };
|
|
68
|
+
const why = nameProblem(name);
|
|
69
|
+
if (why) out.problem = why;
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
const m = ASSIGN.exec(text);
|
|
73
|
+
if (!m) return { kind: "expr", text, rhs: trimmed };
|
|
74
|
+
const [, name, rawParams, rhs] = m;
|
|
75
|
+
const out = { kind: rawParams === undefined ? "assign" : "function", text, name, rhs: rhs.trim() };
|
|
76
|
+
const bad = nameProblem(name);
|
|
77
|
+
if (rawParams !== undefined) {
|
|
78
|
+
const params = rawParams.trim() ? rawParams.split(",").map((p) => p.trim()) : [];
|
|
79
|
+
out.params = params;
|
|
80
|
+
if (!params.length) out.problem = `${name}() needs at least one parameter`;
|
|
81
|
+
else if (params.length > LIMITS.params) out.problem = `a function has at most ${LIMITS.params} parameters`;
|
|
82
|
+
else if (params.some((p) => !NAME.test(p))) out.problem = `the parameters of ${name} are names: use letters, digits and _`;
|
|
83
|
+
else if (new Set(params).size !== params.length) out.problem = `${name} has a parameter twice`;
|
|
84
|
+
else if (params.some((p) => RESERVED.has(p))) out.problem = `a parameter of ${name} is a constant, function or command name`;
|
|
85
|
+
}
|
|
86
|
+
if (bad && !out.problem) out.problem = bad;
|
|
87
|
+
if (!out.rhs && !out.problem) out.problem = `${name} = needs something on its right`;
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The names the lines assign, in order of first assignment: [{ name, kind: "variable" | "function" }] (the pane's output ports). */
|
|
92
|
+
export function assignedNames(lines) {
|
|
93
|
+
const seen = new Map();
|
|
94
|
+
for (const line of Array.isArray(lines) ? lines : []) {
|
|
95
|
+
const c = classify(line);
|
|
96
|
+
if ((c.kind === "assign" || c.kind === "function") && !c.problem) seen.set(c.name, c.kind === "function" ? "function" : "variable");
|
|
97
|
+
}
|
|
98
|
+
return [...seen].map(([name, kind]) => ({ name, kind }));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The index of the `)` that closes the `(` at `open`, or -1. Splits nothing; `splitArgs` does.
|
|
103
|
+
*/
|
|
104
|
+
export function closingParen(text, open) {
|
|
105
|
+
let depth = 0;
|
|
106
|
+
for (let i = open; i < text.length; i++) {
|
|
107
|
+
if (text[i] === "(") depth++;
|
|
108
|
+
else if (text[i] === ")" && --depth === 0) return i;
|
|
109
|
+
}
|
|
110
|
+
return -1;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The top-level comma-separated pieces of `text` (the inside of a call's parentheses), trimmed. */
|
|
114
|
+
export function splitArgs(text) {
|
|
115
|
+
const out = [];
|
|
116
|
+
let depth = 0;
|
|
117
|
+
let from = 0;
|
|
118
|
+
for (let i = 0; i < text.length; i++) {
|
|
119
|
+
if (text[i] === "(") depth++;
|
|
120
|
+
else if (text[i] === ")") depth--;
|
|
121
|
+
else if (text[i] === "," && depth === 0) (out.push(text.slice(from, i).trim()), (from = i + 1));
|
|
122
|
+
}
|
|
123
|
+
out.push(text.slice(from).trim());
|
|
124
|
+
return out;
|
|
125
|
+
}
|
package/src/calc/tex.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// A calc pane as LaTeX (the `latex` trait of src/panes/calc.js, used by src/export/latex.js): a comment line is prose, every other line is
|
|
2
|
+
// display math. `tex` is the exporter's set of helpers (escape, display, math, text, name, titled); nothing here escapes or formats by itself.
|
|
3
|
+
import { classify, splitArgs, COMMANDS } from "./syntax.js";
|
|
4
|
+
|
|
5
|
+
const COMMAND = new RegExp(`^\\s*(${COMMANDS.join("|")})\\s*\\((.*)\\)\\s*$`, "s");
|
|
6
|
+
|
|
7
|
+
function lineTex(raw, tex, called, plain) {
|
|
8
|
+
const c = classify(raw);
|
|
9
|
+
if (c.kind === "blank") return null;
|
|
10
|
+
if (c.kind === "comment") return tex.escape(c.comment);
|
|
11
|
+
const body = (text) => {
|
|
12
|
+
if (plain) return tex.text(text.trim());
|
|
13
|
+
const m = COMMAND.exec(text);
|
|
14
|
+
if (m) return `\\mathrm{${m[1]}}\\left(${splitArgs(m[2]).map((a) => tex.math(a, called)).join(", ")}\\right)`;
|
|
15
|
+
return tex.math(text, called);
|
|
16
|
+
};
|
|
17
|
+
if (c.kind === "function" && !c.problem) return tex.display(`${tex.name(c.name)}(${c.params.map(tex.name).join(", ")}) = ${body(c.rhs)}`);
|
|
18
|
+
if (c.kind === "assign" && !c.problem) return tex.display(`${tex.name(c.name)} = ${body(c.rhs)}`);
|
|
19
|
+
return tex.display(body(c.rhs ?? raw));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The lines of a calc pane's section. */
|
|
23
|
+
export function calcTex(pane, tex) {
|
|
24
|
+
const all = (Array.isArray(pane.lines) ? pane.lines : []).filter((l) => typeof l === "string");
|
|
25
|
+
const called = new Set(all.map((l) => classify(l)).filter((c) => c.kind === "function").map((c) => c.name));
|
|
26
|
+
const how = pane.arithmetic && pane.arithmetic !== "float" ? [`(${pane.arithmetic === "modulus" ? `Z/${tex.escape(pane.modulus ?? 7)}Z` : tex.escape(pane.arithmetic)} arithmetic)`] : [];
|
|
27
|
+
return tex.titled(pane, [...how, ...all.map((l) => lineTex(l, tex, called, pane.arithmetic === "units")).filter((l) => l !== null)]);
|
|
28
|
+
}
|