Skip to content

Commit d6eecb0

Browse files
Revert "Merge pull request #304 from swiftwasm/yt/remove-cdecls"
This reverts commit 1de7acc, reversing changes made to e36e93c.
1 parent 1de7acc commit d6eecb0

File tree

8 files changed

+88
-16
lines changed

8 files changed

+88
-16
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import JavaScriptKit
22

3+
// NOTE: it seems the embedded tree shaker gets rid of these exports if they are not used somewhere
4+
func _i_need_to_be_here_for_wasm_exports_to_work() {
5+
_ = _swjs_library_features
6+
_ = _swjs_call_host_function
7+
_ = _swjs_free_host_function
8+
}
9+
310
// 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
411
@_cdecl("strlen")
512
func strlen(_ s: UnsafePointer<Int8>) -> Int {

Diff for: Runtime/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ export class SwiftRuntime {
5656
`
5757
);
5858
}
59+
if (this.exports.swjs_library_version() != this.version) {
60+
throw new Error(
61+
`The versions of JavaScriptKit are incompatible.
62+
WebAssembly runtime ${this.exports.swjs_library_version()} != JS runtime ${
63+
this.version
64+
}`
65+
);
66+
}
5967
}
6068

6169
main() {

Diff for: Runtime/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type JavaScriptValueKind = number;
77
export type JavaScriptValueKindAndFlags = number;
88

99
export interface ExportedFunctions {
10+
swjs_library_version(): number;
1011
swjs_library_features(): number;
1112
swjs_prepare_host_function_call(size: number): pointer;
1213
swjs_cleanup_host_function_call(argv: pointer): void;

Diff for: Sources/JavaScriptKit/Features.swift

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

5-
@_expose(wasm, "swjs_library_features")
6-
@_cdecl("_swjs_library_features")
7-
@available(*, unavailable)
8-
public func _swjs_library_features() -> Int32 {
5+
@_cdecl("_library_features")
6+
func _library_features() -> Int32 {
97
var features: Int32 = 0
108
#if !JAVASCRIPTKIT_WITHOUT_WEAKREFS
119
features |= LibraryFeatures.weakRefs
1210
#endif
1311
return features
1412
}
13+
14+
#if compiler(>=6.0) && 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-12
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ private func makeAsyncClosure(
197197
// └─────────────────────┴──────────────────────────┘
198198

199199
/// Returns true if the host function has been already released, otherwise false.
200-
@_expose(wasm, "swjs_call_host_function")
201-
@_cdecl("_swjs_call_host_function")
202-
@available(*, unavailable)
203-
public func _swjs_call_host_function(
200+
@_cdecl("_call_host_function_impl")
201+
func _call_host_function_impl(
204202
_ hostFuncRef: JavaScriptHostFuncRef,
205203
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
206204
_ callbackFuncRef: JavaScriptObjectRef
@@ -233,10 +231,9 @@ extension JSClosure {
233231
}
234232
}
235233

236-
@_expose(wasm, "swjs_free_host_function")
237-
@_cdecl("_swjs_free_host_function")
238-
@available(*, unavailable)
239-
func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {}
234+
235+
@_cdecl("_free_host_function_impl")
236+
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {}
240237

241238
#else
242239

@@ -247,10 +244,25 @@ extension JSClosure {
247244

248245
}
249246

250-
@_expose(wasm, "swjs_free_host_function")
251-
@_cdecl("_swjs_free_host_function")
252-
@available(*, unavailable)
253-
func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {
247+
@_cdecl("_free_host_function_impl")
248+
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {
254249
JSClosure.sharedClosures.wrappedValue[hostFuncRef] = nil
255250
}
256251
#endif
252+
253+
#if compiler(>=6.0) && hasFeature(Embedded)
254+
// cdecls currently don't work in embedded, and expose for wasm only works >=6.0
255+
@_expose(wasm, "swjs_call_host_function")
256+
public func _swjs_call_host_function(
257+
_ hostFuncRef: JavaScriptHostFuncRef,
258+
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
259+
_ callbackFuncRef: JavaScriptObjectRef) -> Bool {
260+
261+
_call_host_function_impl(hostFuncRef, argv, argc, callbackFuncRef)
262+
}
263+
264+
@_expose(wasm, "swjs_free_host_function")
265+
public func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {
266+
_free_host_function_impl(hostFuncRef)
267+
}
268+
#endif

Diff for: Sources/JavaScriptKit/Runtime/index.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Sources/JavaScriptKit/Runtime/index.mjs

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Sources/_CJavaScriptKit/_CJavaScriptKit.c

+32
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) {
@@ -28,6 +35,31 @@ void swjs_cleanup_host_function_call(void *argv_buffer) {
2835
// cdecls don't work in Embedded, but @_expose(wasm) can be used with Swift >=6.0
2936
// the previously used `#if __Embedded` did not play well with SwiftPM (defines needed to be on every target up the chain)
3037
#ifdef __wasi__
38+
bool _call_host_function_impl(const JavaScriptHostFuncRef host_func_ref,
39+
const RawJSValue *argv, const int argc,
40+
const JavaScriptObjectRef callback_func);
41+
42+
__attribute__((export_name("swjs_call_host_function")))
43+
bool swjs_call_host_function(const JavaScriptHostFuncRef host_func_ref,
44+
const RawJSValue *argv, const int argc,
45+
const JavaScriptObjectRef callback_func) {
46+
return _call_host_function_impl(host_func_ref, argv, argc, callback_func);
47+
}
48+
49+
void _free_host_function_impl(const JavaScriptHostFuncRef host_func_ref);
50+
51+
__attribute__((export_name("swjs_free_host_function")))
52+
void swjs_free_host_function(const JavaScriptHostFuncRef host_func_ref) {
53+
_free_host_function_impl(host_func_ref);
54+
}
55+
56+
int _library_features(void);
57+
58+
__attribute__((export_name("swjs_library_features")))
59+
int swjs_library_features(void) {
60+
return _library_features();
61+
}
62+
3163
int swjs_get_worker_thread_id_cached(void) {
3264
_Thread_local static int tid = 0;
3365
if (tid == 0) {

0 commit comments

Comments
 (0)