summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/bindings/v8_set_return_value.h
blob: 52269d57c410cea5b733ccc2dc9cccfd9123bde2 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_PLATFORM_BINDINGS_V8_SET_RETURN_VALUE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_SET_RETURN_VALUE_H_

#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/platform/bindings/dom_data_store.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/bindings/v8_value_cache.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "v8/include/v8.h"

namespace blink {

namespace bindings {

// V8SetReturnValue sets a return value in a V8 callback function.  The first
// two arguments are fixed as v8::{Function,Property}CallbackInfo and the
// return value.  V8SetReturnValue may take more arguments as optimization hints
// depending on the return value type.

struct V8ReturnValue {
  // Support compile-time overload resolution by making each value have its own
  // type.

  // Applies strict typing to IDL primitive types.
  template <typename T>
  struct PrimitiveType {};

  // Nullable or not
  enum NonNullable { kNonNullable };
  enum Nullable { kNullable };

  // FrozenArray or not (the integrity level = frozen or not)
  enum Frozen { kFrozen };

  // Main world or not
  enum MainWorld { kMainWorld };

  // Returns the interface object of the given type.
  enum InterfaceObject { kInterfaceObject };
};

// V8 handle types
template <typename CallbackInfo, typename S>
void V8SetReturnValue(const CallbackInfo& info, const v8::Local<S> value) {
  info.GetReturnValue().Set(value);
}

template <typename CallbackInfo, typename S>
void V8SetReturnValue(const CallbackInfo& info,
                      const v8::Local<S> value,
                      V8ReturnValue::Frozen) {
  if (value->IsObject()) {
    bool result =
        value.template As<v8::Object>()
            ->SetIntegrityLevel(info.GetIsolate()->GetCurrentContext(),
                                v8::IntegrityLevel::kFrozen)
            .ToChecked();
    CHECK(result);
  }
  info.GetReturnValue().Set(value);
}

// Property descriptor
PLATFORM_EXPORT v8::Local<v8::Object> CreatePropertyDescriptorObject(
    v8::Isolate* isolate,
    const v8::PropertyDescriptor& desc);

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const v8::PropertyDescriptor& value) {
  info.GetReturnValue().Set(
      CreatePropertyDescriptorObject(info.GetIsolate(), value));
}

// Indexed properties and named properties
PLATFORM_EXPORT inline void V8SetReturnValue(
    const v8::FunctionCallbackInfo<v8::Value>& info,
    IndexedPropertySetterResult value) {
  // If an operation implementing indexed property setter is invoked as a
  // regular operation, and the return type is not type void (V8SetReturnValue
  // won't be called in case of type void), then return the given value as is.
  info.GetReturnValue().Set(info[1]);
}

PLATFORM_EXPORT inline void V8SetReturnValue(
    const v8::PropertyCallbackInfo<v8::Value>& info,
    IndexedPropertySetterResult value) {
  if (value == IndexedPropertySetterResult::kDidNotIntercept) {
    // Do not set the return value to indicate that the request was not
    // intercepted.
    return;
  }
  info.GetReturnValue().SetNull();
}

PLATFORM_EXPORT inline void V8SetReturnValue(
    const v8::FunctionCallbackInfo<v8::Value>& info,
    NamedPropertySetterResult value) {
  // If an operation implementing named property setter is invoked as a
  // regular operation, and the return type is not type void (V8SetReturnValue
  // won't be called in case of type void), then return the given value as is.
  info.GetReturnValue().Set(info[1]);
}

PLATFORM_EXPORT inline void V8SetReturnValue(
    const v8::PropertyCallbackInfo<v8::Value>& info,
    NamedPropertySetterResult value) {
  if (value == NamedPropertySetterResult::kDidNotIntercept) {
    // Do not set the return value to indicate that the request was not
    // intercepted.
    return;
  }
  info.GetReturnValue().SetNull();
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      NamedPropertyDeleterResult value) {
  if (value == NamedPropertyDeleterResult::kDidNotIntercept) {
    // Do not set the return value to indicate that the request was not
    // intercepted.
    return;
  }
  info.GetReturnValue().Set(value == NamedPropertyDeleterResult::kDeleted);
}

// nullptr
template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, std::nullptr_t) {
  info.GetReturnValue().SetNull();
}

// Primitive types
template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, bool value) {
  info.GetReturnValue().Set(value);
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, int32_t value) {
  info.GetReturnValue().Set(value);
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, uint32_t value) {
  info.GetReturnValue().Set(value);
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, int64_t value) {
  // ECMAScript doesn't support 64-bit integer in Number type.
  info.GetReturnValue().Set(static_cast<double>(value));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, uint64_t value) {
  // ECMAScript doesn't support 64-bit integer in Number type.
  info.GetReturnValue().Set(static_cast<double>(value));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info, double value) {
  info.GetReturnValue().Set(value);
}

