Skip to content

Commit 5478068

Browse files
Add library compatibility check
1 parent 45d9431 commit 5478068

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

Diff for: Package.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ let package = Package(
1919
"-Xlinker",
2020
"--export=swjs_prepare_host_function_call",
2121
"-Xlinker",
22-
"--export=swjs_cleanup_host_function_call"
22+
"--export=swjs_cleanup_host_function_call",
23+
"-Xlinker",
24+
"--export=swjs_library_version",
2325
],
2426
.when(platforms: [.wasi])
2527
)

Diff for: Package@swift-5.2.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ let package = Package(
1919
"-Xlinker",
2020
"--export=swjs_prepare_host_function_call",
2121
"-Xlinker",
22-
"--export=swjs_cleanup_host_function_call"
22+
"--export=swjs_cleanup_host_function_call",
23+
"-Xlinker",
24+
"--export=swjs_library_version",
2325
]
2426
)
2527
]),

Diff for: Runtime/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare const window: GlobalVariable;
1010
declare const global: GlobalVariable;
1111

1212
interface SwiftRuntimeExportedFunctions {
13+
swjs_library_version(): number;
1314
swjs_prepare_host_function_call(size: number): pointer;
1415
swjs_cleanup_host_function_call(argv: pointer): void;
1516
swjs_call_host_function(
@@ -84,6 +85,7 @@ class SwiftRuntimeHeap {
8485
export class SwiftRuntime {
8586
private instance: WebAssembly.Instance | null;
8687
private heap: SwiftRuntimeHeap
88+
private version: number = 400
8789

8890
constructor() {
8991
this.instance = null;
@@ -92,6 +94,10 @@ export class SwiftRuntime {
9294

9395
setInstance(instance: WebAssembly.Instance) {
9496
this.instance = instance
97+
const exports = this.instance.exports as any as SwiftRuntimeExportedFunctions;
98+
if (exports.swjs_library_version() != this.version) {
99+
throw new Error("The versions of JavaScriptKit are incompatible.")
100+
}
95101
}
96102

97103
importObjects() {
@@ -351,4 +357,4 @@ export class SwiftRuntime {
351357
}
352358
}
353359
}
354-
}
360+
}

Diff for: Sources/JavaScriptKit/Compatibility.swift

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@_cdecl("swjs_library_version")
2+
func _library_version() -> Double {
3+
return 400
4+
}

0 commit comments

Comments
 (0)