1// Copyright 2021 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef INCLUDE_V8_SCRIPT_H_ 6#define INCLUDE_V8_SCRIPT_H_ 7 8#include <stddef.h> 9#include <stdint.h> 10 11#include <memory> 12#include <vector> 13 14#include "v8-callbacks.h" // NOLINT(build/include_directory) 15#include "v8-data.h" // NOLINT(build/include_directory) 16#include "v8-local-handle.h" // NOLINT(build/include_directory) 17#include "v8-maybe.h" // NOLINT(build/include_directory) 18#include "v8-message.h" // NOLINT(build/include_directory) 19#include "v8config.h" // NOLINT(build/include_directory) 20 21namespace v8 { 22 23class Function; 24class Message; 25class Object; 26class PrimitiveArray; 27class Script; 28 29namespace internal { 30class BackgroundDeserializeTask; 31struct ScriptStreamingData; 32} // namespace internal 33 34/** 35 * A container type that holds relevant metadata for module loading. 36 * 37 * This is passed back to the embedder as part of 38 * HostImportModuleDynamicallyCallback for module loading. 39 */ 40class V8_EXPORT ScriptOrModule { 41 public: 42 /** 43 * The name that was passed by the embedder as ResourceName to the 44 * ScriptOrigin. This can be either a v8::String or v8::Undefined. 45 */ 46 Local<Value> GetResourceName(); 47 48 /** 49 * The options that were passed by the embedder as HostDefinedOptions to 50 * the ScriptOrigin. 51 */ 52 Local<Data> HostDefinedOptions(); 53}; 54 55/** 56 * A compiled JavaScript script, not yet tied to a Context. 57 */ 58class V8_EXPORT UnboundScript { 59 public: 60 /** 61 * Binds the script to the currently entered context. 62 */ 63 Local<Script> BindToCurrentContext(); 64 65 int GetId() const; 66 Local<Value> GetScriptName(); 67 68 /** 69 * Data read from magic sourceURL comments. 70 */ 71 Local<Value> GetSourceURL(); 72 /** 73 * Data read from magic sourceMappingURL comments. 74 */ 75 Local<Value> GetSourceMappingURL(); 76 77 /** 78 * Returns zero based line number of the code_pos location in the script. 79 * -1 will be returned if no information available. 80 */ 81 int GetLineNumber(int code_pos = 0); 82 83 /** 84 * Returns zero based column number of the code_pos location in the script. 85 * -1 will be returned if no information available. 86 */ 87 int GetColumnNumber(int code_pos = 0); 88 89 static const int kNoScriptId = 0; 90}; 91 92/** 93 * A compiled JavaScript module, not yet tied to a Context. 94 */ 95class V8_EXPORT UnboundModuleScript : public Data { 96 public: 97 /** 98 * Data read from magic sourceURL comments. 99 */ 100 Local<Value> GetSourceURL(); 101 /** 102 * Data read from magic sourceMappingURL comments. 103 */ 104 Local<Value> GetSourceMappingURL(); 105}; 106 107/** 108 * A location in JavaScript source. 109 */ 110class V8_EXPORT Location { 111 public: 112 int GetLineNumber() { return line_number_; } 113 int GetColumnNumber() { return column_number_; } 114 115 Location(int line_number, int column_number) 116 : line_number_(line_number), column_number_(column_number) {} 117 118 private: 119 int line_number_; 120 int column_number_; 121}; 122 123class V8_EXPORT ModuleRequest : public Data { 124 public: 125 /** 126 * Returns the module specifier for this ModuleRequest. 127 */ 128 Local<String> GetSpecifier() const; 129 130 /** 131 * Returns the source code offset of this module request. 132 * Use Module::SourceOffsetToLocation to convert this to line/column numbers. 133 */ 134 int GetSourceOffset() const; 135 136 /** 137 * Contains the import assertions for this request in the form: 138 * [key1, value1, source_offset1, key2, value2, source_offset2, ...]. 139 * The keys and values are of type v8::String, and the source offsets are of 140 * type Int32. Use Module::SourceOffsetToLocation to convert the source 141 * offsets to Locations with line/column numbers. 142 * 143 * All assertions present in the module request will be supplied in this 144 * list, regardless of whether they are supported by the host. Per 145 * https://tc39.es/proposal-import-assertions/#sec-hostgetsupportedimportassertions, 146 * hosts are expected to ignore assertions that they do not support (as 147 * opposed to, for example, triggering an error if an unsupported assertion is 148 * present). 149 */ 150 Local<FixedArray> GetImportAssertions() const; 151 152 V8_INLINE static ModuleRequest* Cast(Data* data); 153 154 private: 155 static void CheckCast(Data* obj); 156}; 157 158/** 159 * A compiled JavaScript module. 160 */ 161class V8_EXPORT Module : public Data { 162 public: 163 /** 164 * The different states a module can be in. 165 * 166 * This corresponds to the states used in ECMAScript except that "evaluated" 167 * is split into kEvaluated and kErrored, indicating success and failure, 168 * respectively. 169 */ 170 enum Status { 171 kUninstantiated, 172 kInstantiating, 173 kInstantiated, 174 kEvaluating, 175 kEvaluated, 176 kErrored 177 }; 178 179 /** 180 * Returns the module's current status. 181 */ 182 Status GetStatus() const; 183 184 /** 185 * For a module in kErrored status, this returns the corresponding exception. 186 */ 187 Local<Value> GetException() const; 188 189 /** 190 * Returns the ModuleRequests for this module. 191 */ 192 Local<FixedArray> GetModuleRequests() const; 193 194 /** 195 * For the given source text offset in this module, returns the corresponding 196 * Location with line and column numbers. 197 */ 198 Location SourceOffsetToLocation(int offset) const; 199 200 /** 201 * Returns the identity hash for this object. 202 */ 203 int GetIdentityHash() const; 204 205 using ResolveModuleCallback = MaybeLocal<Module> (*)( 206 Local<Context> context, Local<String> specifier, 207 Local<FixedArray> import_assertions, Local<Module> referrer); 208 209 /** 210 * Instantiates the module and its dependencies. 211 * 212 * Returns an empty Maybe<bool> if an exception occurred during 213 * instantiation. (In the case where the callback throws an exception, that 214 * exception is propagated.) 215 */ 216 V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule( 217 Local<Context> context, ResolveModuleCallback callback); 218 219 /** 220 * Evaluates the module and its dependencies. 221 * 222 * If status is kInstantiated, run the module's code and return a Promise 223 * object. On success, set status to kEvaluated and resolve the Promise with 224 * the completion value; on failure, set status to kErrored and reject the 225 * Promise with the error. 226 * 227 * If IsGraphAsync() is false, the returned Promise is settled. 228 */ 229 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Evaluate(Local<Context> context); 230 231 /** 232 * Returns the namespace object of this module. 233 * 234 * The module's status must be at least kInstantiated. 235 */ 236 Local<Value> GetModuleNamespace(); 237 238 /** 239 * Returns the corresponding context-unbound module script. 240 * 241 * The module must be unevaluated, i.e. its status must not be kEvaluating, 242 * kEvaluated or kErrored. 243 */ 244 Local<UnboundModuleScript> GetUnboundModuleScript(); 245 246 /** 247 * Returns the underlying script's id. 248 * 249 * The module must be a SourceTextModule and must not have a kErrored status. 250 */ 251 int ScriptId() const; 252 253 /** 254 * Returns whether this module or any of its requested modules is async, 255 * i.e. contains top-level await. 256 * 257 * The module's status must be at least kInstantiated. 258 */ 259 bool IsGraphAsync() const; 260 261 /** 262 * Returns whether the module is a SourceTextModule. 263 */ 264 bool IsSourceTextModule() const; 265 266 /** 267 * Returns whether the module is a SyntheticModule. 268 */ 269 bool IsSyntheticModule() const; 270 271 /* 272 * Callback defined in the embedder. This is responsible for setting 273 * the module's exported values with calls to SetSyntheticModuleExport(). 274 * The callback must return a resolved Promise to indicate success (where no 275 * exception was thrown) and return an empy MaybeLocal to indicate falure 276 * (where an exception was thrown). 277 */ 278 using SyntheticModuleEvaluationSteps = 279 MaybeLocal<Value> (*)(Local<Context> context, Local<Module> module); 280 281 /** 282 * Creates a new SyntheticModule with the specified export names, where 283 * evaluation_steps will be executed upon module evaluation. 284 * export_names must not contain duplicates. 285 * module_name is used solely for logging/debugging and doesn't affect module 286 * behavior. 287 */ 288 static Local<Module> CreateSyntheticModule( 289 Isolate* isolate, Local<String> module_name, 290 const std::vector<Local<String>>& export_names, 291 SyntheticModuleEvaluationSteps evaluation_steps); 292 293 /** 294 * Set this module's exported value for the name export_name to the specified 295 * export_value. This method must be called only on Modules created via 296 * CreateSyntheticModule. An error will be thrown if export_name is not one 297 * of the export_names that were passed in that CreateSyntheticModule call. 298 * Returns Just(true) on success, Nothing<bool>() if an error was thrown. 299 */ 300 V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport( 301 Isolate* isolate, Local<String> export_name, Local<Value> export_value); 302 303 /** 304 * Search the modules requested directly or indirectly by the module for 305 * any top-level await that has not yet resolved. If there is any, the 306 * returned vector contains a tuple of the unresolved module and a message 307 * with the pending top-level await. 308 * An embedder may call this before exiting to improve error messages. 309 */ 310 std::vector<std::tuple<Local<Module>, Local<Message>>> 311 GetStalledTopLevelAwaitMessage(Isolate* isolate); 312 313 V8_INLINE static Module* Cast(Data* data); 314 315 private: 316 static void CheckCast(Data* obj); 317}; 318 319/** 320 * A compiled JavaScript script, tied to a Context which was active when the 321 * script was compiled. 322 */ 323class V8_EXPORT Script { 324 public: 325 /** 326 * A shorthand for ScriptCompiler::Compile(). 327 */ 328 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 329 Local<Context> context, Local<String> source, 330 ScriptOrigin* origin = nullptr); 331 332 /** 333 * Runs the script returning the resulting value. It will be run in the 334 * context in which it was created (ScriptCompiler::CompileBound or 335 * UnboundScript::BindToCurrentContext()). 336 */ 337 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context); 338 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context, 339 Local<Data> host_defined_options); 340 341 /** 342 * Returns the corresponding context-unbound script. 343 */ 344 Local<UnboundScript> GetUnboundScript(); 345 346 /** 347 * The name that was passed by the embedder as ResourceName to the 348 * ScriptOrigin. This can be either a v8::String or v8::Undefined. 349 */ 350 Local<Value> GetResourceName(); 351 352 /** 353 * If the script was compiled, returns the positions of lazy functions which 354 * were eventually compiled and executed. 355 */ 356 std::vector<int> GetProducedCompileHints() const; 357}; 358 359enum class ScriptType { kClassic, kModule }; 360 361/** 362 * For compiling scripts. 363 */ 364class V8_EXPORT ScriptCompiler { 365 public: 366 class ConsumeCodeCacheTask; 367 368 /** 369 * Compilation data that the embedder can cache and pass back to speed up 370 * future compilations. The data is produced if the CompilerOptions passed to 371 * the compilation functions in ScriptCompiler contains produce_data_to_cache 372 * = true. The data to cache can then can be retrieved from 373 * UnboundScript. 374 */ 375 struct V8_EXPORT CachedData { 376 enum BufferPolicy { BufferNotOwned, BufferOwned }; 377 378 CachedData() 379 : data(nullptr), 380 length(0), 381 rejected(false), 382 buffer_policy(BufferNotOwned) {} 383 384 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of 385 // data and guarantees that it stays alive until the CachedData object is 386 // destroyed. If the policy is BufferOwned, the given data will be deleted 387 // (with delete[]) when the CachedData object is destroyed. 388 CachedData(const uint8_t* data, int length, 389 BufferPolicy buffer_policy = BufferNotOwned); 390 ~CachedData(); 391 // TODO(marja): Async compilation; add constructors which take a callback 392 // which will be called when V8 no longer needs the data. 393 const uint8_t* data; 394 int length; 395 bool rejected; 396 BufferPolicy buffer_policy; 397 398 // Prevent copying. 399 CachedData(const CachedData&) = delete; 400 CachedData& operator=(const CachedData&) = delete; 401 }; 402 403 /** 404 * Source code which can be then compiled to a UnboundScript or Script. 405 */ 406 class Source { 407 public: 408 // Source takes ownership of both CachedData and CodeCacheConsumeTask. 409 // The caller *must* ensure that the cached data is from a trusted source. 410 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin, 411 CachedData* cached_data = nullptr, 412 ConsumeCodeCacheTask* consume_cache_task = nullptr); 413 // Source takes ownership of both CachedData and CodeCacheConsumeTask. 414 V8_INLINE explicit Source( 415 Local<String> source_string, CachedData* cached_data = nullptr, 416 ConsumeCodeCacheTask* consume_cache_task = nullptr); 417 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin, 418 CompileHintCallback callback, void* callback_data); 419 V8_INLINE ~Source() = default; 420 421 // Ownership of the CachedData or its buffers is *not* transferred to the 422 // caller. The CachedData object is alive as long as the Source object is 423 // alive. 424 V8_INLINE const CachedData* GetCachedData() const; 425 426 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const; 427 428 private: 429 friend class ScriptCompiler; 430 431 Local<String> source_string; 432 433 // Origin information 434 Local<Value> resource_name; 435 int resource_line_offset; 436 int resource_column_offset; 437 ScriptOriginOptions resource_options; 438 Local<Value> source_map_url; 439 Local<Data> host_defined_options; 440 441 // Cached data from previous compilation (if a kConsume*Cache flag is 442 // set), or hold newly generated cache data (kProduce*Cache flags) are 443 // set when calling a compile method. 444 std::unique_ptr<CachedData> cached_data; 445 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task; 446 447 // For requesting compile hints from the embedder. 448 CompileHintCallback compile_hint_callback = nullptr; 449 void* compile_hint_callback_data = nullptr; 450 }; 451 452 /** 453 * For streaming incomplete script data to V8. The embedder should implement a 454 * subclass of this class. 455 */ 456 class V8_EXPORT ExternalSourceStream { 457 public: 458 virtual ~ExternalSourceStream() = default; 459 460 /** 461 * V8 calls this to request the next chunk of data from the embedder. This 462 * function will be called on a background thread, so it's OK to block and 463 * wait for the data, if the embedder doesn't have data yet. Returns the 464 * length of the data returned. When the data ends, GetMoreData should 465 * return 0. Caller takes ownership of the data. 466 * 467 * When streaming UTF-8 data, V8 handles multi-byte characters split between 468 * two data chunks, but doesn't handle multi-byte characters split between 469 * more than two data chunks. The embedder can avoid this problem by always 470 * returning at least 2 bytes of data. 471 * 472 * When streaming UTF-16 data, V8 does not handle characters split between 473 * two data chunks. The embedder has to make sure that chunks have an even 474 * length. 475 * 476 * If the embedder wants to cancel the streaming, they should make the next 477 * GetMoreData call return 0. V8 will interpret it as end of data (and most 478 * probably, parsing will fail). The streaming task will return as soon as 479 * V8 has parsed the data it received so far. 480 */ 481 virtual size_t GetMoreData(const uint8_t** src) = 0; 482 }; 483 484 /** 485 * Source code which can be streamed into V8 in pieces. It will be parsed 486 * while streaming and compiled after parsing has completed. StreamedSource 487 * must be kept alive while the streaming task is run (see ScriptStreamingTask 488 * below). 489 */ 490 class V8_EXPORT StreamedSource { 491 public: 492 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 }; 493 494 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream, 495 Encoding encoding); 496 ~StreamedSource(); 497 498 internal::ScriptStreamingData* impl() const { return impl_.get(); } 499 500 // Prevent copying. 501 StreamedSource(const StreamedSource&) = delete; 502 StreamedSource& operator=(const StreamedSource&) = delete; 503 504 private: 505 std::unique_ptr<internal::ScriptStreamingData> impl_; 506 }; 507 508 /** 509 * A streaming task which the embedder must run on a background thread to 510 * stream scripts into V8. Returned by ScriptCompiler::StartStreaming. 511 */ 512 class V8_EXPORT ScriptStreamingTask final { 513 public: 514 void Run(); 515 516 private: 517 friend class ScriptCompiler; 518 519 explicit ScriptStreamingTask(internal::ScriptStreamingData* data) 520 : data_(data) {} 521 522 internal::ScriptStreamingData* data_; 523 }; 524 525 /** 526 * A task which the embedder must run on a background thread to 527 * consume a V8 code cache. Returned by 528 * ScriptCompiler::StartConsumingCodeCache. 529 */ 530 class V8_EXPORT ConsumeCodeCacheTask final { 531 public: 532 ~ConsumeCodeCacheTask(); 533 534 void Run(); 535 536 /** 537 * Provides the source text string and origin information to the consumption 538 * task. May be called before, during, or after Run(). This step checks 539 * whether the script matches an existing script in the Isolate's 540 * compilation cache. To check whether such a script was found, call 541 * ShouldMergeWithExistingScript. 542 * 543 * The Isolate provided must be the same one used during 544 * StartConsumingCodeCache and must be currently entered on the thread that 545 * calls this function. The source text and origin provided in this step 546 * must precisely match those used later in the ScriptCompiler::Source that 547 * will contain this ConsumeCodeCacheTask. 548 */ 549 void SourceTextAvailable(Isolate* isolate, Local<String> source_text, 550 const ScriptOrigin& origin); 551 552 /** 553 * Returns whether the embedder should call MergeWithExistingScript. This 554 * function may be called from any thread, any number of times, but its 555 * return value is only meaningful after SourceTextAvailable has completed. 556 */ 557 bool ShouldMergeWithExistingScript() const; 558 559 /** 560 * Merges newly deserialized data into an existing script which was found 561 * during SourceTextAvailable. May be called only after Run() has completed. 562 * Can execute on any thread, like Run(). 563 */ 564 void MergeWithExistingScript(); 565 566 private: 567 friend class ScriptCompiler; 568 569 explicit ConsumeCodeCacheTask( 570 std::unique_ptr<internal::BackgroundDeserializeTask> impl); 571 572 std::unique_ptr<internal::BackgroundDeserializeTask> impl_; 573 }; 574 575 enum CompileOptions { 576 kNoCompileOptions = 0, 577 kConsumeCodeCache, 578 kEagerCompile, 579 kProduceCompileHints, 580 kConsumeCompileHints 581 }; 582 583 /** 584 * The reason for which we are not requesting or providing a code cache. 585 */ 586 enum NoCacheReason { 587 kNoCacheNoReason = 0, 588 kNoCacheBecauseCachingDisabled, 589 kNoCacheBecauseNoResource, 590 kNoCacheBecauseInlineScript, 591 kNoCacheBecauseModule, 592 kNoCacheBecauseStreamingSource, 593 kNoCacheBecauseInspector, 594 kNoCacheBecauseScriptTooSmall, 595 kNoCacheBecauseCacheTooCold, 596 kNoCacheBecauseV8Extension, 597 kNoCacheBecauseExtensionModule, 598 kNoCacheBecausePacScript, 599 kNoCacheBecauseInDocumentWrite, 600 kNoCacheBecauseResourceWithNoCacheHandler, 601 kNoCacheBecauseDeferredProduceCodeCache 602 }; 603 604 /** 605 * Compiles the specified script (context-independent). 606 * Cached data as part of the source object can be optionally produced to be 607 * consumed later to speed up compilation of identical source scripts. 608 * 609 * Note that when producing cached data, the source must point to NULL for 610 * cached data. When consuming cached data, the cached data must have been 611 * produced by the same version of V8, and the embedder needs to ensure the 612 * cached data is the correct one for the given script. 613 * 614 * \param source Script source code. 615 * \return Compiled script object (context independent; for running it must be 616 * bound to a context). 617 */ 618 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript( 619 Isolate* isolate, Source* source, 620 CompileOptions options = kNoCompileOptions, 621 NoCacheReason no_cache_reason = kNoCacheNoReason); 622 623 /** 624 * Compiles the specified script (bound to current context). 625 * 626 * \param source Script source code. 627 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() 628 * using pre_data speeds compilation if it's done multiple times. 629 * Owned by caller, no references are kept when this function returns. 630 * \return Compiled script object, bound to the context that was active 631 * when this function was called. When run it will always use this 632 * context. 633 */ 634 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 635 Local<Context> context, Source* source, 636 CompileOptions options = kNoCompileOptions, 637 NoCacheReason no_cache_reason = kNoCacheNoReason); 638 639 /** 640 * Returns a task which streams script data into V8, or NULL if the script 641 * cannot be streamed. The user is responsible for running the task on a 642 * background thread and deleting it. When ran, the task starts parsing the 643 * script, and it will request data from the StreamedSource as needed. When 644 * ScriptStreamingTask::Run exits, all data has been streamed and the script 645 * can be compiled (see Compile below). 646 * 647 * This API allows to start the streaming with as little data as possible, and 648 * the remaining data (for example, the ScriptOrigin) is passed to Compile. 649 */ 650 static ScriptStreamingTask* StartStreaming( 651 Isolate* isolate, StreamedSource* source, 652 ScriptType type = ScriptType::kClassic, 653 CompileOptions options = kNoCompileOptions); 654 655 static ConsumeCodeCacheTask* StartConsumingCodeCache( 656 Isolate* isolate, std::unique_ptr<CachedData> source); 657 658 /** 659 * Compiles a streamed script (bound to current context). 660 * 661 * This can only be called after the streaming has finished 662 * (ScriptStreamingTask has been run). V8 doesn't construct the source string 663 * during streaming, so the embedder needs to pass the full source here. 664 */ 665 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( 666 Local<Context> context, StreamedSource* source, 667 Local<String> full_source_string, const ScriptOrigin& origin); 668 669 /** 670 * Return a version tag for CachedData for the current V8 version & flags. 671 * 672 * This value is meant only for determining whether a previously generated 673 * CachedData instance is still valid; the tag has no other meaing. 674 * 675 * Background: The data carried by CachedData may depend on the exact 676 * V8 version number or current compiler flags. This means that when 677 * persisting CachedData, the embedder must take care to not pass in 678 * data from another V8 version, or the same version with different 679 * features enabled. 680 * 681 * The easiest way to do so is to clear the embedder's cache on any 682 * such change. 683 * 684 * Alternatively, this tag can be stored alongside the cached data and 685 * compared when it is being used. 686 */ 687 static uint32_t CachedDataVersionTag(); 688 689 /** 690 * Compile an ES module, returning a Module that encapsulates 691 * the compiled code. 692 * 693 * Corresponds to the ParseModule abstract operation in the 694 * ECMAScript specification. 695 */ 696 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule( 697 Isolate* isolate, Source* source, 698 CompileOptions options = kNoCompileOptions, 699 NoCacheReason no_cache_reason = kNoCacheNoReason); 700 701 /** 702 * Compiles a streamed module script. 703 * 704 * This can only be called after the streaming has finished 705 * (ScriptStreamingTask has been run). V8 doesn't construct the source string 706 * during streaming, so the embedder needs to pass the full source here. 707 */ 708 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule( 709 Local<Context> context, StreamedSource* v8_source, 710 Local<String> full_source_string, const ScriptOrigin& origin); 711 712 /** 713 * Compile a function for a given context. This is equivalent to running 714 * 715 * with (obj) { 716 * return function(args) { ... } 717 * } 718 * 719 * It is possible to specify multiple context extensions (obj in the above 720 * example). 721 */ 722 V8_DEPRECATED("Use CompileFunction") 723 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext( 724 Local<Context> context, Source* source, size_t arguments_count, 725 Local<String> arguments[], size_t context_extension_count, 726 Local<Object> context_extensions[], 727 CompileOptions options = kNoCompileOptions, 728 NoCacheReason no_cache_reason = kNoCacheNoReason, 729 Local<ScriptOrModule>* script_or_module_out = nullptr); 730 731 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunction( 732 Local<Context> context, Source* source, size_t arguments_count = 0, 733 Local<String> arguments[] = nullptr, size_t context_extension_count = 0, 734 Local<Object> context_extensions[] = nullptr, 735 CompileOptions options = kNoCompileOptions, 736 NoCacheReason no_cache_reason = kNoCacheNoReason); 737 738 /** 739 * Creates and returns code cache for the specified unbound_script. 740 * This will return nullptr if the script cannot be serialized. The 741 * CachedData returned by this function should be owned by the caller. 742 */ 743 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script); 744 745 /** 746 * Creates and returns code cache for the specified unbound_module_script. 747 * This will return nullptr if the script cannot be serialized. The 748 * CachedData returned by this function should be owned by the caller. 749 */ 750 static CachedData* CreateCodeCache( 751 Local<UnboundModuleScript> unbound_module_script); 752 753 /** 754 * Creates and returns code cache for the specified function that was 755 * previously produced by CompileFunction. 756 * This will return nullptr if the script cannot be serialized. The 757 * CachedData returned by this function should be owned by the caller. 758 */ 759 static CachedData* CreateCodeCacheForFunction(Local<Function> function); 760 761 private: 762 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal( 763 Isolate* isolate, Source* source, CompileOptions options, 764 NoCacheReason no_cache_reason); 765 766 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal( 767 Local<Context> context, Source* source, size_t arguments_count, 768 Local<String> arguments[], size_t context_extension_count, 769 Local<Object> context_extensions[], CompileOptions options, 770 NoCacheReason no_cache_reason, 771 Local<ScriptOrModule>* script_or_module_out); 772}; 773 774ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin, 775 CachedData* data, 776 ConsumeCodeCacheTask* consume_cache_task) 777 : source_string(string), 778 resource_name(origin.ResourceName()), 779 resource_line_offset(origin.LineOffset()), 780 resource_column_offset(origin.ColumnOffset()), 781 resource_options(origin.Options()), 782 source_map_url(origin.SourceMapUrl()), 783 host_defined_options(origin.GetHostDefinedOptions()), 784 cached_data(data), 785 consume_cache_task(consume_cache_task) {} 786 787ScriptCompiler::Source::Source(Local<String> string, CachedData* data, 788 ConsumeCodeCacheTask* consume_cache_task) 789 : source_string(string), 790 cached_data(data), 791 consume_cache_task(consume_cache_task) {} 792 793ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin, 794 CompileHintCallback callback, 795 void* callback_data) 796 : source_string(string), 797 resource_name(origin.ResourceName()), 798 resource_line_offset(origin.LineOffset()), 799 resource_column_offset(origin.ColumnOffset()), 800 resource_options(origin.Options()), 801 source_map_url(origin.SourceMapUrl()), 802 host_defined_options(origin.GetHostDefinedOptions()), 803 compile_hint_callback(callback), 804 compile_hint_callback_data(callback_data) {} 805 806const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData() 807 const { 808 return cached_data.get(); 809} 810 811const ScriptOriginOptions& ScriptCompiler::Source::GetResourceOptions() const { 812 return resource_options; 813} 814 815ModuleRequest* ModuleRequest::Cast(Data* data) { 816#ifdef V8_ENABLE_CHECKS 817 CheckCast(data); 818#endif 819 return reinterpret_cast<ModuleRequest*>(data); 820} 821 822Module* Module::Cast(Data* data) { 823#ifdef V8_ENABLE_CHECKS 824 CheckCast(data); 825#endif 826 return reinterpret_cast<Module*>(data); 827} 828 829} // namespace v8 830 831#endif // INCLUDE_V8_SCRIPT_H_ 832