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