|
|
|||
File indexing completed on 2026-07-29 09:13:30
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 * This flag is used to distinguish which callbacks were provided - 0688 * GenericNamedPropertyXXXCallback (old signature) or 0689 * NamedPropertyXXXCallback (new signature). 0690 * DO NOT use this flag, it'll be removed once embedders migrate to new 0691 * callbacks signatures. 0692 */ 0693 kInternalNewCallbacksSignatures = 1 << 10, 0694 }; 0695 0696 struct NamedPropertyHandlerConfiguration { 0697 private: 0698 static constexpr PropertyHandlerFlags WithNewSignatureFlag( 0699 PropertyHandlerFlags flags) { 0700 return static_cast<PropertyHandlerFlags>( 0701 static_cast<int>(flags) | 0702 static_cast<int>( 0703 PropertyHandlerFlags::kInternalNewCallbacksSignatures)); 0704 } 0705 0706 public: 0707 NamedPropertyHandlerConfiguration( 0708 NamedPropertyGetterCallback getter, // 0709 NamedPropertySetterCallback setter, // 0710 NamedPropertyQueryCallback query, // 0711 NamedPropertyDeleterCallback deleter, // 0712 NamedPropertyEnumeratorCallback enumerator, // 0713 NamedPropertyDefinerCallback definer, // 0714 NamedPropertyDescriptorCallback descriptor, // 0715 Local<Value> data = Local<Value>(), 0716 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0717 : getter(getter), 0718 setter(setter), 0719 query(query), 0720 deleter(deleter), 0721 enumerator(enumerator), 0722 definer(definer), 0723 descriptor(descriptor), 0724 data(data), 0725 flags(flags) {} 0726 0727 explicit NamedPropertyHandlerConfiguration( 0728 NamedPropertyGetterCallback getter, 0729 NamedPropertySetterCallback setter = nullptr, 0730 NamedPropertyQueryCallback query = nullptr, 0731 NamedPropertyDeleterCallback deleter = nullptr, 0732 NamedPropertyEnumeratorCallback enumerator = nullptr, 0733 Local<Value> data = Local<Value>(), 0734 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0735 : getter(getter), 0736 setter(setter), 0737 query(query), 0738 deleter(deleter), 0739 enumerator(enumerator), 0740 definer(nullptr), 0741 descriptor(nullptr), 0742 data(data), 0743 flags(flags) {} 0744 0745 NamedPropertyHandlerConfiguration( 0746 NamedPropertyGetterCallback getter, // 0747 NamedPropertySetterCallback setter, // 0748 NamedPropertyDescriptorCallback descriptor, // 0749 NamedPropertyDeleterCallback deleter, // 0750 NamedPropertyEnumeratorCallback enumerator, // 0751 NamedPropertyDefinerCallback definer, // 0752 Local<Value> data = Local<Value>(), 0753 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0754 : getter(getter), 0755 setter(setter), 0756 query(nullptr), 0757 deleter(deleter), 0758 enumerator(enumerator), 0759 definer(definer), 0760 descriptor(descriptor), 0761 data(data), 0762 flags(flags) {} 0763 0764 NamedPropertyGetterCallback getter; 0765 NamedPropertySetterCallback setter; 0766 NamedPropertyQueryCallback query; 0767 NamedPropertyDeleterCallback deleter; 0768 NamedPropertyEnumeratorCallback enumerator; 0769 NamedPropertyDefinerCallback definer; 0770 NamedPropertyDescriptorCallback descriptor; 0771 Local<Value> data; 0772 PropertyHandlerFlags flags; 0773 }; 0774 0775 struct IndexedPropertyHandlerConfiguration { 0776 private: 0777 static constexpr PropertyHandlerFlags WithNewSignatureFlag( 0778 PropertyHandlerFlags flags) { 0779 return static_cast<PropertyHandlerFlags>( 0780 static_cast<int>(flags) | 0781 static_cast<int>( 0782 PropertyHandlerFlags::kInternalNewCallbacksSignatures)); 0783 } 0784 0785 public: 0786 IndexedPropertyHandlerConfiguration( 0787 IndexedPropertyGetterCallbackV2 getter, // 0788 IndexedPropertySetterCallbackV2 setter, // 0789 IndexedPropertyQueryCallbackV2 query, // 0790 IndexedPropertyDeleterCallbackV2 deleter, // 0791 IndexedPropertyEnumeratorCallback enumerator, // 0792 IndexedPropertyDefinerCallbackV2 definer, // 0793 IndexedPropertyDescriptorCallbackV2 descriptor, // 0794 Local<Value> data = Local<Value>(), 0795 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0796 : getter(getter), 0797 setter(setter), 0798 query(query), 0799 deleter(deleter), 0800 enumerator(enumerator), 0801 definer(definer), 0802 descriptor(descriptor), 0803 data(data), 0804 flags(flags) {} 0805 0806 explicit IndexedPropertyHandlerConfiguration( 0807 IndexedPropertyGetterCallbackV2 getter = nullptr, 0808 IndexedPropertySetterCallbackV2 setter = nullptr, 0809 IndexedPropertyQueryCallbackV2 query = nullptr, 0810 IndexedPropertyDeleterCallbackV2 deleter = nullptr, 0811 IndexedPropertyEnumeratorCallback enumerator = nullptr, 0812 Local<Value> data = Local<Value>(), 0813 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0814 : getter(getter), 0815 setter(setter), 0816 query(query), 0817 deleter(deleter), 0818 enumerator(enumerator), 0819 definer(nullptr), 0820 descriptor(nullptr), 0821 data(data), 0822 flags(flags) {} 0823 0824 IndexedPropertyHandlerConfiguration( 0825 IndexedPropertyGetterCallbackV2 getter, 0826 IndexedPropertySetterCallbackV2 setter, 0827 IndexedPropertyDescriptorCallbackV2 descriptor, 0828 IndexedPropertyDeleterCallbackV2 deleter, 0829 IndexedPropertyEnumeratorCallback enumerator, 0830 IndexedPropertyDefinerCallbackV2 definer, 0831 Local<Value> data = Local<Value>(), 0832 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) 0833 : getter(getter), 0834 setter(setter), 0835 query(nullptr), 0836 deleter(deleter), 0837 enumerator(enumerator), 0838 definer(definer), 0839 descriptor(descriptor), 0840 data(data), 0841 flags(flags) {} 0842 0843 IndexedPropertyGetterCallbackV2 getter; 0844 IndexedPropertySetterCallbackV2 setter; 0845 IndexedPropertyQueryCallbackV2 query; 0846 IndexedPropertyDeleterCallbackV2 deleter; 0847 IndexedPropertyEnumeratorCallback enumerator; 0848 IndexedPropertyDefinerCallbackV2 definer; 0849 IndexedPropertyDescriptorCallbackV2 descriptor; 0850 Local<Value> data; 0851 PropertyHandlerFlags flags; 0852 }; 0853 0854 /** 0855 * An ObjectTemplate is used to create objects at runtime. 0856 * 0857 * Properties added to an ObjectTemplate are added to each object 0858 * created from the ObjectTemplate. 0859 */ 0860 class V8_EXPORT ObjectTemplate : public Template { 0861 public: 0862 /** Creates an ObjectTemplate. */ 0863 static Local<ObjectTemplate> New( 0864 Isolate* isolate, 0865 Local<FunctionTemplate> constructor = Local<FunctionTemplate>()); 0866 0867 /** 0868 * Creates a new instance of this template. 0869 * 0870 * \param context The context in which the instance is created. 0871 */ 0872 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context); 0873 0874 /** 0875 * Sets a named property handler on the object template. 0876 * 0877 * Whenever a property whose name is a string or a symbol is accessed on 0878 * objects created from this object template, the provided callback is 0879 * invoked instead of accessing the property directly on the JavaScript 0880 * object. 0881 * 0882 * @param configuration The NamedPropertyHandlerConfiguration that defines the 0883 * callbacks to invoke when accessing a property. 0884 */ 0885 void SetHandler(const NamedPropertyHandlerConfiguration& configuration); 0886 0887 /** 0888 * Sets an indexed property handler on the object template. 0889 * 0890 * Whenever an indexed property is accessed on objects created from 0891 * this object template, the provided callback is invoked instead of 0892 * accessing the property directly on the JavaScript object. 0893 * 0894 * @param configuration The IndexedPropertyHandlerConfiguration that defines 0895 * the callbacks to invoke when accessing a property. 0896 */ 0897 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration); 0898 0899 /** 0900 * Sets the callback to be used when calling instances created from 0901 * this template as a function. If no callback is set, instances 0902 * behave like normal JavaScript objects that cannot be called as a 0903 * function. 0904 */ 0905 void SetCallAsFunctionHandler(FunctionCallback callback, 0906 Local<Value> data = Local<Value>()); 0907 0908 /** 0909 * Mark object instances of the template as undetectable. 0910 * 0911 * In many ways, undetectable objects behave as though they are not 0912 * there. They behave like 'undefined' in conditionals and when 0913 * printed. However, properties can be accessed and called as on 0914 * normal objects. 0915 */ 0916 void MarkAsUndetectable(); 0917 0918 /** 0919 * Sets access check callback on the object template and enables access 0920 * checks. 0921 * 0922 * When accessing properties on instances of this object template, 0923 * the access check callback will be called to determine whether or 0924 * not to allow cross-context access to the properties. 0925 */ 0926 void SetAccessCheckCallback(AccessCheckCallback callback, 0927 Local<Value> data = Local<Value>()); 0928 0929 /** 0930 * Like SetAccessCheckCallback but invokes an interceptor on failed access 0931 * checks instead of looking up all-can-read properties. You can only use 0932 * either this method or SetAccessCheckCallback, but not both at the same 0933 * time. 0934 */ 0935 void SetAccessCheckCallbackAndHandler( 0936 AccessCheckCallback callback, 0937 const NamedPropertyHandlerConfiguration& named_handler, 0938 const IndexedPropertyHandlerConfiguration& indexed_handler, 0939 Local<Value> data = Local<Value>()); 0940 0941 /** 0942 * Gets the number of internal fields for objects generated from 0943 * this template. 0944 */ 0945 int InternalFieldCount() const; 0946 0947 /** 0948 * Sets the number of internal fields for objects generated from 0949 * this template. 0950 */ 0951 void SetInternalFieldCount(int value); 0952 0953 /** 0954 * Returns true if the object will be an immutable prototype exotic object. 0955 */ 0956 bool IsImmutableProto() const; 0957 0958 /** 0959 * Makes the ObjectTemplate for an immutable prototype exotic object, with an 0960 * immutable __proto__. 0961 */ 0962 void SetImmutableProto(); 0963 0964 /** 0965 * Support for TC39 "dynamic code brand checks" proposal. 0966 * 0967 * This API allows to mark (& query) objects as "code like", which causes 0968 * them to be treated like Strings in the context of eval and function 0969 * constructor. 0970 * 0971 * Reference: https://github.com/tc39/proposal-dynamic-code-brand-checks 0972 */ 0973 void SetCodeLike(); 0974 bool IsCodeLike() const; 0975 0976 /** 0977 * Seal the object and mark it for promotion to read only space during 0978 * context snapshot creation. 0979 * 0980 * This is an experimental feature and may still change significantly. 0981 */ 0982 void SealAndPrepareForPromotionToReadOnly(); 0983 0984 V8_INLINE static ObjectTemplate* Cast(Data* data); 0985 0986 private: 0987 ObjectTemplate(); 0988 0989 static void CheckCast(Data* that); 0990 friend class FunctionTemplate; 0991 }; 0992 0993 /** 0994 * A template to create dictionary objects at runtime. 0995 */ 0996 class V8_EXPORT DictionaryTemplate final : public Data { 0997 public: 0998 /** Creates a new template. Also declares data properties that can be passed 0999 * on instantiation of the template. Properties can only be declared on 1000 * construction and are then immutable. The values are passed on creating the 1001 * object via `NewInstance()`. 1002 * 1003 * \param names the keys that can be passed on instantiation. 1004 */ 1005 static Local<DictionaryTemplate> New( 1006 Isolate* isolate, MemorySpan<const std::string_view> names); 1007 1008 /** 1009 * Creates a new instance of this template. 1010 * 1011 * \param context The context used to create the dictionary object. 1012 * \param property_values Values of properties that were declared using 1013 * `DeclareDataProperties()`. The span only passes values and expectes the 1014 * order to match the declaration. Non-existent properties are signaled via 1015 * empty `MaybeLocal`s. 1016 */ 1017 V8_WARN_UNUSED_RESULT Local<Object> NewInstance( 1018 Local<Context> context, MemorySpan<MaybeLocal<Value>> property_values); 1019 1020 V8_INLINE static DictionaryTemplate* Cast(Data* data); 1021 1022 private: 1023 static void CheckCast(Data* that); 1024 1025 DictionaryTemplate(); 1026 }; 1027 1028 /** 1029 * A Signature specifies which receiver is valid for a function. 1030 * 1031 * A receiver matches a given signature if the receiver (or any of its 1032 * hidden prototypes) was created from the signature's FunctionTemplate, or 1033 * from a FunctionTemplate that inherits directly or indirectly from the 1034 * signature's FunctionTemplate. 1035 */ 1036 class V8_EXPORT Signature : public Data { 1037 public: 1038 static Local<Signature> New( 1039 Isolate* isolate, 1040 Local<FunctionTemplate> receiver = Local<FunctionTemplate>()); 1041 1042 V8_INLINE static Signature* Cast(Data* data); 1043 1044 private: 1045 Signature(); 1046 1047 static void CheckCast(Data* that); 1048 }; 1049 1050 // --- Implementation --- 1051 1052 void Template::Set(Isolate* isolate, const char* name, Local<Data> value, 1053 PropertyAttribute attributes) { 1054 Set(String::NewFromUtf8(isolate, name, NewStringType::kInternalized) 1055 .ToLocalChecked(), 1056 value, attributes); 1057 } 1058 1059 FunctionTemplate* FunctionTemplate::Cast(Data* data) { 1060 #ifdef V8_ENABLE_CHECKS 1061 CheckCast(data); 1062 #endif 1063 return reinterpret_cast<FunctionTemplate*>(data); 1064 } 1065 1066 ObjectTemplate* ObjectTemplate::Cast(Data* data) { 1067 #ifdef V8_ENABLE_CHECKS 1068 CheckCast(data); 1069 #endif 1070 return reinterpret_cast<ObjectTemplate*>(data); 1071 } 1072 1073 DictionaryTemplate* DictionaryTemplate::Cast(Data* data) { 1074 #ifdef V8_ENABLE_CHECKS 1075 CheckCast(data); 1076 #endif 1077 return reinterpret_cast<DictionaryTemplate*>(data); 1078 } 1079 1080 Signature* Signature::Cast(Data* data) { 1081 #ifdef V8_ENABLE_CHECKS 1082 CheckCast(data); 1083 #endif 1084 return reinterpret_cast<Signature*>(data); 1085 } 1086 1087 } // namespace v8 1088 1089 #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 |
|