Skip to content

Commit 025b9fc

Browse files
committed
revert to cdecl-based exports for swift 5.10 compatibility
1 parent 4912646 commit 025b9fc

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

Diff for: Examples/Basic/Package.swift

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import PackageDescription
44

55
let package = Package(
66
name: "Basic",
7+
platforms: [
8+
.macOS(.v14)
9+
],
710
dependencies: [.package(name: "JavaScriptKit", path: "../../")],
811
targets: [
912
.executableTarget(

Diff for: Examples/Embedded/Sources/EmbeddedApp/_thingsThatShouldNotBeNeeded.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import JavaScriptKit
22

33
// NOTE: it seems the embedded tree shaker gets rid of these exports if they are not used somewhere
44
func _i_need_to_be_here_for_wasm_exports_to_work() {
5-
_ = _library_features
6-
_ = _call_host_function_impl
7-
_ = _free_host_function_impl
5+
_ = _swjs_library_features
6+
_ = _swjs_call_host_function
7+
_ = _swjs_free_host_function
88
}
99

1010
// 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

Diff for: Sources/JavaScriptKit/Features.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ enum LibraryFeatures {
22
static let weakRefs: Int32 = 1 << 0
33
}
44

5-
@_expose(wasm, "swjs_library_features")
6-
public func _library_features() -> Int32 {
5+
@_cdecl("_library_features")
6+
func _library_features() -> Int32 {
77
var features: Int32 = 0
88
#if !JAVASCRIPTKIT_WITHOUT_WEAKREFS
99
features |= LibraryFeatures.weakRefs
1010
#endif
1111
return features
1212
}
13+
14+
#if hasFeature(Embedded)
15+
// cdecls currently don't work in embedded, and expose for wasm only works >=6.0
16+
@_expose(wasm, "swjs_library_features")
17+
public func _swjs_library_features() -> Int32 { _library_features() }
18+
#endif

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

+24-6
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ private func makeAsyncClosure(_ body: @escaping ([JSValue]) async throws -> JSVa
186186
// └─────────────────────┴──────────────────────────┘
187187

188188
/// Returns true if the host function has been already released, otherwise false.
189-
@_expose(wasm, "swjs_call_host_function")
190-
public func _call_host_function_impl(
189+
@_cdecl("_call_host_function_impl")
190+
func _call_host_function_impl(
191191
_ hostFuncRef: JavaScriptHostFuncRef,
192192
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
193193
_ callbackFuncRef: JavaScriptObjectRef
@@ -217,8 +217,9 @@ extension JSClosure {
217217
}
218218
}
219219

220-
@_expose(wasm, "swjs_free_host_function")
221-
public func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {}
220+
221+
@_cdecl("_free_host_function_impl")
222+
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {}
222223

223224
#else
224225

@@ -229,8 +230,25 @@ extension JSClosure {
229230

230231
}
231232

232-
@_expose(wasm, "swjs_free_host_function")
233-
public func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {
233+
@_cdecl("_free_host_function_impl")
234+
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {
234235
JSClosure.sharedClosures[hostFuncRef] = nil
235236
}
236237
#endif
238+
239+
#if hasFeature(Embedded)
240+
// cdecls currently don't work in embedded, and expose for wasm only works >=6.0
241+
@_expose(wasm, "swjs_call_host_function")
242+
public func _swjs_call_host_function(
243+
_ hostFuncRef: JavaScriptHostFuncRef,
244+
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
245+
_ callbackFuncRef: JavaScriptObjectRef) -> Bool {
246+
247+
_call_host_function_impl(hostFuncRef, argv, argc, callbackFuncRef)
248+
}
249+
250+
@_expose(wasm, "swjs_free_host_function")
251+
public func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {
252+
_free_host_function_impl(hostFuncRef)
253+
}
254+
#endif

Diff for: Sources/_CJavaScriptKit/_CJavaScriptKit.c

+32-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ extern void *memcpy (void *__restrict, const void *__restrict, size_t);
1313
#include <stdbool.h>
1414

1515
#endif
16+
/// The compatibility runtime library version.
17+
/// Notes: If you change any interface of runtime library, please increment
18+
/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`.
19+
__attribute__((export_name("swjs_library_version")))
20+
int swjs_library_version(void) {
21+
return 708;
22+
}
1623

1724
__attribute__((export_name("swjs_prepare_host_function_call")))
1825
void *swjs_prepare_host_function_call(const int argc) {
@@ -24,14 +31,33 @@ void swjs_cleanup_host_function_call(void *argv_buffer) {
2431
free(argv_buffer);
2532
}
2633

27-
/// The compatibility runtime library version.
28-
/// Notes: If you change any interface of runtime library, please increment
29-
/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`.
30-
__attribute__((export_name("swjs_library_version")))
31-
int swjs_library_version(void) {
32-
return 708;
34+
#ifndef __Embedded
35+
// cdecls don't work in Embedded, also @_expose(wasm) can be used with Swift >=6.0
36+
bool _call_host_function_impl(const JavaScriptHostFuncRef host_func_ref,
37+
const RawJSValue *argv, const int argc,
38+
const JavaScriptObjectRef callback_func);
39+
40+
__attribute__((export_name("swjs_call_host_function")))
41+
bool swjs_call_host_function(const JavaScriptHostFuncRef host_func_ref,
42+
const RawJSValue *argv, const int argc,
43+
const JavaScriptObjectRef callback_func) {
44+
return _call_host_function_impl(host_func_ref, argv, argc, callback_func);
45+
}
46+
47+
void _free_host_function_impl(const JavaScriptHostFuncRef host_func_ref);
48+
49+
__attribute__((export_name("swjs_free_host_function")))
50+
void swjs_free_host_function(const JavaScriptHostFuncRef host_func_ref) {
51+
_free_host_function_impl(host_func_ref);
3352
}
3453

54+
int _library_features(void);
55+
56+
__attribute__((export_name("swjs_library_features")))
57+
int swjs_library_features(void) {
58+
return _library_features();
59+
}
60+
#endif
3561
#endif
3662

3763
_Thread_local void *swjs_thread_local_closures;

0 commit comments

Comments
 (0)