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