Extension methods runtime support and typedefs#212
Merged
Conversation
vmoroz
approved these changes
Feb 26, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change adds runtime and build-time (typedefs) support for calling .NET extension methods from JS.
Previously it was possible, though awkward, to call extension methods as regular static methods. Now, loading an assembly with extension methods dynamically applies the extension methods to existing JS types and instances. This works by defining additional properties on the JS class prototype. And when the corresponding type definitions for the assembly are loaded by the TS compiler, the extension method declarations are merged with the applicable types.
There are some limitations which are inherent in the way .NET types are projected to JS at runtime, and in how .NET type structure can be represented by TS:
numberconstant values.IEnumerable<>,IList<>,IDictionary<>, etc) are not supported, because those types are projected as JS built-in collection types (Array,Iterable,Set, andMap). While technically it could be possible to modify the prototype of those JS classes, that is not a good practice.Task,BigInteger,Memory<T>,Tuple<>.Aside from those limitations, most things work, including:
MyGenericClass<int>) are supported at runtime (but without typedefs).MyGenericClass<T>) are supported.All of this only works with the "dynamic invocation" scenario, in which assemblies and types are dynamically loaded using reflection and marshalling code is emitted at runtime. Extension methods will NOT be supported for the "dotnet module" scenario (whether using AOT or not), but there is no great need for them there anyway because the module explicitly defines the methods that are exported to JS.
Runtime implementation of extension methods mostly involved refactoring some of the dynamic loading code (previously in
ManagedHost) into new classesNamespaceProxyandTypeProxythat keep track of the type relationships (derived types, constructed generics) as needed in order to dynamically apply extension methods to already-exported types whenever new assemblies are loaded.Type definitions for extension methods became possible after I figured out two things:
.d.tsfile) declare an interface that adds extension methods to the class. And of course extension methods on interfaces can use plain interface merging.typeofkeyword to return a type that represents a generic class's constructor. This avoids the need to split generic classes into a constructor/static interface (previously tagged with an ugly double-dollar) plus an instance interface. And then the same class+interface merging technique can be used for extension methods to generic classes.The
SemanticKernelAPIs make heavy use of extension methods, and I updated that example to use extension methods, so the JS code looks a lot cleaner and more like the equivalent C#.Also as part of this change I improved the way tests and examples use
PackageReferenceto reference the current locally-built version of the packages, using aversion.propsfile generated by thepack.jsscript. Previously some testing required temporarily updating thecsprojfiles to reference a specific build version.Fixes: #126