Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Pointer_stringify() is not defined.(Pass string to js)

Discussion in 'Project Tiny' started by kfconeone, Jun 27, 2019.

  1. kfconeone

    kfconeone

    Joined:
    Oct 3, 2013
    Posts:
    7
    As title, I have tried to pass a string to js function, and caught this error from browser.

    First I thought maybe it's due to the emsdk in my environment, but apparently Unity would cache it's own,
    so what should I do?

    below is my code

    C#
    Code (CSharp):
    1. [DllImport("__Internal")]
    2. private static extern string HelloString(string str);
    3.  
    4. protected override void OnUpdate()
    5. {
    6.       var s = HelloString("Hello js!");
    7.  
    8.       //snip
    9. }
    10.  
    JS
    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.     HelloString: function (str) {
    3.         window.alert(pointer_stringify(str));
    4.      
    5.         var returnStr = "Hello C#!";
    6.         var bufferSize = lengthBytesUTF8(returnStr) + 1;
    7.         var buffer = _malloc(bufferSize);
    8.         stringToUTF8(returnStr, buffer, bufferSize);
    9.         return buffer;
    10.     }
    11. });
     
    Last edited: Jun 27, 2019
  2. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    I believe pointer_stringify has been removed (deprecated for a while) in the version of Emscripten that Tiny uses (sadly the Unity WebGL platform uses an older variant) - you shoulld be able to simply replace it with UTF8ToString.

    UTF8ToString has an overload so you don't have to manually calculate the string size like what's happening in the snippet (which is stringToUTF8 not UTF8ToString).

    i.e. just try
    window.alert(UTF8ToString(str));
     
  3. Linkupdated

    Linkupdated

    Joined:
    Sep 24, 2014
    Posts:
    5
    Unity should really update that description on the site since the official example is outdated...
    Thanks a lot !
     
unityunity