Loading...
Searching...
No Matches
v8-script.h
Go to the documentation of this file.
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 <tuple>
13#include <vector>
14
15#include "v8-callbacks.h" // NOLINT(build/include_directory)
16#include "v8-data.h" // NOLINT(build/include_directory)
17#include "v8-local-handle.h" // NOLINT(build/include_directory)
18#include "v8-maybe.h" // NOLINT(build/include_directory)
19#include "v8-memory-span.h" // NOLINT(build/include_directory)
20#include "v8-message.h" // NOLINT(build/include_directory)
21#include "v8config.h" // NOLINT(build/include_directory)
22
23namespace v8 {
24
25class Function;
26class Message;
27class Object;
28class PrimitiveArray;
29class Script;
30
31namespace internal {
32class BackgroundDeserializeTask;
33struct ScriptStreamingData;
34} // namespace internal
35
43 public:
49
55};
56
61 public:
66
67 int GetId() const;
69
78
83 int GetLineNumber(int code_pos = 0);
84
89 int GetColumnNumber(int code_pos = 0);
90
91 static const int kNoScriptId = 0;
92};
93
98 public:
107};
108
113 public:
114 int GetLineNumber() { return line_number_; }
115 int GetColumnNumber() { return column_number_; }
116
117 Location(int line_number, int column_number)
118 : line_number_(line_number), column_number_(column_number) {}
119
120 private:
121 int line_number_;
122 int column_number_;
123};
124
126 public:
131
136 int GetSourceOffset() const;
137
152
153 V8_DEPRECATE_SOON("Use GetImportAttributes instead")
154 Local<FixedArray> GetImportAssertions() const {
155 return GetImportAttributes();
156 }
157
158 V8_INLINE static ModuleRequest* Cast(Data* data);
159
160 private:
161 static void CheckCast(Data* obj);
162};
163
167class V8_EXPORT Module : public Data {
168 public:
176 enum Status {
182 kErrored
183 };
184
189
194
199
205
209 int GetIdentityHash() const;
210
212 Local<Context> context, Local<String> specifier,
213 Local<FixedArray> import_assertions, Local<Module> referrer);
214
223 Local<Context> context, ResolveModuleCallback callback);
224
236
243
251
257 int ScriptId() const;
258
265 bool IsGraphAsync() const;
266
270 bool IsSourceTextModule() const;
271
275 bool IsSyntheticModule() const;
276
277 /*
278 * Callback defined in the embedder. This is responsible for setting
279 * the module's exported values with calls to SetSyntheticModuleExport().
280 * The callback must return a resolved Promise to indicate success (where no
281 * exception was thrown) and return an empy MaybeLocal to indicate falure
282 * (where an exception was thrown).
283 */
286
295 Isolate* isolate, Local<String> module_name,
296 const MemorySpan<const Local<String>>& export_names,
297 SyntheticModuleEvaluationSteps evaluation_steps);
298
307 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
308
316 std::pair<LocalVector<Module>, LocalVector<Message>>
318
319 V8_INLINE static Module* Cast(Data* data);
320
321 private:
322 static void CheckCast(Data* obj);
323};
324
329class V8_EXPORT Script : public Data {
330 public:
335 Local<Context> context, Local<String> source,
336 ScriptOrigin* origin = nullptr);
337
345 Local<Data> host_defined_options);
346
351
357
362 std::vector<int> GetProducedCompileHints() const;
363};
364
366
371 public:
373
382 enum BufferPolicy { BufferNotOwned, BufferOwned };
383
385 : data(nullptr),
386 length(0),
387 rejected(false),
388 buffer_policy(BufferNotOwned) {}
389
390 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
391 // data and guarantees that it stays alive until the CachedData object is
392 // destroyed. If the policy is BufferOwned, the given data will be deleted
393 // (with delete[]) when the CachedData object is destroyed.
394 CachedData(const uint8_t* data, int length,
395 BufferPolicy buffer_policy = BufferNotOwned);
397
399 // Don't change order/existing values of this enum since it keys into the
400 // `code_cache_reject_reason` histogram. Append-only!
402 kMagicNumberMismatch = 1,
403 kVersionMismatch = 2,
404 kSourceMismatch = 3,
405 kFlagsMismatch = 5,
406 kChecksumMismatch = 6,
407 kInvalidHeader = 7,
408 kLengthMismatch = 8,
409 kReadOnlySnapshotChecksumMismatch = 9,
410
411 // This should always point at the last real enum value.
412 kLast = kReadOnlySnapshotChecksumMismatch
413 };
414
415 // Check if the CachedData can be loaded in the given isolate.
417
418 // TODO(marja): Async compilation; add constructors which take a callback
419 // which will be called when V8 no longer needs the data.
420 const uint8_t* data;
424
425 // Prevent copying.
426 CachedData(const CachedData&) = delete;
427 CachedData& operator=(const CachedData&) = delete;
428 };
429
431 // V8 did not attempt to find this script in its in-memory cache.
432 kNotAttempted,
433
434 // V8 found a previously compiled copy of this script in its in-memory
435 // cache. Any data generated by a streaming compilation or background
436 // deserialization was abandoned.
437 kHit,
438
439 // V8 didn't have any previously compiled data for this script.
440 kMiss,
441
442 // V8 had some previously compiled data for an identical script, but the
443 // data was incomplete.
444 kPartial,
445 };
446
447 // Details about what happened during a compilation.
449 InMemoryCacheResult in_memory_cache_result =
450 InMemoryCacheResult::kNotAttempted;
451
452 static constexpr int64_t kTimeNotMeasured = -1;
453 int64_t foreground_time_in_microseconds = kTimeNotMeasured;
454 int64_t background_time_in_microseconds = kTimeNotMeasured;
455 };
456
460 class Source {
461 public:
462 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
463 // The caller *must* ensure that the cached data is from a trusted source.
464 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
465 CachedData* cached_data = nullptr,
466 ConsumeCodeCacheTask* consume_cache_task = nullptr);
467 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
468 V8_INLINE explicit Source(
469 Local<String> source_string, CachedData* cached_data = nullptr,
470 ConsumeCodeCacheTask* consume_cache_task = nullptr);
471 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
472 CompileHintCallback callback, void* callback_data);
473 V8_INLINE ~Source() = default;
474
475 // Ownership of the CachedData or its buffers is *not* transferred to the
476 // caller. The CachedData object is alive as long as the Source object is
477 // alive.
478 V8_INLINE const CachedData* GetCachedData() const;
479
480 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
481
482 V8_INLINE const CompilationDetails& GetCompilationDetails() const;
483
484 private:
485 friend class ScriptCompiler;
486
487 Local<String> source_string;
488
489 // Origin information
490 Local<Value> resource_name;
491 int resource_line_offset = -1;
492 int resource_column_offset = -1;
493 ScriptOriginOptions resource_options;
494 Local<Value> source_map_url;
495 Local<Data> host_defined_options;
496
497 // Cached data from previous compilation (if a kConsume*Cache flag is
498 // set), or hold newly generated cache data (kProduce*Cache flags) are
499 // set when calling a compile method.
500 std::unique_ptr<CachedData> cached_data;
501 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
502
503 // For requesting compile hints from the embedder.
504 CompileHintCallback compile_hint_callback = nullptr;
505 void* compile_hint_callback_data = nullptr;
506
507 // V8 writes this data and never reads it. It exists only to be informative
508 // to the embedder.
509 CompilationDetails compilation_details;
510 };
511
517 public:
518 virtual ~ExternalSourceStream() = default;
519
541 virtual size_t GetMoreData(const uint8_t** src) = 0;
542 };
543
551 public:
552 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
553
554 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
555 Encoding encoding);
557
558 internal::ScriptStreamingData* impl() const { return impl_.get(); }
559
560 // Prevent copying.
563
564 CompilationDetails& compilation_details() { return compilation_details_; }
565
566 private:
567 std::unique_ptr<internal::ScriptStreamingData> impl_;
568
569 // V8 writes this data and never reads it. It exists only to be informative
570 // to the embedder.
571 CompilationDetails compilation_details_;
572 };
573
579 public:
580 void Run();
581
582 private:
583 friend class ScriptCompiler;
584
585 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
586 : data_(data) {}
587
588 internal::ScriptStreamingData* data_;
589 };
590
597 public:
599
600 void Run();
601
615 void SourceTextAvailable(Isolate* isolate, Local<String> source_text,
616 const ScriptOrigin& origin);
617
624
631
632 private:
633 friend class ScriptCompiler;
634
635 explicit ConsumeCodeCacheTask(
636 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
637
638 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
639 };
640
642 kNoCompileOptions = 0,
646 kConsumeCompileHints
647 };
648
653 kNoCacheNoReason = 0,
667 kNoCacheBecauseDeferredProduceCodeCache
668 };
669
685 Isolate* isolate, Source* source,
686 CompileOptions options = kNoCompileOptions,
687 NoCacheReason no_cache_reason = kNoCacheNoReason);
688
701 Local<Context> context, Source* source,
702 CompileOptions options = kNoCompileOptions,
703 NoCacheReason no_cache_reason = kNoCacheNoReason);
704
717 Isolate* isolate, StreamedSource* source,
718 ScriptType type = ScriptType::kClassic,
719 CompileOptions options = kNoCompileOptions,
720 CompileHintCallback compile_hint_callback = nullptr,
721 void* compile_hint_callback_data = nullptr);
722
724 Isolate* isolate, std::unique_ptr<CachedData> source);
725
734 Local<Context> context, StreamedSource* source,
735 Local<String> full_source_string, const ScriptOrigin& origin);
736
755 static uint32_t CachedDataVersionTag();
756
765 Isolate* isolate, Source* source,
766 CompileOptions options = kNoCompileOptions,
767 NoCacheReason no_cache_reason = kNoCacheNoReason);
768
777 Local<Context> context, StreamedSource* v8_source,
778 Local<String> full_source_string, const ScriptOrigin& origin);
779
791 Local<Context> context, Source* source, size_t arguments_count = 0,
792 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
793 Local<Object> context_extensions[] = nullptr,
794 CompileOptions options = kNoCompileOptions,
795 NoCacheReason no_cache_reason = kNoCacheNoReason);
796
803
810 Local<UnboundModuleScript> unbound_module_script);
811
819
820 private:
821 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
822 Isolate* isolate, Source* source, CompileOptions options,
823 NoCacheReason no_cache_reason);
824
825 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
826 Local<Context> context, Source* source, size_t arguments_count,
827 Local<String> arguments[], size_t context_extension_count,
828 Local<Object> context_extensions[], CompileOptions options,
829 NoCacheReason no_cache_reason,
830 Local<ScriptOrModule>* script_or_module_out);
831};
832
834 CachedData* data,
835 ConsumeCodeCacheTask* consume_cache_task)
836 : source_string(string),
837 resource_name(origin.ResourceName()),
838 resource_line_offset(origin.LineOffset()),
839 resource_column_offset(origin.ColumnOffset()),
840 resource_options(origin.Options()),
841 source_map_url(origin.SourceMapUrl()),
842 host_defined_options(origin.GetHostDefinedOptions()),
843 cached_data(data),
844 consume_cache_task(consume_cache_task) {}
845
847 ConsumeCodeCacheTask* consume_cache_task)
848 : source_string(string),
849 cached_data(data),
850 consume_cache_task(consume_cache_task) {}
851
853 CompileHintCallback callback,
854 void* callback_data)
855 : source_string(string),
856 resource_name(origin.ResourceName()),
857 resource_line_offset(origin.LineOffset()),
858 resource_column_offset(origin.ColumnOffset()),
859 resource_options(origin.Options()),
860 source_map_url(origin.SourceMapUrl()),
861 host_defined_options(origin.GetHostDefinedOptions()),
862 compile_hint_callback(callback),
863 compile_hint_callback_data(callback_data) {}
864
866 const {
867 return cached_data.get();
868}
869
871 return resource_options;
872}
873
876 return compilation_details;
877}
878
880#ifdef V8_ENABLE_CHECKS
881 CheckCast(data);
882#endif
883 return reinterpret_cast<ModuleRequest*>(data);
884}
885
887#ifdef V8_ENABLE_CHECKS
888 CheckCast(data);
889#endif
890 return reinterpret_cast<Module*>(data);
891}
892
893} // namespace v8
894
895#endif // INCLUDE_V8_SCRIPT_H_
Definition: v8-data.h:18
Definition: v8-data.h:62
Definition: v8-isolate.h:210
Definition: v8-local-handle.h:483
Definition: v8-local-handle.h:258
Definition: v8-script.h:112
int GetLineNumber()
Definition: v8-script.h:114
Location(int line_number, int column_number)
Definition: v8-script.h:117
int GetColumnNumber()
Definition: v8-script.h:115
Definition: v8-local-handle.h:619
Definition: v8-maybe.h:32
Definition: v8-memory-span.h:29
Definition: v8-script.h:125
static ModuleRequest * Cast(Data *data)
Definition: v8-script.h:879
Local< FixedArray > GetImportAttributes() const
Local< String > GetSpecifier() const
int GetSourceOffset() const
Definition: v8-script.h:167
Location SourceOffsetToLocation(int offset) const
MaybeLocal< Value > Evaluate(Local< Context > context)
Local< Value > GetModuleNamespace()
Maybe< bool > SetSyntheticModuleExport(Isolate *isolate, Local< String > export_name, Local< Value > export_value)
Local< FixedArray > GetModuleRequests() const
bool IsGraphAsync() const
Maybe< bool > InstantiateModule(Local< Context > context, ResolveModuleCallback callback)
int ScriptId() const
Status GetStatus() const
bool IsSyntheticModule() const
Local< Value > GetException() const
std::pair< LocalVector< Module >, LocalVector< Message > > GetStalledTopLevelAwaitMessages(Isolate *isolate)
Status
Definition: v8-script.h:176
@ kInstantiating
Definition: v8-script.h:178
@ kInstantiated
Definition: v8-script.h:179
@ kUninstantiated
Definition: v8-script.h:177
@ kEvaluating
Definition: v8-script.h:180
@ kEvaluated
Definition: v8-script.h:181
int GetIdentityHash() const
static Local< Module > CreateSyntheticModule(Isolate *isolate, Local< String > module_name, const MemorySpan< const Local< String > > &export_names, SyntheticModuleEvaluationSteps evaluation_steps)
Local< UnboundModuleScript > GetUnboundModuleScript()
static Module * Cast(Data *data)
Definition: v8-script.h:886
bool IsSourceTextModule() const
Definition: v8-script.h:596
void SourceTextAvailable(Isolate *isolate, Local< String > source_text, const ScriptOrigin &origin)
Definition: v8-script.h:516
virtual size_t GetMoreData(const uint8_t **src)=0
Definition: v8-script.h:578
Definition: v8-script.h:460
Source(Local< String > source_string, const ScriptOrigin &origin, CachedData *cached_data=nullptr, ConsumeCodeCacheTask *consume_cache_task=nullptr)
Definition: v8-script.h:833
const CompilationDetails & GetCompilationDetails() const
Definition: v8-script.h:875
const CachedData * GetCachedData() const
Definition: v8-script.h:865
const ScriptOriginOptions & GetResourceOptions() const
Definition: v8-script.h:870
Definition: v8-script.h:550
internal::ScriptStreamingData * impl() const
Definition: v8-script.h:558
Encoding
Definition: v8-script.h:552
CompilationDetails & compilation_details()
Definition: v8-script.h:564
StreamedSource & operator=(const StreamedSource &)=delete
StreamedSource(const StreamedSource &)=delete
StreamedSource(std::unique_ptr< ExternalSourceStream > source_stream, Encoding encoding)
Definition: v8-script.h:370
static CachedData * CreateCodeCacheForFunction(Local< Function > function)
static MaybeLocal< Function > CompileFunction(Local< Context > context, Source *source, size_t arguments_count=0, Local< String > arguments[]=nullptr, size_t context_extension_count=0, Local< Object > context_extensions[]=nullptr, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
InMemoryCacheResult
Definition: v8-script.h:430
static CachedData * CreateCodeCache(Local< UnboundModuleScript > unbound_module_script)
NoCacheReason
Definition: v8-script.h:652
@ kNoCacheBecausePacScript
Definition: v8-script.h:664
@ kNoCacheBecauseV8Extension
Definition: v8-script.h:662
@ kNoCacheBecauseStreamingSource
Definition: v8-script.h:658
@ kNoCacheBecauseNoResource
Definition: v8-script.h:655
@ kNoCacheBecauseInspector
Definition: v8-script.h:659
@ kNoCacheBecauseCacheTooCold
Definition: v8-script.h:661
@ kNoCacheBecauseScriptTooSmall
Definition: v8-script.h:660
@ kNoCacheBecauseCachingDisabled
Definition: v8-script.h:654
@ kNoCacheBecauseExtensionModule
Definition: v8-script.h:663
@ kNoCacheBecauseInlineScript
Definition: v8-script.h:656
@ kNoCacheBecauseInDocumentWrite
Definition: v8-script.h:665
@ kNoCacheBecauseModule
Definition: v8-script.h:657
@ kNoCacheBecauseResourceWithNoCacheHandler
Definition: v8-script.h:666
static CachedData * CreateCodeCache(Local< UnboundScript > unbound_script)
static MaybeLocal< Module > CompileModule(Local< Context > context, StreamedSource *v8_source, Local< String > full_source_string, const ScriptOrigin &origin)
CompileOptions
Definition: v8-script.h:641
@ kEagerCompile
Definition: v8-script.h:644
@ kProduceCompileHints
Definition: v8-script.h:645
@ kConsumeCodeCache
Definition: v8-script.h:643
static MaybeLocal< Script > Compile(Local< Context > context, StreamedSource *source, Local< String > full_source_string, const ScriptOrigin &origin)
static MaybeLocal< Script > Compile(Local< Context > context, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static ScriptStreamingTask * StartStreaming(Isolate *isolate, StreamedSource *source, ScriptType type=ScriptType::kClassic, CompileOptions options=kNoCompileOptions, CompileHintCallback compile_hint_callback=nullptr, void *compile_hint_callback_data=nullptr)
static uint32_t CachedDataVersionTag()
static MaybeLocal< UnboundScript > CompileUnboundScript(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static ConsumeCodeCacheTask * StartConsumingCodeCache(Isolate *isolate, std::unique_ptr< CachedData > source)
static MaybeLocal< Module > CompileModule(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
Definition: v8-script.h:42
Local< Value > GetResourceName()
Local< Data > HostDefinedOptions()
Definition: v8-message.h:28
Definition: v8-message.h:62
Definition: v8-script.h:329
static MaybeLocal< Script > Compile(Local< Context > context, Local< String > source, ScriptOrigin *origin=nullptr)
MaybeLocal< Value > Run(Local< Context > context)
Local< UnboundScript > GetUnboundScript()
std::vector< int > GetProducedCompileHints() const
MaybeLocal< Value > Run(Local< Context > context, Local< Data > host_defined_options)
Local< Value > GetResourceName()
Definition: v8-script.h:97
Local< Value > GetSourceURL()
Local< Value > GetSourceMappingURL()
Definition: v8-script.h:60
int GetColumnNumber(int code_pos=0)
Local< Value > GetSourceMappingURL()
Local< Value > GetScriptName()
int GetLineNumber(int code_pos=0)
Local< Value > GetSourceURL()
int GetId() const
Local< Script > BindToCurrentContext()
Definition: libplatform.h:15
bool(*)(int, void *) CompileHintCallback
Definition: v8-callbacks.h:380
ScriptType
Definition: v8-script.h:365
Definition: v8-script.h:381
BufferPolicy buffer_policy
Definition: v8-script.h:423
const uint8_t * data
Definition: v8-script.h:420
CachedData(const CachedData &)=delete
CompatibilityCheckResult
Definition: v8-script.h:398
bool rejected
Definition: v8-script.h:422
CachedData(const uint8_t *data, int length, BufferPolicy buffer_policy=BufferNotOwned)
BufferPolicy
Definition: v8-script.h:382
@ BufferNotOwned
Definition: v8-script.h:382
CachedData()
Definition: v8-script.h:384
int length
Definition: v8-script.h:421
CompatibilityCheckResult CompatibilityCheck(Isolate *isolate)
CachedData & operator=(const CachedData &)=delete
Definition: v8-script.h:448
#define V8_EXPORT
Definition: v8config.h:762
#define V8_INLINE
Definition: v8config.h:477
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:580
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:637