Description
I have an asynchronous JS library function:
C:
void navigator_gpu_request_adapter_async(WGpuRequestAdapterCallback adapterCallback);
JS:
navigator_gpu_request_adapter_async: function(adapterCallback) {
navigator['gpu']['requestAdapter']().then(adapter => {
{{{ makeDynCall('vi', 'adapterCallback') }}}(wgpuStore(adapter));
});
},
To make it possible to synchronously initialize a WebGPU adapter, I want to use ASYNCIFY, so I create a new variant of this function, navigator_gpu_request_adapter_sync
that reads
C:
WGpuAdapter navigator_gpu_request_adapter_sync(void);
JS:
navigator_gpu_request_adapter_sync: function() {
return Asyncify.handleAsync(() => {
return navigator['gpu']['requestAdapter']().then(adapter => {
return wgpuStore(adapter);
})
});
},
and build with -sASYNCIFY=1
linker flag.
However, at runtime I get the error:
Uncaught Error: import navigator_gpu_request_adapter_sync was not in ASYNCIFY_IMPORTS, but changed the state
Reading the documentation, the currently intended way to fix this would be to add the linker flag
-sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync']
However I would not like to do that, because of the two problems:
- it leaks abstractions to the user. Any user of my lib_webgpu.js library will need to know/remember which functions need to be annotated with
-sASYNCIFY_IMPORTS
when they build their program. I.e. one more thing for them to remember to do. - if the users have their own asyncified functions, concatenating/merging lib_webpu.js asyncify imports and their own asyncify imports is not currently possible, i.e. I cannot document them to blindly pass
-sASYNCIFY_IMPORTS=['a','b','c']
to their link line, since if they have their ownASYNCIFY_IMPORTS
as well, either they will override my imports (hence having no effect), or my documented line will replace their imports (breaking their asyncified functions).
Instead, I would like to declare the function to be asynchronous in my JS file
navigator_gpu_request_adapter_sync__asyncify: true, // Declare this function to require asyncify
navigator_gpu_request_adapter_sync: function() {
return Asyncify.handleAsync(() => {
return navigator['gpu']['requestAdapter']().then(adapter => {
return wgpuStore(adapter);
})
});
},
This way both problems 1) and 2) would be fixed, without asking the people to need to know about the internals of my WebGPU JS library and have them manually maintain these types of lists when using the library.
Would this be feasible to implement? (or does it come down to the same JS library parsing order issue that deps_info.py has?)