wasm-objects.h 40.1 KB
Newer Older
1 2
// Copyright 2016 the V8 project authors. All rights reserved.  Use of
// this source code is governed by a BSD-style license that can be
3 4
// found in the LICENSE file.

5 6 7 8
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

9 10
#ifndef V8_WASM_WASM_OBJECTS_H_
#define V8_WASM_WASM_OBJECTS_H_
11

12 13
#include <memory>

14
#include "src/base/bit-field.h"
15
#include "src/base/bits.h"
16
#include "src/codegen/signature.h"
17
#include "src/debug/debug.h"
Marja Hölttä's avatar
Marja Hölttä committed
18
#include "src/heap/heap.h"
19
#include "src/objects/js-function.h"
20
#include "src/objects/js-objects.h"
21
#include "src/objects/objects.h"
22
#include "src/wasm/struct-types.h"
23
#include "src/wasm/value-type.h"
24 25 26 27

// Has to be the last include (doesn't have include guards)
#include "src/objects/object-macros.h"

28 29 30
namespace v8 {
namespace internal {
namespace wasm {
31
class InterpretedFrame;
32 33
class NativeModule;
class WasmCode;
34
struct WasmGlobal;
35
struct WasmModule;
36
struct WasmTag;
37
class WasmValue;
Marja Hölttä's avatar
Marja Hölttä committed
38
class WireBytesRef;
39
}  // namespace wasm
40

Marja Hölttä's avatar
Marja Hölttä committed
41 42 43
class BreakPoint;
class JSArrayBuffer;
class SeqOneByteString;
44
class WasmCapiFunction;
45
class WasmExceptionTag;
46
class WasmExportedFunction;
47
class WasmExternalFunction;
48
class WasmInstanceObject;
49
class WasmJSFunction;
50
class WasmModuleObject;
51

52 53
enum class SharedFlag : uint8_t;

Marja Hölttä's avatar
Marja Hölttä committed
54 55 56
template <class CppType>
class Managed;

57 58
#include "torque-generated/src/wasm/wasm-objects-tq.inc"

59
#define DECL_OPTIONAL_ACCESSORS(name, type) \
60
  DECL_GETTER(has_##name, bool)             \
61
  DECL_ACCESSORS(name, type)
62

63 64 65 66
// A helper for an entry in an indirect function table (IFT).
// The underlying storage in the instance is used by generated code to
// call functions indirectly at runtime.
// Each entry has the following fields:
67
// - object = target instance, if a Wasm function, tuple if imported
68
// - sig_id = signature id of function
69
// - target = entrypoint to Wasm code or import wrapper code
70
class V8_EXPORT_PRIVATE IndirectFunctionTableEntry {
71
 public:
72 73 74 75 76
  inline IndirectFunctionTableEntry(Handle<WasmInstanceObject>, int table_index,
                                    int entry_index);

  inline IndirectFunctionTableEntry(Handle<WasmIndirectFunctionTable> table,
                                    int entry_index);
77

78
  void clear();
79 80
  void Set(int sig_id, Handle<WasmInstanceObject> target_instance,
           int target_func_index);
81
  void Set(int sig_id, Address call_target, Object ref);
82

83 84 85
  Object object_ref() const;
  int sig_id() const;
  Address target() const;
86 87

 private:
88
  Handle<WasmInstanceObject> const instance_;
89
  Handle<WasmIndirectFunctionTable> const table_;
90
  int const index_;
91 92
};

93 94 95 96
// A helper for an entry for an imported function, indexed statically.
// The underlying storage in the instance is used by generated code to
// call imported functions at runtime.
// Each entry is either:
97
//   - Wasm to JS, which has fields
98 99
//      - object = a Tuple2 of the importing instance and the callable
//      - target = entrypoint to import wrapper code
100
//   - Wasm to Wasm, which has fields
101 102
//      - object = target instance
//      - target = entrypoint for the function
103 104
class ImportedFunctionEntry {
 public:
105
  inline ImportedFunctionEntry(Handle<WasmInstanceObject>, int index);
106

107
  // Initialize this entry as a Wasm to JS call. This accepts the isolate as a
108
  // parameter, since it must allocate a tuple.
109 110
  V8_EXPORT_PRIVATE void SetWasmToJs(Isolate*, Handle<JSReceiver> callable,
                                     const wasm::WasmCode* wasm_to_js_wrapper);
111
  // Initialize this entry as a Wasm to Wasm call.
112
  void SetWasmToWasm(WasmInstanceObject target_instance, Address call_target);
113

114 115
  WasmInstanceObject instance();
  JSReceiver callable();
116
  Object maybe_callable();
117
  Object object_ref();
118 119 120
  Address target();

 private:
121
  Handle<WasmInstanceObject> const instance_;
122
  int const index_;
123 124
};

125 126
enum InternalizeString : bool { kInternalize = true, kNoInternalize = false };

127
// Representation of a WebAssembly.Module JavaScript-level object.
128 129
class WasmModuleObject
    : public TorqueGeneratedWasmModuleObject<WasmModuleObject, JSObject> {
130
 public:
131
  inline wasm::NativeModule* native_module() const;
132 133
  inline const std::shared_ptr<wasm::NativeModule>& shared_native_module()
      const;
134
  inline const wasm::WasmModule* module() const;
135 136

  // Dispatched behavior.
137
  DECL_PRINTER(WasmModuleObject)
138

139 140
  // Creates a new {WasmModuleObject} for an existing {NativeModule} that is
  // reference counted and might be shared between multiple Isolates.
141
  V8_EXPORT_PRIVATE static Handle<WasmModuleObject> New(
142
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
143 144 145 146
      Handle<Script> script);
  V8_EXPORT_PRIVATE static Handle<WasmModuleObject> New(
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
      Handle<Script> script, Handle<FixedArray> export_wrappers);
147

148
  // Check whether this module was generated from asm.js source.
149
  inline bool is_asm_js();
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  // Get the module name, if set. Returns an empty handle otherwise.
  static MaybeHandle<String> GetModuleNameOrNull(Isolate*,
                                                 Handle<WasmModuleObject>);

  // Get the function name of the function identified by the given index.
  // Returns a null handle if the function is unnamed or the name is not a valid
  // UTF-8 string.
  static MaybeHandle<String> GetFunctionNameOrNull(Isolate*,
                                                   Handle<WasmModuleObject>,
                                                   uint32_t func_index);

  // Get the raw bytes of the function name of the function identified by the
  // given index.
  // Meant to be used for debugging or frame printing.
  // Does not allocate, hence gc-safe.
166
  base::Vector<const uint8_t> GetRawFunctionName(int func_index);
167

168 169 170
  // Extract a portion of the wire bytes as UTF-8 string, optionally
  // internalized. (Prefer to internalize early if the string will be used for a
  // property lookup anyway.)
171
  static Handle<String> ExtractUtf8StringFromModuleBytes(
172 173
      Isolate*, Handle<WasmModuleObject>, wasm::WireBytesRef,
      InternalizeString);
174
  static Handle<String> ExtractUtf8StringFromModuleBytes(
175
      Isolate*, base::Vector<const uint8_t> wire_byte, wasm::WireBytesRef,
176
      InternalizeString);
177

178
  TQ_OBJECT_CONSTRUCTORS(WasmModuleObject)
179 180 181
};

// Representation of a WebAssembly.Table JavaScript-level object.
182 183
class WasmTableObject
    : public TorqueGeneratedWasmTableObject<WasmTableObject, JSObject> {
184
 public:
185 186
  // Dispatched behavior.
  DECL_PRINTER(WasmTableObject)
187

188
  inline wasm::ValueType type();
189

190 191 192
  V8_EXPORT_PRIVATE static int Grow(Isolate* isolate,
                                    Handle<WasmTableObject> table,
                                    uint32_t count, Handle<Object> init_value);
193

194 195 196 197
  V8_EXPORT_PRIVATE static Handle<WasmTableObject> New(
      Isolate* isolate, Handle<WasmInstanceObject> instance,
      wasm::ValueType type, uint32_t initial, bool has_maximum,
      uint32_t maximum, Handle<FixedArray>* entries);
198

199 200 201
  V8_EXPORT_PRIVATE static void AddDispatchTable(
      Isolate* isolate, Handle<WasmTableObject> table,
      Handle<WasmInstanceObject> instance, int table_index);
202

203 204 205
  static bool IsInBounds(Isolate* isolate, Handle<WasmTableObject> table,
                         uint32_t entry_index);

206 207 208
  static bool IsValidElement(Isolate* isolate, Handle<WasmTableObject> table,
                             Handle<Object> entry);

209 210 211
  V8_EXPORT_PRIVATE static void Set(Isolate* isolate,
                                    Handle<WasmTableObject> table,
                                    uint32_t index, Handle<Object> entry);
212

213 214 215
  V8_EXPORT_PRIVATE static Handle<Object> Get(Isolate* isolate,
                                              Handle<WasmTableObject> table,
                                              uint32_t index);
216

217 218 219 220
  V8_EXPORT_PRIVATE static void Fill(Isolate* isolate,
                                     Handle<WasmTableObject> table,
                                     uint32_t start, Handle<Object> entry,
                                     uint32_t count);
221

222
  // TODO(wasm): Unify these three methods into one.
223 224
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
225 226
                                   int entry_index,
                                   const wasm::FunctionSig* sig,
227 228
                                   Handle<WasmInstanceObject> target_instance,
                                   int target_func_index);
229 230 231 232
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
                                   int entry_index,
                                   Handle<WasmJSFunction> function);
233 234 235 236
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
                                   int entry_index,
                                   Handle<WasmCapiFunction> capi_function);
237

238 239
  static void ClearDispatchTables(Isolate* isolate,
                                  Handle<WasmTableObject> table, int index);
240

241 242 243
  V8_EXPORT_PRIVATE static void SetFunctionTablePlaceholder(
      Isolate* isolate, Handle<WasmTableObject> table, int entry_index,
      Handle<WasmInstanceObject> instance, int func_index);
244 245

