11cb0ef41Sopenharmony_ci// Copyright 2012 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 V8_API_API_H_ 61cb0ef41Sopenharmony_ci#define V8_API_API_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <memory> 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include "include/v8-container.h" 111cb0ef41Sopenharmony_ci#include "include/v8-external.h" 121cb0ef41Sopenharmony_ci#include "include/v8-proxy.h" 131cb0ef41Sopenharmony_ci#include "include/v8-typed-array.h" 141cb0ef41Sopenharmony_ci#include "include/v8-wasm.h" 151cb0ef41Sopenharmony_ci#include "src/execution/isolate.h" 161cb0ef41Sopenharmony_ci#include "src/heap/factory.h" 171cb0ef41Sopenharmony_ci#include "src/objects/bigint.h" 181cb0ef41Sopenharmony_ci#include "src/objects/contexts.h" 191cb0ef41Sopenharmony_ci#include "src/objects/js-collection.h" 201cb0ef41Sopenharmony_ci#include "src/objects/js-generator.h" 211cb0ef41Sopenharmony_ci#include "src/objects/js-promise.h" 221cb0ef41Sopenharmony_ci#include "src/objects/js-proxy.h" 231cb0ef41Sopenharmony_ci#include "src/objects/objects.h" 241cb0ef41Sopenharmony_ci#include "src/objects/shared-function-info.h" 251cb0ef41Sopenharmony_ci#include "src/objects/source-text-module.h" 261cb0ef41Sopenharmony_ci#include "src/objects/templates.h" 271cb0ef41Sopenharmony_ci#include "src/utils/detachable-vector.h" 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cinamespace v8 { 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciclass AccessorSignature; 321cb0ef41Sopenharmony_ciclass Extension; 331cb0ef41Sopenharmony_ciclass Signature; 341cb0ef41Sopenharmony_ciclass Template; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cinamespace internal { 371cb0ef41Sopenharmony_ciclass JSArrayBufferView; 381cb0ef41Sopenharmony_ciclass JSFinalizationRegistry; 391cb0ef41Sopenharmony_ci} // namespace internal 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cinamespace debug { 421cb0ef41Sopenharmony_ciclass AccessorPair; 431cb0ef41Sopenharmony_ciclass GeneratorObject; 441cb0ef41Sopenharmony_ciclass ScriptSource; 451cb0ef41Sopenharmony_ciclass Script; 461cb0ef41Sopenharmony_ciclass EphemeronTable; 471cb0ef41Sopenharmony_ci} // namespace debug 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci// Constants used in the implementation of the API. The most natural thing 501cb0ef41Sopenharmony_ci// would usually be to place these with the classes that use them, but 511cb0ef41Sopenharmony_ci// we want to keep them out of v8.h because it is an externally 521cb0ef41Sopenharmony_ci// visible file. 531cb0ef41Sopenharmony_ciclass Consts { 541cb0ef41Sopenharmony_ci public: 551cb0ef41Sopenharmony_ci enum TemplateType { FUNCTION_TEMPLATE = 0, OBJECT_TEMPLATE = 1 }; 561cb0ef41Sopenharmony_ci}; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_citemplate <typename T> 591cb0ef41Sopenharmony_ciinline T ToCData(v8::internal::Object obj); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_citemplate <> 621cb0ef41Sopenharmony_ciinline v8::internal::Address ToCData(v8::internal::Object obj); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_citemplate <typename T> 651cb0ef41Sopenharmony_ciinline v8::internal::Handle<v8::internal::Object> FromCData( 661cb0ef41Sopenharmony_ci v8::internal::Isolate* isolate, T obj); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_citemplate <> 691cb0ef41Sopenharmony_ciinline v8::internal::Handle<v8::internal::Object> FromCData( 701cb0ef41Sopenharmony_ci v8::internal::Isolate* isolate, v8::internal::Address obj); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciclass ApiFunction { 731cb0ef41Sopenharmony_ci public: 741cb0ef41Sopenharmony_ci explicit ApiFunction(v8::internal::Address addr) : addr_(addr) {} 751cb0ef41Sopenharmony_ci v8::internal::Address address() { return addr_; } 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci private: 781cb0ef41Sopenharmony_ci v8::internal::Address addr_; 791cb0ef41Sopenharmony_ci}; 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciclass RegisteredExtension { 821cb0ef41Sopenharmony_ci public: 831cb0ef41Sopenharmony_ci static void Register(std::unique_ptr<Extension>); 841cb0ef41Sopenharmony_ci static void UnregisterAll(); 851cb0ef41Sopenharmony_ci Extension* extension() const { return extension_.get(); } 861cb0ef41Sopenharmony_ci RegisteredExtension* next() const { return next_; } 871cb0ef41Sopenharmony_ci static RegisteredExtension* first_extension() { return first_extension_; } 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci private: 901cb0ef41Sopenharmony_ci explicit RegisteredExtension(Extension*); 911cb0ef41Sopenharmony_ci explicit RegisteredExtension(std::unique_ptr<Extension>); 921cb0ef41Sopenharmony_ci std::unique_ptr<Extension> extension_; 931cb0ef41Sopenharmony_ci RegisteredExtension* next_ = nullptr; 941cb0ef41Sopenharmony_ci static RegisteredExtension* first_extension_; 951cb0ef41Sopenharmony_ci}; 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci#define OPEN_HANDLE_LIST(V) \ 981cb0ef41Sopenharmony_ci V(Template, TemplateInfo) \ 991cb0ef41Sopenharmony_ci V(FunctionTemplate, FunctionTemplateInfo) \ 1001cb0ef41Sopenharmony_ci V(ObjectTemplate, ObjectTemplateInfo) \ 1011cb0ef41Sopenharmony_ci V(Signature, FunctionTemplateInfo) \ 1021cb0ef41Sopenharmony_ci V(AccessorSignature, FunctionTemplateInfo) \ 1031cb0ef41Sopenharmony_ci V(Data, Object) \ 1041cb0ef41Sopenharmony_ci V(RegExp, JSRegExp) \ 1051cb0ef41Sopenharmony_ci V(Object, JSReceiver) \ 1061cb0ef41Sopenharmony_ci V(Array, JSArray) \ 1071cb0ef41Sopenharmony_ci V(Map, JSMap) \ 1081cb0ef41Sopenharmony_ci V(Set, JSSet) \ 1091cb0ef41Sopenharmony_ci V(ArrayBuffer, JSArrayBuffer) \ 1101cb0ef41Sopenharmony_ci V(ArrayBufferView, JSArrayBufferView) \ 1111cb0ef41Sopenharmony_ci V(TypedArray, JSTypedArray) \ 1121cb0ef41Sopenharmony_ci V(Uint8Array, JSTypedArray) \ 1131cb0ef41Sopenharmony_ci V(Uint8ClampedArray, JSTypedArray) \ 1141cb0ef41Sopenharmony_ci V(Int8Array, JSTypedArray) \ 1151cb0ef41Sopenharmony_ci V(Uint16Array, JSTypedArray) \ 1161cb0ef41Sopenharmony_ci V(Int16Array, JSTypedArray) \ 1171cb0ef41Sopenharmony_ci V(Uint32Array, JSTypedArray) \ 1181cb0ef41Sopenharmony_ci V(Int32Array, JSTypedArray) \ 1191cb0ef41Sopenharmony_ci V(Float32Array, JSTypedArray) \ 1201cb0ef41Sopenharmony_ci V(Float64Array, JSTypedArray) \ 1211cb0ef41Sopenharmony_ci V(DataView, JSDataView) \ 1221cb0ef41Sopenharmony_ci V(SharedArrayBuffer, JSArrayBuffer) \ 1231cb0ef41Sopenharmony_ci V(Name, Name) \ 1241cb0ef41Sopenharmony_ci V(String, String) \ 1251cb0ef41Sopenharmony_ci V(Symbol, Symbol) \ 1261cb0ef41Sopenharmony_ci V(Script, JSFunction) \ 1271cb0ef41Sopenharmony_ci V(UnboundModuleScript, SharedFunctionInfo) \ 1281cb0ef41Sopenharmony_ci V(UnboundScript, SharedFunctionInfo) \ 1291cb0ef41Sopenharmony_ci V(Module, Module) \ 1301cb0ef41Sopenharmony_ci V(Function, JSReceiver) \ 1311cb0ef41Sopenharmony_ci V(Message, JSMessageObject) \ 1321cb0ef41Sopenharmony_ci V(Context, Context) \ 1331cb0ef41Sopenharmony_ci V(External, Object) \ 1341cb0ef41Sopenharmony_ci V(StackTrace, FixedArray) \ 1351cb0ef41Sopenharmony_ci V(StackFrame, StackFrameInfo) \ 1361cb0ef41Sopenharmony_ci V(Proxy, JSProxy) \ 1371cb0ef41Sopenharmony_ci V(debug::GeneratorObject, JSGeneratorObject) \ 1381cb0ef41Sopenharmony_ci V(debug::ScriptSource, HeapObject) \ 1391cb0ef41Sopenharmony_ci V(debug::Script, Script) \ 1401cb0ef41Sopenharmony_ci V(debug::EphemeronTable, EphemeronHashTable) \ 1411cb0ef41Sopenharmony_ci V(debug::AccessorPair, AccessorPair) \ 1421cb0ef41Sopenharmony_ci V(Promise, JSPromise) \ 1431cb0ef41Sopenharmony_ci V(Primitive, Object) \ 1441cb0ef41Sopenharmony_ci V(PrimitiveArray, FixedArray) \ 1451cb0ef41Sopenharmony_ci V(BigInt, BigInt) \ 1461cb0ef41Sopenharmony_ci V(ScriptOrModule, ScriptOrModule) \ 1471cb0ef41Sopenharmony_ci V(FixedArray, FixedArray) \ 1481cb0ef41Sopenharmony_ci V(ModuleRequest, ModuleRequest) \ 1491cb0ef41Sopenharmony_ci IF_WASM(V, WasmMemoryObject, WasmMemoryObject) 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciclass Utils { 1521cb0ef41Sopenharmony_ci public: 1531cb0ef41Sopenharmony_ci static inline bool ApiCheck(bool condition, const char* location, 1541cb0ef41Sopenharmony_ci const char* message) { 1551cb0ef41Sopenharmony_ci if (!condition) Utils::ReportApiFailure(location, message); 1561cb0ef41Sopenharmony_ci return condition; 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci static void ReportOOMFailure(v8::internal::Isolate* isolate, 1591cb0ef41Sopenharmony_ci const char* location, bool is_heap_oom); 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci static inline Local<debug::AccessorPair> ToLocal( 1621cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::AccessorPair> obj); 1631cb0ef41Sopenharmony_ci static inline Local<Context> ToLocal( 1641cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Context> obj); 1651cb0ef41Sopenharmony_ci static inline Local<Value> ToLocal( 1661cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 1671cb0ef41Sopenharmony_ci static inline Local<Module> ToLocal( 1681cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Module> obj); 1691cb0ef41Sopenharmony_ci static inline Local<Name> ToLocal( 1701cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Name> obj); 1711cb0ef41Sopenharmony_ci static inline Local<String> ToLocal( 1721cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::String> obj); 1731cb0ef41Sopenharmony_ci static inline Local<Symbol> ToLocal( 1741cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Symbol> obj); 1751cb0ef41Sopenharmony_ci static inline Local<RegExp> ToLocal( 1761cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSRegExp> obj); 1771cb0ef41Sopenharmony_ci static inline Local<Object> ToLocal( 1781cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSReceiver> obj); 1791cb0ef41Sopenharmony_ci static inline Local<Object> ToLocal( 1801cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSObject> obj); 1811cb0ef41Sopenharmony_ci static inline Local<Function> ToLocal( 1821cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSFunction> obj); 1831cb0ef41Sopenharmony_ci static inline Local<Array> ToLocal( 1841cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSArray> obj); 1851cb0ef41Sopenharmony_ci static inline Local<Map> ToLocal( 1861cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSMap> obj); 1871cb0ef41Sopenharmony_ci static inline Local<Set> ToLocal( 1881cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSSet> obj); 1891cb0ef41Sopenharmony_ci static inline Local<Proxy> ToLocal( 1901cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSProxy> obj); 1911cb0ef41Sopenharmony_ci static inline Local<ArrayBuffer> ToLocal( 1921cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSArrayBuffer> obj); 1931cb0ef41Sopenharmony_ci static inline Local<ArrayBufferView> ToLocal( 1941cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSArrayBufferView> obj); 1951cb0ef41Sopenharmony_ci static inline Local<DataView> ToLocal( 1961cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSDataView> obj); 1971cb0ef41Sopenharmony_ci static inline Local<TypedArray> ToLocal( 1981cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 1991cb0ef41Sopenharmony_ci static inline Local<Uint8Array> ToLocalUint8Array( 2001cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2011cb0ef41Sopenharmony_ci static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray( 2021cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2031cb0ef41Sopenharmony_ci static inline Local<Int8Array> ToLocalInt8Array( 2041cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2051cb0ef41Sopenharmony_ci static inline Local<Uint16Array> ToLocalUint16Array( 2061cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2071cb0ef41Sopenharmony_ci static inline Local<Int16Array> ToLocalInt16Array( 2081cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2091cb0ef41Sopenharmony_ci static inline Local<Uint32Array> ToLocalUint32Array( 2101cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2111cb0ef41Sopenharmony_ci static inline Local<Int32Array> ToLocalInt32Array( 2121cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2131cb0ef41Sopenharmony_ci static inline Local<Float32Array> ToLocalFloat32Array( 2141cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2151cb0ef41Sopenharmony_ci static inline Local<Float64Array> ToLocalFloat64Array( 2161cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2171cb0ef41Sopenharmony_ci static inline Local<BigInt64Array> ToLocalBigInt64Array( 2181cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2191cb0ef41Sopenharmony_ci static inline Local<BigUint64Array> ToLocalBigUint64Array( 2201cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSTypedArray> obj); 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ci static inline Local<SharedArrayBuffer> ToLocalShared( 2231cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSArrayBuffer> obj); 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci static inline Local<Message> MessageToLocal( 2261cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 2271cb0ef41Sopenharmony_ci static inline Local<Promise> PromiseToLocal( 2281cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSObject> obj); 2291cb0ef41Sopenharmony_ci static inline Local<StackTrace> StackTraceToLocal( 2301cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FixedArray> obj); 2311cb0ef41Sopenharmony_ci static inline Local<StackFrame> StackFrameToLocal( 2321cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::StackFrameInfo> obj); 2331cb0ef41Sopenharmony_ci static inline Local<Number> NumberToLocal( 2341cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 2351cb0ef41Sopenharmony_ci static inline Local<Integer> IntegerToLocal( 2361cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 2371cb0ef41Sopenharmony_ci static inline Local<Uint32> Uint32ToLocal( 2381cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 2391cb0ef41Sopenharmony_ci static inline Local<BigInt> ToLocal( 2401cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::BigInt> obj); 2411cb0ef41Sopenharmony_ci static inline Local<FunctionTemplate> ToLocal( 2421cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); 2431cb0ef41Sopenharmony_ci static inline Local<ObjectTemplate> ToLocal( 2441cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj); 2451cb0ef41Sopenharmony_ci static inline Local<Signature> SignatureToLocal( 2461cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); 2471cb0ef41Sopenharmony_ci static inline Local<AccessorSignature> AccessorSignatureToLocal( 2481cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj); 2491cb0ef41Sopenharmony_ci static inline Local<External> ExternalToLocal( 2501cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSObject> obj); 2511cb0ef41Sopenharmony_ci static inline Local<Function> CallableToLocal( 2521cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::JSReceiver> obj); 2531cb0ef41Sopenharmony_ci static inline Local<Primitive> ToLocalPrimitive( 2541cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj); 2551cb0ef41Sopenharmony_ci static inline Local<FixedArray> FixedArrayToLocal( 2561cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FixedArray> obj); 2571cb0ef41Sopenharmony_ci static inline Local<PrimitiveArray> PrimitiveArrayToLocal( 2581cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::FixedArray> obj); 2591cb0ef41Sopenharmony_ci static inline Local<ScriptOrModule> ToLocal( 2601cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::ScriptOrModule> obj); 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci#define DECLARE_OPEN_HANDLE(From, To) \ 2631cb0ef41Sopenharmony_ci static inline v8::internal::Handle<v8::internal::To> OpenHandle( \ 2641cb0ef41Sopenharmony_ci const From* that, bool allow_empty_handle = false); 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE) 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ci#undef DECLARE_OPEN_HANDLE 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci template <class From, class To> 2711cb0ef41Sopenharmony_ci static inline Local<To> Convert(v8::internal::Handle<From> obj); 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci template <class T, class M> 2741cb0ef41Sopenharmony_ci static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( 2751cb0ef41Sopenharmony_ci const v8::Persistent<T, M>& persistent) { 2761cb0ef41Sopenharmony_ci return v8::internal::Handle<v8::internal::Object>( 2771cb0ef41Sopenharmony_ci reinterpret_cast<v8::internal::Address*>(persistent.val_)); 2781cb0ef41Sopenharmony_ci } 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci template <class T> 2811cb0ef41Sopenharmony_ci static inline v8::internal::Handle<v8::internal::Object> OpenPersistent( 2821cb0ef41Sopenharmony_ci v8::Persistent<T>* persistent) { 2831cb0ef41Sopenharmony_ci return OpenPersistent(*persistent); 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci template <class From, class To> 2871cb0ef41Sopenharmony_ci static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) { 2881cb0ef41Sopenharmony_ci return OpenHandle(*handle); 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci private: 2921cb0ef41Sopenharmony_ci static void ReportApiFailure(const char* location, const char* message); 2931cb0ef41Sopenharmony_ci}; 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_citemplate <class T> 2961cb0ef41Sopenharmony_ciinline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) { 2971cb0ef41Sopenharmony_ci return reinterpret_cast<T*>(obj.location()); 2981cb0ef41Sopenharmony_ci} 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_citemplate <class T> 3011cb0ef41Sopenharmony_ciinline v8::Local<T> ToApiHandle( 3021cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> obj) { 3031cb0ef41Sopenharmony_ci return Utils::Convert<v8::internal::Object, T>(obj); 3041cb0ef41Sopenharmony_ci} 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_citemplate <class T> 3071cb0ef41Sopenharmony_ciinline bool ToLocal(v8::internal::MaybeHandle<v8::internal::Object> maybe, 3081cb0ef41Sopenharmony_ci Local<T>* local) { 3091cb0ef41Sopenharmony_ci v8::internal::Handle<v8::internal::Object> handle; 3101cb0ef41Sopenharmony_ci if (maybe.ToHandle(&handle)) { 3111cb0ef41Sopenharmony_ci *local = Utils::Convert<v8::internal::Object, T>(handle); 3121cb0ef41Sopenharmony_ci return true; 3131cb0ef41Sopenharmony_ci } 3141cb0ef41Sopenharmony_ci return false; 3151cb0ef41Sopenharmony_ci} 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_cinamespace internal { 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ciclass PersistentHandles; 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci// This class is here in order to be able to declare it a friend of 3221cb0ef41Sopenharmony_ci// HandleScope. Moving these methods to be members of HandleScope would be 3231cb0ef41Sopenharmony_ci// neat in some ways, but it would expose internal implementation details in 3241cb0ef41Sopenharmony_ci// our public header file, which is undesirable. 3251cb0ef41Sopenharmony_ci// 3261cb0ef41Sopenharmony_ci// An isolate has a single instance of this class to hold the current thread's 3271cb0ef41Sopenharmony_ci// data. In multithreaded V8 programs this data is copied in and out of storage 3281cb0ef41Sopenharmony_ci// so that the currently executing thread always has its own copy of this 3291cb0ef41Sopenharmony_ci// data. 3301cb0ef41Sopenharmony_ciclass HandleScopeImplementer { 3311cb0ef41Sopenharmony_ci public: 3321cb0ef41Sopenharmony_ci class V8_NODISCARD EnteredContextRewindScope { 3331cb0ef41Sopenharmony_ci public: 3341cb0ef41Sopenharmony_ci explicit EnteredContextRewindScope(HandleScopeImplementer* hsi) 3351cb0ef41Sopenharmony_ci : hsi_(hsi), saved_entered_context_count_(hsi->EnteredContextCount()) {} 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci ~EnteredContextRewindScope() { 3381cb0ef41Sopenharmony_ci DCHECK_LE(saved_entered_context_count_, hsi_->EnteredContextCount()); 3391cb0ef41Sopenharmony_ci while (saved_entered_context_count_ < hsi_->EnteredContextCount()) 3401cb0ef41Sopenharmony_ci hsi_->LeaveContext(); 3411cb0ef41Sopenharmony_ci } 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci private: 3441cb0ef41Sopenharmony_ci HandleScopeImplementer* hsi_; 3451cb0ef41Sopenharmony_ci size_t saved_entered_context_count_; 3461cb0ef41Sopenharmony_ci }; 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci explicit HandleScopeImplementer(Isolate* isolate) 3491cb0ef41Sopenharmony_ci : isolate_(isolate), 3501cb0ef41Sopenharmony_ci spare_(nullptr), 3511cb0ef41Sopenharmony_ci last_handle_before_deferred_block_(nullptr) {} 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ci ~HandleScopeImplementer() { DeleteArray(spare_); } 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci HandleScopeImplementer(const HandleScopeImplementer&) = delete; 3561cb0ef41Sopenharmony_ci HandleScopeImplementer& operator=(const HandleScopeImplementer&) = delete; 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ci // Threading support for handle data. 3591cb0ef41Sopenharmony_ci static int ArchiveSpacePerThread(); 3601cb0ef41Sopenharmony_ci char* RestoreThread(char* from); 3611cb0ef41Sopenharmony_ci char* ArchiveThread(char* to); 3621cb0ef41Sopenharmony_ci void FreeThreadResources(); 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci // Garbage collection support. 3651cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE void Iterate(v8::internal::RootVisitor* v); 3661cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static char* Iterate(v8::internal::RootVisitor* v, 3671cb0ef41Sopenharmony_ci char* data); 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci inline internal::Address* GetSpareOrNewBlock(); 3701cb0ef41Sopenharmony_ci inline void DeleteExtensions(internal::Address* prev_limit); 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci inline void EnterContext(Context context); 3731cb0ef41Sopenharmony_ci inline void LeaveContext(); 3741cb0ef41Sopenharmony_ci inline bool LastEnteredContextWas(Context context); 3751cb0ef41Sopenharmony_ci inline size_t EnteredContextCount() const { return entered_contexts_.size(); } 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci inline void EnterMicrotaskContext(Context context); 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ci // Returns the last entered context or an empty handle if no 3801cb0ef41Sopenharmony_ci // contexts have been entered. 3811cb0ef41Sopenharmony_ci inline Handle<Context> LastEnteredContext(); 3821cb0ef41Sopenharmony_ci inline Handle<Context> LastEnteredOrMicrotaskContext(); 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci inline void SaveContext(Context context); 3851cb0ef41Sopenharmony_ci inline Context RestoreContext(); 3861cb0ef41Sopenharmony_ci inline bool HasSavedContexts(); 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci inline DetachableVector<Address*>* blocks() { return &blocks_; } 3891cb0ef41Sopenharmony_ci Isolate* isolate() const { return isolate_; } 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ci void ReturnBlock(Address* block) { 3921cb0ef41Sopenharmony_ci DCHECK_NOT_NULL(block); 3931cb0ef41Sopenharmony_ci if (spare_ != nullptr) DeleteArray(spare_); 3941cb0ef41Sopenharmony_ci spare_ = block; 3951cb0ef41Sopenharmony_ci } 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ci static const size_t kEnteredContextsOffset; 3981cb0ef41Sopenharmony_ci static const size_t kIsMicrotaskContextOffset; 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci private: 4011cb0ef41Sopenharmony_ci void ResetAfterArchive() { 4021cb0ef41Sopenharmony_ci blocks_.detach(); 4031cb0ef41Sopenharmony_ci entered_contexts_.detach(); 4041cb0ef41Sopenharmony_ci is_microtask_context_.detach(); 4051cb0ef41Sopenharmony_ci saved_contexts_.detach(); 4061cb0ef41Sopenharmony_ci spare_ = nullptr; 4071cb0ef41Sopenharmony_ci last_handle_before_deferred_block_ = nullptr; 4081cb0ef41Sopenharmony_ci } 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci void Free() { 4111cb0ef41Sopenharmony_ci DCHECK(blocks_.empty()); 4121cb0ef41Sopenharmony_ci DCHECK(entered_contexts_.empty()); 4131cb0ef41Sopenharmony_ci DCHECK(is_microtask_context_.empty()); 4141cb0ef41Sopenharmony_ci DCHECK(saved_contexts_.empty()); 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ci blocks_.free(); 4171cb0ef41Sopenharmony_ci entered_contexts_.free(); 4181cb0ef41Sopenharmony_ci is_microtask_context_.free(); 4191cb0ef41Sopenharmony_ci saved_contexts_.free(); 4201cb0ef41Sopenharmony_ci if (spare_ != nullptr) { 4211cb0ef41Sopenharmony_ci DeleteArray(spare_); 4221cb0ef41Sopenharmony_ci spare_ = nullptr; 4231cb0ef41Sopenharmony_ci } 4241cb0ef41Sopenharmony_ci DCHECK(isolate_->thread_local_top()->CallDepthIsZero()); 4251cb0ef41Sopenharmony_ci } 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_ci void BeginDeferredScope(); 4281cb0ef41Sopenharmony_ci std::unique_ptr<PersistentHandles> DetachPersistent(Address* prev_limit); 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci Isolate* isolate_; 4311cb0ef41Sopenharmony_ci DetachableVector<Address*> blocks_; 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci // Used as a stack to keep track of entered contexts. 4341cb0ef41Sopenharmony_ci // If |i|th item of |entered_contexts_| is added by EnterMicrotaskContext, 4351cb0ef41Sopenharmony_ci // `is_microtask_context_[i]` is 1. 4361cb0ef41Sopenharmony_ci // TODO(tzik): Remove |is_microtask_context_| after the deprecated 4371cb0ef41Sopenharmony_ci // v8::Isolate::GetEnteredContext() is removed. 4381cb0ef41Sopenharmony_ci DetachableVector<Context> entered_contexts_; 4391cb0ef41Sopenharmony_ci DetachableVector<int8_t> is_microtask_context_; 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_ci // Used as a stack to keep track of saved contexts. 4421cb0ef41Sopenharmony_ci DetachableVector<Context> saved_contexts_; 4431cb0ef41Sopenharmony_ci Address* spare_; 4441cb0ef41Sopenharmony_ci Address* last_handle_before_deferred_block_; 4451cb0ef41Sopenharmony_ci // This is only used for threading support. 4461cb0ef41Sopenharmony_ci HandleScopeData handle_scope_data_; 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ci void IterateThis(RootVisitor* v); 4491cb0ef41Sopenharmony_ci char* RestoreThreadHelper(char* from); 4501cb0ef41Sopenharmony_ci char* ArchiveThreadHelper(char* to); 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci friend class HandleScopeImplementerOffsets; 4531cb0ef41Sopenharmony_ci friend class PersistentHandlesScope; 4541cb0ef41Sopenharmony_ci}; 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ciconst int kHandleBlockSize = v8::internal::KB - 2; // fit in one page 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_civoid HandleScopeImplementer::SaveContext(Context context) { 4591cb0ef41Sopenharmony_ci saved_contexts_.push_back(context); 4601cb0ef41Sopenharmony_ci} 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ciContext HandleScopeImplementer::RestoreContext() { 4631cb0ef41Sopenharmony_ci Context last_context = saved_contexts_.back(); 4641cb0ef41Sopenharmony_ci saved_contexts_.pop_back(); 4651cb0ef41Sopenharmony_ci return last_context; 4661cb0ef41Sopenharmony_ci} 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_cibool HandleScopeImplementer::HasSavedContexts() { 4691cb0ef41Sopenharmony_ci return !saved_contexts_.empty(); 4701cb0ef41Sopenharmony_ci} 4711cb0ef41Sopenharmony_ci 4721cb0ef41Sopenharmony_civoid HandleScopeImplementer::LeaveContext() { 4731cb0ef41Sopenharmony_ci DCHECK(!entered_contexts_.empty()); 4741cb0ef41Sopenharmony_ci DCHECK_EQ(entered_contexts_.capacity(), is_microtask_context_.capacity()); 4751cb0ef41Sopenharmony_ci DCHECK_EQ(entered_contexts_.size(), is_microtask_context_.size()); 4761cb0ef41Sopenharmony_ci entered_contexts_.pop_back(); 4771cb0ef41Sopenharmony_ci is_microtask_context_.pop_back(); 4781cb0ef41Sopenharmony_ci} 4791cb0ef41Sopenharmony_ci 4801cb0ef41Sopenharmony_cibool HandleScopeImplementer::LastEnteredContextWas(Context context) { 4811cb0ef41Sopenharmony_ci return !entered_contexts_.empty() && entered_contexts_.back() == context; 4821cb0ef41Sopenharmony_ci} 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci// If there's a spare block, use it for growing the current scope. 4851cb0ef41Sopenharmony_ciinternal::Address* HandleScopeImplementer::GetSpareOrNewBlock() { 4861cb0ef41Sopenharmony_ci internal::Address* block = 4871cb0ef41Sopenharmony_ci (spare_ != nullptr) ? spare_ 4881cb0ef41Sopenharmony_ci : NewArray<internal::Address>(kHandleBlockSize); 4891cb0ef41Sopenharmony_ci spare_ = nullptr; 4901cb0ef41Sopenharmony_ci return block; 4911cb0ef41Sopenharmony_ci} 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_civoid HandleScopeImplementer::DeleteExtensions(internal::Address* prev_limit) { 4941cb0ef41Sopenharmony_ci while (!blocks_.empty()) { 4951cb0ef41Sopenharmony_ci internal::Address* block_start = blocks_.back(); 4961cb0ef41Sopenharmony_ci internal::Address* block_limit = block_start + kHandleBlockSize; 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci // SealHandleScope may make the prev_limit to point inside the block. 4991cb0ef41Sopenharmony_ci // Cast possibly-unrelated pointers to plain Addres before comparing them 5001cb0ef41Sopenharmony_ci // to avoid undefined behavior. 5011cb0ef41Sopenharmony_ci if (reinterpret_cast<Address>(block_start) <= 5021cb0ef41Sopenharmony_ci reinterpret_cast<Address>(prev_limit) && 5031cb0ef41Sopenharmony_ci reinterpret_cast<Address>(prev_limit) <= 5041cb0ef41Sopenharmony_ci reinterpret_cast<Address>(block_limit)) { 5051cb0ef41Sopenharmony_ci#ifdef ENABLE_HANDLE_ZAPPING 5061cb0ef41Sopenharmony_ci internal::HandleScope::ZapRange(prev_limit, block_limit); 5071cb0ef41Sopenharmony_ci#endif 5081cb0ef41Sopenharmony_ci break; 5091cb0ef41Sopenharmony_ci } 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci blocks_.pop_back(); 5121cb0ef41Sopenharmony_ci#ifdef ENABLE_HANDLE_ZAPPING 5131cb0ef41Sopenharmony_ci internal::HandleScope::ZapRange(block_start, block_limit); 5141cb0ef41Sopenharmony_ci#endif 5151cb0ef41Sopenharmony_ci if (spare_ != nullptr) { 5161cb0ef41Sopenharmony_ci DeleteArray(spare_); 5171cb0ef41Sopenharmony_ci } 5181cb0ef41Sopenharmony_ci spare_ = block_start; 5191cb0ef41Sopenharmony_ci } 5201cb0ef41Sopenharmony_ci DCHECK((blocks_.empty() && prev_limit == nullptr) || 5211cb0ef41Sopenharmony_ci (!blocks_.empty() && prev_limit != nullptr)); 5221cb0ef41Sopenharmony_ci} 5231cb0ef41Sopenharmony_ci 5241cb0ef41Sopenharmony_ci// Interceptor functions called from generated inline caches to notify 5251cb0ef41Sopenharmony_ci// CPU profiler that external callbacks are invoked. 5261cb0ef41Sopenharmony_civoid InvokeAccessorGetterCallback( 5271cb0ef41Sopenharmony_ci v8::Local<v8::Name> property, 5281cb0ef41Sopenharmony_ci const v8::PropertyCallbackInfo<v8::Value>& info, 5291cb0ef41Sopenharmony_ci v8::AccessorNameGetterCallback getter); 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_civoid InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info, 5321cb0ef41Sopenharmony_ci v8::FunctionCallback callback); 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_civoid InvokeFinalizationRegistryCleanupFromTask( 5351cb0ef41Sopenharmony_ci Handle<Context> context, 5361cb0ef41Sopenharmony_ci Handle<JSFinalizationRegistry> finalization_registry, 5371cb0ef41Sopenharmony_ci Handle<Object> callback); 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_citemplate <typename T> 5401cb0ef41Sopenharmony_ciEXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) 5411cb0ef41Sopenharmony_ciT ConvertDouble(double d); 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ci} // namespace internal 5441cb0ef41Sopenharmony_ci} // namespace v8 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci#endif // V8_API_API_H_ 547