V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
wasm-objects-inl.h
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/contexts-inl.h"
11 #include "src/heap/heap-inl.h"
12 #include "src/objects/js-array-buffer-inl.h"
13 #include "src/objects/managed.h"
14 #include "src/v8memory.h"
15 #include "src/wasm/wasm-code-manager.h"
16 #include "src/wasm/wasm-module.h"
17 
18 // Has to be the last include (doesn't have include guards)
19 #include "src/objects/object-macros.h"
20 
21 namespace v8 {
22 namespace internal {
23 
24 CAST_ACCESSOR(WasmDebugInfo)
25 CAST_ACCESSOR(WasmExceptionObject)
26 CAST_ACCESSOR(WasmExportedFunctionData)
27 CAST_ACCESSOR(WasmGlobalObject)
28 CAST_ACCESSOR(WasmInstanceObject)
29 CAST_ACCESSOR(WasmMemoryObject)
30 CAST_ACCESSOR(WasmModuleObject)
31 CAST_ACCESSOR(WasmTableObject)
32 CAST_ACCESSOR(AsmWasmData)
33 
34 #define OPTIONAL_ACCESSORS(holder, name, type, offset) \
35  bool holder::has_##name() { \
36  return !READ_FIELD(this, offset)->IsUndefined(); \
37  } \
38  ACCESSORS(holder, name, type, offset)
39 
40 // TODO(3770): Replacement for the above, temporarily separate.
41 #define OPTIONAL_ACCESSORS2(holder, name, type, offset) \
42  bool holder::has_##name() { \
43  return !READ_FIELD(this, offset)->IsUndefined(); \
44  } \
45  ACCESSORS2(holder, name, type, offset)
46 
47 #define READ_PRIMITIVE_FIELD(p, type, offset) \
48  (*reinterpret_cast<type const*>(FIELD_ADDR(p, offset)))
49 
50 #define WRITE_PRIMITIVE_FIELD(p, type, offset, value) \
51  (*reinterpret_cast<type*>(FIELD_ADDR(p, offset)) = value)
52 
53 #define PRIMITIVE_ACCESSORS(holder, name, type, offset) \
54  type holder::name() const { \
55  return READ_PRIMITIVE_FIELD(this, type, offset); \
56  } \
57  void holder::set_##name(type value) { \
58  WRITE_PRIMITIVE_FIELD(this, type, offset, value); \
59  }
60 
61 // WasmModuleObject
62 ACCESSORS(WasmModuleObject, managed_native_module, Managed<wasm::NativeModule>,
63  kNativeModuleOffset)
64 ACCESSORS2(WasmModuleObject, export_wrappers, FixedArray, kExportWrappersOffset)
65 ACCESSORS(WasmModuleObject, script, Script, kScriptOffset)
66 ACCESSORS(WasmModuleObject, weak_instance_list, WeakArrayList,
67  kWeakInstanceListOffset)
68 OPTIONAL_ACCESSORS2(WasmModuleObject, asm_js_offset_table, ByteArray,
69  kAsmJsOffsetTableOffset)
70 OPTIONAL_ACCESSORS2(WasmModuleObject, breakpoint_infos, FixedArray,
71  kBreakPointInfosOffset)
72 wasm::NativeModule* WasmModuleObject::native_module() const {
73  return managed_native_module()->raw();
74 }
75 const wasm::WasmModule* WasmModuleObject::module() const {
76  // TODO(clemensh): Remove this helper (inline in callers).
77  return native_module()->module();
78 }
79 void WasmModuleObject::reset_breakpoint_infos() {
80  WRITE_FIELD(this, kBreakPointInfosOffset,
81  GetReadOnlyRoots().undefined_value());
82 }
83 bool WasmModuleObject::is_asm_js() {
84  bool asm_js = module()->origin == wasm::kAsmJsOrigin;
85  DCHECK_EQ(asm_js, script()->IsUserJavaScript());
86  DCHECK_EQ(asm_js, has_asm_js_offset_table());
87  return asm_js;
88 }
89 
90 // WasmTableObject
91 ACCESSORS2(WasmTableObject, functions, FixedArray, kFunctionsOffset)
92 ACCESSORS(WasmTableObject, maximum_length, Object, kMaximumLengthOffset)
93 ACCESSORS2(WasmTableObject, dispatch_tables, FixedArray, kDispatchTablesOffset)
94 
95 // WasmMemoryObject
96 ACCESSORS(WasmMemoryObject, array_buffer, JSArrayBuffer, kArrayBufferOffset)
97 SMI_ACCESSORS(WasmMemoryObject, maximum_pages, kMaximumPagesOffset)
98 OPTIONAL_ACCESSORS(WasmMemoryObject, instances, WeakArrayList, kInstancesOffset)
99 
100 // WasmGlobalObject
101 ACCESSORS(WasmGlobalObject, array_buffer, JSArrayBuffer, kArrayBufferOffset)
102 SMI_ACCESSORS(WasmGlobalObject, offset, kOffsetOffset)
103 SMI_ACCESSORS(WasmGlobalObject, flags, kFlagsOffset)
104 BIT_FIELD_ACCESSORS(WasmGlobalObject, flags, type, WasmGlobalObject::TypeBits)
105 BIT_FIELD_ACCESSORS(WasmGlobalObject, flags, is_mutable,
106  WasmGlobalObject::IsMutableBit)
107 
108 int WasmGlobalObject::type_size() const {
109  return wasm::ValueTypes::ElementSizeInBytes(type());
110 }
111 
112 Address WasmGlobalObject::address() const {
113  DCHECK_LE(offset() + type_size(), array_buffer()->byte_length());
114  return Address(array_buffer()->backing_store()) + offset();
115 }
116 
117 int32_t WasmGlobalObject::GetI32() {
118  return ReadLittleEndianValue<int32_t>(address());
119 }
120 
121 int64_t WasmGlobalObject::GetI64() {
122  return ReadLittleEndianValue<int64_t>(address());
123 }
124 
125 float WasmGlobalObject::GetF32() {
126  return ReadLittleEndianValue<float>(address());
127 }
128 
129 double WasmGlobalObject::GetF64() {
130  return ReadLittleEndianValue<double>(address());
131 }
132 
133 void WasmGlobalObject::SetI32(int32_t value) {
134  WriteLittleEndianValue<int32_t>(address(), value);
135 }
136 
137 void WasmGlobalObject::SetI64(int64_t value) {
138  WriteLittleEndianValue<int64_t>(address(), value);
139 }
140 
141 void WasmGlobalObject::SetF32(float value) {
142  WriteLittleEndianValue<float>(address(), value);
143 }
144 
145 void WasmGlobalObject::SetF64(double value) {
146  WriteLittleEndianValue<double>(address(), value);
147 }
148 
149 // WasmInstanceObject
150 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_start, byte*, kMemoryStartOffset)
151 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_size, size_t, kMemorySizeOffset)
152 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_mask, size_t, kMemoryMaskOffset)
153 PRIMITIVE_ACCESSORS(WasmInstanceObject, isolate_root, Address,
154  kIsolateRootOffset)
155 PRIMITIVE_ACCESSORS(WasmInstanceObject, stack_limit_address, Address,
156  kStackLimitAddressOffset)
157 PRIMITIVE_ACCESSORS(WasmInstanceObject, real_stack_limit_address, Address,
158  kRealStackLimitAddressOffset)
159 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_function_targets, Address*,
160  kImportedFunctionTargetsOffset)
161 PRIMITIVE_ACCESSORS(WasmInstanceObject, globals_start, byte*,
162  kGlobalsStartOffset)
163 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_mutable_globals, Address*,
164  kImportedMutableGlobalsOffset)
165 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_size, uint32_t,
166  kIndirectFunctionTableSizeOffset)
167 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_sig_ids,
168  uint32_t*, kIndirectFunctionTableSigIdsOffset)
169 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_targets,
170  Address*, kIndirectFunctionTableTargetsOffset)
171 PRIMITIVE_ACCESSORS(WasmInstanceObject, jump_table_start, Address,
172  kJumpTableStartOffset)
173 
174 ACCESSORS(WasmInstanceObject, module_object, WasmModuleObject,
175  kModuleObjectOffset)
176 ACCESSORS(WasmInstanceObject, exports_object, JSObject, kExportsObjectOffset)
177 ACCESSORS2(WasmInstanceObject, native_context, Context, kNativeContextOffset)
178 OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, WasmMemoryObject,
179  kMemoryObjectOffset)
180 OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, JSArrayBuffer,
181  kGlobalsBufferOffset)
182 OPTIONAL_ACCESSORS2(WasmInstanceObject, imported_mutable_globals_buffers,
183  FixedArray, kImportedMutableGlobalsBuffersOffset)
184 OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, WasmDebugInfo,
185  kDebugInfoOffset)
186 OPTIONAL_ACCESSORS(WasmInstanceObject, table_object, WasmTableObject,
187  kTableObjectOffset)
188 ACCESSORS2(WasmInstanceObject, imported_function_refs, FixedArray,
189  kImportedFunctionRefsOffset)
190 OPTIONAL_ACCESSORS2(WasmInstanceObject, indirect_function_table_refs,
191  FixedArray, kIndirectFunctionTableRefsOffset)
192 OPTIONAL_ACCESSORS(WasmInstanceObject, managed_native_allocations, Foreign,
193  kManagedNativeAllocationsOffset)
194 OPTIONAL_ACCESSORS2(WasmInstanceObject, exceptions_table, FixedArray,
195  kExceptionsTableOffset)
196 ACCESSORS(WasmInstanceObject, undefined_value, Oddball, kUndefinedValueOffset)
197 ACCESSORS(WasmInstanceObject, null_value, Oddball, kNullValueOffset)
198 ACCESSORS2(WasmInstanceObject, centry_stub, Code, kCEntryStubOffset)
199 
200 inline bool WasmInstanceObject::has_indirect_function_table() {
201  return indirect_function_table_sig_ids() != nullptr;
202 }
203 
204 IndirectFunctionTableEntry::IndirectFunctionTableEntry(
205  Handle<WasmInstanceObject> instance, int index)
206  : instance_(instance), index_(index) {
207  DCHECK_GE(index, 0);
208  DCHECK_LT(index, instance->indirect_function_table_size());
209 }
210 
211 ImportedFunctionEntry::ImportedFunctionEntry(
212  Handle<WasmInstanceObject> instance, int index)
213  : instance_(instance), index_(index) {
214  DCHECK_GE(index, 0);
215  DCHECK_LT(index, instance->module()->num_imported_functions);
216 }
217 
218 // WasmExceptionObject
219 ACCESSORS2(WasmExceptionObject, serialized_signature, PodArray<wasm::ValueType>,
220  kSerializedSignatureOffset)
221 ACCESSORS(WasmExceptionObject, exception_tag, HeapObject, kExceptionTagOffset)
222 
223 // WasmExportedFunctionData
224 ACCESSORS2(WasmExportedFunctionData, wrapper_code, Code, kWrapperCodeOffset)
225 ACCESSORS(WasmExportedFunctionData, instance, WasmInstanceObject,
226  kInstanceOffset)
227 SMI_ACCESSORS(WasmExportedFunctionData, jump_table_offset,
228  kJumpTableOffsetOffset)
229 SMI_ACCESSORS(WasmExportedFunctionData, function_index, kFunctionIndexOffset)
230 
231 // WasmDebugInfo
232 ACCESSORS(WasmDebugInfo, wasm_instance, WasmInstanceObject, kInstanceOffset)
233 ACCESSORS(WasmDebugInfo, interpreter_handle, Object, kInterpreterHandleOffset)
234 ACCESSORS2(WasmDebugInfo, interpreted_functions, FixedArray,
235  kInterpretedFunctionsOffset)
236 OPTIONAL_ACCESSORS2(WasmDebugInfo, locals_names, FixedArray, kLocalsNamesOffset)
237 OPTIONAL_ACCESSORS2(WasmDebugInfo, c_wasm_entries, FixedArray,
238  kCWasmEntriesOffset)
239 OPTIONAL_ACCESSORS(WasmDebugInfo, c_wasm_entry_map, Managed<wasm::SignatureMap>,
240  kCWasmEntryMapOffset)
241 
242 #undef OPTIONAL_ACCESSORS
243 #undef READ_PRIMITIVE_FIELD
244 #undef WRITE_PRIMITIVE_FIELD
245 #undef PRIMITIVE_ACCESSORS
246 
247 uint32_t WasmTableObject::current_length() { return functions()->length(); }
248 
249 bool WasmMemoryObject::has_maximum_pages() { return maximum_pages() >= 0; }
250 
251 // AsmWasmData
252 ACCESSORS(AsmWasmData, managed_native_module, Managed<wasm::NativeModule>,
253  kManagedNativeModuleOffset)
254 ACCESSORS2(AsmWasmData, export_wrappers, FixedArray, kExportWrappersOffset)
255 ACCESSORS2(AsmWasmData, asm_js_offset_table, ByteArray, kAsmJsOffsetTableOffset)
256 ACCESSORS(AsmWasmData, uses_bitset, HeapNumber, kUsesBitsetOffset)
257 
258 #include "src/objects/object-macros-undef.h"
259 
260 } // namespace internal
261 } // namespace v8
262 
263 #endif // V8_WASM_WASM_OBJECTS_INL_H_
Definition: libplatform.h:13