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

AOT (Trimming) Exception. #65

Open
cyraid opened this issue Feb 20, 2023 · 8 comments
Open

AOT (Trimming) Exception. #65

cyraid opened this issue Feb 20, 2023 · 8 comments

Comments

@cyraid
Copy link

cyraid commented Feb 20, 2023

The library doesn't seem to work when producing a self contained, trimmed, executable.
The code of the program is:

using YantraJS.Core;
using var ctx = new JSContext();

Console.WriteLine(ctx.Eval("'Hi'").ToString());

Running without trimming works, and prints Hi. When trimming is on, running said executable produces:

Unhandled exception. System.MissingMethodException: Cannot dynamically create an instance of type 'YantraJS.Core.JSJSON'. Reason: No parameterless constructor defined.
   at System.RuntimeType.ActivatorCache..ctor(RuntimeType)
   at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at YantraJS.Core.JSContext.<.ctor>g__CreateInternalObject|74_0[T](KeyString)
   at YantraJS.Core.JSContext..ctor(SynchronizationContext )
   at Program.<Main>$(String[]) in D:\Projects\GameEditor\GamePlayer.Desktop\Program.cs:line 2
@ackava
Copy link
Contributor

ackava commented Feb 21, 2023

@cyraid Trimming will not work as JavaScript is dynamic language and the all the runtime engine is loaded dynamically. For timebing you can simply disable trimming and see if it works, I tried looking at following issues, but looks like there isn't any easy way to do it. dotnet/runtime#49040 , dotnet/runtime#79003

@cyraid
Copy link
Author

cyraid commented Feb 22, 2023

@ackava With the help of a few hints (at https://learn.microsoft.com/en-ca/dotnet/core/deploying/trimming/prepare-libraries-for-trimming), you could avoid the trim warnings and allow the library to at least be partially trimmed, saving space. :)

Edit: By having [DynamicallyAccessedMembers(TypeHere)] and [RequiresUnreferencedCode("Message")]

@ackava
Copy link
Contributor

ackava commented Feb 23, 2023

@cyraid This will require every method to be decorated with these attributes, which isn't possible. And it is too much of unnecessary code. Since none of the JavaScript runtime features are statically referenced anywhere in the code.

In Xamarin, there was a way to completely disable trimming (linking) only for specific assemblies. And we were able to use YantraJS successfully in the Xamarin by disabling trimming.

I think if it is not possible to trim entire assemblies, the feature may come in future. In this case, you can supply assemblies (YantraJS.Core, and YantraJS.ExpressionCompiler) as a separate file and load them dynamically via Assembly.Load.

I will keep the issue open till I find some easy way to entirely ignore trimming/linking whole assemblies.

@cyraid
Copy link
Author

cyraid commented Feb 23, 2023

@cyraid This will require every method to be decorated with these attributes, which isn't possible. And it is too much of unnecessary code. Since none of the JavaScript runtime features are statically referenced anywhere in the code.

In Xamarin, there was a way to completely disable trimming (linking) only for specific assemblies. And we were able to use YantraJS successfully in the Xamarin by disabling trimming.

I think if it is not possible to trim entire assemblies, the feature may come in future. In this case, you can supply assemblies (YantraJS.Core, and YantraJS.ExpressionCompiler) as a separate file and load them dynamically via Assembly.Load.

I will keep the issue open till I find some easy way to entirely ignore trimming/linking whole assemblies.

With <TrimmerSingleWarn>false</TrimmerSingleWarn> you can get warnings of which methods specifically would need annotations (should help narrowing down the warnings). Shouldn't need annotations on every method, they're just hints for dynamic methods which the trimmer can't determine if it needs or not.

@cyraid
Copy link
Author

cyraid commented Mar 2, 2023

I think if it is not possible to trim entire assemblies, the feature may come in future. In this case, you can supply assemblies (YantraJS.Core, and YantraJS.ExpressionCompiler) as a separate file and load them dynamically via Assembly.Load.

@ackava You mentioned supplying assemblies to YantraJS.Core and YantraJS.ExpressionCompiler, I wonder.. Is it possible to currently save the JavaScript compiled Assembly to a .dll file?

@ackava
Copy link
Contributor

ackava commented Mar 3, 2023

@cyraid It would not work as every method would need to be present to execute the code. Also compile to assembly is actually used as caching as opposed to shipping assembly as alternative to JS. We haven't tried it yet.

We are working on a new feature to improve faster loading time by statically initializing Runtime features as opposed to using reflection. This will improve speed and might support trimming.

We will keep this issue open till then.

@cyraid
Copy link
Author

cyraid commented Mar 3, 2023

@cyraid It would not work as every method would need to be present to execute the code. Also compile to assembly is actually used as caching as opposed to shipping assembly as alternative to JS. We haven't tried it yet.

We are working on a new feature to improve faster loading time by statically initializing Runtime features as opposed to using reflection. This will improve speed and might support trimming.

We will keep this issue open till then.

Thank you! :)

@ackava
Copy link
Contributor

ackava commented Jun 4, 2023

@cyraid We have removed all Reflection code and we have replaced it with generated source, AOT trimming should work, you can try new release and let us know if you face any issues.

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

No branches or pull requests

2 participants