Coverage Report

Created: 2025-07-04 09:33

/src/node/deps/v8/include/v8-template.h
Line
Count
Source (jump to first uncovered line)
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_TEMPLATE_H_
6
#define INCLUDE_V8_TEMPLATE_H_
7
8
#include "v8-data.h"               // NOLINT(build/include_directory)
9
#include "v8-function-callback.h"  // NOLINT(build/include_directory)
10
#include "v8-local-handle.h"       // NOLINT(build/include_directory)
11
#include "v8-memory-span.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 CFunction;
18
class FunctionTemplate;
19
class ObjectTemplate;
20
class Signature;
21
22
// --- Templates ---
23
24
#define V8_INTRINSICS_LIST(F)                                 \
25
  F(ArrayProto_entries, array_entries_iterator)               \
26
  F(ArrayProto_forEach, array_for_each_iterator)              \
27
  F(ArrayProto_keys, array_keys_iterator)                     \
28
  F(ArrayProto_values, array_values_iterator)                 \
29
  F(ArrayPrototype, initial_array_prototype)                  \
30
  F(AsyncIteratorPrototype, initial_async_iterator_prototype) \
31
  F(ErrorPrototype, initial_error_prototype)                  \
32
  F(IteratorPrototype, initial_iterator_prototype)            \
33
  F(MapIteratorPrototype, initial_map_iterator_prototype)     \
34
  F(ObjProto_valueOf, object_value_of_function)               \
35
  F(SetIteratorPrototype, initial_set_iterator_prototype)
36
37
enum Intrinsic {
38
#define V8_DECL_INTRINSIC(name, iname) k##name,
39
  V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
40
#undef V8_DECL_INTRINSIC
41
};
42
43
/**
44
 * The superclass of object and function templates.
45
 */
46
class V8_EXPORT Template : public Data {
47
 public:
48
  /**
49
   * Adds a property to each instance created by this template.
50
   *
51
   * The property must be defined either as a primitive value, or a template.
52
   */
53
  void Set(Local<Name> name, Local<Data> value,
54
           PropertyAttribute attributes = None);
55
  void SetPrivate(Local<Private> name, Local<Data> value,
56
                  PropertyAttribute attributes = None);
57
  V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value,
58
                     PropertyAttribute attributes = None);
59
60
  void SetAccessorProperty(
61
      Local<Name> name,
62
      Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
63
      Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
64
      PropertyAttribute attribute = None);
65
66
  /**
67
   * Whenever the property with the given name is accessed on objects
68
   * created from this Template the getter and setter callbacks
69
   * are called instead of getting and setting the property directly
70
   * on the JavaScript object.
71
   *
72
   * \param name The name of the property for which an accessor is added.
73
   * \param getter The callback to invoke when getting the property.
74
   * \param setter The callback to invoke when setting the property.
75
   * \param data A piece of data that will be passed to the getter and setter
76
   *   callbacks whenever they are invoked.
77
   * \param attribute The attributes of the property for which an accessor
78
   *   is added.
79
   */
80
  V8_DEPRECATE_SOON("Use SetNativeDataProperty without AccessControl instead")
