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\$

1 Answer 1

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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.