nan_private.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2018 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_PRIVATE_H_
  9. #define NAN_PRIVATE_H_
  10. inline Maybe<bool>
  11. HasPrivate(v8::Local<v8::Object> object, v8::Local<v8::String> key) {
  12. HandleScope scope;
  13. #if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  14. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  15. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  16. v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
  17. return object->HasPrivate(context, private_key);
  18. #else
  19. return Just(!object->GetHiddenValue(key).IsEmpty());
  20. #endif
  21. }
  22. inline MaybeLocal<v8::Value>
  23. GetPrivate(v8::Local<v8::Object> object, v8::Local<v8::String> key) {
  24. #if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  25. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  26. v8::EscapableHandleScope scope(isolate);
  27. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  28. v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
  29. v8::MaybeLocal<v8::Value> v = object->GetPrivate(context, private_key);
  30. return scope.Escape(v.ToLocalChecked());
  31. #else
  32. EscapableHandleScope scope;
  33. v8::Local<v8::Value> v = object->GetHiddenValue(key);
  34. if (v.IsEmpty()) {
  35. v = Undefined();
  36. }
  37. return scope.Escape(v);
  38. #endif
  39. }
  40. inline Maybe<bool> SetPrivate(
  41. v8::Local<v8::Object> object,
  42. v8::Local<v8::String> key,
  43. v8::Local<v8::Value> value) {
  44. #if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  45. HandleScope scope;
  46. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  47. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  48. v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
  49. return object->SetPrivate(context, private_key, value);
  50. #else
  51. return Just(object->SetHiddenValue(key, value));
  52. #endif
  53. }
  54. inline Maybe<bool> DeletePrivate(
  55. v8::Local<v8::Object> object,
  56. v8::Local<v8::String> key) {
  57. #if NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION
  58. HandleScope scope;
  59. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  60. v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);
  61. return object->DeletePrivate(isolate->GetCurrentContext(), private_key);
  62. #else
  63. return Just(object->DeleteHiddenValue(key));
  64. #endif
  65. }
  66. #endif // NAN_PRIVATE_H_