tana / Wasm3DotNet

A .NET binding for wasm3
MIT License
19 stars 2 forks source link

DLL for x64 does not work on Unity #1

Closed ku6ryo closed 3 years ago

ku6ryo commented 3 years ago

I'm trying to use this lib on Unity on windows x64 environment.

Opened Wasm3DotNet/Wasm3DotNet.sln and set Wasm3DotNet for x64 env as a build target (not Wasm3DotNetDemo). Then, built a DLL and confirmed that a DLL is generated in bin/Release folder.

I copied the DLL and placed in Unity. Wasm3DotNet library is recognized and Unity build is success fully done in Unity Editor. However, in runtime, I see the following error. Seems like wasm3 lib is not imported.

DllNotFoundException: wasm3
Wasm3DotNet.Wrapper.Environment..ctor () (at <2f06ae5233474975a39a8d6c14c0b3bd>:0)
ConnectorWasm3.Load (System.Byte[] wasm) (at Assets/wasm/ConnectorWasm3.cs:36)
Sandbox+<>c__DisplayClass21_0.<Start>b__0 (UnityEngine.AsyncOperation operation) (at Assets/wasm/Sandbox.cs:135)
UnityEngine.AsyncOperation.InvokeCompletionEvent () (at <c7864a0eaeb24b2a999fb177623d54b4>:0)

My code is like this.

public void Load(byte[] wasm)
    {
        using (var environment = new Wasm3DotNet.Wrapper.Environment())
        {
            runtime = new Runtime(environment);
            runtime.PrintRuntimeInfo();
            var module = runtime.ParseModule(wasm);
            runtime.LoadModule(module);
        }
    }

Next, I tried to build a DLL of wasm3 to get that wasm3 DLL included in my Unity project. On the same .sln on Visual Studio, built wasm3_dll csxproject for x64. I encountered the following error messages.

Unity\Assets\Wasm3DotNet\wasm3_dll\wasm3\source\m3_info.c(66,5): error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Unity\Assets\Wasm3DotNet\wasm3_dll\wasm3\source\m3_math_utils.h(128,33): error C4146: unary minus operator applied to unsigned type, result still unsigned

And next, I changed the original C code in wasm3 submodule and got rid of them. However, I see a new message.

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    wasm3_dll   C:\Users\ryokuro\Development\Wasm3DotNet\wasm3_dll\MSVCRTD.lib(exe_main.obj)    1   

I'm not able to fix the error on my end. Could you help me to resolve it?

ku6ryo commented 3 years ago

Note: I successfully run Wasm3DotNetDemo x86 and x64 and see results calculated in wasm module.

tana commented 3 years ago

Thank you for reporting. I fixed the compiler and linker settings for x64 architecture (in commit 337dd80be9c7a9f1b9def10819dbef30f6b54f5c). Does it work on your environment?

ku6ryo commented 3 years ago

thanks, I've confirmed that x64 build is done successfully. :)

But I faced crashes when I brought that x64 build to Unity and run Unity project. I will report that in a different issue.

Anyways, thanks a lot !!

tana commented 3 years ago

Sorry, I forgot testing on Unity despite this issue has "on Unity" in the title... However, closing this because moved to #2.