  // This function reads the content of a function table entry and returns it
246 247 248
  // through the out parameters {is_valid}, {is_null}, {instance},
  // {function_index}, and {maybe_js_function}.
  static void GetFunctionTableEntry(
249 250 251
      Isolate* isolate, const wasm::WasmModule* module,
      Handle<WasmTableObject> table, int entry_index, bool* is_valid,
      bool* is_null, MaybeHandle<WasmInstanceObject>* instance,
252
      int* function_index, MaybeHandle<WasmJSFunction>* maybe_js_function);
253

254 255 256 257 258 259
 private:
  static void SetFunctionTableEntry(Isolate* isolate,
                                    Handle<WasmTableObject> table,
                                    Handle<FixedArray> entries, int entry_index,
                                    Handle<Object> entry);

260
  TQ_OBJECT_CONSTRUCTORS(WasmTableObject)
261 262 263
};

// Representation of a WebAssembly.Memory JavaScript-level object.
264 265
class WasmMemoryObject
    : public TorqueGeneratedWasmMemoryObject<WasmMemoryObject, JSObject> {
266
 public:
267
  DECL_OPTIONAL_ACCESSORS(instances, WeakArrayList)
268

269 270
  // Dispatched behavior.
  DECL_PRINTER(WasmMemoryObject)
271

272
  // Add an instance to the internal (weak) list.
273 274 275
  V8_EXPORT_PRIVATE static void AddInstance(Isolate* isolate,
                                            Handle<WasmMemoryObject> memory,
                                            Handle<WasmInstanceObject> object);
276
  inline bool has_maximum_pages();
277

278
  V8_EXPORT_PRIVATE static Handle<WasmMemoryObject> New(
279
      Isolate* isolate, MaybeHandle<JSArrayBuffer> buffer, int maximum);
280

281
  V8_EXPORT_PRIVATE static MaybeHandle<WasmMemoryObject> New(Isolate* isolate,
282 283
                                                             int initial,
                                                             int maximum,
284
                                                             SharedFlag shared);
285

286 287
  static constexpr int kNoMaximum = -1;

288 289
  void update_instances(Isolate* isolate, Handle<JSArrayBuffer> buffer);

290 291
  V8_EXPORT_PRIVATE static int32_t Grow(Isolate*, Handle<WasmMemoryObject>,
                                        uint32_t pages);
292

293
  TQ_OBJECT_CONSTRUCTORS(WasmMemoryObject)
294 295
};

