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

Make JsSet public #1987

Merged
merged 4 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions Jint/Native/JsSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Jint.Native;

internal sealed class JsSet : ObjectInstance
public sealed class JsSet : ObjectInstance
{
internal readonly OrderedSet<JsValue> _set;

Expand Down Expand Up @@ -47,17 +47,17 @@ protected override bool TryGetProperty(JsValue property, [NotNullWhen(true)] out
return base.TryGetProperty(property, out descriptor);
}

internal void Add(JsValue value) => _set.Add(value);
public void Add(JsValue value) => _set.Add(value);

internal void Remove(JsValue value) => _set.Remove(value);
public void Remove(JsValue value) => _set.Remove(value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean rename the method itself to Delete(JsValue value)? Given that it was previously internal, am I safe to assume that all existing consumers that could break as a result are in the same repo? Hopefully refactoring it globally is adequate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, so I see it's only used in SetPrototype, however I wonder if the name "SetDelete" was used to avoid conflicting with ObjectInstance.Delete() which would attempt to delete a property by that name from the object (presumably like javascript's delete foo.bar) whereas we're wanting mySet.delete("abc") to take a value out of the set.


internal void Clear() => _set.Clear();
public void Clear() => _set.Clear();

internal bool Has(JsValue key) => _set.Contains(key);
public bool Has(JsValue key) => _set.Contains(key);

internal bool SetDelete(JsValue key) => _set.Remove(key);
public bool SetDelete(JsValue key) => _set.Remove(key);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove (which should be Delete) already handles this so can be removed altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept this name for now given that "Delete" is inherited. Alternatively I could use new to hide the inherited version although I think that's generally discouraged.


internal void ForEach(ICallable callable, JsValue thisArg)
public void ForEach(ICallable callable, JsValue thisArg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe better keep this internal, very JS spec specific and not a natural c# API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. This also eliminates a warning I was getting because ICallable is internal.

{
var args = _engine._jsValueArrayPool.RentArray(3);
args[2] = this;
Expand All @@ -73,7 +73,7 @@ internal void ForEach(ICallable callable, JsValue thisArg)
_engine._jsValueArrayPool.ReturnArray(args);
}

internal ObjectInstance Entries() => _engine.Realm.Intrinsics.SetIteratorPrototype.ConstructEntryIterator(this);
public ObjectInstance Entries() => _engine.Realm.Intrinsics.SetIteratorPrototype.ConstructEntryIterator(this);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are Entries and Values really needed, they don't expose meaningful API outside of Jint's logic at the moment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, right enough. They're not really needed elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed back to internal.


internal ObjectInstance Values() => _engine.Realm.Intrinsics.SetIteratorPrototype.ConstructValueIterator(this);
public ObjectInstance Values() => _engine.Realm.Intrinsics.SetIteratorPrototype.ConstructValueIterator(this);
}
2 changes: 1 addition & 1 deletion Jint/Runtime/OrderedSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Jint.Runtime;

internal sealed class OrderedSet<T>
public sealed class OrderedSet<T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this can remain as internal, unfortunate need for JS usage where they come up with oddities

Copy link
Collaborator

Choose a reason for hiding this comment

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

So SetConstructor would handle all constructor requirements and JsSet constructor should be internal, like I indicated in earlier discussion, SetConstructor.Construct() would have the needed overloads

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, I wasn't able to compile without this change. I think the alternative would be to downgrade the JsSet constructor that uses it to also be internal. This could potentially be a breaking change as I don't know where all it is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was able to revert this by changing the affected JsSet constructor to internal. Hopefully that doesn't break something elsewhere. I only see it used by SetPrototype.

{
internal List<T> _list;
Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

The generic version ( https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ordereddictionary-2 ) has been added NET 9, maybe not yet worth to add another TFM just for that..

Copy link
Owner

Choose a reason for hiding this comment

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

That's what was confusing me then. I knew the generic one was added to 9, but on the docs it said 2,3....9. I was pointing to the wrong one. It all makes sense and I agree not worth it if just for 9.

internal HashSet<T> _set;
Expand Down
Loading