Skip to content

Commit 581d2ae

Browse files
authored
Simplify EMSCRIPTEN_BINDINGS macro. NFC (#16092)
At least for me its easier to see the intent this way. Its also slightly more effecient as this function ends up directly getting called from `__wasm_call_ctors` rather can getting includes in another per-object file dispatch function for C++ statics. Also, avoid the use the macro in `bind.cpp` where is didn't seem to convey its intent.
1 parent 9a0fa6b commit 581d2ae

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

system/include/emscripten/bind.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,10 +1884,9 @@ void constant(const char* name, const ConstantType& v) {
18841884
static_cast<double>(asGenericValue(BT::toWireType(v))));
18851885
}
18861886

1887-
} // end namespace emscripten
1887+
// EMSCRIPTEN_BINDINGS simple creates a static constructor function which
1888+
// will get included in the program if the translation unit in which it is
1889+
// define gets linked into the program.
1890+
#define EMSCRIPTEN_BINDINGS(name) __attribute__((constructor)) static void __embind_init_##name(void)
18881891

1889-
#define EMSCRIPTEN_BINDINGS(name) \
1890-
static struct EmscriptenBindingInitializer_##name { \
1891-
EmscriptenBindingInitializer_##name(); \
1892-
} EmscriptenBindingInitializer_##name##_instance; \
1893-
EmscriptenBindingInitializer_##name::EmscriptenBindingInitializer_##name()
1892+
} // end namespace emscripten

system/lib/embind/bind.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ template <typename T> static void register_memory_view(const char* name) {
100100
} // namespace
101101

102102
extern "C" {
103-
void EMSCRIPTEN_KEEPALIVE __embind_register_native_and_builtin_types() {
103+
104+
// Normal initialization, executed through a global constructor. This
105+
// happens on the main thread; pthreads will call it manually, so make
106+
// sure we also export it (via EMSCRIPTEN_KEEPALIVE).
107+
EMSCRIPTEN_KEEPALIVE __attribute__((constructor))
108+
void __embind_register_native_and_builtin_types() {
104109
using namespace emscripten::internal;
105110

106111
_embind_register_void(TypeID<void>::get(), "void");
@@ -161,10 +166,5 @@ void EMSCRIPTEN_KEEPALIVE __embind_register_native_and_builtin_types() {
161166
register_memory_view<long double>("emscripten::memory_view<long double>");
162167
#endif
163168
}
164-
}
165169

166-
EMSCRIPTEN_BINDINGS(native_and_builtin_types) {
167-
// Normal initialization, executed through a global constructor. This
168-
// happens on the main thread; pthreads will call it manually.
169-
__embind_register_native_and_builtin_types();
170-
}
170+
} // extern "C"

0 commit comments

Comments
 (0)