v8 10.2.154 (node 18.16.0)
V8 is Google's open source JavaScript engine
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 <vector>
13
14#include "v8-data.h" // NOLINT(build/include_directory)
15#include "v8-local-handle.h" // NOLINT(build/include_directory)
16#include "v8-maybe.h" // NOLINT(build/include_directory)
17#include "v8-message.h" // NOLINT(build/include_directory)
18#include "v8config.h" // NOLINT(build/include_directory)
19
20namespace v8 {
21
22class Function;
23class Object;
24class PrimitiveArray;
25class Script;
26
27namespace internal {
28class BackgroundDeserializeTask;
29struct ScriptStreamingData;
30} // namespace internal
31
39 public:
45
50 V8_DEPRECATED("Use HostDefinedOptions")
51 Local<PrimitiveArray> GetHostDefinedOptions();
52 Local<Data> HostDefinedOptions();
53};
54
59 public:
64
65 int GetId() const;
67
76
81 int GetLineNumber(int code_pos);
82
83 static const int kNoScriptId = 0;
84};
85
90 // Only used as a container for code caching.
91};
92
97 public:
98 int GetLineNumber() { return line_number_; }
99 int GetColumnNumber() { return column_number_; }
100
101 Location(int line_number, int column_number)
102 : line_number_(line_number), column_number_(column_number) {}
103
104 private:
105 int line_number_;
106 int column_number_;
107};
108
110 public:
115
120 int GetSourceOffset() const;
121
137
138 V8_INLINE static ModuleRequest* Cast(Data* data);
139
140 private:
141 static void CheckCast(Data* obj);
142};
143
147class V8_EXPORT Module : public Data {
148 public:
156 enum Status {
162 kErrored
163 };
164
169
174
179
185
189 int GetIdentityHash() const;
190
192 Local<Context> context, Local<String> specifier,
193 Local<FixedArray> import_assertions, Local<Module> referrer);
194
203 Local<Context> context, ResolveModuleCallback callback);
204
216
223
231
237 int ScriptId() const;
238
245 bool IsGraphAsync() const;
246
250 bool IsSourceTextModule() const;
251
255 bool IsSyntheticModule() const;
256
257 /*
258 * Callback defined in the embedder. This is responsible for setting
259 * the module's exported values with calls to SetSyntheticModuleExport().
260 * The callback must return a resolved Promise to indicate success (where no
261 * exception was thrown) and return an empy MaybeLocal to indicate falure
262 * (where an exception was thrown).
263 */
266
275 Isolate* isolate, Local<String> module_name,
276 const std::vector<Local<String>>& export_names,
277 SyntheticModuleEvaluationSteps evaluation_steps);
278
287 Isolate* isolate, Local<String> export_name, Local<Value> export_value);
288
289 V8_INLINE static Module* Cast(Data* data);
290
291 private:
292 static void CheckCast(Data* obj);
293};
294
300 public:
305 Local<Context> context, Local<String> source,
306 ScriptOrigin* origin = nullptr);
307
315 Local<Data> host_defined_options);
316
321
327};
328
330
335 public:
337
346 enum BufferPolicy { BufferNotOwned, BufferOwned };
347
349 : data(nullptr),
350 length(0),
351 rejected(false),
352 buffer_policy(BufferNotOwned) {}
353
354 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
355 // data and guarantees that it stays alive until the CachedData object is
356 // destroyed. If the policy is BufferOwned, the given data will be deleted
357 // (with delete[]) when the CachedData object is destroyed.
358 CachedData(const uint8_t* data, int length,
359 BufferPolicy buffer_policy = BufferNotOwned);
361 // TODO(marja): Async compilation; add constructors which take a callback
362 // which will be called when V8 no longer needs the data.
363 const uint8_t* data;
367
368 // Prevent copying.
369 CachedData(const CachedData&) = delete;
370 CachedData& operator=(const CachedData&) = delete;
371 };
372
376 class Source {
377 public:
378 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
379 // The caller *must* ensure that the cached data is from a trusted source.
380 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
381 CachedData* cached_data = nullptr,
382 ConsumeCodeCacheTask* consume_cache_task = nullptr);
383 // Source takes ownership of both CachedData and CodeCacheConsumeTask.
384 V8_INLINE explicit Source(
385 Local<String> source_string, CachedData* cached_data = nullptr,
386 ConsumeCodeCacheTask* consume_cache_task = nullptr);
387 V8_INLINE ~Source() = default;
388
389 // Ownership of the CachedData or its buffers is *not* transferred to the
390 // caller. The CachedData object is alive as long as the Source object is
391 // alive.
392 V8_INLINE const CachedData* GetCachedData() const;
393
394 V8_INLINE const ScriptOriginOptions& GetResourceOptions() const;
395
396 private:
397 friend class ScriptCompiler;
398
399 Local<String> source_string;
400
401 // Origin information
402 Local<Value> resource_name;
403 int resource_line_offset;
404 int resource_column_offset;
405 ScriptOriginOptions resource_options;
406 Local<Value> source_map_url;
407 Local<Data> host_defined_options;
408
409 // Cached data from previous compilation (if a kConsume*Cache flag is
410 // set), or hold newly generated cache data (kProduce*Cache flags) are
411 // set when calling a compile method.
412 std::unique_ptr<CachedData> cached_data;
413 std::unique_ptr<ConsumeCodeCacheTask> consume_cache_task;
414 };
415
421 public:
422 virtual ~ExternalSourceStream() = default;
423
445 virtual size_t GetMoreData(const uint8_t** src) = 0;
446 };
447
455 public:
456 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8, WINDOWS_1252 };
457
458 StreamedSource(std::unique_ptr<ExternalSourceStream> source_stream,
459 Encoding encoding);
461
462 internal::ScriptStreamingData* impl() const { return impl_.get(); }
463
464 // Prevent copying.
467
468 private:
469 std::unique_ptr<internal::ScriptStreamingData> impl_;
470 };
471
477 public:
478 void Run();
479
480 private:
481 friend class ScriptCompiler;
482
483 explicit ScriptStreamingTask(internal::ScriptStreamingData* data)
484 : data_(data) {}
485
486 internal::ScriptStreamingData* data_;
487 };
488
495 public:
497
498 void Run();
499
500 private:
501 friend class ScriptCompiler;
502
503 explicit ConsumeCodeCacheTask(
504 std::unique_ptr<internal::BackgroundDeserializeTask> impl);
505
506 std::unique_ptr<internal::BackgroundDeserializeTask> impl_;
507 };
508
510 kNoCompileOptions = 0,
512 kEagerCompile
513 };
514
519 kNoCacheNoReason = 0,
533 kNoCacheBecauseDeferredProduceCodeCache
534 };
535
551 Isolate* isolate, Source* source,
552 CompileOptions options = kNoCompileOptions,
553 NoCacheReason no_cache_reason = kNoCacheNoReason);
554
567 Local<Context> context, Source* source,
568 CompileOptions options = kNoCompileOptions,
569 NoCacheReason no_cache_reason = kNoCacheNoReason);
570
583 Isolate* isolate, StreamedSource* source,
584 ScriptType type = ScriptType::kClassic);
585
587 Isolate* isolate, std::unique_ptr<CachedData> source);
588
597 Local<Context> context, StreamedSource* source,
598 Local<String> full_source_string, const ScriptOrigin& origin);
599
618 static uint32_t CachedDataVersionTag();
619
628 Isolate* isolate, Source* source,
629 CompileOptions options = kNoCompileOptions,
630 NoCacheReason no_cache_reason = kNoCacheNoReason);
631
640 Local<Context> context, StreamedSource* v8_source,
641 Local<String> full_source_string, const ScriptOrigin& origin);
642
653 V8_DEPRECATED("Use CompileFunction")
654 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
655 Local<Context> context, Source* source, size_t arguments_count,
656 Local<String> arguments[], size_t context_extension_count,
657 Local<Object> context_extensions[],
658 CompileOptions options = kNoCompileOptions,
659 NoCacheReason no_cache_reason = kNoCacheNoReason,
660 Local<ScriptOrModule>* script_or_module_out = nullptr);
662 Local<Context> context, Source* source, size_t arguments_count = 0,
663 Local<String> arguments[] = nullptr, size_t context_extension_count = 0,
664 Local<Object> context_extensions[] = nullptr,
665 CompileOptions options = kNoCompileOptions,
666 NoCacheReason no_cache_reason = kNoCacheNoReason);
667
673 static CachedData* CreateCodeCache(Local<UnboundScript> unbound_script);
674
680 static CachedData* CreateCodeCache(
681 Local<UnboundModuleScript> unbound_module_script);
682
689 static CachedData* CreateCodeCacheForFunction(Local<Function> function);
690
691 private:
692 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
693 Isolate* isolate, Source* source, CompileOptions options,
694 NoCacheReason no_cache_reason);
695
696 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInternal(
697 Local<Context> context, Source* source, size_t arguments_count,
698 Local<String> arguments[], size_t context_extension_count,
699 Local<Object> context_extensions[], CompileOptions options,
700 NoCacheReason no_cache_reason,
701 Local<ScriptOrModule>* script_or_module_out);
702};
703
705 CachedData* data,
706 ConsumeCodeCacheTask* consume_cache_task)
707 : source_string(string),
708 resource_name(origin.ResourceName()),
709 resource_line_offset(origin.LineOffset()),
710 resource_column_offset(origin.ColumnOffset()),
711 resource_options(origin.Options()),
712 source_map_url(origin.SourceMapUrl()),
713 host_defined_options(origin.GetHostDefinedOptions()),
714 cached_data(data),
715 consume_cache_task(consume_cache_task) {}
716
717ScriptCompiler::Source::Source(Local<String> string, CachedData* data,
718 ConsumeCodeCacheTask* consume_cache_task)
719 : source_string(string),
720 cached_data(data),
721 consume_cache_task(consume_cache_task) {}
722
724 const {
725 return cached_data.get();
726}
727
729 return resource_options;
730}
731
733#ifdef V8_ENABLE_CHECKS
734 CheckCast(data);
735#endif
736 return reinterpret_cast<ModuleRequest*>(data);
737}
738
740#ifdef V8_ENABLE_CHECKS
741 CheckCast(data);
742#endif
743 return reinterpret_cast<Module*>(data);
744}
745
746} // namespace v8
747
748#endif // INCLUDE_V8_SCRIPT_H_
int GetColumnNumber()
Definition v8-script.h:99
int GetLineNumber()
Definition v8-script.h:98
Location(int line_number, int column_number)
Definition v8-script.h:101
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Evaluate(Local< Context > context)
Status GetStatus() const
Local< UnboundModuleScript > GetUnboundModuleScript()
@ kUninstantiated
Definition v8-script.h:157
Local< FixedArray > GetModuleRequests() const
Local< Value > GetModuleNamespace()
Local< Value > GetException() const
V8_WARN_UNUSED_RESULT Maybe< bool > InstantiateModule(Local< Context > context, ResolveModuleCallback callback)
bool IsSyntheticModule() const
static V8_INLINE Module * Cast(Data *data)
Definition v8-script.h:739
Location SourceOffsetToLocation(int offset) const
int GetIdentityHash() const
int ScriptId() const
bool IsSourceTextModule() const
V8_WARN_UNUSED_RESULT Maybe< bool > SetSyntheticModuleExport(Isolate *isolate, Local< String > export_name, Local< Value > export_value)
bool IsGraphAsync() const
static Local< Module > CreateSyntheticModule(Isolate *isolate, Local< String > module_name, const std::vector< Local< String > > &export_names, SyntheticModuleEvaluationSteps evaluation_steps)
static V8_INLINE ModuleRequest * Cast(Data *data)
Definition v8-script.h:732
Local< FixedArray > GetImportAssertions() const
int GetSourceOffset() const
Local< String > GetSpecifier() const
virtual size_t GetMoreData(const uint8_t **src)=0
V8_INLINE ~Source()=default
V8_INLINE const CachedData * GetCachedData() const
Definition v8-script.h:723
V8_INLINE const ScriptOriginOptions & GetResourceOptions() const
Definition v8-script.h:728
internal::ScriptStreamingData * impl() const
Definition v8-script.h:462
StreamedSource & operator=(const StreamedSource &)=delete
StreamedSource(const StreamedSource &)=delete
StreamedSource(std::unique_ptr< ExternalSourceStream > source_stream, Encoding encoding)
static V8_WARN_UNUSED_RESULT MaybeLocal< Module > CompileModule(Local< Context > context, StreamedSource *v8_source, Local< String > full_source_string, const ScriptOrigin &origin)
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, StreamedSource *source, Local< String > full_source_string, const ScriptOrigin &origin)
static ConsumeCodeCacheTask * StartConsumingCodeCache(Isolate *isolate, std::unique_ptr< CachedData > source)
@ kNoCacheBecauseResourceWithNoCacheHandler
Definition v8-script.h:532
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static V8_WARN_UNUSED_RESULT MaybeLocal< UnboundScript > CompileUnboundScript(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
static ScriptStreamingTask * StartStreaming(Isolate *isolate, StreamedSource *source, ScriptType type=ScriptType::kClassic)
static uint32_t CachedDataVersionTag()
static V8_WARN_UNUSED_RESULT MaybeLocal< Module > CompileModule(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
Local< UnboundScript > GetUnboundScript()
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Local< String > source, ScriptOrigin *origin=nullptr)
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Run(Local< Context > context)
Local< Value > GetResourceName()
V8_WARN_UNUSED_RESULT MaybeLocal< Value > Run(Local< Context > context, Local< Data > host_defined_options)
Local< Value > GetResourceName()
Local< Script > BindToCurrentContext()
Local< Value > GetSourceMappingURL()
Local< Value > GetSourceURL()
int GetLineNumber(int code_pos)
int GetId() const
Local< Value > GetScriptName()
ScriptType
Definition v8-script.h:329
CachedData(const CachedData &)=delete
CachedData & operator=(const CachedData &)=delete
CachedData(const uint8_t *data, int length, BufferPolicy buffer_policy=BufferNotOwned)
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425
#define V8_DEPRECATED(message)
Definition v8config.h:462
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:499