Files
better-sqlite3-multiple-cip…/src/better_sqlite3.lzz
T
2021-05-12 23:55:34 -05:00

89 lines
3.0 KiB
C++

#hdr
#include <climits>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <sqlite3.h>
#include <node.h>
#include <node_object_wrap.h>
#include <node_buffer.h>
#end
#insert "util/macros.lzz"
#insert "util/query-macros.lzz"
#insert "util/constants.lzz"
#insert "util/bind-map.lzz"
struct Addon;
class Statement;
class Backup;
#insert "objects/database.lzz"
#insert "objects/statement.lzz"
#insert "objects/statement-iterator.lzz"
#insert "objects/backup.lzz"
#insert "util/data-converter.lzz"
#insert "util/custom-function.lzz"
#insert "util/custom-aggregate.lzz"
#insert "util/custom-table.lzz"
#insert "util/data.lzz"
#insert "util/binder.lzz"
struct Addon {
NODE_METHOD(JS_setErrorConstructor) {
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> SqliteError);
OnlyAddon->SqliteError.Reset(OnlyIsolate, SqliteError);
}
static void Cleanup(void* ptr) {
Addon* addon = static_cast<Addon*>(ptr);
for (Database* db : addon->dbs) db->CloseHandles();
addon->dbs.clear();
delete addon;
}
explicit Addon(v8::Isolate* isolate) :
privileged_info(NULL),
next_id(0),
cs(isolate) {}
inline sqlite3_uint64 NextId() {
return next_id++;
}
CopyablePersistent<v8::Function> Statement;
CopyablePersistent<v8::Function> StatementIterator;
CopyablePersistent<v8::Function> Backup;
CopyablePersistent<v8::Function> SqliteError;
NODE_ARGUMENTS_POINTER privileged_info;
sqlite3_uint64 next_id;
CS cs;
std::set<Database*, Database::CompareDatabase> dbs;
};
#src
NODE_MODULE_INIT(/* exports, context */) {
v8::Isolate* isolate = context->GetIsolate();
v8::HandleScope scope(isolate);
// Initialize addon instance.
Addon* addon = new Addon(isolate);
v8::Local<v8::External> data = v8::External::New(isolate, addon);
node::AddEnvironmentCleanupHook(isolate, Addon::Cleanup, addon);
// Create and export native-backed classes and functions.
exports->Set(context, InternalizedFromLatin1(isolate, "Database"), Database::Init(isolate, data)).FromJust();
exports->Set(context, InternalizedFromLatin1(isolate, "Statement"), Statement::Init(isolate, data)).FromJust();
exports->Set(context, InternalizedFromLatin1(isolate, "StatementIterator"), StatementIterator::Init(isolate, data)).FromJust();
exports->Set(context, InternalizedFromLatin1(isolate, "Backup"), Backup::Init(isolate, data)).FromJust();
exports->Set(context, InternalizedFromLatin1(isolate, "setErrorConstructor"), v8::FunctionTemplate::New(isolate, Addon::JS_setErrorConstructor, data)->GetFunction(context).ToLocalChecked()).FromJust();
// Store addon instance data.
addon->Statement.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "Statement")).ToLocalChecked().As<v8::Function>());
addon->StatementIterator.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "StatementIterator")).ToLocalChecked().As<v8::Function>());
addon->Backup.Reset(isolate, exports->Get(context, InternalizedFromLatin1(isolate, "Backup")).ToLocalChecked().As<v8::Function>());
}
#end