All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.nan_private.h Maven / Gradle / Ivy

Go to download

Native Abstractions for Node.js: C++ header for Node 0.8 -> 18 compatibility

There is a newer version: 2.19.0
Show newest version
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2018 NAN contributors
 *
 * MIT License 
 ********************************************************************/

#ifndef NAN_PRIVATE_H_
#define NAN_PRIVATE_H_

inline Maybe
HasPrivate(v8::Local object, v8::Local key) {
  HandleScope scope;
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::Local context = isolate->GetCurrentContext();
  v8::Local private_key = v8::Private::ForApi(isolate, key);
  return object->HasPrivate(context, private_key);
#else
  return Just(!object->GetHiddenValue(key).IsEmpty());
#endif
}

inline MaybeLocal
GetPrivate(v8::Local object, v8::Local key) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  v8::Local context = isolate->GetCurrentContext();
  v8::Local private_key = v8::Private::ForApi(isolate, key);
  v8::MaybeLocal v = object->GetPrivate(context, private_key);
  return scope.Escape(v.ToLocalChecked());
#else
  EscapableHandleScope scope;
  v8::Local v = object->GetHiddenValue(key);
  if (v.IsEmpty()) {
    v = Undefined();
  }
  return scope.Escape(v);
#endif
}

inline Maybe SetPrivate(
    v8::Local object,
    v8::Local key,
    v8::Local value) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  HandleScope scope;
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::Local context = isolate->GetCurrentContext();
  v8::Local private_key = v8::Private::ForApi(isolate, key);
  return object->SetPrivate(context, private_key, value);
#else
  return Just(object->SetHiddenValue(key, value));
#endif
}

inline Maybe DeletePrivate(
    v8::Local object,
    v8::Local key) {
#if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  HandleScope scope;
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::Local private_key = v8::Private::ForApi(isolate, key);
  return object->DeletePrivate(isolate->GetCurrentContext(), private_key);
#else
  return Just(object->DeleteHiddenValue(key));
#endif
}

#endif  // NAN_PRIVATE_H_





© 2015 - 2024 Weber Informatics LLC | Privacy Policy