Skip to content

[embind] Don't truncate unsigned long under wasm64 #24678

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
Jul 11, 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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ See docs/process.md for more on how version tagging works.
wrapped with `WebAssembly.Suspending` functions. To automatically wrap library
functions for use with JSPI they must now explicitly set
`myLibraryFunction__async: true`.
- Removed special casing for `size_t` in Embind, since it was also inadvertently
affecting `unsigned long` on wasm64. Both will now match the behaviour of
other 64-bit integers on wasm64 and will be passed as `bigint` instead of
`number` to the JavaScript code. (#24678)

4.0.10 - 06/07/25
-----------------
Expand Down
54 changes: 33 additions & 21 deletions system/include/emscripten/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,6 @@ struct SignatureCode : __em_asm_sig<int> {};
template<typename T>
struct SignatureCode<T, decltype(__em_asm_sig<T>::value)> : __em_asm_sig<T> {};

// TODO: should we add this override to em_asm?
// Most places, including Embind, use `p` for `size_t` (aka `unsigned long`) but
// `em_asm` uses platform-specific code instead which represents `unsigned long`
// as a JavaScript `number` on wasm32 and as a `BigInt` on wasm64.
template<>
struct SignatureCode<size_t> : __em_asm_sig<void*> {};

template<typename T>
struct SignatureCode<T&> : SignatureCode<T*> {};

Expand Down Expand Up @@ -1986,7 +1979,7 @@ struct VectorAccess {
#if __cplusplus >= 201703L
static std::optional<typename VectorType::value_type> get(
const VectorType& v,
typename VectorType::size_type index
unsigned int index
) {
if (index < v.size()) {
return v[index];
Expand All @@ -1997,7 +1990,7 @@ struct VectorAccess {
#else
static val get(
const VectorType& v,
typename VectorType::size_type index
unsigned int index
) {
if (index < v.size()) {
return val(v[index], allow_raw_pointers());
Expand All @@ -2009,12 +2002,31 @@ struct VectorAccess {

static bool set(
VectorType& v,
typename VectorType::size_type index,
unsigned int index,
const typename VectorType::value_type& value
) {
v[index] = value;
return true;
}

static unsigned int size(const VectorType& v) {
return v.size();
}

static void resize(
VectorType& v,
unsigned int len,
const typename VectorType::value_type& value
) {
v.resize(len, value);
}

static void push_back(
VectorType& v,
typename VectorType::value_type&& value
) {
v.push_back(std::move(value));
}
};

} // end namespace internal
Expand All @@ -2026,16 +2038,13 @@ class_<std::vector<T, Allocator>> register_vector(const char* name) {
register_optional<T>();
#endif

void (VecType::*push_back)(const T&) = &VecType::push_back;
void (VecType::*resize)(const size_t, const T&) = &VecType::resize;
size_t (VecType::*size)() const = &VecType::size;
return class_<std::vector<T>>(name)
return class_<VecType>(name)
.template constructor<>()
.function("push_back", push_back, allow_raw_pointers())
.function("resize", resize, allow_raw_pointers())
.function("size", size)
.function("get", &internal::VectorAccess<VecType>::get, allow_raw_pointers())
.function("set", &internal::VectorAccess<VecType>::set, allow_raw_pointers())
.function("push_back", internal::VectorAccess<VecType>::push_back, allow_raw_pointers())
.function("resize", internal::VectorAccess<VecType>::resize, allow_raw_pointers())
.function("size", internal::VectorAccess<VecType>::size, allow_raw_pointers())
.function("get", internal::VectorAccess<VecType>::get, allow_raw_pointers())
.function("set", internal::VectorAccess<VecType>::set, allow_raw_pointers())
;
}

Expand Down Expand Up @@ -2093,6 +2102,10 @@ struct MapAccess {
}
return keys;
}

static unsigned int size(const MapType& m) {
return m.size();
}
};

} // end namespace internal
Expand All @@ -2105,10 +2118,9 @@ class_<std::map<K, V, Compare, Allocator>> register_map(const char* name) {
register_optional<V>();
#endif

size_t (MapType::*size)() const = &MapType::size;
return class_<MapType>(name)
.template constructor<>()
.function("size", size)
.function("size", internal::MapAccess<MapType>::size)
.function("get", internal::MapAccess<MapType>::get)
.function("set", internal::MapAccess<MapType>::set)
.function("keys", internal::MapAccess<MapType>::keys)
Expand Down
2 changes: 1 addition & 1 deletion test/embind/embind.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ module({
assert.equal(2147483648, cm.load_unsigned_int());

cm.store_unsigned_long(2147483648);
assert.equal(2147483648, cm.load_unsigned_long());
assert.equal(cm.getCompilerSetting('MEMORY64') ? 2147483648n : 2147483648, cm.load_unsigned_long());
});

if (cm.getCompilerSetting('ASSERTIONS')) {
Expand Down