v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
names-provider.h
Go to the documentation of this file.
1// Copyright 2022 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_NAMES_PROVIDER_H_
6#define V8_WASM_NAMES_PROVIDER_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 <map>
13#include <string>
14
15#include "src/base/vector.h"
16#include "src/utils/utils.h"
18
19namespace v8 {
20namespace internal {
21namespace wasm {
22
23class DecodedNameSection;
24class StringBuilder;
25
27 public:
28 // {kWasmInternal}: only return raw name from name section.
29 // {kDevTools}: prepend '$', use import/export names as fallback,
30 // or "$funcN" as default.
31 enum FunctionNamesBehavior : bool { kWasmInternal = false, kDevTools = true };
32
33 enum IndexAsComment : bool {
34 kDontPrintIndex = false,
35 kIndexAsComment = true
36 };
37
38 NamesProvider(const WasmModule* module,
41
42 void PrintFunctionName(StringBuilder& out, uint32_t function_index,
43 FunctionNamesBehavior behavior = kWasmInternal,
44 IndexAsComment index_as_comment = kDontPrintIndex);
45 void PrintLocalName(StringBuilder& out, uint32_t function_index,
46 uint32_t local_index,
47 IndexAsComment index_as_comment = kDontPrintIndex);
48 void PrintLabelName(StringBuilder& out, uint32_t function_index,
49 uint32_t label_index, uint32_t fallback_index);
50 void PrintTypeName(StringBuilder& out, uint32_t type_index,
51 IndexAsComment index_as_comment = kDontPrintIndex);
53 IndexAsComment index_as_comment = kDontPrintIndex) {
54 PrintTypeName(out, type_index.index, index_as_comment);
55 }
56 void PrintTableName(StringBuilder& out, uint32_t table_index,
57 IndexAsComment index_as_comment = kDontPrintIndex);
58 void PrintMemoryName(StringBuilder& out, uint32_t memory_index,
59 IndexAsComment index_as_comment = kDontPrintIndex);
60 void PrintGlobalName(StringBuilder& out, uint32_t global_index,
61 IndexAsComment index_as_comment = kDontPrintIndex);
62 void PrintElementSegmentName(
63 StringBuilder& out, uint32_t element_segment_index,
64 IndexAsComment index_as_comment = kDontPrintIndex);
65 void PrintDataSegmentName(StringBuilder& out, uint32_t data_segment_index,
66 IndexAsComment index_as_comment = kDontPrintIndex);
67 void PrintFieldName(StringBuilder& out, uint32_t struct_index,
68 uint32_t field_index,
69 IndexAsComment index_as_comment = kDontPrintIndex);
70 void PrintTagName(StringBuilder& out, uint32_t tag_index,
71 IndexAsComment index_as_comment = kDontPrintIndex);
72
73 void PrintHeapType(StringBuilder& out, HeapType type);
74 void PrintValueType(StringBuilder& out, ValueType type);
75
76 size_t EstimateCurrentMemoryConsumption() const;
77
78 private:
79 void DecodeNamesIfNotYetDone();
80 void ComputeFunctionNamesFromImportsExports();
81 void ComputeNamesFromImportsExports();
82 void ComputeImportName(const WasmImport& import,
83 std::map<uint32_t, std::string>& target);
84 void ComputeExportName(const WasmExport& ex,
85 std::map<uint32_t, std::string>& target);
86 void WriteRef(StringBuilder& out, WireBytesRef ref);
87
88 // Lazy loading must guard against concurrent modifications from multiple
89 // {WasmModuleObject}s.
91 bool has_decoded_{false};
92 bool has_computed_function_import_names_{false};
93 bool has_computed_import_names_{false};
96 std::unique_ptr<DecodedNameSection> name_section_names_;
97 std::map<uint32_t, std::string> import_export_function_names_;
98 std::map<uint32_t, std::string> import_export_table_names_;
99 std::map<uint32_t, std::string> import_export_memory_names_;
100 std::map<uint32_t, std::string> import_export_global_names_;
101 std::map<uint32_t, std::string> import_export_tag_names_;
102};
103
104// Specialized version for canonical type names.
106 public:
108
109 void DecodeNameSections();
110 void DecodeNames(NativeModule* native_module);
111
112 void PrintTypeName(StringBuilder& out, CanonicalTypeIndex type_index,
113 NamesProvider::IndexAsComment index_as_comment =
116
117 void PrintFieldName(StringBuilder& out, CanonicalTypeIndex struct_index,
118 uint32_t field_index);
119
121
122 private:
123 // TODO(jkummerow): Use Zone allocation for the character payloads?
125
127
128 std::vector<StringT> type_names_;
129 std::map<uint32_t, std::vector<StringT>> field_names_;
132};
133
134} // namespace wasm
135} // namespace internal
136} // namespace v8
137
138#endif // V8_WASM_NAMES_PROVIDER_H_
void PrintValueType(StringBuilder &out, CanonicalValueType type)
void PrintFieldName(StringBuilder &out, CanonicalTypeIndex struct_index, uint32_t field_index)
void PrintTypeName(StringBuilder &out, CanonicalTypeIndex type_index, NamesProvider::IndexAsComment index_as_comment=NamesProvider::kDontPrintIndex)
std::map< uint32_t, std::vector< StringT > > field_names_
void DecodeNames(NativeModule *native_module)
std::map< uint32_t, std::string > import_export_function_names_
void PrintTypeName(StringBuilder &out, ModuleTypeIndex type_index, IndexAsComment index_as_comment=kDontPrintIndex)
std::map< uint32_t, std::string > import_export_memory_names_
base::Vector< const uint8_t > wire_bytes_
std::unique_ptr< DecodedNameSection > name_section_names_
std::map< uint32_t, std::string > import_export_table_names_
std::map< uint32_t, std::string > import_export_global_names_
std::map< uint32_t, std::string > import_export_tag_names_
Definition c-api.cc:87
#define V8_EXPORT_PRIVATE
Definition macros.h:460