From a6e727470437eceb8d27cadd6737c7e50577d410 Mon Sep 17 00:00:00 2001 From: Piotr Wiercinski Date: Fri, 23 Feb 2024 17:14:08 +0100 Subject: [PATCH] wasm: make qtloader.js use FS.createPreloadedFile when preloading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently qtloader.js fetches and copies the files manually. By doing so we are missing some preproccessing by Emscripten preload plugins. Use Emscripten API to preload files, so preload plugin for .so can download, compile and resolve dependencies of imported shared libraries. This makes looking for dependencies in preload_qml_import.py no longer needed. Remove redundant code. Fixes: QTBUG-121817 Change-Id: Idd35f25d5f54123910f813a636407eea23e157cb Reviewed-by: Piotr WierciƄski --- src/plugins/platforms/wasm/qtloader.js | 42 +++++++++++------------- util/wasm/preload/preload_qml_imports.py | 12 ------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/plugins/platforms/wasm/qtloader.js b/src/plugins/platforms/wasm/qtloader.js index 89944d6dd91..bbc0ac68ab0 100644 --- a/src/plugins/platforms/wasm/qtloader.js +++ b/src/plugins/platforms/wasm/qtloader.js @@ -120,14 +120,15 @@ async function qtLoad(config) } } } - + const fetchJsonHelper = async path => (await fetch(path)).json(); + const filesToPreload = (await Promise.all(config.qt.preload.map(fetchJsonHelper))).flat(); const qtPreRun = (instance) => { // Copy qt.environment to instance.ENV throwIfEnvUsedButNotExported(instance, config); for (const [name, value] of Object.entries(config.qt.environment ?? {})) instance.ENV[name] = value; - // Copy self.preloadData to MEMFS + // Preload files from qt.preload const makeDirs = (FS, filePath) => { const parts = filePath.split("/"); let path = "/"; @@ -146,14 +147,25 @@ async function qtLoad(config) } } + const extractFilenameAndDir = (path) => { + const parts = path.split('/'); + const filename = parts.pop(); + const dir = parts.join('/'); + return { + filename: filename, + dir: dir + }; + } + const preloadFile = (file) => { + makeDirs(instance.FS, file.destination); + const source = file.source.replace('$QTDIR', config.qt.qtdir); + const filenameAndDir = extractFilenameAndDir(file.destination); + instance.FS.createPreloadedFile(filenameAndDir.dir, filenameAndDir.filename, source, true, true); + } const isFsExported = typeof instance.FS === 'object'; if (!isFsExported) throw new Error('FS must be exported if preload is used'); - - for ({destination, data} of self.preloadData) { - makeDirs(instance.FS, destination); - instance.FS.writeFile(destination, new Uint8Array(data)); - } + filesToPreload.forEach(preloadFile); } if (!config.preRun) @@ -197,22 +209,6 @@ async function qtLoad(config) } }; - const fetchPreloadFiles = async () => { - const fetchJson = async path => (await fetch(path)).json(); - const fetchArrayBuffer = async path => (await fetch(path)).arrayBuffer(); - const loadFiles = async (paths) => { - const source = paths['source'].replace('$QTDIR', config.qt.qtdir); - return { - destination: paths['destination'], - data: await fetchArrayBuffer(source) - }; - } - const fileList = (await Promise.all(config.qt.preload.map(fetchJson))).flat(); - self.preloadData = (await Promise.all(fileList.map(loadFiles))).flat(); - } - - await fetchPreloadFiles(); - // Call app/emscripten module entry function. It may either come from the emscripten // runtime script or be customized as needed. let instance; diff --git a/util/wasm/preload/preload_qml_imports.py b/util/wasm/preload/preload_qml_imports.py index 42fea6e0877..9af4fa2a282 100755 --- a/util/wasm/preload/preload_qml_imports.py +++ b/util/wasm/preload/preload_qml_imports.py @@ -6,9 +6,6 @@ import os import sys import subprocess import json -import re - -from wasm_binary_tools import WasmBinary # Paths to shared libraries and qml imports on the Qt installation on the web server. # "$QTDIR" is replaced by qtloader.js at load time (defaults to "qt"), and makes @@ -30,11 +27,6 @@ def preload_file(source, destination): preload_files.append({"source": source, "destination": destination}) -def find_dependencies(filepath): - binary = WasmBinary(filepath) - return binary.get_dependencies() - - def extract_preload_files_from_imports(imports): libraries = [] for qml_import in imports: @@ -55,10 +47,6 @@ def extract_preload_files_from_imports(imports): so_plugin_qt_install_path = os.path.join( qt_wasm_path, "qml", relative_path, plugin_filename ) - deps = find_dependencies(so_plugin_qt_install_path) - if plugin_filename in deps: # sometimes plugin file itself is found as its dependency - deps.remove(plugin_filename) - libraries.extend(deps) # qmldir file qmldir_source_path = os.path.join(qt_qml_path, relative_path, "qmldir")