Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:05:27

0001 // Copyright 2021 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_V8_TEMPLATE_H_
0006 #define INCLUDE_V8_TEMPLATE_H_
0007 
0008 #include <cstddef>
0009 #include <string_view>
0010 
0011 #include "v8-data.h"               // NOLINT(build/include_directory)
0012 #include "v8-function-callback.h"  // NOLINT(build/include_directory)
0013 #include "v8-local-handle.h"       // NOLINT(build/include_directory)
0014 #include "v8-memory-span.h"        // NOLINT(build/include_directory)
0015 #include "v8-object.h"             // NOLINT(build/include_directory)
0016 #include "v8config.h"              // NOLINT(build/include_directory)
0017 
0018 namespace v8 {
0019 
0020 class CFunction;
0021 class FunctionTemplate;
0022 class ObjectTemplate;
0023 class Signature;
0024 
0025 // --- Templates ---
0026 
0027 #define V8_INTRINSICS_LIST(F)                                 \
0028   F(ArrayProto_entries, array_entries_iterator)               \
0029   F(ArrayProto_forEach, array_for_each_iterator)              \
0030   F(ArrayProto_keys, array_keys_iterator)                     \
0031   F(ArrayProto_values, array_values_iterator)                 \
0032   F(ArrayPrototype, initial_array_prototype)                  \
0033   F(AsyncIteratorPrototype, initial_async_iterator_prototype) \
0034   F(ErrorPrototype, initial_error_prototype)                  \
0035   F(IteratorPrototype, initial_iterator_prototype)            \
0036   F(MapIteratorPrototype, initial_map_iterator_prototype)     \
0037   F(ObjProto_valueOf, object_value_of_function)               \
0038   F(SetIteratorPrototype, initial_set_iterator_prototype)
0039 
0040 enum Intrinsic {
0041 #define V8_DECL_INTRINSIC(name, iname) k##name,
0042   V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
0043 #undef V8_DECL_INTRINSIC
0044 };
0045 
0046 /**
0047  * The superclass of object and function templates.
0048  */
0049 class V8_EXPORT Template : public Data {
0050  public:
0051   /**
0052    * Adds a property to each instance created by this template.
0053    *
0054    * The property must be defined either as a primitive value, or a template.
0055    */
0056   void Set(Local<Name> name, Local<Data> value,
0057            PropertyAttribute attributes = None);
0058   void SetPrivate(Local<Private> name, Local<Data> value,
0059                   PropertyAttribute attributes = None);
0060   V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value,
0061                      PropertyAttribute attributes = None);
0062 
0063   void SetAccessorProperty(
0064       Local<Name> name,
0065       Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
0066       Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
0067       PropertyAttribute attribute = None);
0068 
0069   /**
0070    * Whenever the property with the given name is accessed on objects
0071    * created from this Template the getter and setter callbacks
0072    * are called instead of getting and setting the property directly
0073    * on the JavaScript object.
0074    *
0075    * \param name The name of the property for which an accessor is added.
0076    * \param getter The callback to invoke when getting the property.
0077    * \param setter The callback to invoke when setting the property.
0078    * \param data A piece of data that will be passed to the getter and setter
0079    *   callbacks whenever they are invoked.
0080    * \param attribute The attributes of the property for which an accessor
0081    *   is added.
0082    */
0083   V8_DEPRECATE_SOON("Use SetNativeDataProperty without AccessControl instead")
0084   void SetNativeDataProperty(
0085       Local<String> name, AccessorGetterCallback getter,
0086       AccessorSetterCallback setter, Local<Value> data,
0087       PropertyAttribute attribute, AccessControl settings,
0088       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0089       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0090   V8_DEPRECATE_SOON("Use SetNativeDataProperty without AccessControl instead")
0091   void SetNativeDataProperty(
0092       Local<Name> name, AccessorNameGetterCallback getter,
0093       AccessorNameSetterCallback setter, Local<Value> data,
0094       PropertyAttribute attribute, AccessControl settings,
0095       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0096       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0097   V8_DEPRECATE_SOON("Use SetNativeDataProperty with Local<Name> instead")
0098   void SetNativeDataProperty(
0099       Local<String> name, AccessorGetterCallback getter,
0100       AccessorSetterCallback setter = nullptr,
0101       Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
0102       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0103       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0104   void SetNativeDataProperty(
0105       Local<Name> name, AccessorNameGetterCallback getter,
0106       AccessorNameSetterCallback setter = nullptr,
0107       Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
0108       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0109       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0110 
0111   /**
0112    * Like SetNativeDataProperty, but V8 will replace the native data property
0113    * with a real data property on first access.
0114    */
0115   void SetLazyDataProperty(
0116       Local<Name> name, AccessorNameGetterCallback getter,
0117       Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
0118       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
0119       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
0120 
0121   /**
0122    * During template instantiation, sets the value with the intrinsic property
0123    * from the correct context.
0124    */
0125   void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
0126                                 PropertyAttribute attribute = None);
0127 
0128  private:
0129   Template();
0130 
0131   friend class ObjectTemplate;
0132   friend class FunctionTemplate;
0133 };
0134 
0135 /**
0136  * Interceptor callbacks use this value to indicate whether the request was
0137  * intercepted or not.
0138  */
0139 enum class Intercepted : uint8_t { kNo = 0, kYes = 1 };
0140 
0141 /**
0142  * Interceptor for get requests on an object.
0143  *
0144  * If the interceptor handles the request (i.e. the property should not be
0145  * looked up beyond the interceptor) it should
0146  *  - (optionally) use info.GetReturnValue().Set()` to set the return value
0147  *    (by default the result is set to v8::Undefined),
0148  *  - return `Intercepted::kYes`.
0149  * If the interceptor does not handle the request it must return
0150  * `Intercepted::kNo` and it must not produce side effects.
0151  *
0152  * \param property The name of the property for which the request was
0153  * intercepted.
0154  * \param info Information about the intercepted request, such as
0155  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0156  * See `PropertyCallbackInfo`.
0157  *
0158  * \code
0159  *  Intercepted GetterCallback(
0160  *      Local<Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
0161  *    if (!IsKnownProperty(info.GetIsolate(), name)) return Intercepted::kNo;
0162  *    info.GetReturnValue().Set(v8_num(42));
0163  *    return Intercepted::kYes;
0164  *  }
0165  *
0166  *  v8::Local<v8::FunctionTemplate> templ =
0167  *      v8::FunctionTemplate::New(isolate);
0168  *  templ->InstanceTemplate()->SetHandler(
0169  *      v8::NamedPropertyHandlerConfiguration(GetterCallback));
0170  *  LocalContext env;
0171  *  env->Global()
0172  *      ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local())
0173  *                                             .ToLocalChecked()
0174  *                                             ->NewInstance(env.local())
0175  *                                             .ToLocalChecked())
0176  *      .FromJust();
0177  *  v8::Local<v8::Value> result = CompileRun("obj.a = 17; obj.a");
0178  *  CHECK(v8_num(42)->Equals(env.local(), result).FromJust());
0179  * \endcode
0180  *
0181  * See also `ObjectTemplate::SetHandler`.
0182  */
0183 using NamedPropertyGetterCallback = Intercepted (*)(
0184     Local<Name> property, const PropertyCallbackInfo<Value>& info);
0185 // This variant will be deprecated soon.
0186 //
0187 // Use `info.GetReturnValue().Set()` to set the return value of the
0188 // intercepted get request. If the property does not exist the callback should
0189 // not set the result and must not produce side effects.
0190 using GenericNamedPropertyGetterCallback =
0191     void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
0192 
0193 /**
0194  * Interceptor for set requests on an object.
0195  *
0196  * If the interceptor handles the request (i.e. the property should not be
0197  * looked up beyond the interceptor) it should return `Intercepted::kYes`.
0198  * If the interceptor does not handle the request it must return
0199  * `Intercepted::kNo` and it must not produce side effects.
0200  *
0201  * \param property The name of the property for which the request was
0202  * intercepted.
0203  * \param value The value which the property will have if the request
0204  * is not intercepted.
0205  * \param info Information about the intercepted request, such as
0206  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0207  * See `PropertyCallbackInfo`.
0208  *
0209  * See also `ObjectTemplate::SetHandler.`
0210  */
0211 using NamedPropertySetterCallback =
0212     Intercepted (*)(Local<Name> property, Local<Value> value,
0213                     const PropertyCallbackInfo<void>& info);
0214 // This variant will be deprecated soon.
0215 //
0216 // Use `info.GetReturnValue()` to indicate whether the request was intercepted
0217 // or not. If the setter successfully intercepts the request, i.e., if the
0218 // request should not be further executed, call
0219 // `info.GetReturnValue().Set(value)`. If the setter did not intercept the
0220 // request, i.e., if the request should be handled as if no interceptor is
0221 // present, do not not call `Set()` and do not produce side effects.
0222 using GenericNamedPropertySetterCallback =
0223     void (*)(Local<Name> property, Local<Value> value,
0224              const PropertyCallbackInfo<Value>& info);
0225 
0226 /**
0227  * Intercepts all requests that query the attributes of the
0228  * property, e.g., getOwnPropertyDescriptor(), propertyIsEnumerable(), and
0229  * defineProperty().
0230  *
0231  * If the interceptor handles the request (i.e. the property should not be
0232  * looked up beyond the interceptor) it should
0233  *  - use `info.GetReturnValue().Set()` to set to an Integer value encoding
0234  *    a `v8::PropertyAttribute` bits,
0235  *  - return `Intercepted::kYes`.
0236  * If the interceptor does not handle the request it must return
0237  * `Intercepted::kNo` and it must not produce side effects.
0238  *
0239  * \param property The name of the property for which the request was
0240  * intercepted.
0241  * \param info Information about the intercepted request, such as
0242  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0243  * See `PropertyCallbackInfo`.
0244  *
0245  * \note Some functions query the property attributes internally, even though
0246  * they do not return the attributes. For example, `hasOwnProperty()` can
0247  * trigger this interceptor depending on the state of the object.
0248  *
0249  * See also `ObjectTemplate::SetHandler.`
0250  */
0251 using NamedPropertyQueryCallback = Intercepted (*)(
0252     Local<Name> property, const PropertyCallbackInfo<Integer>& info);
0253 // This variant will be deprecated soon.
0254 //
0255 // Use `info.GetReturnValue().Set(value)` to set the property attributes. The
0256 // value is an integer encoding a `v8::PropertyAttribute`. If the property does
0257 // not exist the callback should not set the result and must not produce side
0258 // effects.
0259 using GenericNamedPropertyQueryCallback =
0260     void (*)(Local<Name> property, const PropertyCallbackInfo<Integer>& info);
0261 
0262 /**
0263  * Interceptor for delete requests on an object.
0264  *
0265  * If the interceptor handles the request (i.e. the property should not be
0266  * looked up beyond the interceptor) it should
0267  *  - use `info.GetReturnValue().Set()` to set to a Boolean value indicating
0268  *    whether the property deletion was successful or not,
0269  *  - return `Intercepted::kYes`.
0270  * If the interceptor does not handle the request it must return
0271  * `Intercepted::kNo` and it must not produce side effects.
0272  *
0273  * \param property The name of the property for which the request was
0274  * intercepted.
0275  * \param info Information about the intercepted request, such as
0276  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0277  * See `PropertyCallbackInfo`.
0278  *
0279  * \note If you need to mimic the behavior of `delete`, i.e., throw in strict
0280  * mode instead of returning false, use `info.ShouldThrowOnError()` to determine
0281  * if you are in strict mode.
0282  *
0283  * See also `ObjectTemplate::SetHandler.`
0284  */
0285 using NamedPropertyDeleterCallback = Intercepted (*)(
0286     Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
0287 // This variant will be deprecated soon.
0288 //
0289 // Use `info.GetReturnValue()` to indicate whether the request was intercepted
0290 // or not. If the deleter successfully intercepts the request, i.e., if the
0291 // request should not be further executed, call
0292 // `info.GetReturnValue().Set(value)` with a boolean `value`. The `value` is
0293 // used as the return value of `delete`. If the deleter does not intercept the
0294 // request then it should not set the result and must not produce side effects.
0295 using GenericNamedPropertyDeleterCallback =
0296     void (*)(Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
0297 
0298 /**
0299  * Returns an array containing the names of the properties the named
0300  * property getter intercepts.
0301  *
0302  * Note: The values in the array must be of type v8::Name.
0303  */
0304 using NamedPropertyEnumeratorCallback =
0305     void (*)(const PropertyCallbackInfo<Array>& info);
0306 // This variant will be deprecated soon.
0307 // This is just a renaming of the typedef.
0308 using GenericNamedPropertyEnumeratorCallback = NamedPropertyEnumeratorCallback;
0309 
0310 /**
0311  * Interceptor for defineProperty requests on an object.
0312  *
0313  * If the interceptor handles the request (i.e. the property should not be
0314  * looked up beyond the interceptor) it should return `Intercepted::kYes`.
0315  * If the interceptor does not handle the request it must return
0316  * `Intercepted::kNo` and it must not produce side effects.
0317  *
0318  * \param property The name of the property for which the request was
0319  * intercepted.
0320  * \param desc The property descriptor which is used to define the
0321  * property if the request is not intercepted.
0322  * \param info Information about the intercepted request, such as
0323  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0324  * See `PropertyCallbackInfo`.
0325  *
0326  * See also `ObjectTemplate::SetHandler`.
0327  */
0328 using NamedPropertyDefinerCallback =
0329     Intercepted (*)(Local<Name> property, const PropertyDescriptor& desc,
0330                     const PropertyCallbackInfo<void>& info);
0331 // This variant will be deprecated soon.
0332 //
0333 // Use `info.GetReturnValue()` to indicate whether the request was intercepted
0334 // or not. If the definer successfully intercepts the request, i.e., if the
0335 // request should not be further executed, call
0336 // `info.GetReturnValue().Set(value)`. If the definer did not intercept the
0337 // request, i.e., if the request should be handled as if no interceptor is
0338 // present, do not not call `Set()` and do not produce side effects.
0339 using GenericNamedPropertyDefinerCallback =
0340     void (*)(Local<Name> property, const PropertyDescriptor& desc,
0341              const PropertyCallbackInfo<Value>& info);
0342 
0343 /**
0344  * Interceptor for getOwnPropertyDescriptor requests on an object.
0345  *
0346  * If the interceptor handles the request (i.e. the property should not be
0347  * looked up beyond the interceptor) it should
0348  *  - use `info.GetReturnValue().Set()` to set the return value which must be
0349  *    object that can be converted to a PropertyDescriptor (for example,
0350  *    a value returned by `v8::Object::getOwnPropertyDescriptor`),
0351  *  - return `Intercepted::kYes`.
0352  * If the interceptor does not handle the request it must return
0353  * `Intercepted::kNo` and it must not produce side effects.
0354  *
0355  * \param property The name of the property for which the request was
0356  * intercepted.
0357  * \info Information about the intercepted request, such as
0358  * isolate, receiver, return value, or whether running in `'use strict'` mode.
0359  * See `PropertyCallbackInfo`.
0360  *
0361  * \note If GetOwnPropertyDescriptor is intercepted, it will
0362  * always return true, i.e., indicate that the property was found.
0363  *
0364  * See also `ObjectTemplate::SetHandler`.
0365  */
0366 using NamedPropertyDescriptorCallback = Intercepted (*)(
0367     Local<Name> property, const PropertyCallbackInfo<Value>& info);
0368 // This variant will be deprecated soon.
0369 //
0370 // Use `info.GetReturnValue().Set()` to set the return value of the
0371 // intercepted request. The return value must be an object that
0372 // can be converted to a PropertyDescriptor, e.g., a `v8::Value` returned from
0373 // `v8::Object::getOwnPropertyDescriptor`.
0374 using GenericNamedPropertyDescriptorCallback =
0375     void (*)(Local<Name> property, const PropertyCallbackInfo<Value>& info);
0376 
0377 // TODO(ishell): Rename IndexedPropertyXxxCallbackV2 back to
0378 // IndexedPropertyXxxCallback once the old IndexedPropertyXxxCallback is
0379 // removed.
0380 
0381 /**
0382  * See `v8::GenericNamedPropertyGetterCallback`.
0383  */
0384 using IndexedPropertyGetterCallbackV2 =
0385     Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
0386 // This variant will be deprecated soon.
0387 using IndexedPropertyGetterCallback =
0388     void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
0389 
0390 /**
0391  * See `v8::GenericNamedPropertySetterCallback`.
0392  */
0393 using IndexedPropertySetterCallbackV2 = Intercepted (*)(
0394     uint32_t index, Local<Value> value, const PropertyCallbackInfo<void>& info);
0395 // This variant will be deprecated soon.
0396 using IndexedPropertySetterCallback =
0397     void (*)(uint32_t index, Local<Value> value,
0398              const PropertyCallbackInfo<Value>& info);
0399 
0400 /**
0401  * See `v8::GenericNamedPropertyQueryCallback`.
0402  */
0403 using IndexedPropertyQueryCallbackV2 =
0404     Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
0405 // This variant will be deprecated soon.
0406 using IndexedPropertyQueryCallback =
0407     void (*)(uint32_t index, const PropertyCallbackInfo<Integer>& info);
0408 
0409 /**
0410  * See `v8::GenericNamedPropertyDeleterCallback`.
0411  */
0412 using IndexedPropertyDeleterCallbackV2 =
0413     Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
0414 // This variant will be deprecated soon.
0415 using IndexedPropertyDeleterCallback =
0416     void (*)(uint32_t index, const PropertyCallbackInfo<Boolean>& info);
0417 
0418 /**
0419  * Returns an array containing the indices of the properties the indexed
0420  * property getter intercepts.
0421  *
0422  * Note: The values in the array must be uint32_t.
0423  */
0424 using IndexedPropertyEnumeratorCallback =
0425     void (*)(const PropertyCallbackInfo<Array>& info);
0426 
0427 /**
0428  * See `v8::GenericNamedPropertyDefinerCallback`.
0429  */
0430 using IndexedPropertyDefinerCallbackV2 =
0431     Intercepted (*)(uint32_t index, const PropertyDescriptor& desc,
0432                     const PropertyCallbackInfo<void>& info);
0433 // This variant will be deprecated soon.
0434 using IndexedPropertyDefinerCallback =
0435     void (*)(uint32_t index, const PropertyDescriptor& desc,
0436              const PropertyCallbackInfo<Value>& info);
0437 
0438 /**
0439  * See `v8::GenericNamedPropertyDescriptorCallback`.
0440  */
0441 using IndexedPropertyDescriptorCallbackV2 =
0442     Intercepted (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
0443 // This variant will be deprecated soon.
0444 using IndexedPropertyDescriptorCallback =
0445     void (*)(uint32_t index, const PropertyCallbackInfo<Value>& info);
0446 
0447 /**
0448  * Returns true if the given context should be allowed to access the given
0449  * object.
0450  */
0451 using AccessCheckCallback = bool (*)(Local<Context> accessing_context,
0452                                      Local<Object> accessed_object,
0453                                      Local<Value> data);
0454 
0455 enum class ConstructorBehavior { kThrow, kAllow };
0456 
0457 /**
0458  * A FunctionTemplate is used to create functions at runtime. There
0459  * can only be one function created from a FunctionTemplate in a
0460  * context.  The lifetime of the created function is equal to the
0461  * lifetime of the context.  So in case the embedder needs to create
0462  * temporary functions that can be collected using Scripts is
0463  * preferred.
0464  *
0465  * Any modification of a FunctionTemplate after first instantiation will trigger
0466  * a crash.
0467  *
0468  * A FunctionTemplate can have properties, these properties are added to the
0469  * function object when it is created.
0470  *
0471  * A FunctionTemplate has a corresponding instance template which is
0472  * used to create object instances when the function is used as a
0473  * constructor. Properties added to the instance template are added to
0474  * each object instance.
0475  *
0476  * A FunctionTemplate can have a prototype template. The prototype template
0477  * is used to create the prototype object of the function.
0478  *
0479  * The following example shows how to use a FunctionTemplate:
0480  *
0481  * \code
0482  *    v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
0483  *    t->Set(isolate, "func_property", v8::Number::New(isolate, 1));
0484  *
0485  *    v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
0486  *    proto_t->Set(isolate,
0487  *                 "proto_method",
0488  *                 v8::FunctionTemplate::New(isolate, InvokeCallback));
0489  *    proto_t->Set(isolate, "proto_const", v8::Number::New(isolate, 2));
0490  *
0491  *    v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
0492  *    instance_t->SetAccessor(
0493           String::NewFromUtf8Literal(isolate, "instance_accessor"),
0494  *        InstanceAccessorCallback);
0495  *    instance_t->SetHandler(
0496  *        NamedPropertyHandlerConfiguration(PropertyHandlerCallback));
0497  *    instance_t->Set(String::NewFromUtf8Literal(isolate, "instance_property"),
0498  *                    Number::New(isolate, 3));
0499  *
0500  *    v8::Local<v8::Function> function = t->GetFunction();
0501  *    v8::Local<v8::Object> instance = function->NewInstance();
0502  * \endcode
0503  *
0504  * Let's use "function" as the JS variable name of the function object
0505  * and "instance" for the instance object created above.  The function
0506  * and the instance will have the following properties:
0507  *
0508  * \code
0509  *   func_property in function == true;
0510  *   function.func_property == 1;
0511  *
0512  *   function.prototype.proto_method() invokes 'InvokeCallback'
0513  *   function.prototype.proto_const == 2;
0514  *
0515  *   instance instanceof function == true;
0516  *   instance.instance_accessor calls 'InstanceAccessorCallback'
0517  *   instance.instance_property == 3;
0518  * \endcode
0519  *
0520  * A FunctionTemplate can inherit from another one by calling the
0521  * FunctionTemplate::Inherit method.  The following graph illustrates
0522  * the semantics of inheritance:
0523  *
0524  * \code
0525  *   FunctionTemplate Parent  -> Parent() . prototype -> { }
0526  *     ^                                                  ^
0527  *     | Inherit(Parent)                                  | .__proto__
0528  *     |                                                  |
0529  *   FunctionTemplate Child   -> Child()  . prototype -> { }
0530  * \endcode
0531  *
0532  * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
0533  * object of the Child() function has __proto__ pointing to the
0534  * Parent() function's prototype object. An instance of the Child
0535  * function has all properties on Parent's instance templates.
0536  *
0537  * Let Parent be the FunctionTemplate initialized in the previous
0538  * section and create a Child FunctionTemplate by:
0539  *
0540  * \code
0541  *   Local<FunctionTemplate> parent = t;
0542  *   Local<FunctionTemplate> child = FunctionTemplate::New();
0543  *   child->Inherit(parent);
0544  *
0545  *   Local<Function> child_function = child->GetFunction();
0546  *   Local<Object> child_instance = child_function->NewInstance();
0547  * \endcode
0548  *
0549  * The Child function and Child instance will have the following
0550  * properties:
0551  *
0552  * \code
0553  *   child_func.prototype.__proto__ == function.prototype;
0554  *   child_instance.instance_accessor calls 'InstanceAccessorCallback'
0555  *   child_instance.instance_property == 3;
0556  * \endcode
0557  *
0558  * The additional 'c_function' parameter refers to a fast API call, which
0559  * must not trigger GC or JavaScript execution, or call into V8 in other
0560  * ways. For more information how to define them, see
0561  * include/v8-fast-api-calls.h. Please note that this feature is still
0562  * experimental.
0563  */
0564 class V8_EXPORT FunctionTemplate : public Template {
0565  public:
0566   /** Creates a function template.*/
0567   static Local<FunctionTemplate> New(
0568       Isolate* isolate, FunctionCallback callback = nullptr,
0569       Local<Value> data = Local<Value>(),
0570       Local<Signature> signature = Local<Signature>(), int length = 0,
0571       ConstructorBehavior behavior = ConstructorBehavior::kAllow,
0572       SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
0573       const CFunction* c_function = nullptr, uint16_t instance_type = 0,
0574       uint16_t allowed_receiver_instance_type_range_start = 0,
0575       uint16_t allowed_receiver_instance_type_range_end = 0);
0576 
0577   /** Creates a function template for multiple overloaded fast API calls.*/
0578   static Local<FunctionTemplate> NewWithCFunctionOverloads(
0579       Isolate* isolate, FunctionCallback callback = nullptr,
0580       Local<Value> data = Local<Value>(),
0581       Local<Signature> signature = Local<Signature>(), int length = 0,
0582       ConstructorBehavior behavior = ConstructorBehavior::kAllow,
0583       SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
0584       const MemorySpan<const CFunction>& c_function_overloads = {});
0585 
0586   /**
0587    * Creates a function template backed/cached by a private property.
0588    */
0589   static Local<FunctionTemplate> NewWithCache(
0590       Isolate* isolate, FunctionCallback callback,
0591       Local<Private> cache_property, Local<Value> data = Local<Value>(),
0592       Local<Signature> signature = Local<Signature>(), int length = 0,
0593       SideEffectType side_effect_type = SideEffectType::kHasSideEffect);
0594 
0595   /** Returns the unique function instance in the current execution context.*/
0596   V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
0597       Local<Context> context);
0598 
0599   /**
0600    * Similar to Context::NewRemoteContext, this creates an instance that
0601    * isn't backed by an actual object.
0602    *
0603    * The InstanceTemplate of this FunctionTemplate must have access checks with
0604    * handlers installed.
0605    */
0606   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewRemoteInstance();
0607 
0608   /**
0609    * Set the call-handler callback for a FunctionTemplate.  This
0610    * callback is called whenever the function created from this
0611    * FunctionTemplate is called. The 'c_function' represents a fast
0612    * API call, see the comment above the class declaration.
0613    */
0614   void SetCallHandler(
0615       FunctionCallback callback, Local<Value> data = Local<Value>(),
0616       SideEffectType side_effect_type = SideEffectType::kHasSideEffect,
0617       const MemorySpan<const CFunction>& c_function_overloads = {});
0618 
0619   /** Set the predefined length property for the FunctionTemplate. */
0620   void SetLength(int length);
0621 
0622   /** Get the InstanceTemplate. */
0623   Local<ObjectTemplate> InstanceTemplate();
0624 
0625   /**
0626    * Causes the function template to inherit from a parent function template.
0627    * This means the function's prototype.__proto__ is set to the parent
0628    * function's prototype.
0629    **/
0630   void Inherit(Local<FunctionTemplate> parent);
0631 
0632   /**
0633    * A PrototypeTemplate is the template used to create the prototype object
0634    * of the function created by this template.
0635    */
0636   Local<ObjectTemplate> PrototypeTemplate();
0637 
0638   /**
0639    * A PrototypeProviderTemplate is another function template whose prototype
0640    * property is used for this template. This is mutually exclusive with setting
0641    * a prototype template indirectly by calling PrototypeTemplate() or using
0642    * Inherit().
0643    **/
0644   void SetPrototypeProviderTemplate(Local<FunctionTemplate> prototype_provider);
0645 
0646   /**
0647    * Set the class name of the FunctionTemplate.  This is used for
0648    * printing objects created with the function created from the
0649    * FunctionTemplate as its constructor.
0650    */
0651   void SetClassName(Local<String> name);
0652 
0653   /**
0654    * When set to true, no access check will be performed on the receiver of a
0655    * function call.  Currently defaults to true, but this is subject to change.
0656    */
0657   void SetAcceptAnyReceiver(bool value);
0658 
0659   /**
0660    * Sets the ReadOnly flag in the attributes of the 'prototype' property
0661    * of functions created from this FunctionTemplate to true.
0662    */
0663   void ReadOnlyPrototype();
0664 
0665   /**
0666    * Removes the prototype property from functions created from this
0667    * FunctionTemplate.
0668    */
0669   void RemovePrototype();
0670 
0671   /**
0672    * Returns true if the given object is an instance of this function
0673    * template.
0674    */
0675   bool HasInstance(Local<Value> object);
0676 
0677   /**
0678    * Returns true if the given value is an API object that was constructed by an
0679    * instance of this function template (without checking for inheriting
0680    * function templates).
0681    *
0682    * This is an experimental feature and may still change significantly.
0683    */
0684   bool IsLeafTemplateForApiObject(v8::Local<v8::Value> value) const;
0685 
0686   V8_INLINE static FunctionTemplate* Cast(Data* data);
0687 
0688  private:
0689   FunctionTemplate();
0690 
0691   static void CheckCast(Data* that);
0692   friend class Context;
0693   friend class ObjectTemplate;
0694 };
0695 
0696 /**
0697  * Configuration flags for v8::NamedPropertyHandlerConfiguration or
0698  * v8::IndexedPropertyHandlerConfiguration.
0699  */
0700 enum class PropertyHandlerFlags {
0701   /**
0702    * None.
0703    */
0704   kNone = 0,
0705 
0706   /**
0707    * Will not call into interceptor for properties on the receiver or prototype
0708    * chain, i.e., only call into interceptor for properties that do not exist.
0709    * Currently only valid for named interceptors.
0710    */
0711   kNonMasking = 1,
0712 
0713   /**
0714    * Will not call into interceptor for symbol lookup.  Only meaningful for
0715    * named interceptors.
0716    */
0717   kOnlyInterceptStrings = 1 << 1,
0718 
0719   /**
0720    * The getter, query, enumerator callbacks do not produce side effects.
0721    */
0722   kHasNoSideEffect = 1 << 2,
0723 
0724   /**
0725    * This flag is used to distinguish which callbacks were provided -
0726    * GenericNamedPropertyXXXCallback (old signature) or
0727    * NamedPropertyXXXCallback (new signature).
0728    * DO NOT use this flag, it'll be removed once embedders migrate to new
0729    * callbacks signatures.
0730    */
0731   kInternalNewCallbacksSignatures = 1 << 10,
0732 };
0733 
0734 struct NamedPropertyHandlerConfiguration {
0735  private:
0736   static constexpr PropertyHandlerFlags WithNewSignatureFlag(
0737       PropertyHandlerFlags flags) {
0738     return static_cast<PropertyHandlerFlags>(
0739         static_cast<int>(flags) |
0740         static_cast<int>(
0741             PropertyHandlerFlags::kInternalNewCallbacksSignatures));
0742   }
0743 
0744  public:
0745   NamedPropertyHandlerConfiguration(
0746       NamedPropertyGetterCallback getter,          //
0747       NamedPropertySetterCallback setter,          //
0748       NamedPropertyQueryCallback query,            //
0749       NamedPropertyDeleterCallback deleter,        //
0750       NamedPropertyEnumeratorCallback enumerator,  //
0751       NamedPropertyDefinerCallback definer,        //
0752       NamedPropertyDescriptorCallback descriptor,  //
0753       Local<Value> data = Local<Value>(),
0754       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0755       : getter(reinterpret_cast<void*>(getter)),
0756         setter(reinterpret_cast<void*>(setter)),
0757         query(reinterpret_cast<void*>(query)),
0758         deleter(reinterpret_cast<void*>(deleter)),
0759         enumerator(enumerator),
0760         definer(reinterpret_cast<void*>(definer)),
0761         descriptor(reinterpret_cast<void*>(descriptor)),
0762         data(data),
0763         flags(WithNewSignatureFlag(flags)) {}
0764 
0765   // This variant will be deprecated soon.
0766   NamedPropertyHandlerConfiguration(
0767       GenericNamedPropertyGetterCallback getter,
0768       GenericNamedPropertySetterCallback setter,
0769       GenericNamedPropertyQueryCallback query,
0770       GenericNamedPropertyDeleterCallback deleter,
0771       GenericNamedPropertyEnumeratorCallback enumerator,
0772       GenericNamedPropertyDefinerCallback definer,
0773       GenericNamedPropertyDescriptorCallback descriptor,
0774       Local<Value> data = Local<Value>(),
0775       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0776       : getter(reinterpret_cast<void*>(getter)),
0777         setter(reinterpret_cast<void*>(setter)),
0778         query(reinterpret_cast<void*>(query)),
0779         deleter(reinterpret_cast<void*>(deleter)),
0780         enumerator(enumerator),
0781         definer(reinterpret_cast<void*>(definer)),
0782         descriptor(reinterpret_cast<void*>(descriptor)),
0783         data(data),
0784         flags(flags) {}
0785 
0786   explicit NamedPropertyHandlerConfiguration(
0787       NamedPropertyGetterCallback getter,
0788       NamedPropertySetterCallback setter = nullptr,
0789       NamedPropertyQueryCallback query = nullptr,
0790       NamedPropertyDeleterCallback deleter = nullptr,
0791       NamedPropertyEnumeratorCallback enumerator = nullptr,
0792       Local<Value> data = Local<Value>(),
0793       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0794       : getter(reinterpret_cast<void*>(getter)),
0795         setter(reinterpret_cast<void*>(setter)),
0796         query(reinterpret_cast<void*>(query)),
0797         deleter(reinterpret_cast<void*>(deleter)),
0798         enumerator(enumerator),
0799         definer(nullptr),
0800         descriptor(nullptr),
0801         data(data),
0802         flags(WithNewSignatureFlag(flags)) {}
0803 
0804   // This variant will be deprecated soon.
0805   explicit NamedPropertyHandlerConfiguration(
0806       GenericNamedPropertyGetterCallback getter,
0807       GenericNamedPropertySetterCallback setter = nullptr,
0808       GenericNamedPropertyQueryCallback query = nullptr,
0809       GenericNamedPropertyDeleterCallback deleter = nullptr,
0810       GenericNamedPropertyEnumeratorCallback enumerator = nullptr,
0811       Local<Value> data = Local<Value>(),
0812       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0813       : getter(reinterpret_cast<void*>(getter)),
0814         setter(reinterpret_cast<void*>(setter)),
0815         query(reinterpret_cast<void*>(query)),
0816         deleter(reinterpret_cast<void*>(deleter)),
0817         enumerator(enumerator),
0818         definer(nullptr),
0819         descriptor(nullptr),
0820         data(data),
0821         flags(flags) {}
0822 
0823   NamedPropertyHandlerConfiguration(
0824       NamedPropertyGetterCallback getter,          //
0825       NamedPropertySetterCallback setter,          //
0826       NamedPropertyDescriptorCallback descriptor,  //
0827       NamedPropertyDeleterCallback deleter,        //
0828       NamedPropertyEnumeratorCallback enumerator,  //
0829       NamedPropertyDefinerCallback definer,        //
0830       Local<Value> data = Local<Value>(),
0831       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0832       : getter(reinterpret_cast<void*>(getter)),
0833         setter(reinterpret_cast<void*>(setter)),
0834         query(nullptr),
0835         deleter(reinterpret_cast<void*>(deleter)),
0836         enumerator(enumerator),
0837         definer(reinterpret_cast<void*>(definer)),
0838         descriptor(reinterpret_cast<void*>(descriptor)),
0839         data(data),
0840         flags(WithNewSignatureFlag(flags)) {}
0841 
0842   // This variant will be deprecated soon.
0843   NamedPropertyHandlerConfiguration(
0844       GenericNamedPropertyGetterCallback getter,
0845       GenericNamedPropertySetterCallback setter,
0846       GenericNamedPropertyDescriptorCallback descriptor,
0847       GenericNamedPropertyDeleterCallback deleter,
0848       GenericNamedPropertyEnumeratorCallback enumerator,
0849       GenericNamedPropertyDefinerCallback definer,
0850       Local<Value> data = Local<Value>(),
0851       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0852       : getter(reinterpret_cast<void*>(getter)),
0853         setter(reinterpret_cast<void*>(setter)),
0854         query(nullptr),
0855         deleter(reinterpret_cast<void*>(deleter)),
0856         enumerator(enumerator),
0857         definer(reinterpret_cast<void*>(definer)),
0858         descriptor(reinterpret_cast<void*>(descriptor)),
0859         data(data),
0860         flags(flags) {}
0861 
0862   void* getter;   // [Generic]NamedPropertyGetterCallback
0863   void* setter;   // [Generic]NamedPropertySetterCallback
0864   void* query;    // [Generic]NamedPropertyQueryCallback
0865   void* deleter;  // [Generic]NamedPropertyDeleterCallback
0866   NamedPropertyEnumeratorCallback enumerator;
0867   void* definer;     // [Generic]NamedPropertyDefinerCallback
0868   void* descriptor;  // [Generic]NamedPropertyDescriptorCallback
0869   Local<Value> data;
0870   PropertyHandlerFlags flags;
0871 };
0872 
0873 struct IndexedPropertyHandlerConfiguration {
0874  private:
0875   static constexpr PropertyHandlerFlags WithNewSignatureFlag(
0876       PropertyHandlerFlags flags) {
0877     return static_cast<PropertyHandlerFlags>(
0878         static_cast<int>(flags) |
0879         static_cast<int>(
0880             PropertyHandlerFlags::kInternalNewCallbacksSignatures));
0881   }
0882 
0883  public:
0884   IndexedPropertyHandlerConfiguration(
0885       IndexedPropertyGetterCallbackV2 getter,          //
0886       IndexedPropertySetterCallbackV2 setter,          //
0887       IndexedPropertyQueryCallbackV2 query,            //
0888       IndexedPropertyDeleterCallbackV2 deleter,        //
0889       IndexedPropertyEnumeratorCallback enumerator,    //
0890       IndexedPropertyDefinerCallbackV2 definer,        //
0891       IndexedPropertyDescriptorCallbackV2 descriptor,  //
0892       Local<Value> data = Local<Value>(),
0893       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0894       : getter(reinterpret_cast<void*>(getter)),
0895         setter(reinterpret_cast<void*>(setter)),
0896         query(reinterpret_cast<void*>(query)),
0897         deleter(reinterpret_cast<void*>(deleter)),
0898         enumerator(enumerator),
0899         definer(reinterpret_cast<void*>(definer)),
0900         descriptor(reinterpret_cast<void*>(descriptor)),
0901         data(data),
0902         flags(WithNewSignatureFlag(flags)) {}
0903 
0904   // This variant will be deprecated soon.
0905   IndexedPropertyHandlerConfiguration(
0906       IndexedPropertyGetterCallback getter,          //
0907       IndexedPropertySetterCallback setter,          //
0908       IndexedPropertyQueryCallback query,            //
0909       IndexedPropertyDeleterCallback deleter,        //
0910       IndexedPropertyEnumeratorCallback enumerator,  //
0911       IndexedPropertyDefinerCallback definer,        //
0912       IndexedPropertyDescriptorCallback descriptor,  //
0913       Local<Value> data = Local<Value>(),
0914       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0915       : getter(reinterpret_cast<void*>(getter)),
0916         setter(reinterpret_cast<void*>(setter)),
0917         query(reinterpret_cast<void*>(query)),
0918         deleter(reinterpret_cast<void*>(deleter)),
0919         enumerator(enumerator),
0920         definer(reinterpret_cast<void*>(definer)),
0921         descriptor(reinterpret_cast<void*>(descriptor)),
0922         data(data),
0923         flags(flags) {}
0924 
0925   explicit IndexedPropertyHandlerConfiguration(
0926       IndexedPropertyGetterCallbackV2 getter = nullptr,
0927       IndexedPropertySetterCallbackV2 setter = nullptr,
0928       IndexedPropertyQueryCallbackV2 query = nullptr,
0929       IndexedPropertyDeleterCallbackV2 deleter = nullptr,
0930       IndexedPropertyEnumeratorCallback enumerator = nullptr,
0931       Local<Value> data = Local<Value>(),
0932       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0933       : getter(reinterpret_cast<void*>(getter)),
0934         setter(reinterpret_cast<void*>(setter)),
0935         query(reinterpret_cast<void*>(query)),
0936         deleter(reinterpret_cast<void*>(deleter)),
0937         enumerator(enumerator),
0938         definer(nullptr),
0939         descriptor(nullptr),
0940         data(data),
0941         flags(WithNewSignatureFlag(flags)) {}
0942 
0943   // This variant will be deprecated soon.
0944   explicit IndexedPropertyHandlerConfiguration(
0945       IndexedPropertyGetterCallback getter,
0946       IndexedPropertySetterCallback setter = nullptr,
0947       IndexedPropertyQueryCallback query = nullptr,
0948       IndexedPropertyDeleterCallback deleter = nullptr,
0949       IndexedPropertyEnumeratorCallback enumerator = nullptr,
0950       Local<Value> data = Local<Value>(),
0951       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0952       : getter(reinterpret_cast<void*>(getter)),
0953         setter(reinterpret_cast<void*>(setter)),
0954         query(reinterpret_cast<void*>(query)),
0955         deleter(reinterpret_cast<void*>(deleter)),
0956         enumerator(enumerator),
0957         definer(nullptr),
0958         descriptor(nullptr),
0959         data(data),
0960         flags(flags) {}
0961 
0962   IndexedPropertyHandlerConfiguration(
0963       IndexedPropertyGetterCallbackV2 getter,
0964       IndexedPropertySetterCallbackV2 setter,
0965       IndexedPropertyDescriptorCallbackV2 descriptor,
0966       IndexedPropertyDeleterCallbackV2 deleter,
0967       IndexedPropertyEnumeratorCallback enumerator,
0968       IndexedPropertyDefinerCallbackV2 definer,
0969       Local<Value> data = Local<Value>(),
0970       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0971       : getter(reinterpret_cast<void*>(getter)),
0972         setter(reinterpret_cast<void*>(setter)),
0973         query(nullptr),
0974         deleter(reinterpret_cast<void*>(deleter)),
0975         enumerator(enumerator),
0976         definer(reinterpret_cast<void*>(definer)),
0977         descriptor(reinterpret_cast<void*>(descriptor)),
0978         data(data),
0979         flags(WithNewSignatureFlag(flags)) {}
0980 
0981   // This variant will be deprecated soon.
0982   IndexedPropertyHandlerConfiguration(
0983       IndexedPropertyGetterCallback getter,
0984       IndexedPropertySetterCallback setter,
0985       IndexedPropertyDescriptorCallback descriptor,
0986       IndexedPropertyDeleterCallback deleter,
0987       IndexedPropertyEnumeratorCallback enumerator,
0988       IndexedPropertyDefinerCallback definer,
0989       Local<Value> data = Local<Value>(),
0990       PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
0991       : getter(reinterpret_cast<void*>(getter)),
0992         setter(reinterpret_cast<void*>(setter)),
0993         query(nullptr),
0994         deleter(reinterpret_cast<void*>(deleter)),
0995         enumerator(enumerator),
0996         definer(reinterpret_cast<void*>(definer)),
0997         descriptor(reinterpret_cast<void*>(descriptor)),
0998         data(data),
0999         flags(flags) {}
1000 
1001   void* getter;   // IndexedPropertyGetterCallback[V2]
1002   void* setter;   // IndexedPropertySetterCallback[V2]
1003   void* query;    // IndexedPropertyQueryCallback[V2]
1004   void* deleter;  // IndexedPropertyDeleterCallback[V2]
1005   IndexedPropertyEnumeratorCallback enumerator;
1006   void* definer;     // IndexedPropertyDefinerCallback[V2]
1007   void* descriptor;  // IndexedPropertyDescriptorCallback[V2]
1008   Local<Value> data;
1009   PropertyHandlerFlags flags;
1010 };
1011 
1012 /**
1013  * An ObjectTemplate is used to create objects at runtime.
1014  *
1015  * Properties added to an ObjectTemplate are added to each object
1016  * created from the ObjectTemplate.
1017  */
1018 class V8_EXPORT ObjectTemplate : public Template {
1019  public:
1020   /** Creates an ObjectTemplate. */
1021   static Local<ObjectTemplate> New(
1022       Isolate* isolate,
1023       Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
1024 
1025   /**
1026    * Creates a new instance of this template.
1027    *
1028    * \param context The context in which the instance is created.
1029    */
1030   V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
1031 
1032   /**
1033    * Sets an accessor on the object template.
1034    *
1035    * Whenever the property with the given name is accessed on objects
1036    * created from this ObjectTemplate the getter and setter callbacks
1037    * are called instead of getting and setting the property directly
1038    * on the JavaScript object.
1039    *
1040    * \param name The name of the property for which an accessor is added.
1041    * \param getter The callback to invoke when getting the property.
1042    * \param setter The callback to invoke when setting the property.
1043    * \param data A piece of data that will be passed to the getter and setter
1044    *   callbacks whenever they are invoked.
1045    * \param attribute The attributes of the property for which an accessor
1046    *   is added.
1047    */
1048   V8_DEPRECATE_SOON("Use SetAccessor with Local<Name> instead")
1049   void SetAccessor(
1050       Local<String> name, AccessorGetterCallback getter,
1051       AccessorSetterCallback setter = nullptr,
1052       Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
1053       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
1054       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
1055   void SetAccessor(
1056       Local<Name> name, AccessorNameGetterCallback getter,
1057       AccessorNameSetterCallback setter = nullptr,
1058       Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
1059       SideEffectType getter_side_effect_type = SideEffectType::kHasSideEffect,
1060       SideEffectType setter_side_effect_type = SideEffectType::kHasSideEffect);
1061 
1062   /**
1063    * Sets a named property handler on the object template.
1064    *
1065    * Whenever a property whose name is a string or a symbol is accessed on
1066    * objects created from this object template, the provided callback is
1067    * invoked instead of accessing the property directly on the JavaScript
1068    * object.
1069    *
1070    * @param configuration The NamedPropertyHandlerConfiguration that defines the
1071    * callbacks to invoke when accessing a property.
1072    */
1073   void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
1074 
1075   /**
1076    * Sets an indexed property handler on the object template.
1077    *
1078    * Whenever an indexed property is accessed on objects created from
1079    * this object template, the provided callback is invoked instead of
1080    * accessing the property directly on the JavaScript object.
1081    *
1082    * \param getter The callback to invoke when getting a property.
1083    * \param setter The callback to invoke when setting a property.
1084    * \param query The callback to invoke to check if an object has a property.
1085    * \param deleter The callback to invoke when deleting a property.
1086    * \param enumerator The callback to invoke to enumerate all the indexed
1087    *   properties of an object.
1088    * \param data A piece of data that will be passed to the callbacks
1089    *   whenever they are invoked.
1090    */
1091   V8_DEPRECATE_SOON("Use SetHandler instead")
1092   void SetIndexedPropertyHandler(
1093       IndexedPropertyGetterCallback getter,
1094       IndexedPropertySetterCallback setter = nullptr,
1095       IndexedPropertyQueryCallback query = nullptr,
1096       IndexedPropertyDeleterCallback deleter = nullptr,
1097       IndexedPropertyEnumeratorCallback enumerator = nullptr,
1098       Local<Value> data = Local<Value>()) {
1099     SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
1100                                                    deleter, enumerator, data));
1101   }
1102 
1103   /**
1104    * Sets an indexed property handler on the object template.
1105    *
1106    * Whenever an indexed property is accessed on objects created from
1107    * this object template, the provided callback is invoked instead of
1108    * accessing the property directly on the JavaScript object.
1109    *
1110    * @param configuration The IndexedPropertyHandlerConfiguration that defines
1111    * the callbacks to invoke when accessing a property.
1112    */
1113   void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
1114 
1115   /**
1116    * Sets the callback to be used when calling instances created from
1117    * this template as a function.  If no callback is set, instances
1118    * behave like normal JavaScript objects that cannot be called as a
1119    * function.
1120    */
1121   void SetCallAsFunctionHandler(FunctionCallback callback,
1122                                 Local<Value> data = Local<Value>());
1123 
1124   /**
1125    * Mark object instances of the template as undetectable.
1126    *
1127    * In many ways, undetectable objects behave as though they are not
1128    * there.  They behave like 'undefined' in conditionals and when
1129    * printed.  However, properties can be accessed and called as on
1130    * normal objects.
1131    */
1132   void MarkAsUndetectable();
1133 
1134   /**
1135    * Sets access check callback on the object template and enables access
1136    * checks.
1137    *
1138    * When accessing properties on instances of this object template,
1139    * the access check callback will be called to determine whether or
1140    * not to allow cross-context access to the properties.
1141    */
1142   void SetAccessCheckCallback(AccessCheckCallback callback,
1143                               Local<Value> data = Local<Value>());
1144 
1145   /**
1146    * Like SetAccessCheckCallback but invokes an interceptor on failed access
1147    * checks instead of looking up all-can-read properties. You can only use
1148    * either this method or SetAccessCheckCallback, but not both at the same
1149    * time.
1150    */
1151   void SetAccessCheckCallbackAndHandler(
1152       AccessCheckCallback callback,
1153       const NamedPropertyHandlerConfiguration& named_handler,
1154       const IndexedPropertyHandlerConfiguration& indexed_handler,
1155       Local<Value> data = Local<Value>());
1156 
1157   /**
1158    * Gets the number of internal fields for objects generated from
1159    * this template.
1160    */
1161   int InternalFieldCount() const;
1162 
1163   /**
1164    * Sets the number of internal fields for objects generated from
1165    * this template.
1166    */
1167   void SetInternalFieldCount(int value);
1168 
1169   /**
1170    * Returns true if the object will be an immutable prototype exotic object.
1171    */
1172   bool IsImmutableProto() const;
1173 
1174   /**
1175    * Makes the ObjectTemplate for an immutable prototype exotic object, with an
1176    * immutable __proto__.
1177    */
1178   void SetImmutableProto();
1179 
1180   /**
1181    * Support for TC39 "dynamic code brand checks" proposal.
1182    *
1183    * This API allows to mark (& query) objects as "code like", which causes
1184    * them to be treated like Strings in the context of eval and function
1185    * constructor.
1186    *
1187    * Reference: https://github.com/tc39/proposal-dynamic-code-brand-checks
1188    */
1189   void SetCodeLike();
1190   bool IsCodeLike() const;
1191 
1192   V8_INLINE static ObjectTemplate* Cast(Data* data);
1193 
1194  private:
1195   ObjectTemplate();
1196 
1197   static void CheckCast(Data* that);
1198   friend class FunctionTemplate;
1199 };
1200 
1201 /**
1202  * A template to create dictionary objects at runtime.
1203  */
1204 class V8_EXPORT DictionaryTemplate final {
1205  public:
1206   /** Creates a new template. Also declares data properties that can be passed
1207    * on instantiation of the template. Properties can only be declared on
1208    * construction and are then immutable. The values are passed on creating the
1209    * object via `NewInstance()`.
1210    *
1211    * \param names the keys that can be passed on instantiation.
1212    */
1213   static Local<DictionaryTemplate> New(
1214       Isolate* isolate, MemorySpan<const std::string_view> names);
1215 
1216   /**
1217    * Creates a new instance of this template.
1218    *
1219    * \param context The context used to create the dictionary object.
1220    * \param property_values Values of properties that were declared using
1221    *   `DeclareDataProperties()`. The span only passes values and expectes the
1222    *   order to match the declaration. Non-existent properties are signaled via
1223    *   empty `MaybeLocal`s.
1224    */
1225   V8_WARN_UNUSED_RESULT Local<Object> NewInstance(
1226       Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values);
1227 
1228   V8_INLINE static DictionaryTemplate* Cast(Data* data);
1229 
1230  private:
1231   static void CheckCast(Data* that);
1232 
1233   DictionaryTemplate();
1234 };
1235 
1236 /**
1237  * A Signature specifies which receiver is valid for a function.
1238  *
1239  * A receiver matches a given signature if the receiver (or any of its
1240  * hidden prototypes) was created from the signature's FunctionTemplate, or
1241  * from a FunctionTemplate that inherits directly or indirectly from the
1242  * signature's FunctionTemplate.
1243  */
1244 class V8_EXPORT Signature : public Data {
1245  public:
1246   static Local<Signature> New(
1247       Isolate* isolate,
1248       Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
1249 
1250   V8_INLINE static Signature* Cast(Data* data);
1251 
1252  private:
1253   Signature();
1254 
1255   static void CheckCast(Data* that);
1256 };
1257 
1258 // --- Implementation ---
1259 
1260 void Template::Set(Isolate* isolate, const char* name, Local<Data> value,
1261                    PropertyAttribute attributes) {
1262   Set(String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
1263           .ToLocalChecked(),
1264       value, attributes);
1265 }
1266 
1267 FunctionTemplate* FunctionTemplate::Cast(Data* data) {
1268 #ifdef V8_ENABLE_CHECKS
1269   CheckCast(data);
1270 #endif
1271   return reinterpret_cast<FunctionTemplate*>(data);
1272 }
1273 
1274 ObjectTemplate* ObjectTemplate::Cast(Data* data) {
1275 #ifdef V8_ENABLE_CHECKS
1276   CheckCast(data);
1277 #endif
1278   return reinterpret_cast<ObjectTemplate*>(data);
1279 }
1280 
1281 DictionaryTemplate* DictionaryTemplate::Cast(Data* data) {
1282 #ifdef V8_ENABLE_CHECKS
1283   CheckCast(data);
1284 #endif
1285   return reinterpret_cast<DictionaryTemplate*>(data);
1286 }
1287 
1288 Signature* Signature::Cast(Data* data) {
1289 #ifdef V8_ENABLE_CHECKS
1290   CheckCast(data);
1291 #endif
1292   return reinterpret_cast<Signature*>(data);
1293 }
1294 
1295 }  // namespace v8
1296 
1297 #endif  // INCLUDE_V8_TEMPLATE_H_