File: iterable.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (358 lines) | stat: -rw-r--r-- 13,084 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_ITERABLE_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_ITERABLE_H_

#include <concepts>

#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_for_each_iterator_callback.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/sync_iterator_base.h"

namespace blink {

namespace bindings {

class EnumerationBase;

namespace {

// Helper class to construct a type T without an argument. Note that IDL
// enumeration types are not default-constructible on purpose.
template <typename T>
class IDLTypeDefaultConstructible {
  STACK_ALLOCATED();

 public:
  T content;
};

template <typename T>
  requires(std::derived_from<T, EnumerationBase>)
class IDLTypeDefaultConstructible<T> {
  STACK_ALLOCATED();

 public:
  T content{static_cast<typename T::Enum>(0)};
};

}  // namespace

// https://tc39.es/ecma262/#sec-createiterresultobject
CORE_EXPORT v8::Local<v8::Object> ESCreateIterResultObject(
    ScriptState* script_state,
    bool done,
    v8::Local<v8::Value> value);
CORE_EXPORT v8::Local<v8::Object> ESCreateIterResultObject(
    ScriptState* script_state,
    bool done,
    v8::Local<v8::Value> item1,
    v8::Local<v8::Value> item2);

template <typename IDLKeyType,
          typename IDLValueType,
          typename KeyType,
          typename ValueType>
class PairSyncIterationSource : public SyncIteratorBase::IterationSourceBase {
 public:
  v8::Local<v8::Object> Next(ScriptState* script_state,
                             SyncIteratorBase::Kind kind,
                             ExceptionState& exception_state) override {
    IDLTypeDefaultConstructible<KeyType> key;
    IDLTypeDefaultConstructible<ValueType> value;
    if (!FetchNextItem(script_state, key.content, value.content,
                       exception_state)) {
      if (exception_state.HadException())
        return {};
      return ESCreateIterResultObject(
          script_state, true, v8::Undefined(script_state->GetIsolate()));
    }

    switch (kind) {
      case SyncIteratorBase::Kind::kKey: {
        v8::Local<v8::Value> v8_key =
            ToV8Traits<IDLKeyType>::ToV8(script_state, key.content);
        return ESCreateIterResultObject(script_state, false, v8_key);
      }
      case SyncIteratorBase::Kind::kValue: {
        v8::Local<v8::Value> v8_value =
            ToV8Traits<IDLValueType>::ToV8(script_state, value.content);
        return ESCreateIterResultObject(script_state, false, v8_value);
      }
      case SyncIteratorBase::Kind::kKeyValue: {
        v8::Local<v8::Value> v8_key =
            ToV8Traits<IDLKeyType>::ToV8(script_state, key.content);
        v8::Local<v8::Value> v8_value =
            ToV8Traits<IDLValueType>::ToV8(script_state, value.content);
        return ESCreateIterResultObject(script_state, false, v8_key, v8_value);
      }
    }

    NOTREACHED();
  }

  void ForEach(ScriptState* script_state,
               const ScriptValue& this_value,
               V8ForEachIteratorCallback* callback,
               const ScriptValue& this_arg,
               ExceptionState& exception_state) {
    TryRethrowScope rethrow_scope(script_state->GetIsolate(), exception_state);

    v8::Local<v8::Value> v8_callback_this_value = this_arg.V8Value();
    IDLTypeDefaultConstructible<KeyType> key;
    IDLTypeDefaultConstructible<ValueType> value;
    v8::Local<v8::Value> v8_key;
    v8::Local<v8::Value> v8_value;

    while (true) {
      if (!FetchNextItem(script_state, key.content, value.content,
                         exception_state))
        return;

      v8_key = ToV8Traits<IDLKeyType>::ToV8(script_state, key.content);
      v8_value = ToV8Traits<IDLValueType>::ToV8(script_state, value.content);

      if (callback
              ->Invoke(v8_callback_this_value,
                       ScriptValue(script_state->GetIsolate(), v8_value),
                       ScriptValue(script_state->GetIsolate(), v8_key),
                       this_value)
              .IsNothing()) {
        return;
      }
    }
  }

 private:
  virtual bool FetchNextItem(ScriptState* script_state,
                             KeyType& key,
                             ValueType& value,
                             ExceptionState& exception_state) = 0;
};

template <typename IDLValueType, typename ValueType>
class ValueSyncIterationSource : public SyncIteratorBase::IterationSourceBase {
 public:
  v8::Local<v8::Object> Next(ScriptState* script_state,
                             SyncIteratorBase::Kind kind,
                             ExceptionState& exception_state) override {
    IDLTypeDefaultConstructible<ValueType> value;
    if (!FetchNextItem(script_state, value.content, exception_state)) {
      if (exception_state.HadException())
        return {};
      return ESCreateIterResultObject(
          script_state, true, v8::Undefined(script_state->GetIsolate()));
    }

    v8::Local<v8::Value> v8_value =
        ToV8Traits<IDLValueType>::ToV8(script_state, value.content);

    switch (kind) {
      case SyncIteratorBase::Kind::kKey:
      case SyncIteratorBase::Kind::kValue:
        return ESCreateIterResultObject(script_state, false, v8_value);
      case SyncIteratorBase::Kind::kKeyValue:
        return ESCreateIterResultObject(script_state, false, v8_value,
                                        v8_value);
    }

    NOTREACHED();
  }

