Skip to content

Commit

Permalink
Add base implementation to await futures (only working in NodeJS).
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Feb 26, 2021
1 parent dda471c commit a3b2930
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 6 deletions.
39 changes: 35 additions & 4 deletions source/loaders/node_loader/bootstrap/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function node_loader_trampoline_test(obj) {
}
}

function node_loader_trampoline_await(trampoline) {
function node_loader_trampoline_await_function(trampoline) {
if (!trampoline) {
return function node_loader_trampoline_await_impl(func, args, trampoline_ptr) {
console.error('NodeJS Loader await error, trampoline could not be found, await calls are disabled.');
Expand All @@ -311,9 +311,39 @@ function node_loader_trampoline_await(trampoline) {
return new Promise((resolve, reject) =>
func(...args).then(
x => resolve(trampoline.resolve(trampoline_ptr, x)),
x => reject(trampoline.reject(trampoline_ptr, x)),
x => reject(trampoline.reject(trampoline_ptr, x))
).catch(
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`),
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`)
)
);
};
}

function node_loader_trampoline_await_future(trampoline) {
if (!trampoline) {
return function node_loader_trampoline_await_impl(func, args, trampoline_ptr) {
console.error('NodeJS Loader await error, trampoline could not be found, await calls are disabled.');
};
}

return function node_loader_trampoline_await_impl(future, trampoline_ptr) {
// This apparently does not work for native promises, let it uncommented until we find a proper way of detecting the type
/*
if (!!future && typeof future.then === 'function') {
throw new Error('Await only accepts a thenable promise, not ' + typeof future);
}
*/

if (typeof trampoline_ptr !== 'object') {
throw new Error('Await trampoline_ptr must be an object, not ' + typeof trampoline_ptr);
}

return new Promise((resolve, reject) =>
future.then(
x => resolve(trampoline.resolve(trampoline_ptr, x)),
x => reject(trampoline.reject(trampoline_ptr, x))
).catch(
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`)
)
);
};
Expand Down Expand Up @@ -360,7 +390,8 @@ module.exports = ((impl, ptr) => {
'discover': node_loader_trampoline_discover,
'discover_function': node_loader_trampoline_discover_function,
'test': node_loader_trampoline_test,
'await': node_loader_trampoline_await(trampoline),
'await_function': node_loader_trampoline_await_function(trampoline),
'await_future': node_loader_trampoline_await_future(trampoline),
'destroy': node_loader_trampoline_destroy,
});
} catch (ex) {
Expand Down
Loading

0 comments on commit a3b2930

Please sign in to comment.