@ttsc/wasm 0.12.3 → 0.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cmd/ttsc-wasm/main.go +101 -89
- package/cmd/ttsc-wasm/main_wasm.go +6 -4
- package/dist/ttsc.wasm +0 -0
- package/host/api.go +154 -145
- package/host/doc.go +12 -12
- package/host/host.go +236 -225
- package/host/host_native.go +5 -2
- package/host/plugin.go +31 -31
- package/lib/src/MemFS.d.ts +35 -0
- package/lib/src/MemFS.js +57 -4
- package/lib/src/MemFS.js.map +1 -1
- package/lib/src/api.d.ts +46 -13
- package/lib/src/api.js +7 -4
- package/lib/src/api.js.map +1 -1
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +10 -3
- package/lib/src/index.js.map +1 -1
- package/lib/src/instantiate.d.ts +10 -7
- package/lib/src/instantiate.js +20 -3
- package/lib/src/instantiate.js.map +1 -1
- package/package.json +2 -2
- package/shim-vendor/shim/ast/shim.go +24 -0
- package/shim-vendor/shim/checker/shim.go +35 -0
- package/shim-vendor/shim/core/shim.go +17 -0
- package/shim-vendor/shim/diagnosticwriter/shim.go +4 -0
- package/shim-vendor/shim/lsp/shim.go +3 -3
- package/shim-vendor/shim/printer/shim.go +20 -0
- package/src/MemFS.ts +92 -7
- package/src/api.ts +46 -13
- package/src/index.ts +1 -5
- package/src/instantiate.ts +37 -14
|
@@ -177,44 +177,68 @@ const (
|
|
|
177
177
|
ModifierFlagsReadonly = innerast.ModifierFlagsReadonly
|
|
178
178
|
)
|
|
179
179
|
|
|
180
|
+
// NewNodeFactory creates an AST node factory with the supplied creation hooks.
|
|
181
|
+
// Pass a zero-value NodeFactoryHooks to get the default factory behaviour.
|
|
180
182
|
func NewNodeFactory(options NodeFactoryHooks) *NodeFactory {
|
|
181
183
|
return innerast.NewNodeFactory(options)
|
|
182
184
|
}
|
|
183
185
|
|
|
186
|
+
// NewNodeVisitor creates a visitor that calls visit for each node encountered
|
|
187
|
+
// during a tree walk, delegating node recreation to factory using hooks.
|
|
184
188
|
func NewNodeVisitor(visit func(node *Node) *Node, factory *NodeFactory, options NodeVisitorHooks) *NodeVisitor {
|
|
185
189
|
return innerast.NewNodeVisitor(visit, factory, options)
|
|
186
190
|
}
|
|
187
191
|
|
|
192
|
+
// IsFunctionLike reports whether node is any function-like construct
|
|
193
|
+
// (function declaration, arrow function, method, constructor, accessor, etc.).
|
|
188
194
|
func IsFunctionLike(node *Node) bool {
|
|
189
195
|
return innerast.IsFunctionLike(node)
|
|
190
196
|
}
|
|
191
197
|
|
|
198
|
+
// IsPropertyAssignment reports whether node is a key:value pair inside an
|
|
199
|
+
// object literal expression.
|
|
192
200
|
func IsPropertyAssignment(node *Node) bool {
|
|
193
201
|
return innerast.IsPropertyAssignment(node)
|
|
194
202
|
}
|
|
195
203
|
|
|
204
|
+
// IsPropertyDeclaration reports whether node is a class property declaration.
|
|
196
205
|
func IsPropertyDeclaration(node *Node) bool {
|
|
197
206
|
return innerast.IsPropertyDeclaration(node)
|
|
198
207
|
}
|
|
199
208
|
|
|
209
|
+
// IsModuleBlock reports whether node is the body block of a namespace or
|
|
210
|
+
// module declaration.
|
|
200
211
|
func IsModuleBlock(node *Node) bool {
|
|
201
212
|
return innerast.IsModuleBlock(node)
|
|
202
213
|
}
|
|
203
214
|
|
|
215
|
+
// IsStringLiteral reports whether node is a string literal token.
|
|
204
216
|
func IsStringLiteral(node *Node) bool {
|
|
205
217
|
return innerast.IsStringLiteral(node)
|
|
206
218
|
}
|
|
207
219
|
|
|
220
|
+
// IsPropertyAccessExpression reports whether node is a dotted member access
|
|
221
|
+
// (e.g. `a.b`).
|
|
208
222
|
func IsPropertyAccessExpression(node *Node) bool {
|
|
209
223
|
return innerast.IsPropertyAccessExpression(node)
|
|
210
224
|
}
|
|
211
225
|
|
|
226
|
+
// IsElementAccessExpression reports whether node is a bracket member access
|
|
227
|
+
// (e.g. `a[b]`).
|
|
212
228
|
func IsElementAccessExpression(node *Node) bool {
|
|
213
229
|
return innerast.IsElementAccessExpression(node)
|
|
214
230
|
}
|
|
215
231
|
|
|
232
|
+
// GetSourceFileOfNode walks up the parent chain to return the SourceFile that
|
|
233
|
+
// owns the given node. Linked from the internal package via go:linkname because
|
|
234
|
+
// the function is unexported there.
|
|
235
|
+
//
|
|
216
236
|
//go:linkname GetSourceFileOfNode github.com/microsoft/typescript-go/internal/ast.GetSourceFileOfNode
|
|
217
237
|
func GetSourceFileOfNode(node *innerast.Node) *innerast.SourceFile
|
|
218
238
|
|
|
239
|
+
// GetNodeAtPosition returns the deepest AST node whose range covers position
|
|
240
|
+
// in file. When includeJSDoc is true the search descends into JSDoc sub-trees.
|
|
241
|
+
// Linked from the internal package via go:linkname.
|
|
242
|
+
//
|
|
219
243
|
//go:linkname GetNodeAtPosition github.com/microsoft/typescript-go/internal/ast.GetNodeAtPosition
|
|
220
244
|
func GetNodeAtPosition(file *innerast.SourceFile, position int, includeJSDoc bool) *innerast.Node
|
|
@@ -59,34 +59,50 @@ const (
|
|
|
59
59
|
ElementFlagsVariadic = innerchecker.ElementFlagsVariadic
|
|
60
60
|
)
|
|
61
61
|
|
|
62
|
+
// IsTupleType reports whether t is a fixed-length tuple type.
|
|
62
63
|
func IsTupleType(t *innerchecker.Type) bool {
|
|
63
64
|
return innerchecker.IsTupleType(t)
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
// Checker_getIndexInfosOfType returns the index signatures (string/number/symbol
|
|
68
|
+
// index infos) declared on t.
|
|
66
69
|
func Checker_getIndexInfosOfType(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.IndexInfo {
|
|
67
70
|
return recv.GetIndexInfosOfType(t)
|
|
68
71
|
}
|
|
69
72
|
|
|
73
|
+
// Checker_getPropertiesOfType returns the named property symbols of t. For
|
|
74
|
+
// union and intersection types this is the set of properties visible on every
|
|
75
|
+
// member.
|
|
70
76
|
func Checker_getPropertiesOfType(recv *innerchecker.Checker, t *innerchecker.Type) []*innerast.Symbol {
|
|
71
77
|
return recv.GetPropertiesOfType(t)
|
|
72
78
|
}
|
|
73
79
|
|
|
80
|
+
// Checker_getApparentProperties returns the properties visible on t after
|
|
81
|
+
// resolving primitive wrapper types (e.g. string → String).
|
|
74
82
|
func Checker_getApparentProperties(recv *innerchecker.Checker, t *innerchecker.Type) []*innerast.Symbol {
|
|
75
83
|
return recv.GetApparentProperties(t)
|
|
76
84
|
}
|
|
77
85
|
|
|
86
|
+
// Checker_getTypeArguments returns the type arguments of a generic reference
|
|
87
|
+
// type, or nil when t is not a reference.
|
|
78
88
|
func Checker_getTypeArguments(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type {
|
|
79
89
|
return recv.GetTypeArguments(t)
|
|
80
90
|
}
|
|
81
91
|
|
|
92
|
+
// Checker_getTypeOfSymbol returns the declared type of symbol, resolving
|
|
93
|
+
// aliases and following late-bound types.
|
|
82
94
|
func Checker_getTypeOfSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerchecker.Type {
|
|
83
95
|
return recv.GetTypeOfSymbol(symbol)
|
|
84
96
|
}
|
|
85
97
|
|
|
98
|
+
// Checker_getTypeOfSymbolAtLocation returns the contextual type of symbol as
|
|
99
|
+
// observed at the given AST node (useful for narrowed types in control flow).
|
|
86
100
|
func Checker_getTypeOfSymbolAtLocation(recv *innerchecker.Checker, symbol *innerast.Symbol, node *innerast.Node) *innerchecker.Type {
|
|
87
101
|
return recv.GetTypeOfSymbolAtLocation(symbol, node)
|
|
88
102
|
}
|
|
89
103
|
|
|
104
|
+
// Checker_getTypeOfPropertyOfType looks up the type of the named property on t
|
|
105
|
+
// and returns nil when no such property exists.
|
|
90
106
|
func Checker_getTypeOfPropertyOfType(recv *innerchecker.Checker, t *innerchecker.Type, name string) *innerchecker.Type {
|
|
91
107
|
return recv.GetTypeOfPropertyOfType(t, name)
|
|
92
108
|
}
|
|
@@ -94,6 +110,8 @@ func Checker_getTypeOfPropertyOfType(recv *innerchecker.Checker, t *innerchecker
|
|
|
94
110
|
//go:linkname checkerGetAliasSymbolForTypeNode github.com/microsoft/typescript-go/internal/checker.(*Checker).getAliasSymbolForTypeNode
|
|
95
111
|
func checkerGetAliasSymbolForTypeNode(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol
|
|
96
112
|
|
|
113
|
+
// Checker_getAliasSymbolForTypeNode returns the alias symbol that a type node
|
|
114
|
+
// refers to when the node is itself a type alias reference (e.g. `type Foo = ...`).
|
|
97
115
|
func Checker_getAliasSymbolForTypeNode(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol {
|
|
98
116
|
return checkerGetAliasSymbolForTypeNode(recv, node)
|
|
99
117
|
}
|
|
@@ -101,6 +119,8 @@ func Checker_getAliasSymbolForTypeNode(recv *innerchecker.Checker, node *inneras
|
|
|
101
119
|
//go:linkname checkerGetDeclarationOfAliasSymbol github.com/microsoft/typescript-go/internal/checker.(*Checker).getDeclarationOfAliasSymbol
|
|
102
120
|
func checkerGetDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Node
|
|
103
121
|
|
|
122
|
+
// Checker_getDeclarationOfAliasSymbol resolves an import/export alias symbol to
|
|
123
|
+
// its original declaration node.
|
|
104
124
|
func Checker_getDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Node {
|
|
105
125
|
return checkerGetDeclarationOfAliasSymbol(recv, symbol)
|
|
106
126
|
}
|
|
@@ -108,6 +128,8 @@ func Checker_getDeclarationOfAliasSymbol(recv *innerchecker.Checker, symbol *inn
|
|
|
108
128
|
//go:linkname checkerGetTargetOfImportSpecifier github.com/microsoft/typescript-go/internal/checker.(*Checker).getTargetOfImportSpecifier
|
|
109
129
|
func checkerGetTargetOfImportSpecifier(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol
|
|
110
130
|
|
|
131
|
+
// Checker_getTargetOfImportSpecifier resolves an import specifier node to the
|
|
132
|
+
// exported symbol it binds. Returns nil if recv or node is nil.
|
|
111
133
|
func Checker_getTargetOfImportSpecifier(recv *innerchecker.Checker, node *innerast.Node) *innerast.Symbol {
|
|
112
134
|
if recv == nil || node == nil {
|
|
113
135
|
return nil
|
|
@@ -115,6 +137,8 @@ func Checker_getTargetOfImportSpecifier(recv *innerchecker.Checker, node *innera
|
|
|
115
137
|
return checkerGetTargetOfImportSpecifier(recv, node)
|
|
116
138
|
}
|
|
117
139
|
|
|
140
|
+
// Checker_getAliasedSymbol follows an alias chain to its final target symbol.
|
|
141
|
+
// Returns nil if recv or symbol is nil.
|
|
118
142
|
func Checker_getAliasedSymbol(recv *innerchecker.Checker, symbol *innerast.Symbol) *innerast.Symbol {
|
|
119
143
|
if recv == nil || symbol == nil {
|
|
120
144
|
return nil
|
|
@@ -132,6 +156,11 @@ func checkerResolveEntityName(
|
|
|
132
156
|
location *innerast.Node,
|
|
133
157
|
) *innerast.Symbol
|
|
134
158
|
|
|
159
|
+
// Checker_resolveEntityName resolves a dotted entity name (identifier or
|
|
160
|
+
// qualified name) to the symbol it denotes, filtered by meaning flags.
|
|
161
|
+
// When ignoreErrors is true, resolution failures are silent. When
|
|
162
|
+
// dontResolveAlias is true, the returned symbol may still be an alias.
|
|
163
|
+
// Returns nil if recv or name is nil.
|
|
135
164
|
func Checker_resolveEntityName(
|
|
136
165
|
recv *innerchecker.Checker,
|
|
137
166
|
name *innerast.Node,
|
|
@@ -149,6 +178,9 @@ func Checker_resolveEntityName(
|
|
|
149
178
|
//go:linkname checkerGetTypeNameSymbol github.com/microsoft/typescript-go/internal/checker.getTypeNameSymbol
|
|
150
179
|
func checkerGetTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol
|
|
151
180
|
|
|
181
|
+
// Type_getTypeNameSymbol returns the symbol attached to t's type name field,
|
|
182
|
+
// or nil when t has no name symbol or t is nil. Linked via go:linkname because
|
|
183
|
+
// getTypeNameSymbol is a package-level unexported function in the checker.
|
|
152
184
|
func Type_getTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol {
|
|
153
185
|
if t == nil {
|
|
154
186
|
return nil
|
|
@@ -159,6 +191,7 @@ func Type_getTypeNameSymbol(t *innerchecker.Type) *innerast.Symbol {
|
|
|
159
191
|
//go:linkname checkerIsArrayType github.com/microsoft/typescript-go/internal/checker.(*Checker).isArrayType
|
|
160
192
|
func checkerIsArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool
|
|
161
193
|
|
|
194
|
+
// Checker_isArrayType reports whether t is the built-in Array<T> reference type.
|
|
162
195
|
func Checker_isArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool {
|
|
163
196
|
return checkerIsArrayType(recv, t)
|
|
164
197
|
}
|
|
@@ -166,6 +199,8 @@ func Checker_isArrayType(recv *innerchecker.Checker, t *innerchecker.Type) bool
|
|
|
166
199
|
//go:linkname checkerGetBaseTypes github.com/microsoft/typescript-go/internal/checker.(*Checker).getBaseTypes
|
|
167
200
|
func checkerGetBaseTypes(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type
|
|
168
201
|
|
|
202
|
+
// Checker_getBaseTypes returns the list of base types (from `extends` clauses)
|
|
203
|
+
// for a class or interface type. Returns nil if recv or t is nil.
|
|
169
204
|
func Checker_getBaseTypes(recv *innerchecker.Checker, t *innerchecker.Type) []*innerchecker.Type {
|
|
170
205
|
if recv == nil || t == nil {
|
|
171
206
|
return nil
|
|
@@ -1,17 +1,34 @@
|
|
|
1
|
+
// Package core re-exports the subset of typescript-go's internal/core types
|
|
2
|
+
// and constants that plugins and the ttsc driver need. It provides compiler
|
|
3
|
+
// options, script-kind discrimination, tri-state booleans, and text-range
|
|
4
|
+
// primitives without exposing the full internal surface.
|
|
1
5
|
package core
|
|
2
6
|
|
|
3
7
|
import innercore "github.com/microsoft/typescript-go/internal/core"
|
|
4
8
|
|
|
9
|
+
// CompilerOptions holds the parsed tsconfig compiler options passed to the
|
|
10
|
+
// TypeScript-Go program host.
|
|
5
11
|
type CompilerOptions = innercore.CompilerOptions
|
|
12
|
+
|
|
13
|
+
// Tristate is a three-valued boolean: TSFalse, TSTrue, or TSUnknown. Used by
|
|
14
|
+
// CompilerOptions fields that can be explicitly unset.
|
|
6
15
|
type Tristate = innercore.Tristate
|
|
16
|
+
|
|
17
|
+
// TextPos is a zero-based byte offset into a source file's text.
|
|
7
18
|
type TextPos = innercore.TextPos
|
|
19
|
+
|
|
20
|
+
// TextRange is a half-open [Pos, End) byte range inside a source file.
|
|
8
21
|
type TextRange = innercore.TextRange
|
|
22
|
+
|
|
23
|
+
// ScriptKind identifies the syntactic flavour of a source file.
|
|
9
24
|
type ScriptKind = innercore.ScriptKind
|
|
10
25
|
|
|
11
26
|
const (
|
|
27
|
+
// TSFalse and TSTrue are the explicit-false and explicit-true Tristate values.
|
|
12
28
|
TSFalse = innercore.TSFalse
|
|
13
29
|
TSTrue = innercore.TSTrue
|
|
14
30
|
|
|
31
|
+
// ScriptKind* constants enumerate the file flavours typescript-go recognises.
|
|
15
32
|
ScriptKindUnknown = innercore.ScriptKindUnknown
|
|
16
33
|
ScriptKindJS = innercore.ScriptKindJS
|
|
17
34
|
ScriptKindJSX = innercore.ScriptKindJSX
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// Package diagnosticwriter re-exports the subset of typescript-go's
|
|
2
|
+
// internal/diagnosticwriter surface that the ttsc driver uses. It provides
|
|
3
|
+
// FormatASTDiagnosticsWithColorAndContext for rendering pure tsgo diagnostics
|
|
4
|
+
// and delegates the mixed lint+tsgo rendering path to the adjacent lint.go.
|
|
1
5
|
package diagnosticwriter
|
|
2
6
|
|
|
3
7
|
import (
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
package lsp
|
|
9
9
|
|
|
10
10
|
import (
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
"io"
|
|
12
|
+
_ "unsafe"
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
innerlsp "github.com/microsoft/typescript-go/internal/lsp"
|
|
15
15
|
)
|
|
16
16
|
|
|
17
17
|
// Server is the opaque LSP server type from tsgo.
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// Package printer re-exports the typescript-go internal/printer types and
|
|
2
|
+
// wraps the three functions that the ttsc driver and transform plugins need
|
|
3
|
+
// to emit source text from an AST. The surface is intentionally narrow: only
|
|
4
|
+
// the construction and single-file emit path are exposed.
|
|
1
5
|
package printer
|
|
2
6
|
|
|
3
7
|
import (
|
|
@@ -5,19 +9,35 @@ import (
|
|
|
5
9
|
innerprinter "github.com/microsoft/typescript-go/internal/printer"
|
|
6
10
|
)
|
|
7
11
|
|
|
12
|
+
// PrintHandlers provides optional hooks (e.g. substituteNode) called during
|
|
13
|
+
// AST emission.
|
|
8
14
|
type PrintHandlers = innerprinter.PrintHandlers
|
|
15
|
+
|
|
16
|
+
// Printer holds the stateful emitter produced by NewPrinter.
|
|
9
17
|
type Printer = innerprinter.Printer
|
|
18
|
+
|
|
19
|
+
// PrinterOptions configures newline style, source-map generation, and other
|
|
20
|
+
// emission knobs.
|
|
10
21
|
type PrinterOptions = innerprinter.PrinterOptions
|
|
22
|
+
|
|
23
|
+
// EmitContext accumulates per-emit metadata (source maps, comment positions)
|
|
24
|
+
// and must be created fresh for each emit round via NewEmitContext.
|
|
11
25
|
type EmitContext = innerprinter.EmitContext
|
|
12
26
|
|
|
27
|
+
// NewPrinter creates an emitter with the supplied options, substitution hooks,
|
|
28
|
+
// and emit context. Callers must pass the same EmitContext to all operations
|
|
29
|
+
// in a single emit round.
|
|
13
30
|
func NewPrinter(options PrinterOptions, handlers PrintHandlers, emitContext *EmitContext) *Printer {
|
|
14
31
|
return innerprinter.NewPrinter(options, handlers, emitContext)
|
|
15
32
|
}
|
|
16
33
|
|
|
34
|
+
// NewEmitContext allocates a fresh EmitContext for a new emit round.
|
|
17
35
|
func NewEmitContext() *EmitContext {
|
|
18
36
|
return innerprinter.NewEmitContext()
|
|
19
37
|
}
|
|
20
38
|
|
|
39
|
+
// EmitSourceFile renders the full source file through the printer and returns
|
|
40
|
+
// the emitted text.
|
|
21
41
|
func EmitSourceFile(p *Printer, sourceFile *ast.SourceFile) string {
|
|
22
42
|
return p.EmitSourceFile(sourceFile)
|
|
23
43
|
}
|
package/src/MemFS.ts
CHANGED
|
@@ -20,6 +20,15 @@ const S_IFREG = 0o100000;
|
|
|
20
20
|
const DEFAULT_FILE_MODE = S_IFREG | 0o644;
|
|
21
21
|
const DEFAULT_DIR_MODE = S_IFDIR | 0o755;
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Subset of the Node.js `fs` module that `wasm_exec.js` calls into.
|
|
25
|
+
*
|
|
26
|
+
* Go's js/wasm runtime routes all `syscall/js` filesystem operations through
|
|
27
|
+
* `globalThis.fs`. In a browser there is no real `fs`, so a MemFS
|
|
28
|
+
* implementation fulfils this interface. Only the operations that
|
|
29
|
+
* typescript-go's compiler exercises are required; the rest are no-ops or
|
|
30
|
+
* return `EPERM`/`EINVAL`.
|
|
31
|
+
*/
|
|
23
32
|
export interface IWasmExecFS {
|
|
24
33
|
constants: Record<string, number>;
|
|
25
34
|
writeSync(fd: number, buf: Uint8Array): number;
|
|
@@ -37,7 +46,10 @@ export interface IWasmExecFS {
|
|
|
37
46
|
mode: number,
|
|
38
47
|
callback: (err: NodeJS.ErrnoException | null, fd: number) => void,
|
|
39
48
|
): void;
|
|
40
|
-
close(
|
|
49
|
+
close(
|
|
50
|
+
fd: number,
|
|
51
|
+
callback: (err: NodeJS.ErrnoException | null) => void,
|
|
52
|
+
): void;
|
|
41
53
|
read(
|
|
42
54
|
fd: number,
|
|
43
55
|
buffer: Uint8Array,
|
|
@@ -67,7 +79,10 @@ export interface IWasmExecFS {
|
|
|
67
79
|
fd: number,
|
|
68
80
|
callback: (err: NodeJS.ErrnoException | null, stats: IFileStats) => void,
|
|
69
81
|
): void;
|
|
70
|
-
fsync(
|
|
82
|
+
fsync(
|
|
83
|
+
fd: number,
|
|
84
|
+
callback: (err: NodeJS.ErrnoException | null) => void,
|
|
85
|
+
): void;
|
|
71
86
|
unlink(
|
|
72
87
|
path: string,
|
|
73
88
|
callback: (err: NodeJS.ErrnoException | null) => void,
|
|
@@ -152,6 +167,12 @@ export interface IWasmExecFS {
|
|
|
152
167
|
): void;
|
|
153
168
|
}
|
|
154
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Stat-like object returned by `stat`, `lstat`, and `fstat`.
|
|
172
|
+
*
|
|
173
|
+
* Mirrors the subset of `fs.Stats` that `wasm_exec.js` reads. Fields not
|
|
174
|
+
* relevant to Go's `os.FileInfo` (e.g. ownership) are zeroed.
|
|
175
|
+
*/
|
|
155
176
|
export interface IFileStats {
|
|
156
177
|
isDirectory(): boolean;
|
|
157
178
|
isFile(): boolean;
|
|
@@ -170,12 +191,19 @@ export interface IFileStats {
|
|
|
170
191
|
blocks: number;
|
|
171
192
|
}
|
|
172
193
|
|
|
194
|
+
/** Internal filesystem tree node. Directories carry an empty `data` buffer. */
|
|
173
195
|
interface INode {
|
|
174
196
|
kind: "file" | "dir";
|
|
175
197
|
data: Uint8Array;
|
|
176
198
|
mtimeMs: number;
|
|
177
199
|
}
|
|
178
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Filesystem error with a POSIX error code and numeric `errno`.
|
|
203
|
+
*
|
|
204
|
+
* Matches the shape of `NodeJS.ErrnoException` so Go's os package interprets it
|
|
205
|
+
* as a proper `os.PathError` with a numeric error code.
|
|
206
|
+
*/
|
|
179
207
|
export class MemFSError extends Error {
|
|
180
208
|
public code: string;
|
|
181
209
|
public errno: number;
|
|
@@ -190,6 +218,7 @@ export class MemFSError extends Error {
|
|
|
190
218
|
}
|
|
191
219
|
}
|
|
192
220
|
|
|
221
|
+
/** Map a POSIX error name to its Linux numeric errno (negative by convention). */
|
|
193
222
|
function errnoForCode(code: string): number {
|
|
194
223
|
switch (code) {
|
|
195
224
|
case "ENOENT":
|
|
@@ -207,6 +236,11 @@ function errnoForCode(code: string): number {
|
|
|
207
236
|
}
|
|
208
237
|
}
|
|
209
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Handle returned by `createMemFS`. Provides the `fs` shim to install on
|
|
241
|
+
* `globalThis` plus convenience methods for seeding the virtual filesystem
|
|
242
|
+
* before booting the wasm.
|
|
243
|
+
*/
|
|
210
244
|
export interface IMemFSHost {
|
|
211
245
|
fs: IWasmExecFS;
|
|
212
246
|
writeFile(path: string, data: string | Uint8Array): void;
|
|
@@ -222,6 +256,12 @@ export interface IMemFSHost {
|
|
|
222
256
|
const encoder = new TextEncoder();
|
|
223
257
|
const decoder = new TextDecoder();
|
|
224
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Resolve a path to an absolute, normalized POSIX path.
|
|
261
|
+
*
|
|
262
|
+
* Collapses `.` and `..` segments and converts backslashes. Returns `"/"` for
|
|
263
|
+
* empty input.
|
|
264
|
+
*/
|
|
225
265
|
function normalize(p: string): string {
|
|
226
266
|
if (!p) return "/";
|
|
227
267
|
const parts = p.replace(/\\/g, "/").split("/").filter(Boolean);
|
|
@@ -237,6 +277,15 @@ function normalize(p: string): string {
|
|
|
237
277
|
return "/" + stack.join("/");
|
|
238
278
|
}
|
|
239
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Create an in-memory filesystem suitable for use as `globalThis.fs` inside a
|
|
282
|
+
* Go/wasm runtime.
|
|
283
|
+
*
|
|
284
|
+
* The returned host exposes the low-level `fs` object (install it on
|
|
285
|
+
* `globalThis.fs` before loading `wasm_exec.js`) and convenience helpers
|
|
286
|
+
* (`writeFile`, `readFile`, `mkdirp`, …) for seeding source files and reading
|
|
287
|
+
* compiler output without touching the real filesystem.
|
|
288
|
+
*/
|
|
240
289
|
export function createMemFS(): IMemFSHost {
|
|
241
290
|
const nodes = new Map<string, INode>();
|
|
242
291
|
nodes.set("/", { kind: "dir", data: new Uint8Array(), mtimeMs: Date.now() });
|
|
@@ -271,6 +320,10 @@ export function createMemFS(): IMemFSHost {
|
|
|
271
320
|
}
|
|
272
321
|
const pipes = new Map<number, IPipeState>();
|
|
273
322
|
|
|
323
|
+
/**
|
|
324
|
+
* Drain buffered pipe data into `buffer[offset..offset+length]`. Returns
|
|
325
|
+
* bytes copied.
|
|
326
|
+
*/
|
|
274
327
|
function drainPipeInto(
|
|
275
328
|
state: IPipeState,
|
|
276
329
|
buffer: Uint8Array,
|
|
@@ -289,19 +342,29 @@ export function createMemFS(): IMemFSHost {
|
|
|
289
342
|
return written;
|
|
290
343
|
}
|
|
291
344
|
|
|
345
|
+
/**
|
|
346
|
+
* Satisfy any pending blocked readers using available pipe data or EOF
|
|
347
|
+
* signal.
|
|
348
|
+
*/
|
|
292
349
|
function flushPipeReaders(state: IPipeState): void {
|
|
293
350
|
while (
|
|
294
351
|
state.pendingReaders.length > 0 &&
|
|
295
352
|
(state.buffers.length > 0 || state.writeClosed)
|
|
296
353
|
) {
|
|
297
354
|
const reader = state.pendingReaders.shift()!;
|
|
298
|
-
const n = drainPipeInto(
|
|
355
|
+
const n = drainPipeInto(
|
|
356
|
+
state,
|
|
357
|
+
reader.buffer,
|
|
358
|
+
reader.offset,
|
|
359
|
+
reader.length,
|
|
360
|
+
);
|
|
299
361
|
// Even at EOF (writeClosed && empty buffers) we satisfy with n=0 which
|
|
300
362
|
// signals EOF to the Go-side caller.
|
|
301
363
|
reader.callback(null, n);
|
|
302
364
|
}
|
|
303
365
|
}
|
|
304
366
|
|
|
367
|
+
/** Silently create any missing ancestor directories for path `p`. */
|
|
305
368
|
function ensureParentDirs(p: string): void {
|
|
306
369
|
const segments = normalize(p).split("/").filter(Boolean);
|
|
307
370
|
segments.pop();
|
|
@@ -358,6 +421,7 @@ export function createMemFS(): IMemFSHost {
|
|
|
358
421
|
return nodes.has(normalize(p));
|
|
359
422
|
}
|
|
360
423
|
|
|
424
|
+
/** Synchronously stat `p`; throws `MemFSError("ENOENT")` if not found. */
|
|
361
425
|
function statSync(p: string): IFileStats {
|
|
362
426
|
const norm = normalize(p);
|
|
363
427
|
const node = nodes.get(norm);
|
|
@@ -365,6 +429,7 @@ export function createMemFS(): IMemFSHost {
|
|
|
365
429
|
return makeStats(node);
|
|
366
430
|
}
|
|
367
431
|
|
|
432
|
+
/** Build an `IFileStats` object from a filesystem node. */
|
|
368
433
|
function makeStats(node: INode): IFileStats {
|
|
369
434
|
const isDir = node.kind === "dir";
|
|
370
435
|
return {
|
|
@@ -386,6 +451,12 @@ export function createMemFS(): IMemFSHost {
|
|
|
386
451
|
};
|
|
387
452
|
}
|
|
388
453
|
|
|
454
|
+
/**
|
|
455
|
+
* Return immediate children of directory `p`, sorted alphabetically.
|
|
456
|
+
*
|
|
457
|
+
* Uses a linear scan over the node map and a `Set` to deduplicate nested
|
|
458
|
+
* paths into direct-child names — O(n) in the number of total nodes.
|
|
459
|
+
*/
|
|
389
460
|
function readdirSync(p: string): string[] {
|
|
390
461
|
const norm = normalize(p);
|
|
391
462
|
const node = nodes.get(norm);
|
|
@@ -438,9 +509,12 @@ export function createMemFS(): IMemFSHost {
|
|
|
438
509
|
if (entry) {
|
|
439
510
|
const node = nodes.get(entry.path);
|
|
440
511
|
if (node && node.kind === "file") {
|
|
512
|
+
// subarray(0) is a zero-copy view over the full incoming buffer.
|
|
441
513
|
const incoming = buf.subarray(0);
|
|
442
514
|
const existing = node.data;
|
|
443
|
-
const next = new Uint8Array(
|
|
515
|
+
const next = new Uint8Array(
|
|
516
|
+
existing.byteLength + incoming.byteLength,
|
|
517
|
+
);
|
|
444
518
|
next.set(existing, 0);
|
|
445
519
|
next.set(incoming, existing.byteLength);
|
|
446
520
|
node.data = next;
|
|
@@ -455,7 +529,11 @@ export function createMemFS(): IMemFSHost {
|
|
|
455
529
|
stderr.buffer += decoder.decode(buf);
|
|
456
530
|
// eslint-disable-next-line no-console
|
|
457
531
|
console.error(
|
|
458
|
-
"[wasm] writeSync to unknown fd " +
|
|
532
|
+
"[wasm] writeSync to unknown fd " +
|
|
533
|
+
fd +
|
|
534
|
+
" (" +
|
|
535
|
+
buf.byteLength +
|
|
536
|
+
" bytes); routed to stderr buffer",
|
|
459
537
|
);
|
|
460
538
|
return buf.length;
|
|
461
539
|
},
|
|
@@ -599,7 +677,10 @@ export function createMemFS(): IMemFSHost {
|
|
|
599
677
|
try {
|
|
600
678
|
callback(null, statSync(p));
|
|
601
679
|
} catch (err) {
|
|
602
|
-
callback(
|
|
680
|
+
callback(
|
|
681
|
+
err as NodeJS.ErrnoException,
|
|
682
|
+
undefined as unknown as IFileStats,
|
|
683
|
+
);
|
|
603
684
|
}
|
|
604
685
|
},
|
|
605
686
|
|
|
@@ -614,7 +695,11 @@ export function createMemFS(): IMemFSHost {
|
|
|
614
695
|
if (pipes.has(fd)) {
|
|
615
696
|
callback(
|
|
616
697
|
null,
|
|
617
|
-
makeStats({
|
|
698
|
+
makeStats({
|
|
699
|
+
kind: "file",
|
|
700
|
+
data: new Uint8Array(),
|
|
701
|
+
mtimeMs: Date.now(),
|
|
702
|
+
}),
|
|
618
703
|
);
|
|
619
704
|
return;
|
|
620
705
|
}
|
package/src/api.ts
CHANGED
|
@@ -3,28 +3,40 @@
|
|
|
3
3
|
// `playground.wasm`, typia's `ttsc-typia.wasm`) exposes the same surface, so
|
|
4
4
|
// one set of types covers them all.
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* API object that every host-built wasm binds to `globalThis[apiName]`.
|
|
8
|
+
*
|
|
9
|
+
* Obtain an instance via `bootTtsc`, which waits for the wasm to signal
|
|
10
|
+
* readiness and returns the typed handle together with its `IMemFSHost`.
|
|
11
|
+
*/
|
|
6
12
|
export interface ITtscApi {
|
|
7
13
|
/** Build metadata reported by the wasm. Useful for diagnostics. */
|
|
8
14
|
version(): ITtscVersion;
|
|
9
15
|
|
|
10
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Compile a project: typecheck + emit. `result` is JSON;
|
|
18
|
+
* `parseResult<ITtscCompileResult>` deserializes it.
|
|
19
|
+
*/
|
|
11
20
|
build(opts: ITtscBuildOpts): Promise<ITtscResult>;
|
|
12
21
|
|
|
13
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Typecheck without emit. `result` is JSON; `parseResult<ITtscCompileResult>`
|
|
24
|
+
* deserializes it.
|
|
25
|
+
*/
|
|
14
26
|
check(opts: ITtscBuildOpts): Promise<ITtscResult>;
|
|
15
27
|
|
|
16
28
|
/**
|
|
17
|
-
* Return every source file the program saw, keyed by project-relative
|
|
18
|
-
*
|
|
19
|
-
*
|
|
29
|
+
* Return every source file the program saw, keyed by project-relative path.
|
|
30
|
+
* Used by playgrounds that want to render the TypeScript view after a source
|
|
31
|
+
* rewriter (e.g. paths) has run. `result` is JSON; use
|
|
20
32
|
* `parseResult<ITtscTransformResult>` to deserialize.
|
|
21
33
|
*/
|
|
22
34
|
transform(opts: ITtscBuildOpts): Promise<ITtscResult>;
|
|
23
35
|
|
|
24
36
|
/**
|
|
25
37
|
* Dispatch a registered plugin's subcommand. Returns the captured stdout /
|
|
26
|
-
* stderr (the same streams the native sidecar binary would write to)
|
|
27
|
-
*
|
|
38
|
+
* stderr (the same streams the native sidecar binary would write to) together
|
|
39
|
+
* with the exit code.
|
|
28
40
|
*/
|
|
29
41
|
plugin(opts: ITtscPluginOpts): Promise<ITtscResult>;
|
|
30
42
|
|
|
@@ -32,6 +44,7 @@ export interface ITtscApi {
|
|
|
32
44
|
plugins(): string[];
|
|
33
45
|
}
|
|
34
46
|
|
|
47
|
+
/** Build metadata embedded in the wasm by the Go linker at compile time. */
|
|
35
48
|
export interface ITtscVersion {
|
|
36
49
|
version: string;
|
|
37
50
|
commit: string;
|
|
@@ -41,13 +54,15 @@ export interface ITtscVersion {
|
|
|
41
54
|
goarch: string;
|
|
42
55
|
}
|
|
43
56
|
|
|
57
|
+
/** Options shared by `build`, `check`, and `transform`. */
|
|
44
58
|
export interface ITtscBuildOpts {
|
|
45
59
|
/** Absolute virtual path the project lives at inside the MemFS. */
|
|
46
60
|
cwd: string;
|
|
47
|
-
/**
|
|
61
|
+
/** Tsconfig path, relative to `cwd`. Defaults to `tsconfig.json`. */
|
|
48
62
|
tsconfig?: string;
|
|
49
63
|
}
|
|
50
64
|
|
|
65
|
+
/** Options for dispatching a named plugin subcommand via `api.plugin`. */
|
|
51
66
|
export interface ITtscPluginOpts {
|
|
52
67
|
/** Plugin id registered with `host.Expose` (e.g. `@ttsc/banner`). */
|
|
53
68
|
name: string;
|
|
@@ -61,6 +76,7 @@ export interface ITtscPluginOpts {
|
|
|
61
76
|
[key: string]: string | boolean | number | undefined;
|
|
62
77
|
}
|
|
63
78
|
|
|
79
|
+
/** Envelope returned by every `ITtscApi` method. */
|
|
64
80
|
export interface ITtscResult {
|
|
65
81
|
/** Exit code. 0 = success, 2 = usage error, 3 = runtime error. */
|
|
66
82
|
code: number;
|
|
@@ -69,23 +85,40 @@ export interface ITtscResult {
|
|
|
69
85
|
/** Anything the wasm wrote to its stderr stream. */
|
|
70
86
|
stderr: string;
|
|
71
87
|
/**
|
|
72
|
-
* For the base endpoints, the JSON-encoded compile/transform result. For
|
|
73
|
-
*
|
|
88
|
+
* For the base endpoints, the JSON-encoded compile/transform result. For the
|
|
89
|
+
* plugin endpoint, this is empty — the plugin's own output sits in
|
|
74
90
|
* stdout/stderr. Use `parseResult<T>` to deserialize.
|
|
75
91
|
*/
|
|
76
92
|
result: string;
|
|
77
93
|
}
|
|
78
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Structured payload inside `ITtscResult.result` for `build` and `check`.
|
|
97
|
+
*
|
|
98
|
+
* `output` maps emit-destination paths (relative to `outDir`) to file contents.
|
|
99
|
+
* It is empty when `check` is called without emit.
|
|
100
|
+
*/
|
|
79
101
|
export interface ITtscCompileResult {
|
|
80
102
|
diagnostics?: ITtscDiagnostic[];
|
|
81
103
|
output: Record<string, string>;
|
|
82
104
|
}
|
|
83
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Structured payload inside `ITtscResult.result` for `transform`.
|
|
108
|
+
*
|
|
109
|
+
* `typescript` maps source file paths (relative to `cwd`) to their
|
|
110
|
+
* post-transform TypeScript text — useful for playgrounds that want to show the
|
|
111
|
+
* rewritten source before it is emitted.
|
|
112
|
+
*/
|
|
84
113
|
export interface ITtscTransformResult {
|
|
85
114
|
diagnostics?: ITtscDiagnostic[];
|
|
86
115
|
typescript: Record<string, string>;
|
|
87
116
|
}
|
|
88
117
|
|
|
118
|
+
/**
|
|
119
|
+
* A single TypeScript compiler diagnostic emitted during `build`, `check`, or
|
|
120
|
+
* `transform`. `line` and `character` are 0-based.
|
|
121
|
+
*/
|
|
89
122
|
export interface ITtscDiagnostic {
|
|
90
123
|
file: string | null;
|
|
91
124
|
category: "error" | "warning";
|
|
@@ -98,9 +131,9 @@ export interface ITtscDiagnostic {
|
|
|
98
131
|
}
|
|
99
132
|
|
|
100
133
|
/**
|
|
101
|
-
* Parse the `result` field of an ITtscResult into the structured payload.
|
|
102
|
-
*
|
|
103
|
-
*
|
|
134
|
+
* Parse the `result` field of an ITtscResult into the structured payload. The
|
|
135
|
+
* wasm returns JSON as a string because js.ValueOf does not handle large nested
|
|
136
|
+
* maps efficiently. Callers JSON.parse exactly once at the boundary.
|
|
104
137
|
*/
|
|
105
138
|
export function parseResult<T>(result: ITtscResult): T | null {
|
|
106
139
|
if (!result.result) return null;
|
package/src/index.ts
CHANGED
|
@@ -9,11 +9,7 @@ export { bootTtsc } from "./instantiate";
|
|
|
9
9
|
export type { IBootTtscOptions, IBootResult } from "./instantiate";
|
|
10
10
|
|
|
11
11
|
export { createMemFS, MemFSError } from "./MemFS";
|
|
12
|
-
export type {
|
|
13
|
-
IMemFSHost,
|
|
14
|
-
IWasmExecFS,
|
|
15
|
-
IFileStats,
|
|
16
|
-
} from "./MemFS";
|
|
12
|
+
export type { IMemFSHost, IWasmExecFS, IFileStats } from "./MemFS";
|
|
17
13
|
|
|
18
14
|
export type {
|
|
19
15
|
ITtscApi,
|