0
\$\begingroup\$

I can't understand how to work with .jslib files. I don't understand the syntax of this type of file. Also I couldn't find any easy explanation of it

The problem started when I tried to make two functions global for all .jslib files:

/**
 * @param {string} str
 * @returns {string}
 */
function StrJSToStrCS (str) {
    const bufferSize = lengthBytesUTF8(str) + 1;
    const buffer = _malloc(bufferSize);
    stringToUTF8(str, buffer, bufferSize);
    return buffer;
}

/**
 * @param {string} str
 * @returns {string}
 */
function StrCSToStrJS (str) {
    return UTF8ToString(str);
}

What I already tried:

  • To define these two functions in the same file

    function StrJSToStrCS (str) {
        const bufferSize = lengthBytesUTF8(str) + 1;
        const buffer = _malloc(bufferSize);
        stringToUTF8(str, buffer, bufferSize);
        return buffer;
    }
    
    function StrCSToStrJS (str) {
        return UTF8ToString(str);
    }
    
    const lib = {
        Hello: function () {
            return StrJSToStrCS('HELLO WORLD!');
        },
    
        ConsoleLog: function (str) {
            console.log(StrCSToStrJS(str));
        }
    };
    
    mergeInto(LibraryManager.library, lib);
    
  • To define these two functions in script tag in .html after reading this. If it's important the script tag I inserted at the bottom of body tag

    <script>
        function StrJSToStrCS (str) {
            const bufferSize = lengthBytesUTF8(str) + 1;
            const buffer = _malloc(bufferSize);
            stringToUTF8(str, buffer, bufferSize);
            return buffer;
        }
    
        function StrCSToStrJS (str) {
            return UTF8ToString(str);
        }
    </script>
    
    const lib = {
        Hello: function () {
            return StrJSToStrCS('HELLO WORLD!');
        },
    
        ConsoleLog: function (str) {
            console.log(StrCSToStrJS(str));
        }
    };
    
    mergeInto(LibraryManager.library, lib);
    
  • To define these functions in .jspre file after reading this

    the .jspre files are added to the build using the --pre-js option, while the .jslib are added using the --js-library option

    .jspre file:

    function StrJSToStrCS (str) {
        const bufferSize = lengthBytesUTF8(str) + 1;
        const buffer = _malloc(bufferSize);
        stringToUTF8(str, buffer, bufferSize);
        return buffer;
    }
    
    function StrCSToStrJS (str) {
        return UTF8ToString(str);
    }
    

    .jslib file:

    const lib = {
        Hello: function () {
            return StrJSToStrCS('HELLO WORLD!');
        },
    
        ConsoleLog: function (str) {
            console.log(StrCSToStrJS(str));
        }
    };
    
    mergeInto(LibraryManager.library, lib);
    
  • To simply define these functions as methods in the same object:

    const lib = {
        StrJSToStrCS: function(str) {
            const bufferSize = lengthBytesUTF8(str) + 1;
            const buffer = _malloc(bufferSize);
            stringToUTF8(str, buffer, bufferSize);
            return buffer;
        },
    
        StrCSToStrJS: function(str) {
            return UTF8ToString(str);
        },
    
        Hello: function() {
            return this.StrJSToStrCS('HELLO WORLD!');
        },
    
        ConsoleLog: function(str) {
            console.log(this.StrCSToStrJS(str));
        }
    }
    
    mergeInto(LibraryManager.library, lib);
    

But whatever I do, he writes every time in console that StrJSToStrCS/StrCSToStrJS is not defined

I don't understand at all why the last method doesn't work. Isn't lib just a simple object?

Is it possible to make these two functions global for all .jslib files?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Finally I found the answer!

In .jspre file I had to write:

Module.StrJSToStrCS = function(str) {
    const bufferSize = lengthBytesUTF8(str) + 1;
    const buffer = _malloc(bufferSize);
    stringToUTF8(str, buffer, bufferSize);
    return buffer;
}

Module.StrCSToStrJS = function(str) {
    return UTF8ToString(str);
}

and then in .jslib file call these functions like this:

const lib = {
    Hello: function() {
        return gameInstance.Module.StrJSToStrCS('HELLO WORLD!');
    },

    ConsoleLog: function(str) {
        console.log(gameInstance.Module.StrCSToStrJS(str));
    }
}

mergeInto(LibraryManager.library, lib);
\$\endgroup\$
0
\$\begingroup\$

I found the official documentation very useful when I was working with webgl and unity and also has some examples. Interaction with browser scripting

Its a library file and has to be placed in Plugins folder.

In case this doesn't help, please post the Unity side of things also to get more understanding.

\$\endgroup\$
1
  • \$\begingroup\$ I already read the official documentation and there is only simple examples. I don't have a problem with calling JS functions from CS and vice versa. Please read my question attentively. In short I need global JS function for several JSLIB files \$\endgroup\$
    – EzioMercer
    Commented Dec 28, 2022 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .