Skip to content

Commit

Permalink
Fixed method declarations conforming with Node-API default (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderfloh authored May 21, 2022
1 parent c070c5d commit efee5f0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ Napi::FunctionReference Database::constructor;

Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
// declare napi_default_method here as it is only available in Node v14.12.0+
napi_property_attributes napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);

Napi::Function t = DefineClass(env, "Database", {
InstanceMethod("close", &Database::Close),
InstanceMethod("exec", &Database::Exec),
InstanceMethod("wait", &Database::Wait),
InstanceMethod("loadExtension", &Database::LoadExtension),
InstanceMethod("serialize", &Database::Serialize),
InstanceMethod("parallelize", &Database::Parallelize),
InstanceMethod("configure", &Database::Configure),
InstanceMethod("interrupt", &Database::Interrupt),
InstanceMethod("close", &Database::Close, napi_default_method),
InstanceMethod("exec", &Database::Exec, napi_default_method),
InstanceMethod("wait", &Database::Wait, napi_default_method),
InstanceMethod("loadExtension", &Database::LoadExtension, napi_default_method),
InstanceMethod("serialize", &Database::Serialize, napi_default_method),
InstanceMethod("parallelize", &Database::Parallelize, napi_default_method),
InstanceMethod("configure", &Database::Configure, napi_default_method),
InstanceMethod("interrupt", &Database::Interrupt, napi_default_method),
InstanceAccessor("open", &Database::OpenGetter, nullptr)
});

Expand Down
74 changes: 74 additions & 0 deletions test/patching.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var sqlite3 = require('..');
var assert = require('assert');

describe('patching', function() {
var db;
var originalFunctions = {};

before(function() {
originalFunctions.close = sqlite3.Database.prototype.close;
originalFunctions.exec = sqlite3.Database.prototype.exec;
originalFunctions.wait = sqlite3.Database.prototype.wait;
originalFunctions.loadExtension = sqlite3.Database.prototype.loadExtension;
originalFunctions.serialize = sqlite3.Database.prototype.serialize;
originalFunctions.parallelize = sqlite3.Database.prototype.parallelize;
originalFunctions.configure = sqlite3.Database.prototype.configure;
originalFunctions.interrupt = sqlite3.Database.prototype.interrupt;
});

it('allow patching native functions', function() {
var myFun = function myFunction() {
return "Success";
}

assert.doesNotThrow(() => {
sqlite3.Database.prototype.close = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.exec = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.wait = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.loadExtension = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.serialize = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.parallelize = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.configure = myFun;
});
assert.doesNotThrow(() => {
sqlite3.Database.prototype.interrupt = myFun;
});

db = new sqlite3.Database(':memory:');
assert.strictEqual(db.close(), "Success");
assert.strictEqual(db.exec(), "Success");
assert.strictEqual(db.wait(), "Success");
assert.strictEqual(db.loadExtension(), "Success");
assert.strictEqual(db.serialize(), "Success");
assert.strictEqual(db.parallelize(), "Success");
assert.strictEqual(db.configure(), "Success");
assert.strictEqual(db.interrupt(), "Success");
});

after(function() {
if(db != null) {
sqlite3.Database.prototype.close = originalFunctions.close;
sqlite3.Database.prototype.exec = originalFunctions.exec;
sqlite3.Database.prototype.wait = originalFunctions.wait;
sqlite3.Database.prototype.loadExtension = originalFunctions.loadExtension;
sqlite3.Database.prototype.serialize = originalFunctions.serialize;
sqlite3.Database.prototype.parallelize = originalFunctions.parallelize;
sqlite3.Database.prototype.configure = originalFunctions.configure;
sqlite3.Database.prototype.interrupt = originalFunctions.interrupt;
db.close();
}
});

});

0 comments on commit efee5f0

Please sign in to comment.