Skip to content

Use @_expose(wasm) instead of __attribute__((export_name)) and @_cdecl hack #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Int8>) -> Int {
Expand Down
8 changes: 0 additions & 8 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion Runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions Sources/JavaScriptKit/Features.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 12 additions & 24 deletions Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawJSValue>, _ argc: Int32,
_ callbackFuncRef: JavaScriptObjectRef
Expand Down Expand Up @@ -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

Expand All @@ -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<RawJSValue>, _ 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
4 changes: 0 additions & 4 deletions Sources/JavaScriptKit/Runtime/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Sources/JavaScriptKit/Runtime/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions Sources/_CJavaScriptKit/_CJavaScriptKit.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ extern void *memcpy (void *__restrict, const void *__restrict, size_t);
#include <stdbool.h>

#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) {
Expand All @@ -35,31 +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 _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) {
Expand Down