296
// Representation of a WebAssembly.Global JavaScript-level object.
297 298
class WasmGlobalObject
    : public TorqueGeneratedWasmGlobalObject<WasmGlobalObject, JSObject> {
299
 public:
300 301
  DECL_ACCESSORS(untagged_buffer, JSArrayBuffer)
  DECL_ACCESSORS(tagged_buffer, FixedArray)
302 303
  DECL_PRIMITIVE_ACCESSORS(type, wasm::ValueType)

304 305
  // Dispatched behavior.
  DECL_PRINTER(WasmGlobalObject)
306

307
  V8_EXPORT_PRIVATE static MaybeHandle<WasmGlobalObject> New(
308 309
      Isolate* isolate, Handle<WasmInstanceObject> instance,
      MaybeHandle<JSArrayBuffer> maybe_untagged_buffer,
310
      MaybeHandle<FixedArray> maybe_tagged_buffer, wasm::ValueType type,
311
      int32_t offset, bool is_mutable);
312

313
  inline int type_size() const;
314 315

  inline int32_t GetI32();
316
  inline int64_t GetI64();
317 318
  inline float GetF32();
  inline double GetF64();
319
  inline Handle<Object> GetRef();
320 321

  inline void SetI32(int32_t value);
322
  inline void SetI64(int64_t value);
323 324
  inline void SetF32(float value);
  inline void SetF64(double value);
325
  inline void SetExternRef(Handle<Object> value);
326
  inline bool SetFuncRef(Isolate* isolate, Handle<Object> value);
327 328 329 330 331 332

 private:
  // This function returns the address of the global's data in the
  // JSArrayBuffer. This buffer may be allocated on-heap, in which case it may
  // not have a fixed address.
  inline Address address() const;
333

334
  TQ_OBJECT_CONSTRUCTORS(WasmGlobalObject)
335 336
};

337
// Representation of a WebAssembly.Instance JavaScript-level object.
338
class V8_EXPORT_PRIVATE WasmInstanceObject : public JSObject {
339
 public:
340 341 342 343 344 345 346 347 348
  DECL_CAST(WasmInstanceObject)

  DECL_ACCESSORS(module_object, WasmModuleObject)
  DECL_ACCESSORS(exports_object, JSObject)
  DECL_ACCESSORS(native_context, Context)
  DECL_OPTIONAL_ACCESSORS(memory_object, WasmMemoryObject)
  DECL_OPTIONAL_ACCESSORS(untagged_globals_buffer, JSArrayBuffer)
  DECL_OPTIONAL_ACCESSORS(tagged_globals_buffer, FixedArray)
  DECL_OPTIONAL_ACCESSORS(imported_mutable_globals_buffers, FixedArray)
349
  DECL_OPTIONAL_ACCESSORS(tables, FixedArray)
350
  DECL_OPTIONAL_ACCESSORS(indirect_function_tables, FixedArray)
351 352 353
  DECL_ACCESSORS(imported_function_refs, FixedArray)
  DECL_OPTIONAL_ACCESSORS(indirect_function_table_refs, FixedArray)
  DECL_OPTIONAL_ACCESSORS(managed_native_allocations, Foreign)
354
  DECL_OPTIONAL_ACCESSORS(tags_table, FixedArray)
355
  DECL_OPTIONAL_ACCESSORS(wasm_external_functions, FixedArray)
356
  DECL_ACCESSORS(managed_object_maps, FixedArray)
357
  DECL_PRIMITIVE_ACCESSORS(memory_start, byte*)
358 359
  DECL_PRIMITIVE_ACCESSORS(memory_size, size_t)
  DECL_PRIMITIVE_ACCESSORS(memory_mask, size_t)
360
  DECL_PRIMITIVE_ACCESSORS(isolate_root, Address)
361
  DECL_PRIMITIVE_ACCESSORS(stack_limit_address, Address)
362
  DECL_PRIMITIVE_ACCESSORS(real_stack_limit_address, Address)
363 364 365 366
  DECL_PRIMITIVE_ACCESSORS(new_allocation_limit_address, Address*)
  DECL_PRIMITIVE_ACCESSORS(new_allocation_top_address, Address*)
  DECL_PRIMITIVE_ACCESSORS(old_allocation_limit_address, Address*)
  DECL_PRIMITIVE_ACCESSORS(old_allocation_top_address, Address*)
367
  DECL_PRIMITIVE_ACCESSORS(imported_function_targets, Address*)
368
  DECL_PRIMITIVE_ACCESSORS(globals_start, byte*)
369
  DECL_PRIMITIVE_ACCESSORS(imported_mutable_globals, Address*)
370
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_size, uint32_t)
371 372
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_sig_ids, uint32_t*)
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_targets, Address*)
373
  DECL_PRIMITIVE_ACCESSORS(jump_table_start, Address)
374 375
  DECL_PRIMITIVE_ACCESSORS(data_segment_starts, Address*)
  DECL_PRIMITIVE_ACCESSORS(data_segment_sizes, uint32_t*)
376
  DECL_PRIMITIVE_ACCESSORS(dropped_elem_segments, byte*)
377
  DECL_PRIMITIVE_ACCESSORS(hook_on_function_call_address, Address)
