<commit_before>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by MIT license that can be found in the
// LICENSE file.

#ifndef NATIVE_MATE_SCOPED_PERSISTENT_H_
#define NATIVE_MATE_SCOPED_PERSISTENT_H_

#include "base/memory/ref_counted.h"
#include "v8/include/v8.h"

namespace mate {

// A v8::Persistent handle to a V8 value which destroys and clears the
// underlying handle on destruction.
template <typename T>
class ScopedPersistent {
 public:
  ScopedPersistent() {
  }

  explicit ScopedPersistent(v8::Handle<v8::Value> handle) {
    reset(v8::Handle<T>::Cast(handle));
  }

  ~ScopedPersistent() {
    reset();
  }

  void reset(v8::Handle<T> handle) {
    if (!handle.IsEmpty())
      handle_.Reset(GetIsolate(handle), handle);
    else
      reset();
  }

  void reset() {
    handle_.Reset();
  }

  bool IsEmpty() const {
    return handle_.IsEmpty();
  }

  v8::Handle<T> NewHandle() const {
    if (handle_.IsEmpty())
      return v8::Local<T>();
    return v8::Local<T>::New(GetIsolate(handle_), handle_);
  }

  v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
    if (handle_.IsEmpty())
      return v8::Local<T>();
    return v8::Local<T>::New(isolate, handle_);
  }

 private:
  template <typename U>
  static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) {
    // Only works for v8::Object and its subclasses. Add specialisations for
    // anything else.
    if (!object_handle.IsEmpty())
      return GetIsolate(object_handle->CreationContext());
    return v8::Isolate::GetCurrent();
  }
  static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) {
    if (!context_handle.IsEmpty())
      return context_handle->GetIsolate();
    return v8::Isolate::GetCurrent();
  }
  static v8::Isolate* GetIsolate(
      v8::Handle<v8::ObjectTemplate> template_handle) {
    return v8::Isolate::GetCurrent();
  }
  template <typename U>
  static v8::Isolate* GetIsolate(const U& any_handle) {
    return v8::Isolate::GetCurrent();
  }
<commit_msg>Bring back ScopedPersistent.SetWeak.<commit_after>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by MIT license that can be found in the
// LICENSE file.

#ifndef NATIVE_MATE_SCOPED_PERSISTENT_H_
#define NATIVE_MATE_SCOPED_PERSISTENT_H_

#include "base/memory/ref_counted.h"
#include "v8/include/v8.h"

namespace mate {

// A v8::Persistent handle to a V8 value which destroys and clears the
// underlying handle on destruction.
template <typename T>
class ScopedPersistent {
 public:
  ScopedPersistent() {
  }

  explicit ScopedPersistent(v8::Handle<v8::Value> handle) {
    reset(v8::Handle<T>::Cast(handle));
  }

  ~ScopedPersistent() {
    reset();
  }

  void reset(v8::Handle<T> handle) {
    if (!handle.IsEmpty())
      handle_.Reset(GetIsolate(handle), handle);
    else
      reset();
  }

  void reset() {
    handle_.Reset();
  }

  bool IsEmpty() const {
    return handle_.IsEmpty();
  }

  v8::Handle<T> NewHandle() const {
    if (handle_.IsEmpty())
      return v8::Local<T>();
    return v8::Local<T>::New(GetIsolate(handle_), handle_);
  }

  v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
    if (handle_.IsEmpty())
      return v8::Local<T>();
    return v8::Local<T>::New(isolate, handle_);
  }

  template<typename P>
  void SetWeak(P* parameter,
               typename v8::WeakCallbackData<T, P>::Callback callback) {
    handle_.SetWeak(parameter, callback);
  }

 private:
  template <typename U>
  static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) {
    // Only works for v8::Object and its subclasses. Add specialisations for
    // anything else.
    if (!object_handle.IsEmpty())
      return GetIsolate(object_handle->CreationContext());
    return v8::Isolate::GetCurrent();
  }
  static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) {
    if (!context_handle.IsEmpty())
      return context_handle->GetIsolate();
    return v8::Isolate::GetCurrent();
  }
  static v8::Isolate* GetIsolate(
      v8::Handle<v8::ObjectTemplate> template_handle) {
    return v8::Isolate::GetCurrent();
  }
  template <typename U>
  static v8::Isolate* GetIsolate(const U& any_handle) {
    return v8::Isolate::GetCurrent();
  }

{'fraction_non_alphanumeric': 0.14093801555503183, 'fraction_numerical': 0.015790714117369785, 'mean_word_length': 3.9870740305522916, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3605078', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1423, 'n_words': 323}