📖

async で複数 Promise で wasm binary を初期化するとなんかエラーでるメモ

に公開

WASM(C++ コードを Emscripten で WASM 化し, async で遅延初期化(初期化などには時間かかる)するとなんか一応動きはするようだがエラーが出る.

    // Initialize the native WASM module
    // This is async but the load() method handles it internally with promises
    // Initialize the native WASM module
    // This is async but the load() method handles it internally with promises
    async init() {
        if (!this.native_) {
            console.log('Initializing native module...');
            this.native_ = await initTinyUSDZNative({});
            if (!this.native_) {
                throw new Error('TinyUSDZLoader: Failed to initialize native module.');
            }
            console.log('Native module initialized');
        }
        return this;
    }
    loadAsync(url, onLoad, onProgress, onError) {
        //console.log('url', url);

        const scope = this;

        // Create a promise chain to handle initialization and loading
        const initPromise = this.native_ ? Promise.resolve() : this.init();

const scenes = Promise.all([ loader.loadAsync(a), loader.loadAsync(b), ...])

みたいな.

still waiting on run dependencies:
	setInterval		
addRunDependency	@	tinyusdz.js:472
createWasm

のようなエラーが出る.

this.native_ がアトミックにアップデートされないからかしらん.

解決策

とりあえず init() を先に読んでおく

JS 自体には atomic 機能がない(SharedArrayBuffer つかう). webworker で適切にメッセージングで同期(mutex)とる?

Claude 4 くんとかに聞いてみて.

Discussion