378
  DECL_PRIMITIVE_ACCESSORS(num_liftoff_function_calls_array, uint32_t*)
379
  DECL_PRIMITIVE_ACCESSORS(break_on_entry, uint8_t)
380

381 382
  // Clear uninitialized padding space. This ensures that the snapshot content
  // is deterministic. Depending on the V8 build mode there could be no padding.
383 384
  V8_INLINE void clear_padding();

385 386 387 388
  // Dispatched behavior.
  DECL_PRINTER(WasmInstanceObject)
  DECL_VERIFIER(WasmInstanceObject)

389
// Layout description.
390
#define WASM_INSTANCE_OBJECT_FIELDS(V)                                    \
391
  /* Often-accessed fields go first to minimize generated code size. */   \
392 393 394 395 396 397
  /* Less than system pointer sized fields come first. */                 \
  V(kImportedFunctionRefsOffset, kTaggedSize)                             \
  V(kIndirectFunctionTableRefsOffset, kTaggedSize)                        \
  V(kIndirectFunctionTableSizeOffset, kUInt32Size)                        \
  /* Optional padding to align system pointer size fields */              \
  V(kOptionalPaddingOffset, POINTER_SIZE_PADDING(kOptionalPaddingOffset)) \
398 399 400 401 402 403 404 405 406 407 408 409
  V(kMemoryStartOffset, kSystemPointerSize)                               \
  V(kMemorySizeOffset, kSizetSize)                                        \
  V(kMemoryMaskOffset, kSizetSize)                                        \
  V(kStackLimitAddressOffset, kSystemPointerSize)                         \
  V(kImportedFunctionTargetsOffset, kSystemPointerSize)                   \
  V(kIndirectFunctionTableTargetsOffset, kSystemPointerSize)              \
  V(kIndirectFunctionTableSigIdsOffset, kSystemPointerSize)               \
  V(kGlobalsStartOffset, kSystemPointerSize)                              \
  V(kImportedMutableGlobalsOffset, kSystemPointerSize)                    \
  V(kIsolateRootOffset, kSystemPointerSize)                               \
  V(kJumpTableStartOffset, kSystemPointerSize)                            \
  /* End of often-accessed fields. */                                     \
410
  /* Continue with system pointer size fields to maintain alignment. */   \
411 412 413 414
  V(kNewAllocationLimitAddressOffset, kSystemPointerSize)                 \
  V(kNewAllocationTopAddressOffset, kSystemPointerSize)                   \
  V(kOldAllocationLimitAddressOffset, kSystemPointerSize)                 \
  V(kOldAllocationTopAddressOffset, kSystemPointerSize)                   \
415 416 417 418 419 420 421
  V(kRealStackLimitAddressOffset, kSystemPointerSize)                     \
  V(kDataSegmentStartsOffset, kSystemPointerSize)                         \
  V(kDataSegmentSizesOffset, kSystemPointerSize)                          \
  V(kDroppedElemSegmentsOffset, kSystemPointerSize)                       \
  V(kHookOnFunctionCallAddressOffset, kSystemPointerSize)                 \
  V(kNumLiftoffFunctionCallsArrayOffset, kSystemPointerSize)              \
  /* Less than system pointer size aligned fields are below. */           \
422 423 424 425 426 427 428
  V(kModuleObjectOffset, kTaggedSize)                                     \
  V(kExportsObjectOffset, kTaggedSize)                                    \
  V(kNativeContextOffset, kTaggedSize)                                    \
  V(kMemoryObjectOffset, kTaggedSize)                                     \
  V(kUntaggedGlobalsBufferOffset, kTaggedSize)                            \
  V(kTaggedGlobalsBufferOffset, kTaggedSize)                              \
  V(kImportedMutableGlobalsBuffersOffset, kTaggedSize)                    \
429
  V(kTablesOffset, kTaggedSize)                                           \
430
  V(kIndirectFunctionTablesOffset, kTaggedSize)                           \
431
  V(kManagedNativeAllocationsOffset, kTaggedSize)                         \
432
  V(kTagsTableOffset, kTaggedSize)                                        \
433
  V(kWasmExternalFunctionsOffset, kTaggedSize)                            \
434
  V(kManagedObjectMapsOffset, kTaggedSize)                                \
435 436 437
  V(kBreakOnEntryOffset, kUInt8Size)                                      \
  /* More padding to make the header pointer-size aligned */              \
  V(kHeaderPaddingOffset, POINTER_SIZE_PADDING(kHeaderPaddingOffset))     \
438
  V(kHeaderSize, 0)
439 440 441

  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
                                WASM_INSTANCE_OBJECT_FIELDS)
442
  STATIC_ASSERT(IsAligned(kHeaderSize, kTaggedSize));
443 444 445 446 447 448 449 450
  // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
  // fields (external pointers, doubles and BigInt data) are only kTaggedSize
  // aligned so checking for alignments of fields bigger than kTaggedSize
  // doesn't make sense until v8:8875 is fixed.
#define ASSERT_FIELD_ALIGNED(offset, size)                                 \
  STATIC_ASSERT(size == 0 || IsAligned(offset, size) ||                    \
                (COMPRESS_POINTERS_BOOL && (size == kSystemPointerSize) && \
                 IsAligned(offset, kTaggedSize)));
451 452
  WASM_INSTANCE_OBJECT_FIELDS(ASSERT_FIELD_ALIGNED)
#undef ASSERT_FIELD_ALIGNED
453
#undef WASM_INSTANCE_OBJECT_FIELDS
454

455
  static constexpr uint16_t kTaggedFieldOffsets[] = {
456 457
      kImportedFunctionRefsOffset,
      kIndirectFunctionTableRefsOffset,
458 459 460 461 462 463 464 465
      kModuleObjectOffset,
      kExportsObjectOffset,
      kNativeContextOffset,
      kMemoryObjectOffset,
      kUntaggedGlobalsBufferOffset,
      kTaggedGlobalsBufferOffset,
      kImportedMutableGlobalsBuffersOffset,
      kTablesOffset,
466
      kIndirectFunctionTablesOffset,
467
      kManagedNativeAllocationsOffset,
468
      kTagsTableOffset,
469 470
      kWasmExternalFunctionsOffset,
      kManagedObjectMapsOffset};
