Skip to content

Commit

Permalink
Fixed callback handler in native code. Fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed May 22, 2020
1 parent 486e238 commit c51bd77
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "utils.h"
#include "future.h"

// #include <cstdio>

// #include <v8.h>

// #include "Version.h"
Expand Down Expand Up @@ -178,13 +180,21 @@ MaybeValue fdbFutureToCallback(napi_env env, FDBFuture *f, napi_value cbFunc, Ex
NAPI_OK_OR_RETURN_STATUS(env, napi_get_reference_value(env, ctx->cbFunc, &callback));
NAPI_OK_OR_RETURN_STATUS(env, napi_reference_unref(env, ctx->cbFunc, NULL));

napi_value args[2] = {}; // (err, value).
if (errcode != 0) NAPI_OK_OR_RETURN_STATUS(env, wrap_fdb_error(env, errcode, &args[0]));
else if (value.status != napi_ok) NAPI_OK_OR_RETURN_STATUS(env, napi_get_and_clear_last_exception(env, &args[0]));
else args[1] = value.value;
size_t argc = 1; // In case of error we just won't populate argv[1].
napi_value argv[2] = {}; // (err, value).

if (errcode != 0) NAPI_OK_OR_RETURN_STATUS(env, wrap_fdb_error(env, errcode, &argv[0]));
else if (value.status != napi_ok) NAPI_OK_OR_RETURN_STATUS(env, napi_get_and_clear_last_exception(env, &argv[0]));
else {
argc = 2;
NAPI_OK_OR_RETURN_STATUS(env, napi_get_undefined(env, &argv[0]));
argv[1] = value.value;
}

// If this throws it'll bubble up to the node uncaught exception handler, which is what we want.
napi_call_function(env, NULL, callback, 2, args, NULL);
napi_value global;
NAPI_OK_OR_RETURN_STATUS(env, napi_get_global(env, &global));
// We're discarding the return value here.
NAPI_OK_OR_RETURN_STATUS(env, napi_call_function(env, global, callback, argc, argv, NULL));

return napi_ok;
});
Expand Down
1 change: 1 addition & 0 deletions src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include <cstdlib>
// #include <cstdio>
#include <cassert>

#include "options.h"
Expand Down
27 changes: 27 additions & 0 deletions test/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,33 @@ withEachDb(db => describe('key value functionality', () => {
})
})

describe('callback API', () => {
// Unless benchmarking shows a significant difference, this will be removed
// in a future version of this library.
it('allows tn.get() with a callback', async () => {
let called = false
await db.doTransaction(async tn => {
tn.set('xxx', 'hi')

// Ok now fetch it. I'm wrapping this in an awaited promise so we don't
// commit the transaction before .get() has resolved.
await new Promise((resolve, reject) => {
tn.get('xxx', (err, data) => {
try {
assert(!called)
called = true
if (err) throw err
assert.deepStrictEqual(data, Buffer.from('hi'))
resolve()
} catch (e) { reject(e) }
})
})
})
assert(called)
})

})

describe('regression', () => {
it('does not trim off the end of a string', async () => {
// https://github.com/josephg/node-foundationdb/issues/40
Expand Down

0 comments on commit c51bd77

Please sign in to comment.