81
  void SetNativeDataProperty(
82
      Local<String> name, AccessorGetterCallback getter,
83
      AccessorSetterCallback setter, Local<Value> data,
84
      PropertyAttribute attribute, AccessControl settings,
85
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
86
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
87
  V8_DEPRECATE_SOON("Use SetNativeDataProperty without AccessControl instead")
88
  void SetNativeDataProperty(
89
      Local<Name> name, AccessorNameGetterCallback getter,
90
      AccessorNameSetterCallback setter, Local<Value> data,
91
      PropertyAttribute attribute, AccessControl settings,
92
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
93
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
94
  void SetNativeDataProperty(
95
      Local<String> name, AccessorGetterCallback getter,
96
      AccessorSetterCallback setter = nullptr,
97
      Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
98
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
99
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
100
  void SetNativeDataProperty(
101
      Local<Name> name, AccessorNameGetterCallback getter,
102
      AccessorNameSetterCallback setter = nullptr,
103
      Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
104
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
105
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
106
107
  /**
108
   * Like SetNativeDataProperty, but V8 will replace the native data property
109
   * with a real data property on first access.
110
   */
111
  void SetLazyDataProperty(
112
      Local<Name> name, AccessorNameGetterCallback getter,
113
      Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
114
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
115
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
116
117
  /**
118
   * During template instantiation, sets the value with the intrinsic property
119
   * from the correct context.
120
   */
121
  void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
122
                                PropertyAttribute attribute = None);
123
124
 private:
125
  Template();
126
127
  friend class ObjectTemplate;
128
  friend class FunctionTemplate;
129
};
130
131
// TODO(dcarney): Replace GenericNamedPropertyFooCallback with just
132
// NamedPropertyFooCallback.
133
134
/**
135
 * Interceptor for get requests on an object.
136
 *
137
 * Use `info.GetReturnValue().Set()` to set the return value of the
138
 * intercepted get request. If the property does not exist the callback should
139
 * not set the result and must not produce side effects.
140
 *
141
 * \param property The name of the property for which the request was
142
 * intercepted.
143
 * \param info Information about the intercepted request, such as
144
 * isolate, receiver, return value, or whether running in `'use strict`' mode.
145
 * See `PropertyCallbackInfo`.
146
 *
147
 * \code
148
 *  void GetterCallback(
149
 *    Local<Name> name,
150
 *    const v8::PropertyCallbackInfo<v8::Value>& info) {
151
 *      info.GetReturnValue().Set(v8_num(42));
152
 *  }
153
 *
154
 *  v8::Local<v8::FunctionTemplate> templ =
155
 *      v8::FunctionTemplate::New(isolate);
156
 *  templ->InstanceTemplate()->SetHandler(
157
 *      v8::NamedPropertyHandlerConfiguration(GetterCallback));
158
 *  LocalContext env;
159
 *  env->Global()
160
 *      ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local())
161
 *                                             .ToLocalChecked()
162
 *                                             ->NewInstance(env.local())
163
 *                                             .ToLocalChecked())
164
 *      .FromJust();
165
 *  v8::Local<v8::Value> result = CompileRun("obj.a = 17; obj.a");
166
 *  CHECK(v8_num(42)->Equals(env.local(), result).FromJust());
167
 * \endcode
168
 *
169
 * See also `ObjectTemplate::SetHandler`.
170
 */
171
using GenericNamedPropertyGetterCallback =
172
    void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
173
174
/**
175
 * Interceptor for set requests on an object.
176
 *
177
 * Use `info.GetReturnValue()` to indicate whether the request was intercepted
178
 * or not. If the setter successfully intercepts the request, i.e., if the
179
 * request should not be further executed, call
180
 * `info.GetReturnValue().Set(value)`. If the setter did not intercept the
181
 * request, i.e., if the request should be handled as if no interceptor is
182
 * present, do not not call `Set()` and do not produce side effects.
183
 *
184
 * \param property The name of the property for which the request was
185
 * intercepted.
186
 * \param value The value which the property will have if the request
187
 * is not intercepted.
188
 * \param info Information about the intercepted request, such as
189
 * isolate, receiver, return value, or whether running in `'use strict'` mode.
190
 * See `PropertyCallbackInfo`.
191
 *
192
 * See also
193
 * `ObjectTemplate::SetHandler.`
194
 */
195
using GenericNamedPropertySetterCallback =
196
    void (*)(Local<Name> property, Local<Value> value,
197
             const PropertyCallbackInfo<Value>& info);
198
199
/**
200
 * Intercepts all requests that query the attributes of the
201
 * property, e.g., getOwnPropertyDescriptor(), propertyIsEnumerable(), and
202
 * defineProperty().
203
 *
204
 * Use `info.GetReturnValue().Set(value)` to set the property attributes. The
205
 * value is an integer encoding a `v8::PropertyAttribute`. If the property does
206
 * not exist the callback should not set the result and must not produce side
207
 * effects.
208
 *
209
 * \param property The name of the property for which the request was
210
 * intercepted.
211
 * \param info Information about the intercepted request, such as
212
 * isolate, receiver, return value, or whether running in `'use strict'` mode.
213
 * See `PropertyCallbackInfo`.
214
 *
215
 * \note Some functions query the property attributes internally, even though
216
 * they do not return the attributes. For example, `hasOwnProperty()` can
217
 * trigger this interceptor depending on the state of the object.
218
 *
219
 * See also
220
 * `ObjectTemplate::SetHandler.`
221
 */
222
using GenericNamedPropertyQueryCallback =
223
    void (*)(Local<Name> property, const PropertyCallbackInfo<Integer>& info);
224
225
/**
226
 * Interceptor for delete requests on an object.
227
 *
228
 * Use `info.GetReturnValue()` to indicate whether the request was intercepted
229
 * or not. If the deleter successfully intercepts the request, i.e., if the
230
 * request should not be further executed, call
231
 * `info.GetReturnValue().Set(value)` with a boolean `value`. The `value` is
232
 * used as the return value of `delete`. If the deleter does not intercept the
233
 * request then it should not set the result and must not produce side effects.
234
 *
235
 * \param property The name of the property for which the request was
236
 * intercepted.
237
 * \param info Information about the intercepted request, such as
238
 * isolate, receiver, return value, or whether running in `'use strict'` mode.
239
 * See `PropertyCallbackInfo`.
240
 *
241
 * \note If you need to mimic the behavior of `delete`, i.e., throw in strict
242
 * mode instead of returning false, use `info.ShouldThrowOnError()` to determine
243
 * if you are in strict mode.
244
 *
245
 * See also `ObjectTemplate::SetHandler.`
246
 */
247
using GenericNamedPropertyDeleterCallback =
248
    void (*)(Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
249
250
/**
251
 * Returns an array containing the names of the properties the named
252
 * property getter intercepts.
253
 *
254
 * Note: The values in the array must be of type v8::Name.
255
 */
256
using GenericNamedPropertyEnumeratorCallback =
257
    void (*)(const PropertyCallbackInfo<Array>& info);
258
259
/**
260
 * Interceptor for defineProperty requests on an object.
261
 *
262
 * Use `info.GetReturnValue()` to indicate whether the request was intercepted
263
 * or not. If the definer successfully intercepts the request, i.e., if the
264
 * request should not be further executed, call
265
 * `info.GetReturnValue().Set(value)`. If the definer did not intercept the
266
 * request, i.e., if the request should be handled as if no interceptor is
267
 * present, do not not call `Set()` and do not produce side effects.
268
 *
269
 * \param property The name of the property for which the request was
270
 * intercepted.
271
 * \param desc The property descriptor which is used to define the
272
 * property if the request is not intercepted.
273
 * \param info Information about the intercepted request, such as
274
 * isolate, receiver, return value, or whether running in `'use strict'` mode.
275
 * See `PropertyCallbackInfo`.
276
 *
277
 * See also `ObjectTemplate::SetHandler`.
278
 */
279
using GenericNamedPropertyDefinerCallback =
280
    void (*)(Local<Name> property, const PropertyDescriptor& desc,
281
             const PropertyCallbackInfo<Value>& info);
282
283
/**
284
 * Interceptor for getOwnPropertyDescriptor requests on an object.
285
 *
286
 * Use `info.GetReturnValue().Set()` to set the return value of the
287
 * intercepted request. The return value must be an object that
288
 * can be converted to a PropertyDescriptor, e.g., a `v8::value` returned from
289
 * `v8::Object::getOwnPropertyDescriptor`.
290
 *
291
 * \param property The name of the property for which the request was
292
 * intercepted.
293
 * \info Information about the intercepted request, such as
294
 * isolate, receiver, return value, or whether running in `'use strict'` mode.
295
 * See `PropertyCallbackInfo`.
296
 *
297
 * \note If GetOwnPropertyDescriptor is intercepted, it will
298
 * always return true, i.e., indicate that the property was found.
299
 *
300
 * See also `ObjectTemplate::SetHandler`.
301
 */
302
using GenericNamedPropertyDescriptorCallback =
303
    void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
304
305
/**
306
 * See `v8::GenericNamedPropertyGetterCallback`.
307
 */
308
using IndexedPropertyGetterCallback =
309
    void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
310
311
/**
312
 * See `v8::GenericNamedPropertySetterCallback`.
313
 */
314
using IndexedPropertySetterCallback =
315
    void (*)(uint32_t index, Local<Value> value,
316
             const PropertyCallbackInfo<Value>& info);
317
318
/**
319
 * See `v8::GenericNamedPropertyQueryCallback`.
320
 */
321
using IndexedPropertyQueryCallback =
322
    void (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
323
324
/**
325
 * See `v8::GenericNamedPropertyDeleterCallback`.
326
 */
327
using IndexedPropertyDeleterCallback =
328
    void (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
329
330
/**
331
 * Returns an array containing the indices of the properties the indexed
332
 * property getter intercepts.
333
 *
334
 * Note: The values in the array must be uint32_t.
335
 */
336
using IndexedPropertyEnumeratorCallback =
337
    void (*)(const PropertyCallbackInfo<Array>& info);
338
339
/**
340
 * See `v8::GenericNamedPropertyDefinerCallback`.
341
 */
342
using IndexedPropertyDefinerCallback =
343
    void (*)(uint32_t index, const PropertyDescriptor& desc,
344
             const PropertyCallbackInfo<Value>& info);
345
346
/**
347
 * See `v8::GenericNamedPropertyDescriptorCallback`.
348
 */
349
using IndexedPropertyDescriptorCallback =
350
    void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
351
352
/**
353
 * Returns true if the given context should be allowed to access the given
354
 * object.
355
 */
356
using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
357
                                     Local<Object> accessed_object,
358
                                     Local<Value> data);
359
360
enum class ConstructorBehavior { kThrow, kAllow };
361
362
/**
363
 * A FunctionTemplate is used to create functions at runtime. There
364
 * can only be one function created from a FunctionTemplate in a
365
 * context.  The lifetime of the created function is equal to the
366
 * lifetime of the context.  So in case the embedder needs to create
367
 * temporary functions that can be collected using Scripts is
368
 * preferred.
369
 *
370
 * Any modification of a FunctionTemplate after first instantiation will trigger
371
 * a crash.
372
 *
373
 * A FunctionTemplate can have properties, these properties are added to the
374
 * function object when it is created.
375
 *
376
 * A FunctionTemplate has a corresponding instance template which is
377
 * used to create object instances when the function is used as a
378
 * constructor. Properties added to the instance template are added to
379
 * each object instance.
380
 *
381
 * A FunctionTemplate can have a prototype template. The prototype template
382
 * is used to create the prototype object of the function.
383
 *
384
 * The following example shows how to use a FunctionTemplate:
385
 *
386
 * \code
387
 *    v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
388
 *    t->Set(isolate, "func_property", v8::Number::New(isolate, 1));
389
 *
390
 *    v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
391
 *    proto_t->Set(isolate,
392
 *                 "proto_method",
393
 *                 v8::FunctionTemplate::New(isolate, InvokeCallback));
394
 *    proto_t->Set(isolate, "proto_const", v8::Number::New(isolate, 2));
395
 *
396
 *    v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
397
 *    instance_t->SetAccessor(
398
          String::NewFromUtf8Literal(isolate, "instance_accessor"),
399
 *        InstanceAccessorCallback);
400
 *    instance_t->SetHandler(
401
 *        NamedPropertyHandlerConfiguration(PropertyHandlerCallback));
402
 *    instance_t->Set(String::NewFromUtf8Literal(isolate, "instance_property"),
403
 *                    Number::New(isolate, 3));
404
 *
405
 *    v8::Local<v8::Function> function = t->GetFunction();
406
 *    v8::Local<v8::Object> instance = function->NewInstance();
407
 * \endcode
408
 *
409
 * Let's use "function" as the JS variable name of the function object
410
 * and "instance" for the instance object created above.  The function
411
 * and the instance will have the following properties:
412
 *
413
 * \code
414
 *   func_property in function == true;
415
 *   function.func_property == 1;
416
 *
417
 *   function.prototype.proto_method() invokes 'InvokeCallback'
418
 *   function.prototype.proto_const == 2;
419
 *
420
 *   instance instanceof function == true;
421
 *   instance.instance_accessor calls 'InstanceAccessorCallback'
422
 *   instance.instance_property == 3;
423
 * \endcode
424
 *
425
 * A FunctionTemplate can inherit from another one by calling the
426
 * FunctionTemplate::Inherit method.  The following graph illustrates
427
 * the semantics of inheritance:
428
 *
429
 * \code
430
 *   FunctionTemplate Parent  -> Parent() . prototype -> { }
431
 *     ^                                                  ^
432
 *     | Inherit(Parent)                                  | .__proto__
433
 *     |                                                  |
434
 *   FunctionTemplate Child   -> Child()  . prototype -> { }
435
 * \endcode
436
 *
437
 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
438
 * object of the Child() function has __proto__ pointing to the
439
 * Parent() function's prototype object. An instance of the Child
440
 * function has all properties on Parent's instance templates.
441
 *
442
 * Let Parent be the FunctionTemplate initialized in the previous
443
 * section and create a Child FunctionTemplate by:
444
 *
445
 * \code
446
 *   Local<FunctionTemplate> parent = t;
447
 *   Local<FunctionTemplate> child = FunctionTemplate::New();
448
 *   child->Inherit(parent);
449
 *
450
 *   Local<Function> child_function = child->GetFunction();
451
 *   Local<Object> child_instance = child_function->NewInstance();
452
 * \endcode
453
 *
454
 * The Child function and Child instance will have the following
455
 * properties:
456
 *
457
 * \code
458
 *   child_func.prototype.__proto__ == function.prototype;
459
 *   child_instance.instance_accessor calls 'InstanceAccessorCallback'
460
 *   child_instance.instance_property == 3;
461
 * \endcode
462
 *
463
 * The additional 'c_function' parameter refers to a fast API call, which
464
 * must not trigger GC or JavaScript execution, or call into V8 in other
465
 * ways. For more information how to define them, see
466
 * include/v8-fast-api-calls.h. Please note that this feature is still
467
 * experimental.
468
 */
469
class V8_EXPORT FunctionTemplate : public Template {
470
 public:
471
  /** Creates a function template.*/
472
  static Local<FunctionTemplate> New(
473
      Isolate* isolate, FunctionCallback callback = nullptr,
474
      Local<Value> data = Local<Value>(),
475
      Local<Signature> signature = Local<Signature>(), int length = 0,
476
      ConstructorBehavior behavior = ConstructorBehavior::kAllow,
477
      SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
478
      const CFunction* c_function = nullptr, uint16_t instance_type = 0,
479
      uint16_t allowed_receiver_instance_type_range_start = 0,
480
      uint16_t allowed_receiver_instance_type_range_end = 0);
481
482
  /** Creates a function template for multiple overloaded fast API calls.*/
483
  static Local<FunctionTemplate> NewWithCFunctionOverloads(
484
      Isolate* isolate, FunctionCallback callback = nullptr,
485
      Local<Value> data = Local<Value>(),
486
      Local<Signature> signature = Local<Signature>(), int length = 0,
487
      ConstructorBehavior behavior = ConstructorBehavior::kAllow,
488
      SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
489
      const MemorySpan<const CFunction>& c_function_overloads = {});
490
491
  /**
492
   * Creates a function template backed/cached by a private property.
493
   */
494
  static Local<FunctionTemplate> NewWithCache(
495
      Isolate* isolate, FunctionCallback callback,
496
      Local<Private> cache_property, Local<Value> data = Local<Value>(),
497
      Local<Signature> signature = Local<Signature>(), int length = 0,
498
      SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
499
500
  /** Returns the unique function instance in the current execution context.*/
501
  V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
502
      Local<Context> context);
503
504
  /**
505
   * Similar to Context::NewRemoteContext, this creates an instance that
506
   * isn't backed by an actual object.
507
   *
508
   * The InstanceTemplate of this FunctionTemplate must have access checks with
509
   * handlers installed.
510
   */
511
  V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
512
513
  /**
514
   * Set the call-handler callback for a FunctionTemplate.  This
515
   * callback is called whenever the function created from this
516
   * FunctionTemplate is called. The 'c_function' represents a fast
517
   * API call, see the comment above the class declaration.
518
   */
519
  void SetCallHandler(
520
      FunctionCallback callback, Local<Value> data = Local<Value>(),
521
      SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
522
      const MemorySpan<const CFunction>& c_function_overloads = {});
523
524
  /** Set the predefined length property for the FunctionTemplate. */
525
  void SetLength(int length);
526
527
  /** Get the InstanceTemplate. */
528
  Local<ObjectTemplate> InstanceTemplate();
529
530
  /**
531
   * Causes the function template to inherit from a parent function template.
532
   * This means the function's prototype.__proto__ is set to the parent
533
   * function's prototype.
534
   **/
535
  void Inherit(Local<FunctionTemplate> parent);
536
537
  /**
538
   * A PrototypeTemplate is the template used to create the prototype object
539
   * of the function created by this template.
540
   */
541
  Local<ObjectTemplate> PrototypeTemplate();
542
543
  /**
544
   * A PrototypeProviderTemplate is another function template whose prototype
545
   * property is used for this template. This is mutually exclusive with setting
546
   * a prototype template indirectly by calling PrototypeTemplate() or using
547
   * Inherit().
548
   **/
549
  void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
550
551
  /**
552
   * Set the class name of the FunctionTemplate.  This is used for
553
   * printing objects created with the function created from the
554
   * FunctionTemplate as its constructor.
555
   */
556
  void SetClassName(Local<String> name);
557
558
  /**
559
   * When set to true, no access check will be performed on the receiver of a
560
   * function call.  Currently defaults to true, but this is subject to change.
561
   */
562
  void SetAcceptAnyReceiver(bool value);
563
564
  /**
565
   * Sets the ReadOnly flag in the attributes of the 'prototype' property
566
   * of functions created from this FunctionTemplate to true.
567
   */
568
  void ReadOnlyPrototype();
569
570
  /**
571
   * Removes the prototype property from functions created from this
572
   * FunctionTemplate.
573
   */
574
  void RemovePrototype();
575
576
  /**
577
   * Returns true if the given object is an instance of this function
578
   * template.
579
   */
580
  bool HasInstance(Local<Value> object);
581
582
  /**
583
   * Returns true if the given value is an API object that was constructed by an
584
   * instance of this function template (without checking for inheriting
585
   * function templates).
586
   *
587
   * This is an experimental feature and may still change significantly.
588
   */
589
  bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
590
591
  V8_INLINE static FunctionTemplate* Cast(Data* data);
592
593
 private:
594
  FunctionTemplate();
595
596
  static void CheckCast(Data* that);
597
  friend class Context;
598
  friend class ObjectTemplate;
599
};
600
601
/**
602
 * Configuration flags for v8::NamedPropertyHandlerConfiguration or
603
 * v8::IndexedPropertyHandlerConfiguration.
604
 */
605
enum class PropertyHandlerFlags {
606
  /**
607
   * None.
608
   */
609
  kNone = 0,
610
611
  /** Will not call into interceptor for properties on the receiver or prototype
612
   * chain, i.e., only call into interceptor for properties that do not exist.
613
   * Currently only valid for named interceptors.
614
   */
615
  kNonMasking = 1,
616
617
  /**
618
   * Will not call into interceptor for symbol lookup.  Only meaningful for
619
   * named interceptors.
620
   */
621
  kOnlyInterceptStrings = 1 << 1,
622
623
  /**
624
   * The getter, query, enumerator callbacks do not produce side effects.
625
   */
626
  kHasNoSideEffect = 1 << 2,
627
};
628
629
struct NamedPropertyHandlerConfiguration {
630
  NamedPropertyHandlerConfiguration(
631
      GenericNamedPropertyGetterCallback getter,
632
      GenericNamedPropertySetterCallback setter,
633
      GenericNamedPropertyQueryCallback query,
634
      GenericNamedPropertyDeleterCallback deleter,
635
      GenericNamedPropertyEnumeratorCallback enumerator,
636
      GenericNamedPropertyDefinerCallback definer,
637
      GenericNamedPropertyDescriptorCallback descriptor,
638
      Local<Value> data = Local<Value>(),
639
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
640
134k
      : getter(getter),
641
134k
        setter(setter),
642
134k
        query(query),
643
134k
        deleter(deleter),
644
134k
        enumerator(enumerator),
645
134k
        definer(definer),
646
134k
        descriptor(descriptor),
647
134k
        data(data),
648
134k
        flags(flags) {}
649
650
  NamedPropertyHandlerConfiguration(
651
      /** Note: getter is required */
652
      GenericNamedPropertyGetterCallback getter = nullptr,
653
      GenericNamedPropertySetterCallback setter = nullptr,
654
      GenericNamedPropertyQueryCallback query = nullptr,
655
      GenericNamedPropertyDeleterCallback deleter = nullptr,
656
      GenericNamedPropertyEnumeratorCallback enumerator = nullptr,
657
      Local<Value> data = Local<Value>(),
658
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
659
      : getter(getter),
660
        setter(setter),
661
        query(query),
662
        deleter(deleter),
663
        enumerator(enumerator),
664
        definer(nullptr),
665
        descriptor(nullptr),
666
        data(data),
667
0
        flags(flags) {}
668
669
  NamedPropertyHandlerConfiguration(
670
      GenericNamedPropertyGetterCallback getter,
671
      GenericNamedPropertySetterCallback setter,
672
      GenericNamedPropertyDescriptorCallback descriptor,
673
      GenericNamedPropertyDeleterCallback deleter,
674
      GenericNamedPropertyEnumeratorCallback enumerator,
675
      GenericNamedPropertyDefinerCallback definer,
676
      Local<Value> data = Local<Value>(),
677
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
678
134k
      : getter(getter),
679
134k
        setter(setter),
680
134k
        query(nullptr),
681
134k
        deleter(deleter),
682
134k
        enumerator(enumerator),
683
134k
        definer(definer),
684
134k
        descriptor(descriptor),
685
134k
        data(data),
686
134k
        flags(flags) {}
687
688
  GenericNamedPropertyGetterCallback getter;
689
  GenericNamedPropertySetterCallback setter;
690
  GenericNamedPropertyQueryCallback query;
691
  GenericNamedPropertyDeleterCallback deleter;
692
  GenericNamedPropertyEnumeratorCallback enumerator;
693
  GenericNamedPropertyDefinerCallback definer;
694
  GenericNamedPropertyDescriptorCallback descriptor;
695
  Local<Value> data;
696
  PropertyHandlerFlags flags;
697
};
698
699
struct IndexedPropertyHandlerConfiguration {
700
  IndexedPropertyHandlerConfiguration(
701
      IndexedPropertyGetterCallback getter,
702
      IndexedPropertySetterCallback setter, IndexedPropertyQueryCallback query,
703
      IndexedPropertyDeleterCallback deleter,
704
      IndexedPropertyEnumeratorCallback enumerator,
705
      IndexedPropertyDefinerCallback definer,
706
      IndexedPropertyDescriptorCallback descriptor,
707
      Local<Value> data = Local<Value>(),
708
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
709
      : getter(getter),
710
        setter(setter),
711
        query(query),
712
        deleter(deleter),
713
        enumerator(enumerator),
714
        definer(definer),
715
        descriptor(descriptor),
716
        data(data),
717
0
        flags(flags) {}
718
719
  IndexedPropertyHandlerConfiguration(
720
      /** Note: getter is required */
721
      IndexedPropertyGetterCallback getter = nullptr,
722
      IndexedPropertySetterCallback setter = nullptr,
723
      IndexedPropertyQueryCallback query = nullptr,
724
      IndexedPropertyDeleterCallback deleter = nullptr,
725
      IndexedPropertyEnumeratorCallback enumerator = nullptr,
726
      Local<Value> data = Local<Value>(),
727
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
728
      : getter(getter),
729
        setter(setter),
730
        query(query),
731
        deleter(deleter),
732
        enumerator(enumerator),
733
        definer(nullptr),
734
        descriptor(nullptr),
735
        data(data),
736
0
        flags(flags) {}
737
738
  IndexedPropertyHandlerConfiguration(
739
      IndexedPropertyGetterCallback getter,
740
      IndexedPropertySetterCallback setter,
741
      IndexedPropertyDescriptorCallback descriptor,
742
      IndexedPropertyDeleterCallback deleter,
743
      IndexedPropertyEnumeratorCallback enumerator,
744
      IndexedPropertyDefinerCallback definer,
745
      Local<Value> data = Local<Value>(),
746
      PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
747
134k
      : getter(getter),
748
134k
        setter(setter),
749
134k
        query(nullptr),
750
134k
        deleter(deleter),
751
134k
        enumerator(enumerator),
752
134k
        definer(definer),
753
134k
        descriptor(descriptor),
754
134k
        data(data),
755
134k
        flags(flags) {}
756
757
  IndexedPropertyGetterCallback getter;
758
  IndexedPropertySetterCallback setter;
759
  IndexedPropertyQueryCallback query;
760
  IndexedPropertyDeleterCallback deleter;
761
  IndexedPropertyEnumeratorCallback enumerator;
762
  IndexedPropertyDefinerCallback definer;
763
  IndexedPropertyDescriptorCallback descriptor;
764
  Local<Value> data;
765
  PropertyHandlerFlags flags;
766
};
767
768
/**
769
 * An ObjectTemplate is used to create objects at runtime.
770
 *
771
 * Properties added to an ObjectTemplate are added to each object
772
 * created from the ObjectTemplate.
773
 */
774
class V8_EXPORT ObjectTemplate : public Template {
775
 public:
776
  /** Creates an ObjectTemplate. */
777
  static Local<ObjectTemplate> New(
778
      Isolate* isolate,
779
      Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
780
781
  /** Creates a new instance of this template.*/
782
  V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
783
784
  /**
785
   * Sets an accessor on the object template.
786
   *
787
   * Whenever the property with the given name is accessed on objects
788
   * created from this ObjectTemplate the getter and setter callbacks
789
   * are called instead of getting and setting the property directly
790
   * on the JavaScript object.
791
   *
792
   * \param name The name of the property for which an accessor is added.
793
   * \param getter The callback to invoke when getting the property.
794
   * \param setter The callback to invoke when setting the property.
795
   * \param data A piece of data that will be passed to the getter and setter
796
   *   callbacks whenever they are invoked.
797
   * \param attribute The attributes of the property for which an accessor
798
   *   is added.
799
   */
800
  void SetAccessor(
801
      Local<String> name, AccessorGetterCallback getter,
802
      AccessorSetterCallback setter = nullptr,
803
      Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
804
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
805
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
806
  void SetAccessor(
807
      Local<Name> name, AccessorNameGetterCallback getter,
808
      AccessorNameSetterCallback setter = nullptr,
809
      Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
810
      SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
811
      SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
812
813
  /**
814
   * Sets a named property handler on the object template.
815
   *
816
   * Whenever a property whose name is a string or a symbol is accessed on
817
   * objects created from this object template, the provided callback is
818
   * invoked instead of accessing the property directly on the JavaScript
819
   * object.
820
   *
821
   * @param configuration The NamedPropertyHandlerConfiguration that defines the
822
   * callbacks to invoke when accessing a property.
823
   */
824
  void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
825
826
  /**
827
   * Sets an indexed property handler on the object template.
828
   *
829
   * Whenever an indexed property is accessed on objects created from
830
   * this object template, the provided callback is invoked instead of
831
   * accessing the property directly on the JavaScript object.
832
   *
833
   * \param getter The callback to invoke when getting a property.
834
   * \param setter The callback to invoke when setting a property.
835
   * \param query The callback to invoke to check if an object has a property.
836
   * \param deleter The callback to invoke when deleting a property.
837
   * \param enumerator The callback to invoke to enumerate all the indexed
838
   *   properties of an object.
839
   * \param data A piece of data that will be passed to the callbacks
840
   *   whenever they are invoked.
841
   */
842
  // TODO(dcarney): deprecate
843
  void SetIndexedPropertyHandler(
844
      IndexedPropertyGetterCallback getter,
845
      IndexedPropertySetterCallback setter = nullptr,
846
      IndexedPropertyQueryCallback query = nullptr,
847
      IndexedPropertyDeleterCallback deleter = nullptr,
848
      IndexedPropertyEnumeratorCallback enumerator = nullptr,
849
0
      Local<Value> data = Local<Value>()) {
850
0
    SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
851
0
                                                   deleter, enumerator, data));
852
0
  }
853
854
  /**
855
   * Sets an indexed property handler on the object template.
856
   *
857
   * Whenever an indexed property is accessed on objects created from
858
   * this object template, the provided callback is invoked instead of
859
   * accessing the property directly on the JavaScript object.
860
   *
861
   * @param configuration The IndexedPropertyHandlerConfiguration that defines
862
   * the callbacks to invoke when accessing a property.
863
   */
864
  void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
865
866
  /**
867
   * Sets the callback to be used when calling instances created from
868
   * this template as a function.  If no callback is set, instances
869
   * behave like normal JavaScript objects that cannot be called as a
870
   * function.
871
   */
872
  void SetCallAsFunctionHandler(FunctionCallback callback,
873
                                Local<Value> data = Local<Value>());
874
875
  /**
876
   * Mark object instances of the template as undetectable.
877
   *
878
   * In many ways, undetectable objects behave as though they are not
879
   * there.  They behave like 'undefined' in conditionals and when
880
   * printed.  However, properties can be accessed and called as on
881
   * normal objects.
882
   */
883
  void MarkAsUndetectable();
884
885
  /**
886
   * Sets access check callback on the object template and enables access
887
   * checks.
888
   *
889
   * When accessing properties on instances of this object template,
890
   * the access check callback will be called to determine whether or
891
   * not to allow cross-context access to the properties.
892
   */
893
  void SetAccessCheckCallback(AccessCheckCallback callback,
894
                              Local<Value> data = Local<Value>());
895
896
  /**
897
   * Like SetAccessCheckCallback but invokes an interceptor on failed access
898
   * checks instead of looking up all-can-read properties. You can only use
899
   * either this method or SetAccessCheckCallback, but not both at the same
900
   * time.
901
   */
902
  void SetAccessCheckCallbackAndHandler(
903
      AccessCheckCallback callback,
904
      const NamedPropertyHandlerConfiguration& named_handler,
905
      const IndexedPropertyHandlerConfiguration& indexed_handler,
906
      Local<Value> data = Local<Value>());
907
908
  /**
909
   * Gets the number of internal fields for objects generated from
910
   * this template.
911
   */
912
  int InternalFieldCount() const;
913
914
  /**
915
   * Sets the number of internal fields for objects generated from
916
   * this template.
917
   */
918
  void SetInternalFieldCount(int value);
919
920
  /**
921
   * Returns true if the object will be an immutable prototype exotic object.
922
   */
923
  bool IsImmutableProto() const;
924
925
  /**
926
   * Makes the ObjectTemplate for an immutable prototype exotic object, with an
927
   * immutable __proto__.
928
   */
929
  void SetImmutableProto();
930
931
  /**
932
   * Support for TC39 "dynamic code brand checks" proposal.
933
   *
934
   * This API allows to mark (& query) objects as "code like", which causes
935
   * them to be treated like Strings in the context of eval and function
936
   * constructor.
937
   *
938
   * Reference: https://github.com/tc39/proposal-dynamic-code-brand-checks
939
   */
940
  void SetCodeLike();
941
  bool IsCodeLike() const;
942
943
  V8_INLINE static ObjectTemplate* Cast(Data* data);
944
945
 private:
946
  ObjectTemplate();
947
  static Local<ObjectTemplate> New(internal::Isolate* isolate,
948
                                   Local<FunctionTemplate> constructor);
949
  static void CheckCast(Data* that);
950
  friend class FunctionTemplate;
951
};
952
953
/**
954
 * A Signature specifies which receiver is valid for a function.
955
 *
956
 * A receiver matches a given signature if the receiver (or any of its
957
 * hidden prototypes) was created from the signature's FunctionTemplate, or
958
 * from a FunctionTemplate that inherits directly or indirectly from the
959
 * signature's FunctionTemplate.
960
 */
961
class V8_EXPORT Signature : public Data {
962
 public:
963
  static Local<Signature> New(
964
      Isolate* isolate,
965
      Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
966
967
  V8_INLINE static Signature* Cast(Data* data);
968
969
 private:
970
  Signature();
971
972
  static void CheckCast(Data* that);
973
};
974
975
// --- Implementation ---
976
977
void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
978
269k
                   PropertyAttribute attributes) {
979
269k
  Set(String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
980
269k
          .ToLocalChecked(),
981
269k
      value, attributes);
982
269k
}
983
984
0
FunctionTemplate* FunctionTemplate::Cast(Data* data) {
985
#ifdef V8_ENABLE_CHECKS
986
  CheckCast(data);
987
#endif
988
0
  return reinterpret_cast<FunctionTemplate*>(data);
989
0
}
990
991
0
ObjectTemplate* ObjectTemplate::Cast(Data* data) {
992
#ifdef V8_ENABLE_CHECKS
993
  CheckCast(data);
994
#endif
995
0
  return reinterpret_cast<ObjectTemplate*>(data);
996
0
}
997
998
0
Signature* Signature::Cast(Data* data) {
999
0
#ifdef V8_ENABLE_CHECKS
1000
0
  CheckCast(data);
1001
0
#endif
1002
0
  return reinterpret_cast<Signature*>(data);
1003
0
}
1004
1005
}  // namespace v8
1006
1007
#endif  // INCLUDE_V8_TEMPLATE_H_