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

Make JsSet public #1987

merged 4 commits into from
Oct 26, 2024

Conversation

kenlyon
Copy link
Contributor

@kenlyon kenlyon commented Oct 24, 2024

Allow use of the Jint.Native.JsSet class outside of expressions, allowing consumers to cast to this type and call its members.

For example, you might want to convert it to a HashSet<T> to use it elsewhere without being tied to using Jint types.

The OrderedSet class also had to be made public due to its use in the JsSet constructor.


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.

@@ -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.

@@ -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 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 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.

Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

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

I did small adjustments and a public API test to show usage (and ensure API will be kept), did a 180 and went with the Remove method name as SetDelete felt too clunky...

@lahma lahma merged commit 7d1de2d into sebastienros:main Oct 26, 2024
3 checks passed
@lahma
Copy link
Collaborator

lahma commented Oct 26, 2024

Thanks for iterating a publishing the API!

@lahma
Copy link
Collaborator

lahma commented Oct 26, 2024

In #1988 I did another 180 and came to conclusion that in this case the new hiding actually makes sense as you are working with Set and Map semantics with direct type, you probably don't want to do "generic object operations". So JsSet.Delete now maps to Set.prototype.delete.

namespace Jint.Runtime;

internal sealed class OrderedSet<T>
internal sealed class OrderedSet<T> : IEnumerable<T>
{
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.

@kenlyon
Copy link
Contributor Author

kenlyon commented Oct 28, 2024

Thanks for finishing this. I've upgraded to Jint 4.1.0 and these changes are really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants