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_WEAK_CALLBACK_INFO_H_
0006 #define INCLUDE_V8_WEAK_CALLBACK_INFO_H_
0007 
0008 #include "v8config.h"  // NOLINT(build/include_directory)
0009 
0010 namespace v8 {
0011 
0012 class Isolate;
0013 
0014 namespace api_internal {
0015 V8_EXPORT void InternalFieldOutOfBounds(int index);
0016 }  // namespace api_internal
0017 
0018 static const int kInternalFieldsInWeakCallback = 2;
0019 static const int kEmbedderFieldsInWeakCallback = 2;
0020 
0021 template <typename T>
0022 class WeakCallbackInfo {
0023  public:
0024   using Callback = void (*)(const WeakCallbackInfo<T>& data);
0025 
0026   WeakCallbackInfo(Isolate* isolate, T* parameter,
0027                    void* embedder_fields[kEmbedderFieldsInWeakCallback],
0028                    Callback* callback)
0029       : isolate_(isolate), parameter_(parameter), callback_(callback) {
0030     for (int i = 0; i < kEmbedderFieldsInWeakCallback; ++i) {
0031       embedder_fields_[i] = embedder_fields[i];
0032     }
0033   }
0034 
0035   V8_INLINE Isolate* GetIsolate() const { return isolate_; }
0036   V8_INLINE T* GetParameter() const { return parameter_; }
0037   V8_INLINE void* GetInternalField(int index) const;
0038 
0039   // When first called, the embedder MUST Reset() the Global which triggered the
0040   // callback. The Global itself is unusable for anything else. No v8 other api
0041   // calls may be called in the first callback. Should additional work be
0042   // required, the embedder must set a second pass callback, which will be
0043   // called after all the initial callbacks are processed.
0044   // Calling SetSecondPassCallback on the second pass will immediately crash.
0045   void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
0046 
0047  private:
0048   Isolate* isolate_;
0049   T* parameter_;
0050   Callback* callback_;
0051   void* embedder_fields_[kEmbedderFieldsInWeakCallback];
0052 };
0053 
0054 /**
0055  * Weakness type for weak handles.
0056  */
0057 enum class WeakCallbackType {
0058   /**
0059    * Passes a user-defined void* parameter back to the callback.
0060    */
0061   kParameter,
0062   /**
0063    * Passes the first two internal fields of the object back to the callback.
0064    */
0065   kInternalFields,
0066 };
0067 
0068 template <class T>
0069 void* WeakCallbackInfo<T>::GetInternalField(int index) const {
0070 #ifdef V8_ENABLE_CHECKS
0071   if (index < 0 || index >= kEmbedderFieldsInWeakCallback) {
0072     api_internal::InternalFieldOutOfBounds(index);
0073   }
0074 #endif
0075   return embedder_fields_[index];
0076 }
0077 
0078 }  // namespace v8
0079 
0080 #endif  // INCLUDE_V8_WEAK_CALLBACK_INFO_H_