Search code examples
c#unity-game-engineunity-webglasm.js

Different function signature is called when invoking a event from direct c# and jslib callback c#


I'm working on a Unity project, and dealing with WebGL plugin, which is called "jslib" by Unity.

While the unity recommend using UnitySendMessage when jslib interactive with c#, I prefer using a delegate as callback. In a small test code it worked. But when It runs to a trigger event code, strange error happens.

c#:

public class Test : MonoBehaviour {

    public void Start()
    {
        UserLoginDispatcher.GetInstance ().onLoginOver += onLoginOver;
    }

    public void OnDestroy()
    {
        UserLoginDispatcher.GetInstance ().onLoginOver -= onLoginOver;
    }

    //[MonoPInvokeCallback(typeof(System.Action<string, string, SDKError>))]
    public static void onLoginOver(string context, string pluginId, SDKError err)
    {
        string errmsg = err == null ? "" : err.errmsg;
        LogFormat ("Login Over with context " + context + ", pluginId " + pluginId + ", errmsg:" + errmsg);
    }

    static void LogFormat(string format, params object[] param)
    {
         string log = string.Format (format, param);
        Debug.Log (log);
    }
}

public class UserLoginDispatcher {
    private static UserLoginDispatcher s_Instance = new UserLoginDispatcher ();
    public static UserLoginDispatcher GetInstance()
    {
        return s_Instance;
    }

    public delegate void UserLoginCallback(string context, string pluginId, SDKError error);

    public event UserLoginCallback onLoginOver;

    public void ProcessLoginOver(string context, string pluginId, SDKError error)
    {
        onLoginOver (context, pluginId, error);
    }
}

public class WebGLPluginManager
{
    [DllImport("__Internal")]
    private static extern int Plugin_addLoginOverCB (LoginCBDelegate nativeCb);
    private delegate void LoginCBDelegate(string context, string pluginId, int errno, int channelerrno, string errmsg);

    [MonoPInvokeCallback(typeof(LoginCBDelegate))]
    private static void OnLoginOver(string context, string pluginId, int errno, int channelerrno, string errmsg)
    {
        SDK.GetInstance ().loginDispatcher.ProcessLoginOver (context, pluginId, new SDKError (errno, channelerrno, errmsg));
    }

    public void Init()
    {
        Plugin_addLoginOverCB(OnLoginOver);
    }
}

In jslib:

...//login succeed:
Runtime.dynCall("viiiii", .....);

While ProcessLoginOver in UserLoginDispatcher is called, the onLoginOver in test throw a error:

Invalid function pointer called with signature 'viiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)

But if I try to call this event directly in c# code, it can be successfully triggered.

The stack of error call:

Uncaught abort(68) at Error
at jsStackTrace (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23794:12)
at stackTrace (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23808:11)
at abort (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:464638:43)
at nullFunc_viiii (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:50912:2)
at Array.b68 (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:455121:2)
at ftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:6602:49)
at mftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:732323:2)
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533179:4)
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533170:14)
at _UserLoginDispatcher_ProcessLoginOver_m2254139378 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:562185:2)
at _WebGLPluginManager_OnLoginOver_m1528063286 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:548824:2)
at Array.asm._WebGLPluginManager_OnLoginOver_m1528063286 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:921460:59)
....

the stack of directly c# call

Login Over with context a test session 12:13:28, pluginId DefaultUserLogin, errmsg:
at __Z13GetStacktracei [GetStacktrace(int)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2275897:2)
at __Z17DebugStringToFileRK21DebugStringToFileData [DebugStringToFile(DebugStringToFileData const&)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:1247009:5)
at __Z17DebugStringToFilePKciS0_i15LogMessageFlagsiiPFvRK11CppLogEntryE [DebugStringToFile(char const*, int, char const*, int, LogMessageFlags, int, int, void (*)(CppLogEntry const&))] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2562904:2)
at __Z35DebugLogHandler_CUSTOM_Internal_Log7LogTypeP12Il2CppStringP12Il2CppObject [DebugLogHandler_CUSTOM_Internal_Log(LogType, Il2CppString*, Il2CppObject*)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2126846:2)
at Array.UnityLoader.d9fd6255ccad354c9cc5ad72abf13dcc.asm.__Z35DebugLogHandler_CUSTOM_Internal_Log7LogTypeP12Il2CppStringP12Il2CppObject [DebugLogHandler_CUSTOM_Internal_Log(LogType, Il2CppString*, Il2CppObject*)] (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:145057:93)
at ftCall_viii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:7197:48)
at mftCall_viii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:747533:2)
at _DebugLogHandler_Internal_Log_m1116757358 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:719622:2)
at Array._DebugLogHandler_LogFormat_m2613962716 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:620933:2)
at mftCall_viiiiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:708880:40)
at __ZN23InterfaceActionInvoker4IiP18Object_t3753624189P8String_tP24ObjectU5BU5D_t2405707486E6InvokeEjP11Il2CppClassP12Il2CppObjectiS1_S3_S5_ [InterfaceActionInvoker4<int, Object_t3753624189*, String_t*, ObjectU5BU5D_t2405707486*>::Invoke(unsigned int, Il2CppClass*, Il2CppObject*, int, Object_t3753624189*, String_t*, ObjectU5BU5D_t2405707486*)] (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:394923:2)
at Array._Logger_Log_m314997607 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:564686:3)
at mftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:732320:39)
at __ZN23InterfaceActionInvoker2IiP12Il2CppObjectE6InvokeEjP11Il2CppClassS1_iS1_ [InterfaceActionInvoker2<int, Il2CppObject*>::Invoke(unsigned int, Il2CppClass*, Il2CppObject*, int, Il2CppObject*)] (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:412747:2)
at _Debug_Log_m2324883804 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:619913:2)
at _Test_LogFormat_m983122927 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:579589:2)
at Array._Test_onLoginOver_m2512123873 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:383768:2)
at mftCall_viiiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:720078:40)
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533182:4)
at _UserLoginDispatcher_ProcessLoginOver_m2254139378 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:562185:2)
....

I noticed that in direct c# call, the dynCall is using mftCall_viiiii, while in jslib call, the dynCall is using mftCall_viiii, may be that's why the function call failed. That's odd, why would this happen?


Solution

  • Fixed it by myself!

    In fact that is my little mistake: I just forget to initialize loginDispatcher, so this ptr is 0 when enter loginDispatcher, then the behaviour is very confusing.

    The code did not throw any exception when "this" is a 0, because it is translated from c code. It continue to run, till the event Invoke function , at last the function table don't have this function and complained, so I see the very confusing error report!

    ps: c# delegate in asm.js act as follow: If this ptr is 0, the delegate is treated as a static function ,if this ptr is not 0, the delegate is treated as a member function. That's why when this ptr is 0, mftCall_viiii is called instead of mftCall_viiiii