@ant-lang/wasm 0.7.0 → 0.9.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 CHANGED
@@ -45,9 +45,22 @@ which Vite, webpack and esbuild copy along. Pass `wasm` (a URL, a `Response`, by
45
45
  resolves to `{ exitCode, diagnostics }`. Output reaches `stdout` and `stderr` as `Uint8Array`
46
46
  chunks. Aborting `signal` interrupts the run as Ctrl-C does, once the program waits on
47
47
  something (see [Sandbox](#sandbox)).
48
+ - `test({ files, paths?, run?, skip?, failfast?, onEvent?, env?, stdin?, stdout?, stderr?, signal? })`
49
+ runs the tests of test files as `ant test` does and resolves to `{ exitCode, diagnostics }`.
50
+ `paths` are the test files to run, by default every `*_test.ant` among `files`; `run` and `skip`
51
+ are the patterns of the `-run` and `-skip` flags. `onEvent` gets each event of `ant test -json`
52
+ as an object: `{ action: "run" | "output" | "pass" | "skip" | "fail", file, test, ... }` and a
53
+ last `{ action: "done", passed, failed, skipped, elapsed }`. The files are checked first: when
54
+ the checker reports errors they are the `diagnostics`, and no test runs.
48
55
  - `languageServer(onMessage, { onLog? })` starts the Ant language server and returns
49
56
  `{ send, close }`, carrying JSON-RPC messages as objects. Documents are analyzed when their URI
50
57
  is a `file:` URI. [@ant-lang/monaco](../monaco) connects it to the Monaco editor.
58
+ - `debugAdapter({ files }, onMessage, { onLog? })` starts a debug adapter, what `ant dap` serves,
59
+ for one session and returns `{ send, close }`, carrying
60
+ [Debug Adapter Protocol](https://microsoft.github.io/debug-adapter-protocol/) messages as
61
+ objects. The `program` of the launch request and the sources of breakpoints are paths among
62
+ `files`. Requests reach the adapter while the program waits or is stopped (see
63
+ [Sandbox](#sandbox)), so run it in a worker.
51
64
 
52
65
  `files` maps module paths to source text, and `path` is the module to start from: the only module,
53
66
  or `main.ant` by default. Imports resolve among `files`.
package/ant.wasm CHANGED
Binary file
package/index.d.ts CHANGED
@@ -54,6 +54,51 @@ export interface RunResult {
54
54
  diagnostics: Diagnostic[];
55
55
  }
56
56
 
57
+ export interface TestOptions {
58
+ files: Files;
59
+ /**
60
+ * The test files to run, in order: paths among files whose names end in "_test.ant". The
61
+ * default is every test file among files, in lexical order, as `ant test` finds them.
62
+ */
63
+ paths?: string[];
64
+ /** Runs only the tests whose full name matches, as the -run flag of `ant test`: "parse/0x". */
65
+ run?: string;
66
+ /** Leaves out the tests that match, as the -skip flag. */
67
+ skip?: string;
68
+ /** Starts no test after the first failure. */
69
+ failfast?: boolean;
70
+ /** Called with each event of the run, the last of which is "done". */
71
+ onEvent?: (event: TestEvent) => void;
72
+ /** The whole environment. The default is empty. */
73
+ env?: Record<string, string>;
74
+ /** What the tests read from Os.stdin. */
75
+ stdin?: string;
76
+ /** Called with what the modules write to standard output outside a test, as they initialize. */
77
+ stdout?: (chunk: Uint8Array) => void;
78
+ /** Called with each write to standard error outside a test, the panic of an initializer included. */
79
+ stderr?: (chunk: Uint8Array) => void;
80
+ /** Ends the run, once the tests wait, as RunOptions.signal does. */
81
+ signal?: AbortSignal;
82
+ }
83
+
84
+ /** An event of a test run, as `ant test -json` prints them. `file` is a path of `paths`, `test` a full name such as "parse/0x10", `elapsed` in seconds. */
85
+ export type TestEvent =
86
+ | { action: "run"; file: string; test: string }
87
+ /** The text of one write of a test. */
88
+ | { action: "output"; file: string; test: string; output: string }
89
+ | { action: "pass"; file: string; test: string; elapsed: number }
90
+ | { action: "skip"; file: string; test: string; elapsed: number; reason?: string }
91
+ /** `message` holds the failures, as `ant test` reports them; none when only subtests failed. */
92
+ | { action: "fail"; file: string; test: string; elapsed: number; message?: string }
93
+ | { action: "done"; passed: number; failed: number; skipped: number; elapsed: number };
94
+
95
+ export interface TestResult {
96
+ /** The status `ant test` would exit with: 0, 1 when a test failed or the checker reported an error, 2 when a file did not load, 130 when the signal ended the run, or what Os.exit was given. */
97
+ exitCode: number;
98
+ /** The checker's errors. When there are any, no test ran and there are no events. */
99
+ diagnostics: Diagnostic[];
100
+ }
101
+
57
102
  /** A connection to a language server (spec/lsp.md), carrying JSON-RPC messages as objects. */
58
103
  export interface LanguageServerConnection {
59
104
  send(message: object): void;
@@ -61,6 +106,13 @@ export interface LanguageServerConnection {
61
106
  close(): void;
62
107
  }
63
108
 
109
+ /** A connection to a debug adapter, carrying Debug Adapter Protocol messages as objects. */
110
+ export interface DebugAdapterConnection {
111
+ send(message: object): void;
112
+ /** Ends the session and its program, as the end of the adapter's input stream does. */
113
+ close(): void;
114
+ }
115
+
64
116
  export interface Ant {
65
117
  /** Formats one module, as ant fmt does. Throws a SyntaxError when the source does not parse. */
66
118
  format(source: string, path?: string): string;
@@ -68,6 +120,11 @@ export interface Ant {
68
120
  check(program: Program): Diagnostic[];
69
121
  /** Runs a program. Ant code that blocks, such as on a channel or a timer, does not block the caller. */
70
122
  run(options: RunOptions): Promise<RunResult>;
123
+ /**
124
+ * Runs the tests of test files, as `ant test` does: the files are checked first, then each runs
125
+ * as a program of its own. Rejects when a path is not a test file or a pattern does not compile.
126
+ */
127
+ test(options: TestOptions): Promise<TestResult>;
71
128
  /**
72
129
  * Starts a language server. Documents are analyzed when their URI is a file: URI; their
73
130
  * imports resolve among the open documents, since ant.wasm has no file system.
@@ -76,6 +133,18 @@ export interface Ant {
76
133
  onMessage: (message: any) => void,
77
134
  options?: { onLog?: (line: string) => void },
78
135
  ): LanguageServerConnection;
136
+ /**
137
+ * Starts a debug adapter, what `ant dap` serves, for one session on a program whose modules
138
+ * are files. The launch request's `program` and the sources of breakpoints are paths among
139
+ * files; `env` is the whole environment and `stdin` names one of the files. Requests reach the
140
+ * adapter only while the program waits or is stopped, since JavaScript events run only then:
141
+ * run it in a worker, and terminate the worker to stop a program that never waits.
142
+ */
143
+ debugAdapter(
144
+ program: { files: Files },
145
+ onMessage: (message: any) => void,
146
+ options?: { onLog?: (line: string) => void },
147
+ ): DebugAdapterConnection;
79
148
  }
80
149
 
81
150
  export function loadAnt(options?: LoadOptions): Promise<Ant>;
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ant-lang/wasm loads Ant compiled to WebAssembly and wraps what it exposes: format, check,
2
- // run, and the language server.
2
+ // run, test, the language server, and the debug adapter.
3
3
  import "./wasm_exec.js";
4
4
 
5
5
  let instances = 0;
@@ -65,6 +65,19 @@ function rootOf(files, path) {
65
65
  return paths.length === 1 ? paths[0] : "main.ant";
66
66
  }
67
67
 
68
+ // testFiles are the test files among files, as ant test finds them in a directory: in lexical
69
+ // order, without those in directories whose names begin with a dot.
70
+ function testFiles(files) {
71
+ const inView = (path) =>
72
+ !path
73
+ .split("/")
74
+ .slice(0, -1)
75
+ .some((dir) => dir.startsWith("."));
76
+ return Object.keys(files)
77
+ .filter((path) => path.endsWith("_test.ant") && inView(path))
78
+ .sort();
79
+ }
80
+
68
81
  function wrap(raw) {
69
82
  return {
70
83
  format(source, path = "main.ant") {
@@ -80,6 +93,14 @@ function wrap(raw) {
80
93
  run(options) {
81
94
  return raw.run({ ...options, path: rootOf(options.files, options.path) });
82
95
  },
96
+ test(options) {
97
+ const { onEvent } = options;
98
+ return raw.test({
99
+ ...options,
100
+ paths: options.paths ?? testFiles(options.files),
101
+ onEvent: onEvent && ((json) => onEvent(JSON.parse(json))),
102
+ });
103
+ },
83
104
  languageServer(onMessage, options = {}) {
84
105
  const conn = raw.languageServer((json) => onMessage(JSON.parse(json)), options.onLog);
85
106
  return {
@@ -87,5 +108,12 @@ function wrap(raw) {
87
108
  close: () => conn.close(),
88
109
  };
89
110
  },
111
+ debugAdapter({ files }, onMessage, options = {}) {
112
+ const conn = raw.debugAdapter(files, (json) => onMessage(JSON.parse(json)), options.onLog);
113
+ return {
114
+ send: (message) => conn.send(JSON.stringify(message)),
115
+ close: () => conn.close(),
116
+ };
117
+ },
90
118
  };
91
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ant-lang/wasm",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "The Ant language compiled to WebAssembly: format, check, and run Ant programs, and serve the Ant language server, in browsers and Node.",
5
5
  "keywords": [
6
6
  "ant",
@@ -40,5 +40,5 @@
40
40
  "engines": {
41
41
  "node": ">=20"
42
42
  },
43
- "ant": "v0.3.0"
43
+ "ant": "v0.3.2"
44
44
  }