Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructed async load function #21665

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

impact-maker
Copy link

@impact-maker impact-maker commented Apr 1, 2024

Solves #21440

script.onload = () => {
console.log(`Script ${url} loaded successfully.`);
if (onloadPtr) {
var onload = Module['getProcAddress'](onloadPtr, 'v');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is getProcAddress here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getProcAddress function is expected to obtain the address of a web assembly function and return a callable JavaScript version of it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. In that case I would suggest using the makeDynCall macro call: {{{ makeDynCal(...) }}};

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the macro calls now

@impact-maker
Copy link
Author

impact-maker commented Apr 5, 2024

@sbc100 @tlively I have restructured function in a way the promise is implemented at the top and callbacks are implemented at the base of the promise. For asyncify I can add a handle sleep call but we can use a flag instead.

I have run the test case and verified that the added function is creating no test failures. I am getting the following error while running tests on my local:

error: undefined symbol: saveSetjmp (referenced by root reference (e.g. compiled C/C++ code))
warning: To disable errors for undefined symbols use -sERROR_ON_UNDEFINED_SYMBOLS=0
warning: _saveSetjmp may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: testSetjmp (referenced by root reference (e.g. compiled C/C++ code))
warning: _testSetjmp may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors

Could you help me with your valuable feedback on the overall code as well the test failures.

@tlively
Copy link
Member

tlively commented Apr 5, 2024

Taking a step back, can you describe your high-level goal here? I would have expected to see this PR define a new function called something like emscripten_async_load_script_promise that contains all the implementation logic, then redefine the existing callback-based emscripten_async_load_script in terms of that new function. In contrast, this PR seems to just change the implementation of the existing function to use a promise.

@impact-maker impact-maker force-pushed the asyncloadbranch branch 2 times, most recently from 99b995e to 18c02f7 Compare April 9, 2024 22:08
@impact-maker
Copy link
Author

impact-maker commented Apr 15, 2024

I tried to change the original function in the promise -> callbacks and asyncify format but after the feedback, I have introduced a new function implementing the promise flavour of the api and reconstructed the original function aorund the promise version with backward compatibility with callbacks. I was working on resolving the test failures since some time now. I would appreciate your feedback on my fundamental appproach in the latest commit @tlively @sbc100 @kripken

emscripten_async_run_script: (script, millis) => {
// TODO: cache these to avoid generating garbage
safeSetTimeout(() => _emscripten_run_script(script), millis);
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these 5 lines got duplicated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the duplicate code @sbc100

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to be sure whether I am heading in the right direction fundamentally for the api unification

}
});

},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this approach is to be generalized then perhaps this can be replaced with a helper function. e.g:

emscripten_async_load_script: function(url, onload, onerror) {
  promiseToCallbacks(emscripten_async_load_script_promise(url), onload, onerror);
}

However, I think it might be best to have two helpers: promiseToCallbacks and callbacksToPromise, and then use the one that makes sense for the underlying API. I suggest we only use promiseToCallbacks for APIs where the underlying API is already promise based.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined and used the helper functions @sbc100


emscripten_async_load_script_promise(url).then(() => {
if (onload){
onload();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't call these onload and onerro callback backs directly link this I think. These arguments are C pointers (numbers) on not JS function. I think you need to do what the old code did with {{{ makeDynCall('v', 'onload') }}};.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 converted calls into the old form

@impact-maker
Copy link
Author

@sbc100 Could you review my pull request and suggest me with your valuable feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants