-
Notifications
You must be signed in to change notification settings - Fork 97
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
Calling .NET WebAssembly code from views #1526
Draft
tomasherceg
wants to merge
15
commits into
main
Choose a base branch
from
feature/csharp-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0edecbd
Prototype of calling C# web assembly implemented
tomasherceg d987837
Merge branch 'main' into feature/csharp-client
tomasherceg 1f15eb0
Calling wasm imlemented - basic scenarios work
tomasherceg 13b73ab
Cleanup and refactoring
tomasherceg 7d685dd
Csharp directive renamed to Dotnet to be more generic
tomasherceg d53795a
UI tests for web assembly added
tomasherceg 9608f85
Fixed startup path
tomasherceg 4bd3e0a
Updated pipeline for wasm tooling
tomasherceg c268eb3
Merge branch 'feature/csharp-client' of https://github.com/riganti/do…
tomasherceg 8124aeb
Fix CI
tomasherceg 20adfaf
Fix CI
tomasherceg 6ba7649
Fix CI
tomasherceg f71bb3a
Fixed nullability problems
tomasherceg f526386
Fixed tests
tomasherceg b990ff4
Fixed tests
tomasherceg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
src/Framework/Framework/Compilation/Binding/DotnetViewModuleMethodTranslator.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using DotVVM.Framework.Binding; | ||
using DotVVM.Framework.Compilation.ControlTree; | ||
using DotVVM.Framework.Compilation.Javascript; | ||
using DotVVM.Framework.Compilation.Javascript.Ast; | ||
using DotVVM.Framework.Utils; | ||
|
||
namespace DotVVM.Framework.Compilation.Binding | ||
{ | ||
public class DotnetViewModuleMethodTranslator : IJavascriptMethodTranslator | ||
{ | ||
public JsExpression? TryTranslateCall(LazyTranslatedExpression? context, LazyTranslatedExpression[] arguments, MethodInfo method) | ||
{ | ||
// ignore static methods | ||
if (context == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// check whether we have the annotation - otherwise the type is not used in the _dotnet context and will not be translated | ||
var annotation = context.OriginalExpression.GetParameterAnnotation(); | ||
if (annotation is null || annotation.ExtensionParameter is not DotnetExtensionParameter extensionParameter) | ||
{ | ||
return null; | ||
} | ||
|
||
// check that the method is callable | ||
if (!method.IsPublic) | ||
{ | ||
throw new DotvvmCompilationException($"Cannot call non-public method {method.DeclaringType!.FullName}.{method.Name} on a @dotnet module!"); | ||
} | ||
if (method.IsAbstract) | ||
{ | ||
throw new DotvvmCompilationException($"Cannot call abstract method {method.DeclaringType!.FullName}.{method.Name} on a @dotnet module!"); | ||
} | ||
if (method.IsGenericMethod || method.IsGenericMethodDefinition) | ||
{ | ||
throw new DotvvmCompilationException($"Cannot call generic method {method.DeclaringType!.FullName}.{method.Name} on a @dotnet module!"); | ||
} | ||
|
||
// check that there are not more overloads | ||
var allOverloads = context.NotNull().OriginalExpression.Type | ||
.GetMethods() | ||
.Where(m => m.Name == method.Name && m.IsPublic); | ||
if (allOverloads.Count() > 1) | ||
{ | ||
throw new DotvvmCompilationException($"There are multiple methods named {method.Name} on a @dotnet module {context.OriginalExpression.Type}! Overloads are not supported on @dotnet modules."); | ||
} | ||
|
||
// translate the method | ||
var viewIdOrElementExpr = extensionParameter.IsMarkupControl ? new JsSymbolicParameter(JavascriptTranslator.CurrentElementParameter) : (JsExpression)new JsLiteral(extensionParameter.Id); | ||
|
||
return new JsIdentifierExpression("dotvvm").Member("viewModules").Member("call") | ||
.Invoke( | ||
viewIdOrElementExpr, | ||
new JsLiteral("dotnetWasmInvoke"), | ||
new JsArrayExpression( | ||
new[] { new JsLiteral(method.Name) } | ||
.Concat(arguments.Select(a => a.JsExpression())) | ||
.ToArray()), | ||
new JsLiteral(true) | ||
) | ||
.WithAnnotation(new ResultIsPromiseAnnotation(e => e)); | ||
} | ||
|
||
} | ||
} |
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
src/Framework/Framework/Compilation/ControlTree/IAbstractDotnetViewModuleDirective.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DotVVM.Framework.Compilation.ControlTree; | ||
|
||
public interface IAbstractDotnetViewModuleDirective : IAbstractDirective | ||
{ | ||
/// <summary>Full type name of the module specified</summary> | ||
ITypeDescriptor? ModuleType { get; } | ||
} |
This file contains 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
18 changes: 18 additions & 0 deletions
18
...Framework/Framework/Compilation/ControlTree/Resolved/ResolvedDotnetViewModuleDirective.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Immutable; | ||
using DotVVM.Framework.Compilation.Parser.Binding.Parser; | ||
using DotVVM.Framework.Compilation.Parser.Dothtml.Parser; | ||
|
||
namespace DotVVM.Framework.Compilation.ControlTree.Resolved; | ||
|
||
/// <summary> Represents the @dotnet directive - import .NET WASM module on the client side </summary> | ||
public class ResolvedDotnetViewModuleDirective : ResolvedDirective, IAbstractDotnetViewModuleDirective | ||
{ | ||
/// <summary>Full .NET type of the module</summary> | ||
public ITypeDescriptor? ModuleType { get; } | ||
|
||
public ResolvedDotnetViewModuleDirective(DirectiveCompilationService directiveCompilationService, DothtmlDirectiveNode node, BindingParserNode typeName, ImmutableList<NamespaceImport> imports) | ||
: base(node) | ||
{ | ||
ModuleType = directiveCompilationService.ResolveType(node, typeName, imports); | ||
} | ||
} |
This file contains 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
6 changes: 6 additions & 0 deletions
6
src/Framework/Framework/Compilation/Directives/DotnetViewModuleCompilationResult.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using DotVVM.Framework.Binding; | ||
using DotVVM.Framework.Compilation.ControlTree; | ||
|
||
namespace DotVVM.Framework.Compilation.Directives; | ||
|
||
public record DotnetViewModuleCompilationResult(DotnetExtensionParameter ExtensionParameter, ViewModuleReferenceInfo Reference); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't compile with
object
.However, I'm thinking if we want to allow invoking the methods server-side. I guess it makes sense, but passing null into the constructor will just make it fail on the unexpected null reference. Maybe only allow it if the type also has parameterless constructor?