From 6ab828e82556319bcc9a7d65ebd5aa40b820cb5b Mon Sep 17 00:00:00 2001 From: Boris Verkhovskiy Date: Tue, 11 Apr 2023 21:23:34 +0100 Subject: [PATCH 1/3] Make compilable with Node 19 --- src/conversions.cc | 2 +- src/logger.cc | 14 ++++++++++++-- src/node.cc | 2 +- src/parser.cc | 12 ++++++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/conversions.cc b/src/conversions.cc index fefe01d7..0c749dd9 100644 --- a/src/conversions.cc +++ b/src/conversions.cc @@ -34,7 +34,7 @@ void InitConversions(Local exports) { v8::Local bufferView; bufferView = node::Buffer::New(Isolate::GetCurrent(), point_transfer_buffer, 0, 2 * sizeof(uint32_t)).ToLocalChecked(); auto js_point_transfer_buffer = node::Buffer::Data(bufferView); - #elif V8_MAJOR_VERSION >= 8 + #elif (V8_MAJOR_VERSION > 8 || (V8_MAJOR_VERSION == 8 && V8_MINOR_VERION > 3)) auto backing_store = ArrayBuffer::NewBackingStore(point_transfer_buffer, 2 * sizeof(uint32_t), BackingStore::EmptyDeleter, nullptr); auto js_point_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing_store)); #else diff --git a/src/logger.cc b/src/logger.cc index 5f0cacd1..aeb86ff7 100644 --- a/src/logger.cc +++ b/src/logger.cc @@ -41,14 +41,24 @@ void Logger::Log(void *payload, TSLogType type, const char *message_str) { Local argv[3] = { name, params, type_name }; TryCatch try_catch(Isolate::GetCurrent()); - Nan::Call(fn, fn->CreationContext()->Global(), 3, argv); + + #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) + Nan::Call(fn, fn->GetCreationContext().ToLocalChecked()->Global(), 3, argv); + #else + Nan::Call(fn, fn->CreationContext()->Global(), 3, argv); + #endif if (try_catch.HasCaught()) { Local log_argv[2] = { Nan::New("Error in debug callback:").ToLocalChecked(), try_catch.Exception() }; - Local console = Local::Cast(Nan::Get(fn->CreationContext()->Global(), Nan::New("console").ToLocalChecked()).ToLocalChecked()); + + #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) + Local console = Local::Cast(Nan::Get(fn->GetCreationContext().ToLocalChecked()->Global(), Nan::New("console").ToLocalChecked()).ToLocalChecked()); + #else + Local console = Local::Cast(Nan::Get(fn->CreationContext()->Global(), Nan::New("console").ToLocalChecked()).ToLocalChecked()); + #endif Local error_fn = Local::Cast(Nan::Get(console, Nan::New("error").ToLocalChecked()).ToLocalChecked()); Nan::Call(error_fn, console, 2, log_argv); } diff --git a/src/node.cc b/src/node.cc index be9b0468..f855ec7c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -35,7 +35,7 @@ static inline void setup_transfer_buffer(uint32_t node_count) { v8::Local bufferView; bufferView = node::Buffer::New(Isolate::GetCurrent(), transfer_buffer, 0, transfer_buffer_length * sizeof(uint32_t)).ToLocalChecked(); auto js_point_transfer_buffer = node::Buffer::Data(bufferView); - #elif V8_MAJOR_VERSION >= 8 + #elif (V8_MAJOR_VERSION > 8 || (V8_MAJOR_VERSION == 8 && V8_MINOR_VERION > 3)) auto backing_store = ArrayBuffer::NewBackingStore(transfer_buffer, transfer_buffer_length * sizeof(uint32_t), BackingStore::EmptyDeleter, nullptr); auto js_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing_store)); #else diff --git a/src/parser.cc b/src/parser.cc index 52e1beac..cd333a87 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -60,7 +60,11 @@ class CallbackInput { uint32_t utf16_unit = byte / 2; Local argv[2] = { Nan::New(utf16_unit), PointToJS(position) }; TryCatch try_catch(Isolate::GetCurrent()); - auto maybe_result_value = Nan::Call(callback, callback->CreationContext()->Global(), 2, argv); + #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) + auto maybe_result_value = Nan::Call(callback, callback->GetCreationContext().ToLocalChecked()->Global(), 2, argv); + #else + auto maybe_result_value = Nan::Call(callback, callback->CreationContext()->Global(), 2, argv); + #endif if (try_catch.HasCaught()) return nullptr; Local result_value; @@ -364,7 +368,11 @@ void Parser::ParseTextBuffer(const Nan::FunctionCallbackInfo &info) { delete input; Local argv[] = {Tree::NewInstance(result)}; auto callback = info[0].As(); - Nan::Call(callback, callback->CreationContext()->Global(), 1, argv); + #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) + Nan::Call(callback, callback->GetCreationContext().ToLocalChecked()->Global(), 1, argv); + #else + Nan::Call(callback, callback->CreationContext()->Global(), 1, argv); + #endif return; } } From 2f6a66be8fdecb64501baa639473a323b9e02cd0 Mon Sep 17 00:00:00 2001 From: Boris Verkhovskiy Date: Tue, 11 Apr 2023 21:34:00 +0100 Subject: [PATCH 2/3] Pin tree-sitter-javascript to pass CI --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e4232aff..881ff503 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "mocha": "^8.3.1", "prebuild": "^10.0.1", "superstring": "^2.4.2", - "tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master" + "tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#936d976a782e75395d9b1c8c7c7bf4ba6fe0d86b" }, "scripts": { "install": "prebuild-install || node-gyp rebuild", From 6fc5bb7f37fc87c1e2b834352f383236ebeae72f Mon Sep 17 00:00:00 2001 From: Boris Verkhovskiy Date: Wed, 14 Jun 2023 17:18:24 +0100 Subject: [PATCH 3/3] Move #if to separate function --- src/logger.cc | 15 +++------------ src/parser.cc | 12 ++---------- src/util.cc | 8 ++++++++ src/util.h | 2 ++ 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/logger.cc b/src/logger.cc index aeb86ff7..9c3d47da 100644 --- a/src/logger.cc +++ b/src/logger.cc @@ -3,6 +3,7 @@ #include #include #include +#include "./util.h" namespace node_tree_sitter { @@ -41,24 +42,14 @@ void Logger::Log(void *payload, TSLogType type, const char *message_str) { Local argv[3] = { name, params, type_name }; TryCatch try_catch(Isolate::GetCurrent()); - - #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) - Nan::Call(fn, fn->GetCreationContext().ToLocalChecked()->Global(), 3, argv); - #else - Nan::Call(fn, fn->CreationContext()->Global(), 3, argv); - #endif + Nan::Call(fn, GetGlobal(fn), 3, argv); if (try_catch.HasCaught()) { Local log_argv[2] = { Nan::New("Error in debug callback:").ToLocalChecked(), try_catch.Exception() }; - - #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) - Local console = Local::Cast(Nan::Get(fn->GetCreationContext().ToLocalChecked()->Global(), Nan::New("console").ToLocalChecked()).ToLocalChecked()); - #else - Local console = Local::Cast(Nan::Get(fn->CreationContext()->Global(), Nan::New("console").ToLocalChecked()).ToLocalChecked()); - #endif + Local console = Local::Cast(Nan::Get(GetGlobal(fn), Nan::New("console").ToLocalChecked()).ToLocalChecked()); Local error_fn = Local::Cast(Nan::Get(console, Nan::New("error").ToLocalChecked()).ToLocalChecked()); Nan::Call(error_fn, console, 2, log_argv); } diff --git a/src/parser.cc b/src/parser.cc index cd333a87..12954157 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -60,11 +60,7 @@ class CallbackInput { uint32_t utf16_unit = byte / 2; Local argv[2] = { Nan::New(utf16_unit), PointToJS(position) }; TryCatch try_catch(Isolate::GetCurrent()); - #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) - auto maybe_result_value = Nan::Call(callback, callback->GetCreationContext().ToLocalChecked()->Global(), 2, argv); - #else - auto maybe_result_value = Nan::Call(callback, callback->CreationContext()->Global(), 2, argv); - #endif + auto maybe_result_value = Nan::Call(callback, GetGlobal(callback), 2, argv); if (try_catch.HasCaught()) return nullptr; Local result_value; @@ -368,11 +364,7 @@ void Parser::ParseTextBuffer(const Nan::FunctionCallbackInfo &info) { delete input; Local argv[] = {Tree::NewInstance(result)}; auto callback = info[0].As(); - #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) - Nan::Call(callback, callback->GetCreationContext().ToLocalChecked()->Global(), 1, argv); - #else - Nan::Call(callback, callback->CreationContext()->Global(), 1, argv); - #endif + Nan::Call(callback, GetGlobal(callback), 1, argv); return; } } diff --git a/src/util.cc b/src/util.cc index 41133c66..5ea9084f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -11,4 +11,12 @@ bool instance_of(v8::Local value, v8::Local object) { return maybe_bool.FromJust(); } +v8::Local GetGlobal(v8::Local& callback) { + #if (V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERION > 4)) + return callback->GetCreationContext().ToLocalChecked()->Global(); + #else + return callback->CreationContext()->Global(); + #endif +} + } // namespace node_tree_sitter diff --git a/src/util.h b/src/util.h index eaa7cd99..42de5d13 100644 --- a/src/util.h +++ b/src/util.h @@ -20,6 +20,8 @@ struct FunctionPair { bool instance_of(v8::Local value, v8::Local object); +v8::Local GetGlobal(v8::Local& callback); + } // namespace node_tree_sitter #endif // NODE_TREE_SITTER_UTIL_H_