  void ForEach(ScriptState* script_state,
               const ScriptValue& this_value,
               V8ForEachIteratorCallback* callback,
               const ScriptValue& this_arg,
               ExceptionState& exception_state) {
    TryRethrowScope rethrow_scope(script_state->GetIsolate(), exception_state);

    v8::Local<v8::Value> v8_callback_this_value = this_arg.V8Value();
    IDLTypeDefaultConstructible<ValueType> value;
    v8::Local<v8::Value> v8_value;

    while (true) {
      if (!FetchNextItem(script_state, value.content, exception_state))
        return;

      v8_value = ToV8Traits<IDLValueType>::ToV8(script_state, value.content);
      ScriptValue script_value(script_state->GetIsolate(), v8_value);

      if (callback
              ->Invoke(v8_callback_this_value, script_value, script_value,
                       this_value)
              .IsNothing()) {
        return;
      }
    }
  }

 private:
  virtual bool FetchNextItem(ScriptState* script_state,
                             ValueType& value,
                             ExceptionState& exception_state) = 0;
};

}  // namespace bindings

template <typename IDLInterface>
class PairSyncIterable {
 public:
  using SyncIteratorType = SyncIterator<IDLInterface>;
  // Check whether the v8_sync_iterator_foo_bar.h is appropriately included.
  // Make sizeof require the complete definition of the class.
  static_assert(
      sizeof(SyncIteratorType),  // Read the following for a compile error.
      "You need to include a generated header for SyncIterator<IDLInterface> "
      "in order to inherit from PairSyncIterable. "
      "For an IDL interface FooBar, #include "
      "\"third_party/blink/renderer/bindings/<component>/v8/"
      "v8_sync_iterator_foo_bar.h\" is required.");

  using IDLKeyType = typename SyncIteratorType::IDLKeyType;
  using IDLValueType = typename SyncIteratorType::IDLValueType;
  using KeyType = typename SyncIteratorType::KeyType;
  using ValueType = typename SyncIteratorType::ValueType;
  using IterationSource = bindings::
      PairSyncIterationSource<IDLKeyType, IDLValueType, KeyType, ValueType>;

  PairSyncIterable() = default;
  ~PairSyncIterable() = default;
  PairSyncIterable(const PairSyncIterable&) = delete;
  PairSyncIterable& operator=(const PairSyncIterable&) = delete;

  SyncIteratorType* keysForBinding(ScriptState* script_state,
                                   ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(source,
                                                  SyncIteratorType::Kind::kKey);
  }

  SyncIteratorType* valuesForBinding(ScriptState* script_state,
                                     ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(
        source, SyncIteratorType::Kind::kValue);
  }

  SyncIteratorType* entriesForBinding(ScriptState* script_state,
                                      ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(
        source, SyncIteratorType::Kind::kKeyValue);
  }

  void forEachForBinding(ScriptState* script_state,
                         const ScriptValue& this_value,
                         V8ForEachIteratorCallback* callback,
                         const ScriptValue& this_arg,
                         ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return;
    source->ForEach(script_state, this_value, callback, this_arg,
                    exception_state);
  }

 private:
  virtual IterationSource* CreateIterationSource(
      ScriptState* script_state,
      ExceptionState& exception_state) = 0;
};

template <typename IDLInterface>
class ValueSyncIterable {
 public:
  using SyncIteratorType = SyncIterator<IDLInterface>;
  // Check whether the v8_sync_iterator_foo_bar.h is appropriately included.
  // Make sizeof require the complete definition of the class.
  static_assert(
      sizeof(SyncIteratorType),  // Read the following for a compile error.
      "You need to include a generated header for SyncIterator<IDLInterface> "
      "in order to inherit from ValueSyncIterable. "
      "For an IDL interface FooBar, #include "
      "\"third_party/blink/renderer/bindings/<component>/v8/"
      "v8_sync_iterator_foo_bar.h\" is required.");

  using IDLValueType = typename SyncIteratorType::IDLValueType;
  using ValueType = typename SyncIteratorType::ValueType;
  using IterationSource =
      bindings::ValueSyncIterationSource<IDLValueType, ValueType>;

  ValueSyncIterable() = default;
  ~ValueSyncIterable() = default;
  ValueSyncIterable(const ValueSyncIterable&) = delete;
  ValueSyncIterable& operator=(const ValueSyncIterable&) = delete;

  SyncIteratorType* keysForBinding(ScriptState* script_state,
                                   ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(source,
                                                  SyncIteratorType::Kind::kKey);
  }

  SyncIteratorType* valuesForBinding(ScriptState* script_state,
                                     ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(
        source, SyncIteratorType::Kind::kValue);
  }

  SyncIteratorType* entriesForBinding(ScriptState* script_state,
                                      ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return nullptr;
    return MakeGarbageCollected<SyncIteratorType>(
        source, SyncIteratorType::Kind::kKeyValue);
  }

  void forEachForBinding(ScriptState* script_state,
                         const ScriptValue& this_value,
                         V8ForEachIteratorCallback* callback,
                         const ScriptValue& this_arg,
                         ExceptionState& exception_state) {
    IterationSource* source =
        CreateIterationSource(script_state, exception_state);
    if (!source)
      return;
    source->ForEach(script_state, this_value, callback, this_arg,
                    exception_state);
  }

 private:
  virtual IterationSource* CreateIterationSource(
      ScriptState* script_state,
      ExceptionState& exception_state) = 0;
};

// Unpacks `sync_iteration_result`, stores 'value' and 'done' properties in
// `out_value` and 'out_done` respectively, and returns true on success.
[[nodiscard]] CORE_EXPORT bool V8UnpackIterationResult(
    ScriptState* script_state,
    v8::Local<v8::Object> sync_iteration_result,
    v8::Local<v8::Value>* out_value,
    bool* out_done);

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_ITERABLE_H_