typenative 0.3.1 → 0.4.1
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/.github/workflows/ci.yml +2 -0
- package/CHANGELOG.md +258 -0
- package/CONTRIBUTION.md +1 -1
- package/README.md +17 -1
- package/bin/cli.js +26 -11
- package/bin/go/bridge.js +74 -14
- package/bin/go/convert.js +59 -1
- package/bin/go/metadata.js +13 -4
- package/bin/go/overrides.js +4 -6
- package/bin/go/stdlib.generated.js +2 -0
- package/bin/index.js +338 -47
- package/bin/main.js +36 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/child_process.js +51 -3
- package/bin/node/cluster.js +41 -3
- package/bin/node/core.js +1 -1
- package/bin/node/crypto.js +205 -4
- package/bin/node/dgram.js +230 -6
- package/bin/node/dns.js +471 -8
- package/bin/node/errno_tables.js +445 -0
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +22 -15
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http.js +128 -30
- package/bin/node/http2.js +37 -7
- package/bin/node/module_v8.js +142 -0
- package/bin/node/net.js +9 -6
- package/bin/node/process.js +242 -0
- package/bin/node/readline.js +272 -1
- package/bin/node/registry.js +3 -1
- package/bin/node/sqlite.js +60 -12
- package/bin/node/stream.js +145 -10
- package/bin/node/test.js +311 -19
- package/bin/node/timers.js +1 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/url.js +23 -5
- package/bin/node/util.js +21 -1
- package/bin/node/util_extra.js +6 -30
- package/bin/node/worker_threads.js +79 -19
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +4729 -616
- package/bin/typecheck-node.js +1 -0
- package/bin/version.js +1 -1
- package/conformance.md +1030 -877
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +127 -0
- package/examples/json-server/bench.mjs +263 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-go.d.ts +23 -0
- package/types/typenative-npm.d.ts +241 -5
- package/types/typenative.d.ts +302 -12
package/bin/index.js
CHANGED
|
@@ -9,8 +9,140 @@ import { platform } from 'node:os';
|
|
|
9
9
|
// from Node's; under Node these functions are never called (the CLI wrapper does
|
|
10
10
|
// not reach them), so they're accessed through `any` casts for type-checking.
|
|
11
11
|
import * as childProcess from 'node:child_process';
|
|
12
|
-
import { transpileToNative } from './transpiler.js';
|
|
12
|
+
import { transpileToNative, GENERATED_HEADER } from './transpiler.js';
|
|
13
13
|
import { VERSION } from './version.js';
|
|
14
|
+
// The minimum Go toolchain generated code needs
|
|
15
|
+
const MIN_GO_MAJOR = 1;
|
|
16
|
+
const MIN_GO_MINOR = 26;
|
|
17
|
+
export function usage() {
|
|
18
|
+
return `Usage: typenative [options] [file.ts]
|
|
19
|
+
|
|
20
|
+
Compiles a TypeScript program to a native executable (through Go).
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--source <file> The program's entry file (or pass it as the last argument)
|
|
24
|
+
--output <file> Where to write the executable
|
|
25
|
+
--script Build and run the program (prompts for code without --source)
|
|
26
|
+
--new Create a new TypeNative project
|
|
27
|
+
--target <os/arch> Cross-compile, e.g. linux/amd64, linux/arm64, darwin/arm64,
|
|
28
|
+
windows/amd64 ('go tool dist list' shows every pair)
|
|
29
|
+
--release Smaller binary: drop symbols and file paths
|
|
30
|
+
--cover Build with coverage counters, for go:runtime/coverage
|
|
31
|
+
(set GOCOVERDIR to also write a profile at exit)
|
|
32
|
+
--emit-go <dir> Write the generated Go (and go.mod) to <dir> instead of building
|
|
33
|
+
--no-typecheck Skip type-checking
|
|
34
|
+
-h, --help Show this help
|
|
35
|
+
-v, --version Show the TypeNative version
|
|
36
|
+
|
|
37
|
+
Examples:
|
|
38
|
+
typenative --source main.ts --output bin/app
|
|
39
|
+
typenative main.ts --script
|
|
40
|
+
typenative --source main.ts --output app-linux --target linux/arm64 --release`;
|
|
41
|
+
}
|
|
42
|
+
// Parses argv (without the executable and script): --flag value and
|
|
43
|
+
// --flag=value; one bare argument is the entry file
|
|
44
|
+
export function parseCliArgs(argv) {
|
|
45
|
+
const args = {
|
|
46
|
+
help: false,
|
|
47
|
+
version: false,
|
|
48
|
+
newProject: false,
|
|
49
|
+
script: false,
|
|
50
|
+
noTypeCheck: false,
|
|
51
|
+
release: false,
|
|
52
|
+
cover: false,
|
|
53
|
+
source: null,
|
|
54
|
+
output: null,
|
|
55
|
+
target: null,
|
|
56
|
+
emitGo: null,
|
|
57
|
+
error: ''
|
|
58
|
+
};
|
|
59
|
+
for (let i = 0; i < argv.length; i++) {
|
|
60
|
+
let arg = argv[i];
|
|
61
|
+
let value = null;
|
|
62
|
+
const eq = arg.indexOf('=');
|
|
63
|
+
if (arg.startsWith('--') && eq > 0) {
|
|
64
|
+
value = arg.substring(eq + 1);
|
|
65
|
+
arg = arg.substring(0, eq);
|
|
66
|
+
}
|
|
67
|
+
if (arg === '-h' || arg === '--help')
|
|
68
|
+
args.help = true;
|
|
69
|
+
else if (arg === '-v' || arg === '--version')
|
|
70
|
+
args.version = true;
|
|
71
|
+
else if (arg === '--new')
|
|
72
|
+
args.newProject = true;
|
|
73
|
+
else if (arg === '--script')
|
|
74
|
+
args.script = true;
|
|
75
|
+
else if (arg === '--no-typecheck')
|
|
76
|
+
args.noTypeCheck = true;
|
|
77
|
+
else if (arg === '--release')
|
|
78
|
+
args.release = true;
|
|
79
|
+
else if (arg === '--cover')
|
|
80
|
+
args.cover = true;
|
|
81
|
+
else if (arg === '--source' || arg === '--output' || arg === '--target' || arg === '--emit-go') {
|
|
82
|
+
if (value === null) {
|
|
83
|
+
if (i + 1 >= argv.length || argv[i + 1].startsWith('--')) {
|
|
84
|
+
args.error = `${arg} needs a value`;
|
|
85
|
+
return args;
|
|
86
|
+
}
|
|
87
|
+
i++;
|
|
88
|
+
value = argv[i];
|
|
89
|
+
}
|
|
90
|
+
if (arg === '--source')
|
|
91
|
+
args.source = value;
|
|
92
|
+
else if (arg === '--output')
|
|
93
|
+
args.output = value;
|
|
94
|
+
else if (arg === '--target')
|
|
95
|
+
args.target = value;
|
|
96
|
+
else
|
|
97
|
+
args.emitGo = value;
|
|
98
|
+
}
|
|
99
|
+
else if (arg.startsWith('-')) {
|
|
100
|
+
args.error = `Unknown option: ${arg}`;
|
|
101
|
+
return args;
|
|
102
|
+
}
|
|
103
|
+
else if (args.source === null) {
|
|
104
|
+
// (a typed local: the self-hosted build boxes it into the string | null field)
|
|
105
|
+
const entry = arg;
|
|
106
|
+
args.source = entry;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
args.error = `Unexpected argument: ${arg}`;
|
|
110
|
+
return args;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (args.target !== null && !/^[a-z0-9]+\/[a-z0-9]+$/.test(args.target)) {
|
|
114
|
+
args.error = `--target expects os/arch (like linux/arm64), got '${args.target}'`;
|
|
115
|
+
}
|
|
116
|
+
return args;
|
|
117
|
+
}
|
|
118
|
+
// This machine's os/arch, as Go names them
|
|
119
|
+
function hostTarget() {
|
|
120
|
+
const result = runCommand('go', ['env', 'GOHOSTOS', 'GOHOSTARCH']);
|
|
121
|
+
const lines = (result.stdout ?? '').trim().split('\n');
|
|
122
|
+
return lines.length === 2 ? `${lines[0].trim()}/${lines[1].trim()}` : '';
|
|
123
|
+
}
|
|
124
|
+
// Checks the Go toolchain on the PATH (and a --target against the pairs it
|
|
125
|
+
// supports); returns an error message, '' when usable
|
|
126
|
+
export function checkGo(target) {
|
|
127
|
+
const version = runCommand('go', ['version']);
|
|
128
|
+
const found = (version.stdout ?? '').match(/go(\d+)\.(\d+)/);
|
|
129
|
+
if (version.status !== 0 || !found) {
|
|
130
|
+
return `TypeNative needs Go ${MIN_GO_MAJOR}.${MIN_GO_MINOR} or newer on the PATH (https://go.dev/dl/)`;
|
|
131
|
+
}
|
|
132
|
+
const major = Number(found[1]);
|
|
133
|
+
const minor = Number(found[2]);
|
|
134
|
+
if (major < MIN_GO_MAJOR || (major === MIN_GO_MAJOR && minor < MIN_GO_MINOR)) {
|
|
135
|
+
return `TypeNative needs Go ${MIN_GO_MAJOR}.${MIN_GO_MINOR} or newer; found go${major}.${minor} (https://go.dev/dl/)`;
|
|
136
|
+
}
|
|
137
|
+
if (target !== null) {
|
|
138
|
+
const list = runCommand('go', ['tool', 'dist', 'list']);
|
|
139
|
+
const targets = (list.stdout ?? '').split('\n').map((line) => line.trim());
|
|
140
|
+
if (!targets.includes(target)) {
|
|
141
|
+
return `Go can't build for '${target}' ('go tool dist list' shows the os/arch pairs it supports)`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
14
146
|
// A source path as diagnostics and Go line directives show it: relative to
|
|
15
147
|
// the working directory when inside it, else absolute; forward slashes
|
|
16
148
|
function displayPath(file) {
|
|
@@ -29,11 +161,11 @@ function plural(count, noun) {
|
|
|
29
161
|
}
|
|
30
162
|
// golang.org/x/text's transform and unicode/norm packages (String.normalize),
|
|
31
163
|
// copied from GOROOT/src/vendor
|
|
32
|
-
function vendorXText() {
|
|
164
|
+
function vendorXText(outDir) {
|
|
33
165
|
const goroot = runCommand('go', ['env', 'GOROOT']).stdout.trim();
|
|
34
166
|
const source = path.join(goroot, 'src', 'vendor', 'golang.org', 'x', 'text');
|
|
35
167
|
for (const pkg of ['transform', path.join('unicode', 'norm')]) {
|
|
36
|
-
const target = path.join(
|
|
168
|
+
const target = path.join(outDir, 'tnvendor', 'x-text', pkg);
|
|
37
169
|
fs.mkdirSync(target, { recursive: true });
|
|
38
170
|
for (const file of fs.readdirSync(path.join(source, pkg))) {
|
|
39
171
|
if (file.endsWith('.go') && !file.endsWith('_test.go')) {
|
|
@@ -41,17 +173,33 @@ function vendorXText() {
|
|
|
41
173
|
}
|
|
42
174
|
}
|
|
43
175
|
}
|
|
44
|
-
fs.writeFileSync(path.join(
|
|
176
|
+
fs.writeFileSync(path.join(outDir, 'tnvendor', 'x-text', 'go.mod'), 'module golang.org/x/text\n\ngo 1.23\n');
|
|
45
177
|
return { path: 'golang.org/x/text', version: 'v0.0.0', dir: './tnvendor/x-text' };
|
|
46
178
|
}
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
179
|
+
// golang.org/x/net's dns/dnsmessage (node:dns record queries), copied from
|
|
180
|
+
// GOROOT/src/vendor
|
|
181
|
+
function vendorXNet(outDir) {
|
|
182
|
+
const goroot = runCommand('go', ['env', 'GOROOT']).stdout.trim();
|
|
183
|
+
const pkg = path.join('dns', 'dnsmessage');
|
|
184
|
+
const source = path.join(goroot, 'src', 'vendor', 'golang.org', 'x', 'net', pkg);
|
|
185
|
+
const target = path.join(outDir, 'tnvendor', 'x-net', pkg);
|
|
186
|
+
fs.mkdirSync(target, { recursive: true });
|
|
187
|
+
for (const file of fs.readdirSync(source)) {
|
|
188
|
+
if (file.endsWith('.go') && !file.endsWith('_test.go')) {
|
|
189
|
+
fs.writeFileSync(path.join(target, file), fs.readFileSync(path.join(source, file), 'utf-8'));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
fs.writeFileSync(path.join(outDir, 'tnvendor', 'x-net', 'go.mod'), 'module golang.org/x/net\n\ngo 1.23\n');
|
|
193
|
+
return { path: 'golang.org/x/net', version: 'v0.0.0', dir: './tnvendor/x-net' };
|
|
194
|
+
}
|
|
195
|
+
// node:sqlite's driver, the Brotli codec, fsnotify and their dependencies,
|
|
196
|
+
// copied from the package's govendor/ (listed in govendor/manifest.txt); the
|
|
197
|
+
// copy is skipped while dist/tnvendor already holds the same version. Returns
|
|
198
|
+
// the modules and the Go version their go.mod files declare.
|
|
199
|
+
function vendorGoModules(vendorDir, outDir) {
|
|
52
200
|
const manifest = fs.readFileSync(path.join(vendorDir, 'manifest.txt'), 'utf-8');
|
|
53
201
|
let version = '';
|
|
54
|
-
let goVersion =
|
|
202
|
+
let goVersion = `${MIN_GO_MAJOR}.${MIN_GO_MINOR}`;
|
|
55
203
|
const modules = [];
|
|
56
204
|
for (const line of manifest.split('\n')) {
|
|
57
205
|
const parts = line.trim().split(' ');
|
|
@@ -63,11 +211,11 @@ function vendorGoModules(vendorDir) {
|
|
|
63
211
|
modules.push({ path: parts[1], version: parts[2], dir: './tnvendor/' + parts[1] });
|
|
64
212
|
}
|
|
65
213
|
}
|
|
66
|
-
const stamp = path.join(
|
|
214
|
+
const stamp = path.join(outDir, 'tnvendor', 'govendor.version');
|
|
67
215
|
const current = fs.existsSync(stamp) ? fs.readFileSync(stamp, 'utf-8') : '';
|
|
68
216
|
if (current !== version) {
|
|
69
217
|
for (const mod of modules) {
|
|
70
|
-
const target = path.join(
|
|
218
|
+
const target = path.join(outDir, 'tnvendor', mod.path);
|
|
71
219
|
fs.rmSync(target, { recursive: true, force: true });
|
|
72
220
|
fs.cpSync(path.join(vendorDir, mod.path), target, { recursive: true });
|
|
73
221
|
}
|
|
@@ -75,14 +223,28 @@ function vendorGoModules(vendorDir) {
|
|
|
75
223
|
}
|
|
76
224
|
return { modules: modules, goVersion: goVersion };
|
|
77
225
|
}
|
|
78
|
-
|
|
226
|
+
// Whether generated Go imports a package of a module in govendor/ (node:sqlite's
|
|
227
|
+
// driver, zlib's Brotli codec, fs.watch's fsnotify)
|
|
228
|
+
function importsVendoredModule(content, vendorDir) {
|
|
229
|
+
const manifest = fs.readFileSync(path.join(vendorDir, 'manifest.txt'), 'utf-8');
|
|
230
|
+
for (const line of manifest.split('\n')) {
|
|
231
|
+
const parts = line.trim().split(' ');
|
|
232
|
+
if (parts[0] !== 'module')
|
|
233
|
+
continue;
|
|
234
|
+
if (content.includes(`"${parts[1]}"`) || content.includes(`"${parts[1]}/`))
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
function writeGoMod(modules, goVersion, outDir) {
|
|
79
240
|
let requires = '';
|
|
80
241
|
let replaces = '';
|
|
81
242
|
for (const mod of modules) {
|
|
82
243
|
requires += `\t${mod.path} ${mod.version}\n`;
|
|
83
244
|
replaces += `\t${mod.path} => ${mod.dir}\n`;
|
|
84
245
|
}
|
|
85
|
-
|
|
246
|
+
const deps = modules.length > 0 ? `\nrequire (\n${requires})\n\nreplace (\n${replaces})\n` : '';
|
|
247
|
+
fs.writeFileSync(path.join(outDir, 'go.mod'), `module tnapp\n\ngo ${goVersion}\n${deps}`);
|
|
86
248
|
}
|
|
87
249
|
// Run a command and capture its output. Uses Node's spawnSync under Node and a
|
|
88
250
|
// Go helper (os/exec) once transpiled — both synchronous, same result shape.
|
|
@@ -117,7 +279,102 @@ export function createProject(projectName, installDependencies) {
|
|
|
117
279
|
console.log('Dependencies installed successfully!');
|
|
118
280
|
}
|
|
119
281
|
}
|
|
282
|
+
// go:embed files are copied next to the Go sources at their paths relative
|
|
283
|
+
// to the entry file; this manifest lists the copies for the next build to remove
|
|
284
|
+
const EMBED_MANIFEST = '.tnembed';
|
|
285
|
+
// A go:embed path from a source file's directory → the pattern relative to
|
|
286
|
+
// rootDir (the entry file's directory), or null when nothing matches or it
|
|
287
|
+
// leaves rootDir. An `all:` prefix (dotfiles too) is kept.
|
|
288
|
+
function resolveEmbedPattern(pattern, fromDir, rootDir) {
|
|
289
|
+
const all = pattern.startsWith('all:');
|
|
290
|
+
const spec = all ? pattern.slice(4) : pattern;
|
|
291
|
+
const rel = path.relative(rootDir, path.resolve(fromDir, spec)).split(path.sep).join('/');
|
|
292
|
+
if (!rel || rel === '..' || rel.startsWith('../') || path.isAbsolute(rel))
|
|
293
|
+
return null;
|
|
294
|
+
if (embedMatches(rootDir, rel).length === 0)
|
|
295
|
+
return null;
|
|
296
|
+
return all ? `all:${rel}` : rel;
|
|
297
|
+
}
|
|
298
|
+
// The paths under rootDir an embed pattern names: itself, or for a glob
|
|
299
|
+
// (* ? [...], in the last element) the matching entries of its directory
|
|
300
|
+
function embedMatches(rootDir, rel) {
|
|
301
|
+
const slash = rel.lastIndexOf('/');
|
|
302
|
+
const dir = slash < 0 ? '' : rel.slice(0, slash);
|
|
303
|
+
const base = rel.slice(slash + 1);
|
|
304
|
+
if (!/[*?[]/.test(base))
|
|
305
|
+
return fs.existsSync(path.join(rootDir, rel)) ? [rel] : [];
|
|
306
|
+
if (/[*?[]/.test(dir))
|
|
307
|
+
return [];
|
|
308
|
+
const glob = globRegExp(base);
|
|
309
|
+
let names = [];
|
|
310
|
+
try {
|
|
311
|
+
names = fs.readdirSync(path.join(rootDir, dir));
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return [];
|
|
315
|
+
}
|
|
316
|
+
return names.filter((name) => glob.test(name)).map((name) => (dir ? `${dir}/${name}` : name));
|
|
317
|
+
}
|
|
318
|
+
// Go path.Match syntax for one path element: * ? and [...] ([^...] negates)
|
|
319
|
+
function globRegExp(glob) {
|
|
320
|
+
let source = '';
|
|
321
|
+
for (let i = 0; i < glob.length; i++) {
|
|
322
|
+
const c = glob[i];
|
|
323
|
+
const close = c === '[' ? glob.indexOf(']', i + 1) : -1;
|
|
324
|
+
if (c === '*')
|
|
325
|
+
source += '.*';
|
|
326
|
+
else if (c === '?')
|
|
327
|
+
source += '.';
|
|
328
|
+
else if (close > i) {
|
|
329
|
+
source += `[${glob.slice(i + 1, close)}]`;
|
|
330
|
+
i = close;
|
|
331
|
+
}
|
|
332
|
+
else
|
|
333
|
+
source += /[\\^$.|+(){}[\]]/.test(c) ? `\\${c}` : c;
|
|
334
|
+
}
|
|
335
|
+
return new RegExp(`^${source}$`);
|
|
336
|
+
}
|
|
337
|
+
// Removes the go:embed copies the previous build listed
|
|
338
|
+
function removeEmbedCopies(outDir) {
|
|
339
|
+
const manifest = path.join(outDir, EMBED_MANIFEST);
|
|
340
|
+
if (!fs.existsSync(manifest))
|
|
341
|
+
return;
|
|
342
|
+
for (const line of fs.readFileSync(manifest, 'utf-8').split('\n')) {
|
|
343
|
+
if (line.trim())
|
|
344
|
+
fs.rmSync(path.join(outDir, line.trim()), { recursive: true, force: true });
|
|
345
|
+
}
|
|
346
|
+
fs.rmSync(manifest);
|
|
347
|
+
}
|
|
348
|
+
// Copies the go:embed files into outDir, where go build resolves the
|
|
349
|
+
// directives (embed paths can't leave the package directory)
|
|
350
|
+
function copyEmbeds(rootDir, patterns, outDir) {
|
|
351
|
+
const copied = [];
|
|
352
|
+
for (const pattern of patterns) {
|
|
353
|
+
const rel = pattern.startsWith('all:') ? pattern.slice(4) : pattern;
|
|
354
|
+
for (const match of embedMatches(rootDir, rel)) {
|
|
355
|
+
const top = match.split('/')[0];
|
|
356
|
+
if ((top === match && match.endsWith('.go')) || ['go.mod', 'go.sum', 'tnvendor', EMBED_MANIFEST].includes(top)) {
|
|
357
|
+
console.error(`error: can't embed '${match}': the generated Go files use that name`);
|
|
358
|
+
process.exit(1);
|
|
359
|
+
}
|
|
360
|
+
fs.cpSync(path.join(rootDir, match), path.join(outDir, match), { recursive: true });
|
|
361
|
+
if (!copied.includes(top))
|
|
362
|
+
copied.push(top);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (copied.length > 0)
|
|
366
|
+
fs.writeFileSync(path.join(outDir, EMBED_MANIFEST), `${copied.join('\n')}\n`);
|
|
367
|
+
}
|
|
120
368
|
export function run(opts) {
|
|
369
|
+
const goError = checkGo(opts.target);
|
|
370
|
+
if (goError) {
|
|
371
|
+
console.error(`error: ${goError}`);
|
|
372
|
+
process.exit(1);
|
|
373
|
+
}
|
|
374
|
+
if (opts.target && opts.scriptMode && opts.target !== hostTarget()) {
|
|
375
|
+
console.error(`error: --script can't run a program built for ${opts.target} on ${hostTarget()}; use --output`);
|
|
376
|
+
process.exit(1);
|
|
377
|
+
}
|
|
121
378
|
const sourcePath = opts.tsCode ? null : opts.source;
|
|
122
379
|
const tsCode = opts.tsCode ? opts.tsCode : fs.readFileSync(sourcePath, 'utf-8');
|
|
123
380
|
fs.mkdirSync('dist', { recursive: true });
|
|
@@ -136,14 +393,18 @@ export function run(opts) {
|
|
|
136
393
|
}
|
|
137
394
|
}
|
|
138
395
|
const sourceDir = sourcePath ? path.dirname(path.resolve(sourcePath)) : null;
|
|
139
|
-
// Node's platform names are GOOS names, except win32
|
|
396
|
+
// Node's platform names are GOOS names, except win32; OS-specific runtime
|
|
397
|
+
// helpers follow the --target
|
|
140
398
|
const goos = platform() === 'win32' ? 'windows' : platform();
|
|
399
|
+
// (typed locals: the self-hosted build boxes them into the optional fields)
|
|
400
|
+
const goFile = displayPath(path.join(opts.emitGo ?? 'dist', 'code.go'));
|
|
401
|
+
const targetOs = opts.target ? opts.target.split('/')[0] : goos;
|
|
141
402
|
const transpileOptions = {
|
|
142
403
|
parse: opts.parse,
|
|
143
404
|
tsx: !!sourcePath && sourcePath.endsWith('.tsx'),
|
|
144
405
|
fileName: displayPath(entryPath),
|
|
145
|
-
goFile:
|
|
146
|
-
goos:
|
|
406
|
+
goFile: goFile,
|
|
407
|
+
goos: targetOs
|
|
147
408
|
};
|
|
148
409
|
if (sourceDir) {
|
|
149
410
|
transpileOptions.readFile = (specifier, fromDir) => {
|
|
@@ -189,6 +450,7 @@ export function run(opts) {
|
|
|
189
450
|
content = typed;
|
|
190
451
|
return { content, dir, path: displayPath(resolved.path) };
|
|
191
452
|
};
|
|
453
|
+
transpileOptions.resolveEmbed = (pattern, fromDir) => resolveEmbedPattern(pattern, fromDir ?? sourceDir, sourceDir);
|
|
192
454
|
}
|
|
193
455
|
const transpileResult = transpileToNative(tsCode, transpileOptions);
|
|
194
456
|
if (transpileResult.diagnostics.length > 0) {
|
|
@@ -197,50 +459,79 @@ export function run(opts) {
|
|
|
197
459
|
console.error(`\n${plural(transpileResult.diagnostics.length, 'error')}: TypeNative can't compile this yet`);
|
|
198
460
|
process.exit(1);
|
|
199
461
|
}
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
462
|
+
// --emit-go writes to its own directory; builds happen in dist/
|
|
463
|
+
const outDir = opts.emitGo ?? 'dist';
|
|
464
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
465
|
+
if (opts.emitGo && fs.existsSync(path.join(outDir, 'go.mod'))) {
|
|
466
|
+
const existingMod = fs.readFileSync(path.join(outDir, 'go.mod'), 'utf-8');
|
|
467
|
+
if (!existingMod.startsWith('module tnapp')) {
|
|
468
|
+
console.error(`error: ${outDir}/go.mod belongs to another Go module; --emit-go needs a directory of its own`);
|
|
469
|
+
process.exit(1);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// Clean up stale Go files from previous runs before writing new ones (in an
|
|
473
|
+
// --emit-go directory, only files TypeNative generated)
|
|
474
|
+
removeEmbedCopies(outDir);
|
|
475
|
+
for (const existing of fs.readdirSync(outDir)) {
|
|
476
|
+
const file = path.join(outDir, existing);
|
|
477
|
+
if (existing === 'go.mod' || existing === 'go.sum')
|
|
478
|
+
fs.rmSync(file);
|
|
479
|
+
else if (existing.endsWith('.go') && (!opts.emitGo || fs.readFileSync(file, 'utf-8').startsWith(GENERATED_HEADER)))
|
|
480
|
+
fs.rmSync(file);
|
|
206
481
|
}
|
|
207
|
-
fs.writeFileSync('
|
|
208
|
-
const goFiles = ['
|
|
482
|
+
fs.writeFileSync(path.join(outDir, 'code.go'), transpileResult.main);
|
|
483
|
+
const goFiles = ['code.go'];
|
|
209
484
|
for (const [filename, content] of transpileResult.files) {
|
|
210
|
-
fs.writeFileSync(
|
|
211
|
-
goFiles.push(
|
|
485
|
+
fs.writeFileSync(path.join(outDir, filename), content);
|
|
486
|
+
goFiles.push(filename);
|
|
212
487
|
}
|
|
488
|
+
if (sourceDir)
|
|
489
|
+
copyEmbeds(sourceDir, transpileResult.embeds, outDir);
|
|
213
490
|
// Third-party Go modules the program imports (see vendorXText / vendorGoModules)
|
|
214
491
|
const generated = [transpileResult.main];
|
|
215
492
|
for (const content of transpileResult.files.values())
|
|
216
493
|
generated.push(content);
|
|
217
494
|
const xTextImport = /^import (\w+ )?"golang\.org\/x\/text\//m;
|
|
218
|
-
const sqliteImport = /^import (\w+ )?"github\.com\/ncruces\/go-sqlite3"/m;
|
|
219
495
|
const modules = [];
|
|
220
|
-
let goVersion =
|
|
496
|
+
let goVersion = `${MIN_GO_MAJOR}.${MIN_GO_MINOR}`;
|
|
221
497
|
if (generated.some((content) => xTextImport.test(content)))
|
|
222
|
-
modules.push(vendorXText());
|
|
223
|
-
|
|
224
|
-
|
|
498
|
+
modules.push(vendorXText(outDir));
|
|
499
|
+
const xNetImport = /^import (\w+ )?"golang\.org\/x\/net\//m;
|
|
500
|
+
if (generated.some((content) => xNetImport.test(content)))
|
|
501
|
+
modules.push(vendorXNet(outDir));
|
|
502
|
+
if (generated.some((content) => importsVendoredModule(content, opts.vendorDir))) {
|
|
503
|
+
const vendored = vendorGoModules(opts.vendorDir, outDir);
|
|
225
504
|
for (const mod of vendored.modules)
|
|
226
505
|
modules.push(mod);
|
|
227
506
|
goVersion = vendored.goVersion;
|
|
228
507
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
508
|
+
writeGoMod(modules, goVersion, outDir);
|
|
509
|
+
if (opts.emitGo) {
|
|
510
|
+
console.log(`Wrote the Go sources to ${outDir} (build them with: go build -C ${outDir})`);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
// GOOS / GOARCH for --target: set on this process, which go build inherits
|
|
514
|
+
// (--script only runs host builds, so the program never sees them changed)
|
|
515
|
+
if (opts.target) {
|
|
516
|
+
const pair = opts.target.split('/');
|
|
517
|
+
process.env.GOOS = pair[0];
|
|
518
|
+
process.env.GOARCH = pair[1];
|
|
519
|
+
}
|
|
520
|
+
const exeName = targetOs === 'windows' ? 'native.exe' : 'native';
|
|
521
|
+
const exePath = `dist/${exeName}`;
|
|
522
|
+
const buildArgs = ['build', '-C', 'dist', '-o', exeName];
|
|
523
|
+
// -trimpath gives path-independent cache keys: the vendored packages (sqlite
|
|
524
|
+
// is ~5 MB of Go) compile once, not again for every project directory
|
|
525
|
+
if (opts.release || modules.length > 0)
|
|
526
|
+
buildArgs.push('-trimpath');
|
|
527
|
+
if (opts.release)
|
|
528
|
+
buildArgs.push('-ldflags=-s -w');
|
|
529
|
+
// atomic counters: coverage.ClearCounters refuses the default 'set' mode
|
|
530
|
+
if (opts.cover)
|
|
531
|
+
buildArgs.push('-cover', '-covermode=atomic');
|
|
532
|
+
for (const file of goFiles)
|
|
533
|
+
buildArgs.push(file);
|
|
534
|
+
const buildResult = runCommand('go', buildArgs);
|
|
244
535
|
if (buildResult.status !== 0) {
|
|
245
536
|
// Positions in .ts files come from line directives in the generated Go
|
|
246
537
|
console.error('go build failed:');
|
package/bin/main.js
CHANGED
|
@@ -2,37 +2,59 @@
|
|
|
2
2
|
// the native compiler binary. Interactive prompts use the transpilable
|
|
3
3
|
// prompt module (sync stdin) instead of inquirer.
|
|
4
4
|
import path from 'node:path';
|
|
5
|
-
import { run, createProject } from './index.js';
|
|
5
|
+
import { run, createProject, parseCliArgs, usage } from './index.js';
|
|
6
6
|
import { ask, askMultiline, confirm } from './prompt.js';
|
|
7
7
|
import { parseAstJsonNative, typeCheckNative } from './parse-native.js';
|
|
8
|
+
import { VERSION } from './version.js';
|
|
8
9
|
(function main() {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
const args = parseCliArgs(process.argv.slice(2));
|
|
11
|
+
if (args.error) {
|
|
12
|
+
console.error(`error: ${args.error}\nRun 'typenative --help' for the options.`);
|
|
13
|
+
process.exit(2);
|
|
14
|
+
}
|
|
15
|
+
if (args.help) {
|
|
16
|
+
console.log(usage());
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (args.version) {
|
|
20
|
+
console.log(VERSION);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const scriptMode = args.script;
|
|
24
|
+
let source = args.source;
|
|
25
|
+
let output = args.output;
|
|
26
|
+
if (args.newProject) {
|
|
17
27
|
const projectName = ask('Enter Project Name:');
|
|
18
28
|
const installDependencies = confirm('Do you want to install dependencies?');
|
|
19
29
|
createProject(projectName, installDependencies);
|
|
20
30
|
return;
|
|
21
31
|
}
|
|
22
32
|
let tsCode = null;
|
|
23
|
-
if (!
|
|
33
|
+
if (!scriptMode && !source) {
|
|
24
34
|
source = ask('Enter Path to typescript main file:');
|
|
25
35
|
}
|
|
26
|
-
if (!
|
|
36
|
+
if (!scriptMode && !output && !args.emitGo) {
|
|
27
37
|
output = ask('Enter Output Path:');
|
|
28
38
|
}
|
|
29
|
-
if (
|
|
39
|
+
if (scriptMode && !source) {
|
|
30
40
|
tsCode = askMultiline('Write your typescript code here:');
|
|
31
41
|
}
|
|
32
42
|
// The native compiler lives in native/, next to the package's govendor/ and types/
|
|
33
43
|
const packageDir = path.join(path.dirname(path.resolve(process.argv[0])), '..');
|
|
34
44
|
const vendorDir = path.join(packageDir, 'govendor');
|
|
35
45
|
const typesDir = path.join(packageDir, 'types');
|
|
36
|
-
const typeCheck = noTypeCheck ? null : (fileName) => typeCheckNative(fileName, typesDir);
|
|
37
|
-
run({
|
|
46
|
+
const typeCheck = args.noTypeCheck ? null : (fileName) => typeCheckNative(fileName, typesDir);
|
|
47
|
+
run({
|
|
48
|
+
source,
|
|
49
|
+
output,
|
|
50
|
+
scriptMode,
|
|
51
|
+
tsCode,
|
|
52
|
+
parse: parseAstJsonNative,
|
|
53
|
+
typeCheck,
|
|
54
|
+
vendorDir,
|
|
55
|
+
target: args.target,
|
|
56
|
+
release: args.release,
|
|
57
|
+
cover: args.cover,
|
|
58
|
+
emitGo: args.emitGo
|
|
59
|
+
});
|
|
38
60
|
})();
|
package/bin/node/assert.js
CHANGED
|
@@ -201,7 +201,7 @@ func TnAssertRejects[T any](p chan T, check func(interface{}) bool, msg ...inter
|
|
|
201
201
|
if check != nil && !check(reason) {
|
|
202
202
|
panic(reason)
|
|
203
203
|
}
|
|
204
|
-
out
|
|
204
|
+
TnAsyncSend(out, struct{}{})
|
|
205
205
|
}()
|
|
206
206
|
return out
|
|
207
207
|
}
|
|
@@ -215,7 +215,7 @@ func TnAssertDoesNotReject[T any](p chan T, msg ...interface{}) chan struct{} {
|
|
|
215
215
|
if rejected {
|
|
216
216
|
panic(tnAssertionError(tnAssertMessage(msg), "Got unwanted rejection.\\nActual message: \\""+TnErrorText(reason)+"\\"", reason, nil, "doesNotReject"))
|
|
217
217
|
}
|
|
218
|
-
out
|
|
218
|
+
TnAsyncSend(out, struct{}{})
|
|
219
219
|
}()
|
|
220
220
|
return out
|
|
221
221
|
}
|
package/bin/node/buffer.js
CHANGED
|
@@ -690,6 +690,15 @@ export function registerBuffer(r) {
|
|
|
690
690
|
const type = call.types[0];
|
|
691
691
|
if (call.args.length === 0)
|
|
692
692
|
return '[]byte{}';
|
|
693
|
+
// new Uint8Array(arrayBuffer, byteOffset?, length?): a view sharing its memory
|
|
694
|
+
if (r.isArrayBuffer(call.nodes[0])) {
|
|
695
|
+
if (call.args.length === 1)
|
|
696
|
+
return call.args[0];
|
|
697
|
+
const start = `int(${call.args[1]})`;
|
|
698
|
+
if (call.args.length === 2)
|
|
699
|
+
return `${call.args[0]}[${start}:]`;
|
|
700
|
+
return `${call.args[0]}[${start}:${start}+int(${call.args[2]})]`;
|
|
701
|
+
}
|
|
693
702
|
if (type === 'float64')
|
|
694
703
|
return `make([]byte, int(${call.args[0]}))`;
|
|
695
704
|
use();
|