Skip to content

Commit 3abdbd2

Browse files
omochikateinoigakukun
authored andcommitted
migrate to TypeScript
1 parent f286203 commit 3abdbd2

File tree

11 files changed

+158
-37
lines changed

11 files changed

+158
-37
lines changed

Sources/CartonKit/Server/StaticArchive.swift

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

Sources/carton-release/HashArchive.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ struct HashArchive: AsyncParsableCommand {
4747
try localFileSystem.createDirectory(dotFilesStaticPath, recursive: true)
4848
var hashes: [(String, String)] = []
4949
for entrypoint in ["dev", "bundle", "test", "testNode"] {
50+
let tsFilename = "\(entrypoint).ts"
5051
let filename = "\(entrypoint).js"
5152
var arguments = [
52-
"esbuild", "--bundle", "entrypoint/\(filename)", "--outfile=static/\(filename)",
53+
"esbuild", "--bundle", "entrypoint/\(tsFilename)", "--outfile=static/\(filename)",
5354
]
5455

5556
if entrypoint == "testNode" {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class SwiftRuntime {
2+
setInstance(instance: WebAssembly.Instance): void;
3+
readonly wasmImports: ImportedFunctions;
4+
}
5+
export type SwiftRuntimeConstructor = typeof SwiftRuntime;
6+
export interface ImportedFunctions { }

entrypoint/bundle.js renamed to entrypoint/bundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
// limitations under the License.
1414

1515
import { WasmRunner } from "./common.js";
16+
import type { SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
1617

1718
const startWasiTask = async () => {
1819
// Fetch our Wasm File
1920
const response = await fetch("REPLACE_THIS_WITH_THE_MAIN_WEBASSEMBLY_MODULE");
2021
const responseArrayBuffer = await response.arrayBuffer();
2122

22-
let runtimeConstructor;
23+
let runtimeConstructor: SwiftRuntimeConstructor | undefined = undefined;
2324
try {
2425
const { SwiftRuntime } = await import(
26+
// @ts-ignore
2527
"./JavaScriptKit_JavaScriptKit.resources/Runtime/index.mjs"
2628
);
2729
runtimeConstructor = SwiftRuntime;
@@ -36,7 +38,7 @@ const startWasiTask = async () => {
3638
await wasmRunner.run(wasmBytes);
3739
};
3840

39-
function handleError(e) {
41+
function handleError(e: any) {
4042
console.error(e);
4143
if (e instanceof WebAssembly.RuntimeError) {
4244
console.log(e.stack);

entrypoint/common.js renamed to entrypoint/common.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,34 @@
1515
import { WASI } from "@wasmer/wasi";
1616
import { WasmFs } from "@wasmer/wasmfs";
1717
import * as path from "path-browserify";
18+
import type { SwiftRuntime, SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
1819

19-
export const WasmRunner = (rawOptions, SwiftRuntime) => {
20-
const options = defaultRunnerOptions(rawOptions);
20+
export type Options = {
21+
args?: string[];
22+
onStdout?: (text: string) => void;
23+
onStderr?: (text: string) => void;
24+
};
25+
26+
export type WasmRunner = {
27+
run(wasmBytes: ArrayBufferLike, extraWasmImports?: WebAssembly.Imports): Promise<void>
28+
};
29+
30+
export const WasmRunner = (rawOptions: Options | false, SwiftRuntime: SwiftRuntimeConstructor | undefined): WasmRunner => {
31+
const options: Options = defaultRunnerOptions(rawOptions);
2132

22-
let swift;
33+
let swift: SwiftRuntime;
2334
if (SwiftRuntime) {
2435
swift = new SwiftRuntime();
2536
}
2637

2738
const wasmFs = createWasmFS(
2839
(stdout) => {
2940
console.log(stdout);
30-
options.onStdout(stdout);
41+
options.onStdout?.call(undefined, stdout);
3142
},
3243
(stderr) => {
3344
console.error(stderr);
34-
options.onStderr(stderr);
45+
options.onStderr?.call(undefined, stderr);
3546
}
3647
);
3748

@@ -50,13 +61,16 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
5061
},
5162
});
5263

53-
const createWasmImportObject = (extraWasmImports, wasmModule) => {
54-
const importObject = {
64+
const createWasmImportObject = (
65+
extraWasmImports: WebAssembly.Imports,
66+
wasmModule: WebAssembly.Module
67+
): WebAssembly.Imports => {
68+
const importObject: WebAssembly.Imports = {
5569
wasi_snapshot_preview1: wrapWASI(wasi, wasmModule),
5670
};
5771

5872
if (swift) {
59-
importObject.javascript_kit = swift.wasmImports;
73+
importObject.javascript_kit = swift.wasmImports as unknown as WebAssembly.ModuleImports;
6074
}
6175

6276
if (extraWasmImports) {
@@ -70,7 +84,7 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
7084
};
7185

7286
return {
73-
async run(wasmBytes, extraWasmImports) {
87+
async run(wasmBytes: ArrayBufferLike, extraWasmImports?: WebAssembly.Imports) {
7488
if (!extraWasmImports) {
7589
extraWasmImports = {};
7690
}
@@ -91,7 +105,7 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
91105
wasi.start(instance);
92106

93107
// Initialize and start Reactor
94-
if (instance.exports._initialize) {
108+
if (typeof instance.exports._initialize == "function") {
95109
instance.exports._initialize();
96110
if (typeof instance.exports.main === "function") {
97111
instance.exports.main();
@@ -104,7 +118,7 @@ export const WasmRunner = (rawOptions, SwiftRuntime) => {
104118
};
105119
};
106120

107-
const defaultRunnerOptions = (options) => {
121+
const defaultRunnerOptions = (options: Options | false): Options => {
108122
if (!options) return defaultRunnerOptions({});
109123
if (!options.onStdout) {
110124
options.onStdout = () => { };
@@ -118,13 +132,19 @@ const defaultRunnerOptions = (options) => {
118132
return options;
119133
};
120134

121-
const createWasmFS = (onStdout, onStderr) => {
135+
const createWasmFS = (
136+
onStdout: (text: string) => void,
137+
onStderr: (text: string) => void
138+
): WasmFs => {
122139
// Instantiate a new WASI Instance
123140
const wasmFs = new WasmFs();
124141

125142
// Output stdout and stderr to console
126143
const originalWriteSync = wasmFs.fs.writeSync;
127-
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
144+
(wasmFs.fs as any).writeSync = (
145+
fd: number, buffer: Buffer | Uint8Array,
146+
offset?: number, length?: number, position?: number
147+
): number => {
128148
const text = new TextDecoder("utf-8").decode(buffer);
129149
if (text !== "\n") {
130150
switch (fd) {
@@ -142,7 +162,7 @@ const createWasmFS = (onStdout, onStderr) => {
142162
return wasmFs;
143163
};
144164

145-
const wrapWASI = (wasiObject, wasmModule) => {
165+
const wrapWASI = (wasiObject: WASI, wasmModule: WebAssembly.Module): WebAssembly.ModuleImports => {
146166
// PATCH: @wasmer-js/wasi@0.x forgets to call `refreshMemory` in `clock_res_get`,
147167
// which writes its result to memory view. Without the refresh the memory view,
148168
// it accesses a detached array buffer if the memory is grown by malloc.
@@ -154,7 +174,7 @@ const wrapWASI = (wasiObject, wasmModule) => {
154174
// Reference: https://github.com/wasmerio/wasmer-js/blob/55fa8c17c56348c312a8bd23c69054b1aa633891/packages/wasi/src/index.ts#L557
155175
const original_clock_res_get = wasiObject.wasiImport["clock_res_get"];
156176

157-
wasiObject.wasiImport["clock_res_get"] = (clockId, resolution) => {
177+
wasiObject.wasiImport["clock_res_get"] = (clockId: number, resolution: number) => {
158178
wasiObject.refreshMemory();
159179
return original_clock_res_get(clockId, resolution);
160180
};

entrypoint/dev.js renamed to entrypoint/dev.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
// limitations under the License.
1414

1515
import ReconnectingWebSocket from "reconnecting-websocket";
16-
import { WasmRunner } from "./common.js";
16+
import { WasmRunner } from "./common";
17+
import type { SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
1718

1819
const socket = new ReconnectingWebSocket(`ws://${location.host}/watcher`);
1920

@@ -28,9 +29,10 @@ const startWasiTask = async () => {
2829
const response = await fetch("/main.wasm");
2930
const responseArrayBuffer = await response.arrayBuffer();
3031

31-
let runtimeConstructor;
32+
let runtimeConstructor: SwiftRuntimeConstructor | undefined = undefined;
3233
try {
3334
const { SwiftRuntime } = await import(
35+
// @ts-ignore
3436
"./JavaScriptKit_JavaScriptKit.resources/Runtime/index.mjs"
3537
);
3638
runtimeConstructor = SwiftRuntime;
@@ -62,7 +64,7 @@ const startWasiTask = async () => {
6264
await wasmRunner.run(wasmBytes);
6365
};
6466

65-
function handleError(e) {
67+
function handleError(e: any) {
6668
console.error(e);
6769
if (e instanceof WebAssembly.RuntimeError) {
6870
console.log(e.stack);

entrypoint/test.js renamed to entrypoint/test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import ReconnectingWebSocket from "reconnecting-websocket";
1616
import { WASIExitError } from "@wasmer/wasi";
1717
import { WasmRunner } from "./common.js";
18+
import type { SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
1819

1920
const socket = new ReconnectingWebSocket(`ws://${location.host}/watcher`);
2021
socket.addEventListener("message", (message) => {
@@ -28,9 +29,10 @@ const startWasiTask = async () => {
2829
const response = await fetch("/main.wasm");
2930
const responseArrayBuffer = await response.arrayBuffer();
3031

31-
let runtimeConstructor;
32+
let runtimeConstructor: SwiftRuntimeConstructor | undefined = undefined;
3233
try {
3334
const { SwiftRuntime } = await import(
35+
// @ts-ignore
3436
"./JavaScriptKit_JavaScriptKit.resources/Runtime/index.mjs"
3537
);
3638
runtimeConstructor = SwiftRuntime;
@@ -69,7 +71,7 @@ const startWasiTask = async () => {
6971
// 5. Crash by throwing JS exception synchronously
7072
// 6. Crash by throwing JS exception asynchronously
7173

72-
const handleExitOrError = (error) => {
74+
const handleExitOrError = (error: any) => {
7375
// XCTest always calls `exit` at the end when no crash
7476
if (error instanceof WASIExitError) {
7577
// pass the output to the server in any case
@@ -105,7 +107,7 @@ const startWasiTask = async () => {
105107
// reachable here without catch (case 3, 4, 6)
106108
};
107109

108-
function handleError(e) {
110+
function handleError(e: any) {
109111
console.error(e);
110112
if (e instanceof WebAssembly.RuntimeError) {
111113
console.log(e.stack);

entrypoint/testNode.js renamed to entrypoint/testNode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import fs from "fs/promises";
1616
import { WasmRunner } from "./common.js";
17+
import type { SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
1718

1819
const args = [...process.argv];
1920
args.shift();
@@ -27,9 +28,10 @@ if (!wasmFile) {
2728
const startWasiTask = async () => {
2829
const wasmBytes = await fs.readFile(wasmFile);
2930

30-
let runtimeConstructor;
31+
let runtimeConstructor: SwiftRuntimeConstructor | undefined = undefined;
3132
try {
3233
const { SwiftRuntime } = await import(
34+
// @ts-ignore
3335
"./JavaScriptKit_JavaScriptKit.resources/Runtime/index.mjs"
3436
);
3537

package-lock.json

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
},
2121
"homepage": "https://github.com/swiftwasm/carton#readme",
2222
"devDependencies": {
23+
"@types/node": "^20.12.7",
24+
"@types/path-browserify": "^1.0.2",
2325
"@wasmer/wasi": "^0.12.0",
2426
"@wasmer/wasmfs": "^0.12.0",
25-
"path-browserify": "^1.0.1",
2627
"esbuild": "^0.14.38",
2728
"npm-run-all": "^4.1.5",
28-
"reconnecting-websocket": "^4.4.0"
29+
"path-browserify": "^1.0.1",
30+
"reconnecting-websocket": "^4.4.0",
31+
"typescript": "^5.4.5"
2932
}
3033
}

0 commit comments

Comments
 (0)