summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/module_record.h
blob: 4a1ed78025f76fdf9f56dfd891ffc6e8cad447f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_

#include "third_party/blink/renderer/bindings/core/v8/script_source_location_type.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_code_cache.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/text_position.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "v8/include/v8.h"

namespace blink {

class ExceptionState;
class KURL;
class ScriptFetchOptions;
class ScriptState;
class ScriptValue;
class ScriptPromise;

// ModuleEvaluationResult encapsulates the result of a module evaluation.
// - Without top-level-await
//   - succeed and not return a value, or
//     (IsSuccess() == true), no return value is available.
//   - throw any object.
//     (IsException() == true && GetException()) returns the thrown exception
// - With top-level-await a module can either
//   - return a promise, or
//     (IsSuccess() == true && GetPromise()) returns a valid ScriptPromise())
//   - throw any object.
//     (IsException() == true && GetException()) returns the thrown exception
class CORE_EXPORT ModuleEvaluationResult final {
  STACK_ALLOCATED();

 public:
  ModuleEvaluationResult() = delete;
  static ModuleEvaluationResult Empty();
  static ModuleEvaluationResult FromResult(v8::Local<v8::Value> promise);
  static ModuleEvaluationResult FromException(v8::Local<v8::Value> exception);

  ModuleEvaluationResult(const ModuleEvaluationResult& value) = default;
  ModuleEvaluationResult& operator=(const ModuleEvaluationResult& value) =
      default;
  ~ModuleEvaluationResult() = default;

  ModuleEvaluationResult& Escape(ScriptState::EscapableScope* scope);

  bool IsSuccess() const { return is_success_; }
  bool IsException() const { return !is_success_; }

  v8::Local<v8::Value> GetException() const;
  ScriptPromise GetPromise(ScriptState* script_state) const;

 private:
  ModuleEvaluationResult(bool is_success, v8::Local<v8::Value> value)
      : is_success_(is_success), value_(value) {}

  bool is_success_;
  v8::Local<v8::Value> value_;
};

// ModuleRecordProduceCacheData is a parameter object for
// ModuleRecord::ProduceCache().
class CORE_EXPORT ModuleRecordProduceCacheData final
    : public GarbageCollected<ModuleRecordProduceCacheData> {
 public:
  ModuleRecordProduceCacheData(v8::Isolate*,
                               SingleCachedMetadataHandler*,
                               V8CodeCache::ProduceCacheOptions,
                               v8::Local<v8::Module>);

  void Trace(Visitor*) const;

  SingleCachedMetadataHandler* CacheHandler() const { return cache_handler_; }
  V8CodeCache::ProduceCacheOptions GetProduceCacheOptions() const {
    return produce_cache_options_;
  }
  v8::Local<v8::UnboundModuleScript> UnboundScript(v8::Isolate* isolate) const {
    return unbound_script_.NewLocal(isolate);
  }

 private:
  Member<SingleCachedMetadataHandler> cache_handler_;
  V8CodeCache::ProduceCacheOptions produce_cache_options_;

  // TODO(keishi): Visitor only defines a trace method for v8::Value so this
  // needs to be cast.
  GC_PLUGIN_IGNORE("757708")
  TraceWrapperV8Reference<v8::UnboundModuleScript> unbound_script_;
};

// TODO(rikaf): Add a class level comment
class CORE_EXPORT ModuleRecord final {
  STATIC_ONLY(ModuleRecord);

 public:
  static v8::Local<v8::Module> Compile(
      v8::Isolate*,
      const String& source,
      const KURL& source_url,
      const KURL& base_url,
      const ScriptFetchOptions&,
      const TextPosition&,
      ExceptionState&,
      V8CacheOptions = kV8CacheOptionsDefault,
      SingleCachedMetadataHandler* = nullptr,
      ScriptSourceLocationType source_location_type =
          ScriptSourceLocationType::kInternal,
      ModuleRecordProduceCacheData** out_produce_cache_data = nullptr);

  // Returns exception, if any.
  static ScriptValue Instantiate(ScriptState*,
                                 v8::Local<v8::Module> record,
                                 const KURL& source_url);

  static ModuleEvaluationResult Evaluate(ScriptState*,
                                         v8::Local<v8::Module> record,
                                         const KURL& source_url);

  static void ReportException(ScriptState*, v8::Local<v8::Value> exception);

  static Vector<String> ModuleRequests(ScriptState*,
                                       v8::Local<v8::Module> record);
  static Vector<TextPosition> ModuleRequestPositions(
      ScriptState*,
      v8::Local<v8::Module> record);

  static v8::Local<v8::Value> V8Namespace(v8::Local<v8::Module> record);

 private:
  static v8::MaybeLocal<v8::Module> ResolveModuleCallback(
      v8::Local<v8::Context>,
      v8::Local<v8::String> specifier,
      v8::Local<v8::Module> referrer);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_