Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed May 7, 2022
1 parent bd2bcc8 commit 2bc55b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
17 changes: 13 additions & 4 deletions Realm/Realm/Handles/SharedRealmHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static class NativeMethods

[return: MarshalAs(UnmanagedType.U1)]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate bool ShouldCompactCallback(IntPtr managedDelegate, ulong totalSize, ulong dataSize);
internal delegate bool ShouldCompactCallback(IntPtr managedDelegate, ulong totalSize, ulong dataSize, [MarshalAs(UnmanagedType.U1)] ref bool should_compact);

[return: MarshalAs(UnmanagedType.U1)]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
Expand Down Expand Up @@ -735,11 +735,20 @@ private static bool OnMigration(IntPtr oldRealmPtr, IntPtr newRealmPtr, IntPtr m
}

[MonoPInvokeCallback(typeof(NativeMethods.ShouldCompactCallback))]
private static bool ShouldCompactOnLaunchCallback(IntPtr delegatePtr, ulong totalSize, ulong dataSize)
private static bool ShouldCompactOnLaunchCallback(IntPtr delegatePtr, ulong totalSize, ulong dataSize, ref bool shouldCompact)
{
var handle = GCHandle.FromIntPtr(delegatePtr);
var compactDelegate = (ShouldCompactDelegate)handle.Target;
return compactDelegate(totalSize, dataSize);
var compactDelegate = (CallbackWrapper<ShouldCompactDelegate>)handle.Target;
try
{
shouldCompact = compactDelegate.Value(totalSize, dataSize);
return true;
}
catch (Exception ex)
{
compactDelegate.ManagedException = ex;
return false;
}
}

[MonoPInvokeCallback(typeof(NativeMethods.InitializationCallback))]
Expand Down
9 changes: 7 additions & 2 deletions wrappers/src/shared_realm_cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ using GetNativeSchemaT = void(SchemaForMarshaling schema, void* managed_callback
using ReleaseGCHandleT = void(void* managed_handle);
using LogMessageT = void(realm_value_t message, util::Logger::Level level);
using MigrationCallbackT = bool(realm::SharedRealm* old_realm, realm::SharedRealm* new_realm, Schema* migration_schema, SchemaForMarshaling, uint64_t schema_version, void* managed_migration_handle);
using ShouldCompactCallbackT = bool(void* managed_delegate, uint64_t total_size, uint64_t data_size);
using ShouldCompactCallbackT = bool(void* managed_delegate, uint64_t total_size, uint64_t data_size, bool* should_compact);
using DataInitializationCallbackT = bool(void* managed_delegate, realm::SharedRealm* realm);
namespace realm {
std::function<ObjectNotificationCallbackT> s_object_notification_callback;
Expand Down Expand Up @@ -248,7 +248,12 @@ REALM_EXPORT SharedRealm* shared_realm_open(Configuration configuration, SchemaO

if (configuration.managed_should_compact_delegate) {
config.should_compact_on_launch_function = [&configuration](uint64_t total_bytes, uint64_t used_bytes) {
return s_should_compact(configuration.managed_should_compact_delegate, total_bytes, used_bytes);
bool result;
if (!s_should_compact(configuration.managed_should_compact_delegate, total_bytes, used_bytes, &result)) {
throw ManagedExceptionDuringCallback("Exception occurred in a Realm ShouldCompact callback.");
}

return result;
};
}

Expand Down

0 comments on commit 2bc55b7

Please sign in to comment.