Quantcast
Viewing latest article 27
Browse Latest Browse All 68

How does .JSLIB file work?

I can't understand how to work with `.jslib` files. I don't understand the syntax of t$$anonymous$$s 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 [t$$anonymous$$s](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#javascript-limits-in-library-files). If it's important the script tag I inserted at the bottom of `body` tag HTML file JavaScript file 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 [t$$anonymous$$s](https://medium.com/@depanther/unity-webgl-secrets-b6ddf214f1fd) > the .jspre files are added to the build using the --pre-js option, w$$anonymous$$le 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 t$$anonymous$$s.StrJSToStrCS('HELLO WORLD!'); }, ConsoleLog: function(str) { console.log(t$$anonymous$$s.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?

Viewing latest article 27
Browse Latest Browse All 68

Trending Articles