1 // Copyright 2017 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_WASM_OBJECTS_INL_H_
6 #define V8_WASM_WASM_OBJECTS_INL_H_
7
8 #include "src/wasm/wasm-objects.h"
9
10 #include "src/base/memory.h"
11 #include "src/heap/heap-write-barrier-inl.h"
12 #include "src/objects/contexts-inl.h"
13 #include "src/objects/foreign-inl.h"
14 #include "src/objects/heap-number-inl.h"
15 #include "src/objects/js-array-buffer-inl.h"
16 #include "src/objects/js-function-inl.h"
17 #include "src/objects/js-objects-inl.h"
18 #include "src/objects/managed.h"
19 #include "src/objects/oddball-inl.h"
20 #include "src/objects/script-inl.h"
21 #include "src/roots/roots.h"
22 #include "src/wasm/wasm-code-manager.h"
23 #include "src/wasm/wasm-module.h"
24
25 // Has to be the last include (doesn't have include guards)
26 #include "src/objects/object-macros.h"
27
28 namespace v8 {
29 namespace internal {
30
31 #include "torque-generated/src/wasm/wasm-objects-tq-inl.inc"
32
OBJECT_CONSTRUCTORS_IMPL(WasmExceptionObject,JSObject)33 OBJECT_CONSTRUCTORS_IMPL(WasmExceptionObject, JSObject)
34 TQ_OBJECT_CONSTRUCTORS_IMPL(WasmExceptionTag)
35 OBJECT_CONSTRUCTORS_IMPL(WasmExportedFunctionData, Struct)
36 OBJECT_CONSTRUCTORS_IMPL(WasmGlobalObject, JSObject)
37 OBJECT_CONSTRUCTORS_IMPL(WasmInstanceObject, JSObject)
38 OBJECT_CONSTRUCTORS_IMPL(WasmMemoryObject, JSObject)
39 OBJECT_CONSTRUCTORS_IMPL(WasmModuleObject, JSObject)
40 OBJECT_CONSTRUCTORS_IMPL(WasmTableObject, JSObject)
41 OBJECT_CONSTRUCTORS_IMPL(AsmWasmData, Struct)
42 TQ_OBJECT_CONSTRUCTORS_IMPL(WasmTypeInfo)
43 TQ_OBJECT_CONSTRUCTORS_IMPL(WasmStruct)
44 TQ_OBJECT_CONSTRUCTORS_IMPL(WasmArray)
45
46 CAST_ACCESSOR(WasmExceptionObject)
47 CAST_ACCESSOR(WasmExportedFunctionData)
48 CAST_ACCESSOR(WasmGlobalObject)
49 CAST_ACCESSOR(WasmInstanceObject)
50 CAST_ACCESSOR(WasmMemoryObject)
51 CAST_ACCESSOR(WasmModuleObject)
52 CAST_ACCESSOR(WasmTableObject)
53 CAST_ACCESSOR(AsmWasmData)
54 CAST_ACCESSOR(WasmTypeInfo)
55 CAST_ACCESSOR(WasmStruct)
56 CAST_ACCESSOR(WasmArray)
57
58 #define OPTIONAL_ACCESSORS(holder, name, type, offset) \
59 DEF_GETTER(holder, has_##name, bool) { \
60 Object value = TaggedField<Object, offset>::load(isolate, *this); \
61 return !value.IsUndefined(GetReadOnlyRoots(isolate)); \
62 } \
63 ACCESSORS_CHECKED2(holder, name, type, offset, \
64 !value.IsUndefined(GetReadOnlyRoots(isolate)), true)
65
66 #define PRIMITIVE_ACCESSORS(holder, name, type, offset) \
67 type holder::name() const { \
68 if (COMPRESS_POINTERS_BOOL && alignof(type) > kTaggedSize) { \
69 /* TODO(ishell, v8:8875): When pointer compression is enabled 8-byte */ \
70 /* size fields (external pointers, doubles and BigInt data) are only */ \
71 /* kTaggedSize aligned so we have to use unaligned pointer friendly */ \
72 /* way of accessing them in order to avoid undefined behavior in C++ */ \
73 /* code. */ \
74 return base::ReadUnalignedValue<type>(FIELD_ADDR(*this, offset)); \
75 } else { \
76 return *reinterpret_cast<type const*>(FIELD_ADDR(*this, offset)); \
77 } \
78 } \
79 void holder::set_##name(type value) { \
80 if (COMPRESS_POINTERS_BOOL && alignof(type) > kTaggedSize) { \
81 /* TODO(ishell, v8:8875): When pointer compression is enabled 8-byte */ \
82 /* size fields (external pointers, doubles and BigInt data) are only */ \
83 /* kTaggedSize aligned so we have to use unaligned pointer friendly */ \
84 /* way of accessing them in order to avoid undefined behavior in C++ */ \
85 /* code. */ \
86 base::WriteUnalignedValue<type>(FIELD_ADDR(*this, offset), value); \
87 } else { \
88 *reinterpret_cast<type*>(FIELD_ADDR(*this, offset)) = value; \
89 } \
90 }
91
92 // WasmModuleObject
93 ACCESSORS(WasmModuleObject, managed_native_module, Managed<wasm::NativeModule>,
94 kNativeModuleOffset)
95 ACCESSORS(WasmModuleObject, export_wrappers, FixedArray, kExportWrappersOffset)
96 ACCESSORS(WasmModuleObject, script, Script, kScriptOffset)
97 wasm::NativeModule* WasmModuleObject::native_module() const {
98 return managed_native_module().raw();
99 }
100 const std::shared_ptr<wasm::NativeModule>&
shared_native_module()101 WasmModuleObject::shared_native_module() const {
102 return managed_native_module().get();
103 }
module()104 const wasm::WasmModule* WasmModuleObject::module() const {
105 // TODO(clemensb): Remove this helper (inline in callers).
106 return native_module()->module();
107 }
is_asm_js()108 bool WasmModuleObject::is_asm_js() {
109 bool asm_js = is_asmjs_module(module());
110 DCHECK_EQ(asm_js, script().IsUserJavaScript());
111 return asm_js;
112 }
113
114 // WasmTableObject
ACCESSORS(WasmTableObject,instance,HeapObject,kInstanceOffset)115 ACCESSORS(WasmTableObject, instance, HeapObject, kInstanceOffset)
116 ACCESSORS(WasmTableObject, entries, FixedArray, kEntriesOffset)
117 SMI_ACCESSORS(WasmTableObject, current_length, kCurrentLengthOffset)
118 ACCESSORS(WasmTableObject, maximum_length, Object, kMaximumLengthOffset)
119 ACCESSORS(WasmTableObject, dispatch_tables, FixedArray, kDispatchTablesOffset)
120 SMI_ACCESSORS(WasmTableObject, raw_type, kRawTypeOffset)
121
122 // WasmMemoryObject
123 ACCESSORS(WasmMemoryObject, array_buffer, JSArrayBuffer, kArrayBufferOffset)
124 SMI_ACCESSORS(WasmMemoryObject, maximum_pages, kMaximumPagesOffset)
125 OPTIONAL_ACCESSORS(WasmMemoryObject, instances, WeakArrayList, kInstancesOffset)
126
127 // WasmGlobalObject
128 ACCESSORS(WasmGlobalObject, instance, HeapObject, kInstanceOffset)
129 ACCESSORS(WasmGlobalObject, untagged_buffer, JSArrayBuffer,
130 kUntaggedBufferOffset)
131 ACCESSORS(WasmGlobalObject, tagged_buffer, FixedArray, kTaggedBufferOffset)
132 SMI_ACCESSORS(WasmGlobalObject, offset, kOffsetOffset)
133 // TODO(7748): This will not suffice to hold the 32-bit encoding of a ValueType.
134 // We need to devise and encoding that does, and also encodes is_mutable.
135 SMI_ACCESSORS(WasmGlobalObject, raw_type, kRawTypeOffset)
136 SMI_ACCESSORS(WasmGlobalObject, is_mutable, kIsMutableOffset)
137
138 wasm::ValueType WasmGlobalObject::type() const {
139 return wasm::ValueType::FromRawBitField(static_cast<uint32_t>(raw_type()));
140 }
set_type(wasm::ValueType value)141 void WasmGlobalObject::set_type(wasm::ValueType value) {
142 set_raw_type(static_cast<int>(value.raw_bit_field()));
143 }
144
type_size()145 int WasmGlobalObject::type_size() const { return type().element_size_bytes(); }
146
address()147 Address WasmGlobalObject::address() const {
148 DCHECK_NE(type(), wasm::kWasmExternRef);
149 DCHECK_LE(offset() + type_size(), untagged_buffer().byte_length());
150 return Address(untagged_buffer().backing_store()) + offset();
151 }
152
GetI32()153 int32_t WasmGlobalObject::GetI32() {
154 return base::ReadLittleEndianValue<int32_t>(address());
155 }
156
GetI64()157 int64_t WasmGlobalObject::GetI64() {
158 return base::ReadLittleEndianValue<int64_t>(address());
159 }
160
GetF32()161 float WasmGlobalObject::GetF32() {
162 return base::ReadLittleEndianValue<float>(address());
163 }
164
GetF64()165 double WasmGlobalObject::GetF64() {
166 return base::ReadLittleEndianValue<double>(address());
167 }
168
GetRef()169 Handle<Object> WasmGlobalObject::GetRef() {
170 // We use this getter for externref, funcref, and exnref.
171 DCHECK(type().is_reference_type());
172 return handle(tagged_buffer().get(offset()), GetIsolate());
173 }
174
SetI32(int32_t value)175 void WasmGlobalObject::SetI32(int32_t value) {
176 base::WriteLittleEndianValue<int32_t>(address(), value);
177 }
178
SetI64(int64_t value)179 void WasmGlobalObject::SetI64(int64_t value) {
180 base::WriteLittleEndianValue<int64_t>(address(), value);
181 }
182
SetF32(float value)183 void WasmGlobalObject::SetF32(float value) {
184 base::WriteLittleEndianValue<float>(address(), value);
185 }
186
SetF64(double value)187 void WasmGlobalObject::SetF64(double value) {
188 base::WriteLittleEndianValue<double>(address(), value);
189 }
190
SetExternRef(Handle<Object> value)191 void WasmGlobalObject::SetExternRef(Handle<Object> value) {
192 // We use this getter externref and exnref.
193 DCHECK(type().is_reference_to(wasm::HeapType::kExtern) ||
194 type().is_reference_to(wasm::HeapType::kExn));
195 tagged_buffer().set(offset(), *value);
196 }
197
SetFuncRef(Isolate * isolate,Handle<Object> value)198 bool WasmGlobalObject::SetFuncRef(Isolate* isolate, Handle<Object> value) {
199 DCHECK_EQ(type(), wasm::kWasmFuncRef);
200 if (!value->IsNull(isolate) &&
201 !WasmExternalFunction::IsWasmExternalFunction(*value) &&
202 !WasmCapiFunction::IsWasmCapiFunction(*value)) {
203 return false;
204 }
205 tagged_buffer().set(offset(), *value);
206 return true;
207 }
208
209 // WasmInstanceObject
PRIMITIVE_ACCESSORS(WasmInstanceObject,memory_start,byte *,kMemoryStartOffset)210 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_start, byte*, kMemoryStartOffset)
211 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_size, size_t, kMemorySizeOffset)
212 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_mask, size_t, kMemoryMaskOffset)
213 PRIMITIVE_ACCESSORS(WasmInstanceObject, isolate_root, Address,
214 kIsolateRootOffset)
215 PRIMITIVE_ACCESSORS(WasmInstanceObject, stack_limit_address, Address,
216 kStackLimitAddressOffset)
217 PRIMITIVE_ACCESSORS(WasmInstanceObject, real_stack_limit_address, Address,
218 kRealStackLimitAddressOffset)
219 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_function_targets, Address*,
220 kImportedFunctionTargetsOffset)
221 PRIMITIVE_ACCESSORS(WasmInstanceObject, globals_start, byte*,
222 kGlobalsStartOffset)
223 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_mutable_globals, Address*,
224 kImportedMutableGlobalsOffset)
225 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_size, uint32_t,
226 kIndirectFunctionTableSizeOffset)
227 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_sig_ids,
228 uint32_t*, kIndirectFunctionTableSigIdsOffset)
229 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_targets,
230 Address*, kIndirectFunctionTableTargetsOffset)
231 PRIMITIVE_ACCESSORS(WasmInstanceObject, jump_table_start, Address,
232 kJumpTableStartOffset)
233 PRIMITIVE_ACCESSORS(WasmInstanceObject, data_segment_starts, Address*,
234 kDataSegmentStartsOffset)
235 PRIMITIVE_ACCESSORS(WasmInstanceObject, data_segment_sizes, uint32_t*,
236 kDataSegmentSizesOffset)
237 PRIMITIVE_ACCESSORS(WasmInstanceObject, dropped_elem_segments, byte*,
238 kDroppedElemSegmentsOffset)
239 PRIMITIVE_ACCESSORS(WasmInstanceObject, hook_on_function_call_address, Address,
240 kHookOnFunctionCallAddressOffset)
241 PRIMITIVE_ACCESSORS(WasmInstanceObject, num_liftoff_function_calls_array,
242 uint32_t*, kNumLiftoffFunctionCallsArrayOffset)
243
244 ACCESSORS(WasmInstanceObject, module_object, WasmModuleObject,
245 kModuleObjectOffset)
246 ACCESSORS(WasmInstanceObject, exports_object, JSObject, kExportsObjectOffset)
247 ACCESSORS(WasmInstanceObject, native_context, Context, kNativeContextOffset)
248 OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, WasmMemoryObject,
249 kMemoryObjectOffset)
250 OPTIONAL_ACCESSORS(WasmInstanceObject, untagged_globals_buffer, JSArrayBuffer,
251 kUntaggedGlobalsBufferOffset)
252 OPTIONAL_ACCESSORS(WasmInstanceObject, tagged_globals_buffer, FixedArray,
253 kTaggedGlobalsBufferOffset)
254 OPTIONAL_ACCESSORS(WasmInstanceObject, imported_mutable_globals_buffers,
255 FixedArray, kImportedMutableGlobalsBuffersOffset)
256 OPTIONAL_ACCESSORS(WasmInstanceObject, tables, FixedArray, kTablesOffset)
257 OPTIONAL_ACCESSORS(WasmInstanceObject, indirect_function_tables, FixedArray,
258 kIndirectFunctionTablesOffset)
259 ACCESSORS(WasmInstanceObject, imported_function_refs, FixedArray,
260 kImportedFunctionRefsOffset)
261 OPTIONAL_ACCESSORS(WasmInstanceObject, indirect_function_table_refs, FixedArray,
262 kIndirectFunctionTableRefsOffset)
263 OPTIONAL_ACCESSORS(WasmInstanceObject, managed_native_allocations, Foreign,
264 kManagedNativeAllocationsOffset)
265 OPTIONAL_ACCESSORS(WasmInstanceObject, exceptions_table, FixedArray,
266 kExceptionsTableOffset)
267 OPTIONAL_ACCESSORS(WasmInstanceObject, wasm_external_functions, FixedArray,
268 kWasmExternalFunctionsOffset)
269 ACCESSORS(WasmInstanceObject, managed_object_maps, FixedArray,
270 kManagedObjectMapsOffset)
271
272 void WasmInstanceObject::clear_padding() {
273 if (FIELD_SIZE(kOptionalPaddingOffset) != 0) {
274 DCHECK_EQ(4, FIELD_SIZE(kOptionalPaddingOffset));
275 memset(reinterpret_cast<void*>(address() + kOptionalPaddingOffset), 0,
276 FIELD_SIZE(kOptionalPaddingOffset));
277 }
278 }
279
IndirectFunctionTableEntry(Handle<WasmInstanceObject> instance,int table_index,int entry_index)280 IndirectFunctionTableEntry::IndirectFunctionTableEntry(
281 Handle<WasmInstanceObject> instance, int table_index, int entry_index)
282 : instance_(table_index == 0 ? instance
283 : Handle<WasmInstanceObject>::null()),
284 table_(table_index != 0
285 ? handle(WasmIndirectFunctionTable::cast(
286 instance->indirect_function_tables().get(
287 table_index)),
288 instance->GetIsolate())
289 : Handle<WasmIndirectFunctionTable>::null()),
290 index_(entry_index) {
291 DCHECK_GE(entry_index, 0);
292 DCHECK_LT(entry_index, table_index == 0
293 ? instance->indirect_function_table_size()
294 : table_->size());
295 }
296
IndirectFunctionTableEntry(Handle<WasmIndirectFunctionTable> table,int entry_index)297 IndirectFunctionTableEntry::IndirectFunctionTableEntry(
298 Handle<WasmIndirectFunctionTable> table, int entry_index)
299 : instance_(Handle<WasmInstanceObject>::null()),
300 table_(table),
301 index_(entry_index) {
302 DCHECK_GE(entry_index, 0);
303 DCHECK_LT(entry_index, table_->size());
304 }
305
ImportedFunctionEntry(Handle<WasmInstanceObject> instance,int index)306 ImportedFunctionEntry::ImportedFunctionEntry(
307 Handle<WasmInstanceObject> instance, int index)
308 : instance_(instance), index_(index) {
309 DCHECK_GE(index, 0);
310 DCHECK_LT(index, instance->module()->num_imported_functions);
311 }
312
313 // WasmExceptionObject
ACCESSORS(WasmExceptionObject,serialized_signature,PodArray<wasm::ValueType>,kSerializedSignatureOffset)314 ACCESSORS(WasmExceptionObject, serialized_signature, PodArray<wasm::ValueType>,
315 kSerializedSignatureOffset)
316 ACCESSORS(WasmExceptionObject, exception_tag, HeapObject, kExceptionTagOffset)
317
318 // WasmExceptionPackage
319 OBJECT_CONSTRUCTORS_IMPL(WasmExceptionPackage, JSReceiver)
320 CAST_ACCESSOR(WasmExceptionPackage)
321
322 // WasmExportedFunction
323 WasmExportedFunction::WasmExportedFunction(Address ptr) : JSFunction(ptr) {
324 SLOW_DCHECK(IsWasmExportedFunction(*this));
325 }
326 CAST_ACCESSOR(WasmExportedFunction)
327
328 // WasmExportedFunctionData
ACCESSORS(WasmExportedFunctionData,wrapper_code,Code,kWrapperCodeOffset)329 ACCESSORS(WasmExportedFunctionData, wrapper_code, Code, kWrapperCodeOffset)
330 ACCESSORS(WasmExportedFunctionData, instance, WasmInstanceObject,
331 kInstanceOffset)
332 SMI_ACCESSORS(WasmExportedFunctionData, jump_table_offset,
333 kJumpTableOffsetOffset)
334 SMI_ACCESSORS(WasmExportedFunctionData, function_index, kFunctionIndexOffset)
335 ACCESSORS(WasmExportedFunctionData, signature, Foreign, kSignatureOffset)
336 SMI_ACCESSORS(WasmExportedFunctionData, call_count, kCallCountOffset)
337 ACCESSORS(WasmExportedFunctionData, c_wrapper_code, Object, kCWrapperCodeOffset)
338 ACCESSORS(WasmExportedFunctionData, wasm_call_target, Object,
339 kWasmCallTargetOffset)
340 SMI_ACCESSORS(WasmExportedFunctionData, packed_args_size, kPackedArgsSizeOffset)
341
342 // WasmJSFunction
343 WasmJSFunction::WasmJSFunction(Address ptr) : JSFunction(ptr) {
344 SLOW_DCHECK(IsWasmJSFunction(*this));
345 }
346 CAST_ACCESSOR(WasmJSFunction)
347
348 // WasmJSFunctionData
OBJECT_CONSTRUCTORS_IMPL(WasmJSFunctionData,Struct)349 OBJECT_CONSTRUCTORS_IMPL(WasmJSFunctionData, Struct)
350 CAST_ACCESSOR(WasmJSFunctionData)
351 SMI_ACCESSORS(WasmJSFunctionData, serialized_return_count,
352 kSerializedReturnCountOffset)
353 SMI_ACCESSORS(WasmJSFunctionData, serialized_parameter_count,
354 kSerializedParameterCountOffset)
355 ACCESSORS(WasmJSFunctionData, serialized_signature, PodArray<wasm::ValueType>,
356 kSerializedSignatureOffset)
357 ACCESSORS(WasmJSFunctionData, callable, JSReceiver, kCallableOffset)
358 ACCESSORS(WasmJSFunctionData, wrapper_code, Code, kWrapperCodeOffset)
359 ACCESSORS(WasmJSFunctionData, wasm_to_js_wrapper_code, Code,
360 kWasmToJsWrapperCodeOffset)
361
362 // WasmCapiFunction
363 WasmCapiFunction::WasmCapiFunction(Address ptr) : JSFunction(ptr) {
364 SLOW_DCHECK(IsWasmCapiFunction(*this));
365 }
CAST_ACCESSOR(WasmCapiFunction)366 CAST_ACCESSOR(WasmCapiFunction)
367
368 // WasmExternalFunction
369 WasmExternalFunction::WasmExternalFunction(Address ptr) : JSFunction(ptr) {
370 SLOW_DCHECK(IsWasmExternalFunction(*this));
371 }
372 CAST_ACCESSOR(WasmExternalFunction)
373
374 // WasmIndirectFunctionTable
OBJECT_CONSTRUCTORS_IMPL(WasmIndirectFunctionTable,Struct)375 OBJECT_CONSTRUCTORS_IMPL(WasmIndirectFunctionTable, Struct)
376 CAST_ACCESSOR(WasmIndirectFunctionTable)
377 PRIMITIVE_ACCESSORS(WasmIndirectFunctionTable, size, uint32_t, kSizeOffset)
378 PRIMITIVE_ACCESSORS(WasmIndirectFunctionTable, sig_ids, uint32_t*,
379 kSigIdsOffset)
380 PRIMITIVE_ACCESSORS(WasmIndirectFunctionTable, targets, Address*,
381 kTargetsOffset)
382 OPTIONAL_ACCESSORS(WasmIndirectFunctionTable, managed_native_allocations,
383 Foreign, kManagedNativeAllocationsOffset)
384 ACCESSORS(WasmIndirectFunctionTable, refs, FixedArray, kRefsOffset)
385
386 #undef OPTIONAL_ACCESSORS
387 #undef READ_PRIMITIVE_FIELD
388 #undef WRITE_PRIMITIVE_FIELD
389 #undef PRIMITIVE_ACCESSORS
390
391 wasm::ValueType WasmTableObject::type() {
392 // TODO(7748): Support other table types? Wait for spec to clear up.
393 return wasm::ValueType::Ref(raw_type(), wasm::kNullable);
394 }
395
has_maximum_pages()396 bool WasmMemoryObject::has_maximum_pages() { return maximum_pages() >= 0; }
397
398 // AsmWasmData
ACCESSORS(AsmWasmData,managed_native_module,Managed<wasm::NativeModule>,kManagedNativeModuleOffset)399 ACCESSORS(AsmWasmData, managed_native_module, Managed<wasm::NativeModule>,
400 kManagedNativeModuleOffset)
401 ACCESSORS(AsmWasmData, export_wrappers, FixedArray, kExportWrappersOffset)
402 ACCESSORS(AsmWasmData, uses_bitset, HeapNumber, kUsesBitsetOffset)
403
404 wasm::StructType* WasmStruct::type(Map map) {
405 WasmTypeInfo type_info = map.wasm_type_info();
406 return reinterpret_cast<wasm::StructType*>(type_info.foreign_address());
407 }
408
GcSafeType(Map map)409 wasm::StructType* WasmStruct::GcSafeType(Map map) {
410 DCHECK_EQ(WASM_STRUCT_TYPE, map.instance_type());
411 HeapObject raw = HeapObject::cast(map.constructor_or_backpointer());
412 MapWord map_word = raw.map_word();
413 HeapObject forwarded =
414 map_word.IsForwardingAddress() ? map_word.ToForwardingAddress() : raw;
415 Foreign foreign = Foreign::cast(forwarded);
416 return reinterpret_cast<wasm::StructType*>(foreign.foreign_address());
417 }
418
type()419 wasm::StructType* WasmStruct::type() const { return type(map()); }
420
RawField(int raw_offset)421 ObjectSlot WasmStruct::RawField(int raw_offset) {
422 int offset = WasmStruct::kHeaderSize + raw_offset;
423 return ObjectSlot(FIELD_ADDR(*this, offset));
424 }
425
type(Map map)426 wasm::ArrayType* WasmArray::type(Map map) {
427 DCHECK_EQ(WASM_ARRAY_TYPE, map.instance_type());
428 WasmTypeInfo type_info = map.wasm_type_info();
429 return reinterpret_cast<wasm::ArrayType*>(type_info.foreign_address());
430 }
431
GcSafeType(Map map)432 wasm::ArrayType* WasmArray::GcSafeType(Map map) {
433 DCHECK_EQ(WASM_ARRAY_TYPE, map.instance_type());
434 HeapObject raw = HeapObject::cast(map.constructor_or_backpointer());
435 MapWord map_word = raw.map_word();
436 HeapObject forwarded =
437 map_word.IsForwardingAddress() ? map_word.ToForwardingAddress() : raw;
438 Foreign foreign = Foreign::cast(forwarded);
439 return reinterpret_cast<wasm::ArrayType*>(foreign.foreign_address());
440 }
441
type()442 wasm::ArrayType* WasmArray::type() const { return type(map()); }
443
SizeFor(Map map,int length)444 int WasmArray::SizeFor(Map map, int length) {
445 int element_size = type(map)->element_type().element_size_bytes();
446 return kHeaderSize + RoundUp(element_size * length, kTaggedSize);
447 }
448
clear_foreign_address(Isolate * isolate)449 void WasmTypeInfo::clear_foreign_address(Isolate* isolate) {
450 #ifdef V8_HEAP_SANDBOX
451 // Due to the type-specific pointer tags for external pointers, we need to
452 // allocate an entry in the table here even though it will just store nullptr.
453 AllocateExternalPointerEntries(isolate);
454 #endif
455 set_foreign_address(isolate, 0);
456 }
457
458 #include "src/objects/object-macros-undef.h"
459
460 } // namespace internal
461 } // namespace v8
462
463 #endif // V8_WASM_WASM_OBJECTS_INL_H_
464