Skip to content

Commit

Permalink
Make AppClass::transport_generator initialized at const-init time (#3665
Browse files Browse the repository at this point in the history
)

This fixes the "bad_function_call" issue where it was invoked while null
(probably actually uninitialized). I'm not sure why it wasn't
initialized yet, since the compiler should have ensured that it was.
There is likely a compiler bug there but I'll need to dig in more. To
work around that, I made it a simple function pointer, which lambdas can
implicitly convert to, and all of that should be possible at const-init
time (basically, it should be burned into the binary image with nothing
happening at runtime). Unfortunately due to a different compiler bug,
(https://developercommunity.visualstudio.com/t/const-init-of-function-pointers-from-lambdas/1383098),
this requires putting a `+` in front of the lambda to explicitly decay
it to a function pointer, rather than relying on the implicit conversion.
  • Loading branch information
RedBeard0531 authored and kneth committed Mar 29, 2021
1 parent f3b7c9f commit b8ce8c3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/js_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AppClass : public ClassDefinition<T, SharedApp> {
* Generates instances of GenericNetworkTransport, eventually allowing Realm Object Store to perform network requests.
* Exposed to allow other components (ex the RPCServer) to override the underlying implementation.
*/
static inline NetworkTransportFactory transport_generator = [] (ContextType ctx, typename NetworkTransport::Dispatcher eld) {
static inline NetworkTransportFactory transport_generator = +[] (ContextType ctx, typename NetworkTransport::Dispatcher eld) -> std::unique_ptr<app::GenericNetworkTransport> {
return std::make_unique<NetworkTransport>(ctx, std::move(eld));
};

Expand Down
4 changes: 3 additions & 1 deletion src/js_network_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ struct JavaScriptNetworkTransport : public app::GenericNetworkTransport {
}

using SendRequestHandler = void(ContextType m_ctx, const app::Request request, std::function<void(const app::Response)> completion_callback);
using NetworkTransportFactory = std::function<std::unique_ptr<app::GenericNetworkTransport>(ContextType, Dispatcher)>;

// This needs to be a plain function pointer to ensure that AppClass::transport_factory is correctly initialized on MSVC builds.
using NetworkTransportFactory = std::unique_ptr<app::GenericNetworkTransport>(*)(ContextType, Dispatcher);

JavaScriptNetworkTransport(ContextType ctx, Dispatcher eld) : m_ctx(ctx), m_dispatcher(std::move(eld)) {};

Expand Down
5 changes: 3 additions & 2 deletions src/jsc/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ using json = nlohmann::json;

using RPCObjectID = u_int64_t;
using RPCRequest = std::function<json(const json)>;
using NetworkTransportFactory = typename js::JavaScriptNetworkTransport<jsc::Types>::NetworkTransportFactory;
using NetworkTransport = js::JavaScriptNetworkTransport<jsc::Types>;
using NetworkTransportFactory = typename NetworkTransport::NetworkTransportFactory;

using Value = js::Value<jsc::Types>;
using Accessor = realm::js::NativeAccessor<jsc::Types>;
Expand Down Expand Up @@ -347,7 +348,7 @@ RPCServerImpl::RPCServerImpl() {

// Make the App use the RPC Network Transport from now on
previous_transport_generator = AppClass::transport_generator;
AppClass::transport_generator = [] (jsc::Types::Context ctx, auto&& dispatcher) {
AppClass::transport_generator = [] (jsc::Types::Context ctx, NetworkTransport::Dispatcher dispatcher) -> std::unique_ptr<app::GenericNetworkTransport> {
(void)dispatcher; // We don't need to use the dispatcher because JSC separately guarantees thread-safety.
return std::make_unique<RPCNetworkTransport>(ctx);
};
Expand Down

0 comments on commit b8ce8c3

Please sign in to comment.