@defold-typescript/transpiler 0.45.0 → 0.46.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/hsm/async.ts +19 -1
- package/dist/hsm/debug.ts +15 -1
- package/dist/hsm/defold.ts +25 -2
- package/dist/hsm/index.ts +154 -5
- package/dist/index.js +15 -9
- package/package.json +2 -2
- package/src/companion-emit.ts +24 -6
- package/src/transpile.ts +5 -1
package/dist/hsm/async.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import type { EventObject, TaskStart } from "./index";
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* The second argument of a `sequence` function: a wait that leaving the state cancels, and a flag
|
|
5
|
+
* that tells the state was left.
|
|
6
|
+
* @noSelf
|
|
7
|
+
*/
|
|
4
8
|
export interface SequenceSignal {
|
|
9
|
+
/**
|
|
10
|
+
* Turns `true` once the state is left. Check it after awaiting anything other than `wait`: such
|
|
11
|
+
* a promise can still resolve after the state is left, and the code after it then runs.
|
|
12
|
+
*/
|
|
5
13
|
readonly aborted: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Resolves after `seconds`, counted by `timer.delay` rather than by `update(dt)`. A wait still
|
|
16
|
+
* pending when the state is left never resolves, so the code after it does not run.
|
|
17
|
+
*/
|
|
6
18
|
readonly wait: (seconds: number) => Promise<void>;
|
|
7
19
|
}
|
|
8
20
|
|
|
@@ -32,6 +44,12 @@ function rethrow(reason: unknown): void {
|
|
|
32
44
|
|
|
33
45
|
// The sequence plugs into the state's task, so its cleanup cancels the pending
|
|
34
46
|
// waits on every way out of the state; a wait left pending is never resolved.
|
|
47
|
+
/**
|
|
48
|
+
* Turns an `async` function into a state's `task`. The function gets the context and a
|
|
49
|
+
* `SequenceSignal`, and the event it returns is sent to the machine; one returned after the state
|
|
50
|
+
* is left is ignored. An error thrown inside it leaves the machine where it is and is raised
|
|
51
|
+
* again from a timer callback, so it shows in the engine console.
|
|
52
|
+
*/
|
|
35
53
|
export function sequence<Ctx, E extends EventObject>(
|
|
36
54
|
// biome-ignore lint/suspicious/noConfusingVoidType: `void` lets a sequence end without returning an event.
|
|
37
55
|
run: (ctx: Ctx, signal: SequenceSignal) => Promise<NoInfer<E> | void>,
|
package/dist/hsm/debug.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import type { EventObject, MachineInstance } from "./index";
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Returned by `inspect`. Keep it on `self` and call its `draw` from `update`.
|
|
5
|
+
* @noSelf
|
|
6
|
+
*/
|
|
4
7
|
export interface MachineInspector {
|
|
8
|
+
/**
|
|
9
|
+
* Draws the label and the active leaves as debug text, 40 units above `target`'s world
|
|
10
|
+
* position. Each call also advances the frame number the console lines show. In a release
|
|
11
|
+
* build it does nothing.
|
|
12
|
+
*/
|
|
5
13
|
readonly draw: (target: Hash | Url | string) => void;
|
|
6
14
|
}
|
|
7
15
|
|
|
@@ -86,6 +94,12 @@ function shownLeaves(leaves: readonly string[]): string {
|
|
|
86
94
|
return leaves.length === 0 ? "(stopped)" : shownList(leaves, raw);
|
|
87
95
|
}
|
|
88
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Prints one line when it is called, then one line for each move `instance` makes, each starting
|
|
99
|
+
* with `hsm` and the label. Call it once after `start`. A label another `inspect` call already
|
|
100
|
+
* used gets an ordinal, such as `enemy#2`. In a release build it registers nothing and returns an
|
|
101
|
+
* inspector whose `draw` does nothing, so the calls can stay in shipped code.
|
|
102
|
+
*/
|
|
89
103
|
export function inspect<Ctx, E extends EventObject, P extends string>(
|
|
90
104
|
instance: MachineInstance<Ctx, E, P>,
|
|
91
105
|
label: string,
|
package/dist/hsm/defold.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The event `messageEvents` builds from the Defold message `K`, for a machine's event union. It
|
|
3
|
+
* holds the message id as `type`, the payload fields declared through `BuiltinMessages` and
|
|
4
|
+
* `CustomMessages`, and the `sender`.
|
|
5
|
+
*/
|
|
1
6
|
export type MessageEvent<K extends MessageId> = K extends MessageId
|
|
2
|
-
? {
|
|
7
|
+
? {
|
|
8
|
+
/** The message id. A payload field named `type` never overrides it. */
|
|
9
|
+
readonly type: K;
|
|
10
|
+
/** The URL the message came from. A payload field named `sender` never overrides it. */
|
|
11
|
+
readonly sender: Url;
|
|
12
|
+
} & Omit<MessagePayload<K>, "type" | "sender">
|
|
3
13
|
: never;
|
|
4
14
|
|
|
5
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Returned by `messageEvents`. Build it once at module scope: it hashes the ids when it is
|
|
17
|
+
* created.
|
|
18
|
+
* @noSelf
|
|
19
|
+
*/
|
|
6
20
|
export interface MessageEventMapper<K extends MessageId> {
|
|
21
|
+
/**
|
|
22
|
+
* Turns the parameters of `on_message` into the event for that message, a new table on each
|
|
23
|
+
* call. Returns `undefined` for a message id that `messageEvents` was not given.
|
|
24
|
+
*/
|
|
7
25
|
readonly toEvent: (
|
|
8
26
|
message_id: Hash,
|
|
9
27
|
message: Record<string | number, unknown>,
|
|
@@ -11,6 +29,11 @@ export interface MessageEventMapper<K extends MessageId> {
|
|
|
11
29
|
) => MessageEvent<K> | undefined;
|
|
12
30
|
}
|
|
13
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Builds a mapper that turns the Defold messages listed in `ids` into typed machine events of
|
|
34
|
+
* the shape `{ type: id, ...payload, sender }`. Call it once at module scope: the ids are hashed
|
|
35
|
+
* here.
|
|
36
|
+
*/
|
|
14
37
|
export function messageEvents<const K extends MessageId>(ids: readonly K[]): MessageEventMapper<K> {
|
|
15
38
|
const hashes: Hash[] = [];
|
|
16
39
|
for (let i = 0; i < ids.length; i++) {
|
package/dist/hsm/index.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An event a machine handles: an object with a string `type`, plus any payload fields. A
|
|
3
|
+
* machine's event type is a union of these.
|
|
4
|
+
*/
|
|
1
5
|
export interface EventObject {
|
|
6
|
+
/** Names the event. A state's `on` takes these values as its keys. */
|
|
2
7
|
readonly type: string;
|
|
3
8
|
}
|
|
4
9
|
|
|
10
|
+
/**
|
|
11
|
+
* What made a move happen: `event` for a sent event, `after` for a timer, `update` for a path an
|
|
12
|
+
* `update` hook returned, `always` for an `always` rule, `stop` for `stop()`, and `reload` for a
|
|
13
|
+
* hot reload that changed the current state.
|
|
14
|
+
*/
|
|
5
15
|
export type MoveCause = "event" | "after" | "update" | "stop" | "reload" | "always";
|
|
6
16
|
|
|
7
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* A listener given to `onMove`. It gets the leaf left, the leaf entered (`undefined` after
|
|
19
|
+
* `stop()`), the cause, and the event, which is `undefined` unless the cause is `event`. It runs
|
|
20
|
+
* after the move's `enter` hooks.
|
|
21
|
+
* @noSelf
|
|
22
|
+
*/
|
|
8
23
|
export type MoveListener<E extends EventObject, P extends string = string> = (
|
|
9
24
|
from: P,
|
|
10
25
|
to: P | undefined,
|
|
@@ -12,14 +27,41 @@ export type MoveListener<E extends EventObject, P extends string = string> = (
|
|
|
12
27
|
event: E | undefined,
|
|
13
28
|
) => void;
|
|
14
29
|
|
|
15
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* One running machine, returned by `start`. `P` is the union of the machine's state paths; the
|
|
32
|
+
* `machine` a hook, a `run` or a `task` receives uses plain `string` paths instead.
|
|
33
|
+
* @noSelf
|
|
34
|
+
*/
|
|
16
35
|
export interface MachineInstance<Ctx, E extends EventObject, P extends string = string> {
|
|
36
|
+
/** The context passed to `start`. */
|
|
17
37
|
readonly ctx: Ctx;
|
|
38
|
+
/**
|
|
39
|
+
* The full path of the deepest active state, or `undefined` once stopped. In a parallel state
|
|
40
|
+
* it is the first region's leaf.
|
|
41
|
+
*/
|
|
18
42
|
readonly path: P | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* The full path of every active leaf, in region order: one path without parallel states, empty
|
|
45
|
+
* once stopped. It is the same array on every read, updated in place.
|
|
46
|
+
*/
|
|
19
47
|
readonly leaves: readonly P[];
|
|
48
|
+
/** Whether the state at the full path `path` is active. Ancestors of the current state count. */
|
|
20
49
|
readonly matches: (path: P) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Handles `event`, or queues it when called during a step. An event no active state handles is
|
|
52
|
+
* dropped, and once the machine is stopped the call does nothing.
|
|
53
|
+
*/
|
|
21
54
|
readonly send: (event: E) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Advances the machine by `dt` seconds: runs the `update` hooks, then the `after` timers if no
|
|
57
|
+
* hook moved it. A machine whose `update` is not called is frozen in time, and once the machine
|
|
58
|
+
* is stopped the call does nothing.
|
|
59
|
+
*/
|
|
22
60
|
readonly update: (dt: number) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Exits every active state, deepest first, and sets `path` to `undefined`. Called during a
|
|
63
|
+
* step, it lets that step finish and drops the queued events.
|
|
64
|
+
*/
|
|
23
65
|
readonly stop: () => void;
|
|
24
66
|
/**
|
|
25
67
|
* Adds a listener called after each move completes, with the leaf left, the leaf entered and
|
|
@@ -30,6 +72,10 @@ export interface MachineInstance<Ctx, E extends EventObject, P extends string =
|
|
|
30
72
|
readonly onMove: (listener: MoveListener<E, P>) => () => void;
|
|
31
73
|
}
|
|
32
74
|
|
|
75
|
+
/**
|
|
76
|
+
* A function a rule's `run` calls during the move, after the `exit` hooks and before the `enter`
|
|
77
|
+
* hooks. Its `event` is the variant the `on` key names.
|
|
78
|
+
*/
|
|
33
79
|
export type MoveAction<Ctx, E extends EventObject, V extends E = E> = (
|
|
34
80
|
ctx: Ctx,
|
|
35
81
|
event: V,
|
|
@@ -45,7 +91,11 @@ interface RuleList<Rule> {
|
|
|
45
91
|
readonly length: number;
|
|
46
92
|
}
|
|
47
93
|
|
|
48
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* The object form of a rule under `on`: a `to`, a `when` and a `run`, each optional. A rule with
|
|
96
|
+
* no `to` runs its `run` and nothing else: no state exits or enters.
|
|
97
|
+
* @noSelf
|
|
98
|
+
*/
|
|
49
99
|
export interface TransitionConfig<
|
|
50
100
|
Ctx,
|
|
51
101
|
E extends EventObject,
|
|
@@ -79,6 +129,10 @@ export interface TransitionConfig<
|
|
|
79
129
|
readonly reenter?: never;
|
|
80
130
|
}
|
|
81
131
|
|
|
132
|
+
/**
|
|
133
|
+
* What an `on` key maps to: a target path, a `TransitionConfig`, or a non-empty list of them
|
|
134
|
+
* tried in order. The first rule with no `when`, or whose `when` returns `true`, is taken.
|
|
135
|
+
*/
|
|
82
136
|
export type TransitionSpec<
|
|
83
137
|
Ctx,
|
|
84
138
|
E extends EventObject,
|
|
@@ -86,8 +140,16 @@ export type TransitionSpec<
|
|
|
86
140
|
To extends string = string,
|
|
87
141
|
> = To | TransitionConfig<Ctx, E, V, To> | RuleList<TransitionConfig<Ctx, E, V, To>>;
|
|
88
142
|
|
|
89
|
-
/**
|
|
143
|
+
/**
|
|
144
|
+
* The object form of an `always` rule: a `to` and an optional `when`. It gets no event and has
|
|
145
|
+
* no `run`; side effects belong in the target's `enter`.
|
|
146
|
+
* @noSelf
|
|
147
|
+
*/
|
|
90
148
|
export interface AlwaysConfig<Ctx, To extends string = string> {
|
|
149
|
+
/**
|
|
150
|
+
* The full path of the state to move to. An `always` rule without one throws when the machine
|
|
151
|
+
* is defined.
|
|
152
|
+
*/
|
|
91
153
|
readonly to: To;
|
|
92
154
|
/** Checked on every recheck, so it must have no side effects. */
|
|
93
155
|
readonly when?: (ctx: Ctx) => boolean;
|
|
@@ -97,11 +159,19 @@ export interface AlwaysConfig<Ctx, To extends string = string> {
|
|
|
97
159
|
readonly guard?: never;
|
|
98
160
|
}
|
|
99
161
|
|
|
162
|
+
/**
|
|
163
|
+
* What `always` takes: a target path, an `AlwaysConfig`, or a non-empty list of them. The first
|
|
164
|
+
* whose `when` is missing or passes is taken.
|
|
165
|
+
*/
|
|
100
166
|
export type AlwaysSpec<Ctx, To extends string = string> =
|
|
101
167
|
| To
|
|
102
168
|
| AlwaysConfig<Ctx, To>
|
|
103
169
|
| RuleList<AlwaysConfig<Ctx, To>>;
|
|
104
170
|
|
|
171
|
+
/**
|
|
172
|
+
* A state's rules by event `type`. Only the machine's event types are keys, and each rule's
|
|
173
|
+
* `when` and `run` see the event variant its key names.
|
|
174
|
+
*/
|
|
105
175
|
export type OnConfig<Ctx, E extends EventObject, To extends string = string> = {
|
|
106
176
|
readonly [K in E["type"]]?: TransitionSpec<Ctx, E, Extract<E, { type: K }>, To>;
|
|
107
177
|
};
|
|
@@ -116,6 +186,11 @@ export type StateHook<Ctx, E extends EventObject> = (
|
|
|
116
186
|
machine: MachineInstance<Ctx, E>,
|
|
117
187
|
) => void;
|
|
118
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Runs on every `update(dt)` while the state is active, deepest state first. It returns a full
|
|
191
|
+
* path to move there, or `undefined` to pass. The first hook to return a path wins: the hooks
|
|
192
|
+
* above it and the `after` timers do not run on that call, and the move has no `run`.
|
|
193
|
+
*/
|
|
119
194
|
export type UpdateHook<Ctx, E extends EventObject, To extends string = string> = (
|
|
120
195
|
ctx: Ctx,
|
|
121
196
|
dt: number,
|
|
@@ -134,8 +209,17 @@ export type TaskStart<Ctx, E extends EventObject> = (
|
|
|
134
209
|
// biome-ignore lint/suspicious/noConfusingVoidType: `void` keeps an expression-body task returning a void call (`=> go.animate(...)`) valid.
|
|
135
210
|
) => (() => void) | void;
|
|
136
211
|
|
|
137
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* A machine definition, returned by the second `defineMachine` call. Its `start` creates the
|
|
214
|
+
* instances that run.
|
|
215
|
+
* @noSelf
|
|
216
|
+
*/
|
|
138
217
|
export interface Machine<Ctx, E extends EventObject, P extends string = string> {
|
|
218
|
+
/**
|
|
219
|
+
* Creates an instance with `ctx` as its context and enters the root's `initial` chain. Each
|
|
220
|
+
* call returns a separate instance, which has already handled the events its `enter` hooks
|
|
221
|
+
* sent.
|
|
222
|
+
*/
|
|
139
223
|
readonly start: (ctx: Ctx) => MachineInstance<Ctx, E, P>;
|
|
140
224
|
}
|
|
141
225
|
|
|
@@ -173,6 +257,10 @@ type TreeOf<C> = C extends { readonly states: infer Children }
|
|
|
173
257
|
? { readonly [K in keyof Children]: TreeOf<Children[K]> }
|
|
174
258
|
: unknown;
|
|
175
259
|
|
|
260
|
+
/**
|
|
261
|
+
* The union of a config's state paths, each with its leading `/`. Paths are spelled out four
|
|
262
|
+
* levels deep; below that, any string under a fourth-level path is accepted.
|
|
263
|
+
*/
|
|
176
264
|
export type StatePath<C> = TreePath<TreeOf<C>>;
|
|
177
265
|
|
|
178
266
|
// The levels of child states below a node. It counts tree levels, so above a parallel state
|
|
@@ -206,14 +294,41 @@ type AnyChild<Ctx, E extends EventObject, T, To extends string> = unknown extend
|
|
|
206
294
|
// at the first such callback, before the states beside it are read.
|
|
207
295
|
/** @noSelf */
|
|
208
296
|
interface StateBase<Ctx, E extends EventObject, T, Self extends string, To extends string> {
|
|
297
|
+
/** Child states, by name. A name is non-empty and contains no `/`. */
|
|
209
298
|
readonly states?: {
|
|
210
299
|
readonly [K in keyof T]: StateNode<Ctx, E, T[K], `${Self}/${K & string}`, To>;
|
|
211
300
|
} & NoInfer<AnyChild<Ctx, E, T, To>>;
|
|
301
|
+
/**
|
|
302
|
+
* Rules by event `type`, each a target path, a rule object or a list of them. An event starts
|
|
303
|
+
* at the deepest active state and moves up to the parent while no rule takes it.
|
|
304
|
+
*/
|
|
212
305
|
readonly on?: NoInfer<OnConfig<Ctx, E, To>>;
|
|
306
|
+
/**
|
|
307
|
+
* Targets by delay in seconds of game time. The delay is counted by `update(dt)` while the
|
|
308
|
+
* state is active, from zero on each entry. One `update` call fires at most one timer, and
|
|
309
|
+
* none when an `update` hook moved the machine.
|
|
310
|
+
*/
|
|
213
311
|
readonly after?: NoInfer<{ readonly [seconds: number]: To }>;
|
|
312
|
+
/**
|
|
313
|
+
* Targets taken right after any move that leaves this state active; the first whose `when` is
|
|
314
|
+
* missing or passes wins. It is checked after a move only: not after a rule with no `to`, and
|
|
315
|
+
* not on an `update` call that moves nothing.
|
|
316
|
+
*/
|
|
214
317
|
readonly always?: AlwaysSpec<Ctx, To>;
|
|
318
|
+
/**
|
|
319
|
+
* Runs each time the state is entered, outermost state first: on a move, at `start`, and when
|
|
320
|
+
* a hot reload adds the state. It gets no event.
|
|
321
|
+
*/
|
|
215
322
|
readonly enter?: StateHook<Ctx, E>;
|
|
323
|
+
/**
|
|
324
|
+
* Runs each time the state is left, deepest state first, including on `stop()`. A state a hot
|
|
325
|
+
* reload removes is dropped without it.
|
|
326
|
+
*/
|
|
216
327
|
readonly exit?: StateHook<Ctx, E>;
|
|
328
|
+
/**
|
|
329
|
+
* Runs on every `update(dt)` while the state is active. Return one of the machine's full paths
|
|
330
|
+
* to move there, or `undefined` to stay.
|
|
331
|
+
*/
|
|
217
332
|
readonly update?: NoInfer<UpdateHook<Ctx, E, To>>;
|
|
218
333
|
/** Started on each entry; see `TaskStart`. */
|
|
219
334
|
readonly task?: TaskStart<Ctx, E>;
|
|
@@ -224,14 +339,32 @@ interface StateBase<Ctx, E extends EventObject, T, Self extends string, To exten
|
|
|
224
339
|
/** @noSelf */
|
|
225
340
|
interface CompoundState<Ctx, E extends EventObject, T, Self extends string, To extends string>
|
|
226
341
|
extends StateBase<Ctx, E, T, Self, To> {
|
|
342
|
+
/**
|
|
343
|
+
* `"parallel"` keeps every child active at once, each as a region with its own active state.
|
|
344
|
+
* Not allowed on the root.
|
|
345
|
+
*/
|
|
227
346
|
readonly type?: never;
|
|
347
|
+
/**
|
|
348
|
+
* The full path of the child entered with this state, such as `/on/bright` inside `on`.
|
|
349
|
+
* Required when `states` is set, unless the state is parallel.
|
|
350
|
+
*/
|
|
228
351
|
readonly initial?: NoInfer<unknown extends T ? string : `${Self}/${keyof T & string}`>;
|
|
352
|
+
/**
|
|
353
|
+
* Makes the state remember which child was active when it was last left, and enter it again
|
|
354
|
+
* instead of `initial`. The count is how many levels down to restore, and `"all"` restores
|
|
355
|
+
* every level below the state. The first entry uses `initial`, and a move that targets a state
|
|
356
|
+
* inside this one lands on that target.
|
|
357
|
+
*/
|
|
229
358
|
readonly restoreDepth?: NoInfer<TreeDepth<T, 4> | "all">;
|
|
230
359
|
}
|
|
231
360
|
|
|
232
361
|
/** @noSelf */
|
|
233
362
|
interface ParallelState<Ctx, E extends EventObject, T, Self extends string, To extends string>
|
|
234
363
|
extends StateBase<Ctx, E, T, Self, To> {
|
|
364
|
+
/**
|
|
365
|
+
* `"parallel"` keeps every child active at once, each as a region with its own active state.
|
|
366
|
+
* Not allowed on the root.
|
|
367
|
+
*/
|
|
235
368
|
readonly type: "parallel";
|
|
236
369
|
readonly initial?: never;
|
|
237
370
|
readonly restoreDepth?: never;
|
|
@@ -252,8 +385,17 @@ type RootState<Ctx, E extends EventObject, T> = CompoundState<
|
|
|
252
385
|
readonly states: object;
|
|
253
386
|
};
|
|
254
387
|
|
|
388
|
+
/**
|
|
389
|
+
* The config of one state. Every field is optional. A state with child `states` is a compound
|
|
390
|
+
* state and needs an `initial`, unless its `type` is `"parallel"`; a state without children is a
|
|
391
|
+
* leaf.
|
|
392
|
+
*/
|
|
255
393
|
export type StateConfig<Ctx, E extends EventObject> = StateNode<Ctx, E, unknown, string, string>;
|
|
256
394
|
|
|
395
|
+
/**
|
|
396
|
+
* The root config passed to `defineMachine`. It takes the fields of a state and requires
|
|
397
|
+
* `initial` and `states`. The root has no name and cannot be parallel.
|
|
398
|
+
*/
|
|
257
399
|
export type MachineConfig<Ctx, E extends EventObject> = CompoundState<
|
|
258
400
|
Ctx,
|
|
259
401
|
E,
|
|
@@ -1445,6 +1587,13 @@ function define<Ctx, E extends EventObject>(
|
|
|
1445
1587
|
|
|
1446
1588
|
// Ctx and E are given explicitly and the config is inferred, so the config takes a second call.
|
|
1447
1589
|
// A key makes a later definition under it (a hot-reloaded module re-running) rebind live instances.
|
|
1590
|
+
/**
|
|
1591
|
+
* Defines a machine in two calls: `defineMachine<Ctx, E>()` fixes the context and event types and
|
|
1592
|
+
* takes an optional hot-reload key, and the second call takes the config. The config is checked
|
|
1593
|
+
* when that call runs, and a mistake throws, naming the state path. With a key, a later
|
|
1594
|
+
* definition under it puts its states into the machine already built, so every running instance
|
|
1595
|
+
* sees the edit; without one, each call builds a separate machine.
|
|
1596
|
+
*/
|
|
1448
1597
|
export function defineMachine<Ctx, E extends EventObject>(
|
|
1449
1598
|
key?: string,
|
|
1450
1599
|
): <T>(config: RootState<Ctx, E, T>) => Machine<Ctx, E, TreePath<T>> {
|
package/dist/index.js
CHANGED
|
@@ -3150,6 +3150,14 @@ function resolvedRequirePaths(sourceFile, checker) {
|
|
|
3150
3150
|
}
|
|
3151
3151
|
return paths;
|
|
3152
3152
|
}
|
|
3153
|
+
var NO_RESOLUTION_MARKER = "@NoResolution:";
|
|
3154
|
+
function companionRequirePath(argument, paths) {
|
|
3155
|
+
const resolved = paths.get(argument);
|
|
3156
|
+
if (resolved !== undefined) {
|
|
3157
|
+
return resolved;
|
|
3158
|
+
}
|
|
3159
|
+
return argument.startsWith(NO_RESOLUTION_MARKER) ? argument.slice(NO_RESOLUTION_MARKER.length) : argument;
|
|
3160
|
+
}
|
|
3153
3161
|
function resolveMovedRequires(statements, paths) {
|
|
3154
3162
|
for (const statement of statements) {
|
|
3155
3163
|
forEachLuaNode(statement, (node) => {
|
|
@@ -3163,10 +3171,7 @@ function resolveMovedRequires(statements, paths) {
|
|
|
3163
3171
|
if (argument === undefined || !isStringLiteral7(argument)) {
|
|
3164
3172
|
return;
|
|
3165
3173
|
}
|
|
3166
|
-
|
|
3167
|
-
if (resolved !== undefined) {
|
|
3168
|
-
argument.value = resolved;
|
|
3169
|
-
}
|
|
3174
|
+
argument.value = companionRequirePath(argument.value, paths);
|
|
3170
3175
|
});
|
|
3171
3176
|
}
|
|
3172
3177
|
}
|
|
@@ -3186,8 +3191,8 @@ function withResolvedRequire(statement, paths) {
|
|
|
3186
3191
|
if (argument === undefined || !isStringLiteral7(argument)) {
|
|
3187
3192
|
return statement;
|
|
3188
3193
|
}
|
|
3189
|
-
const resolved =
|
|
3190
|
-
if (resolved ===
|
|
3194
|
+
const resolved = companionRequirePath(argument.value, paths);
|
|
3195
|
+
if (resolved === argument.value) {
|
|
3191
3196
|
return statement;
|
|
3192
3197
|
}
|
|
3193
3198
|
return createVariableDeclarationStatement3(createIdentifier4(name.text), createCallExpression4(createIdentifier4("require"), [createStringLiteral3(resolved)]));
|
|
@@ -3655,7 +3660,7 @@ function collectOutputs(transpiledFiles, diagnostics, userKeys, emittedCompanion
|
|
|
3655
3660
|
...hsm !== undefined ? { hsmModules: hsm } : {}
|
|
3656
3661
|
};
|
|
3657
3662
|
}
|
|
3658
|
-
function transpileVirtual(files, companionEmit) {
|
|
3663
|
+
function transpileVirtual(files, companionEmit, overrides = {}) {
|
|
3659
3664
|
return tstl2.transpileVirtualProject({ ...AMBIENT_FILES, ...files }, {
|
|
3660
3665
|
luaTarget: tstl2.LuaTarget.Lua51,
|
|
3661
3666
|
sourceMap: true,
|
|
@@ -3672,7 +3677,8 @@ function transpileVirtual(files, companionEmit) {
|
|
|
3672
3677
|
{ plugin: hsmLoweringPlugin },
|
|
3673
3678
|
{ plugin: typeApplicationErasurePlugin },
|
|
3674
3679
|
{ plugin: companionEmit.plugin }
|
|
3675
|
-
]
|
|
3680
|
+
],
|
|
3681
|
+
...overrides
|
|
3676
3682
|
});
|
|
3677
3683
|
}
|
|
3678
3684
|
function transpileProject(input) {
|
|
@@ -3697,7 +3703,7 @@ function compileHsmModules() {
|
|
|
3697
3703
|
nameByKey.set(key, name);
|
|
3698
3704
|
files[key] = readFileSync(path3.join(sourceDir, `${name}.ts`), "utf8");
|
|
3699
3705
|
}
|
|
3700
|
-
const result = transpileVirtual(files, createCompanionEmitPlugin());
|
|
3706
|
+
const result = transpileVirtual(files, createCompanionEmitPlugin(), { removeComments: true });
|
|
3701
3707
|
const problems = result.diagnostics.map((d) => `${d.file?.fileName ?? "(project)"}: ${flattenDiagnosticMessage(d.messageText)}`);
|
|
3702
3708
|
const lua = {};
|
|
3703
3709
|
for (const file of result.transpiledFiles) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defold-typescript/transpiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "TypeScript-to-Lua build pipeline tuned for Defold's runtime.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"test": "bun test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@defold-typescript/types": "0.
|
|
38
|
+
"@defold-typescript/types": "0.46.0",
|
|
39
39
|
"@typescript-to-lua/language-extensions": "1.19.0",
|
|
40
40
|
"typescript": "6.0.2",
|
|
41
41
|
"typescript-to-lua": "^1.36.0"
|
package/src/companion-emit.ts
CHANGED
|
@@ -290,6 +290,27 @@ function resolvedRequirePaths(
|
|
|
290
290
|
return paths;
|
|
291
291
|
}
|
|
292
292
|
|
|
293
|
+
/**
|
|
294
|
+
* The prefix a lowering puts on the require of a runtime module with no source in
|
|
295
|
+
* the program (`hsm`, the timers), which TSTL's resolver strips from the script.
|
|
296
|
+
*/
|
|
297
|
+
const NO_RESOLUTION_MARKER = "@NoResolution:";
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* How the companion spells the require the Lua AST spells `argument`: a project
|
|
301
|
+
* source by its resolved path, and a marked runtime module without the marker.
|
|
302
|
+
* Left on, the marker is a path Defold cannot load and Windows cannot even name.
|
|
303
|
+
*/
|
|
304
|
+
function companionRequirePath(argument: string, paths: ReadonlyMap<string, string>): string {
|
|
305
|
+
const resolved = paths.get(argument);
|
|
306
|
+
if (resolved !== undefined) {
|
|
307
|
+
return resolved;
|
|
308
|
+
}
|
|
309
|
+
return argument.startsWith(NO_RESOLUTION_MARKER)
|
|
310
|
+
? argument.slice(NO_RESOLUTION_MARKER.length)
|
|
311
|
+
: argument;
|
|
312
|
+
}
|
|
313
|
+
|
|
293
314
|
/**
|
|
294
315
|
* Rewrites every `require` a moved statement carries, at whatever depth — a
|
|
295
316
|
* re-export's is nested in the `do … end` block TSTL emits for it.
|
|
@@ -313,10 +334,7 @@ function resolveMovedRequires(
|
|
|
313
334
|
if (argument === undefined || !isStringLiteral(argument)) {
|
|
314
335
|
return;
|
|
315
336
|
}
|
|
316
|
-
|
|
317
|
-
if (resolved !== undefined) {
|
|
318
|
-
argument.value = resolved;
|
|
319
|
-
}
|
|
337
|
+
argument.value = companionRequirePath(argument.value, paths);
|
|
320
338
|
});
|
|
321
339
|
}
|
|
322
340
|
}
|
|
@@ -339,8 +357,8 @@ function withResolvedRequire(statement: Statement, paths: ReadonlyMap<string, st
|
|
|
339
357
|
if (argument === undefined || !isStringLiteral(argument)) {
|
|
340
358
|
return statement;
|
|
341
359
|
}
|
|
342
|
-
const resolved =
|
|
343
|
-
if (resolved ===
|
|
360
|
+
const resolved = companionRequirePath(argument.value, paths);
|
|
361
|
+
if (resolved === argument.value) {
|
|
344
362
|
return statement;
|
|
345
363
|
}
|
|
346
364
|
return createVariableDeclarationStatement(
|
package/src/transpile.ts
CHANGED
|
@@ -327,6 +327,7 @@ export function collectOutputs(
|
|
|
327
327
|
function transpileVirtual(
|
|
328
328
|
files: Readonly<Record<string, string>>,
|
|
329
329
|
companionEmit: ReturnType<typeof createCompanionEmitPlugin>,
|
|
330
|
+
overrides: Pick<tstl.CompilerOptions, "removeComments"> = {},
|
|
330
331
|
): tstl.TranspileVirtualProjectResult {
|
|
331
332
|
return tstl.transpileVirtualProject(
|
|
332
333
|
{ ...AMBIENT_FILES, ...files },
|
|
@@ -354,6 +355,7 @@ function transpileVirtual(
|
|
|
354
355
|
{ plugin: typeApplicationErasurePlugin },
|
|
355
356
|
{ plugin: companionEmit.plugin },
|
|
356
357
|
],
|
|
358
|
+
...overrides,
|
|
357
359
|
},
|
|
358
360
|
);
|
|
359
361
|
}
|
|
@@ -398,7 +400,9 @@ export function compileHsmModules(): Readonly<Record<string, string>> {
|
|
|
398
400
|
nameByKey.set(key, name);
|
|
399
401
|
files[key] = readFileSync(path.join(sourceDir, `${name}.ts`), "utf8");
|
|
400
402
|
}
|
|
401
|
-
|
|
403
|
+
// The source's doc comments are for the editor, which reads the declarations; the Lua a
|
|
404
|
+
// project gets carries none of them.
|
|
405
|
+
const result = transpileVirtual(files, createCompanionEmitPlugin(), { removeComments: true });
|
|
402
406
|
|
|
403
407
|
const problems = result.diagnostics.map(
|
|
404
408
|
(d) => `${d.file?.fileName ?? "(project)"}: ${flattenDiagnosticMessage(d.messageText)}`,
|