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_SCRIPT_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_V8_SCRIPT_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <stddef.h> 91cb0ef41Sopenharmony_ci#include <stdint.h> 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#include <memory> 121cb0ef41Sopenharmony_ci#include <vector> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci#include "v8-callbacks.h" // NOLINT(build/include_directory) 151cb0ef41Sopenharmony_ci#include "v8-data.h" // NOLINT(build/include_directory) 161cb0ef41Sopenharmony_ci#include "v8-local-handle.h" // NOLINT(build/include_directory) 171cb0ef41Sopenharmony_ci#include "v8-maybe.h" // NOLINT(build/include_directory) 181cb0ef41Sopenharmony_ci#include "v8-message.h" // NOLINT(build/include_directory) 191cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cinamespace v8 { 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciclass Function; 241cb0ef41Sopenharmony_ciclass Message; 251cb0ef41Sopenharmony_ciclass Object; 261cb0ef41Sopenharmony_ciclass PrimitiveArray; 271cb0ef41Sopenharmony_ciclass Script; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cinamespace internal { 301cb0ef41Sopenharmony_ciclass BackgroundDeserializeTask; 311cb0ef41Sopenharmony_cistruct ScriptStreamingData; 321cb0ef41Sopenharmony_ci} // namespace internal 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci/** 351cb0ef41Sopenharmony_ci * A container type that holds relevant metadata for module loading. 361cb0ef41Sopenharmony_ci * 371cb0ef41Sopenharmony_ci * This is passed back to the embedder as part of 381cb0ef41Sopenharmony_ci * HostImportModuleDynamicallyCallback for module loading. 391cb0ef41Sopenharmony_ci */ 401cb0ef41Sopenharmony_ciclass V8_EXPORT ScriptOrModule { 411cb0ef41Sopenharmony_ci public: 421cb0ef41Sopenharmony_ci /** 431cb0ef41Sopenharmony_ci * The name that was passed by the embedder as ResourceName to the 441cb0ef41Sopenharmony_ci * ScriptOrigin. This can be either a v8::String or v8::Undefined. 451cb0ef41Sopenharmony_ci */ 461cb0ef41Sopenharmony_ci Local<Value> GetResourceName(); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci /** 491cb0ef41Sopenharmony_ci * The options that were passed by the embedder as HostDefinedOptions to 501cb0ef41Sopenharmony_ci * the ScriptOrigin. 511cb0ef41Sopenharmony_ci */ 521cb0ef41Sopenharmony_ci Local<Data> HostDefinedOptions(); 531cb0ef41Sopenharmony_ci}; 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci/** 561cb0ef41Sopenharmony_ci * A compiled JavaScript script, not yet tied to a Context. 571cb0ef41Sopenharmony_ci */ 581cb0ef41Sopenharmony_ciclass V8_EXPORT UnboundScript { 591cb0ef41Sopenharmony_ci public: 601cb0ef41Sopenharmony_ci /** 611cb0ef41Sopenharmony_ci * Binds the script to the currently entered context. 621cb0ef41Sopenharmony_ci */ 631cb0ef41Sopenharmony_ci Local<Script> BindToCurrentContext(); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci int GetId() const; 661cb0ef41Sopenharmony_ci Local<Value> GetScriptName(); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci /** 691cb0ef41Sopenharmony_ci * Data read from magic sourceURL comments. 701cb0ef41Sopenharmony_ci */ 711cb0ef41Sopenharmony_ci Local<Value> GetSourceURL(); 721cb0ef41Sopenharmony_ci /** 731cb0ef41Sopenharmony_ci * Data read from magic sourceMappingURL comments. 741cb0ef41Sopenharmony_ci */ 751cb0ef41Sopenharmony_ci Local<Value> GetSourceMappingURL(); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci /** 781cb0ef41Sopenharmony_ci * Returns zero based line number of the code_pos location in the script. 791cb0ef41Sopenharmony_ci * -1 will be returned if no information available. 801cb0ef41Sopenharmony_ci */ 811cb0ef41Sopenharmony_ci int GetLineNumber(int code_pos = 0); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci /** 841cb0ef41Sopenharmony_ci * Returns zero based column number of the code_pos location in the script. 851cb0ef41Sopenharmony_ci * -1 will be returned if no information available. 861cb0ef41Sopenharmony_ci */ 871cb0ef41Sopenharmony_ci int GetColumnNumber(int code_pos = 0); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci static const int kNoScriptId = 0; 901cb0ef41Sopenharmony_ci}; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci/** 931cb0ef41Sopenharmony_ci * A compiled JavaScript module, not yet tied to a Context. 941cb0ef41Sopenharmony_ci */ 951cb0ef41Sopenharmony_ciclass V8_EXPORT UnboundModuleScript : public Data { 961cb0ef41Sopenharmony_ci public: 971cb0ef41Sopenharmony_ci /** 981cb0ef41Sopenharmony_ci * Data read from magic sourceURL comments. 991cb0ef41Sopenharmony_ci */ 1001cb0ef41Sopenharmony_ci Local<Value> GetSourceURL(); 1011cb0ef41Sopenharmony_ci /** 1021cb0ef41Sopenharmony_ci * Data read from magic sourceMappingURL comments. 1031cb0ef41Sopenharmony_ci */ 1041cb0ef41Sopenharmony_ci Local<Value> GetSourceMappingURL(); 1051cb0ef41Sopenharmony_ci}; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci/** 1081cb0ef41Sopenharmony_ci * A location in JavaScript source. 1091cb0ef41Sopenharmony_ci */ 1101cb0ef41Sopenharmony_ciclass V8_EXPORT Location { 1111cb0ef41Sopenharmony_ci public: 1121cb0ef41Sopenharmony_ci int GetLineNumber() { return line_number_; } 1131cb0ef41Sopenharmony_ci int GetColumnNumber() { return column_number_; } 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci Location(int line_number, int column_number) 1161cb0ef41Sopenharmony_ci : line_number_(line_number), column_number_(column_number) {} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci private: 1191cb0ef41Sopenharmony_ci int line_number_; 1201cb0ef41Sopenharmony_ci int column_number_; 1211cb0ef41Sopenharmony_ci}; 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciclass V8_EXPORT ModuleRequest : public Data { 1241cb0ef41Sopenharmony_ci public: 1251cb0ef41Sopenharmony_ci /** 1261cb0ef41Sopenharmony_ci * Returns the module specifier for this ModuleRequest. 1271cb0ef41Sopenharmony_ci */ 1281cb0ef41Sopenharmony_ci Local<String> GetSpecifier() const; 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci /** 1311cb0ef41Sopenharmony_ci * Returns the source code offset of this module request. 1321cb0ef41Sopenharmony_ci * Use Module::SourceOffsetToLocation to convert this to line/column numbers. 1331cb0ef41Sopenharmony_ci */ 1341cb0ef41Sopenharmony_ci int GetSourceOffset() const; 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci /** 1371cb0ef41Sopenharmony_ci * Contains the import assertions for this request in the form: 1381cb0ef41Sopenharmony_ci * [key1, value1, source_offset1, key2, value2, source_offset2, ...]. 1391cb0ef41Sopenharmony_ci * The keys and values are of type v8::String, and the source offsets are of 1401cb0ef41Sopenharmony_ci * type Int32. Use Module::SourceOffsetToLocation to convert the source 1411cb0ef41Sopenharmony_ci * offsets to Locations with line/column numbers. 1421cb0ef41Sopenharmony_ci * 1431cb0ef41Sopenharmony_ci * All assertions present in the module request will be supplied in this 1441cb0ef41Sopenharmony_ci * list, regardless of whether they are supported by the host. Per 1451cb0ef41Sopenharmony_ci * https://tc39.es/proposal-import-assertions/#sec-hostgetsupportedimportassertions, 1461cb0ef41Sopenharmony_ci * hosts are expected to ignore assertions that they do not support (as 1471cb0ef41Sopenharmony_ci * opposed to, for example, triggering an error if an unsupported assertion is 1481cb0ef41Sopenharmony_ci * present). 1491cb0ef41Sopenharmony_ci */ 1501cb0ef41Sopenharmony_ci Local<FixedArray> GetImportAssertions() const; 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci V8_INLINE static ModuleRequest* Cast(Data* data); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci private: 1551cb0ef41Sopenharmony_ci static void CheckCast(Data* obj); 1561cb0ef41Sopenharmony_ci}; 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci/** 1591cb0ef41Sopenharmony_ci * A compiled JavaScript module. 1601cb0ef41Sopenharmony_ci */ 1611cb0ef41Sopenharmony_ciclass V8_EXPORT Module : public Data { 1621cb0ef41Sopenharmony_ci public: 1631cb0ef41Sopenharmony_ci /** 1641cb0ef41Sopenharmony_ci * The different states a module can be in. 1651cb0ef41Sopenharmony_ci * 1661cb0ef41Sopenharmony_ci * This corresponds to the states used in ECMAScript except that "evaluated" 1671cb0ef41Sopenharmony_ci * is split into kEvaluated and kErrored, indicating success and failure, 1681cb0ef41Sopenharmony_ci * respectively. 1691cb0ef41Sopenharmony_ci */ 1701cb0ef41Sopenharmony_ci enum Status { 1711cb0ef41Sopenharmony_ci kUninstantiated, 1721cb0ef41Sopenharmony_ci kInstantiating, 1731cb0ef41Sopenharmony_ci kInstantiated, 1741cb0ef41Sopenharmony_ci kEvaluating, 1751cb0ef41Sopenharmony_ci kEvaluated, 1761cb0ef41Sopenharmony_ci kErrored 1771cb0ef41Sopenharmony_ci }; 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci /** 1801cb0ef41Sopenharmony_ci * Returns the module's current status. 1811cb0ef41Sopenharmony_ci */ 1821cb0ef41Sopenharmony_ci Status GetStatus() const; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci /** 1851cb0ef41Sopenharmony_ci * For a module in kErrored status, this returns the corresponding exception. 1861cb0ef41Sopenharmony_ci */ 1871cb0ef41Sopenharmony_ci Local<Value> GetException() const; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci /** 1901cb0ef41Sopenharmony_ci * Returns the ModuleRequests for this module. 1911cb0ef41Sopenharmony_ci */ 1921cb0ef41Sopenharmony_ci Local<FixedArray> GetModuleRequests() const; 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci /** 1951cb0ef41Sopenharmony_ci * For the given source text offset in this module, returns the corresponding 1961cb0ef41Sopenharmony_ci * Location with line and column numbers. 1971cb0ef41Sopenharmony_ci */ 1981cb0ef41Sopenharmony_ci Location SourceOffsetToLocation(int offset) const; 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci /** 2011cb0ef41Sopenharmony_ci * Returns the identity hash for this object. 2021cb0ef41Sopenharmony_ci */ 2031cb0ef41Sopenharmony_ci int GetIdentityHash() const; 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci using ResolveModuleCallback = MaybeLocal<Module> (*)( 2061cb0ef41Sopenharmony_ci Local<Context> context, Local<String> specifier, 2071cb0ef41Sopenharmony_ci Local<FixedArray> import_assertions, Local<Module> referrer); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci /** 2101cb0ef41Sopenharmony_ci * Instantiates the module and its dependencies. 2111cb0ef41Sopenharmony_ci * 2121cb0ef41Sopenharmony_ci * Returns an empty Maybe<bool> if an exception occurred during 2131cb0ef41Sopenharmony_ci * instantiation. (In the case where the callback throws an exception, that 2141cb0ef41Sopenharmony_ci * exception is propagated.) 2151cb0ef41Sopenharmony_ci */ 2161cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule( 2171cb0ef41Sopenharmony_ci Local<Context> context, ResolveModuleCallback callback); 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ci /** 2201cb0ef41Sopenharmony_ci * Evaluates the module and its dependencies. 2211cb0ef41Sopenharmony_ci * 2221cb0ef41Sopenharmony_ci * If status is kInstantiated, run the module's code and return a Promise 2231cb0ef41Sopenharmony_ci * object. On success, set status to kEvaluated and resolve the Promise with 2241cb0ef41Sopenharmony_ci * the completion value; on failure, set status to kErrored and reject the 2251cb0ef41Sopenharmony_ci * Promise with the error. 2261cb0ef41Sopenharmony_ci * 2271cb0ef41Sopenharmony_ci * If IsGraphAsync() is false, the returned Promise is settled. 2281cb0ef41Sopenharmony_ci */ 2291cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT MaybeLocal<Value> Evaluate(Local<Context> context); 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci /** 2321cb0ef41Sopenharmony_ci * Returns the namespace object of this module. 2331cb0ef41Sopenharmony_ci * 2341cb0ef41Sopenharmony_ci * The module's status must be at least kInstantiated. 2351cb0ef41Sopenharmony_ci */ 2361cb0ef41Sopenharmony_ci Local<Value> GetModuleNamespace(); 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci /** 2391cb0ef41Sopenharmony_ci * Returns the corresponding context-unbound module script. 2401cb0ef41Sopenharmony_ci * 2411cb0ef41Sopenharmony_ci * The module must be unevaluated, i.e. its status must not be kEvaluating, 2421cb0ef41Sopenharmony_ci * kEvaluated or kErrored. 2431cb0ef41Sopenharmony_ci */ 2441cb0ef41Sopenharmony_ci Local<UnboundModuleScript> GetUnboundModuleScript(); 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci /** 2471cb0ef41Sopenharmony_ci * Returns the underlying script's id. 2481cb0ef41Sopenharmony_ci * 2491cb0ef41Sopenharmony_ci * The module must be a SourceTextModule and must not have a kErrored status. 2501cb0ef41Sopenharmony_ci */ 2511cb0ef41Sopenharmony_ci int ScriptId() const; 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci /** 2541cb0ef41Sopenharmony_ci * Returns whether this module or any of its requested modules is async, 2551cb0ef41Sopenharmony_ci * i.e. contains top-level await. 2561cb0ef41Sopenharmony_ci * 2571cb0ef41Sopenharmony_ci * The module's status must be at least kInstantiated. 2581cb0ef41Sopenharmony_ci */ 2591cb0ef41Sopenharmony_ci bool IsGraphAsync() const; 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ci /** 2621cb0ef41Sopenharmony_ci * Returns whether the module is a SourceTextModule. 2631cb0ef41Sopenharmony_ci */ 2641cb0ef41Sopenharmony_ci bool IsSourceTextModule() const; 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci /** 2671cb0ef41Sopenharmony_ci * Returns whether the module is a SyntheticModule. 2681cb0ef41Sopenharmony_ci */ 2691cb0ef41Sopenharmony_ci bool IsSyntheticModule() const; 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci /* 2721cb0ef41Sopenharmony_ci * Callback defined in the embedder. This is responsible for setting 2731cb0ef41Sopenharmony_ci * the module's exported values with calls to SetSyntheticModuleExport(). 2741cb0ef41Sopenharmony_ci * The callback must return a resolved Promise to indicate success (where no 2751cb0ef41Sopenharmony_ci * exception was thrown) and return an empy MaybeLocal to indicate falure 2761cb0ef41Sopenharmony_ci * (where an exception was thrown). 2771cb0ef41Sopenharmony_ci */ 2781cb0ef41Sopenharmony_ci using SyntheticModuleEvaluationSteps = 2791cb0ef41Sopenharmony_ci MaybeLocal<Value> (*)(Local<Context> context, Local<Module> module); 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci /** 2821cb0ef41Sopenharmony_ci * Creates a new SyntheticModule with the specified export names, where 2831cb0ef41Sopenharmony_ci * evaluation_steps will be executed upon module evaluation. 2841cb0ef41Sopenharmony_ci * export_names must not contain duplicates. 2851cb0ef41Sopenharmony_ci * module_name is used solely for logging/debugging and doesn't affect module 2861cb0ef41Sopenharmony_ci * behavior. 2871cb0ef41Sopenharmony_ci */ 2881cb0ef41Sopenharmony_ci static Local<Module> CreateSyntheticModule( 2891cb0ef41Sopenharmony_ci Isolate* isolate, Local<String> module_name, 2901cb0ef41Sopenharmony_ci const std::vector<Local<String>>& export_names, 2911cb0ef41Sopenharmony_ci SyntheticModuleEvaluationSteps evaluation_steps); 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci /** 2941cb0ef41Sopenharmony_ci * Set this module's exported value for the name export_name to the specified 2951cb0ef41Sopenharmony_ci * export_value. This method must be called only on Modules created via 2961cb0ef41Sopenharmony_ci * CreateSyntheticModule. An error will be thrown if export_name is not one 2971cb0ef41Sopenharmony_ci * of the export_names that were passed in that CreateSyntheticModule call. 2981cb0ef41Sopenharmony_ci * Returns Just(true) on success, Nothing<bool>() if an error was thrown. 2991cb0ef41Sopenharmony_ci */ 3001cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport( 3011cb0ef41Sopenharmony_ci Isolate* isolate, Local<String> export_name, Local<Value> export_value); 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci /** 3041cb0ef41Sopenharmony_ci * Search the modules requested directly or indirectly by the module for 3051cb0ef41Sopenharmony_ci * any top-level await that has not yet resolved. If there is any, the 3061cb0ef41Sopenharmony_ci * returned vector contains a tuple of the unresolved module and a message 3071cb0ef41Sopenharmony_ci * with the pending top-level await. 3081cb0ef41Sopenharmony_ci * An embedder may call this before exiting to improve error messages. 3091cb0ef41Sopenharmony_ci */ 3101cb0ef41Sopenharmony_ci std::vector<std::tuple<Local<Module>, Local<Message>>> 3111cb0ef41Sopenharmony_ci GetStalledTopLevelAwaitMessage(Isolate* isolate); 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ci V8_INLINE static Module* Cast(Data* data); 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ci private: 3161cb0ef41Sopenharmony_ci static void CheckCast(Data* obj); 3171cb0ef41Sopenharmony_ci}; 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci/** 3201cb0ef41Sopenharmony_ci * A compiled JavaScript script, tied to a Context which was active when the 3211cb0ef41Sopenharmony_ci * script was compiled. 3221cb0ef41Sopenharmony_ci */ 3231cb0ef41Sopenharmony_ciclass V8_EXPORT Script { 3241cb0ef41Sopenharmony_ci public: 3251cb0ef41Sopenharmony_ci /** 3261cb0ef41Sopenharmony_ci * A shorthand for ScriptCompiler::Compile(). 3271cb0ef41Sopenharmony_ci */ 3281cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 3291cb0ef41Sopenharmony_ci Local<Context> context, Local<String> source, 3301cb0ef41Sopenharmony_ci ScriptOrigin* origin = nullptr); 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci /** 3331cb0ef41Sopenharmony_ci * Runs the script returning the resulting value. It will be run in the 3341cb0ef41Sopenharmony_ci * context in which it was created (ScriptCompiler::CompileBound or 3351cb0ef41Sopenharmony_ci * UnboundScript::BindToCurrentContext()). 3361cb0ef41Sopenharmony_ci */ 3371cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context); 3381cb0ef41Sopenharmony_ci V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context, 3391cb0ef41Sopenharmony_ci Local<Data> host_defined_options); 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_ci /** 3421cb0ef41Sopenharmony_ci * Returns the corresponding context-unbound script. 3431cb0ef41Sopenharmony_ci */ 3441cb0ef41Sopenharmony_ci Local<UnboundScript> GetUnboundScript(); 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci /** 3471cb0ef41Sopenharmony_ci * The name that was passed by the embedder as ResourceName to the 3481cb0ef41Sopenharmony_ci * ScriptOrigin. This can be either a v8::String or v8::Undefined. 3491cb0ef41Sopenharmony_ci */ 3501cb0ef41Sopenharmony_ci Local<Value> GetResourceName(); 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ci /** 3531cb0ef41Sopenharmony_ci * If the script was compiled, returns the positions of lazy functions which 3541cb0ef41Sopenharmony_ci * were eventually compiled and executed. 3551cb0ef41Sopenharmony_ci */ 3561cb0ef41Sopenharmony_ci std::vector<int> GetProducedCompileHints() const; 3571cb0ef41Sopenharmony_ci}; 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_cienum class ScriptType { kClassic, kModule }; 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci/** 3621cb0ef41Sopenharmony_ci * For compiling scripts. 3631cb0ef41Sopenharmony_ci */ 3641cb0ef41Sopenharmony_ciclass V8_EXPORT ScriptCompiler { 3651cb0ef41Sopenharmony_ci public: 3661cb0ef41Sopenharmony_ci class ConsumeCodeCacheTask; 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ci /** 3691cb0ef41Sopenharmony_ci * Compilation data that the embedder can cache and pass back to speed up 3701cb0ef41Sopenharmony_ci * future compilations. The data is produced if the CompilerOptions passed to 3711cb0ef41Sopenharmony_ci * the compilation functions in ScriptCompiler contains produce_data_to_cache 3721cb0ef41Sopenharmony_ci * = true. The data to cache can then can be retrieved from 3731cb0ef41Sopenharmony_ci * UnboundScript. 3741cb0ef41Sopenharmony_ci */ 3751cb0ef41Sopenharmony_ci struct V8_EXPORT CachedData { 3761cb0ef41Sopenharmony_ci enum BufferPolicy { BufferNotOwned, BufferOwned }; 3771cb0ef41Sopenharmony_ci 3781cb0ef41Sopenharmony_ci CachedData() 3791cb0ef41Sopenharmony_ci : data(nullptr), 3801cb0ef41Sopenharmony_ci length(0), 3811cb0ef41Sopenharmony_ci rejected(false), 3821cb0ef41Sopenharmony_ci buffer_policy(BufferNotOwned) {} 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci // If buffer_policy is BufferNotOwned, the caller keeps the ownership of 3851cb0ef41Sopenharmony_ci // data and guarantees that it stays alive until the CachedData object is 3861cb0ef41Sopenharmony_ci // destroyed. If the policy is BufferOwned, the given data will be deleted 3871cb0ef41Sopenharmony_ci // (with delete[]) when the CachedData object is destroyed. 3881cb0ef41Sopenharmony_ci CachedData(const uint8_t* data, int length, 3891cb0ef41Sopenharmony_ci BufferPolicy buffer_policy = BufferNotOwned); 3901cb0ef41Sopenharmony_ci ~CachedData(); 3911cb0ef41Sopenharmony_ci // TODO(marja): Async compilation; add constructors which take a callback 3921cb0ef41Sopenharmony_ci // which will be called when V8 no longer needs the data. 3931cb0ef41Sopenharmony_ci const uint8_t* data; 3941cb0ef41Sopenharmony_ci int length; 3951cb0ef41Sopenharmony_ci bool rejected; 3961cb0ef41Sopenharmony_ci BufferPolicy buffer_policy; 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ci // Prevent copying. 3991cb0ef41Sopenharmony_ci CachedData(const CachedData&) = delete; 4001cb0ef41Sopenharmony_ci CachedData& operator=(const CachedData&) = delete; 4011cb0ef41Sopenharmony_ci }; 4021cb0ef41Sopenharmony_ci 4031cb0ef41Sopenharmony_ci /** 4041cb0ef41Sopenharmony_ci * Source code which can be then compiled to a UnboundScript or Script. 4051cb0ef41Sopenharmony_ci */ 4061cb0ef41Sopenharmony_ci class Source { 4071cb0ef41Sopenharmony_ci public: 4081cb0ef41Sopenharmony_ci // Source takes ownership of both CachedData and CodeCacheConsumeTask. 4091cb0ef41Sopenharmony_ci // The caller *must* ensure that the cached data is from a trusted source. 4101cb0ef41Sopenharmony_ci V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin, 4111cb0ef41Sopenharmony_ci CachedData* cached_data = nullptr, 4121cb0ef41Sopenharmony_ci ConsumeCodeCacheTask* consume_cache_task = nullptr); 4131cb0ef41Sopenharmony_ci // Source takes ownership of both CachedData and CodeCacheConsumeTask. 4141cb0ef41Sopenharmony_ci V8_INLINE explicit Source( 4151cb0ef41Sopenharmony_ci Local<String> source_string, CachedData* cached_data = nullptr, 4161cb0ef41Sopenharmony_ci ConsumeCodeCacheTask* consume_cache_task = nullptr); 4171cb0ef41Sopenharmony_ci V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin, 4181cb0ef41Sopenharmony_ci CompileHintCallback callback, void* callback_data); 4191cb0ef41Sopenharmony_ci V8_INLINE ~Source() = default; 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_ci // Ownership of the CachedData or its buffers is *not* transferred to the 4221cb0ef41Sopenharmony_ci // caller. The CachedData object is alive as long as the Source object is 4231cb0ef41Sopenharmony_ci // alive. 4241cb0ef41Sopenharmony_ci V8_INLINE const CachedData* GetCachedData() const; 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci V8_INLINE const ScriptOriginOptions& GetResourceOptions() const; 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ci private: 4291cb0ef41Sopenharmony_ci friend class ScriptCompiler; 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ci Local<String> source_string; 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci // Origin information 4341cb0ef41Sopenharmony_ci Local<Value> resource_name; 4351cb0ef41Sopenharmony_ci int resource_line_offset; 4361cb0ef41Sopenharmony_ci int resource_column_offset; 4371cb0ef41Sopenharmony_ci ScriptOriginOptions resource_options; 4381cb0ef41Sopenharmony_ci Local<Value> source_map_url; 4391cb0ef41Sopenharmony_ci Local<Data> host_defined_options; 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_ci // Cached data from previous compilation (if a kConsume*Cache flag is 4421cb0ef41Sopenharmony_ci // set), or hold newly generated cache data (kProduce*Cache flags) are 4431cb0ef41Sopenharmony_ci // set when calling a compile method. 4441cb0ef41Sopenharmony_ci std::unique_ptr<CachedData> cached_data; 4451cb0ef41Sopenharmony_ci std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task; 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci // For requesting compile hints from the embedder. 4481cb0ef41Sopenharmony_ci CompileHintCallback compile_hint_callback = nullptr; 4491cb0ef41Sopenharmony_ci void* compile_hint_callback_data = nullptr; 4501cb0ef41Sopenharmony_ci }; 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci /** 4531cb0ef41Sopenharmony_ci * For streaming incomplete script data to V8. The embedder should implement a 4541cb0ef41Sopenharmony_ci * subclass of this class. 4551cb0ef41Sopenharmony_ci */ 4561cb0ef41Sopenharmony_ci class V8_EXPORT ExternalSourceStream { 4571cb0ef41Sopenharmony_ci public: 4581cb0ef41Sopenharmony_ci virtual ~ExternalSourceStream() = default; 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci /** 4611cb0ef41Sopenharmony_ci * V8 calls this to request the next chunk of data from the embedder. This 4621cb0ef41Sopenharmony_ci * function will be called on a background thread, so it's OK to block and 4631cb0ef41Sopenharmony_ci * wait for the data, if the embedder doesn't have data yet. Returns the 4641cb0ef41Sopenharmony_ci * length of the data returned. When the data ends, GetMoreData should 4651cb0ef41Sopenharmony_ci * return 0. Caller takes ownership of the data. 4661cb0ef41Sopenharmony_ci * 4671cb0ef41Sopenharmony_ci * When streaming UTF-8 data, V8 handles multi-byte characters split between 4681cb0ef41Sopenharmony_ci * two data chunks, but doesn't handle multi-byte characters split between 4691cb0ef41Sopenharmony_ci * more than two data chunks. The embedder can avoid this problem by always 4701cb0ef41Sopenharmony_ci * returning at least 2 bytes of data. 4711cb0ef41Sopenharmony_ci * 4721cb0ef41Sopenharmony_ci * When streaming UTF-16 data, V8 does not handle characters split between 4731cb0ef41Sopenharmony_ci * two data chunks. The embedder has to make sure that chunks have an even 4741cb0ef41Sopenharmony_ci * length. 4751cb0ef41Sopenharmony_ci * 4761cb0ef41Sopenharmony_ci * If the embedder wants to cancel the streaming, they should make the next 4771cb0ef41Sopenharmony_ci * GetMoreData call return 0. V8 will interpret it as end of data (and most 4781cb0ef41Sopenharmony_ci * probably, parsing will fail). The streaming task will return as soon as 4791cb0ef41Sopenharmony_ci * V8 has parsed the data it received so far. 4801cb0ef41Sopenharmony_ci */ 4811cb0ef41Sopenharmony_ci virtual size_t GetMoreData(const uint8_t** src) = 0; 4821cb0ef41Sopenharmony_ci }; 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci /** 4851cb0ef41Sopenharmony_ci * Source code which can be streamed into V8 in pieces. It will be parsed 4861cb0ef41Sopenharmony_ci * while streaming and compiled after parsing has completed. StreamedSource 4871cb0ef41Sopenharmony_ci * must be kept alive while the streaming task is run (see ScriptStreamingTask 4881cb0ef41Sopenharmony_ci * below). 4891cb0ef41Sopenharmony_ci */ 4901cb0ef41Sopenharmony_ci class V8_EXPORT StreamedSource { 4911cb0ef41Sopenharmony_ci public: 4921cb0ef41Sopenharmony_ci enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 }; 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream, 4951cb0ef41Sopenharmony_ci Encoding encoding); 4961cb0ef41Sopenharmony_ci ~StreamedSource(); 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci internal::ScriptStreamingData* impl() const { return impl_.get(); } 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci // Prevent copying. 5011cb0ef41Sopenharmony_ci StreamedSource(const StreamedSource&) = delete; 5021cb0ef41Sopenharmony_ci StreamedSource& operator=(const StreamedSource&) = delete; 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci private: 5051cb0ef41Sopenharmony_ci std::unique_ptr<internal::ScriptStreamingData> impl_; 5061cb0ef41Sopenharmony_ci }; 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ci /** 5091cb0ef41Sopenharmony_ci * A streaming task which the embedder must run on a background thread to 5101cb0ef41Sopenharmony_ci * stream scripts into V8. Returned by ScriptCompiler::StartStreaming. 5111cb0ef41Sopenharmony_ci */ 5121cb0ef41Sopenharmony_ci class V8_EXPORT ScriptStreamingTask final { 5131cb0ef41Sopenharmony_ci public: 5141cb0ef41Sopenharmony_ci void Run(); 5151cb0ef41Sopenharmony_ci 5161cb0ef41Sopenharmony_ci private: 5171cb0ef41Sopenharmony_ci friend class ScriptCompiler; 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci explicit ScriptStreamingTask(internal::ScriptStreamingData* data) 5201cb0ef41Sopenharmony_ci : data_(data) {} 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci internal::ScriptStreamingData* data_; 5231cb0ef41Sopenharmony_ci }; 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ci /** 5261cb0ef41Sopenharmony_ci * A task which the embedder must run on a background thread to 5271cb0ef41Sopenharmony_ci * consume a V8 code cache. Returned by 5281cb0ef41Sopenharmony_ci * ScriptCompiler::StartConsumingCodeCache. 5291cb0ef41Sopenharmony_ci */ 5301cb0ef41Sopenharmony_ci class V8_EXPORT ConsumeCodeCacheTask final { 5311cb0ef41Sopenharmony_ci public: 5321cb0ef41Sopenharmony_ci ~ConsumeCodeCacheTask(); 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci void Run(); 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_ci /** 5371cb0ef41Sopenharmony_ci * Provides the source text string and origin information to the consumption 5381cb0ef41Sopenharmony_ci * task. May be called before, during, or after Run(). This step checks 5391cb0ef41Sopenharmony_ci * whether the script matches an existing script in the Isolate's 5401cb0ef41Sopenharmony_ci * compilation cache. To check whether such a script was found, call 5411cb0ef41Sopenharmony_ci * ShouldMergeWithExistingScript. 5421cb0ef41Sopenharmony_ci * 5431cb0ef41Sopenharmony_ci * The Isolate provided must be the same one used during 5441cb0ef41Sopenharmony_ci * StartConsumingCodeCache and must be currently entered on the thread that 5451cb0ef41Sopenharmony_ci * calls this function. The source text and origin provided in this step 5461cb0ef41Sopenharmony_ci * must precisely match those used later in the ScriptCompiler::Source that 5471cb0ef41Sopenharmony_ci * will contain this ConsumeCodeCacheTask. 5481cb0ef41Sopenharmony_ci */ 5491cb0ef41Sopenharmony_ci void SourceTextAvailable(Isolate* isolate, Local<String> source_text, 5501cb0ef41Sopenharmony_ci const ScriptOrigin& origin); 5511cb0ef41Sopenharmony_ci 5521cb0ef41Sopenharmony_ci /** 5531cb0ef41Sopenharmony_ci * Returns whether the embedder should call MergeWithExistingScript. This 5541cb0ef41Sopenharmony_ci * function may be called from any thread, any number of times, but its 5551cb0ef41Sopenharmony_ci * return value is only meaningful after SourceTextAvailable has completed. 5561cb0ef41Sopenharmony_ci */ 5571cb0ef41Sopenharmony_ci bool ShouldMergeWithExistingScript() const; 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci /** 5601cb0ef41Sopenharmony_ci * Merges newly deserialized data into an existing script which was found 5611cb0ef41Sopenharmony_ci * during SourceTextAvailable. May be called only after Run() has completed. 5621cb0ef41Sopenharmony_ci * Can execute on any thread, like Run(). 5631cb0ef41Sopenharmony_ci */ 5641cb0ef41Sopenharmony_ci void MergeWithExistingScript(); 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci private: 5671cb0ef41Sopenharmony_ci friend class ScriptCompiler; 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci explicit ConsumeCodeCacheTask( 5701cb0ef41Sopenharmony_ci std::unique_ptr<internal::BackgroundDeserializeTask> impl); 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci std::unique_ptr<internal::BackgroundDeserializeTask> impl_; 5731cb0ef41Sopenharmony_ci }; 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ci enum CompileOptions { 5761cb0ef41Sopenharmony_ci kNoCompileOptions = 0, 5771cb0ef41Sopenharmony_ci kConsumeCodeCache, 5781cb0ef41Sopenharmony_ci kEagerCompile, 5791cb0ef41Sopenharmony_ci kProduceCompileHints, 5801cb0ef41Sopenharmony_ci kConsumeCompileHints 5811cb0ef41Sopenharmony_ci }; 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ci /** 5841cb0ef41Sopenharmony_ci * The reason for which we are not requesting or providing a code cache. 5851cb0ef41Sopenharmony_ci */ 5861cb0ef41Sopenharmony_ci enum NoCacheReason { 5871cb0ef41Sopenharmony_ci kNoCacheNoReason = 0, 5881cb0ef41Sopenharmony_ci kNoCacheBecauseCachingDisabled, 5891cb0ef41Sopenharmony_ci kNoCacheBecauseNoResource, 5901cb0ef41Sopenharmony_ci kNoCacheBecauseInlineScript, 5911cb0ef41Sopenharmony_ci kNoCacheBecauseModule, 5921cb0ef41Sopenharmony_ci kNoCacheBecauseStreamingSource, 5931cb0ef41Sopenharmony_ci kNoCacheBecauseInspector, 5941cb0ef41Sopenharmony_ci kNoCacheBecauseScriptTooSmall, 5951cb0ef41Sopenharmony_ci kNoCacheBecauseCacheTooCold, 5961cb0ef41Sopenharmony_ci kNoCacheBecauseV8Extension, 5971cb0ef41Sopenharmony_ci kNoCacheBecauseExtensionModule, 5981cb0ef41Sopenharmony_ci kNoCacheBecausePacScript, 5991cb0ef41Sopenharmony_ci kNoCacheBecauseInDocumentWrite, 6001cb0ef41Sopenharmony_ci kNoCacheBecauseResourceWithNoCacheHandler, 6011cb0ef41Sopenharmony_ci kNoCacheBecauseDeferredProduceCodeCache 6021cb0ef41Sopenharmony_ci }; 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci /** 6051cb0ef41Sopenharmony_ci * Compiles the specified script (context-independent). 6061cb0ef41Sopenharmony_ci * Cached data as part of the source object can be optionally produced to be 6071cb0ef41Sopenharmony_ci * consumed later to speed up compilation of identical source scripts. 6081cb0ef41Sopenharmony_ci * 6091cb0ef41Sopenharmony_ci * Note that when producing cached data, the source must point to NULL for 6101cb0ef41Sopenharmony_ci * cached data. When consuming cached data, the cached data must have been 6111cb0ef41Sopenharmony_ci * produced by the same version of V8, and the embedder needs to ensure the 6121cb0ef41Sopenharmony_ci * cached data is the correct one for the given script. 6131cb0ef41Sopenharmony_ci * 6141cb0ef41Sopenharmony_ci * \param source Script source code. 6151cb0ef41Sopenharmony_ci * \return Compiled script object (context independent; for running it must be 6161cb0ef41Sopenharmony_ci * bound to a context). 6171cb0ef41Sopenharmony_ci */ 6181cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript( 6191cb0ef41Sopenharmony_ci Isolate* isolate, Source* source, 6201cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions, 6211cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason = kNoCacheNoReason); 6221cb0ef41Sopenharmony_ci 6231cb0ef41Sopenharmony_ci /** 6241cb0ef41Sopenharmony_ci * Compiles the specified script (bound to current context). 6251cb0ef41Sopenharmony_ci * 6261cb0ef41Sopenharmony_ci * \param source Script source code. 6271cb0ef41Sopenharmony_ci * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() 6281cb0ef41Sopenharmony_ci * using pre_data speeds compilation if it's done multiple times. 6291cb0ef41Sopenharmony_ci * Owned by caller, no references are kept when this function returns. 6301cb0ef41Sopenharmony_ci * \return Compiled script object, bound to the context that was active 6311cb0ef41Sopenharmony_ci * when this function was called. When run it will always use this 6321cb0ef41Sopenharmony_ci * context. 6331cb0ef41Sopenharmony_ci */ 6341cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 6351cb0ef41Sopenharmony_ci Local<Context> context, Source* source, 6361cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions, 6371cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason = kNoCacheNoReason); 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ci /** 6401cb0ef41Sopenharmony_ci * Returns a task which streams script data into V8, or NULL if the script 6411cb0ef41Sopenharmony_ci * cannot be streamed. The user is responsible for running the task on a 6421cb0ef41Sopenharmony_ci * background thread and deleting it. When ran, the task starts parsing the 6431cb0ef41Sopenharmony_ci * script, and it will request data from the StreamedSource as needed. When 6441cb0ef41Sopenharmony_ci * ScriptStreamingTask::Run exits, all data has been streamed and the script 6451cb0ef41Sopenharmony_ci * can be compiled (see Compile below). 6461cb0ef41Sopenharmony_ci * 6471cb0ef41Sopenharmony_ci * This API allows to start the streaming with as little data as possible, and 6481cb0ef41Sopenharmony_ci * the remaining data (for example, the ScriptOrigin) is passed to Compile. 6491cb0ef41Sopenharmony_ci */ 6501cb0ef41Sopenharmony_ci static ScriptStreamingTask* StartStreaming( 6511cb0ef41Sopenharmony_ci Isolate* isolate, StreamedSource* source, 6521cb0ef41Sopenharmony_ci ScriptType type = ScriptType::kClassic, 6531cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions); 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci static ConsumeCodeCacheTask* StartConsumingCodeCache( 6561cb0ef41Sopenharmony_ci Isolate* isolate, std::unique_ptr<CachedData> source); 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci /** 6591cb0ef41Sopenharmony_ci * Compiles a streamed script (bound to current context). 6601cb0ef41Sopenharmony_ci * 6611cb0ef41Sopenharmony_ci * This can only be called after the streaming has finished 6621cb0ef41Sopenharmony_ci * (ScriptStreamingTask has been run). V8 doesn't construct the source string 6631cb0ef41Sopenharmony_ci * during streaming, so the embedder needs to pass the full source here. 6641cb0ef41Sopenharmony_ci */ 6651cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 6661cb0ef41Sopenharmony_ci Local<Context> context, StreamedSource* source, 6671cb0ef41Sopenharmony_ci Local<String> full_source_string, const ScriptOrigin& origin); 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ci /** 6701cb0ef41Sopenharmony_ci * Return a version tag for CachedData for the current V8 version & flags. 6711cb0ef41Sopenharmony_ci * 6721cb0ef41Sopenharmony_ci * This value is meant only for determining whether a previously generated 6731cb0ef41Sopenharmony_ci * CachedData instance is still valid; the tag has no other meaing. 6741cb0ef41Sopenharmony_ci * 6751cb0ef41Sopenharmony_ci * Background: The data carried by CachedData may depend on the exact 6761cb0ef41Sopenharmony_ci * V8 version number or current compiler flags. This means that when 6771cb0ef41Sopenharmony_ci * persisting CachedData, the embedder must take care to not pass in 6781cb0ef41Sopenharmony_ci * data from another V8 version, or the same version with different 6791cb0ef41Sopenharmony_ci * features enabled. 6801cb0ef41Sopenharmony_ci * 6811cb0ef41Sopenharmony_ci * The easiest way to do so is to clear the embedder's cache on any 6821cb0ef41Sopenharmony_ci * such change. 6831cb0ef41Sopenharmony_ci * 6841cb0ef41Sopenharmony_ci * Alternatively, this tag can be stored alongside the cached data and 6851cb0ef41Sopenharmony_ci * compared when it is being used. 6861cb0ef41Sopenharmony_ci */ 6871cb0ef41Sopenharmony_ci static uint32_t CachedDataVersionTag(); 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ci /** 6901cb0ef41Sopenharmony_ci * Compile an ES module, returning a Module that encapsulates 6911cb0ef41Sopenharmony_ci * the compiled code. 6921cb0ef41Sopenharmony_ci * 6931cb0ef41Sopenharmony_ci * Corresponds to the ParseModule abstract operation in the 6941cb0ef41Sopenharmony_ci * ECMAScript specification. 6951cb0ef41Sopenharmony_ci */ 6961cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule( 6971cb0ef41Sopenharmony_ci Isolate* isolate, Source* source, 6981cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions, 6991cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason = kNoCacheNoReason); 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ci /** 7021cb0ef41Sopenharmony_ci * Compiles a streamed module script. 7031cb0ef41Sopenharmony_ci * 7041cb0ef41Sopenharmony_ci * This can only be called after the streaming has finished 7051cb0ef41Sopenharmony_ci * (ScriptStreamingTask has been run). V8 doesn't construct the source string 7061cb0ef41Sopenharmony_ci * during streaming, so the embedder needs to pass the full source here. 7071cb0ef41Sopenharmony_ci */ 7081cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule( 7091cb0ef41Sopenharmony_ci Local<Context> context, StreamedSource* v8_source, 7101cb0ef41Sopenharmony_ci Local<String> full_source_string, const ScriptOrigin& origin); 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ci /** 7131cb0ef41Sopenharmony_ci * Compile a function for a given context. This is equivalent to running 7141cb0ef41Sopenharmony_ci * 7151cb0ef41Sopenharmony_ci * with (obj) { 7161cb0ef41Sopenharmony_ci * return function(args) { ... } 7171cb0ef41Sopenharmony_ci * } 7181cb0ef41Sopenharmony_ci * 7191cb0ef41Sopenharmony_ci * It is possible to specify multiple context extensions (obj in the above 7201cb0ef41Sopenharmony_ci * example). 7211cb0ef41Sopenharmony_ci */ 7221cb0ef41Sopenharmony_ci V8_DEPRECATED("Use CompileFunction") 7231cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext( 7241cb0ef41Sopenharmony_ci Local<Context> context, Source* source, size_t arguments_count, 7251cb0ef41Sopenharmony_ci Local<String> arguments[], size_t context_extension_count, 7261cb0ef41Sopenharmony_ci Local<Object> context_extensions[], 7271cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions, 7281cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason = kNoCacheNoReason, 7291cb0ef41Sopenharmony_ci Local<ScriptOrModule>* script_or_module_out = nullptr); 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunction( 7321cb0ef41Sopenharmony_ci Local<Context> context, Source* source, size_t arguments_count = 0, 7331cb0ef41Sopenharmony_ci Local<String> arguments[] = nullptr, size_t context_extension_count = 0, 7341cb0ef41Sopenharmony_ci Local<Object> context_extensions[] = nullptr, 7351cb0ef41Sopenharmony_ci CompileOptions options = kNoCompileOptions, 7361cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason = kNoCacheNoReason); 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci /** 7391cb0ef41Sopenharmony_ci * Creates and returns code cache for the specified unbound_script. 7401cb0ef41Sopenharmony_ci * This will return nullptr if the script cannot be serialized. The 7411cb0ef41Sopenharmony_ci * CachedData returned by this function should be owned by the caller. 7421cb0ef41Sopenharmony_ci */ 7431cb0ef41Sopenharmony_ci static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script); 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci /** 7461cb0ef41Sopenharmony_ci * Creates and returns code cache for the specified unbound_module_script. 7471cb0ef41Sopenharmony_ci * This will return nullptr if the script cannot be serialized. The 7481cb0ef41Sopenharmony_ci * CachedData returned by this function should be owned by the caller. 7491cb0ef41Sopenharmony_ci */ 7501cb0ef41Sopenharmony_ci static CachedData* CreateCodeCache( 7511cb0ef41Sopenharmony_ci Local<UnboundModuleScript> unbound_module_script); 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ci /** 7541cb0ef41Sopenharmony_ci * Creates and returns code cache for the specified function that was 7551cb0ef41Sopenharmony_ci * previously produced by CompileFunction. 7561cb0ef41Sopenharmony_ci * This will return nullptr if the script cannot be serialized. The 7571cb0ef41Sopenharmony_ci * CachedData returned by this function should be owned by the caller. 7581cb0ef41Sopenharmony_ci */ 7591cb0ef41Sopenharmony_ci static CachedData* CreateCodeCacheForFunction(Local<Function> function); 7601cb0ef41Sopenharmony_ci 7611cb0ef41Sopenharmony_ci private: 7621cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal( 7631cb0ef41Sopenharmony_ci Isolate* isolate, Source* source, CompileOptions options, 7641cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason); 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ci static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal( 7671cb0ef41Sopenharmony_ci Local<Context> context, Source* source, size_t arguments_count, 7681cb0ef41Sopenharmony_ci Local<String> arguments[], size_t context_extension_count, 7691cb0ef41Sopenharmony_ci Local<Object> context_extensions[], CompileOptions options, 7701cb0ef41Sopenharmony_ci NoCacheReason no_cache_reason, 7711cb0ef41Sopenharmony_ci Local<ScriptOrModule>* script_or_module_out); 7721cb0ef41Sopenharmony_ci}; 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ciScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin, 7751cb0ef41Sopenharmony_ci CachedData* data, 7761cb0ef41Sopenharmony_ci ConsumeCodeCacheTask* consume_cache_task) 7771cb0ef41Sopenharmony_ci : source_string(string), 7781cb0ef41Sopenharmony_ci resource_name(origin.ResourceName()), 7791cb0ef41Sopenharmony_ci resource_line_offset(origin.LineOffset()), 7801cb0ef41Sopenharmony_ci resource_column_offset(origin.ColumnOffset()), 7811cb0ef41Sopenharmony_ci resource_options(origin.Options()), 7821cb0ef41Sopenharmony_ci source_map_url(origin.SourceMapUrl()), 7831cb0ef41Sopenharmony_ci host_defined_options(origin.GetHostDefinedOptions()), 7841cb0ef41Sopenharmony_ci cached_data(data), 7851cb0ef41Sopenharmony_ci consume_cache_task(consume_cache_task) {} 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ciScriptCompiler::Source::Source(Local<String> string, CachedData* data, 7881cb0ef41Sopenharmony_ci ConsumeCodeCacheTask* consume_cache_task) 7891cb0ef41Sopenharmony_ci : source_string(string), 7901cb0ef41Sopenharmony_ci cached_data(data), 7911cb0ef41Sopenharmony_ci consume_cache_task(consume_cache_task) {} 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_ciScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin, 7941cb0ef41Sopenharmony_ci CompileHintCallback callback, 7951cb0ef41Sopenharmony_ci void* callback_data) 7961cb0ef41Sopenharmony_ci : source_string(string), 7971cb0ef41Sopenharmony_ci resource_name(origin.ResourceName()), 7981cb0ef41Sopenharmony_ci resource_line_offset(origin.LineOffset()), 7991cb0ef41Sopenharmony_ci resource_column_offset(origin.ColumnOffset()), 8001cb0ef41Sopenharmony_ci resource_options(origin.Options()), 8011cb0ef41Sopenharmony_ci source_map_url(origin.SourceMapUrl()), 8021cb0ef41Sopenharmony_ci host_defined_options(origin.GetHostDefinedOptions()), 8031cb0ef41Sopenharmony_ci compile_hint_callback(callback), 8041cb0ef41Sopenharmony_ci compile_hint_callback_data(callback_data) {} 8051cb0ef41Sopenharmony_ci 8061cb0ef41Sopenharmony_ciconst ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData() 8071cb0ef41Sopenharmony_ci const { 8081cb0ef41Sopenharmony_ci return cached_data.get(); 8091cb0ef41Sopenharmony_ci} 8101cb0ef41Sopenharmony_ci 8111cb0ef41Sopenharmony_ciconst ScriptOriginOptions& ScriptCompiler::Source::GetResourceOptions() const { 8121cb0ef41Sopenharmony_ci return resource_options; 8131cb0ef41Sopenharmony_ci} 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ciModuleRequest* ModuleRequest::Cast(Data* data) { 8161cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 8171cb0ef41Sopenharmony_ci CheckCast(data); 8181cb0ef41Sopenharmony_ci#endif 8191cb0ef41Sopenharmony_ci return reinterpret_cast<ModuleRequest*>(data); 8201cb0ef41Sopenharmony_ci} 8211cb0ef41Sopenharmony_ci 8221cb0ef41Sopenharmony_ciModule* Module::Cast(Data* data) { 8231cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS 8241cb0ef41Sopenharmony_ci CheckCast(data); 8251cb0ef41Sopenharmony_ci#endif 8261cb0ef41Sopenharmony_ci return reinterpret_cast<Module*>(data); 8271cb0ef41Sopenharmony_ci} 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci} // namespace v8 8301cb0ef41Sopenharmony_ci 8311cb0ef41Sopenharmony_ci#endif // INCLUDE_V8_SCRIPT_H_ 832