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
scripttag in.htmlafter reading this. If it's important the script tag I inserted at the bottom ofbodytag<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
.jsprefile after reading thisthe .jspre files are added to the build using the --pre-js option, while the .jslib are added using the --js-library option
.jsprefile:function StrJSToStrCS (str) { const bufferSize = lengthBytesUTF8(str) + 1; const buffer = _malloc(bufferSize); stringToUTF8(str, buffer, bufferSize); return buffer; } function StrCSToStrJS (str) { return UTF8ToString(str); }.jslibfile: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?