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

Mapping between .Net objects and JSReference should not allow to match objects of different types. #156

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 24 additions & 1 deletion src/NodeApi/Interop/JSRuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public sealed class JSRuntimeContext : IDisposable
/// </remarks>
public const string GlobalObjectName = "node_api_dotnet";

private static IEqualityComparer<object> s_jsTranslationEqualityComparer = Interop.JSTranslationEqualityComparer.Instance;

private readonly napi_env _env;

// Track JS constructors and instance JS wrappers for exported classes, enabling
Expand Down Expand Up @@ -73,8 +75,11 @@ public sealed class JSRuntimeContext : IDisposable
/// a C# object maps to the same JS instance when marshalled multiple times. The
/// references are weak to allow the JS wrappers to be released; new wrappers are
/// re-constructed as needed.
/// Important! We need to match by reference, otherwise for classes that overrides
/// the <see cref="object.Equals(object?)"/> method, JS can receive unpredictable
/// object instance.
/// </remarks>
private readonly ConcurrentDictionary<object, JSReference> _objectMap = new();
private readonly ConcurrentDictionary<object, JSReference> _objectMap = new(JSTranslationEqualityComparer);

/// <summary>
/// Maps from exported struct types to (strong references to) JS constructors for classes
Expand Down Expand Up @@ -136,6 +141,24 @@ public static explicit operator JSRuntimeContext(napi_env env)
/// thread.</exception>
public static JSRuntimeContext Current => JSValueScope.Current.RuntimeContext;

/// <summary>
/// The equality comparer used to translate .NET objects to JS objects.
/// </summary>
/// <remarks>
/// This property allows for altering the behavior of the translation process from .NET objects to JS objects.
/// If certain types have equality members that block access to required instances, this property can be assigned
/// to a more suitable implementation. See the <see cref="Microsoft.JavaScript.NodeApi.Interop.JSTranslationEqualityComparer"/>
/// comparer for more details.
/// </remarks>
public static IEqualityComparer<object> JSTranslationEqualityComparer
{
get => s_jsTranslationEqualityComparer;
set
{
s_jsTranslationEqualityComparer = value ?? throw new ArgumentNullException(nameof(value));
}
}

public JSRuntime Runtime { get; }

public JSSynchronizationContext SynchronizationContext { get; }
Expand Down
55 changes: 55 additions & 0 deletions src/NodeApi/Interop/JSTranslationEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;

namespace Microsoft.JavaScript.NodeApi.Interop;

/// <summary>
/// Default .Net to JS objects translation equality comparer.
/// </summary>
public class JSTranslationEqualityComparer : IEqualityComparer<object>
{
/// <summary>
/// Single instance of the comparer.
/// </summary>
public static JSTranslationEqualityComparer Instance { get; } = new ();

private JSTranslationEqualityComparer()
{
}

/// <inheritdoc />
public new bool Equals(object? x, object? y)
{
if (x is null)
{
return y is null;
}

if (y is null)
{
return false;
}

// We should deliberately make different types not equal to avoid type definition mismatch.
// Object of type B that matched through A.Equal(B) can be accessed in JS through description of type A, which is wrong.
if (x.GetType() != y.GetType())
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right, and is different from what was discussed. It will return the wrong result for two (equal) instances of the same class, as in the original problem scenario you described.

If x is a reference type then we want to use reference equality, as in the original iteration of this PR. If x is a (boxed) value-type then we want to use value equality. (Value equality should fail if the values are different types.)

I think this should work, but it might need some testing:

return x is ValueType ? x.Equals(y) : Object.ReferenceEquals(x, y);

{
return false;
}

return x.Equals(y);
}

/// <inheritdoc />
public int GetHashCode(object? obj)
{
if (obj is null)
{
return 0;
}

return obj.GetHashCode();
}
}