Skip to content

Commit 5ce06a3

Browse files
zhuoweikateinoigakukun
authored andcommitted
attempt to fix swift_once; doesn't work
1 parent 53d2e91 commit 5ce06a3

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

include/swift/Runtime/Once.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ typedef std::once_flag swift_once_t;
4444
/// extent of type swift_once_t.
4545
SWIFT_RUNTIME_EXPORT
4646
void swift_once(swift_once_t *predicate, void (*fn)(void *), void *context);
47+
#ifdef __wasm__
48+
// WebAssembly: hack
49+
void swift_once_real(swift_once_t *predicate, void (*fn)(void *), void *context);
50+
#endif
4751

4852
}
4953

stdlib/public/runtime/CompatibilityOverride.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ static_assert(std::is_pod<OverrideSection>::value,
4949
static OverrideSection *getOverrideSectionPtr() {
5050
static OverrideSection *OverrideSectionPtr;
5151
static swift_once_t Predicate;
52+
// WebAssembly: hack
53+
#ifdef __wasm__
54+
swift_once_real(&Predicate, [](void *) {
55+
#else
5256
swift_once(&Predicate, [](void *) {
57+
#endif
5358
size_t Size;
5459
OverrideSectionPtr = static_cast<OverrideSection *>(
5560
lookupSection("__DATA", "__swift52_hooks", &Size));

stdlib/public/runtime/HeapObject.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ swift::swift_initStaticObject(HeapMetadata const *metadata,
152152
// refcount to 1 while another thread already incremented it - and would
153153
// decrement it to 0 afterwards.
154154
InitStaticObjectContext Ctx = { object, metadata };
155+
#ifdef __wasm__
156+
// WebAssembly: hack: swift_once has been modified to take a function pointer without a parameter.
157+
// so use the _real version that does have a parameter
158+
swift_once_real(token, initStaticObjectWithContext, &Ctx);
159+
#else
155160
swift_once(token, initStaticObjectWithContext, &Ctx);
161+
#endif
156162

157163
return object;
158164
}

stdlib/public/runtime/Once.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,28 @@ void swift::swift_once(swift_once_t *predicate, void (*fn)(void *),
5252
dispatch_once_f(predicate, context, fn);
5353
#elif defined(__CYGWIN__)
5454
_swift_once_f(predicate, context, fn);
55+
#elif defined(__wasm__)
56+
// WebAssembly: hack: Swift compiler passes in a fn that doesn't take a parameter,
57+
// which is invalid in WebAssembly. So swift_once casts the function.
58+
// The correct way to fix this is to change
59+
// SILGenModule::emitLazyGlobalInitializer
60+
// but this is OK as a proof of concept.
61+
// Keep a copy of the unmodified swift_once function below.
62+
std::call_once(*predicate, [fn, context]() { ((void (*)())fn)(); });
5563
#else
5664
std::call_once(*predicate, [fn, context]() { fn(context); });
5765
#endif
5866
}
67+
68+
#ifdef __wasm__
69+
void swift::swift_once_real(swift_once_t *predicate, void (*fn)(void *),
70+
void *context) {
71+
#if defined(__APPLE__)
72+
dispatch_once_f(predicate, context, fn);
73+
#elif defined(__CYGWIN__)
74+
_swift_once_f(predicate, context, fn);
75+
#else
76+
std::call_once(*predicate, [fn, context]() { fn(context); });
77+
#endif
78+
}
79+
#endif

0 commit comments

Comments
 (0)