Skip to content

Commit 8ab148a

Browse files
Fix metadata accessor function pointer representation on WASI
The metadata accessor function pointer is represented as a relative pointer on all platforms except for WebAssembly, where it is represented as an absolute pointer. This is because WebAssembly does not support relative function pointers. See forum discussion for more context: https://forums.swift.org/t/wasm-support/16087/39
1 parent 55c717d commit 8ab148a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Sources/_TestingInternals/Discovery.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ struct SWTRelativePointer {
8686
}
8787
};
8888

89+
/// A type representing a 32-bit absolute function pointer, usually used on platforms
90+
/// where relative function pointers are not supported.
91+
///
92+
/// This type is derived from `AbsoluteFunctionPointer` in the Swift repository.
93+
template <typename T>
94+
struct SWTAbsoluteFunctionPointer {
95+
private:
96+
T *_pointer;
97+
static_assert(sizeof(T *) == sizeof(int32_t), "Function pointer must be 32-bit when using compact absolute pointer");
98+
99+
public:
100+
const T *_Nullable get(void) const & {
101+
return _pointer;
102+
}
103+
104+
const T *_Nullable operator ->(void) const & {
105+
return get();
106+
}
107+
};
108+
89109
/// A type representing a pointer relative to itself with low bits reserved for
90110
/// use as flags.
91111
///
@@ -98,6 +118,13 @@ struct SWTRelativePointerIntPair: public SWTRelativePointer<T, maskValue> {
98118
}
99119
};
100120

121+
template <typename T>
122+
#if defined(__wasm32__)
123+
using SWTCompactFunctionPointer = SWTAbsoluteFunctionPointer<T>;
124+
#else
125+
using SWTCompactFunctionPointer = SWTRelativePointer<T>;
126+
#endif
127+
101128
/// A type representing a metatype as constructed during compilation of a Swift
102129
/// module.
103130
///
@@ -114,7 +141,7 @@ struct SWTTypeContextDescriptor {
114141
size_t state;
115142
};
116143
using MetadataAccessFunction = __attribute__((swiftcall)) MetadataAccessResponse(size_t);
117-
SWTRelativePointer<MetadataAccessFunction> _metadataAccessFunction;
144+
SWTCompactFunctionPointer<MetadataAccessFunction> _metadataAccessFunction;
118145

119146
public:
120147
const char *_Nullable getName(void) const& {

0 commit comments

Comments
 (0)