11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef INCLUDE_V8_WEAK_CALLBACK_INFO_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_V8_WEAK_CALLBACK_INFO_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cinamespace v8 { 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciclass Isolate; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace api_internal { 151cb0ef41Sopenharmony_ciV8_EXPORT void InternalFieldOutOfBounds(int index); 161cb0ef41Sopenharmony_ci} // namespace api_internal 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cistatic const int kInternalFieldsInWeakCallback = 2; 191cb0ef41Sopenharmony_cistatic const int kEmbedderFieldsInWeakCallback = 2; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_citemplate <typename T> 221cb0ef41Sopenharmony_ciclass WeakCallbackInfo { 231cb0ef41Sopenharmony_ci public: 241cb0ef41Sopenharmony_ci using Callback = void (*)(const WeakCallbackInfo<T>& data); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci WeakCallbackInfo(Isolate* isolate, T* parameter, 271cb0ef41Sopenharmony_ci void* embedder_fields[kEmbedderFieldsInWeakCallback], 281cb0ef41Sopenharmony_ci Callback* callback) 291cb0ef41Sopenharmony_ci : isolate_(isolate), parameter_(parameter), callback_(callback) { 301cb0ef41Sopenharmony_ci for (int i = 0; i < kEmbedderFieldsInWeakCallback; ++i) { 311cb0ef41Sopenharmony_ci embedder_fields_[i] = embedder_fields[i]; 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci V8_INLINE Isolate* GetIsolate() const { return isolate_; } 361cb0ef41Sopenharmony_ci V8_INLINE T* GetParameter() const { return parameter_; } 371cb0ef41Sopenharmony_ci V8_INLINE void* GetInternalField(int index) const; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // When first called, the embedder MUST Reset() the Global which triggered the 401cb0ef41Sopenharmony_ci // callback. The Global itself is unusable for anything else. No v8 other api 411cb0ef41Sopenharmony_ci // calls may be called in the first callback. Should additional work be 421cb0ef41Sopenharmony_ci // required, the embedder must set a second pass callback, which will be 431cb0ef41Sopenharmony_ci // called after all the initial callbacks are processed. 441cb0ef41Sopenharmony_ci // Calling SetSecondPassCallback on the second pass will immediately crash. 451cb0ef41Sopenharmony_ci void SetSecondPassCallback(Callback callback) const { *callback_ = callback; } 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci private: 481cb0ef41Sopenharmony_ci Isolate* isolate_; 491cb0ef41Sopenharmony_ci T* parameter_; 501cb0ef41Sopenharmony_ci Callback* callback_; 511cb0ef41Sopenharmony_ci void* embedder_fields_[kEmbedderFieldsInWeakCallback]; 521cb0ef41Sopenharmony_ci}; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci/** 551cb0ef41Sopenharmony_ci * Weakness type for weak handles. 561cb0ef41Sopenharmony_ci */ 571cb0ef41Sopenharmony_cienum class WeakCallbackType { 581cb0ef41Sopenharmony_ci /** 591cb0ef41Sopenharmony_ci * Passes a user-defined void* parameter back to the callback. 601cb0ef41Sopenharmony_ci */ 611cb0ef41Sopenharmony_ci kParameter, 621cb0ef41Sopenharmony_ci /** 631cb0ef41Sopenharmony_ci * Passes the first two internal fields of the object back to the callback. 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_ci kInternalFields, 661cb0ef41Sopenharmony_ci}; 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_citemplate <class T> 691cb0ef41Sopenharmony_civoid* WeakCallbackInfo<T>::GetInternalField(int index) const { 701cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 711cb0ef41Sopenharmony_ci if (index < 0 || index >= kEmbedderFieldsInWeakCallback) { 721cb0ef41Sopenharmony_ci api_internal::InternalFieldOutOfBounds(index); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci#endif 751cb0ef41Sopenharmony_ci return embedder_fields_[index]; 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci} // namespace v8 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci#endif // INCLUDE_V8_WEAK_CALLBACK_INFO_H_ 81