v8-container.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2021 the V8 project authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef INCLUDE_V8_CONTAINER_H_
  5. #define INCLUDE_V8_CONTAINER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "v8-local-handle.h" // NOLINT(build/include_directory)
  9. #include "v8-object.h" // NOLINT(build/include_directory)
  10. #include "v8config.h" // NOLINT(build/include_directory)
  11. namespace v8 {
  12. class Context;
  13. class Isolate;
  14. /**
  15. * An instance of the built-in array constructor (ECMA-262, 15.4.2).
  16. */
  17. class V8_EXPORT Array : public Object {
  18. public:
  19. uint32_t Length() const;
  20. /**
  21. * Creates a JavaScript array with the given length. If the length
  22. * is negative the returned array will have length 0.
  23. */
  24. static Local<Array> New(Isolate* isolate, int length = 0);
  25. /**
  26. * Creates a JavaScript array out of a Local<Value> array in C++
  27. * with a known length.
  28. */
  29. static Local<Array> New(Isolate* isolate, Local<Value>* elements,
  30. size_t length);
  31. V8_INLINE static Array* Cast(Value* value) {
  32. #ifdef V8_ENABLE_CHECKS
  33. CheckCast(value);
  34. #endif
  35. return static_cast<Array*>(value);
  36. }
  37. private:
  38. Array();
  39. static void CheckCast(Value* obj);
  40. };
  41. /**
  42. * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
  43. */
  44. class V8_EXPORT Map : public Object {
  45. public:
  46. size_t Size() const;
  47. void Clear();
  48. V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
  49. Local<Value> key);
  50. V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
  51. Local<Value> key,
  52. Local<Value> value);
  53. V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
  54. Local<Value> key);
  55. V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
  56. Local<Value> key);
  57. /**
  58. * Returns an array of length Size() * 2, where index N is the Nth key and
  59. * index N + 1 is the Nth value.
  60. */
  61. Local<Array> AsArray() const;
  62. /**
  63. * Creates a new empty Map.
  64. */
  65. static Local<Map> New(Isolate* isolate);
  66. V8_INLINE static Map* Cast(Value* value) {
  67. #ifdef V8_ENABLE_CHECKS
  68. CheckCast(value);
  69. #endif
  70. return static_cast<Map*>(value);
  71. }
  72. private:
  73. Map();
  74. static void CheckCast(Value* obj);
  75. };
  76. /**
  77. * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
  78. */
  79. class V8_EXPORT Set : public Object {
  80. public:
  81. size_t Size() const;
  82. void Clear();
  83. V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
  84. Local<Value> key);
  85. V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
  86. Local<Value> key);
  87. V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
  88. Local<Value> key);
  89. /**
  90. * Returns an array of the keys in this Set.
  91. */
  92. Local<Array> AsArray() const;
  93. /**
  94. * Creates a new empty Set.
  95. */
  96. static Local<Set> New(Isolate* isolate);
  97. V8_INLINE static Set* Cast(Value* value) {
  98. #ifdef V8_ENABLE_CHECKS
  99. CheckCast(value);
  100. #endif
  101. return static_cast<Set*>(value);
  102. }
  103. private:
  104. Set();
  105. static void CheckCast(Value* obj);
  106. };
  107. } // namespace v8
  108. #endif // INCLUDE_V8_CONTAINER_H_