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

I have a question about AOT (Ahead-of-Time) publishing. #620

Open
lgBlog opened this issue Dec 18, 2024 · 2 comments
Open

I have a question about AOT (Ahead-of-Time) publishing. #620

lgBlog opened this issue Dec 18, 2024 · 2 comments
Assignees
Labels

Comments

@lgBlog
Copy link

lgBlog commented Dec 18, 2024

I have a relatively simple project that just registers one or two classes with V8 from C#. When using AOT publishing, an exception occurs during operation. I downloaded the source code and debugged it step by step, and determined that the exception was caused by calling binder.Bind:

public static DynamicMetaObject Bind(DynamicMetaObjectBinder binder, object target, object[] args)
{
return binder.Bind(CreateDynamicTarget(target), CreateDynamicArgs(args));
}

The exception information is as follows:
Error: Object reference not set to an instance of an object.
at Microsoft.ClearScript.V8.SplitProxy.V8SplitProxyNative.Invoke(Action1) + 0x9d at Microsoft.ClearScript.V8.SplitProxy.V8ContextProxyImpl.InvokeWithLock(Action) + 0x64 at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func1) + 0x82
at TestAot.Program.Main(String[] args) + 0x111

I guess this problem may be caused by dynamic calling, which is incompatible with AOT. This function is called only once in the following location:

private static object BindMethodRaw(BindingFlags bindFlags, InvokeMemberBinder binder, HostTarget target, object[] bindArgs)
{

}

My idea is that if dynamic calling is not supported (using RuntimeFeature.IsDynamicCodeSupported for judgment), reflection is used to obtain the matching method. After a simple test, the corresponding method can be called normally after modification.

I would like to ask if it is appropriate to modify BindMethodRaw if reflection is used to obtain MethodInfo. Are there any other points that need attention?

@ClearScriptLib
Copy link
Collaborator

Hi @lgBlog,

My idea is that if dynamic calling is not supported (using RuntimeFeature.IsDynamicCodeSupported for judgment), reflection is used to obtain the matching method.

Have you tried disabling the default method binding algorithm via DisableDynamicBinding?

Thanks!

@lgBlog
Copy link
Author

lgBlog commented Dec 18, 2024

Thank you for your prompt reply.
After disabling dynamic binding, the AOT release test was successful.

Other minor issues such as type loss can be solved using the rd.xml file.

The rd.xml example is as follows

<?xml version="1.0" encoding="utf-8" ?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
    <Library>
        <Assembly Name="ClearScript.Core" Dynamic="Required All">
            <Type Name="Microsoft.ClearScript.Util.ScriptableEnumerableHelpers`1[System.Byte]" Dynamic="Required All" />
            <Type Name="Microsoft.ClearScript.Util.MemberMap+Method" Dynamic="Required All"/>
        </Assembly>
    </Library>
</Directives>

Include rd.xml in your .csproj

<ItemGroup>
    <RdXmlFile  Include="rd.xml" />
</ItemGroup>

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

No branches or pull requests

2 participants