Search Unity

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:
    206
    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));
     
    kfconeone likes this.