11cb0ef41Sopenharmony_ci# The V8 public C++ API 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci# Overview 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciThe V8 public C++ API aims to support four use cases: 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci1. Enable applications that embed V8 (called the embedder) to configure and run 81cb0ef41Sopenharmony_ci one or more instances of V8. 91cb0ef41Sopenharmony_ci2. Expose ECMAScript-like capabilities to the embedder. 101cb0ef41Sopenharmony_ci3. Enable the embedder to interact with ECMAScript by exposing API objects. 111cb0ef41Sopenharmony_ci4. Provide access to the V8 debugger (inspector). 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci# Configuring and running an instance of V8 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciV8 requires access to certain OS-level primitives such as the ability to 161cb0ef41Sopenharmony_cischedule work on threads, or allocate memory. 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciThe embedder can define how to access those primitives via the v8::Platform 191cb0ef41Sopenharmony_ciinterface. While V8 bundles a basic implementation, embedders are highly 201cb0ef41Sopenharmony_ciencouraged to implement v8::Platform themselves. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciCurrently, the v8::ArrayBuffer::Allocator is passed to the v8::Isolate factory 231cb0ef41Sopenharmony_cimethod, however, conceptually it should also be part of the v8::Platform since 241cb0ef41Sopenharmony_ciall instances of V8 should share one allocator. 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciOnce the v8::Platform is configured, an v8::Isolate can be created. All 271cb0ef41Sopenharmony_cifurther interactions with V8 should explicitly reference the v8::Isolate they 281cb0ef41Sopenharmony_cirefer to. All API methods should eventually take an v8::Isolate parameter. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciWhen a given instance of V8 is no longer needed, it can be destroyed by 311cb0ef41Sopenharmony_cidisposing the respective v8::Isolate. If the embedder wishes to free all memory 321cb0ef41Sopenharmony_ciassociated with the v8::Isolate, it has to first clear all global handles 331cb0ef41Sopenharmony_ciassociated with that v8::Isolate. 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci# ECMAScript-like capabilities 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciIn general, the C++ API shouldn't enable capabilities that aren't available to 381cb0ef41Sopenharmony_ciscripts running in V8. Experience has shown that it's not possible to maintain 391cb0ef41Sopenharmony_cisuch API methods in the long term. However, capabilities also available to 401cb0ef41Sopenharmony_ciscripts, i.e., ones that are defined in the ECMAScript standard are there to 411cb0ef41Sopenharmony_cistay, and we can safely expose them to embedders. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciThe C++ API should also be pleasant to use, and not require learning new 441cb0ef41Sopenharmony_ciparadigms. Similarly to how the API exposed to scripts aims to provide good 451cb0ef41Sopenharmony_ciergonomics, we should aim to provide a reasonable developer experience for this 461cb0ef41Sopenharmony_ciAPI surface. 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciECMAScript makes heavy use of exceptions, however, V8's C++ code doesn't use 491cb0ef41Sopenharmony_ciC++ exceptions. Therefore, all API methods that can throw exceptions should 501cb0ef41Sopenharmony_ciindicate so by returning a v8::Maybe<> or v8::MaybeLocal<> result, 511cb0ef41Sopenharmony_ciand by taking a v8::Local<v8::Context> parameter that indicates in which 521cb0ef41Sopenharmony_cicontext a possible exception should be thrown. 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci# API objects 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciV8 allows embedders to define special objects that expose additional 571cb0ef41Sopenharmony_cicapabilities and APIs to scripts. The most prominent example is exposing the 581cb0ef41Sopenharmony_ciHTML DOM in Blink. Other examples are e.g. node.js. It is less clear what kind 591cb0ef41Sopenharmony_ciof capabilities we want to expose via this API surface. As a rule of thumb, we 601cb0ef41Sopenharmony_ciwant to expose operations as defined in the WebIDL and HTML spec: we 611cb0ef41Sopenharmony_ciassume that those requirements are somewhat stable, and that they are a 621cb0ef41Sopenharmony_cisuperset of the requirements of other embedders including node.js. 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciIdeally, the API surfaces defined in those specs hook into the ECMAScript spec 651cb0ef41Sopenharmony_ciwhich in turn guarantees long-term stability of the API. 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci# The V8 inspector 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciAll debugging capabilities of V8 should be exposed via the inspector protocol. 701cb0ef41Sopenharmony_ciThe exception to this are profiling features exposed via v8-profiler.h. 711cb0ef41Sopenharmony_ciChanges to the inspector protocol need to ensure backwards compatibility and 721cb0ef41Sopenharmony_cicommitment to maintain. 73