Debian Codegraph @

js/src/wasm/WasmModuleTypes.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 *
4 * Copyright 2015 Mozilla Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "wasm/WasmModuleTypes.h"
20
21 #include "mozilla/Range.h"
22
23 #include "vm/JSAtomUtils.h" // AtomizeUTF8Chars
24 #include "vm/MallocProvider.h"
25 #include "wasm/WasmUtility.h"
26
27 #include "vm/JSAtomUtils-inl.h" // AtomToId
28
29 using namespace js;
30 using namespace js::wasm;
31
32 using mozilla::CheckedInt32;
33 using mozilla::MallocSizeOf;
34
35 /* static */
36 CacheableName CacheableName::fromUTF8Chars(UniqueChars&& utf8Chars) {
37 size_t length = strlen(utf8Chars.get());
38 UTF8Bytes bytes;
39 bytes.replaceRawBuffer(utf8Chars.release(), length, length + 1);
40 return CacheableName(std::move(bytes));
41 }
42
43 /* static */
44 bool CacheableName::fromUTF8Chars(const char* utf8Chars, CacheableName* name) {
45 size_t utf8CharsLen = strlen(utf8Chars);
46 UTF8Bytes bytes;
47 if (!bytes.resizeUninitialized(utf8CharsLen)) {
48 return false;
49 }
50 memcpy(bytes.begin(), utf8Chars, utf8CharsLen);
51 *name = CacheableName(std::move(bytes));
52 return true;
53 }
54
55 MOZ_RUNINIT BranchHintVector BranchHintCollection::invalidVector_;
56
57 JSString* CacheableName::toJSString(JSContext* cx) const {
58 return NewStringCopyUTF8N(cx, JS::UTF8Chars(begin(), length()));
59 }
60
61 JSAtom* CacheableName::toAtom(JSContext* cx) const {
62 return AtomizeUTF8Chars(cx, begin(), length());
63 }
64
65 bool CacheableName::toPropertyKey(JSContext* cx,
66 MutableHandleId propertyKey) const {
67 JSAtom* atom = toAtom(cx);
68 if (!atom) {
69 return false;
70 }
71 propertyKey.set(AtomToId(atom));
72 return true;
73 }
74
75 UniqueChars CacheableName::toQuotedString(JSContext* cx) const {
76 RootedString atom(cx, toAtom(cx));
77 if (!atom) {
78 return nullptr;
79 }
80 return QuoteString(cx, atom.get());
81 }
82
83 size_t CacheableName::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
84 return bytes_.sizeOfExcludingThis(mallocSizeOf);
85 }
86
87 size_t Import::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
88 return module.sizeOfExcludingThis(mallocSizeOf) +
89 field.sizeOfExcludingThis(mallocSizeOf);
90 }
91
92 Export::Export(CacheableName&& fieldName, uint32_t index, DefinitionKind kind)
93 : fieldName_(std::move(fieldName)) {
94 pod.kind_ = kind;
95 pod.index_ = index;
96 }
97
98 uint32_t Export::funcIndex() const {
99 MOZ_ASSERT(pod.kind_ == DefinitionKind::Function);
100 return pod.index_;
101 }
102
103 uint32_t Export::memoryIndex() const {
104 MOZ_ASSERT(pod.kind_ == DefinitionKind::Memory);
105 return pod.index_;
106 }
107
108 uint32_t Export::globalIndex() const {
109 MOZ_ASSERT(pod.kind_ == DefinitionKind::Global);
110 return pod.index_;
111 }
112
113 uint32_t Export::tagIndex() const {
114 MOZ_ASSERT(pod.kind_ == DefinitionKind::Tag);
115 return pod.index_;
116 }
117
118 uint32_t Export::tableIndex() const {
119 MOZ_ASSERT(pod.kind_ == DefinitionKind::Table);
120 return pod.index_;
121 }
122
123 size_t Export::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
124 return fieldName_.sizeOfExcludingThis(mallocSizeOf);
125 }
126
127 size_t GlobalDesc::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
128 return initial_.sizeOfExcludingThis(mallocSizeOf);
129 }
130
131 bool TagType::initialize(const SharedTypeDef& funcType) {
132 MOZ_ASSERT(funcType->isFuncType());
133 type_ = funcType;
134
135 const ValTypeVector& args = argTypes();
136 // Compute the byte offsets for arguments when we layout an exception.
137 if (!argOffsets_.resize(args.length())) {
138 return false;
139 }
140
141 StructLayout layout;
142 for (size_t i = 0; i < args.length(); i++) {
143 CheckedInt32 offset = layout.addField(StorageType(args[i].packed()));
144 if (!offset.isValid()) {
145 return false;
146 }
147 argOffsets_[i] = offset.value();
148 }
149
150 // Find the total size of all the arguments.
151 CheckedInt32 size = layout.close();
152 if (!size.isValid()) {
153 return false;
154 }
155 this->size_ = size.value();
156
157 return true;
158 }
159
160 size_t TagType::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
161 return argOffsets_.sizeOfExcludingThis(mallocSizeOf);
162 }
163
164 size_t TagDesc::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
165 return type->sizeOfExcludingThis(mallocSizeOf);
166 }
167
168 size_t ModuleElemSegment::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
169 return SizeOfMaybeExcludingThis(offsetIfActive, mallocSizeOf) +
170 elemIndices.sizeOfExcludingThis(mallocSizeOf);
171 }
172
173 size_t ModuleElemSegment::Expressions::sizeOfExcludingThis(
174 MallocSizeOf mallocSizeOf) const {
175 return exprBytes.sizeOfExcludingThis(mallocSizeOf);
176 }
177
178 size_t DataSegment::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
179 return SizeOfMaybeExcludingThis(offsetIfActive, mallocSizeOf) +
180 bytes.sizeOfExcludingThis(mallocSizeOf);
181 }
182
183 size_t CustomSection::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
184 return name.sizeOfExcludingThis(mallocSizeOf) + sizeof(*payload) +
185 payload->sizeOfExcludingThis(mallocSizeOf);
186 }
187
188 size_t NameSection::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const {
189 return funcNames.sizeOfExcludingThis(mallocSizeOf);
190 }
191
192 const char* wasm::ToString(LimitsKind kind) {
193 switch (kind) {
194 case LimitsKind::Memory:
195 return "Memory";
196 case LimitsKind::Table:
197 return "Table";
198 default:
199 MOZ_CRASH();
200 }
201 }