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