Skip to content

Commit a3b2930

Browse files
committed
Add base implementation to await futures (only working in NodeJS).
1 parent dda471c commit a3b2930

File tree

7 files changed

+419
-6
lines changed

7 files changed

+419
-6
lines changed

source/loaders/node_loader/bootstrap/lib/bootstrap.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function node_loader_trampoline_test(obj) {
288288
}
289289
}
290290

291-
function node_loader_trampoline_await(trampoline) {
291+
function node_loader_trampoline_await_function(trampoline) {
292292
if (!trampoline) {
293293
return function node_loader_trampoline_await_impl(func, args, trampoline_ptr) {
294294
console.error('NodeJS Loader await error, trampoline could not be found, await calls are disabled.');
@@ -311,9 +311,39 @@ function node_loader_trampoline_await(trampoline) {
311311
return new Promise((resolve, reject) =>
312312
func(...args).then(
313313
x => resolve(trampoline.resolve(trampoline_ptr, x)),
314-
x => reject(trampoline.reject(trampoline_ptr, x)),
314+
x => reject(trampoline.reject(trampoline_ptr, x))
315315
).catch(
316-
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`),
316+
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`)
317+
)
318+
);
319+
};
320+
}
321+
322+
function node_loader_trampoline_await_future(trampoline) {
323+
if (!trampoline) {
324+
return function node_loader_trampoline_await_impl(func, args, trampoline_ptr) {
325+
console.error('NodeJS Loader await error, trampoline could not be found, await calls are disabled.');
326+
};
327+
}
328+
329+
return function node_loader_trampoline_await_impl(future, trampoline_ptr) {
330+
// This apparently does not work for native promises, let it uncommented until we find a proper way of detecting the type
331+
/*
332+
if (!!future && typeof future.then === 'function') {
333+
throw new Error('Await only accepts a thenable promise, not ' + typeof future);
334+
}
335+
*/
336+
337+
if (typeof trampoline_ptr !== 'object') {
338+
throw new Error('Await trampoline_ptr must be an object, not ' + typeof trampoline_ptr);
339+
}
340+
341+
return new Promise((resolve, reject) =>
342+
future.then(
343+
x => resolve(trampoline.resolve(trampoline_ptr, x)),
344+
x => reject(trampoline.reject(trampoline_ptr, x))
345+
).catch(
346+
x => console.error(`NodeJS await error: ${x && x.message ? x.message : util.inspect(x, false, null, true)}`)
317347
)
318348
);
319349
};
@@ -360,7 +390,8 @@ module.exports = ((impl, ptr) => {
360390
'discover': node_loader_trampoline_discover,
361391
'discover_function': node_loader_trampoline_discover_function,
362392
'test': node_loader_trampoline_test,
363-
'await': node_loader_trampoline_await(trampoline),
393+
'await_function': node_loader_trampoline_await_function(trampoline),
394+
'await_future': node_loader_trampoline_await_future(trampoline),
364395
'destroy': node_loader_trampoline_destroy,
365396
});
366397
} catch (ex) {

0 commit comments

Comments
 (0)