[Web] Use the module config to preload GDExtension libraries.

Instead of calling loadDynamicLibraries ourselves, we add the
GDExtension libraries to preload to the "dynamicLibraries" module config
property.

This seems to fix some threading issue with some browsers during the
init phase.
This commit is contained in:
Fabio Alessandrelli 2024-06-14 00:24:30 +02:00
parent c95bd2d264
commit 27d67b5ae6
2 changed files with 13 additions and 16 deletions

View File

@ -271,12 +271,13 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
*/ */
Config.prototype.getModuleConfig = function (loadPath, response) { Config.prototype.getModuleConfig = function (loadPath, response) {
let r = response; let r = response;
const gdext = this.gdextensionLibs;
return { return {
'print': this.onPrint, 'print': this.onPrint,
'printErr': this.onPrintError, 'printErr': this.onPrintError,
'thisProgram': this.executable, 'thisProgram': this.executable,
'noExitRuntime': false, 'noExitRuntime': false,
'dynamicLibraries': [`${loadPath}.side.wasm`], 'dynamicLibraries': [`${loadPath}.side.wasm`].concat(this.gdextensionLibs),
'instantiateWasm': function (imports, onSuccess) { 'instantiateWasm': function (imports, onSuccess) {
function done(result) { function done(result) {
onSuccess(result['instance'], result['module']); onSuccess(result['instance'], result['module']);
@ -300,6 +301,8 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
return `${loadPath}.audio.worklet.js`; return `${loadPath}.audio.worklet.js`;
} else if (path.endsWith('.js')) { } else if (path.endsWith('.js')) {
return `${loadPath}.js`; return `${loadPath}.js`;
} else if (path in gdext) {
return path;
} else if (path.endsWith('.side.wasm')) { } else if (path.endsWith('.side.wasm')) {
return `${loadPath}.side.wasm`; return `${loadPath}.side.wasm`;
} else if (path.endsWith('.wasm')) { } else if (path.endsWith('.wasm')) {

View File

@ -163,25 +163,19 @@ const Engine = (function () {
me.rtenv['initConfig'](config); me.rtenv['initConfig'](config);
// Preload GDExtension libraries. // Preload GDExtension libraries.
const libs = [];
if (me.config.gdextensionLibs.length > 0 && !me.rtenv['loadDynamicLibrary']) { if (me.config.gdextensionLibs.length > 0 && !me.rtenv['loadDynamicLibrary']) {
return Promise.reject(new Error('GDExtension libraries are not supported by this engine version. ' return Promise.reject(new Error('GDExtension libraries are not supported by this engine version. '
+ 'Enable "Extensions Support" for your export preset and/or build your custom template with "dlink_enabled=yes".')); + 'Enable "Extensions Support" for your export preset and/or build your custom template with "dlink_enabled=yes".'));
} }
me.config.gdextensionLibs.forEach(function (lib) { return new Promise(function (resolve, reject) {
libs.push(me.rtenv['loadDynamicLibrary'](lib, { 'loadAsync': true })); for (const file of preloader.preloadedFiles) {
}); me.rtenv['copyToFS'](file.path, file.buffer);
return Promise.all(libs).then(function () { }
return new Promise(function (resolve, reject) { preloader.preloadedFiles.length = 0; // Clear memory
preloader.preloadedFiles.forEach(function (file) { me.rtenv['callMain'](me.config.args);
me.rtenv['copyToFS'](file.path, file.buffer); initPromise = null;
}); me.installServiceWorker();
preloader.preloadedFiles.length = 0; // Clear memory resolve();
me.rtenv['callMain'](me.config.args);
initPromise = null;
me.installServiceWorker();
resolve();
});
}); });
}); });
}, },