Skip to content

Commit

Permalink
Don't break support for NAPI_VERSION < 6
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoalencar committed May 12, 2022
1 parent 9fae592 commit 730ab55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"remote_path": "v{version}",
"package_name": "napi-v{napi_build_version}-{platform}-{libc}-{arch}.tar.gz",
"napi_versions": [
3,
6
]
},
Expand Down
10 changes: 7 additions & 3 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ template <class T> Values::Field*
else if (OtherInstanceOf(source.As<Object>(), "Date")) {
return new Values::Float(pos, source.ToNumber().DoubleValue());
}
#if NAPI_VERSION >= 6
else if (source.IsBigInt()) {
bool lossless;
auto ret = new Values::Integer(pos, source.As<Napi::BigInt>().Int64Value(&lossless));
Expand All @@ -218,6 +219,7 @@ template <class T> Values::Field*

return ret;
}
#endif
else if (source.IsObject()) {
Napi::String napiVal = source.ToString();
// Check whether toString returned a value that is not undefined.
Expand Down Expand Up @@ -826,11 +828,13 @@ Napi::Value Statement::RowToJS(Napi::Env env, Row* row) {
case SQLITE_INTEGER: {
auto field_value = ((Values::Integer*)field)->value;

#if NAPI_VERSION >= 6
if (field_value > JS_MAX_SAFE_INTEGER || field_value < JS_MIN_SAFE_INTEGER) {
value = Napi::BigInt::New(env, field_value);
}
else {
value = Napi::Number::New(env, ((Values::Integer*)field)->value);
} else
#endif
{
value = Napi::Number::New(env, field_value);
}
} break;
case SQLITE_FLOAT: {
Expand Down
58 changes: 31 additions & 27 deletions test/other_objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,40 @@ describe('data types', function() {
});
});

[
1n,
1337n,
8876343627091516928n,
-8876343627091516928n,
].forEach(function (bigint) {
it('should serialize BigInt ' + bigint, function (done) {
db.run("INSERT INTO bigint_table VALUES(?)", bigint, function(err) {
if (err) throw err;
db.get("SELECT big AS bigint FROM bigint_table", function (err, row) {
if (err) throw err
assert.equal(row.bigint, bigint);
done();
if (process.versions.napi >= '6') {
[
1n,
1337n,
8876343627091516928n,
-8876343627091516928n,
].forEach(function (bigint) {
it('should serialize BigInt ' + bigint, function (done) {
db.run("INSERT INTO bigint_table VALUES(?)", bigint, function(err) {
if (err) throw err;
db.get("SELECT big AS bigint FROM bigint_table", function (err, row) {
if (err) throw err
assert.equal(row.bigint, bigint);
done();
});
});
});
});
});

it('should fail to serialize larger numbers', function (done) {
const bigint = 0xffffffffffffffffffffn; // 80 bits
let error;
}

try {
db.run('INSERT INTO bigint_table VALUES(?)', bigint);
} catch (err) {
error = err;
} finally {
assert.notEqual(error, undefined);
done();
}
})
if (process.versions.napi >= '6') {
it('should fail to serialize larger numbers', function (done) {
const bigint = 0xffffffffffffffffffffn; // 80 bits
let error;

try {
db.run('INSERT INTO bigint_table VALUES(?)', bigint);
} catch (err) {
error = err;
} finally {
assert.notEqual(error, undefined);
done();
}
})
}

});

0 comments on commit 730ab55

Please sign in to comment.