471

472
  const wasm::WasmModule* module();
473

474
  static bool EnsureIndirectFunctionTableWithMinimumSize(
475 476
      Handle<WasmInstanceObject> instance, int table_index,
      uint32_t minimum_size);
477

478
  void SetRawMemory(byte* mem_start, size_t mem_size);
479

480
  static Handle<WasmInstanceObject> New(Isolate*, Handle<WasmModuleObject>);
481

482 483
  Address GetCallTarget(uint32_t func_index);

484 485 486 487
  static int IndirectFunctionTableSize(Isolate* isolate,
                                       Handle<WasmInstanceObject> instance,
                                       uint32_t table_index);

488 489 490
  // Copies table entries. Returns {false} if the ranges are out-of-bounds.
  static bool CopyTableEntries(Isolate* isolate,
                               Handle<WasmInstanceObject> instance,
491 492
                               uint32_t table_dst_index,
                               uint32_t table_src_index, uint32_t dst,
493
                               uint32_t src,
494 495
                               uint32_t count) V8_WARN_UNUSED_RESULT;

496 497 498 499 500 501 502 503
  // Copy table entries from an element segment. Returns {false} if the ranges
  // are out-of-bounds.
  static bool InitTableEntries(Isolate* isolate,
                               Handle<WasmInstanceObject> instance,
                               uint32_t table_index, uint32_t segment_index,
                               uint32_t dst, uint32_t src,
                               uint32_t count) V8_WARN_UNUSED_RESULT;

504 505
  // Iterates all fields in the object except the untagged fields.
  class BodyDescriptor;
506

507
  static MaybeHandle<WasmExternalFunction> GetWasmExternalFunction(
508
      Isolate* isolate, Handle<WasmInstanceObject> instance, int index);
509

510
  // Acquires the {WasmExternalFunction} for a given {function_index} from the
511 512 513
  // cache of the given {instance}, or creates a new {WasmExportedFunction} if
  // it does not exist yet. The new {WasmExportedFunction} is added to the
  // cache of the {instance} immediately.
514 515 516
  static Handle<WasmExternalFunction> GetOrCreateWasmExternalFunction(
      Isolate* isolate, Handle<WasmInstanceObject> instance,
      int function_index);
517

518
  static void SetWasmExternalFunction(Isolate* isolate,
519 520
                                      Handle<WasmInstanceObject> instance,
                                      int index,
521
                                      Handle<WasmExternalFunction> val);
522

523 524 525 526 527
  // Imports a constructed {WasmJSFunction} into the indirect function table of
  // this instance. Note that this might trigger wrapper compilation, since a
  // {WasmJSFunction} is instance-independent and just wraps a JS callable.
  static void ImportWasmJSFunctionIntoTable(Isolate* isolate,
                                            Handle<WasmInstanceObject> instance,
528
                                            int table_index, int entry_index,
529 530
                                            Handle<WasmJSFunction> js_function);

531 532 533 534 535 536 537 538 539 540
  // Get a raw pointer to the location where the given global is stored.
  // {global} must not be a reference type.
  static uint8_t* GetGlobalStorage(Handle<WasmInstanceObject>,
                                   const wasm::WasmGlobal&);

  // Get the FixedArray and the index in that FixedArray for the given global,
  // which must be a reference type.
  static std::pair<Handle<FixedArray>, uint32_t> GetGlobalBufferAndIndex(
      Handle<WasmInstanceObject>, const wasm::WasmGlobal&);

541 542 543 544
  // Get the value of a global in the given instance.
  static wasm::WasmValue GetGlobalValue(Handle<WasmInstanceObject>,
                                        const wasm::WasmGlobal&);

545
  OBJECT_CONSTRUCTORS(WasmInstanceObject, JSObject);
546 547 548 549

 private:
  static void InitDataSegmentArrays(Handle<WasmInstanceObject>,
                                    Handle<WasmModuleObject>);
550 551
  static void InitElemSegmentArrays(Handle<WasmInstanceObject>,
                                    Handle<WasmModuleObject>);
552 553
};

554
// Representation of WebAssembly.Exception JavaScript-level object.
555 556
class WasmTagObject
    : public TorqueGeneratedWasmTagObject<WasmTagObject, JSObject> {
557
 public:
558
  // Dispatched behavior.
559
  DECL_PRINTER(WasmTagObject)
560

561
  // Checks whether the given {sig} has the same parameter types as the
562
  // serialized signature stored within this tag object.
563
  bool MatchesSignature(const wasm::FunctionSig* sig);
564

565 566 567
  static Handle<WasmTagObject> New(Isolate* isolate,
                                   const wasm::FunctionSig* sig,
                                   Handle<HeapObject> tag);
568

569
  TQ_OBJECT_CONSTRUCTORS(WasmTagObject)
570 571
};

572
// A Wasm exception that has been thrown out of Wasm code.
573
class V8_EXPORT_PRIVATE WasmExceptionPackage : public JSObject {
574
 public:
575 576 577
  static Handle<WasmExceptionPackage> New(
      Isolate* isolate, Handle<WasmExceptionTag> exception_tag,
      int encoded_size);
578

579 580 581 582
  static Handle<WasmExceptionPackage> New(
      Isolate* isolate, Handle<WasmExceptionTag> exception_tag,
      Handle<FixedArray> values);

583 584
  // The below getters return {undefined} in case the given exception package
  // does not carry the requested values (i.e. is of a different type).
585 586 587 588
  static Handle<Object> GetExceptionTag(
      Isolate* isolate, Handle<WasmExceptionPackage> exception_package);
  static Handle<Object> GetExceptionValues(
      Isolate* isolate, Handle<WasmExceptionPackage> exception_package);
589 590

