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. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    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
    Nerzal likes this.
  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));
     
    Iq110, nfynt-zap, Aurigan and 9 others like this.
  3. Linkupdated

    Linkupdated

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

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    326
    Please Update the documentation!
    It is really frustrating, when even the example code does not work :(
     
  5. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    290
    After upgrading to Unity 2021.2 it appears that WebGL builds now don't allow/include pointer_stringify(), replacing with
    UTF8ToString() works.
     
    IndieMarkus, jush0824, mrSaig and 3 others like this.
  6. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    Awesome Thank You! Saved me some headache ;)
     
  7. chandraprasadajay26

    chandraprasadajay26

    Joined:
    Jul 11, 2021
    Posts:
    1
    hey how do I do this Where should I change the code
    Need help
     
  8. JPoenisch

    JPoenisch

    Joined:
    Mar 2, 2018
    Posts:
    4

    Just exactly as JJJohan said


    so in short instead of

    Code (CSharp):
    1. window.alert(pointer_stringify(str));
    use

    Code (CSharp):
    1. window.alert(UTF8ToString(str));