Skip to content
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

Fix .NET Native compilation #2170

Merged
merged 2 commits into from
Jan 4, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* None

### Fixed
* None
* Worked around an issue with the .NET Native compiler (used in UWP projects) that would result in the following exception being thrown in Release: `Incompatible MarshalAs detected in parameter named 'value'. Please refer to MCG's warning message for more information.`. (Issue [#2169](https://github.com/realm/realm-dotnet/issues/2169))

### Enhancements
* Add support for the `GUID` data type. It can be used as primary key and is indexable. (PR [#2120](https://github.com/realm/realm-dotnet/pull/2120))
Expand Down
8 changes: 2 additions & 6 deletions Realm/Realm/Native/PrimitiveValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ namespace Realms.Native
[DebuggerDisplay("PrimitiveValue({Type})")]
internal unsafe struct PrimitiveValue
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.U1)]
private bool bool_value;
Comment on lines -31 to -33
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the [MarshalAs] attribute from bool_value appears to fix the issue.


[FieldOffset(0)]
private long int_value;

Expand Down Expand Up @@ -76,7 +72,7 @@ internal unsafe struct PrimitiveValue
public static PrimitiveValue Bool(bool value) => new PrimitiveValue
{
Type = RealmValueType.Bool,
bool_value = value
int_value = value ? 1 : 0,
};

public static PrimitiveValue NullableBool(bool? value) => value.HasValue ? Bool(value.Value) : Null();
Expand Down Expand Up @@ -202,7 +198,7 @@ public static PrimitiveValue Object(ObjectHandle handle)
};
}

public bool AsBool() => bool_value;
public bool AsBool() => int_value == 1;

public long AsInt() => int_value;

Expand Down
26 changes: 18 additions & 8 deletions Tests/Tests.UWP/Tests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
<AssemblyName>Tests.UWP</AssemblyName>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewers can ignore changes to this file - since we don't run UWP tests automatically, the project file had rotted a little and needed to bring it up to date to be able to debug things.

<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.16299.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.19041.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<RealmWrappersPlatform>$(Platform)</RealmWrappersPlatform>
<RealmWrappersPlatform Condition="'$(Platform)' == 'x86'">Win32</RealmWrappersPlatform>
<WindowsXamlEnableOverview>true</WindowsXamlEnableOverview>
<PackageCertificateKeyFile>Tests.UWP_TemporaryKey.pfx</PackageCertificateKeyFile>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
Expand Down Expand Up @@ -123,21 +122,32 @@
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.8" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.11" />
<PackageReference Include="NUnit.Xamarin.Realm" Version="3.12.0"/>
<PackageReference Include="PCLStorage" Version="1.0.2" />
<PackageReference Include="Xamarin.Forms" Version="3.6.0.293080" />
<PackageReference Include="NUnit.Xamarin.Realm">
<Version>3.12.0</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1687" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Realm.Tests\Realm.Tests.csproj">
<Project>{78799fcd-be2e-4ece-a52e-662846615b58}</Project>
<Name>Realm.Tests</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup Condition="'$(UseRealmNupkgsWithVersion)' == ''">
<None Include="..\..\wrappers\build\WindowsStore\$(Configuration)-x64\realm-wrappers.dll" Condition="Exists('..\..\wrappers\build\WindowsStore\$(Configuration)-x64\realm-wrappers.dll')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\..\wrappers\build\WindowsStore\$(Configuration)-Win32\realm-wrappers.dll" Condition="Exists('..\..\wrappers\build\WindowsStore\$(Configuration)-Win32\realm-wrappers.dll')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\..\Realm\Realm\Realm.csproj" PrivateAssets="None">
<Project>{C0DB0E07-4FBC-44D1-8E84-A074497385C6}</Project>
<Name>Realm</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<Import Project="..\..\Realm\Realm.Fody\InSolutionWeaver.props" Condition="'$(UseRealmNupkgsWithVersion)' == ''" />
</Project>
3 changes: 3 additions & 0 deletions wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ if(APPLE)
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "^Windows")
add_definitions(/DWIN32_LEAN_AND_MEAN)
add_compile_options(
/MP # Enable multi-processor compilation
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fealebenpae added this since it seemed like we didn't compile the wrapper sources in parallel.

)

# always produce a PDB
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
Expand Down
9 changes: 6 additions & 3 deletions wrappers/src/marshalling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ typedef struct realm_uuid {
typedef struct realm_value {
union {
int64_t integer;
bool boolean;
realm_string_t string;
realm_binary_t binary;
realm_timestamp_t timestamp;
Expand All @@ -96,6 +95,10 @@ typedef struct realm_value {
bool is_null() {
return type == realm_value_type::RLM_TYPE_NULL;
}

bool boolean() {
return integer == 1;
}
} realm_value_t;

static inline realm_string_t to_capi(StringData data)
Expand Down Expand Up @@ -265,7 +268,7 @@ static inline Mixed from_capi(realm_value_t val)
case realm_value_type::RLM_TYPE_INT:
return Mixed{ val.integer };
case realm_value_type::RLM_TYPE_BOOL:
return Mixed{ val.boolean };
return Mixed{ val.boolean() };
case realm_value_type::RLM_TYPE_STRING:
return Mixed{ from_capi(val.string) };
case realm_value_type::RLM_TYPE_BINARY:
Expand Down Expand Up @@ -322,7 +325,7 @@ static inline realm_value_t to_capi(Mixed value)
}
case type_Bool: {
val.type = realm_value_type::RLM_TYPE_BOOL;
val.boolean = value.get<bool>();
val.integer = value.get<bool>() ? 1 : 0;
break;
}
case type_String: {
Expand Down
4 changes: 2 additions & 2 deletions wrappers/src/query_cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ REALM_EXPORT void query_primitive_equal(Query& query, SharedRealm& realm, size_t
case realm_value_type::RLM_TYPE_NULL:
throw std::runtime_error("Comparing null values should be done via query_null_equal. If you get this error, please report it to help@realm.io.");
case realm_value_type::RLM_TYPE_BOOL:
query.equal(std::move(col_key), primitive.boolean);
query.equal(std::move(col_key), primitive.boolean());
break;
case realm_value_type::RLM_TYPE_INT:
query.equal(std::move(col_key), primitive.integer);
Expand Down Expand Up @@ -173,7 +173,7 @@ REALM_EXPORT void query_primitive_not_equal(Query& query, SharedRealm& realm, si
case realm_value_type::RLM_TYPE_NULL:
throw std::runtime_error("Comparing null values should be done via query_null_equal. If you get this error, please report it to help@realm.io.");
case realm_value_type::RLM_TYPE_BOOL:
query.not_equal(std::move(col_key), primitive.boolean);
query.not_equal(std::move(col_key), primitive.boolean());
break;
case realm_value_type::RLM_TYPE_INT:
query.not_equal(std::move(col_key), primitive.integer);
Expand Down