// Primitive types with IDL type
//
// |IdlType| represents a C++ type corresponding to an IDL type, and |value| is
// passed from Blink implementation and its type occasionally does not match
// the IDL type because Blink is not always respectful to IDL types.  These
// functions fix such a type mismatch.
template <typename CallbackInfo, typename BlinkType, typename IdlType>
typename std::enable_if_t<std::is_arithmetic<BlinkType>::value ||
                          std::is_enum<BlinkType>::value>
V8SetReturnValue(const CallbackInfo& info,
                 BlinkType value,
                 V8ReturnValue::PrimitiveType<IdlType>) {
  V8SetReturnValue(info, IdlType(value));
}

template <typename CallbackInfo, typename BlinkType>
void V8SetReturnValue(const CallbackInfo& info,
                      BlinkType* value,
                      V8ReturnValue::PrimitiveType<bool>) {
  V8SetReturnValue(info, bool(value));
}

// String types
template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const AtomicString& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::NonNullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetEmptyString();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), string.Impl());
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const String& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::NonNullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetEmptyString();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), string.Impl());
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const WebString& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::NonNullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetEmptyString();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), static_cast<String>(string).Impl());
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const AtomicString& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::Nullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetNull();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), string.Impl());
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const String& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::Nullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetNull();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), string.Impl());
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const WebString& string,
                      v8::Isolate* isolate,
                      V8ReturnValue::Nullable) {
  if (string.IsNull())
    return info.GetReturnValue().SetNull();
  V8PerIsolateData::From(isolate)->GetStringCache()->SetReturnValueFromString(
      info.GetReturnValue(), static_cast<String>(string).Impl());
}

// ScriptWrappable
template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const ScriptWrappable* value,
                      V8ReturnValue::MainWorld) {
  DCHECK(DOMWrapperWorld::Current(info.GetIsolate()).IsMainWorld());
  if (UNLIKELY(!value))
    return info.GetReturnValue().SetNull();

  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(value);
  if (DOMDataStore::SetReturnValueForMainWorld(info.GetReturnValue(),
                                               wrappable))
    return;

  info.GetReturnValue().Set(wrappable->Wrap(info.GetIsolate(), info.This()));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const ScriptWrappable& value,
                      V8ReturnValue::MainWorld) {
  DCHECK(DOMWrapperWorld::Current(info.GetIsolate()).IsMainWorld());
  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(&value);
  if (DOMDataStore::SetReturnValueForMainWorld(info.GetReturnValue(),
                                               wrappable))
    return;

  info.GetReturnValue().Set(wrappable->Wrap(info.GetIsolate(), info.This()));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const ScriptWrappable* value,
                      const ScriptWrappable* receiver) {
  if (UNLIKELY(!value))
    return info.GetReturnValue().SetNull();

  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(value);
  if (DOMDataStore::SetReturnValueFast(info.GetReturnValue(), wrappable,
                                       info.This(), receiver)) {
    return;
  }

  info.GetReturnValue().Set(wrappable->Wrap(info.GetIsolate(), info.This()));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const ScriptWrappable& value,
                      const ScriptWrappable* receiver) {
  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(&value);
  if (DOMDataStore::SetReturnValueFast(info.GetReturnValue(), wrappable,
                                       info.This(), receiver)) {
    return;
  }

  info.GetReturnValue().Set(wrappable->Wrap(info.GetIsolate(), info.This()));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const ScriptWrappable* value,
                      v8::Local<v8::Context> creation_context) {
  if (UNLIKELY(!value))
    return info.GetReturnValue().SetNull();

  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(value);
  if (DOMDataStore::SetReturnValue(info.GetReturnValue(), wrappable))
    return;

  info.GetReturnValue().Set(
      wrappable->Wrap(info.GetIsolate(), creation_context->Global()));
}

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      ScriptWrappable& value,
                      v8::Local<v8::Context> creation_context) {
  ScriptWrappable* wrappable = const_cast<ScriptWrappable*>(&value);
  if (DOMDataStore::SetReturnValue(info.GetReturnValue(), wrappable))
    return;

  info.GetReturnValue().Set(
      wrappable->Wrap(info.GetIsolate(), creation_context->Global()));
}

// Interface object
PLATFORM_EXPORT v8::Local<v8::Value> GetInterfaceObjectExposedOnGlobal(
    v8::Isolate* isolate,
    v8::Local<v8::Object> creation_context,
    const WrapperTypeInfo* wrapper_type_info);

template <typename CallbackInfo>
void V8SetReturnValue(const CallbackInfo& info,
                      const WrapperTypeInfo* wrapper_type_info,
                      V8ReturnValue::InterfaceObject) {
  info.GetReturnValue().Set(GetInterfaceObjectExposedOnGlobal(
      info.GetIsolate(), info.This(), wrapper_type_info));
}

// Nullable types
template <typename CallbackInfo, typename T, typename... ExtraArgs>
void V8SetReturnValue(const CallbackInfo& info,
                      base::Optional<T> value,
                      ExtraArgs... extra_args) {
  if (value.has_value()) {
    V8SetReturnValue(info, value.value(),
                     std::forward<ExtraArgs>(extra_args)...);
  } else {
    info.GetReturnValue().SetNull();
  }
}

}  // namespace bindings

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_SET_RETURN_VALUE_H_