From 653deb5cb4b05cdc3b913fd588d712b489f1bff4 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 17 Mar 2025 23:59:04 +0000 Subject: [PATCH 1/3] Remove `swjs_library_version` This function was used to check the compatibility between the JavaScriptKit Swift library and JS runtime because they were manually kept in sync by users of the library with npm and SwiftPM. Now that the library is distributed as a single SwiftPM package, there is no need for this function. --- Runtime/src/index.ts | 8 -------- Runtime/src/types.ts | 1 - Sources/JavaScriptKit/Runtime/index.js | 4 ---- Sources/JavaScriptKit/Runtime/index.mjs | 4 ---- Sources/_CJavaScriptKit/_CJavaScriptKit.c | 7 ------- 5 files changed, 24 deletions(-) diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index 3f23ed753..64c1225e0 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -56,14 +56,6 @@ export class SwiftRuntime { ` ); } - if (this.exports.swjs_library_version() != this.version) { - throw new Error( - `The versions of JavaScriptKit are incompatible. - WebAssembly runtime ${this.exports.swjs_library_version()} != JS runtime ${ - this.version - }` - ); - } } main() { diff --git a/Runtime/src/types.ts b/Runtime/src/types.ts index 587b60770..e6aa99363 100644 --- a/Runtime/src/types.ts +++ b/Runtime/src/types.ts @@ -7,7 +7,6 @@ export type JavaScriptValueKind = number; export type JavaScriptValueKindAndFlags = number; export interface ExportedFunctions { - swjs_library_version(): number; swjs_library_features(): number; swjs_prepare_host_function_call(size: number): pointer; swjs_cleanup_host_function_call(argv: pointer): void; diff --git a/Sources/JavaScriptKit/Runtime/index.js b/Sources/JavaScriptKit/Runtime/index.js index 25b6af3c9..696a7df9d 100644 --- a/Sources/JavaScriptKit/Runtime/index.js +++ b/Sources/JavaScriptKit/Runtime/index.js @@ -328,10 +328,6 @@ -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor `); } - if (this.exports.swjs_library_version() != this.version) { - throw new Error(`The versions of JavaScriptKit are incompatible. - WebAssembly runtime ${this.exports.swjs_library_version()} != JS runtime ${this.version}`); - } } main() { const instance = this.instance; diff --git a/Sources/JavaScriptKit/Runtime/index.mjs b/Sources/JavaScriptKit/Runtime/index.mjs index 668368203..a18d90c37 100644 --- a/Sources/JavaScriptKit/Runtime/index.mjs +++ b/Sources/JavaScriptKit/Runtime/index.mjs @@ -322,10 +322,6 @@ class SwiftRuntime { -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor `); } - if (this.exports.swjs_library_version() != this.version) { - throw new Error(`The versions of JavaScriptKit are incompatible. - WebAssembly runtime ${this.exports.swjs_library_version()} != JS runtime ${this.version}`); - } } main() { const instance = this.instance; diff --git a/Sources/_CJavaScriptKit/_CJavaScriptKit.c b/Sources/_CJavaScriptKit/_CJavaScriptKit.c index ed8240ca1..aaf3f69f0 100644 --- a/Sources/_CJavaScriptKit/_CJavaScriptKit.c +++ b/Sources/_CJavaScriptKit/_CJavaScriptKit.c @@ -13,13 +13,6 @@ extern void *memcpy (void *__restrict, const void *__restrict, size_t); #include #endif -/// The compatibility runtime library version. -/// Notes: If you change any interface of runtime library, please increment -/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`. -__attribute__((export_name("swjs_library_version"))) -int swjs_library_version(void) { - return 708; -} __attribute__((export_name("swjs_prepare_host_function_call"))) void *swjs_prepare_host_function_call(const int argc) { From 13362700e6e7f9f732a62fb72f9627178aa6f480 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 18 Mar 2025 00:04:42 +0000 Subject: [PATCH 2/3] Expose `swjs_library_features` from Swift side We now support only Swift 6.0 and above, so we can use `@_expose(wasm)` instead of `__attribute__((export_name))` and `@_cdecl` hack. --- Sources/JavaScriptKit/Features.swift | 12 ++++-------- Sources/_CJavaScriptKit/_CJavaScriptKit.c | 7 ------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/Sources/JavaScriptKit/Features.swift b/Sources/JavaScriptKit/Features.swift index db6e00f26..4148f2adb 100644 --- a/Sources/JavaScriptKit/Features.swift +++ b/Sources/JavaScriptKit/Features.swift @@ -2,17 +2,13 @@ enum LibraryFeatures { static let weakRefs: Int32 = 1 << 0 } -@_cdecl("_library_features") -func _library_features() -> Int32 { +@_expose(wasm, "swjs_library_features") +@_cdecl("_swjs_library_features") +@available(*, unavailable) +public func _swjs_library_features() -> Int32 { var features: Int32 = 0 #if !JAVASCRIPTKIT_WITHOUT_WEAKREFS features |= LibraryFeatures.weakRefs #endif return features } - -#if compiler(>=6.0) && hasFeature(Embedded) -// cdecls currently don't work in embedded, and expose for wasm only works >=6.0 -@_expose(wasm, "swjs_library_features") -public func _swjs_library_features() -> Int32 { _library_features() } -#endif \ No newline at end of file diff --git a/Sources/_CJavaScriptKit/_CJavaScriptKit.c b/Sources/_CJavaScriptKit/_CJavaScriptKit.c index aaf3f69f0..67a83cab0 100644 --- a/Sources/_CJavaScriptKit/_CJavaScriptKit.c +++ b/Sources/_CJavaScriptKit/_CJavaScriptKit.c @@ -46,13 +46,6 @@ void swjs_free_host_function(const JavaScriptHostFuncRef host_func_ref) { _free_host_function_impl(host_func_ref); } -int _library_features(void); - -__attribute__((export_name("swjs_library_features"))) -int swjs_library_features(void) { - return _library_features(); -} - int swjs_get_worker_thread_id_cached(void) { _Thread_local static int tid = 0; if (tid == 0) { From 396899c2d2e1c20fa37aa0a672e4d02fb00a3807 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 18 Mar 2025 00:23:36 +0000 Subject: [PATCH 3/3] Remove remaining export_name + cdecl hacks --- .../_thingsThatShouldNotBeNeeded.swift | 7 ---- .../FundamentalObjects/JSClosure.swift | 36 +++++++------------ Sources/_CJavaScriptKit/_CJavaScriptKit.c | 18 ---------- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift b/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift index 8f45ccee9..a5da489dc 100644 --- a/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift +++ b/Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift @@ -1,12 +1,5 @@ import JavaScriptKit -// NOTE: it seems the embedded tree shaker gets rid of these exports if they are not used somewhere -func _i_need_to_be_here_for_wasm_exports_to_work() { - _ = _swjs_library_features - _ = _swjs_call_host_function - _ = _swjs_free_host_function -} - // TODO: why do I need this? and surely this is not ideal... figure this out, or at least have this come from a C lib @_cdecl("strlen") func strlen(_ s: UnsafePointer) -> Int { diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift index 261b5b5cb..e56713483 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift @@ -197,8 +197,10 @@ private func makeAsyncClosure( // └─────────────────────┴──────────────────────────┘ /// Returns true if the host function has been already released, otherwise false. -@_cdecl("_call_host_function_impl") -func _call_host_function_impl( +@_expose(wasm, "swjs_call_host_function") +@_cdecl("_swjs_call_host_function") +@available(*, unavailable) +public func _swjs_call_host_function( _ hostFuncRef: JavaScriptHostFuncRef, _ argv: UnsafePointer, _ argc: Int32, _ callbackFuncRef: JavaScriptObjectRef @@ -231,9 +233,10 @@ extension JSClosure { } } - -@_cdecl("_free_host_function_impl") -func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {} +@_expose(wasm, "swjs_free_host_function") +@_cdecl("_swjs_free_host_function") +@available(*, unavailable) +func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {} #else @@ -244,25 +247,10 @@ extension JSClosure { } -@_cdecl("_free_host_function_impl") -func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) { - JSClosure.sharedClosures.wrappedValue[hostFuncRef] = nil -} -#endif - -#if compiler(>=6.0) && hasFeature(Embedded) -// cdecls currently don't work in embedded, and expose for wasm only works >=6.0 -@_expose(wasm, "swjs_call_host_function") -public func _swjs_call_host_function( - _ hostFuncRef: JavaScriptHostFuncRef, - _ argv: UnsafePointer, _ argc: Int32, - _ callbackFuncRef: JavaScriptObjectRef) -> Bool { - - _call_host_function_impl(hostFuncRef, argv, argc, callbackFuncRef) -} - @_expose(wasm, "swjs_free_host_function") -public func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) { - _free_host_function_impl(hostFuncRef) +@_cdecl("_swjs_free_host_function") +@available(*, unavailable) +func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) { + JSClosure.sharedClosures.wrappedValue[hostFuncRef] = nil } #endif diff --git a/Sources/_CJavaScriptKit/_CJavaScriptKit.c b/Sources/_CJavaScriptKit/_CJavaScriptKit.c index 67a83cab0..ed52a55f6 100644 --- a/Sources/_CJavaScriptKit/_CJavaScriptKit.c +++ b/Sources/_CJavaScriptKit/_CJavaScriptKit.c @@ -28,24 +28,6 @@ void swjs_cleanup_host_function_call(void *argv_buffer) { // cdecls don't work in Embedded, but @_expose(wasm) can be used with Swift >=6.0 // the previously used `#if __Embedded` did not play well with SwiftPM (defines needed to be on every target up the chain) #ifdef __wasi__ -bool _call_host_function_impl(const JavaScriptHostFuncRef host_func_ref, - const RawJSValue *argv, const int argc, - const JavaScriptObjectRef callback_func); - -__attribute__((export_name("swjs_call_host_function"))) -bool swjs_call_host_function(const JavaScriptHostFuncRef host_func_ref, - const RawJSValue *argv, const int argc, - const JavaScriptObjectRef callback_func) { - return _call_host_function_impl(host_func_ref, argv, argc, callback_func); -} - -void _free_host_function_impl(const JavaScriptHostFuncRef host_func_ref); - -__attribute__((export_name("swjs_free_host_function"))) -void swjs_free_host_function(const JavaScriptHostFuncRef host_func_ref) { - _free_host_function_impl(host_func_ref); -} - int swjs_get_worker_thread_id_cached(void) { _Thread_local static int tid = 0; if (tid == 0) {