  // Determines the size of the array holding all encoded exception values.
591
  static uint32_t GetEncodedSize(const wasm::WasmTag* tag);
592 593

  DECL_CAST(WasmExceptionPackage)
594
  OBJECT_CONSTRUCTORS(WasmExceptionPackage, JSObject);
595 596
};

597 598 599 600 601 602
void V8_EXPORT_PRIVATE EncodeI32ExceptionValue(
    Handle<FixedArray> encoded_values, uint32_t* encoded_index, uint32_t value);

void V8_EXPORT_PRIVATE EncodeI64ExceptionValue(
    Handle<FixedArray> encoded_values, uint32_t* encoded_index, uint64_t value);

603 604 605 606 607 608 609 610
void V8_EXPORT_PRIVATE
DecodeI32ExceptionValue(Handle<FixedArray> encoded_values,
                        uint32_t* encoded_index, uint32_t* value);

void V8_EXPORT_PRIVATE
DecodeI64ExceptionValue(Handle<FixedArray> encoded_values,
                        uint32_t* encoded_index, uint64_t* value);

611
// A Wasm function that is wrapped and exported to JavaScript.
612
// Representation of WebAssembly.Function JavaScript-level object.
613 614
class WasmExportedFunction : public JSFunction {
 public:
615
  WasmInstanceObject instance();
616
  V8_EXPORT_PRIVATE int function_index();
617

618
  V8_EXPORT_PRIVATE static bool IsWasmExportedFunction(Object object);
619

620
  V8_EXPORT_PRIVATE static Handle<WasmExportedFunction> New(
621 622
      Isolate* isolate, Handle<WasmInstanceObject> instance, int func_index,
      int arity, Handle<Code> export_wrapper);
623

624
  Address GetWasmCallTarget();
625

626
  V8_EXPORT_PRIVATE const wasm::FunctionSig* sig();
627

628 629 630
  bool MatchesSignature(const wasm::WasmModule* other_module,
                        const wasm::FunctionSig* other_sig);

631 632 633 634
  // Return a null-terminated string with the debug name in the form
  // 'js-to-wasm:<sig>'.
  static std::unique_ptr<char[]> GetDebugName(const wasm::FunctionSig* sig);

635
  DECL_CAST(WasmExportedFunction)
636
  OBJECT_CONSTRUCTORS(WasmExportedFunction, JSFunction);
637 638
};

639 640 641 642 643 644
// A Wasm function that was created by wrapping a JavaScript callable.
// Representation of WebAssembly.Function JavaScript-level object.
class WasmJSFunction : public JSFunction {
 public:
  static bool IsWasmJSFunction(Object object);

645 646
  static Handle<WasmJSFunction> New(Isolate* isolate,
                                    const wasm::FunctionSig* sig,
647 648
                                    Handle<JSReceiver> callable);

649
  JSReceiver GetCallable() const;
650 651
  // Deserializes the signature of this function using the provided zone. Note
  // that lifetime of the signature is hence directly coupled to the zone.
652 653
  const wasm::FunctionSig* GetSignature(Zone* zone);
  bool MatchesSignature(const wasm::FunctionSig* sig);
654

655 656 657 658
  DECL_CAST(WasmJSFunction)
  OBJECT_CONSTRUCTORS(WasmJSFunction, JSFunction);
};

659 660 661 662 663 664
// An external function exposed to Wasm via the C/C++ API.
class WasmCapiFunction : public JSFunction {
 public:
  static bool IsWasmCapiFunction(Object object);

  static Handle<WasmCapiFunction> New(
665
      Isolate* isolate, Address call_target, Handle<Foreign> embedder_data,
666 667 668 669 670
      Handle<PodArray<wasm::ValueType>> serialized_signature);

  PodArray<wasm::ValueType> GetSerializedSignature() const;
  // Checks whether the given {sig} has the same parameter types as the
  // serialized signature stored within this C-API function object.
671
  bool MatchesSignature(const wasm::FunctionSig* sig) const;
672 673 674 675 676

  DECL_CAST(WasmCapiFunction)
  OBJECT_CONSTRUCTORS(WasmCapiFunction, JSFunction);
};

677 678 679 680 681 682 683 684 685 686 687 688 689
// Any external function that can be imported/exported in modules. This abstract
// class just dispatches to the following concrete classes:
//  - {WasmExportedFunction}: A proper Wasm function exported from a module.
//  - {WasmJSFunction}: A function constructed via WebAssembly.Function in JS.
// // TODO(wasm): Potentially {WasmCapiFunction} will be added here as well.
class WasmExternalFunction : public JSFunction {
 public:
  static bool IsWasmExternalFunction(Object object);

  DECL_CAST(WasmExternalFunction)
  OBJECT_CONSTRUCTORS(WasmExternalFunction, JSFunction);
};

690 691 692
class WasmIndirectFunctionTable
    : public TorqueGeneratedWasmIndirectFunctionTable<WasmIndirectFunctionTable,
                                                      Struct> {
693 694 695 696 697
 public:
  DECL_PRIMITIVE_ACCESSORS(sig_ids, uint32_t*)
  DECL_PRIMITIVE_ACCESSORS(targets, Address*)
  DECL_OPTIONAL_ACCESSORS(managed_native_allocations, Foreign)

698 699
  V8_EXPORT_PRIVATE static Handle<WasmIndirectFunctionTable> New(
      Isolate* isolate, uint32_t size);
700 701
  static void Resize(Isolate* isolate, Handle<WasmIndirectFunctionTable> table,
                     uint32_t new_size);
702 703 704 705 706 707

  DECL_PRINTER(WasmIndirectFunctionTable)

  STATIC_ASSERT(kStartOfStrongFieldsOffset == kManagedNativeAllocationsOffset);
  using BodyDescriptor = FlexibleBodyDescriptor<kStartOfStrongFieldsOffset>;

708
  TQ_OBJECT_CONSTRUCTORS(WasmIndirectFunctionTable)
709 710
};

