@lucy/liblucy 0.6.0 → 0.7.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/dist/lucy.wasm CHANGED
Binary file
package/liblucy.d.ts CHANGED
@@ -1,14 +1,29 @@
1
1
  declare namespace LibLucy {
2
2
  interface CompileOptions {
3
- /** Also produce TypeScript declarations (.d.ts) for the module. Experimental. */
3
+ /**
4
+ * What to compile to: 'js', the default, for a JavaScript module and
5
+ * its declarations, or 'ts' for the two as one TypeScript module.
6
+ */
7
+ format?: 'js' | 'ts';
8
+ /**
9
+ * @deprecated The declarations are always produced. This does nothing.
10
+ */
4
11
  dts?: boolean;
5
12
  }
6
13
 
7
14
  interface CompileResult {
8
15
  /** The compiled JavaScript module. */
9
16
  js: string;
10
- /** The module's TypeScript declarations, when the `dts` option is set. */
11
- dts?: string;
17
+ /** The module's TypeScript declarations (.d.ts). */
18
+ dts: string;
19
+ }
20
+
21
+ interface TypeScriptCompileResult {
22
+ /**
23
+ * The compiled TypeScript module: the JavaScript with its types in it.
24
+ * Its syntax is erasable, so Node can run it as it is.
25
+ */
26
+ ts: string;
12
27
  }
13
28
 
14
29
  /** An error or warning from the compiler. */
@@ -44,7 +59,9 @@ declare namespace LibLucy {
44
59
  * Throws a CompileError if the source doesn't compile, after printing
45
60
  * them.
46
61
  */
47
- export function compile(source: string, filename: string, options?: CompileOptions): CompileResult;
62
+ export function compile(source: string, filename: string, options?: CompileOptions & { format?: 'js' }): CompileResult;
63
+ /** The same, to TypeScript. */
64
+ export function compile(source: string, filename: string, options: CompileOptions & { format: 'ts' }): TypeScriptCompileResult;
48
65
  }
49
66
 
50
67
  export = LibLucy;
package/liblucy.mjs CHANGED
@@ -95,9 +95,10 @@ export function createCompiler(instance, {
95
95
  * Compile Lucy source to a module of plain JavaScript state machines.
96
96
  * @param source {String} the input Lucy source.
97
97
  * @param filename {String} the name of the Lucy file.
98
- * @param options {{ dts?: boolean }}
99
- * @returns {{ js: string, dts?: string }} The compiled JavaScript module,
100
- * and its TypeScript declarations if options.dts is set.
98
+ * @param options {{ format?: 'js' | 'ts' }}
99
+ * @returns {{ js: string, dts: string } | { ts: string }} The compiled
100
+ * JavaScript module and its TypeScript declarations, or with the 'ts'
101
+ * format the two as one TypeScript module.
101
102
  * @throws {Error} 'Compiler error' if the source doesn't compile, with the
102
103
  * errors and warnings in its diagnostics property.
103
104
  */
@@ -108,7 +109,10 @@ export function createCompiler(instance, {
108
109
 
109
110
  // Read the options before allocating anything, so that bad options
110
111
  // can't throw with memory still allocated.
111
- const dts = options.dts;
112
+ const format = options.format ?? 'js';
113
+ if(format !== 'js' && format !== 'ts') {
114
+ throw new Error(`Unknown format '${format}'. Use 'js' or 'ts'.`);
115
+ }
112
116
 
113
117
  const srcPtr = writeString(source);
114
118
  let fnPtr;
@@ -123,20 +127,20 @@ export function createCompiler(instance, {
123
127
  let diagnostics = null;
124
128
  try {
125
129
  result = wasm.lucy_result_create();
126
- // dts becomes an i32 as it always has (true is 1, undefined 0), while
127
- // whether out.dts is set follows its truthiness, as before.
128
- wasm.lucy_result_set_options(result, dts);
130
+ // 1 is LUCY_FORMAT_TS.
131
+ wasm.lucy_result_set_format(result, format === 'ts' ? 1 : 0);
129
132
  wasm.lucy_compile(result, srcPtr, fnPtr);
130
133
 
131
134
  // The compiler doesn't print; this prints what lucyc would.
132
135
  printDiagnostics(result);
133
136
 
134
137
  if(wasm.lucy_result_success(result)) {
135
- out = {};
136
- out.js = readString(wasm.lucy_result_js(result));
137
- if(dts) {
138
- out.dts = readString(wasm.lucy_result_dts(result));
139
- }
138
+ out = format === 'ts' ? {
139
+ ts: readString(wasm.lucy_result_ts(result))
140
+ } : {
141
+ js: readString(wasm.lucy_result_js(result)),
142
+ dts: readString(wasm.lucy_result_dts(result))
143
+ };
140
144
  } else {
141
145
  diagnostics = readDiagnostics(result);
142
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucy/liblucy",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "The Lucy compiler project.",
5
5
  "exports": {
6
6
  ".": {
package/readme.md CHANGED
@@ -21,7 +21,8 @@ machine.stop();
21
21
  - `createMachine({ context, actions, guards, assigns, delays, services })`: the options supply what the Lucy source names with `:symbols`, each called as `(context, event)`; a `services` entry returns a promise for `invoke(:name)` and an unstarted machine for `spawn(:name)`. Only the options a machine uses are read.
22
22
  - `state` is the current state's name, or a dotted path such as `'red.walk'` inside a nested machine. `context` is a plain object, replaced (not mutated) by each `assign`. `done` becomes `true` when a final state is entered.
23
23
  - `send(event)` takes a type or an `{ type, data }` object and runs the transition at once. `subscribe(listener)` calls the listener with the machine after each change and returns an unsubscribe function. `start()` enters the initial state (entry actions, timers, invokes) and returns the machine; `stop()` clears timers, drops pending invokes, stops invoked and spawned children, and clears the listeners.
24
- - `--experimental-dts` writes a `.d.ts` that types each factory's options and the machine it returns.
24
+ - Every module comes with a `.d.ts` that types each factory's options and the machine it returns: `lucyc --out-file toggle.js` writes `toggle.d.ts` beside it, and `compile()` returns it as `dts`.
25
+ - `lucyc --ts` (and `compile(source, filename, { format: 'ts' })`, which returns `ts`) compiles to one TypeScript module instead: the same code with those types in it. Its syntax is erasable, so Node runs it as it is. The types are those of the factories; the code inside them is marked `// @ts-nocheck`, as it is generated and uses `any` for the events.
25
26
 
26
27
  ## Typed events
27
28