Skip to content

Commit cc850ed

Browse files
committed
[WebAssembly] Use absolute pointers #ifdef __WASM__
1 parent fa4800b commit cc850ed

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

include/swift/Basic/RelativePointer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
146146
std::is_signed<Offset>::value,
147147
"offset type should be signed integer");
148148

149+
#if defined(__WASM__)
150+
// WebAssembly target uses only absolute pointers.
151+
// See https://forums.swift.org/t/wasm-support/16087/17 for more details.
152+
return (uintptr_t)(intptr_t)offset;
153+
#endif
154+
149155
auto base = reinterpret_cast<uintptr_t>(basePtr);
150156
// We want to do wrapping arithmetic, but with a sign-extended
151157
// offset. To do this in C, we need to do signed promotion to get
@@ -165,6 +171,14 @@ static inline Offset measureRelativeOffset(A *referent, B *base) {
165171
std::is_signed<Offset>::value,
166172
"offset type should be signed integer");
167173

174+
#if defined(__WASM__)
175+
// WebAssembly target uses only absolute pointers.
176+
auto offset = (Offset)(uintptr_t)referent;
177+
assert((intptr_t)offset == (intptr_t)(uintptr_t)referent
178+
&& "pointer too large to fit in offset type");
179+
return offset;
180+
#endif
181+
168182
auto distance = (uintptr_t)referent - (uintptr_t)base;
169183
// Truncate as unsigned, then wrap around to signed.
170184
auto truncatedDistance =

0 commit comments

Comments
 (0)