-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Restructuring emscripten_dlopen_js #21595
Open
impact-maker
wants to merge
6
commits into
emscripten-core:main
Choose a base branch
from
impact-maker:emscripten_dlopen_js
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4aa494d
restructuring emscripten_dlopen_js
impact-maker 5877e57
restructed the code
impact-maker 2d2f41f
changed promise to callback format
impact-maker de9e66b
dlopen revision
impact-maker 6d3a264
added promisetoCallbacks and removed error
impact-maker 0508b17
internal function restored
impact-maker File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1134,6 +1134,7 @@ var LibraryDylink = { | |
} | ||
}, | ||
|
||
|
||
_dlopen_js__deps: ['$dlopenInternal'], | ||
#if ASYNCIFY | ||
_dlopen_js__async: true, | ||
|
@@ -1150,12 +1151,33 @@ var LibraryDylink = { | |
#endif | ||
}, | ||
|
||
// Async version of dlopen. | ||
_emscripten_dlopen_js__deps: ['$dlopenInternal', '$callUserCallback', '$dlSetError'], | ||
_emscripten_dlopen_js: (handle, onsuccess, onerror, user_data) => { | ||
// Utility function to encapsulate promise handling with callbacks | ||
$promiseToCallback: function(promise, successFnPtr, errorFnPtr, user_data) { | ||
function errorCallback(e) { | ||
var filename = UTF8ToString(handle + C_STRUCTS.dso.name); | ||
dlSetError(`Could not load dynamic lib: ${filename}\n${e}`); | ||
runtimeKeepalivePop(); | ||
if(errorFnPtr) { | ||
callUserCallback(() => dynCall('vpp', errorFnPtr, [0, user_data])); | ||
} | ||
} | ||
function successCallback() { | ||
runtimeKeepalivePop(); | ||
if(successFnPtr) { | ||
callUserCallback(() => dynCall('vpp', successFnPtr, [0, user_data])); | ||
} | ||
} | ||
|
||
// Assert that the promise is provided | ||
assert(promise, 'promiseToCallback was called without a valid promise.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this line at the top of the function and wrap in |
||
promise.then(successCallback).catch(errorCallback); | ||
}, | ||
|
||
_emscripten_dlopen_js__deps: ['$dlopenInternal', '$callUserCallback', '$dlSetError', $promiseToCallback], | ||
_emscripten_dlopen_js: (filename, onsuccess, onerror, user_data) => { | ||
/** @param {Object=} e */ | ||
function errorCallback(e) { | ||
var filename = UTF8ToString(handle + {{{ C_STRUCTS.dso.name }}}); | ||
var filename = UTF8ToString(filename + {{{ C_STRUCTS.dso.name }}}); | ||
dlSetError(`'Could not load dynamic lib: ${filename}\n${e}`); | ||
{{{ runtimeKeepalivePop() }}} | ||
callUserCallback(() => {{{ makeDynCall('vpp', 'onerror') }}}(handle, user_data)); | ||
|
@@ -1166,14 +1188,13 @@ var LibraryDylink = { | |
} | ||
|
||
{{{ runtimeKeepalivePush() }}} | ||
var promise = dlopenInternal(handle, { loadAsync: true }); | ||
if (promise) { | ||
promise.then(successCallback, errorCallback); | ||
} else { | ||
errorCallback(); | ||
} | ||
var promise = dlopenInternal(filename, { loadAsync: true }); | ||
promiseToCallback(promise, successCallback, errorCallback,user_data); | ||
}, | ||
|
||
|
||
|
||
|
||
_dlsym_catchup_js: (handle, symbolIndex) => { | ||
#if DYLINK_DEBUG | ||
dbg("_dlsym_catchup: handle=" + ptrToString(handle) + " symbolIndex=" + symbolIndex); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,35 +586,13 @@ void* dlopen(const char* file, int flags) { | |
|
||
void emscripten_dlopen(const char* filename, int flags, void* user_data, | ||
em_dlopen_callback onsuccess, em_arg_callback_func onerror) { | ||
dbg("emscripten_dlopen: %s", filename); | ||
if (!filename) { | ||
onsuccess(user_data, head->dso); | ||
return; | ||
} | ||
do_write_lock(); | ||
|
||
|
||
char buf[2*NAME_MAX+2]; | ||
filename = resolve_path(buf, filename, sizeof buf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happened to this call to resolve_path? |
||
struct dso* p = find_existing(filename); | ||
if (p) { | ||
onsuccess(user_data, p); | ||
return; | ||
} | ||
p = load_library_start(filename, flags); | ||
if (!p) { | ||
do_write_unlock(); | ||
onerror(user_data); | ||
return; | ||
} | ||
|
||
// For async mode | ||
struct async_data* d = malloc(sizeof(struct async_data)); | ||
d->user_data = user_data; | ||
d->onsuccess = onsuccess; | ||
d->onerror = onerror; | ||
em_promise_t promise = emscripten_dlopen_promise(filename,flags,user_data); | ||
promiseToCallback(promise, onsuccess, onerror, user_data); | ||
|
||
dbg("calling emscripten_dlopen_js %p", p); | ||
// Unlock happens in dlopen_onsuccess/dlopen_onerror | ||
_emscripten_dlopen_js(p, dlopen_onsuccess, dlopen_onerror, d); | ||
} | ||
|
||
static void promise_onsuccess(void* user_data, void* handle) { | ||
|
@@ -635,17 +613,31 @@ static void promise_onerror(void* user_data) { | |
// based API (emscripten_dlopen). | ||
// TODO(sbc): Consider inverting this and perhaps deprecating/removing | ||
// the old API. | ||
em_promise_t emscripten_dlopen_promise(const char* filename, int flags) { | ||
// Create a promise that is resolved (and destroyed) once the operation | ||
// succeeds. | ||
em_promise_t p = emscripten_promise_create(); | ||
emscripten_dlopen(filename, flags, p, promise_onsuccess, promise_onerror); | ||
|
||
// Create a second promise bound the first one to return the caller. It's | ||
// then up to the caller to destroy this promise. | ||
em_promise_t ret = emscripten_promise_create(); | ||
emscripten_promise_resolve(ret, EM_PROMISE_MATCH, p); | ||
return ret; | ||
em_promise_t emscripten_dlopen_promise(const char* filename, int flags,void* user_data) { | ||
|
||
if (!filename) { | ||
return emscripten_promise_resolve(EM_PROMISE_FULFILL, RTLD_DEFAULT); | ||
} | ||
|
||
struct dso* p = find_existing(filename); | ||
|
||
if (p) { | ||
return emscripten_promise_resolve(EM_PROMISE_FULFILL, p); | ||
} | ||
|
||
em_promise_t promise = emscripten_promise_create(); | ||
|
||
p = load_library_start(filename, flags); | ||
|
||
if (!p) { | ||
emscripten_promise_reject(promise, NULL); | ||
} else { | ||
struct async_data* d = malloc(sizeof(struct async_data)); | ||
d->user_data = user_data; | ||
_emscripten_dlopen_js(p, dlopen_onsuccess, dlopen_onerror,d); | ||
} | ||
|
||
return promise; | ||
} | ||
|
||
void* __dlsym(void* restrict p, const char* restrict s, void* restrict ra) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this utility being called anywhere