711 712 713 714
class WasmFunctionData
    : public TorqueGeneratedWasmFunctionData<WasmFunctionData, Foreign> {
 public:
  DECL_ACCESSORS(ref, Object)
715
  DECL_ACCESSORS(wrapper_code, Code)
716 717 718 719 720 721

  DECL_PRINTER(WasmFunctionData)

  TQ_OBJECT_CONSTRUCTORS(WasmFunctionData)
};

722 723 724
// Information for a WasmExportedFunction which is referenced as the function
// data of the SharedFunctionInfo underlying the function. For details please
// see the {SharedFunctionInfo::HasWasmExportedFunctionData} predicate.
725 726 727
class WasmExportedFunctionData
    : public TorqueGeneratedWasmExportedFunctionData<WasmExportedFunctionData,
                                                     WasmFunctionData> {
728
 public:
729 730
  inline wasm::FunctionSig* sig() const;

731 732 733 734
  // Dispatched behavior.
  DECL_PRINTER(WasmExportedFunctionData)
  DECL_VERIFIER(WasmExportedFunctionData)

735 736
  class BodyDescriptor;

737
  TQ_OBJECT_CONSTRUCTORS(WasmExportedFunctionData)
738 739
};

740 741 742
// Information for a WasmJSFunction which is referenced as the function data of
// the SharedFunctionInfo underlying the function. For details please see the
// {SharedFunctionInfo::HasWasmJSFunctionData} predicate.
743 744 745
class WasmJSFunctionData
    : public TorqueGeneratedWasmJSFunctionData<WasmJSFunctionData,
                                               WasmFunctionData> {
746
 public:
747
  DECL_ACCESSORS(wasm_to_js_wrapper_code, Code)
748 749 750 751

  // Dispatched behavior.
  DECL_PRINTER(WasmJSFunctionData)

752 753
  class BodyDescriptor;

754 755 756
 private:
  DECL_ACCESSORS(raw_wasm_to_js_wrapper_code, CodeT)

757
  TQ_OBJECT_CONSTRUCTORS(WasmJSFunctionData)
758 759
};

760 761 762
class WasmCapiFunctionData
    : public TorqueGeneratedWasmCapiFunctionData<WasmCapiFunctionData,
                                                 WasmFunctionData> {
763 764 765 766 767
 public:
  DECL_PRINTER(WasmCapiFunctionData)

  class BodyDescriptor;

768
  TQ_OBJECT_CONSTRUCTORS(WasmCapiFunctionData)
769 770
};

771 772
class WasmScript : public AllStatic {
 public:
773 774 775 776
  // Position used for storing "on entry" breakpoints (a.k.a. instrumentation
  // breakpoints). This would be an illegal position for any other breakpoint.
  static constexpr int kOnEntryBreakpointPosition = -1;

777 778 779 780 781 782 783 784 785
  // Set a breakpoint on the given byte position inside the given module.
  // This will affect all live and future instances of the module.
  // The passed position might be modified to point to the next breakable
  // location inside the same function.
  // If it points outside a function, or behind the last breakable location,
  // this function returns false and does not set any breakpoint.
  V8_EXPORT_PRIVATE static bool SetBreakPoint(Handle<Script>, int* position,
                                              Handle<BreakPoint> break_point);

786 787 788 789 790 791
  // Set an "on entry" breakpoint (a.k.a. instrumentation breakpoint) inside
  // the given module. This will affect all live and future instances of the
  // module.
  V8_EXPORT_PRIVATE static void SetBreakPointOnEntry(
      Handle<Script>, Handle<BreakPoint> break_point);

792 793 794 795 796 797 798 799 800 801 802 803 804
  // Set a breakpoint on first breakable position of the given function index
  // inside the given module. This will affect all live and future instances of
  // the module.
  V8_EXPORT_PRIVATE static bool SetBreakPointOnFirstBreakableForFunction(
      Handle<Script>, int function_index, Handle<BreakPoint> break_point);

  // Set a breakpoint at the breakable offset of the given function index
  // inside the given module. This will affect all live and future instances of
  // the module.
  V8_EXPORT_PRIVATE static bool SetBreakPointForFunction(
      Handle<Script>, int function_index, int breakable_offset,
      Handle<BreakPoint> break_point);

805 806 807 808 809
  // Remove a previously set breakpoint at the given byte position inside the
  // given module. If this breakpoint is not found this function returns false.
  V8_EXPORT_PRIVATE static bool ClearBreakPoint(Handle<Script>, int position,
                                                Handle<BreakPoint> break_point);

810 811 812 813 814
  // Remove a previously set breakpoint by id. If this breakpoint is not found,
  // returns false.
  V8_EXPORT_PRIVATE static bool ClearBreakPointById(Handle<Script>,
                                                    int breakpoint_id);

815 816 817
  // Remove all set breakpoints.
  static void ClearAllBreakpoints(Script);

818 819 820 821 822 823 824 825
  // Get a list of all possible breakpoints within a given range of this module.
  V8_EXPORT_PRIVATE static bool GetPossibleBreakpoints(
      wasm::NativeModule* native_module, const debug::Location& start,
      const debug::Location& end, std::vector<debug::BreakLocation>* locations);

  // Return an empty handle if no breakpoint is hit at that location, or a
  // FixedArray with all hit breakpoint objects.
  static MaybeHandle<FixedArray> CheckBreakPoints(Isolate*, Handle<Script>,
826 827
                                                  int position,
                                                  StackFrameId stack_frame_id);
828 829 830 831 832 833 834

 private:
  // Helper functions that update the breakpoint info list.
  static void AddBreakpointToInfo(Handle<Script>, int position,
                                  Handle<BreakPoint> break_point);
};

