RyanLamansky / dotnet-webassembly

Create, read, modify, write and execute WebAssembly (WASM) files from .NET-based applications.
Apache License 2.0
789 stars 74 forks source link

Problem With Imports #23

Closed JasonBock closed 4 years ago

JasonBock commented 4 years ago

I have a demo (https://github.com/jasonbock/blazingtheweb - specifically the BlazingTheWeb.UsingWasmInCSharp project), where, if I try to do Compile.FromBinary() on a module that has import functions, it doesn't work, or at least it doesn't work the way I think it should. In the RunCollatzWithCallback() method, if I change this:

Compile.FromBinary<dynamic>

to this:

Compile.FromBinary<Collatz>

It doesn't work, even though there's only one exported function: collatz. But when I used dynamic, everything works as expected. Do I need to specify something on the abstract class if I have import functions as well?

RyanLamansky commented 4 years ago

I downloaded the WASM and Collatz class, pasted it into a test project I already had set up... and it worked. It's weird that dynamic works for you but the class does not--the class appears to be set up perfectly.

I can take a deeper dive this weekend to see if there's something strange going on in your project.

JasonBock commented 4 years ago

Thanks for looking into this. BTW here's the exception information I'm seeing:

Unhandled exception. WebAssembly.ModuleLoadException: At offset 145: Method 'collatz' in type 'CompiledExports' from assembly 'CompiledWebAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
 ---> System.TypeLoadException: Method 'collatz' in type 'CompiledExports' from assembly 'CompiledWebAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
   at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
   at System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
   at WebAssembly.Runtime.Compile.FromBinary(Reader reader, CompilerConfiguration configuration, Type instanceContainer, Type exportContainer)
   at WebAssembly.Runtime.Compile.FromBinary[TExports](Stream input, CompilerConfiguration configuration)
   --- End of inner exception stack trace ---
   at WebAssembly.Runtime.Compile.FromBinary[TExports](Stream input, CompilerConfiguration configuration)
   at WebAssembly.Runtime.Compile.FromBinary[TExports](String path, CompilerConfiguration configuration)
   at WebAssembly.Runtime.Compile.FromBinary[TExports](String path)
   at BlazingTheWeb.UsingWasmInCSharp.Program.RunCollatzWithCallback() in M:\JasonBock\Personal\Code Projects\BlazingTheWeb\BlazingTheWeb.UsingWasmInCSharp\Program.cs:line 28
   at BlazingTheWeb.UsingWasmInCSharp.Program.Main() in M:\JasonBock\Personal\Code Projects\BlazingTheWeb\BlazingTheWeb.UsingWasmInCSharp\Program.cs:line 12

FYI here's the NuGet package version:

<PackageReference Include="WebAssembly" Version="0.8.0-preview" />

The exception happens in both Debug and Release builds, I'm using .NET Core 3.1.1.

RyanLamansky commented 4 years ago

That error says that the collatz abstract method doesn't exactly match what the WASM is exporting. I should detect that and provide a nicer error 🤔

As noted above, I checked your repo and it looks like the abstract class is right, which makes this especially strange as this is one of the most thoroughly tested areas.

JasonBock commented 4 years ago

It's the corner-case issues that are always fun to resolve ;)

RyanLamansky commented 4 years ago

The WAST for collatzWithCallback.wasm doesn't match the WASM--the collatz function in collatzWithCallback.wasm has no return type, which means the abstract collatz method in your class is never implemented, leading to the error you're observing.

It works with dynamic because the way you're calling it doesn't require a return type, so a compatible run-time match is found.

Changing the class to public abstract void collatz(int value); gets things working, but would have to be a different class to avoid conflicting with the other WASM.

Does this address your isssue?

RyanLamansky commented 4 years ago

BTW, I was looking at the wrong WASM file earlier--the problem was quite apparent when I looked at the right one 🙄

JasonBock commented 4 years ago

🤦‍♂

Yep, that fixes it. Thanks for catching that.