v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
well-known-imports.h
Go to the documentation of this file.
1// Copyright 2023 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 V8_WASM_WELL_KNOWN_IMPORTS_H_
6#define V8_WASM_WELL_KNOWN_IMPORTS_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <atomic>
13#include <memory>
14
15#include "src/base/vector.h"
16
17namespace v8::internal::wasm {
18
19enum class WellKnownImport : uint8_t {
20 // Generic:
24
26 // Compile-time "builtin" imports:
29
30 // JS String Builtins
31 // https://github.com/WebAssembly/js-string-builtins
32 // TODO(14179): Rename some of these to reflect the new import names.
50
53 // End of compile-time "builtin" imports.
55
56 // DataView methods:
78
79 // Math functions.
90 kMathF64Sqrt, // Used by dart2wasm. f64.sqrt is equivalent.
91
92 // String-related functions:
96
102 // Fast API calls:
104};
105
106class NativeModule;
107
108// For debugging/tracing.
109const char* WellKnownImportName(WellKnownImport wki);
110
112 using T = std::underlying_type_t<WellKnownImport>;
113 T num = static_cast<T>(wki);
114 constexpr T kFirst = static_cast<T>(WellKnownImport::kFirstCompileTimeImport);
115 constexpr T kLast = static_cast<T>(WellKnownImport::kLastCompileTimeImport);
116 return kFirst <= num && num <= kLast;
117}
118
120 public:
121 enum class UpdateResult : bool { kFoundIncompatibility, kOK };
122
124
125 // Regular initialization. Allocates size-dependent internal data.
126 void Initialize(int size) {
127#if DEBUG
128 DCHECK_EQ(-1, size_);
129 size_ = size;
130#endif
131 static_assert(static_cast<int>(WellKnownImport::kUninstantiated) == 0);
132 statuses_ = std::make_unique<std::atomic<WellKnownImport>[]>(size);
133#if !defined(__cpp_lib_atomic_value_initialization) || \
134 __cpp_lib_atomic_value_initialization < 201911L
135 for (int i = 0; i < size; i++) {
136 std::atomic_init(&statuses_.get()[i], WellKnownImport::kUninstantiated);
137 }
138#endif
139 }
140
141 // Intended for deserialization. Does not check consistency with code.
143
144 WellKnownImport get(int index) const {
145 DCHECK_LT(index, size_);
146 return statuses_[index].load(std::memory_order_relaxed);
147 }
148
149 // Note: you probably want to be holding the associated NativeModule's
150 // {allocation_lock_} when calling this method.
153
154 private:
155 // Operations that need to ensure that they see a consistent view of
156 // {statuses_} for some period of time should use the associated
157 // NativeModule's {allocation_lock_} for that purpose (which they will
158 // likely need anyway, due to WellKnownImport statuses and published
159 // code objects needing to stay in sync).
160 std::unique_ptr<std::atomic<WellKnownImport>[]> statuses_;
161
162#if DEBUG
163 int size_{-1};
164#endif
165};
166
167} // namespace v8::internal::wasm
168
169#endif // V8_WASM_WELL_KNOWN_IMPORTS_H_
V8_WARN_UNUSED_RESULT UpdateResult Update(base::Vector< WellKnownImport > entries)
WellKnownImport get(int index) const
std::unique_ptr< std::atomic< WellKnownImport >[]> statuses_
const int size_
Definition assembler.cc:132
ZoneVector< Entry > entries
bool IsCompileTimeImport(WellKnownImport wki)
const char * WellKnownImportName(WellKnownImport wki)
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671