Calling a jslib function from jspre code

I am trying to call a function defined in the .jslib from a .jspre file
Example
My jslib file looks like

var NativeLib = {

OnPeerAdded : function(peer)
    {
        //Send peer info to unity
    }

};
mergeInto(LibraryManager.library, NativeLib);

And my jspre code looks like|

function OnPeerAdded(peerInfo)
{
    //Need to call .jslib OnPeerAdded here
}

How can I achieve that?

I recommend you read our docs on this, we provide plenty of examples and explanations, and if the docs don’t help, please reply to this thread again with what you tried that didn’t work!

Thanks for the quick reply @unityruba
I have tested this link and its working fine in case I wanted to invoke any message from js to unity : Unity - Manual: Call Unity C# script functions from JavaScript
But there is no example which shows how to call a function of .jslib from .jspre file

Also I tried something like this

function callMergeStrings() {
    const str1 = 'Hello';
    const str2 = 'World';

    // Allocate memory for the strings
    const str1Ptr = Module._malloc(lengthBytesUTF8(str1) + 1);
    const str2Ptr = Module._malloc(lengthBytesUTF8(str2) + 1);

    // Copy the strings to the allocated memory
    stringToUTF8(str1, str1Ptr, lengthBytesUTF8(str1) + 1);
    stringToUTF8(str2, str2Ptr, lengthBytesUTF8(str2) + 1);

    // Call the mergeStrings function from MyLibrary.jslib
    const mergedStringPtr = Module.ccall(
        'SendMessageToUnity',      // Name of the function in the .jslib file
        'number',            // Return type (pointer to a string)
        ['number', 'number'], // Parameter types (pointers to char arrays)
        [str1Ptr, str2Ptr]    // Arguments passed to the function
    );

    // Read the merged string from the pointer
    const mergedString = Pointer_stringify(mergedStringPtr);
    console.log('Received merged string:', mergedString);

    // Free the allocated memory
    Module._free(str1Ptr);
    Module._free(str2Ptr);
    Module._free(mergedStringPtr);
}

this is a function in a .jspre file which merge 2 strings and call a method “SendMessageToUnity” which exist in .jslib
But for some reason its not working.

I will gather some more info, but meanwhile can you provide me some hint or a solution for fixing the above problem.

@AdhikS Check this post reply .

@Marks4 Thanks for the reference
Can you tell me how can i call this function of jspre?

Module['CoolJsPlugin'] = Module['CoolJsPlugin'] || {};
Module['CoolJsPlugin'].RunCoolFunction = function () {
    _CoolFunction();
}

Suppose I have a function which wants to call RunCoolFunction.
Should I simply call like RunCoolFunction()?

@Marks4
I have tried the solution that you suggested
This is my jspre file

Module['CoolJsPlugin'] = Module['CoolJsPlugin'] || {};
Module['CoolJsPlugin'].RunCoolFunction = function () {
    _CoolFunction();
}

window.CoolJsPlugin = Module['CoolJsPlugin'];

function RunTest()
{
    if (CoolJsPlugin && CoolJsPlugin.RunCoolFunction)
    {
        console.log("Calling Cool Function");
        CoolJsPlugin.RunCoolFunction();
    } else
    {
        console.error("CoolJsPlugin or RunCoolFunction is not available.");
    }
}

I am calling RunTest from the .jslib but its giving me error at

Module['CoolJsPlugin'].RunCoolFunction = function () {
    _CoolFunction();
}

Says _CoolFunction is not defined

Am I doing something wrong here?

Sorry for misunderstanding your original question! I’ll see if we can add even more examples to that page that cover this case.

Generally, the names merged into the library generates a function name prefixed with a _ (see this Emscripten note)
If they’re defined starting with a $ will have that character stripped and no underscore added.

With that being said, how did you define CoolFunction? can you share that code? did you do it exactly as the other thread?

var CoolJsPlugin= {
  $CoolContainer: {
      data: "data",
  },
  CoolFunction: function() {
    console.log("Cool");
  },
};
autoAddDeps(CoolJsPlugin, '$CoolContainer');
mergeInto(LibraryManager.library, CoolJsPlugin);

@unityruba yeah sure
You can can this link

Its not working for me

ah, you’re just missing the $ then:

$CoolFunction: function() {
    console.log("Cool");
  },

then you should see it generated as CoolFunction instead of _CoolFunction :slight_smile:

@unityruba
thanks for the suggestion
let me try
will let you know if this works

1 Like