File indexing completed on 2026-04-01 08:24:14
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_SCRIPT_H_
0006 #define INCLUDE_V8_SCRIPT_H_
0007
0008 #include <stddef.h>
0009 #include <stdint.h>
0010
0011 #include <memory>
0012 #include <tuple>
0013 #include <vector>
0014
0015 #include "v8-callbacks.h" // NOLINT(build/include_directory)
0016 #include "v8-data.h" // NOLINT(build/include_directory)
0017 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0018 #include "v8-maybe.h" // NOLINT(build/include_directory)
0019 #include "v8-memory-span.h" // NOLINT(build/include_directory)
0020 #include "v8-message.h" // NOLINT(build/include_directory)
0021 #include "v8config.h" // NOLINT(build/include_directory)
0022
0023 namespace v8 {
0024
0025 class Function;
0026 class Message;
0027 class Object;
0028 class PrimitiveArray;
0029 class Script;
0030
0031 namespace internal {
0032 class BackgroundDeserializeTask;
0033 struct ScriptStreamingData;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042 class V8_EXPORT ScriptOrModule {
0043 public:
0044
0045
0046
0047
0048 Local<Value> GetResourceName();
0049
0050
0051
0052
0053
0054 Local<Data> HostDefinedOptions();
0055 };
0056
0057
0058
0059
0060 class V8_EXPORT UnboundScript : public Data {
0061 public:
0062
0063
0064
0065 Local<Script> BindToCurrentContext();
0066
0067 int GetId() const;
0068 Local<Value> GetScriptName();
0069
0070
0071
0072
0073 Local<Value> GetSourceURL();
0074
0075
0076
0077 Local<Value> GetSourceMappingURL();
0078
0079
0080
0081
0082
0083 int GetLineNumber(int code_pos = 0);
0084
0085
0086
0087
0088
0089 int GetColumnNumber(int code_pos = 0);
0090
0091 static const int kNoScriptId = 0;
0092 };
0093
0094
0095
0096
0097 class V8_EXPORT UnboundModuleScript : public Data {
0098 public:
0099
0100
0101
0102 Local<Value> GetSourceURL();
0103
0104
0105
0106 Local<Value> GetSourceMappingURL();
0107 };
0108
0109
0110
0111
0112 class V8_EXPORT Location {
0113 public:
0114 int GetLineNumber() { return line_number_; }
0115 int GetColumnNumber() { return column_number_; }
0116
0117 Location(int line_number, int column_number)
0118 : line_number_(line_number), column_number_(column_number) {}
0119
0120 private:
0121 int line_number_;
0122 int column_number_;
0123 };
0124
0125 class V8_EXPORT ModuleRequest : public Data {
0126 public:
0127
0128
0129
0130 Local<String> GetSpecifier() const;
0131
0132
0133
0134
0135 ModuleImportPhase GetPhase() const;
0136
0137
0138
0139
0140
0141 int GetSourceOffset() const;
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156 Local<FixedArray> GetImportAttributes() const;
0157
0158 V8_DEPRECATED("Use GetImportAttributes instead")
0159 Local<FixedArray> GetImportAssertions() const {
0160 return GetImportAttributes();
0161 }
0162
0163 V8_INLINE static ModuleRequest* Cast(Data* data);
0164
0165 private:
0166 static void CheckCast(Data* obj);
0167 };
0168
0169
0170
0171
0172 class V8_EXPORT Module : public Data {
0173 public:
0174
0175
0176
0177
0178
0179
0180
0181 enum Status {
0182 kUninstantiated,
0183 kInstantiating,
0184 kInstantiated,
0185 kEvaluating,
0186 kEvaluated,
0187 kErrored
0188 };
0189
0190
0191
0192
0193 Status GetStatus() const;
0194
0195
0196
0197
0198 Local<Value> GetException() const;
0199
0200
0201
0202
0203 Local<FixedArray> GetModuleRequests() const;
0204
0205
0206
0207
0208
0209 Location SourceOffsetToLocation(int offset) const;
0210
0211
0212
0213
0214 int GetIdentityHash() const;
0215
0216 using ResolveModuleCallback = MaybeLocal<Module> (*)(
0217 Local<Context> context, Local<String> specifier,
0218 Local<FixedArray> import_attributes, Local<Module> referrer);
0219 using ResolveSourceCallback = MaybeLocal<Object> (*)(
0220 Local<Context> context, Local<String> specifier,
0221 Local<FixedArray> import_attributes, Local<Module> referrer);
0222
0223
0224
0225
0226
0227
0228
0229
0230 V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule(
0231 Local<Context> context, ResolveModuleCallback module_callback,
0232 ResolveSourceCallback source_callback = nullptr);
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Evaluate(Local<Context> context);
0245
0246
0247
0248
0249
0250
0251 Local<Value> GetModuleNamespace();
0252
0253
0254
0255
0256
0257
0258
0259 Local<UnboundModuleScript> GetUnboundModuleScript();
0260
0261
0262
0263
0264
0265
0266 int ScriptId() const;
0267
0268
0269
0270
0271
0272
0273
0274 bool IsGraphAsync() const;
0275
0276
0277
0278
0279
0280
0281 bool HasTopLevelAwait() const;
0282
0283
0284
0285
0286 bool IsSourceTextModule() const;
0287
0288
0289
0290
0291 bool IsSyntheticModule() const;
0292
0293
0294
0295
0296
0297
0298
0299
0300 using SyntheticModuleEvaluationSteps =
0301 MaybeLocal<Value> (*)(Local<Context> context, Local<Module> module);
0302
0303
0304
0305
0306
0307
0308
0309
0310 static Local<Module> CreateSyntheticModule(
0311 Isolate* isolate, Local<String> module_name,
0312 const MemorySpan<const Local<String>>& export_names,
0313 SyntheticModuleEvaluationSteps evaluation_steps);
0314
0315
0316
0317
0318
0319
0320
0321
0322 V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport(
0323 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
0324
0325
0326
0327
0328
0329
0330
0331
0332 std::pair<LocalVector<Module>, LocalVector<Message>>
0333 GetStalledTopLevelAwaitMessages(Isolate* isolate);
0334
0335 V8_INLINE static Module* Cast(Data* data);
0336
0337 private:
0338 static void CheckCast(Data* obj);
0339 };
0340
0341 class V8_EXPORT CompileHintsCollector : public Data {
0342 public:
0343
0344
0345
0346 std::vector<int> GetCompileHints(Isolate* isolate) const;
0347 };
0348
0349
0350
0351
0352
0353 class V8_EXPORT Script : public Data {
0354 public:
0355
0356
0357
0358 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0359 Local<Context> context, Local<String> source,
0360 ScriptOrigin* origin = nullptr);
0361
0362
0363
0364
0365
0366
0367 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
0368 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context,
0369 Local<Data> host_defined_options);
0370
0371
0372
0373
0374 Local<UnboundScript> GetUnboundScript();
0375
0376
0377
0378
0379
0380 Local<Value> GetResourceName();
0381
0382
0383
0384
0385
0386 V8_DEPRECATE_SOON("Use GetCompileHintsCollector instead")
0387 std::vector<int> GetProducedCompileHints() const;
0388
0389
0390
0391
0392
0393
0394 Local<CompileHintsCollector> GetCompileHintsCollector() const;
0395 };
0396
0397 enum class ScriptType { kClassic, kModule };
0398
0399
0400
0401
0402 class V8_EXPORT ScriptCompiler {
0403 public:
0404 class ConsumeCodeCacheTask;
0405
0406
0407
0408
0409
0410
0411
0412
0413 struct V8_EXPORT CachedData {
0414 enum BufferPolicy { BufferNotOwned, BufferOwned };
0415
0416 CachedData()
0417 : data(nullptr),
0418 length(0),
0419 rejected(false),
0420 buffer_policy(BufferNotOwned) {}
0421
0422
0423
0424
0425
0426 CachedData(const uint8_t* data, int length,
0427 BufferPolicy buffer_policy = BufferNotOwned);
0428 ~CachedData();
0429
0430 enum CompatibilityCheckResult {
0431
0432
0433 kSuccess = 0,
0434 kMagicNumberMismatch = 1,
0435 kVersionMismatch = 2,
0436 kSourceMismatch = 3,
0437 kFlagsMismatch = 5,
0438 kChecksumMismatch = 6,
0439 kInvalidHeader = 7,
0440 kLengthMismatch = 8,
0441 kReadOnlySnapshotChecksumMismatch = 9,
0442
0443
0444 kLast = kReadOnlySnapshotChecksumMismatch
0445 };
0446
0447
0448 CompatibilityCheckResult CompatibilityCheck(Isolate* isolate);
0449
0450
0451
0452 const uint8_t* data;
0453 int length;
0454 bool rejected;
0455 BufferPolicy buffer_policy;
0456
0457
0458 CachedData(const CachedData&) = delete;
0459 CachedData& operator=(const CachedData&) = delete;
0460 };
0461
0462 enum class InMemoryCacheResult {
0463
0464 kNotAttempted,
0465
0466
0467
0468
0469 kHit,
0470
0471
0472 kMiss,
0473
0474
0475
0476 kPartial,
0477 };
0478
0479
0480 struct CompilationDetails {
0481 InMemoryCacheResult in_memory_cache_result =
0482 InMemoryCacheResult::kNotAttempted;
0483
0484 static constexpr int64_t kTimeNotMeasured = -1;
0485 int64_t foreground_time_in_microseconds = kTimeNotMeasured;
0486 int64_t background_time_in_microseconds = kTimeNotMeasured;
0487 };
0488
0489
0490
0491
0492 class Source {
0493 public:
0494
0495
0496 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
0497 CachedData* cached_data = nullptr,
0498 ConsumeCodeCacheTask* consume_cache_task = nullptr);
0499
0500 V8_INLINE explicit Source(
0501 Local<String> source_string, CachedData* cached_data = nullptr,
0502 ConsumeCodeCacheTask* consume_cache_task = nullptr);
0503 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
0504 CompileHintCallback callback, void* callback_data);
0505 V8_INLINE ~Source() = default;
0506
0507
0508
0509
0510 V8_INLINE const CachedData* GetCachedData() const;
0511
0512 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
0513
0514 V8_INLINE const CompilationDetails& GetCompilationDetails() const;
0515
0516 private:
0517 friend class ScriptCompiler;
0518
0519 Local<String> source_string;
0520
0521
0522 Local<Value> resource_name;
0523 int resource_line_offset = -1;
0524 int resource_column_offset = -1;
0525 ScriptOriginOptions resource_options;
0526 Local<Value> source_map_url;
0527 Local<Data> host_defined_options;
0528
0529
0530
0531
0532 std::unique_ptr<CachedData> cached_data;
0533 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
0534
0535
0536 CompileHintCallback compile_hint_callback = nullptr;
0537 void* compile_hint_callback_data = nullptr;
0538
0539
0540
0541 CompilationDetails compilation_details;
0542 };
0543
0544
0545
0546
0547
0548 class V8_EXPORT ExternalSourceStream {
0549 public:
0550 virtual ~ExternalSourceStream() = default;
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573 virtual size_t GetMoreData(const uint8_t** src) = 0;
0574 };
0575
0576
0577
0578
0579
0580
0581
0582 class V8_EXPORT StreamedSource {
0583 public:
0584 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
0585
0586 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
0587 Encoding encoding);
0588 ~StreamedSource();
0589
0590 internal::ScriptStreamingData* impl() const { return impl_.get(); }
0591
0592
0593 StreamedSource(const StreamedSource&) = delete;
0594 StreamedSource& operator=(const StreamedSource&) = delete;
0595
0596 CompilationDetails& compilation_details() { return compilation_details_; }
0597
0598 private:
0599 std::unique_ptr<internal::ScriptStreamingData> impl_;
0600
0601
0602
0603 CompilationDetails compilation_details_;
0604 };
0605
0606
0607
0608
0609
0610 class V8_EXPORT ScriptStreamingTask final {
0611 public:
0612 void Run();
0613
0614 private:
0615 friend class ScriptCompiler;
0616
0617 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
0618 : data_(data) {}
0619
0620 internal::ScriptStreamingData* data_;
0621 };
0622
0623
0624
0625
0626
0627
0628 class V8_EXPORT ConsumeCodeCacheTask final {
0629 public:
0630 ~ConsumeCodeCacheTask();
0631
0632 void Run();
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647 void SourceTextAvailable(Isolate* isolate, Local<String> source_text,
0648 const ScriptOrigin& origin);
0649
0650
0651
0652
0653
0654
0655 bool ShouldMergeWithExistingScript() const;
0656
0657
0658
0659
0660
0661
0662 void MergeWithExistingScript();
0663
0664 private:
0665 friend class ScriptCompiler;
0666
0667 explicit ConsumeCodeCacheTask(
0668 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
0669
0670 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
0671 };
0672
0673 enum CompileOptions {
0674 kNoCompileOptions = 0,
0675 kConsumeCodeCache = 1 << 0,
0676 kEagerCompile = 1 << 1,
0677 kProduceCompileHints = 1 << 2,
0678 kConsumeCompileHints = 1 << 3,
0679 kFollowCompileHintsMagicComment = 1 << 4,
0680 kFollowCompileHintsPerFunctionMagicComment = 1 << 5,
0681 };
0682
0683 static inline bool CompileOptionsIsValid(CompileOptions compile_options) {
0684
0685 if ((compile_options & kConsumeCodeCache) &&
0686 compile_options != kConsumeCodeCache) {
0687 return false;
0688 }
0689
0690 if ((compile_options & kEagerCompile) && compile_options != kEagerCompile) {
0691 return false;
0692 }
0693
0694
0695 constexpr int produce_and_consume = CompileOptions::kProduceCompileHints |
0696 CompileOptions::kConsumeCompileHints;
0697 if ((compile_options & produce_and_consume) == produce_and_consume) {
0698 return false;
0699 }
0700 return true;
0701 }
0702
0703
0704
0705
0706 enum NoCacheReason {
0707 kNoCacheNoReason = 0,
0708 kNoCacheBecauseCachingDisabled,
0709 kNoCacheBecauseNoResource,
0710 kNoCacheBecauseInlineScript,
0711 kNoCacheBecauseModule,
0712 kNoCacheBecauseStreamingSource,
0713 kNoCacheBecauseInspector,
0714 kNoCacheBecauseScriptTooSmall,
0715 kNoCacheBecauseCacheTooCold,
0716 kNoCacheBecauseV8Extension,
0717 kNoCacheBecauseExtensionModule,
0718 kNoCacheBecausePacScript,
0719 kNoCacheBecauseInDocumentWrite,
0720 kNoCacheBecauseResourceWithNoCacheHandler,
0721 kNoCacheBecauseDeferredProduceCodeCache,
0722 kNoCacheBecauseStaticCodeCache,
0723 };
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
0740 Isolate* isolate, Source* source,
0741 CompileOptions options = kNoCompileOptions,
0742 NoCacheReason no_cache_reason = kNoCacheNoReason);
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0756 Local<Context> context, Source* source,
0757 CompileOptions options = kNoCompileOptions,
0758 NoCacheReason no_cache_reason = kNoCacheNoReason);
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771 static ScriptStreamingTask* StartStreaming(
0772 Isolate* isolate, StreamedSource* source,
0773 ScriptType type = ScriptType::kClassic,
0774 CompileOptions options = kNoCompileOptions,
0775 CompileHintCallback compile_hint_callback = nullptr,
0776 void* compile_hint_callback_data = nullptr);
0777
0778 static ConsumeCodeCacheTask* StartConsumingCodeCache(
0779 Isolate* isolate, std::unique_ptr<CachedData> source);
0780 static ConsumeCodeCacheTask* StartConsumingCodeCacheOnBackground(
0781 Isolate* isolate, std::unique_ptr<CachedData> source);
0782
0783
0784
0785
0786
0787
0788
0789
0790 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
0791 Local<Context> context, StreamedSource* source,
0792 Local<String> full_source_string, const ScriptOrigin& origin);
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812 static uint32_t CachedDataVersionTag();
0813
0814
0815
0816
0817
0818
0819
0820
0821 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule(
0822 Isolate* isolate, Source* source,
0823 CompileOptions options = kNoCompileOptions,
0824 NoCacheReason no_cache_reason = kNoCacheNoReason);
0825
0826
0827
0828
0829
0830
0831
0832
0833 static V8_WARN_UNUSED_RESULT MaybeLocal<Module> CompileModule(
0834 Local<Context> context, StreamedSource* v8_source,
0835 Local<String> full_source_string, const ScriptOrigin& origin);
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunction(
0848 Local<Context> context, Source* source, size_t arguments_count = 0,
0849 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
0850 Local<Object> context_extensions[] = nullptr,
0851 CompileOptions options = kNoCompileOptions,
0852 NoCacheReason no_cache_reason = kNoCacheNoReason);
0853
0854
0855
0856
0857
0858
0859 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script);
0860
0861
0862
0863
0864
0865
0866 static CachedData* CreateCodeCache(
0867 Local<UnboundModuleScript> unbound_module_script);
0868
0869
0870
0871
0872
0873
0874
0875 static CachedData* CreateCodeCacheForFunction(Local<Function> function);
0876
0877 private:
0878 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
0879 Isolate* isolate, Source* source, CompileOptions options,
0880 NoCacheReason no_cache_reason);
0881
0882 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
0883 Local<Context> context, Source* source, size_t arguments_count,
0884 Local<String> arguments[], size_t context_extension_count,
0885 Local<Object> context_extensions[], CompileOptions options,
0886 NoCacheReason no_cache_reason,
0887 Local<ScriptOrModule>* script_or_module_out);
0888 };
0889
0890 ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
0891 CachedData* data,
0892 ConsumeCodeCacheTask* consume_cache_task)
0893 : source_string(string),
0894 resource_name(origin.ResourceName()),
0895 resource_line_offset(origin.LineOffset()),
0896 resource_column_offset(origin.ColumnOffset()),
0897 resource_options(origin.Options()),
0898 source_map_url(origin.SourceMapUrl()),
0899 host_defined_options(origin.GetHostDefinedOptions()),
0900 cached_data(data),
0901 consume_cache_task(consume_cache_task) {}
0902
0903 ScriptCompiler::Source::Source(Local<String> string, CachedData* data,
0904 ConsumeCodeCacheTask* consume_cache_task)
0905 : source_string(string),
0906 cached_data(data),
0907 consume_cache_task(consume_cache_task) {}
0908
0909 ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
0910 CompileHintCallback callback,
0911 void* callback_data)
0912 : source_string(string),
0913 resource_name(origin.ResourceName()),
0914 resource_line_offset(origin.LineOffset()),
0915 resource_column_offset(origin.ColumnOffset()),
0916 resource_options(origin.Options()),
0917 source_map_url(origin.SourceMapUrl()),
0918 host_defined_options(origin.GetHostDefinedOptions()),
0919 compile_hint_callback(callback),
0920 compile_hint_callback_data(callback_data) {}
0921
0922 const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
0923 const {
0924 return cached_data.get();
0925 }
0926
0927 const ScriptOriginOptions& ScriptCompiler::Source::GetResourceOptions() const {
0928 return resource_options;
0929 }
0930
0931 const ScriptCompiler::CompilationDetails&
0932 ScriptCompiler::Source::GetCompilationDetails() const {
0933 return compilation_details;
0934 }
0935
0936 ModuleRequest* ModuleRequest::Cast(Data* data) {
0937 #ifdef V8_ENABLE_CHECKS
0938 CheckCast(data);
0939 #endif
0940 return reinterpret_cast<ModuleRequest*>(data);
0941 }
0942
0943 Module* Module::Cast(Data* data) {
0944 #ifdef V8_ENABLE_CHECKS
0945 CheckCast(data);
0946 #endif
0947 return reinterpret_cast<Module*>(data);
0948 }
0949
0950 }
0951
0952 #endif