Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix method declarations to conform with napi default for methods #1510

Merged
merged 3 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix method declarations to conform with napi default for methods
  • Loading branch information
alexanderfloh committed Oct 11, 2021
commit 243fc436e99959e48a3ffb3c54abfe0ef3fa9bc9
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
56 changes: 56 additions & 0 deletions test/patching.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var sqlite3 = require('..');
var assert = require('assert');

describe('patching', function() {
var db;
var originalClose;

it('allow patching native functions', function() {
var myFun = function myFunction() {
return "Success";
}
originalClose = sqlite3.Database.prototype.close;
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) {
db.close = originalClose;
db.close();
}
});

});