835 836
// Tags provide an object identity for each exception defined in a wasm module
// header. They are referenced by the following fields:
837 838
//  - {WasmTagObject::tag}: The tag of the {Tag} object.
//  - {WasmInstanceObject::tags_table}: List of tags used by an instance.
839 840
class WasmExceptionTag
    : public TorqueGeneratedWasmExceptionTag<WasmExceptionTag, Struct> {
841
 public:
842 843
  V8_EXPORT_PRIVATE static Handle<WasmExceptionTag> New(Isolate* isolate,
                                                        int index);
844 845 846

  DECL_PRINTER(WasmExceptionTag)

847
  TQ_OBJECT_CONSTRUCTORS(WasmExceptionTag)
848 849
};

850 851
// Data annotated to the asm.js Module function. Used for later instantiation of
// that function.
852
class AsmWasmData : public TorqueGeneratedAsmWasmData<AsmWasmData, Struct> {
853 854 855
 public:
  static Handle<AsmWasmData> New(
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
856
      Handle<FixedArray> export_wrappers, Handle<HeapNumber> uses_bitset);
857 858 859

  DECL_PRINTER(AsmWasmData)

860
  TQ_OBJECT_CONSTRUCTORS(AsmWasmData)
861 862
};

863 864
class WasmTypeInfo : public TorqueGeneratedWasmTypeInfo<WasmTypeInfo, Foreign> {
 public:
865 866
  inline void clear_foreign_address(Isolate* isolate);

867 868 869 870 871 872 873
  DECL_PRINTER(WasmTypeInfo)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmTypeInfo)
};

874
class WasmObject : public TorqueGeneratedWasmObject<WasmObject, JSReceiver> {
875
 public:
876 877 878 879
  // Prepares given value for being stored into a field of given Wasm type.
  V8_WARN_UNUSED_RESULT static inline MaybeHandle<Object> ToWasmValue(
      Isolate* isolate, wasm::ValueType type, Handle<Object> value);

880 881 882 883 884 885 886 887
 protected:
  // Returns boxed value of the object's field/element with given type and
  // offset.
  static inline Handle<Object> ReadValueAt(Isolate* isolate,
                                           Handle<HeapObject> obj,
                                           wasm::ValueType type,
                                           uint32_t offset);

888 889 890 891 892 893 894 895
  static inline void WriteValueAt(Isolate* isolate, Handle<HeapObject> obj,
                                  wasm::ValueType type, uint32_t offset,
                                  Handle<Object> value);

 private:
  template <typename ElementType>
  static ElementType FromNumber(Object value);

896
  TQ_OBJECT_CONSTRUCTORS(WasmObject)
897 898 899
};

class WasmStruct : public TorqueGeneratedWasmStruct<WasmStruct, WasmObject> {
900 901 902
 public:
  static inline wasm::StructType* type(Map map);
  inline wasm::StructType* type() const;
903
  static inline wasm::StructType* GcSafeType(Map map);
904 905
  static inline int Size(const wasm::StructType* type);
  static inline int GcSafeSize(Map map);
906

907 908 909 910
  // Returns the address of the field at given offset.
  inline Address RawFieldAddress(int raw_offset);

  // Returns the ObjectSlot for tagged value at given offset.
911 912
  inline ObjectSlot RawField(int raw_offset);

913 914
  wasm::WasmValue GetFieldValue(uint32_t field_index);

915 916 917 918 919
  // Returns boxed value of the object's field.
  static inline Handle<Object> GetField(Isolate* isolate,
                                        Handle<WasmStruct> obj,
                                        uint32_t field_index);

920 921 922
  static inline void SetField(Isolate* isolate, Handle<WasmStruct> obj,
                              uint32_t field_index, Handle<Object> value);

923 924 925 926 927 928 929
  DECL_PRINTER(WasmStruct)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmStruct)
};

930
class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> {
931 932 933 934 935
 public:
  static inline wasm::ArrayType* type(Map map);
  inline wasm::ArrayType* type() const;
  static inline wasm::ArrayType* GcSafeType(Map map);

936 937 938
  // Get the {ObjectSlot} corresponding to the element at {index}. Requires that
  // this is a reference array.
  ObjectSlot ElementSlot(uint32_t index);
939 940
  wasm::WasmValue GetElement(uint32_t index);

941
  static inline int SizeFor(Map map, int length);
942
  static inline int GcSafeSizeFor(Map map, int length);
943

944 945 946 947 948
  // Returns boxed value of the array's element.
  static inline Handle<Object> GetElement(Isolate* isolate,
                                          Handle<WasmArray> array,
                                          uint32_t index);

949 950 951
  // Returns the Address of the element at {index}.
  Address ElementAddress(uint32_t index);

952 953 954 955 956 957 958
  DECL_PRINTER(WasmArray)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmArray)
};

959 960
#undef DECL_OPTIONAL_ACCESSORS

961 962 963
namespace wasm {

Handle<Map> CreateStructMap(Isolate* isolate, const WasmModule* module,
964 965
                            int struct_index, MaybeHandle<Map> rtt_parent,
                            Handle<WasmInstanceObject> instance);
966
Handle<Map> CreateArrayMap(Isolate* isolate, const WasmModule* module,
967 968
                           int array_index, MaybeHandle<Map> rtt_parent,
                           Handle<WasmInstanceObject> instance);
969 970
Handle<Map> AllocateSubRtt(Isolate* isolate,
                           Handle<WasmInstanceObject> instance, uint32_t type,
971
                           Handle<Map> parent, WasmRttSubMode mode);
972

973 974 975
bool TypecheckJSObject(Isolate* isolate, const WasmModule* module,
                       Handle<Object> value, ValueType expected,
                       const char** error_message);
976 977
}  // namespace wasm

978 979 980
}  // namespace internal
}  // namespace v8

981 982
#include "src/objects/object-macros-undef.h"

983
#endif  // V8_WASM_WASM_OBJECTS_H_