diff --git a/.travis.yml b/.travis.yml index b1f7bc67..f1922462 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ notifications: node_js: - "8" - "10" + - "12" before_install: - export CXX="g++-4.9" CC="gcc-4.9" diff --git a/package.json b/package.json index de461b76..2fa538ee 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/atom/superstring", "dependencies": { - "nan": "^2.10.0" + "nan": "^2.13.2" }, "devDependencies": { "chai": "^2.0.0", diff --git a/src/bindings/marker-index-wrapper.cc b/src/bindings/marker-index-wrapper.cc index 8b835737..abdfa8a0 100644 --- a/src/bindings/marker-index-wrapper.cc +++ b/src/bindings/marker-index-wrapper.cc @@ -70,7 +70,7 @@ void MarkerIndexWrapper::init(Local exports) { ending_string.Reset(Nan::Persistent(Nan::New("ending").ToLocalChecked())); marker_index_constructor_template.Reset(constructor_template); - exports->Set(Nan::New("MarkerIndex").ToLocalChecked(), constructor_template->GetFunction()); + Nan::Set(exports, Nan::New("MarkerIndex").ToLocalChecked(), Nan::GetFunction(constructor_template).ToLocalChecked()); } MarkerIndex *MarkerIndexWrapper::from_js(Local value) { @@ -347,7 +347,7 @@ void MarkerIndexWrapper::find_boundaries_after(const Nan::FunctionCallbackInfo max_count; Local js_max_count; if (Nan::To(info[1]).ToLocal(&js_max_count)) { - max_count = js_max_count->NumberValue(); + max_count = Nan::To(js_max_count).FromMaybe(0); } if (start && max_count) { diff --git a/src/bindings/number-conversion.h b/src/bindings/number-conversion.h index 4c75a240..cc063be9 100644 --- a/src/bindings/number-conversion.h +++ b/src/bindings/number-conversion.h @@ -9,10 +9,12 @@ namespace number_conversion { optional number_from_js(v8::Local js_value) { v8::Local js_number; if (Nan::To(js_value).ToLocal(&js_number)) { - return js_number->NumberValue(); - } else { - return optional{}; + auto maybe_number = Nan::To(js_number); + if (maybe_number.IsJust()) { + return maybe_number.FromJust(); + } } + return optional{}; } } diff --git a/src/bindings/patch-wrapper.cc b/src/bindings/patch-wrapper.cc index 30363e0b..d2672ace 100644 --- a/src/bindings/patch-wrapper.cc +++ b/src/bindings/patch-wrapper.cc @@ -37,7 +37,7 @@ class ChangeWrapper : public Nan::ObjectWrap { const auto &prototype_template = constructor_template->PrototypeTemplate(); prototype_template->Set(Nan::New("toString").ToLocalChecked(), Nan::New(to_string)); - change_wrapper_constructor.Reset(constructor_template->GetFunction()); + change_wrapper_constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } static Local FromChange(Patch::Change change) { @@ -137,7 +137,7 @@ void PatchWrapper::init(Local exports) { prototype_template->Set(Nan::New("getChangeCount").ToLocalChecked(), Nan::New(get_change_count)); prototype_template->Set(Nan::New("getBounds").ToLocalChecked(), Nan::New(get_bounds)); patch_wrapper_constructor_template.Reset(constructor_template_local); - patch_wrapper_constructor.Reset(constructor_template_local->GetFunction()); + patch_wrapper_constructor.Reset(Nan::GetFunction(constructor_template_local).ToLocalChecked()); exports->Set(Nan::New("Patch").ToLocalChecked(), Nan::New(patch_wrapper_constructor)); } @@ -158,10 +158,9 @@ void PatchWrapper::construct(const Nan::FunctionCallbackInfo &info) { Local options; if (info.Length() > 0 && Nan::To(info[0]).ToLocal(&options)) { - Local js_merge_adjacent_changes; - if (Nan::To(options->Get(Nan::New("mergeAdjacentChanges").ToLocalChecked())) - .ToLocal(&js_merge_adjacent_changes)) { - merges_adjacent_changes = js_merge_adjacent_changes->BooleanValue(); + Local js_merge_adjacent_changes; + if (Nan::Get(options, Nan::New("mergeAdjacentChanges").ToLocalChecked()).ToLocal(&js_merge_adjacent_changes)) { + merges_adjacent_changes = Nan::To(js_merge_adjacent_changes).FromMaybe(false); } } PatchWrapper *patch = new PatchWrapper(Patch{merges_adjacent_changes}); diff --git a/src/bindings/point-wrapper.cc b/src/bindings/point-wrapper.cc index 4dd163b6..be66cd8b 100644 --- a/src/bindings/point-wrapper.cc +++ b/src/bindings/point-wrapper.cc @@ -9,7 +9,7 @@ static Nan::Persistent column_string; static Nan::Persistent constructor; static uint32_t number_from_js(Local js_number) { - double number = js_number->NumberValue(); + double number = Nan::To(js_number).FromMaybe(0); if (number > 0 && !std::isfinite(number)) { return UINT32_MAX; } else { @@ -51,7 +51,7 @@ void PointWrapper::init() { constructor_template->InstanceTemplate()->SetInternalFieldCount(1); Nan::SetAccessor(constructor_template->InstanceTemplate(), Nan::New(row_string), get_row); Nan::SetAccessor(constructor_template->InstanceTemplate(), Nan::New(column_string), get_column); - constructor.Reset(constructor_template->GetFunction()); + constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } Local PointWrapper::from_point(Point point) { diff --git a/src/bindings/range-wrapper.cc b/src/bindings/range-wrapper.cc index f6ab020b..346301a6 100644 --- a/src/bindings/range-wrapper.cc +++ b/src/bindings/range-wrapper.cc @@ -34,7 +34,7 @@ void RangeWrapper::init() { constructor_template->InstanceTemplate()->SetInternalFieldCount(1); Nan::SetAccessor(constructor_template->InstanceTemplate(), Nan::New(start_string), get_start); Nan::SetAccessor(constructor_template->InstanceTemplate(), Nan::New(end_string), get_end); - constructor.Reset(constructor_template->GetFunction()); + constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } Local RangeWrapper::from_range(Range range) { diff --git a/src/bindings/string-conversion.cc b/src/bindings/string-conversion.cc index a442b69c..7730fe01 100644 --- a/src/bindings/string-conversion.cc +++ b/src/bindings/string-conversion.cc @@ -14,6 +14,12 @@ optional string_conversion::string_from_js(Local value) { u16string result; result.resize(string->Length()); string->Write( + + // Nan doesn't wrap this functionality + #if NODE_MAJOR_VERSION >= 12 + Isolate::GetCurrent(), + #endif + reinterpret_cast(&result[0]), 0, -1, diff --git a/src/bindings/text-buffer-snapshot-wrapper.cc b/src/bindings/text-buffer-snapshot-wrapper.cc index 3ce25a7f..cfd03ec3 100644 --- a/src/bindings/text-buffer-snapshot-wrapper.cc +++ b/src/bindings/text-buffer-snapshot-wrapper.cc @@ -16,7 +16,7 @@ void TextBufferSnapshotWrapper::init() { const auto &prototype_template = constructor_template->PrototypeTemplate(); prototype_template->Set(Nan::New("destroy").ToLocalChecked(), Nan::New(destroy)); - snapshot_wrapper_constructor.Reset(constructor_template->GetFunction()); + snapshot_wrapper_constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } TextBufferSnapshotWrapper::TextBufferSnapshotWrapper(Local js_buffer, void *snapshot) : @@ -46,7 +46,7 @@ void TextBufferSnapshotWrapper::construct(const Nan::FunctionCallbackInfo } void TextBufferSnapshotWrapper::destroy(const Nan::FunctionCallbackInfo &info) { - auto reader = Nan::ObjectWrap::Unwrap(info.This()->ToObject()); + auto reader = Nan::ObjectWrap::Unwrap(Nan::To(info.This()).ToLocalChecked()); if (reader->snapshot) { delete reinterpret_cast(reader->snapshot); reader->snapshot = nullptr; diff --git a/src/bindings/text-buffer-wrapper.cc b/src/bindings/text-buffer-wrapper.cc index 8d0c6b95..1c24ae46 100644 --- a/src/bindings/text-buffer-wrapper.cc +++ b/src/bindings/text-buffer-wrapper.cc @@ -93,7 +93,7 @@ class RegexWrapper : public Nan::ObjectWrap { js_regex = Local::Cast(value); Local stored_regex = js_regex->Get(cache_key); if (!stored_regex->IsUndefined()) { - return &Nan::ObjectWrap::Unwrap(stored_regex->ToObject())->regex; + return &Nan::ObjectWrap::Unwrap(Nan::To(stored_regex).ToLocalChecked())->regex; } js_pattern = js_regex->GetSource(); if (js_regex->GetFlags() & RegExp::kIgnoreCase) ignore_case = true; @@ -127,7 +127,7 @@ class RegexWrapper : public Nan::ObjectWrap { Local constructor_template = Nan::New(construct); constructor_template->SetClassName(Nan::New("TextBufferRegex").ToLocalChecked()); constructor_template->InstanceTemplate()->SetInternalFieldCount(1); - constructor.Reset(constructor_template->GetFunction()); + constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } }; @@ -150,7 +150,7 @@ class SubsequenceMatchWrapper : public Nan::ObjectWrap { Nan::SetAccessor(instance_template, Nan::New("matchIndices").ToLocalChecked(), get_match_indices); Nan::SetAccessor(instance_template, Nan::New("score").ToLocalChecked(), get_score); - constructor.Reset(constructor_template->GetFunction()); + constructor.Reset(Nan::GetFunction(constructor_template).ToLocalChecked()); } static Local from_subsequence_match(SubsequenceMatch match) { @@ -227,7 +227,7 @@ void TextBufferWrapper::init(Local exports) { prototype_template->Set(Nan::New("getSnapshot").ToLocalChecked(), Nan::New(get_snapshot)); RegexWrapper::init(); SubsequenceMatchWrapper::init(); - exports->Set(Nan::New("TextBuffer").ToLocalChecked(), constructor_template->GetFunction()); + exports->Set(Nan::New("TextBuffer").ToLocalChecked(), Nan::GetFunction(constructor_template).ToLocalChecked()); } void TextBufferWrapper::construct(const Nan::FunctionCallbackInfo &info) { @@ -470,7 +470,7 @@ void TextBufferWrapper::find_and_mark_all_sync(const Nan::FunctionCallbackInfo(info[1]); if (!next_id.IsJust()) return; if (!info[2]->IsBoolean()) return; - bool exclusive = info[2]->BooleanValue(); + bool exclusive = Nan::To(info[2]).FromMaybe(false); const Regex *regex = RegexWrapper::regex_from_js(info[3]); if (regex) { @@ -628,7 +628,7 @@ void TextBufferWrapper::find_words_with_subsequence_in_range(const Nan::Function auto query = string_conversion::string_from_js(info[0]); auto extra_word_characters = string_conversion::string_from_js(info[1]); - auto max_count = number_conversion::number_from_js(info[2]); + auto max_count = number_conversion::number_from_js(info[2]); auto range = RangeWrapper::range_from_js(info[3]); auto callback = new Nan::Callback(info[4].As()); @@ -926,7 +926,8 @@ void TextBufferWrapper::load(const Nan::FunctionCallbackInfo &info) { if (!force && text_buffer.is_modified()) { Local argv[] = {Nan::Null(), Nan::Null()}; - info[0].As()->Call(Nan::Null(), 2, argv); + auto callback = info[0].As(); + Nan::Call(callback, callback->CreationContext()->Global(), 2, argv); return; } @@ -958,7 +959,7 @@ void TextBufferWrapper::load(const Nan::FunctionCallbackInfo &info) { compute_patch ); } else { - auto text_writer = Nan::ObjectWrap::Unwrap(info[4]->ToObject()); + auto text_writer = Nan::ObjectWrap::Unwrap(Nan::To(info[4]).ToLocalChecked()); worker = new LoadWorker( completion_callback, progress_callback, @@ -1026,10 +1027,11 @@ void TextBufferWrapper::base_text_matches_file(const Nan::FunctionCallbackInfo(info[1]->ToObject())->get_text(); + auto file_contents = Nan::ObjectWrap::Unwrap(Nan::To(info[1]).ToLocalChecked())->get_text(); bool result = std::equal(file_contents.begin(), file_contents.end(), text_buffer.base_text().begin()); Local argv[] = {Nan::Null(), Nan::New(result)}; - info[0].As()->Call(Nan::Null(), 2, argv); + auto callback = info[0].As(); + Nan::Call(callback, callback->CreationContext()->Global(), 2, argv); } } diff --git a/src/bindings/text-reader.cc b/src/bindings/text-reader.cc index 7ad3369c..555cfac9 100644 --- a/src/bindings/text-reader.cc +++ b/src/bindings/text-reader.cc @@ -15,7 +15,7 @@ void TextReader::init(Local exports) { prototype_template->Set(Nan::New("read").ToLocalChecked(), Nan::New(read)); prototype_template->Set(Nan::New("end").ToLocalChecked(), Nan::New(end)); prototype_template->Set(Nan::New("destroy").ToLocalChecked(), Nan::New(destroy)); - exports->Set(Nan::New("TextReader").ToLocalChecked(), constructor_template->GetFunction()); + exports->Set(Nan::New("TextReader").ToLocalChecked(), Nan::GetFunction(constructor_template).ToLocalChecked()); } TextReader::TextReader(Local js_buffer, @@ -34,13 +34,14 @@ TextReader::~TextReader() { } void TextReader::construct(const Nan::FunctionCallbackInfo &info) { - auto js_text_buffer = info[0]->ToObject(); + Local js_text_buffer; + if (!Nan::To(info[0]).ToLocal(&js_text_buffer)) return; auto &text_buffer = Nan::ObjectWrap::Unwrap(js_text_buffer)->text_buffer; auto snapshot = text_buffer.create_snapshot(); Local js_encoding_name; if (!Nan::To(info[1]).ToLocal(&js_encoding_name)) return; - String::Utf8Value encoding_name(js_encoding_name); + Nan::Utf8String encoding_name(js_encoding_name); auto conversion = transcoding_to(*encoding_name); if (!conversion) { Nan::ThrowError((string("Invalid encoding name: ") + *encoding_name).c_str()); @@ -52,7 +53,7 @@ void TextReader::construct(const Nan::FunctionCallbackInfo &info) { } void TextReader::read(const Nan::FunctionCallbackInfo &info) { - TextReader *reader = Nan::ObjectWrap::Unwrap(info.This()->ToObject()); + TextReader *reader = Nan::ObjectWrap::Unwrap(Nan::To(info.This()).ToLocalChecked()); if (!info[0]->IsUint8Array()) { Nan::ThrowError("Expected a buffer"); @@ -87,7 +88,7 @@ void TextReader::read(const Nan::FunctionCallbackInfo &info) { } void TextReader::end(const Nan::FunctionCallbackInfo &info) { - TextReader *reader = Nan::ObjectWrap::Unwrap(info.This()->ToObject()); + TextReader *reader = Nan::ObjectWrap::Unwrap(Nan::To(info.This()).ToLocalChecked()); if (reader->snapshot) { reader->snapshot->flush_preceding_changes(); delete reader->snapshot; @@ -96,9 +97,9 @@ void TextReader::end(const Nan::FunctionCallbackInfo &info) { } void TextReader::destroy(const Nan::FunctionCallbackInfo &info) { - TextReader *reader = Nan::ObjectWrap::Unwrap(info.This()->ToObject()); + TextReader *reader = Nan::ObjectWrap::Unwrap(Nan::To(info.This()).ToLocalChecked()); if (reader->snapshot) { delete reader->snapshot; reader->snapshot = nullptr; } -} \ No newline at end of file +} diff --git a/src/bindings/text-writer.cc b/src/bindings/text-writer.cc index 321a6081..9a330a84 100644 --- a/src/bindings/text-writer.cc +++ b/src/bindings/text-writer.cc @@ -12,7 +12,7 @@ void TextWriter::init(Local exports) { const auto &prototype_template = constructor_template->PrototypeTemplate(); prototype_template->Set(Nan::New("write").ToLocalChecked(), Nan::New(write)); prototype_template->Set(Nan::New("end").ToLocalChecked(), Nan::New(end)); - exports->Set(Nan::New("TextWriter").ToLocalChecked(), constructor_template->GetFunction()); + exports->Set(Nan::New("TextWriter").ToLocalChecked(), Nan::GetFunction(constructor_template).ToLocalChecked()); } TextWriter::TextWriter(EncodingConversion &&conversion) : conversion{move(conversion)} {} @@ -20,7 +20,7 @@ TextWriter::TextWriter(EncodingConversion &&conversion) : conversion{move(conver void TextWriter::construct(const Nan::FunctionCallbackInfo &info) { Local js_encoding_name; if (!Nan::To(info[0]).ToLocal(&js_encoding_name)) return; - String::Utf8Value encoding_name(js_encoding_name); + Nan::Utf8String encoding_name(js_encoding_name); auto conversion = transcoding_from(*encoding_name); if (!conversion) { Nan::ThrowError((string("Invalid encoding name: ") + *encoding_name).c_str()); @@ -34,11 +34,17 @@ void TextWriter::construct(const Nan::FunctionCallbackInfo &info) { void TextWriter::write(const Nan::FunctionCallbackInfo &info) { auto writer = Nan::ObjectWrap::Unwrap(info.This()); - if (info[0]->IsString()) { - Local js_chunk = info[0]->ToString(); + Local js_chunk; + if (Nan::To(info[0]).ToLocal(&js_chunk)) { size_t size = writer->content.size(); writer->content.resize(size + js_chunk->Length()); js_chunk->Write( + +// Nan doesn't wrap this functionality +#if NODE_MAJOR_VERSION >= 12 + Isolate::GetCurrent(), +#endif + reinterpret_cast(&writer->content[0]) + size, 0, -1,