Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't implement .jslib functions in WebGL project, crash occurs on build

Discussion in 'WebGL' started by markoc98, Apr 23, 2021.

  1. markoc98

    markoc98

    Joined:
    Mar 24, 2020
    Posts:
    2
    Hello,
    I need to access local storage of browser in order to get some data. So I followed the official Unity guide
    ( https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html )
    I wrote a .jslib file with needed functions:
    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.  
    3.     SetAccessToken: function(){
    4.         window.localStorage.setItem("accessToken", "tokenForAccess");
    5.         window.localStorage.setItem("user", "userData");
    6.     },
    7.  
    8.     GetAccessToken: function(){
    9.         var data;
    10.         data["accessToken"] = window.localStorage.getItem("custom-token");
    11.         data["user"] = window.localStorage.getItem("user");
    12.         return data;
    13.     },
    14.  
    15.     PrintSomething: function(str){
    16.         console.log(str);
    17.     },
    18. });
    Then imported into my Unity project under folder Assets/Plugins.
    This is my C# Script in which I try calling the implemented js functions:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. public class LoginController : MonoBehaviour
    5. {
    6.     [DllImport("__internal")]
    7.     private static extern void SetAccessToken();
    8.  
    9.     [DllImport("__Internal")]
    10.     private static extern void PrintSomething(string str);
    11.  
    12.     [DllImport("__Internal")]
    13.     private static extern string GetAccessToken();
    14.  
    15.     private void Start()
    16.     {
    17.         SetAccessToken();
    18.         string userData = GetAccessToken();
    19.         PrintSomething(userData);
    20.     }
    21. }
    22.  
    When I build into WebGL locally and run in Mozzila's Firefox (I've set up the privacy.file_unique_origin to false) I get the following error:

    An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:
    uncaught exception: abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/kripken/emscripten/wiki/Linking") at jsStackTrace (UnityWebGLBuild.wasm.framework.unityweb:8:15620)
    stackTrace (UnityWebGLBuild.wasm.framework.unityweb:8:15791)
    onAbort@http://localhost:51474/Build/UnityLoader.js:4:11199
    abort (UnityWebGLBuild.wasm.framework.unityweb:8:515994)
    _dlopen (UnityWebGLBuild.wasm.framework.unityweb:8:170718)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[60596]:0x12c55dc (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[62016]:0x13049a6)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[63063]:0x1334da7 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[63062]:0x1334d3d)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[63066]:0x1334dfe (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[25168]:0xba44c8)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[63528]:0x1351dd1
    UnityModule [UnityModule/Module.dynCall_iiiii] (UnityWebGLBuild.wasm.framework.unityweb:8:486880)
    invoke_iiiii (UnityWebGLBuild.wasm.framework.unityweb:8:347250)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[61183]:0x12e23e0 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[60495]:0x12c1d82)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[4415]:0x1c6f54 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[4413]:0x1c6c2c)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[8083]:0x304bd8 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[8080]:0x3039fa)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10567]:0x410de7 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[8396]:0x32a5e8)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10952]:0x43d9e7 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10666]:0x41ac62)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10666]:0x41ac77 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10661]:0x41a780)
    @blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[10654]:0x418708 (blob:http://localhost:51474/23094b0d-8d77-4761-857d-9a784ad41696 line 8 > WebAssembly.instantiate:wasm-function[63546]:0x13520cf)
    UnityModule [UnityModule/Module.dynCall_v] (UnityWebGLBuild.wasm.framework.unityweb:8:496436)
    browserIterationFunc (UnityWebGLBuild.wasm.framework.unityweb:8:174940)
    runIter (UnityWebGLBuild.wasm.framework.unityweb:8:178013)
    Browser_mainLoop_runner (UnityWebGLBuild.wasm.framework.unityweb:8:176475)

    These are the libraries we use for our WebAPI if that is of any matter:

    Code (CSharp):
    1. using System;
    2. using Newtonsoft.Json;
    3. using UnityEngine.Networking;
     
  2. NickoG

    NickoG

    Joined:
    Apr 19, 2015
    Posts:
    8
    It could have something to do with syncing the filesystem.
    I use the solution from this thread . the HandleIO.jslib script
    But now I sometimes get the "Two fs sync happening at once" message. Not sure if it's that plugin.
     
  3. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    277
    1. [DllImport("__internal")] -> [DllImport("__Internal")]
    2. GetAccessToken() returns the object type, but the jslib function can basically only return numbers (including pointers) and strings.
      So if you convert it to a JSON string and return it, the function caller will convert it from JSON to an object.
      If you want to return a string, you need to convert it to a pointer and then return it.
    3. PrintSomething() it is passed as an argument instead of a string. Therefore, you need to convert the pointer to a string.
    Fixed code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. public class LoginController : MonoBehaviour
    5. {
    6.     [DllImport("__Internal")]
    7.     private static extern void SetAccessToken();
    8.  
    9.     [DllImport("__Internal")]
    10.     private static extern void PrintSomething(string str);
    11.  
    12.     [DllImport("__Internal")]
    13.     private static extern string GetAccessToken();
    14.  
    15.     private class UserData
    16.     {
    17.         public string accessToken;
    18.         public string user;
    19.     }
    20.  
    21.     private void Start()
    22.     {
    23.         SetAccessToken();
    24.         string userDataJson = GetAccessToken();
    25.         PrintSomething(userDataJson);
    26.         var userData = JsonUtility.FromJson<UserData>(userDataJson);
    27.         Debug.Log($"accessToken: {userData.accessToken}");
    28.         Debug.Log($"user: {userData.user}");
    29.     }
    30. }
    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.     SetAccessToken: function(){
    3.         window.localStorage.setItem("accessToken", "tokenForAccess");
    4.         window.localStorage.setItem("user", "userData");
    5.     },
    6.     GetAccessToken: function(){
    7.         var data = {};
    8.         data["accessToken"] = window.localStorage.getItem("accessToken");
    9.         data["user"] = window.localStorage.getItem("user");
    10.         var json = JSON.stringify(data);
    11.         var size = lengthBytesUTF8(json) + 1;
    12.         var ptr = _malloc(size);
    13.         stringToUTF8(json, ptr, size);
    14.         return ptr;
    15.     },
    16.     PrintSomething: function(strPtr){
    17.         var str = UTF8ToString(strPtr);
    18.         console.log(str);
    19.     }
    20. });
     
    jukka_j and markoc98 like this.
  4. markoc98

    markoc98

    Joined:
    Mar 24, 2020
    Posts:
    2
    Thank you! Works well now :)
     
  5. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Does anyone know why this code fails to build. No doubt doing something dumb but I can't see it

    Code (CSharp):
    1.    
    2.      CopyToClipboard : function(strPtr, message, failed)
    3.      {
    4.      
    5.           var type = "text/plain";
    6.           var text = UTF8ToString(strPtr);
    7.           var blob = new Blob([text], { type });
    8.           var data = [new ClipboardItem({ [type]: blob })];
    9.        
    10.           navigator.clipboard.write(data).then(
    11.              function ()
    12.              {
    13.                    window.alert(UTF8ToString(message));
    14.              },
    15.              function ()
    16.              {
    17.                    window.alert(UTF8ToString(failed));
    18.              }
    19.          );
    20.      }
    21.  
    Code (CSharp):
    1.  
    2. [DllImport("__Internal")]
    3.     private static extern void CopyToClipboard(string url, string message, string failed);
    4.  
    For context I was using navigator.clipboard.writeText but that doesn't work on the mac
     
  6. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    277
    (Not confirmed)
    Maybe
    Code (JavaScript):
    1. {[type]: blob}
    I think that part is bad.

    Since the version of emscripten before Unity2022 is old, ES6 + syntax cannot be used.
    Please rewrite it as follows and try it.

    Code (JavaScript):
    1. var obj = {};
    2. obj [type] = blob;
    3. var data = [new ClipboardItem (obj)];
     
  7. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Sadly that still fails
     
    Last edited: Mar 17, 2022
  8. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595