Skip to content

Commit

Permalink
conditional compile with CreationContext for NodeJs < 16
Browse files Browse the repository at this point in the history
  • Loading branch information
neoxpert committed Sep 10, 2022
1 parent 361be1c commit a0227e3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
50 changes: 27 additions & 23 deletions src/better_sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
//

#include "better_sqlite3.hpp"
#line 39 "./src/util/binder.lzz"
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
v8::Local<v8::Value> proto = obj->GetPrototype();

#if defined NODE_MODULE_VERSION && NODE_MODULE_VERSION < 93
v8::Local<v8::Context> ctx = obj->CreationContext();
#else
v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
#endif

ctx->Enter();
v8::Local<v8::Value> baseProto = v8::Object::New(isolate)->GetPrototype();
ctx->Exit();
return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
}
#line 67 "./src/better_sqlite3.lzz"
NODE_MODULE_INIT(/* exports, context */) {
v8::Isolate* isolate = context->GetIsolate();
Expand Down Expand Up @@ -1943,37 +1958,26 @@ bool Binder::Bind (v8::FunctionCallbackInfo <v8 :: Value> const & info, int argc
}
return success;
}
#line 35 "./src/util/binder.lzz"
bool Binder::IsPlainObject (v8::Isolate * isolate, v8::Local <v8::Object> obj)
#line 35 "./src/util/binder.lzz"
{
v8::Local<v8::Value> proto = obj->GetPrototype();
v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
ctx->Enter();
v8::Local<v8::Value> baseProto = v8::Object::New(isolate)->GetPrototype();
ctx->Exit();
return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
}
#line 44 "./src/util/binder.lzz"
#line 54 "./src/util/binder.lzz"
void Binder::Fail (void (* Throw) (char const *), char const * message)
#line 44 "./src/util/binder.lzz"
#line 54 "./src/util/binder.lzz"
{
assert(success == true);
assert((Throw == NULL) == (message == NULL));
assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
if (Throw) Throw(message);
success = false;
}
#line 52 "./src/util/binder.lzz"
#line 62 "./src/util/binder.lzz"
int Binder::NextAnonIndex ()
#line 52 "./src/util/binder.lzz"
#line 62 "./src/util/binder.lzz"
{
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
return anon_index;
}
#line 58 "./src/util/binder.lzz"
#line 68 "./src/util/binder.lzz"
void Binder::BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int index)
#line 58 "./src/util/binder.lzz"
#line 68 "./src/util/binder.lzz"
{
int status = Data::BindValueFromJS(isolate, handle, index, value);
if (status != SQLITE_OK) {
Expand All @@ -1992,9 +1996,9 @@ void Binder::BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int
assert(false);
}
}
#line 79 "./src/util/binder.lzz"
#line 89 "./src/util/binder.lzz"
int Binder::BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr)
#line 79 "./src/util/binder.lzz"
#line 89 "./src/util/binder.lzz"
{
v8 :: Local < v8 :: Context > ctx = isolate -> GetCurrentContext ( ) ;
uint32_t length = arr->Length();
Expand All @@ -2016,9 +2020,9 @@ int Binder::BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr)
}
return len;
}
#line 105 "./src/util/binder.lzz"
#line 115 "./src/util/binder.lzz"
int Binder::BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, Statement * stmt)
#line 105 "./src/util/binder.lzz"
#line 115 "./src/util/binder.lzz"
{
v8 :: Local < v8 :: Context > ctx = isolate -> GetCurrentContext ( ) ;
BindMap* bind_map = stmt->GetBindMap(isolate);
Expand Down Expand Up @@ -2055,9 +2059,9 @@ int Binder::BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, State

return len;
}
#line 149 "./src/util/binder.lzz"
#line 159 "./src/util/binder.lzz"
Binder::Result Binder::BindArgs (v8::FunctionCallbackInfo <v8 :: Value> const & info, int argc, Statement * stmt)
#line 149 "./src/util/binder.lzz"
#line 159 "./src/util/binder.lzz"
{
v8 :: Isolate * isolate = info . GetIsolate ( ) ;
int count = 0;
Expand Down
24 changes: 12 additions & 12 deletions src/better_sqlite3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <node_buffer.h>
#line 31 "./src/util/macros.lzz"
template <class T> using CopyablePersistent = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;
#line 36 "./src/util/binder.lzz"
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj);
#define LZZ_INLINE inline
#line 16 "./src/util/macros.lzz"
v8::Local <v8::String> StringFromUtf8 (v8::Isolate * isolate, char const * data, int length);
Expand Down Expand Up @@ -752,27 +754,25 @@ class Binder
#line 32 "./src/util/binder.lzz"
bool bound_object;
};
#line 35 "./src/util/binder.lzz"
static bool IsPlainObject (v8::Isolate * isolate, v8::Local <v8::Object> obj);
#line 44 "./src/util/binder.lzz"
#line 54 "./src/util/binder.lzz"
void Fail (void (* Throw) (char const *), char const * message);
#line 52 "./src/util/binder.lzz"
#line 62 "./src/util/binder.lzz"
int NextAnonIndex ();
#line 58 "./src/util/binder.lzz"
#line 68 "./src/util/binder.lzz"
void BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int index);
#line 79 "./src/util/binder.lzz"
#line 89 "./src/util/binder.lzz"
int BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr);
#line 105 "./src/util/binder.lzz"
#line 115 "./src/util/binder.lzz"
int BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, Statement * stmt);
#line 149 "./src/util/binder.lzz"
#line 159 "./src/util/binder.lzz"
Result BindArgs (v8::FunctionCallbackInfo <v8 :: Value> const & info, int argc, Statement * stmt);
#line 189 "./src/util/binder.lzz"
#line 199 "./src/util/binder.lzz"
sqlite3_stmt * handle;
#line 190 "./src/util/binder.lzz"
#line 200 "./src/util/binder.lzz"
int param_count;
#line 191 "./src/util/binder.lzz"
#line 201 "./src/util/binder.lzz"
int anon_index;
#line 192 "./src/util/binder.lzz"
#line 202 "./src/util/binder.lzz"
bool success;
};
#line 34 "./src/better_sqlite3.lzz"
Expand Down
12 changes: 11 additions & 1 deletion src/util/binder.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ private:
bool bound_object;
};

#hdr
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj);
#end
#src
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
v8::Local<v8::Value> proto = obj->GetPrototype();

#if defined NODE_MODULE_VERSION && NODE_MODULE_VERSION < 93
v8::Local<v8::Context> ctx = obj->CreationContext();
#else
v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
#endif

ctx->Enter();
v8::Local<v8::Value> baseProto = v8::Object::New(isolate)->GetPrototype();
ctx->Exit();
return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
}

#end
void Fail(void (*Throw)(const char* _), const char* message) {
assert(success == true);
assert((Throw == NULL) == (message == NULL));
Expand Down

0 comments on commit a0227e3

Please sign in to comment.