Skip to content

Can't declare [JSFunction] and [JSEvent] under non-entry assemblies #173

@elringus

Description

@elringus

In #171 we're moving to direct invocation of the interop bindings (instead of using delegate proxies) for performance reasons.

The interop bindings are emitted at build time and not available at design-time due to .NET's SG limitation. This is usually not an issue, as most of the other interop code is also emitted at build, except for [JSFunction] and [JSEvent] partial methods, which require the implementations to be available at design-time.

As a workaround, the source-generated implementations for the partial methods are conditionally compiled:

public string EmitSource (Compilation compilation)
{
method = compilation.GetSemanticModel(syntax.SyntaxTree).GetDeclaredSymbol(syntax)!;
return $"""
{syntax.Modifiers} {EmitSignature()} =>
#if BOOTSHARP_EMITTED
{EmitBody()};
#else

— and the compiler constant is defined at build time after the bindings are emitted:

<Target Name="BootsharpEmit" BeforeTargets="GenerateAdditionalSources"
Condition="'$(BootsharpSkip)' != 'true' And Exists('$(OutputPath)')">
<PropertyGroup>
<DefineConstants>$(DefineConstants);BOOTSHARP_EMITTED</DefineConstants>
</PropertyGroup>

However, the define seem to only apply to the entry assembly during the build, resulting in exception being thrown when [JSFunction] and [JSEvent] methods defined under none-entry assemblies are invoked.

As a workaround for the issue, use [JSImport] to import the functions via interfaces, which works under any assembly: https://sharp.elringus.com/guide/interop-interfaces.

If anyone have an idea on how to fix this, please share.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedCommunity assistance is most appreciated

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions